From: Emil Velikov Date: Wed, 9 Oct 2024 15:26:25 +0000 (+0100) Subject: Add support for clang sanitizers X-Git-Tag: v34~214 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f5b4ff82f45af668cb4d62043184829b1e9621cb;p=thirdparty%2Fkmod.git Add support for clang sanitizers 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 Link: https://github.com/kmod-project/kmod/pull/172 Signed-off-by: Lucas De Marchi --- diff --git a/meson.build b/meson.build index 9a14195d..c4eb57dd 100644 --- a/meson.build +++ b/meson.build @@ -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 ################################################################################ diff --git a/scripts/sanitizer-env.sh b/scripts/sanitizer-env.sh index 9c759a82..dc1103df 100755 --- a/scripts/sanitizer-env.sh +++ b/scripts/sanitizer-env.sh @@ -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