]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
Add support for clang sanitizers
authorEmil Velikov <emil.l.velikov@gmail.com>
Wed, 9 Oct 2024 15:26:25 +0000 (16:26 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Fri, 18 Oct 2024 18:22:12 +0000 (13:22 -0500)
By default clang uses static sanitizer libraries, which causes build and
test-time failures. Swap for the shared libasan which resolves both.

Note: meson tries to be helpful here, throwing a warning that we should
use -D b_lundef=false which is incorrect in our case.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/172
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
meson.build
scripts/sanitizer-env.sh

index 9a14195dfcc35930a0b342b09a04a361296ee978..c4eb57dd1ffe962ce6b9c1d2e895cc5016f277a7 100644 (file)
@@ -159,6 +159,17 @@ add_project_link_arguments(
   language : 'c'
 )
 
+# Clang as of v18, relies on statically linking the sanitizers. This causes two
+# distinct issues:
+#  - the shared library is underlinked, so the build fails
+#  - the modules (that we dlopen/ld_preload) are underlinked so the tests fail
+#
+# Force shared libasan (GCC defaults to shared and this toggle doesn't exist),
+# which combined with the LD_PRELOAD in our wrapper makes everything happy.
+if get_option('b_sanitize') != 'none' and cc.get_id() == 'clang'
+  add_project_arguments('-shared-libasan', language : 'c')
+  add_project_link_arguments('-shared-libasan', language : 'c')
+endif
 ################################################################################
 # Options
 ################################################################################
index 9c759a82db37a94513bedd6a1dedbd9f543810f6..dc1103dfa4daa0804d078f21a116c055a379c967 100755 (executable)
@@ -2,7 +2,16 @@
 
 # set -euo pipefail # don't set these, since this script is sourced
 
-OUR_PRELOAD=$(gcc -print-file-name=libasan.so)
+if [[ ${CC-} == *gcc* ]]; then
+    OUR_PRELOAD=$("$CC" -print-file-name=libasan.so)
+elif [[ ${CC-} == *clang* ]]; then
+    OUR_PRELOAD=$("$CC" -print-file-name=libclang_rt.asan-x86_64.so)
+else
+    echo "Unknown compiler CC=\"${CC-}\" - gcc and clang are supported."
+    echo "Assuming \"gcc\", manually set the variable and retry if needed."
+    echo
+    OUR_PRELOAD=$(gcc -print-file-name=libasan.so)
+fi
 
 if test -n "$OUR_PRELOAD"; then
     # In some cases, like in Fedora, the file is a script which cannot be