Valgrind can’t work with sanitizers together

Valgrind can’t work with sanitizers together. Check following program with explicit memory leak:

# cat memory-leak.c
#include <stdlib.h>
void *p;
int main() {
  p = malloc(7);
  p = 0; // The memory is leaked here.
  return 0;
}

Build it and run with valgrind:

# gcc memory-leak.c -o memory-leak
# valgrind ./memory-leak
==1155== Memcheck, a memory error detector
==1155== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1155== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==1155== Command: ./memory-leak
==1155==
==1155==
==1155== HEAP SUMMARY:
==1155==     in use at exit: 7 bytes in 1 blocks
==1155==   total heap usage: 1 allocs, 0 frees, 7 bytes allocated
==1155==
==1155== LEAK SUMMARY:
==1155==    definitely lost: 7 bytes in 1 blocks
==1155==    indirectly lost: 0 bytes in 0 blocks
==1155==      possibly lost: 0 bytes in 0 blocks
==1155==    still reachable: 0 bytes in 0 blocks
==1155==         suppressed: 0 bytes in 0 blocks
==1155== Rerun with --leak-check=full to see details of leaked memory
==1155==
==1155== For lists of detected and suppressed errors, rerun with: -s
==1155== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

The memory leak is detected successfully. Build the program with “-fsanitize=address” option and run valgrind again:

# gcc -fsanitize=address memory-leak.c -o memory-leak
# valgrind ./memory-leak
==1193== Memcheck, a memory error detector
==1193== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1193== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==1193== Command: ./memory-leak
==1193==
==1193==ASan runtime does not come first in initial library list; you should either link runtime to your application or manually preload it with LD_PRELOAD.
==1193==
==1193== HEAP SUMMARY:
==1193==     in use at exit: 0 bytes in 0 blocks
==1193==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==1193==
==1193== All heap blocks were freed -- no leaks are possible
==1193==
==1193== For lists of detected and suppressed errors, rerun with: -s
==1193== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

We can see the valgrind can’t work normally.

Reference:
Sourceforge.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.