Skip to content

Commit 2b5e786

Browse files
committed
test: enable LSan for certain wrapped binaries
So we're able to detect memory leaks in our NSS modules. An example after introducing a memory leak in nss-myhostname.c: testsuite-71.sh[2881]: ================================================================= testsuite-71.sh[2881]: ==2880==ERROR: LeakSanitizer: detected memory leaks testsuite-71.sh[2881]: Direct leak of 2 byte(s) in 1 object(s) allocated from: testsuite-71.sh[2881]: #0 0x7fa28907243b in strdup (/usr/lib64/libasan.so.8.0.0+0x7243b) testsuite-71.sh[2881]: #1 0x7fa286a7bc10 in gethostname_full ../src/basic/hostname-util.c:67 testsuite-71.sh[2881]: #2 0x7fa286a74af9 in gethostname_malloc ../src/basic/hostname-util.h:24 testsuite-71.sh[2881]: #3 0x7fa286a756f4 in _nss_myhostname_gethostbyname4_r ../src/nss-myhostname/nss-myhostname.c:79 testsuite-71.sh[2881]: #4 0x7fa288f17588 in getaddrinfo (/lib64/libc.so.6+0xf4588) testsuite-71.sh[2881]: #5 0x7fa2890a4d93 in __interceptor_getaddrinfo.part.0 (/usr/lib64/libasan.so.8.0.0+0xa4d93) testsuite-71.sh[2881]: #6 0x55a54b2b7159 in ahosts_keys_int.part.0 (/usr/bin/getent.orig+0x4159) testsuite-71.sh[2881]: SUMMARY: AddressSanitizer: 2 byte(s) leaked in 1 allocation(s).
1 parent 0f90d4f commit 2b5e786

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

test/test-functions

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2708,6 +2708,7 @@ inst_binary() {
27082708
# DSOs provided by systemd
27092709
local systemd_so_regex='/(libudev|libsystemd.*|.+[\-_]systemd([\-_].+)?|libnss_(mymachines|myhostname|resolve)).so'
27102710
local wrap_binary=0
2711+
local enable_lsan=0
27112712
# I love bash!
27122713
while read -r line; do
27132714
[[ "$line" = 'not a dynamic executable' ]] && break
@@ -2750,6 +2751,14 @@ inst_binary() {
27502751
bin_rx='/(agetty|chown|curl|delv|dig|getfacl|getent|id|login|ls|mkfs\.[a-z0-9]+|mksquashfs|mkswap|setfacl|setpriv|stat|su|tar|useradd|userdel)$'
27512752
if get_bool "$IS_BUILT_WITH_ASAN" && [[ "$bin" =~ $bin_rx ]]; then
27522753
wrap_binary=1
2754+
# Ugh, so we want to disable LSan in most cases for the wrapped binaries, since
2755+
# we don't care about memory leaks in such binaries. However, in certain cases
2756+
# the external binary is the only interface for the systemd code, like for
2757+
# the systemd NSS modules, where we want to detect memory leaks. So let's
2758+
# do another check to decide if we want to enable LSan for given binary.
2759+
if [[ "$bin" =~ /getent$ ]]; then
2760+
enable_lsan=1
2761+
fi
27532762
fi
27542763

27552764
# If the target binary is built with ASan support, we don't need to wrap
@@ -2767,7 +2776,7 @@ inst_binary() {
27672776
export LD_PRELOAD="$ASAN_RT_PATH"
27682777
# Disable LSan to speed things up, since we don't care about leak reports
27692778
# from 'external' binaries
2770-
export ASAN_OPTIONS=detect_leaks=0
2779+
export ASAN_OPTIONS=detect_leaks=$enable_lsan
27712780
# Set argv[0] to the original binary name without the ".orig" suffix
27722781
exec -a "\$0" -- "${target}.orig" "\$@"
27732782
EOF

0 commit comments

Comments
 (0)