4

writing my own version of malloc() (and his best friend Mr. free()), I need to know if I free'd my memory correctly. Seems that I can't use valgrind because it checks for the libc'malloc, and mine is in a shared library loaded with LD_PRELOAD.

Currently if I use valgrind it says "0alloc 0free 0leak, everything is good", because it does not detect the allocations I did with MY malloc. (I use (s)brk() functions)

Does someone know if there's a way to check with valgrind anyways or with sth else ?

Thanks !

1 Answers1

1

Looks like there are two possible solutions:

--soname-synonyms

If your custom allocator functions are still called malloc() and free(), use the --soname-synonyms command line parameter to tell Valgrind to look for these functions in your preloaded library. It can then automatically intercept calls to these functions. Example:

--soname-synonyms=somalloc=mymalloclib.so

See http://valgrind.org/docs/manual/manual-core.html#manual-core.rareopts for details.

Client Requests

If you have a more complicated custom allocator (with an API different from malloc()/free()), you can use special "client requests" in your malloc()/free() implementation to tell Valgrind about the structure of your memory pool. See http://valgrind.org/docs/manual/mc-manual.html#mc-manual.mempools for details.

I haven't tested either of these solutions :-) so would be nice to hear whether it actually works for you.

oliver
  • 6,204
  • 9
  • 46
  • 50
  • Thanks I'll try this when I will have finished my free() function :) Otherwise to use valgrind drives me to a "Out of Space" ^^ – user3210859 Feb 05 '14 at 12:14
  • --soname-synonyms seems to not works as expected. Still have 0alloc/free/leak when tryin this option: --soname-synonyms=syn1=pattern1,syn2=pattern2,... – user3210859 Feb 05 '14 at 13:42
  • What is the name of your preloaded library? I think you only need to specify the --soname-synonyms option once, like this: --soname-synonyms=somalloc=. The "somalloc" part must not be changed (IIUC it's a keyword by Valgrind to indicate all malloc-related functions at once). – oliver Feb 06 '14 at 11:28
  • Here is what I tried : "LD_PRELOAD=$PWD/libmy_malloc.so valgrind --soname-synonyms=somalloc=libmy_malloc.so ls" this is a simple example with 'ls' command which works. But nothing about the allocations in valgrind :/ – user3210859 Feb 06 '14 at 13:56
  • 2
    You could try running Valgrind with "-v" switch to get more details on its startup. Also, according to http://valgrind.10908.n7.nabble.com/soname-synonym-replace-libcmalloc-td43829.html it is necessary to build your library with `-Wl,-soname,libmy_malloc.so` to set the name of the shared library. – oliver Feb 06 '14 at 14:46
  • Yes I tried this too but it still not works :/ example of a compilation line : "gcc -W -Wall -Wextra -ansi -pedantic -fPIC -Iinclude -Wl,-soname,libmy_malloc.so -c -o malloc.o malloc.c" – user3210859 Feb 06 '14 at 15:00
  • 1
    I suppose you need to specify the -soname option when compiling (actually linking) the libmy_malloc.so file, not when compiling the malloc.o file. – oliver Feb 06 '14 at 15:03