Skip to content

Instantly share code, notes, and snippets.

@sharth
Created November 16, 2010 14:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharth/701897 to your computer and use it in GitHub Desktop.
Save sharth/701897 to your computer and use it in GitHub Desktop.
/* Example of a library interposer: interpose on malloc().
* gcc -D_GNU_SOURCE -fPIC -O2 -Wall -o malloc_interposer.so -shared malloc_interposer.c -ldl
* setenv LD_PRELOAD $cwd/malloc_interposer.so
* run the app
*/
#include <stdio.h>
#include <dlfcn.h>
#include <string.h>
void *malloc(size_t size) {
static void * (*func)();
if(!func)
func = (void *(*)()) dlsym(RTLD_NEXT, "malloc");
void *mem = func(size);
if (mem)
memset(mem, 0, size);
return mem;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment