2.1.2.2 : How to use the valgrind's debugger



This the command to be used to launch our hadamard_product program with valgrind :

valgrind ./hadamard_product 
==4924== Memcheck, a memory error detector
==4924== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4924== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==4924== Command: ./hadamard_product
==4924== 
Hadamard product
==4924== Invalid read of size 16
==4924==    at 0x109207: hadamard_product (main.cpp:19)
==4924==    by 0x109207: evaluateHadamardProduct(unsigned long) (main.cpp:37)
==4924==    by 0x108D31: main (main.cpp:49)
==4924==  Address 0x5b24020 is 0 bytes after a block of size 4,000 alloc'd
==4924==    at 0x4C3089F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4924==    by 0x1090C3: evaluateHadamardProduct(unsigned long) (main.cpp:30)
==4924==    by 0x108D31: main (main.cpp:49)
==4924== 
==4924== Invalid read of size 16
==4924==    at 0x1091F8: hadamard_product (main.cpp:19)
==4924==    by 0x1091F8: evaluateHadamardProduct(unsigned long) (main.cpp:37)
==4924==    by 0x108D31: main (main.cpp:49)
==4924==  Address 0x5b23040 is 0 bytes after a block of size 4,000 alloc'd
==4924==    at 0x4C3089F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4924==    by 0x1090B8: evaluateHadamardProduct(unsigned long) (main.cpp:29)
==4924==    by 0x108D31: main (main.cpp:49)
==4924== 
==4924== Invalid write of size 8
==4924==    at 0x1091FC: hadamard_product (main.cpp:19)
==4924==    by 0x1091FC: evaluateHadamardProduct(unsigned long) (main.cpp:37)
==4924==    by 0x108D31: main (main.cpp:49)
==4924==  Address 0x5b22060 is 0 bytes after a block of size 4,000 alloc'd
==4924==    at 0x4C3089F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4924==    by 0x1090AD: evaluateHadamardProduct(unsigned long) (main.cpp:28)
==4924==    by 0x108D31: main (main.cpp:49)
==4924== 
==4924== Invalid read of size 16
==4924==    at 0x1091F0: hadamard_product (main.cpp:19)
==4924==    by 0x1091F0: evaluateHadamardProduct(unsigned long) (main.cpp:37)
==4924==    by 0x108D31: main (main.cpp:49)
==4924==  Address 0x5b24030 is 16 bytes after a block of size 4,000 alloc'd
==4924==    at 0x4C3089F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4924==    by 0x1090C3: evaluateHadamardProduct(unsigned long) (main.cpp:30)
==4924==    by 0x108D31: main (main.cpp:49)
==4924== 
tabResult[0] = 0

valgrind: m_mallocfree.c:280 (mk_plain_bszB): Assertion 'bszB != 0' failed. valgrind: This is probably caused by your program erroneously writing past the end of a heap block and corrupting heap metadata. If you fix any invalid writes reported by Memcheck, this assertion failure will probably go away. Please try that before reporting this as a bug.

host stacktrace: ==4924== at 0x580441BA: ??? (in /usr/lib/valgrind/memcheck-amd64-linux) ==4924== by 0x580442D4: ??? (in /usr/lib/valgrind/memcheck-amd64-linux) ==4924== by 0x58044459: ??? (in /usr/lib/valgrind/memcheck-amd64-linux) ==4924== by 0x580531EC: ??? (in /usr/lib/valgrind/memcheck-amd64-linux) ==4924== by 0x5800BA84: ??? (in /usr/lib/valgrind/memcheck-amd64-linux) ==4924== by 0x5800BD39: ??? (in /usr/lib/valgrind/memcheck-amd64-linux) ==4924== by 0x5809F785: ??? (in /usr/lib/valgrind/memcheck-amd64-linux) ==4924== by 0x580AED50: ??? (in /usr/lib/valgrind/memcheck-amd64-linux)

sched status: running_tid=1

Thread 1: status = VgTs_Runnable (lwpid 4924) ==4924== at 0x4C3089F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==4924== by 0x1090AD: evaluateHadamardProduct(unsigned long) (main.cpp:28) ==4924== by 0x108D3B: main (main.cpp:50)

Note: see also the FAQ in the source distribution. It contains workarounds to several common problems. In particular, if Valgrind aborted or crashed after identifying problems in your program, there's a good chance that fixing those problems will prevent Valgrind aborting or crashing, especially if it happened in m_mallocfree.c.

If that doesn't help, please report this bug to: www.valgrind.org

In the bug report, send all the above text, the valgrind version, and what OS and version you are using. Thanks.


The very first output give us the version of valgrind we are using and the command we are executed :

==4924== Memcheck, a memory error detector
==4924== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4924== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==4924== Command: ./hadamard_product


Then, we have directly the position of the problem :

==4924== Invalid read of size 16
==4924==    at 0x109207: hadamard_product (main.cpp:19)
==4924==    by 0x109207: evaluateHadamardProduct(unsigned long) (main.cpp:37)
==4924==    by 0x108D31: main (main.cpp:49)
==4924==  Address 0x5b24020 is 0 bytes after a block of size 4,000 alloc'd
==4924==    at 0x4C3089F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4924==    by 0x1090C3: evaluateHadamardProduct(unsigned long) (main.cpp:30)
==4924==    by 0x108D31: main (main.cpp:49)


We have :

==4924== Invalid read of size 16


The so problem concerns type of 16 bytes. This can be due to SSE4 vectorization (4 float, so 16 bytes).



Then :

==4924==    at 0x109207: hadamard_product (main.cpp:19)
==4924==    by 0x109207: evaluateHadamardProduct(unsigned long) (main.cpp:37)
==4924==    by 0x108D31: main (main.cpp:49)


The problem hapens at line 19 of the main and the function hadamard_product is called by evaluateHadamardProduct at line 37.

And :
==4924==  Address 0x5b24020 is 0 bytes after a block of size 4,000 alloc'd


Because we do not have data after 4000 bytes (so 1000 float) which is the case because we allocate only 1000 float. So there is a conflict with operator new[] :
==4924==    at 0x4C3089F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4924==    by 0x1090C3: evaluateHadamardProduct(unsigned long) (main.cpp:30)
==4924==    by 0x108D31: main (main.cpp:49)


So, if we take a look at the line 37 of the main.cpp, we get :



1
hadamard_product(tabResult, tabX, tabY, 4000lu);


Of course, there is a mistake here because 4000lu is greater than the expected 1000 elements, and the overflow is here.

So this line must be changed into :



1
hadamard_product(tabResult, tabX, tabY, nbElement);


Then we recompile :

1
2
3
4
5
make
Scanning dependencies of target hadamard_product
[ 50%] Building CXX object CMakeFiles/hadamard_product.dir/main.cpp.o
[100%] Linking CXX executable hadamard_product
[100%] Built target hadamard_product


And the execution is good :

1
2
3
4
5
6
7
./hadamard_product 
Hadamard product
tabResult[0] = 0
tabResult[0] = 0
tabResult[0] = 0
tabResult[0] = 0
tabResult[0] = 0


The debugging is finished.