Skip to contentSkip to conversation

build: Use meson b_sanitize option for sanitizers

PATCH New Nabih Estefan 1 participant 1 message Jun 23 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 build: Use meson b_sanitize option for sanitizers

Refactor sanitizer configuration to use Meson's built-in b_sanitize option instead of manually adding flags to qemu_cflags and qemu_ldflags. This ensures that sanitizer flags are correctly passed to both C and Rust targets, fixing linker errors when Rust and UBSan are both enabled.

Explicitly parse --enable-ubsan, --enable-asan, and --enable-tsan in configure and map them to -Db_sanitize meson option. Remove redundant custom options from meson_options.txt.

How to test:
`./configure --target-list=aarch64-softmmu --enable-ubsan --enable-rust
&& make -j all`

Before this change it will fail at the end of make, since the linker will have different flags, after this change it will link properly

---
 configure                           | 35 ++++++++++++++++++
 meson.build                         | 55 +++++++++++++----------------
 meson_options.txt                   |  7 +---
 scripts/meson-buildoptions.sh       |  9 -----
 tests/functional/x86_64/meson.build |  2 +-
 tests/unit/meson.build              |  2 +-
 6 files changed, 62 insertions(+), 48 deletions(-)
configure +35 -0
index d8bc10060e..579df2f2c8 100755
@@ -281,6 +281,9 @@ cfi="false"
 # which requires knowing whether --static is enabled.
 pie=""
 static="no"
+ubsan="false"
+asan="false"
+tsan="false"
 
 # Preferred compiler:
 #  ${CC} (if set)
@@ -746,6 +749,18 @@ for opt do
   ;;
   --wasm64-32bit-address-limit)
   ;;
+  --enable-ubsan) ubsan="true"
+  ;;
+  --disable-ubsan) ubsan="false"
+  ;;
+  --enable-asan) asan="true"
+  ;;
+  --disable-asan) asan="false"
+  ;;
+  --enable-tsan) tsan="true"
+  ;;
+  --disable-tsan) tsan="false"
+  ;;
   # everything else has the same name in configure and meson
   --*) meson_option_parse "$opt" "$optarg"
   ;;
@@ -1939,6 +1954,26 @@ if test "$skip_meson" = no; then
 
   # QEMU options
   test "$rust" != "disabled" && meson_option_add "-Drust=$rust"
+
+  # Translate asan/ubsan/tsan to b_sanitize
+  sanitizers=""
+  if test "$ubsan" = "true" && test "$asan" = "true"; then
+    sanitizers="undefined,address"
+  elif test "$ubsan" = "true"; then
+    sanitizers="undefined"
+  elif test "$asan" = "true"; then
+    sanitizers="address"
+  fi
+  if test "$tsan" = "true"; then
+    if test -n "$sanitizers"; then
+      error_exit "TSAN is not supported with other sanitizers"
+    fi
+    sanitizers="thread"
+  fi
+  if test -n "$sanitizers"; then
+    meson_option_add "-Db_sanitize=$sanitizers"
+  fi
+
   test "$cfi" != false && meson_option_add "-Dcfi=$cfi" "-Db_lto=$cfi"
   test "$docs" != auto && meson_option_add "-Ddocs=$docs"
   test -n "${LIB_FUZZING_ENGINE+xxx}" && meson_option_add "-Dfuzzing_engine=$LIB_FUZZING_ENGINE"
meson.build +24 -31

96 lines not on this page · raw message

meson_options.txt +1 -6
index a07cb47d35..d61c0241cc 100644
@@ -99,12 +99,7 @@ option('tcg_interpreter', type: 'boolean', value: false,
        description: 'TCG with bytecode interpreter (slow)')
 option('safe_stack', type: 'boolean', value: false,
        description: 'SafeStack Stack Smash Protection (requires clang/llvm and coroutine backend ucontext)')
