Search code examples
valgrindabortheap-corruption

making valgrind abort on error for heap corruption checking?


I'd like to try using valgrind to do some heap corruption detection. With the following corruption "unit test":

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
   char * c = (char *) malloc(10) ;

   memset( c, 0xAB, 20 ) ;
   printf("not aborted\n") ;

   return 0 ;
}

I was suprised to find that valgrind doesn't abort on error, but just produces a message:

valgrind -q --leak-check=no a.out
==11097== Invalid write of size 4
==11097==    at 0x40061F: main (in /home/hotellnx94/peeterj/tmp/a.out)
==11097==  Address 0x51c6048 is 8 bytes inside a block of size 10 alloc'd
==11097==    at 0x4A2058F: malloc (vg_replace_malloc.c:236)
==11097==    by 0x400609: main (in /home/hotellnx94/peeterj/tmp/a.out)
...
not aborted

I don't see a valgrind option to abort on error (like gnu-libc's mcheck does, but I can't use mcheck because it isn't thread safe). Does anybody know if that is possible (our code dup2's stdout to /dev/null since it runs as a daemon, so a report isn't useful and I'd rather catch the culprit in the act or closer to it).


Solution

  • There is no such option in valgrind.

    Consider adding a non-daemon mode (debug mode) into your daemon.

    http://valgrind.org/docs/manual/mc-manual.html#mc-manual.clientreqs 4.6 explains some requests from debugged program to valgrind+memcheck, so you can use some of this in your daemon to do some checks at fixed code positions.