Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FS#3392 - valgrind: does not detect obvious memory leak #8255

Closed
openwrt-bot opened this issue Oct 18, 2020 · 1 comment
Closed

FS#3392 - valgrind: does not detect obvious memory leak #8255

openwrt-bot opened this issue Oct 18, 2020 · 1 comment
Labels

Comments

@openwrt-bot
Copy link

MikePetullo:

I am running valgrind on OpenWrt trunk x86_64. The package provides valgrind 3.15.0. My OpenWrt install also makes use of musl.

I have the following test program, which has an obvious memory leak:

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

int f(void) {
char *buf = malloc(BUFSIZ);
if (buf == NULL) {
return -1;
}
return 0;
}

int main(void)
{
f();
}

Running valgrind on OpenWrt produces this:

valgrind --leak-check=full ./leak
==22148== Memcheck, a memory error detector
==22148== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==22148== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==22148== Command: ./leak
==22148==
==22148== Conditional jump or move depends on uninitialised value(s)
==22148== at 0x4019DDC: ??? (in /lib/libc.so)
==22148== by 0x405AE5D: ??? (in /lib/libc.so)
==22148==
==22148== Conditional jump or move depends on uninitialised value(s)
==22148== at 0x401961F: ??? (in /lib/libc.so)
==22148== by 0x405AE5D: ??? (in /lib/libc.so)
==22148==
==22148==
==22148== HEAP SUMMARY:
==22148== in use at exit: 0 bytes in 0 blocks
==22148== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==22148==
==22148== All heap blocks were freed -- no leaks are possible
==22148==
==22148== Use --track-origins=yes to see where uninitialised values come from
==22148== For lists of detected and suppressed errors, rerun with: -s
==22148== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

There is no mention of the memory leak.

Here is the output I expect, as produced by running valgrind on Fedora:

valgrind --leak-check=full ./leak
==7325== Memcheck, a memory error detector
==7325== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==7325== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==7325== Command: ./leak
==7325==
==7325==
==7325== HEAP SUMMARY:
==7325== in use at exit: 8,192 bytes in 1 blocks
==7325== total heap usage: 1 allocs, 0 frees, 8,192 bytes allocated
==7325==
==7325== 8,192 bytes in 1 blocks are definitely lost in loss record 1 of 1
==7325== at 0x483A809: malloc (vg_replace_malloc.c:307)
==7325== by 0x401137: f (leak.c:5)
==7325== by 0x401159: main (leak.c:14)
==7325==
==7325== LEAK SUMMARY:
==7325== definitely lost: 8,192 bytes in 1 blocks
==7325== indirectly lost: 0 bytes in 0 blocks
==7325== possibly lost: 0 bytes in 0 blocks
==7325== still reachable: 0 bytes in 0 blocks
==7325== suppressed: 0 bytes in 0 blocks
==7325==
==7325== For lists of detected and suppressed errors, rerun with: -s
==7325== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

@openwrt-bot
Copy link
Author

jow-:

Works for me:

BusyBox v1.31.1 () built-in shell (ash)


| |.-----.-----.-----.| | | |.----.| |_
| - || _ | -| || | | || || |
|
_____|| |
||||___||| |____|
|
| W I R E L E S S F R E E D O M

OpenWrt SNAPSHOT, r14601-8b4233188d

=== WARNING! =====================================
There is no root password defined on this device!
Use the "passwd" command to set up a new password
in order to prevent unauthorized SSH logins.

root@OpenWrt:/# valgrind /tmp/test
==2866== Memcheck, a memory error detector
==2866== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2866== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==2866== Command: /tmp/test
==2866==
==2866== Conditional jump or move depends on uninitialised value(s)
==2866== at 0x401961F: stpcpy (stpcpy.c:20)
==2866== by 0x4019A18: strcpy (strcpy.c:5)
==2866== by 0x405AC3B: load_library (dynlink.c:1133)
==2866== by 0x405AE9F: load_direct_deps (dynlink.c:1192)
==2866== by 0x405AE9F: load_deps (dynlink.c:1209)
==2866== by 0x405AE9F: load_deps (dynlink.c:1205)
==2866== by 0x405B826: __dls3 (dynlink.c:1845)
==2866== by 0x405B175: __dls2 (dynlink.c:1650)
==2866== by 0x4058D17: ??? (in /lib/libc.so)
==2866==
==2866==
==2866== HEAP SUMMARY:
==2866== in use at exit: 1,903 bytes in 7 blocks
==2866== total heap usage: 8 allocs, 1 frees, 1,951 bytes allocated
==2866==
==2866== LEAK SUMMARY:
==2866== definitely lost: 1,024 bytes in 1 blocks
==2866== indirectly lost: 0 bytes in 0 blocks
==2866== possibly lost: 0 bytes in 0 blocks
==2866== still reachable: 879 bytes in 6 blocks
==2866== suppressed: 0 bytes in 0 blocks
==2866== Rerun with --leak-check=full to see details of leaked memory
==2866==
==2866== Use --track-origins=yes to see where uninitialised values come from
==2866== For lists of detected and suppressed errors, rerun with: -s
==2866== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
root@OpenWrt:/#