-option('asan', type: 'boolean', value: false,
-       description: 'enable address sanitizer')
-option('ubsan', type: 'boolean', value: false,
-       description: 'enable undefined behaviour sanitizer')
-option('tsan', type: 'boolean', value: false,
-       description: 'enable thread sanitizer')
+
 option('stack_protector', type: 'feature', value: 'auto',
        description: 'compiler-provided stack protection')
 option('cfi', type: 'boolean', value: false,
scripts/meson-buildoptions.sh +0 -9
index c003985047..8d3b992ed7 100644
@@ -21,7 +21,6 @@ meson_options_help() {
   printf "%s\n" '  --disable-relocatable    toggle relocatable install'
   printf "%s\n" '  --docdir=VALUE           Base directory for documentation installation'
   printf "%s\n" '                           (can be empty) [share/doc]'
-  printf "%s\n" '  --enable-asan            enable address sanitizer'
   printf "%s\n" '  --enable-block-drv-whitelist-in-tools'
   printf "%s\n" '                           use block whitelist also in tools instead of only'
   printf "%s\n" '                           QEMU'
@@ -54,8 +53,6 @@ meson_options_help() {
   printf "%s\n" '  --enable-trace-backends=CHOICES'
   printf "%s\n" '                           Set available tracing backends [log] (choices:'
   printf "%s\n" '                           dtrace/ftrace/log/nop/simple/syslog/ust)'
-  printf "%s\n" '  --enable-tsan            enable thread sanitizer'
-  printf "%s\n" '  --enable-ubsan           enable undefined behaviour sanitizer'
   printf "%s\n" '  --firmwarepath=VALUES    search PATH for firmware files [share/qemu-'
   printf "%s\n" '                           firmware]'
   printf "%s\n" '  --iasl=VALUE             Path to ACPI disassembler'
@@ -238,8 +235,6 @@ _meson_option_parse() {
     --disable-af-xdp) printf "%s" -Daf_xdp=disabled ;;
     --enable-alsa) printf "%s" -Dalsa=enabled ;;
     --disable-alsa) printf "%s" -Dalsa=disabled ;;
-    --enable-asan) printf "%s" -Dasan=true ;;
-    --disable-asan) printf "%s" -Dasan=false ;;
     --enable-attr) printf "%s" -Dattr=enabled ;;
     --disable-attr) printf "%s" -Dattr=disabled ;;
     --audio-drv-list=*) quote_sh "-Daudio_drv_list=$2" ;;
@@ -521,14 +516,10 @@ _meson_option_parse() {
     --disable-tpm) printf "%s" -Dtpm=disabled ;;
     --enable-trace-backends=*) quote_sh "-Dtrace_backends=$2" ;;
     --with-trace-file=*) quote_sh "-Dtrace_file=$2" ;;
-    --enable-tsan) printf "%s" -Dtsan=true ;;
-    --disable-tsan) printf "%s" -Dtsan=false ;;
     --enable-u2f) printf "%s" -Du2f=enabled ;;
     --disable-u2f) printf "%s" -Du2f=disabled ;;
     --enable-uadk) printf "%s" -Duadk=enabled ;;
     --disable-uadk) printf "%s" -Duadk=disabled ;;
-    --enable-ubsan) printf "%s" -Dubsan=true ;;
-    --disable-ubsan) printf "%s" -Dubsan=false ;;
     --enable-usb-redir) printf "%s" -Dusb_redir=enabled ;;
     --disable-usb-redir) printf "%s" -Dusb_redir=disabled ;;
     --enable-valgrind) printf "%s" -Dvalgrind=enabled ;;
tests/functional/x86_64/meson.build +1 -1
index 1ed10ad6c2..54af748f15 100644
@@ -24,7 +24,7 @@ tests_x86_64_system_quick = [
 # interacts badly with the sanitizer; this means the memlock
 # test (which checks via /proc for whether pages were locked)
 # will always fail on a sanitizer build, so don't run it.
-if not get_option('asan')
+if not sanitize.contains('address')
   tests_x86_64_system_quick += [ 'memlock' ]
 endif
 
tests/unit/meson.build +1 -1
index 01cc540a45..b0ba64d0ec 100644
@@ -147,7 +147,7 @@ if have_system
   # Some tests: test-char, test-qdev-global-props, and test-qga,
   # are not runnable under TSan due to a known issue.
   # https://github.com/google/sanitizers/issues/1116
-  if not get_option('tsan')
+  if not sanitize.contains('thread')
     if host_os != 'windows'
         tests += {
           'test-char': ['socket-helpers.c', qom, io, chardev]
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog