Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Memory allocations within libc are not routed to my allocation wrappers?

I am trying to provide memory wrappers on CentOS and using clang compiler/linker. I wrote wrappers for the allocation functions (malloc et al) and rerouted the calls using -Wl,-wrap,malloc.

This all works fine and I can see it in action. void* mem = malloc(10); // routes to __wrap_malloc free(mem);// routes to __wrap_free

However, the problem I am seeing is that any memory allocated within libc is not being routed to my wrapper but the application is making the free call that gets intercepted (and crash as a result). For example,

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 a 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 am doing wrong? Do I need any special compiler/linker flags?

Thanks in advance.

like image 644
mast3r_of_univ3rs3 Avatar asked Mar 15 '18 23:03

mast3r_of_univ3rs3


People also ask

How does memory allocation work in C?

How does Memory Allocation work in C? In C language, static and dynamic memory allocation is also known as stack memory and heap memory which are allocated during compile time and run time, respectively. 1. Static Memory Allocation

What is static and dynamic memory allocation in C language?

In C language, static and dynamic memory allocation is also known as stack memory and heap memory which are allocated during compile time and run time, respectively. 1. Static Memory Allocation As we discussed static memory allocation is the allocation of memory for the data variables when the computer programs start.

What is the difference between malloc () and calloc () in dynamic memory allocation?

In Dynamic memory allocation, malloc () and calloc () function only allocates memory but cannot free the memory on their own so this is done using free () method explicitly to release the memory that is not in use to avoid memory leakage.

What is the use of realloc () function in dynamic memory allocation?

As the name suggests, in dynamic memory allocation if suppose a user wants to allocate more memory which means more memory than specified or required by the program then we can use this realloc () function to alter the size of memory that was allocated previously. Suppose if we want to change the size of memory from 200 bytes to 600 bytes.


1 Answers

The recommended way to replace the glibc malloc implementation is ELF symbol interposition:

  • Replacing malloc

This way, you do not have to recompile everything, including glibc, and your malloc replacement will still be called once glibc removes the malloc hooks.

The __wrap approach does not work without recompiling (or at least rewriting) everything because all other libraries (including glibc) will use non-wrapped symbols.

like image 115
Florian Weimer Avatar answered Sep 28 '22 06:09

Florian Weimer