I am trying to provide memory wrappers on CentOS and use the clang compiler / linker. I wrote wrappers for distribution functions (malloc, etc.) and redirected calls using -Wl, -wrap, malloc.
It all works great, and I see it in action.
void* mem = malloc(10); // routes to __wrap_malloc
free(mem);// routes to __wrap_free
However, the problem I see is that any memory allocated in libc is not routed to my shell, but the application makes a free call that gets intercepted (and, as a result, crashes). For instance,
char* newStr = strdup("foo"); // The internal malloc in libcdoes not come to wrapper
free(newStr); // The free call makes it to the wrapper
My program is in C ++. I created mallocimpl.cpp and did something like
extern "C"{
void* __wrap_malloc(size_t size)
{
// Route memory via custom memory allocator
}
//Similarly, __wrap_calloc, __wrap_realloc, __wrap_memalign and __wrap_free
Any ideas what I'm doing wrong? Do you need any special compiler / linker flags?
Thanks in advance.