In order for valgrind to be able to wrap malloc/free etc, you need an unstripped musl libc. To avoid doing a new build you could scp the unstripped libc.so to your target and execute it directly:

jow@j7:~/devel/lede/staging.git$ scp build_dir/toolchain-x86_64_gcc-8.4.0_musl/musl-1.1.24/lib/libc.so root@192.168.1.1:/tmp/ Warning: Permanently added '192.168.1.1' (ED25519) to the list of known hosts. libc.so 100% 3811KB 9.8MB/s 00:00 jow@j7:~/devel/lede/staging.git$ ssh root@192.168.1.1 Warning: Permanently added '192.168.1.1' (ED25519) to the list of known hosts.

BusyBox v1.31.1 () built-in shell (ash)


| |.-----.-----.-----.| | | |.----.| |_
| - || _ | -| || | | || || |
|
_____|| |
||||___||| |____|
|
| W I R E L E S S F R E E D O M

OpenWrt SNAPSHOT, r14601-8b4233188d

=== WARNING! =====================================
There is no root password defined on this device!
Use the "passwd" command to set up a new password
in order to prevent unauthorized SSH logins.

root@OpenWrt:# valgrind /tmp/test
==2884== Memcheck, a memory error detector
==2884== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2884== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==2884== Command: /tmp/test
==2884==
==2884== Conditional jump or move depends on uninitialised value(s)
==2884== at 0x121DDC: ??? (in /lib/libc.so)
==2884== by 0x162E9F: ??? (in /lib/libc.so)
==2884==
==2884== Conditional jump or move depends on uninitialised value(s)
==2884== at 0x12161F: ??? (in /lib/libc.so)
==2884== by 0x162E9F: ??? (in /lib/libc.so)
==2884==
==2884==
==2884== HEAP SUMMARY:
==2884== in use at exit: 0 bytes in 0 blocks
==2884== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==2884==
==2884== All heap blocks were freed -- no leaks are possible
==2884==
==2884== Use --track-origins=yes to see where uninitialised values come from
==2884== For lists of detected and suppressed errors, rerun with: -s
==2884== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
root@OpenWrt:
# valgrind /tmp/libc.so /tmp/test
==2885== Memcheck, a memory error detector
==2885== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2885== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==2885== Command: /tmp/libc.so /tmp/test
==2885==
==2885== Conditional jump or move depends on uninitialised value(s)
==2885== at 0x12161F: stpcpy (stpcpy.c:20)
==2885== by 0x121A18: strcpy (strcpy.c:5)
==2885== by 0x162C3B: load_library (dynlink.c:1133)
==2885== by 0x162E9F: load_direct_deps (dynlink.c:1192)
==2885== by 0x162E9F: load_deps (dynlink.c:1209)
==2885== by 0x162E9F: load_deps (dynlink.c:1205)
==2885== by 0x163826: __dls3 (dynlink.c:1845)
==2885== by 0x163175: __dls2 (dynlink.c:1650)
==2885== by 0x160D17: ??? (in /tmp/libc.so)
==2885== by 0x1: ???
==2885==
==2885==
==2885== HEAP SUMMARY:
==2885== in use at exit: 1,903 bytes in 7 blocks
==2885== total heap usage: 8 allocs, 1 frees, 1,951 bytes allocated
==2885==
==2885== LEAK SUMMARY:
==2885== definitely lost: 1,024 bytes in 1 blocks
==2885== indirectly lost: 0 bytes in 0 blocks
==2885== possibly lost: 0 bytes in 0 blocks
==2885== still reachable: 879 bytes in 6 blocks
==2885== suppressed: 0 bytes in 0 blocks
==2885== Rerun with --leak-check=full to see details of leaked memory
==2885==
==2885== Use --track-origins=yes to see where uninitialised values come from
==2885== For lists of detected and suppressed errors, rerun with: -s
==2885== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
root@OpenWrt:~#

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant