Re: [AD] Memory debuggers

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Sven Sandberg <ssven.geo@xxxxxxxxxx> writes:
> I decided to test it on Allegro. Guess what? It immediately found two 
> memory leaks, one in demo.c and one in istretch.c (I'll send a patch soon 
> to fix this).

There are actually several places in Allegro that don't bother freeing 
memory, beacuse it was awkward to hook this into any clean part of 
the shutdown process. This is only a problem if memory is being repeatedly 
allocated and then forgotten (ie. leaked), rather than just one buffer that 
is resized and reused as required.

> OK, to my point: Fortify works by doing '#define malloc Fortify_malloc'
> and simliar things for realloc, free etc.
[...]
> Allegro could quite easily be fixed to allow this though, if we made all 
> Allegro code use function pointers when calling malloc, calloc, realloc 
> and free.

Does Fortify simply redefine the names of the functions, or does it also add 
some hidden parameters to tell what source file and line number the 
allocation came from?

If it is just a name redefinition, the GNU linker has a wrappers feature 
which is particularly useful for this sort of thing, and avoids any need 
for recompiling source code at all. You pass the option "--wrap malloc" to 
the linker (if using gcc to invoke ld in the normal way, this needs to be 
escaped eg. "gcc -Wl,--wrap,malloc"). This will replace any calls to 
malloc() with calls to __wrap_malloc(), and any calls to __real_malloc() 
with calls to malloc(). So you can have a __wrap_malloc() function which is 
used by the memory checker, and then calls __real_malloc() when it wants to 
do the genuine allocation.

Alternatively, can't you just rebuild Allegro including the memory checker? 
You don't even need to alter the header to do that, just run something like:

   make CFLAGS="-Dmalloc=Fortify_malloc -Dfree=Fortify_free"

I'm reluctant to pointerificate all the memory allocations unless there is 
truly no other way to do this. After all, one of the main advantages of 
having the source code is that you can trivially recompile it to support 
any such different configurations that you might need!


--
Shawn Hargreaves - shawn@xxxxxxxxxx - http://www.talula.demon.co.uk/
"A binary is barely software: it's more like hardware on a floppy disk."



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/