4bff0a
From 34a38e3632fc504b55847fb9658788ccf5d42dad Mon Sep 17 00:00:00 2001
4bff0a
From: Evgeny Vereshchagin <evvers@ya.ru>
4bff0a
Date: Thu, 5 Jul 2018 04:09:30 +0000
4bff0a
Subject: [PATCH] tests: create the asan wrapper automatically if systemd has
4bff0a
 been built with ASAN
4bff0a
4bff0a
(cherry picked from commit ec9181d2ce4c0ad8b1c70b16a2b02a2667b1cc05)
4bff0a
---
4bff0a
 test/test-functions | 24 ++++++++++++++++++++++++
4bff0a
 1 file changed, 24 insertions(+)
4bff0a
4bff0a
diff --git a/test/test-functions b/test/test-functions
4bff0a
index e69420aeca..4417301be9 100644
4bff0a
--- a/test/test-functions
4bff0a
+++ b/test/test-functions
4bff0a
@@ -28,6 +28,27 @@ STATEDIR="${BUILD_DIR:-.}/test/$(basename $(dirname $(realpath $0)))"
4bff0a
 STATEFILE="$STATEDIR/.testdir"
4bff0a
 TESTLOG="$STATEDIR/test.log"
4bff0a
 
4bff0a
+is_built_with_asan() {
4bff0a
+    if ! type -P objdump >/dev/null; then
4bff0a
+        ddebug "Failed to find objdump. Assuming systemd hasn't been built with ASAN."
4bff0a
+        return 1
4bff0a
+    fi
4bff0a
+
4bff0a
+    # Borrowed from https://github.com/google/oss-fuzz/blob/cd9acd02f9d3f6e80011cc1e9549be526ce5f270/infra/base-images/base-runner/bad_build_check#L182
4bff0a
+    local _asan_calls=$(objdump -dC $BUILD_DIR/systemd | egrep "callq\s+[0-9a-f]+\s+<__asan" -c)
4bff0a
+    if (( $_asan_calls < 1000 )); then
4bff0a
+        return 1
4bff0a
+    else
4bff0a
+        return 0
4bff0a
+    fi
4bff0a
+}
4bff0a
+
4bff0a
+IS_BUILT_WITH_ASAN=$(is_built_with_asan && echo yes || echo no)
4bff0a
+
4bff0a
+if [[ "$IS_BUILT_WITH_ASAN" = "yes" ]]; then
4bff0a
+    STRIP_BINARIES=no
4bff0a
+fi
4bff0a
+
4bff0a
 function find_qemu_bin() {
4bff0a
     # SUSE and Red Hat call the binary qemu-kvm. Debian and Gentoo call it kvm.
4bff0a
     # Either way, only use this version if we aren't running in KVM, because
4bff0a
@@ -217,6 +238,9 @@ setup_basic_environment() {
4bff0a
     strip_binaries
4bff0a
     install_depmod_files
4bff0a
     generate_module_dependencies
4bff0a
+    if [[ "$IS_BUILT_WITH_ASAN" = "yes" ]]; then
4bff0a
+         create_asan_wrapper
4bff0a
+    fi
4bff0a
 }
4bff0a
 
4bff0a
 setup_selinux() {