Mtrace shows memory leak because of its own __cxa_atexit call

Janito Vaqueiro Ferreira Filho janito.vff@gmail.com
Fri May 1 18:25:00 GMT 2015


Hello,

I'm a noob here, but I've found a behaviour in mtrace that I found odd. 
If a certain number of atexit calls are placed before mtrace() is 
called, an additional memory allocation is registered and is reported as 
a leak by the auxiliary command line tool.

I found out that this happens because mtrace configures the malloc hooks 
before it calls __cxa_atexit, and this function may allocate memory for 
storing new function references. From what I understood by reading 
stdlib.h/cxa_atexit.c, __new_exitfn allocates space when registering the 
33rd function reference.

I created a simple test.c file (attached) to test it, and it seems to 
work if I perform 31 calls to atexit before calling mtrace().

I attempted to "fix" the problem by moving the order of operations in 
the mtrace function. I've attached a patch that only sets the hooks 
after the __cxa_atexit call. It seems to have solved my problem, but I'm 
unsure if there are other consequences I can't foresee.

Would this "fix" be acceptable, or would it be better if I worked around 
the problem in my client program. If a workaround is better, what would 
be a good approach? I'm thinking of perhaps calling mtrace() followed by 
muntrace() at the start of the program once to make sure the 
__cxa_atexit function is called, and consequently preventing the memory 
leak from appearing in future calls of mtrace().

Thanks in advance for any feedback,

Janito

PS: To test it, I ran 'gcc test.c && ./a.out && cat /tmp/mtrace_test'

PS2: I made the patch from the 'glibc-2.21' tag, but I also tested it 
with the master branch at commit 'c5a3a509dfddad86b9fef84b46a172f969cb9b4b'
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.c
Type: text/x-csrc
Size: 330 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/libc-help/attachments/20150501/ae6239df/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Move-mtrace-exit-handler-registration.patch
Type: text/x-patch
Size: 1606 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/libc-help/attachments/20150501/ae6239df/attachment-0001.bin>


More information about the Libc-help mailing list