In this blog post, I will explain how I used valgrind with vgdb to detect memory errors, leaks and misbehaviors:
>>> target remote 192.168.0.100:1234 Remote debugging using 192.168.0.100:1234 warning: remote target does not support file transfer, attempting to access fil es from local filesystem. warning: Unable to find dynamic linker breakpoint function. GDB will be unable to debug shared library initializers and track explicitly loaded dynamic code. 0x04013d18 in ?? () ─── Assembly ────────────────────────────────────────────────────────────────── 0x04013d18 ? ldr r10, [pc, #60] ; 0x4013d5c 0x04013d1c ? mov r0, sp 0x04013d20 ? bl 0x401477c 0x04013d24 ? add r6, pc, #48 ; 0x30 0x04013d28 ? add r10, r10, r6 0x04013d2c ? ldr r4, [r10, r4] 0x04013d30 ? mov r6, r0 0x04013d34 ? ldr r1, [sp] 0x04013d38 ? add r2, sp, #4 0x04013d3c ? add r3, r2, r1, lsl #2 ─── Breakpoints ─────────────────────────────────────────────────────────────── ─── Expressions ─────────────────────────────────────────────────────────────── ─── History ─────────────────────────────────────────────────────────────────── ─── Memory ──────────────────────────────────────────────────────────────────── ─── Registers ───────────────────────────────────────────────────────────────── r0 0x00000000 r1 0xbdccbd50 r2 0x00000000 r3 0x00000000 r4 0x00000000 r5 0x00000000 r6 0x00000000 r7 0x00000000 r8 0x00000000 r9 0x00000000 r10 0x00000000 r11 0x00000000 r12 0x00000000 sp 0xbdccbd50 lr 0x00000000 pc 0x04013d18 cpsr 0x00000010 fpscr 0x00000000 ─── Source ──────────────────────────────────────────────────────────────────── ─── Stack ───────────────────────────────────────────────────────────────────── [0] from 0x04013d18 [1] from 0x00000000 ─── Threads ─────────────────────────────────────────────────────────────────── [1] id 315 from 0x04013d18 ─── Variables ───────────────────────────────────────────────────────────────── ─────────────────────────────────────────────────────────────────────────────── >>>
# valgrind --vgdb=yes --vgdb-error=0 --leak-check=full ./faulty_mem_app ==315== Memcheck, a memory error detector ==315== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al. ==315== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info ==315== Command: ./faulty_mem_app ==315== ==315== (action at startup) vgdb me ... ==315== ==315== TO DEBUG THIS PROCESS USING GDB: start GDB like this ==315== /path/to/gdb ./faulty_mem_app ==315== and then give GDB the following command ==315== target remote | /usr/libexec/valgrind/../../bin/vgdb --pid=315 ==315== --pid is optional if only one valgrind process is running ==315==
# vgdb --port=1234 listening on port 1234 ...connected. relaying data between gdb and process 315
as well as address sanitizer to do the same, but quicker:
1 CC=${CROSS_COMPILE}gcc 2 CFLAGS=-g3 -fsanitize=address -static-libasan 3 LDFLAGS=-fsanitize=address -static-libasan 4 5 faulty_mem_app: faulty_mem_app.c 6 ${CC} ${CFLAGS} $< -o $@ ${LDFLAGS} ▼ NORMAL ⚙ Makefile ≡
# [25/252] # ./faulty_mem_app ================================================================= ==207==ERROR: AddressSanitizer: heap-buffer-overflow on address 0xb4803fd0 at pc 0x00515ce0 bp 0xbe820c1c sp 0xbe820c14 WRITE of size 4 at 0xb4803fd0 thread T0 #0 0x515ce4 in clear_array /home/mcvitic/debugging-labs/nfsroot/root/valgr ind/faulty_mem_app.c:15 #1 0x515e90 in do_something /home/mcvitic/debugging-labs/nfsroot/root/valg rind/faulty_mem_app.c:46 #2 0x515eb0 in main /home/mcvitic/debugging-labs/nfsroot/root/valgrind/fau lty_mem_app.c:51 #3 0xb6e3e8a4 in __libc_start_call_main (/lib/libc.so.6+0x228a4) #4 0xb6e3e9ac in __libc_start_main@GLIBC_2.4 (/lib/libc.so.6+0x229ac) 0xb4803fd0 is located 0 bytes after 400-byte region [0xb4803e40,0xb4803fd0) allocated by thread T0 here: #0 0x4ddb28 in __interceptor_malloc (/root/valgrind/faulty_mem_app+0x8eb28 ) #1 0x515c4c in allocate_array /home/mcvitic/debugging-labs/nfsroot/root/va lgrind/faulty_mem_app.c:7 #2 0x515e5c in do_something /home/mcvitic/debugging-labs/nfsroot/root/valg rind/faulty_mem_app.c:40 #3 0x515eb0 in main /home/mcvitic/debugging-labs/nfsroot/root/valgrind/fau lty_mem_app.c:51 #4 0xb6e3e8a4 in __libc_start_call_main (/lib/libc.so.6+0x228a4) #5 0xb6e3e9ac in __libc_start_main@GLIBC_2.4 (/lib/libc.so.6+0x229ac) SUMMARY: AddressSanitizer: heap-buffer-overflow /home/mcvitic/debugging-labs/n fsroot/root/valgrind/faulty_mem_app.c:15 in clear_array Shadow bytes around the buggy address: 0xb4803d00: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0xb4803d80: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0xb4803e00: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00 0xb4803e80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0xb4803f00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0xb4803f80: 00 00 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa 0xb4804000: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
In this post, I will not be focusing on giving a detailed tutorial on how to solve Bootlin’s debugging lab but rather present how I approached solving memory leaks of a running application and how I detected and fixed the root cause of it.
As usual, I would like to give a shout out to Bootlin for making their materials free and open source, including materials I used to demonstrate detecting and fixing memory leaks described in this blog post.
Table of Contents
Introduction
In this blog post, I will be using the same setup as described in the GDB setup for debugging embedded applications blog post. As always, special thanks to Bootlin for providing the source code.
On your development host, change directory to valgrind folder, cross-compile valgrind.c and then run it on the target:
mcvitic@mcvitic-ThinkPad-L580:~/debugging-labs/nfsroot/root/valgrind$ export CR
OSS_COMPILE=/home/$USER/debugging-labs/buildroot/output/host/bin/arm-linux-
mcvitic@mcvitic-ThinkPad-L580:~/debugging-labs/nfsroot/root/valgrind$ ls
faulty_mem_app.c Makefile
mcvitic@mcvitic-ThinkPad-L580:~/debugging-labs/nfsroot/root/valgrind$ make
/home/mcvitic/debugging-labs/buildroot/output/host/bin/arm-linux-gcc faulty_mem
_app.c -g3 -o faulty_mem_app
mcvitic@mcvitic-ThinkPad-L580:~/debugging-labs/nfsroot/root/valgrind$ ls
faulty_mem_app faulty_mem_app.c Makefile
mcvitic@mcvitic-ThinkPad-L580:~/debugging-labs/nfsroot/root/valgrind$
# ./faulty_mem_app
#
Even though there is no segfault, an application might leak memory or perform out-of-bounds access or just simply contain uninitialized memory blocks. One useful tool to detect these “invisible” memory issues is valgrind.
Using valgrind
When we run the application again with valgrind we get the following result:
$ valgrind --leak-check=full ./faulty_mem_app
# ./faulty_mem_app [25/149] # valgrind --leak-check=full ./faulty_mem_app ==164== Memcheck, a memory error detector ==164== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al. ==164== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info ==164== Command: ./faulty_mem_app ==164== ==164== Syscall param write(buf) points to uninitialised byte(s) ==164== at 0x490BFA4: write (in /lib/libc.so.6) ==164== Address 0x49811b4 is 396 bytes inside a block of size 400 alloc'd ==164== at 0x482DEA8: malloc (vg_replace_malloc.c:442) ==164== by 0x108637: allocate_array (faulty_mem_app.c:7) ==164== by 0x10879F: do_something (faulty_mem_app.c:40) ==164== by 0x1087F3: main (faulty_mem_app.c:51) ==164== ==164== Invalid write of size 4 ==164== at 0x10867C: clear_array (faulty_mem_app.c:15) ==164== by 0x1087D3: do_something (faulty_mem_app.c:46) ==164== by 0x1087F3: main (faulty_mem_app.c:51) ==164== Address 0x49811b8 is 0 bytes after a block of size 400 alloc'd ==164== at 0x482DEA8: malloc (vg_replace_malloc.c:442) ==164== by 0x108637: allocate_array (faulty_mem_app.c:7) ==164== by 0x10879F: do_something (faulty_mem_app.c:40) ==164== by 0x1087F3: main (faulty_mem_app.c:51) ==164== ==164== Invalid write of size 4 ==164== at 0x10867C: clear_array (faulty_mem_app.c:15) ==164== by 0x1087D3: do_something (faulty_mem_app.c:46) ==164== by 0x1087FB: main (faulty_mem_app.c:52) ==164== Address 0x4981508 is 0 bytes after a block of size 800 alloc'd ==164== at 0x482DEA8: malloc (vg_replace_malloc.c:442) ==164== by 0x108637: allocate_array (faulty_mem_app.c:7) ==164== by 0x10879F: do_something (faulty_mem_app.c:40) ==164== by 0x1087FB: main (faulty_mem_app.c:52) ==164== ==164== ==164== HEAP SUMMARY: ==164== in use at exit: 1,200 bytes in 2 blocks
==164== Invalid write of size 4
==164== at 0x10867C: clear_array (faulty_mem_app.c:15)
==164== by 0x1087D3: do_something (faulty_mem_app.c:46)
==164== by 0x1087FB: main (faulty_mem_app.c:52)
==164== Address 0x4981508 is 0 bytes after a block of size 800 alloc'd
==164== at 0x482DEA8: malloc (vg_replace_malloc.c:442)
==164== by 0x108637: allocate_array (faulty_mem_app.c:7)
==164== by 0x10879F: do_something (faulty_mem_app.c:40)
==164== by 0x1087FB: main (faulty_mem_app.c:52)
==164==
==164==
==164== HEAP SUMMARY:
==164== in use at exit: 1,200 bytes in 2 blocks
==164== total heap usage: 2 allocs, 0 frees, 1,200 bytes allocated
==164==
==164== 400 bytes in 1 blocks are definitely lost in loss record 1 of 2
==164== at 0x482DEA8: malloc (vg_replace_malloc.c:442)
==164== by 0x108637: allocate_array (faulty_mem_app.c:7)
==164== by 0x10879F: do_something (faulty_mem_app.c:40)
==164== by 0x1087F3: main (faulty_mem_app.c:51)
==164==
==164== 800 bytes in 1 blocks are definitely lost in loss record 2 of 2
==164== at 0x482DEA8: malloc (vg_replace_malloc.c:442)
==164== by 0x108637: allocate_array (faulty_mem_app.c:7)
==164== by 0x10879F: do_something (faulty_mem_app.c:40)
==164== by 0x1087FB: main (faulty_mem_app.c:52)
==164==
==164== LEAK SUMMARY:
==164== definitely lost: 1,200 bytes in 2 blocks
==164== indirectly lost: 0 bytes in 0 blocks
==164== possibly lost: 0 bytes in 0 blocks
==164== still reachable: 0 bytes in 0 blocks
==164== suppressed: 0 bytes in 0 blocks
==164==
==164== Use --track-origins=yes to see where uninitialised values come from
==164== For lists of detected and suppressed errors, rerun with: -s
==164== ERROR SUMMARY: 6 errors from 5 contexts (suppressed: 0 from 0)
#
The output indicates the following occurrences in faulty_mem_app:
- invalid memory write
- uninitialized memory
- memory leak (memory was allocated but never freed)
The call stack indicates the following origin and error propagation:
main() -> do_something() -> clear_array()
To fix this issue, we have to check if array access is out of bounds and that malloc() is allocating enough space. Also, this memory has to be initialized before use and freed in the end.
Backtracing the call stack to pinpoint where the issue occurs will, in majority of cases, most likely be enough to solve the memory issue.
However, in order to pinpoint each error exactly dynamically and be able to debug with gdb, vgdb can be used. vgdb is a bridge that allows gdb to attach to and control a running valgrind process.
valgrind and vgdb
Using valgrind with vgdb gives us dynamic interactive control by letting us stop execution at memory errors, inspect variables, set breakpoints, and trace exact causes, whereas using plain valgrind only gives a post-mortem report.
We will do that remotely on the host using gdb-multiarch. First, run the following on the target:
$ valgrind --vgdb=yes --vgdb-error=0 --leak-check=full ./faulty_mem_app
In order to do remote debugging using vgdb, it has to be in listen mode. Start another terminal in SSH on the target and run:
# vgdb --port=1234
On the host side, run:
$ gdb-multiarch ./faulty_mem_app
and connect to vgdb using the following:
(gdb) target remote 192.168.0.100:1234
We can now debug each error using gdb with valgrind’s supervision which will interrupt the program each time it detects an error. The setup should now look something like this (same picture as the first one):
>>> target remote 192.168.0.100:1234 Remote debugging using 192.168.0.100:1234 warning: remote target does not support file transfer, attempting to access fil es from local filesystem. warning: Unable to find dynamic linker breakpoint function. GDB will be unable to debug shared library initializers and track explicitly loaded dynamic code. 0x04013d18 in ?? () ─── Assembly ────────────────────────────────────────────────────────────────── 0x04013d18 ? ldr r10, [pc, #60] ; 0x4013d5c 0x04013d1c ? mov r0, sp 0x04013d20 ? bl 0x401477c 0x04013d24 ? add r6, pc, #48 ; 0x30 0x04013d28 ? add r10, r10, r6 0x04013d2c ? ldr r4, [r10, r4] 0x04013d30 ? mov r6, r0 0x04013d34 ? ldr r1, [sp] 0x04013d38 ? add r2, sp, #4 0x04013d3c ? add r3, r2, r1, lsl #2 ─── Breakpoints ─────────────────────────────────────────────────────────────── ─── Expressions ─────────────────────────────────────────────────────────────── ─── History ─────────────────────────────────────────────────────────────────── ─── Memory ──────────────────────────────────────────────────────────────────── ─── Registers ───────────────────────────────────────────────────────────────── r0 0x00000000 r1 0xbdccbd50 r2 0x00000000 r3 0x00000000 r4 0x00000000 r5 0x00000000 r6 0x00000000 r7 0x00000000 r8 0x00000000 r9 0x00000000 r10 0x00000000 r11 0x00000000 r12 0x00000000 sp 0xbdccbd50 lr 0x00000000 pc 0x04013d18 cpsr 0x00000010 fpscr 0x00000000 ─── Source ──────────────────────────────────────────────────────────────────── ─── Stack ───────────────────────────────────────────────────────────────────── [0] from 0x04013d18 [1] from 0x00000000 ─── Threads ─────────────────────────────────────────────────────────────────── [1] id 315 from 0x04013d18 ─── Variables ───────────────────────────────────────────────────────────────── ─────────────────────────────────────────────────────────────────────────────── >>>
# valgrind --vgdb=yes --vgdb-error=0 --leak-check=full ./faulty_mem_app ==315== Memcheck, a memory error detector ==315== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al. ==315== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info ==315== Command: ./faulty_mem_app ==315== ==315== (action at startup) vgdb me ... ==315== ==315== TO DEBUG THIS PROCESS USING GDB: start GDB like this ==315== /path/to/gdb ./faulty_mem_app ==315== and then give GDB the following command ==315== target remote | /usr/libexec/valgrind/../../bin/vgdb --pid=315 ==315== --pid is optional if only one valgrind process is running ==315==
# vgdb --port=1234 listening on port 1234 ...connected. relaying data between gdb and process 315
One more thing to note is that backtrace for leaks is not shown on the target because all libraries are stripped and thus do not have any debugging symbols anymore. This leads to the impossibility to use the DWARF information for backtracing.
First thing I always like to do is to set a breakpoint to main() and start the program to reach the application’s entry point. Then, we can set breakpoints to both functions mentioned in the backtrace above: clear_array() and do_something(). If we were to execute continue command, eventually, gdb would stop execution when valgrind detects an error as shown in the upper-right corner:
0x00108664 clear_array+28 b 0x10868c <clear_array+68> 0x00108668 clear_array+32 ldr r3, [r11, #-8] 0x0010866c clear_array+36 lsl r3, r3, #2 ─── Breakpoints ─────────────────────────────────────────────────────────────── [1] break at 0x001087ec in faulty_mem_app.c:51 for main hit 1 time [2] break at 0x0010865c in faulty_mem_app.c:14 for clear_array hit 2 times [3] break at 0x00108798 in faulty_mem_app.c:40 for do_something hit 2 times ─── Expressions ─────────────────────────────────────────────────────────────── ─── History ─────────────────────────────────────────────────────────────────── ─── Memory ──────────────────────────────────────────────────────────────────── ─── Registers ───────────────────────────────────────────────────────────────── r0 0x049811e8 r1 0x000000c8 r2 0x00000320 r3 0x00000001 r4 0xbdfb8d54 r5 0x00000001 r6 0x00109ebc r7 0x00109ebc r8 0x00000000 r9 0xbdfb8d5c r10 0x04022ce0 r11 0xbdfb8bc4 r12 0x00109fe8 sp 0xbdfb8bb0 lr 0x001087d4 pc 0x0010865c cpsr 0x00000010 fpscr 0x00000000 ─── Source ──────────────────────────────────────────────────────────────────── 9 10 static void clear_array(int *p, int size) 11 { 12 int i; 13 !14 for (i = 0; i <= size; i++) 15 p[i] = 0; 16 } 17 18 static void write_file(int *p, int size) ─── Stack ───────────────────────────────────────────────────────────────────── [0] from 0x0010865c in clear_array+20 at faulty_mem_app.c:14 [1] from 0x001087d4 in do_something+76 at faulty_mem_app.c:46 [2] from 0x001087fc in main+24 at faulty_mem_app.c:52 ─── Threads ─────────────────────────────────────────────────────────────────── [1] id 397 from 0x0010865c in clear_array+20 at faulty_mem_app.c:14 ─── Variables ───────────────────────────────────────────────────────────────── arg p = 0x49811e8: 0, size = 200 loc i = 3 ─────────────────────────────────────────────────────────────────────────────── >>>
==397== by 0x108637: allocate_array (faulty_mem_app.c:7) ==397== by 0x10879F: do_something (faulty_mem_app.c:40) ==397== by 0x1087F3: main (faulty_mem_app.c:51) ==397== ==397== (action on error) vgdb me ... ==397== Continuing ... ==397== Invalid write of size 4 ==397== at 0x10867C: clear_array (faulty_mem_app.c:15) ==397== by 0x1087D3: do_something (faulty_mem_app.c:46) ==397== by 0x1087F3: main (faulty_mem_app.c:51) ==397== Address 0x49811b8 is 0 bytes after a block of size 400 alloc'd ==397== at 0x482DEA8: malloc (vg_replace_malloc.c:442) ==397== by 0x108637: allocate_array (faulty_mem_app.c:7) ==397== by 0x10879F: do_something (faulty_mem_app.c:40) ==397== by 0x1087F3: main (faulty_mem_app.c:51) ==397== ==397== (action on error) vgdb me ... ==397== Continuing ...
# vgdb --port=1234 listening on port 1234 ...connected. relaying data between gdb and process 397
until we reach the end of execution:
─── Output/messages ─────────────────────────────────────────────────────────── [Inferior 1 (Remote target) exited normally] >>>
==397== by 0x1087F3: main (faulty_mem_app.c:51) ==397== ==397== 800 bytes in 1 blocks are definitely lost in loss record 2 of 2 ==397== at 0x482DEA8: malloc (vg_replace_malloc.c:442) ==397== by 0x108637: allocate_array (faulty_mem_app.c:7) ==397== by 0x10879F: do_something (faulty_mem_app.c:40) ==397== by 0x1087FB: main (faulty_mem_app.c:52) ==397== ==397== LEAK SUMMARY: ==397== definitely lost: 1,200 bytes in 2 blocks ==397== indirectly lost: 0 bytes in 0 blocks ==397== possibly lost: 0 bytes in 0 blocks ==397== still reachable: 0 bytes in 0 blocks ==397== suppressed: 0 bytes in 0 blocks ==397== ==397== Use --track-origins=yes to see where uninitialised values come from ==397== For lists of detected and suppressed errors, rerun with: -s ==397== ERROR SUMMARY: 6 errors from 5 contexts (suppressed: 0 from 0) #
# vgdb --port=1234 listening on port 1234 ...connected. relaying data between gdb and process 397 #
Solution to memory issues is quite straightforward and consists of changing <= to < in the clear_array() function. You can see the fixed source code here.
Recompiling and running valgrind on the recompiled app will now give the following result:
mcvitic@mcvitic-ThinkPad-L580:~/debugging-labs/nfsroot/root/valgrind$ make /home/mcvitic/debugging-labs/buildroot/output/host/bin/arm-linux-gcc faulty_mem _app.c -g3 -o faulty_mem_app mcvitic@mcvitic-ThinkPad-L580:~/debugging-labs/nfsroot/root/valgrind$
# valgrind --leak-check=full ./faulty_mem_app
==454== Memcheck, a memory error detector
==454== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==454== Using Valgrind-3.22.0 and LibVEX; rerun with -h for copyright info
==454== Command: ./faulty_mem_app
==454==
==454==
==454== HEAP SUMMARY:
==454== in use at exit: 0 bytes in 0 blocks
==454== total heap usage: 2 allocs, 2 frees, 1,200 bytes allocated
==454==
==454== All heap blocks were freed -- no leaks are possible
==454==
==454== For lists of detected and suppressed errors, rerun with: -s
==454== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
#
Address Sanitizer
An even simpler way to detect memory errors is to use address sanitizer.
First, make sure you obtain address sanitizer. I downloaded it via apt on my Ubuntu machine:
$ sudo apt install libasan* # replace "*" with corresponding version of gcc you are using
Once this was done, I opened the Makefile and simply added the following compiler and linker flags as shown below and just ran the app as I would when testing its behavior on the target:
1 CC=${CROSS_COMPILE}gcc 2 CFLAGS=-g3 -fsanitize=address -static-libasan 3 LDFLAGS=-fsanitize=address -static-libasan 4 5 faulty_mem_app: faulty_mem_app.c 6 ${CC} ${CFLAGS} $< -o $@ ${LDFLAGS} ▼ NORMAL ⚙ Makefile ≡
# [25/252] # ./faulty_mem_app ================================================================= ==207==ERROR: AddressSanitizer: heap-buffer-overflow on address 0xb4803fd0 at pc 0x00515ce0 bp 0xbe820c1c sp 0xbe820c14 WRITE of size 4 at 0xb4803fd0 thread T0 #0 0x515ce4 in clear_array /home/mcvitic/debugging-labs/nfsroot/root/valgr ind/faulty_mem_app.c:15 #1 0x515e90 in do_something /home/mcvitic/debugging-labs/nfsroot/root/valg rind/faulty_mem_app.c:46 #2 0x515eb0 in main /home/mcvitic/debugging-labs/nfsroot/root/valgrind/fau lty_mem_app.c:51 #3 0xb6e3e8a4 in __libc_start_call_main (/lib/libc.so.6+0x228a4) #4 0xb6e3e9ac in __libc_start_main@GLIBC_2.4 (/lib/libc.so.6+0x229ac) 0xb4803fd0 is located 0 bytes after 400-byte region [0xb4803e40,0xb4803fd0) allocated by thread T0 here: #0 0x4ddb28 in __interceptor_malloc (/root/valgrind/faulty_mem_app+0x8eb28 ) #1 0x515c4c in allocate_array /home/mcvitic/debugging-labs/nfsroot/root/va lgrind/faulty_mem_app.c:7 #2 0x515e5c in do_something /home/mcvitic/debugging-labs/nfsroot/root/valg rind/faulty_mem_app.c:40 #3 0x515eb0 in main /home/mcvitic/debugging-labs/nfsroot/root/valgrind/fau lty_mem_app.c:51 #4 0xb6e3e8a4 in __libc_start_call_main (/lib/libc.so.6+0x228a4) #5 0xb6e3e9ac in __libc_start_main@GLIBC_2.4 (/lib/libc.so.6+0x229ac) SUMMARY: AddressSanitizer: heap-buffer-overflow /home/mcvitic/debugging-labs/n fsroot/root/valgrind/faulty_mem_app.c:15 in clear_array Shadow bytes around the buggy address: 0xb4803d00: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0xb4803d80: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0xb4803e00: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00 0xb4803e80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0xb4803f00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0xb4803f80: 00 00 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa 0xb4804000: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
1 CC=${CROSS_COMPILE}gcc 2 CFLAGS=-g3 -fsanitize=address -static-libasan 3 LDFLAGS=-fsanitize=address -static-libasan 4 5 faulty_mem_app: faulty_mem_app.c 6 ${CC} ${CFLAGS} $< -o $@ ${LDFLAGS} ▼ NORMAL ⚙ Makefile ≡
#4 0xb6e3e8a4 in __libc_start_call_main (/lib/libc.so.6+0x228a4)
#5 0xb6e3e9ac in __libc_start_main@GLIBC_2.4 (/lib/libc.so.6+0x229ac)
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/mcvitic/debugging-labs/n
fsroot/root/valgrind/faulty_mem_app.c:15 in clear_array
Shadow bytes around the buggy address:
0xb4803d00: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0xb4803d80: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0xb4803e00: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00
0xb4803e80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0xb4803f00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0xb4803f80: 00 00 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa
0xb4804000: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0xb4804080: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0xb4804100: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0xb4804180: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0xb4804200: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
==207==ABORTING
#
Conclusion
Address sanitizer gave us a clear backtrace to the clear_array() function as the source of heap buffer overflow and more or less same information that valgrind provided. This is something I would first recommend someone to run their code through since it doesn’t require much effort to set up and yet gives quite a readable insight into where memory leaks lurk. If you want to go a step further and observe leaky code dynamically, use valgrind with vgdb.
If you would like to support my work, consider: