Skip to content

Instantly share code, notes, and snippets.

@drrost
Created September 14, 2020 13:27
Show Gist options
  • Save drrost/bb33af18253ee4e81c330fd45d300551 to your computer and use it in GitHub Desktop.
Save drrost/bb33af18253ee4e81c330fd45d300551 to your computer and use it in GitHub Desktop.
static void *(*real_malloc)(unsigned long) = 0; // 1
int malloc_counter = 0; // 2
static void malloc_init(void) { // 3
real_malloc = (void *(*)(unsigned long))dlsym(RTLD_NEXT, "malloc");
if (real_malloc == 0)
fprintf(stderr, "Error in `dlsym`: %s\n", dlerror());
}
void *malloc(unsigned long size) { // 4
if (real_malloc == 0)
malloc_init(); // 5
malloc_counter++; // 6
return real_malloc(size); // 7
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment