User memory manager with local thread storage

Our program has a user memory manager, all our malloc / free calls are controlled by the memory manager, but at the beginning of the program getpwuid () will be called, and on some computers of clients with activated nss_ldap it will call malloc from libc not from our memory manager, which leads to error in our memory manager, stack report from gdb:

Breakpoint 2, 0x0000003df8cc6eb0 in brk () from /lib64/libc.so.6 0 0x0000003df8cc6eb0 in brk () from /lib64/libc.so.6 1 0x0000003df8cc6f72 in sbrk () from /lib64/libc.so.6 2 0x0000003df8c73d29 in __default_morecore () from /lib64/libc.so.6 3 0x0000003df8c70090 in _int_malloc () from /lib64/libc.so.6 4 0x0000003df8c70c9d in malloc () from /lib64/libc.so.6 5 0x0000003df880fc65 in __tls_get_addr () from /lib64/ld-linux-x86-64.so.2 6 0x00002aaaae302a7c in _nss_ldap_inc_depth () from /lib64/libnss_ldap.so.2 7 0x00002aaaae2f91a4 in _nss_ldap_enter () from /lib64/libnss_ldap.so.2 8 0x00002aaaae2f942c in _nss_ldap_getbyname () from /lib64/libnss_ldap.so.2 9 0x00002aaaae2f9aa9 in _nss_ldap_getpwuid_r () from /lib64/libnss_ldap.so.2 10 0x0000003df8c947c5 in getpwuid_r@ @GLIBC_2.2.5 () from /lib64/libc.so.6 11 0x0000003df8c9412f in getpwuid () from /lib64/libc.so.6 12 0x0000000001414be3 in lc_username () 

I have drawn the code _nss_ldap_inc_depth (), it seems that the __tls_get_addr () call got the call because the local storage is used, I try to change the memory manager to a shared library, but __tls_get_addr () still calls malloc from libc, how can I make it call our memory manager instead of libc ??

+6
source share
2 answers

You can use LD_PRELOAD to load your library before any other library (including glibc), and it will be linked, and something like:

 $ LD_PRELOAD=/path/to/library/libmymalloc.so /bin/myprog 

There's a tutorial here that shows how it works, it even has an example of a nested malloc

+2
source

You can change the memory manager to use mmap instread brk .

There can only be one brk user per process. Therefore, if you have not replaced all calls with malloc and related functions ( calloc , strdup , etc.), you should not use brk .

mmap , however, does not have such problems. The memory manager can use mmap , and malloc can work in parallel.

0
source

All Articles