Skip to content

Commit

Permalink
test: enable LSan for certain wrapped binaries
Browse files Browse the repository at this point in the history
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).

(cherry picked from commit 2b5e786)
(cherry picked from commit 5a29fbb)
(cherry picked from commit 7d667e5)
  • Loading branch information
mrc0mmand authored and bluca committed Jul 10, 2023
1 parent 43cabc5 commit ac43f3d
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion test/test-functions
Original file line number Diff line number Diff line change
Expand Up @@ -2449,6 +2449,7 @@ inst_binary() {
# DSOs provided by systemd
local systemd_so_regex='/(libudev|libsystemd.*|.+[\-_]systemd([\-_].+)?|libnss_(mymachines|myhostname|resolve)).so'
local wrap_binary=0
local enable_lsan=0
# I love bash!
while read -r line; do
[[ "$line" = 'not a dynamic executable' ]] && break
Expand Down Expand Up @@ -2490,6 +2491,14 @@ inst_binary() {
bin_rx='/(agetty|chown|curl|delv|dig|getfacl|getent|login|ls|mkfs\.[a-z0-9]+|mksquashfs|mkswap|setpriv|stat|su|tar|useradd|userdel)$'
if get_bool "$IS_BUILT_WITH_ASAN" && [[ "$bin" =~ $bin_rx ]]; then
wrap_binary=1
# Ugh, so we want to disable LSan in most cases for the wrapped binaries, since
# we don't care about memory leaks in such binaries. However, in certain cases
# the external binary is the only interface for the systemd code, like for
# the systemd NSS modules, where we want to detect memory leaks. So let's
# do another check to decide if we want to enable LSan for given binary.
if [[ "$bin" =~ /getent$ ]]; then
enable_lsan=1
fi
fi

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

0 comments on commit ac43f3d

Please sign in to comment.