1

For an extremely light version of ASAN, I would like to intercept calls to malloc, free, calloc and others. I'm doing this because for my use-case, even ASAN is too slow.

My approach would be creating an LD_PRELOAD library which replaces e.g. malloc(), performs checks and then calls the 'real malloc'. To be able to call the 'real malloc', one would use dlsym to obtain a pointer to that function. For malloc, free and realloc that works fine.

But: calloc is a problem. That is because dlsym() uses calloc underneath. Now in theory I could just use the 'real malloc' with a memset, but I'm afraid that that would affect timing (by becoming considerably slower) too much - calloc does some clever things underneath.

So my question is: how can I replace calloc without causing an infinite loop with the first dlsym() I execute?

Folkert van Heusden
  • 433
  • 4
  • 17
  • 38
  • 1
    Your only issue is the first few calls to calloc. Maybe for those, before the initialization, you could have a static buffer that you give access to using a naive allocator, and then switch back to the C implementation as soon as you have successfully dlsym'ed the original symbols ? – Vincent Fourmond Dec 08 '20 at 10:49
  • 1
    What is ASAN ?. – Mad Physicist Dec 08 '20 at 10:55
  • 1
    Still would be nice to expand [the acronym ASan](https://en.wikipedia.org/wiki/AddressSanitizer) Also see [replacing malloc in glibc](https://www.gnu.org/software/libc/manual/html_node/Replacing-malloc.html). you may use `--wrap=malloc` or `__malloc_hook` and stuff and you can also replace `__libc_malloc` symbols. Can you modify/recompile the program or do you want to do that with any program? – KamilCuk Dec 08 '20 at 11:04
  • Can't you call `sbrk`, `mmap` or whatever is preferred in Linux directly? – Lundin Dec 08 '20 at 11:43

0 Answers0