Skip to content

Commit 30767c8

Browse files
mrc0mmandbluca
authored andcommitted
test: split the ASan wrapper into smaller blocks and tidy it up a bit
No functional change (hopefully), just making it easier on the eyes. (cherry picked from commit ba79e8c) (cherry picked from commit 91f08ec) (cherry picked from commit bf60262)
1 parent 61d7209 commit 30767c8

File tree

1 file changed

+68
-60
lines changed

1 file changed

+68
-60
lines changed

test/test-functions

+68-60
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ IS_BUILT_WITH_COVERAGE=$(is_built_with_coverage && echo yes || echo no)
255255

256256
if get_bool "$IS_BUILT_WITH_ASAN"; then
257257
STRIP_BINARIES=no
258+
PATH_TO_INIT="$ROOTLIBDIR/systemd-under-asan"
258259
SKIP_INITRD="${SKIP_INITRD:-yes}"
259-
PATH_TO_INIT=$ROOTLIBDIR/systemd-under-asan
260260
QEMU_MEM="${QEMU_MEM:-2G}"
261261
QEMU_SMP="${QEMU_SMP:-4}"
262262

@@ -782,88 +782,96 @@ EOF
782782
}
783783

784784
create_asan_wrapper() {
785-
local asan_wrapper="$initdir/$ROOTLIBDIR/systemd-under-asan"
786-
dinfo "Create ASan wrapper as '$asan_wrapper'"
785+
local asan_wrapper default_asan_options default_ubsan_options default_environment
787786

788787
[[ -z "$ASAN_RT_PATH" ]] && dfatal "ASAN_RT_PATH is empty, but it shouldn't be"
789788

790-
# clang: install llvm-symbolizer to generate useful reports
791-
# See: https://clang.llvm.org/docs/AddressSanitizer.html#symbolizing-the-reports
792-
[[ "$ASAN_COMPILER" == "clang" ]] && image_install "llvm-symbolizer"
789+
default_asan_options="${ASAN_OPTIONS:-strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1}"
790+
default_ubsan_options="${UBSAN_OPTIONS:-print_stacktrace=1:print_summary=1:halt_on_error=1}"
793791

794-
cat >"$asan_wrapper" <<EOF
795-
#!/usr/bin/env bash
792+
if [[ "$ASAN_COMPILER" == "clang" ]]; then
793+
# clang: install llvm-symbolizer to generate useful reports
794+
# See: https://clang.llvm.org/docs/AddressSanitizer.html#symbolizing-the-reports
795+
image_install "llvm-symbolizer"
796796

797-
set -x
798-
799-
echo "ASan RT: $ASAN_RT_PATH"
800-
if [[ ! -e "$ASAN_RT_PATH" ]]; then
801-
echo >&2 "Couldn't find ASan RT at '$ASAN_RT_PATH', can't continue"
802-
exit 1
803-
fi
804-
805-
DEFAULT_ASAN_OPTIONS=${ASAN_OPTIONS:-strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1}
806-
DEFAULT_UBSAN_OPTIONS=${UBSAN_OPTIONS:-print_stacktrace=1:print_summary=1:halt_on_error=1}
807-
DEFAULT_ENVIRONMENT="ASAN_OPTIONS=\$DEFAULT_ASAN_OPTIONS UBSAN_OPTIONS=\$DEFAULT_UBSAN_OPTIONS"
797+
# Let's add the ASan DSO's path to the dynamic linker's cache. This is pretty
798+
# unnecessary for gcc & libasan, however, for clang this is crucial, as its
799+
# runtime ASan DSO is in a non-standard (library) path.
800+
mkdir -p "${initdir:?}/etc/ld.so.conf.d/"
801+
echo "${ASAN_RT_PATH%/*}" >"${initdir:?}/etc/ld.so.conf.d/asan-path-override.conf"
802+
fi
808803

809-
# Create a simple environment file which can be included by systemd services
810-
# that need it (i.e. services that utilize DynamicUser=true and bash, etc.)
811-
cat >/usr/lib/systemd/systemd-asan-env <<INNER_EOF
804+
# Create a simple environment file which can be included by systemd services
805+
# that need it (i.e. services that utilize DynamicUser=true and bash, etc.)
806+
cat >"${initdir:?}/usr/lib/systemd/systemd-asan-env" <<EOF
812807
LD_PRELOAD=$ASAN_RT_PATH
813-
ASAN_OPTIONS=$DEFAULT_ASAN_OPTIONS
808+
ASAN_OPTIONS=$default_asan_options
814809
LSAN_OPTIONS=detect_leaks=0
815-
UBSAN_OPTIONS=$DEFAULT_UBSAN_OPTIONS
816-
INNER_EOF
817-
818-
# As right now bash is the PID 1, we can't expect PATH to have a sane value.
819-
# Let's make one to prevent unexpected "<bin> not found" issues in the future
820-
export PATH="/sbin:/bin:/usr/sbin:/usr/bin"
821-
822-
mountpoint -q /proc || mount -t proc proc /proc
823-
mountpoint -q /sys || mount -t sysfs sysfs /sys
824-
mount -o remount,rw /
810+
UBSAN_OPTIONS=$default_ubsan_options
811+
EOF
825812

826-
DEFAULT_ENVIRONMENT="\$DEFAULT_ENVIRONMENT ASAN_RT_PATH=$ASAN_RT_PATH"
813+
default_environment=(
814+
"ASAN_OPTIONS='$default_asan_options'"
815+
"UBSAN_OPTIONS='$default_ubsan_options'"
816+
"ASAN_RT_PATH='$ASAN_RT_PATH'"
817+
)
827818

828-
if [[ "$ASAN_COMPILER" == "clang" ]]; then
829-
# Let's add the ASan DSO's path to the dynamic linker's cache. This is pretty
830-
# unnecessary for gcc & libasan, however, for clang this is crucial, as its
831-
# runtime ASan DSO is in a non-standard (library) path.
832-
echo "${ASAN_RT_PATH%/*}" >/etc/ld.so.conf.d/asan-path-override.conf
833-
ldconfig
834-
fi
835-
echo DefaultEnvironment=\$DEFAULT_ENVIRONMENT >>/etc/systemd/system.conf
836-
echo DefaultTimeoutStartSec=180s >>/etc/systemd/system.conf
837-
echo DefaultStandardOutput=journal+console >>/etc/systemd/system.conf
819+
mkdir -p "${initdir:?}/etc/systemd/system.conf.d/"
820+
cat >"${initdir:?}/etc/systemd/system.conf.d/asan.conf" <<EOF
821+
[Manager]
822+
DefaultEnvironment=${default_environment[*]}
823+
ManagerEnvironment=${default_environment[*]}
824+
DefaultTimeoutStartSec=180s
825+
DefaultStandardOutput=journal+console
826+
EOF
838827

839-
# ASAN and syscall filters aren't compatible with each other.
840-
find / -name '*.service' -type f | xargs sed -i 's/^\\(MemoryDeny\\|SystemCall\\)/#\\1/'
828+
# ASAN and syscall filters aren't compatible with each other.
829+
find "${initdir:?}" -name '*.service' -type f -print0 | xargs -0 sed -i 's/^\(MemoryDeny\|SystemCall\)/#\1/'
841830

831+
mkdir -p "${initdir:?}/etc/systemd/system/systemd-journald.service.d/"
832+
cat >"${initdir:?}/etc/systemd/system/systemd-journald.service.d/asan-env.conf" <<EOF
833+
[Service]
842834
# The redirection of ASAN reports to a file prevents them from ending up in /dev/null.
843835
# But, apparently, sometimes it doesn't work: https://github.com/google/sanitizers/issues/886.
844-
JOURNALD_CONF_DIR=/etc/systemd/system/systemd-journald.service.d
845-
mkdir -p "\$JOURNALD_CONF_DIR"
846-
printf "[Service]\nEnvironment=ASAN_OPTIONS=\$DEFAULT_ASAN_OPTIONS:log_path=/systemd-journald.asan.log UBSAN_OPTIONS=\$DEFAULT_UBSAN_OPTIONS:log_path=/systemd-journald.ubsan.log\n" >"\$JOURNALD_CONF_DIR/env.conf"
836+
Environment=ASAN_OPTIONS=$default_asan_options:log_path=/systemd-journald.asan.log UBSAN_OPTIONS=$default_ubsan_options:log_path=/systemd-journald.ubsan.log
847837
848838
# Sometimes UBSan sends its reports to stderr regardless of what is specified in log_path
849839
# Let's try to catch them by redirecting stderr (and stdout just in case) to a file
850840
# See https://github.com/systemd/systemd/pull/12524#issuecomment-491108821
851-
printf "[Service]\nStandardOutput=file:/systemd-journald.out\n" >"\$JOURNALD_CONF_DIR/out.conf"
841+
StandardOutput=file:/systemd-journald.out
842+
EOF
852843

853-
# 90s isn't enough for some services to finish when literally everything is run
854-
# under ASan+UBSan in containers, which, in turn, are run in VMs.
855-
# Let's limit which environments such services should be executed in.
856-
mkdir -p /etc/systemd/system/systemd-hwdb-update.service.d
857-
printf "[Unit]\nConditionVirtualization=container\n\n[Service]\nTimeoutSec=240s\n" >/etc/systemd/system/systemd-hwdb-update.service.d/env-override.conf
844+
# 90s isn't enough for some services to finish when literally everything is run
845+
# under ASan+UBSan in containers, which, in turn, are run in VMs.
846+
# Let's limit which environments such services should be executed in.
847+
mkdir -p "${initdir:?}/etc/systemd/system/systemd-hwdb-update.service.d/"
848+
cat >"${initdir:?}/etc/systemd/system/systemd-hwdb-update.service.d/asan.conf" <<EOF
849+
[Unit]
850+
ConditionVirtualization=container
858851
859-
# Let's override another hard-coded timeout that kicks in too early
860-
mkdir -p /etc/systemd/system/systemd-journal-flush.service.d
861-
printf "[Service]\nTimeoutSec=180s\n" >/etc/systemd/system/systemd-journal-flush.service.d/timeout.conf
852+
[Service]
853+
TimeoutSec=240s
854+
EOF
862855

863-
export ASAN_OPTIONS=\$DEFAULT_ASAN_OPTIONS:log_path=/systemd.asan.log UBSAN_OPTIONS=\$DEFAULT_UBSAN_OPTIONS
864-
exec "$ROOTLIBDIR/systemd" "\$@"
856+
# Let's override another hard-coded timeout that kicks in too early
857+
mkdir -p "${initdir:?}/etc/systemd/system/systemd-journal-flush.service.d/"
858+
cat >"${initdir:?}/etc/systemd/system/systemd-journal-flush.service.d/asan.conf" <<EOF
859+
[Service]
860+
TimeoutSec=180s
865861
EOF
866862

863+
asan_wrapper="${initdir:?}/${PATH_TO_INIT:?}"
864+
# Sanity check to make sure we don't overwrite something we shouldn't.
865+
[[ "$asan_wrapper" =~ systemd-under-asan$ ]]
866+
867+
cat >"$asan_wrapper" <<EOF
868+
#!/usr/bin/env bash
869+
set -eux
870+
871+
export ${default_environment[@]}
872+
[[ -n "\$ASAN_OPTIONS" && -n "\$UBSAN_OPTIONS" ]]
873+
exec "$ROOTLIBDIR/systemd" "\$@"
874+
EOF
867875
chmod 0755 "$asan_wrapper"
868876
}
869877

0 commit comments

Comments
 (0)