From nobody Sun Jul 26 12:33:36 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=google.com ARC-Seal: i=1; a=rsa-sha256; t=1782245044; cv=none; d=zohomail.com; s=zohoarc; b=PbB58TiKHtWdXruizSKc0na4/eiYyVcvqug2boWxDBgCN9qoOJnFZgqPTXj/90qjYLTnfn1D5ndNAzFv0kdB/eYYKXGxFEQ6xCeJab9TTbYaCgJ/mhW08jOx8hApdHugDLxZmzkueiUz4KqoMTJdKto0wBKvOxU4HRZXHVI2Ec4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1782245044; h=Content-Type:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Iw4apqtQ7sIu7G2OtOGwry3Vix6t9KUumRSelZ4ZOLs=; b=jWas4a0F+AHe4KPEv0IFZul12GmsoZSk8Hw1ZDY7qdZx4oZvpocOW9WcT6scg5aq3UBctaFyzuxaStftk172BeNvTlKrwXVdj2JnmOC6z3N1Oc6jkp1IDxA3hakMlQ4IN2i5kQpDBZxaGn0o901YVZHbSU6GS1/diJW0taJIVIg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1782245044592881.045191790263; Tue, 23 Jun 2026 13:04:04 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wc7Lb-0006Lc-Qp; Tue, 23 Jun 2026 16:03:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3nuY6agwKCq8cPQXWThiTUPcVddVaT.RdbfTbj-STkTacdcVcj.dgV@flex--nabihestefan.bounces.google.com>) id 1wc7LY-0006LJ-LF for qemu-devel@nongnu.org; Tue, 23 Jun 2026 16:03:49 -0400 Received: from mail-dl1-x1249.google.com ([2607:f8b0:4864:20::1249]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3nuY6agwKCq8cPQXWThiTUPcVddVaT.RdbfTbj-STkTacdcVcj.dgV@flex--nabihestefan.bounces.google.com>) id 1wc7LW-0004hY-2f for qemu-devel@nongnu.org; Tue, 23 Jun 2026 16:03:48 -0400 Received: by mail-dl1-x1249.google.com with SMTP id a92af1059eb24-137fc6f8e9fso447389c88.1 for ; Tue, 23 Jun 2026 13:03:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1782245023; x=1782849823; darn=nongnu.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=Iw4apqtQ7sIu7G2OtOGwry3Vix6t9KUumRSelZ4ZOLs=; b=GMruAKyPNap6dNyRX/dfoDgoaTvET1797MMo4rlbTrQFj47+eaACN9AlbAHZ20Juwj UH1yrh9P6AhYSy9u8dOADih1qN0yn/hDwdmg9DXGnVbLev7L/gt7+2PvBra6HR05Cfnh 4r/vgvsbVr9ZEM4cmaQEXtC3QleROrMCgIbOuppXG80cvk6ov+6wjhBkdEThhNlyR4I8 ycltO6YR2/HoE0toZGjYNVKX7Jmom7Fa67CCFm5F7HGkWPhPo4ZoEsRy2OJNM/jnWwQh zq1QHCWlMfdRWwSLeDPPt72m6bvGTENSWSxotY0XdyE1zfW1T/El+2KoPsZM8DCMLpju NQkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782245023; x=1782849823; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Iw4apqtQ7sIu7G2OtOGwry3Vix6t9KUumRSelZ4ZOLs=; b=PLhUTTQz8irpIoRt3B+bB2x95/6IAj/KQ1cAAKjpDIFjabKQrLWkhAFpELpLhbKt6o lM4fh3oeqT5+NuqfsLd6rXq09Ouc3y3WT8LhwDkhcWMSZZZna3xcFUrpgBUvIjr5r1TY az26ow9cBlUCX/P7W6M1RX3xpyYDaga/jhkN7eKum6PlbP8XPFgTuV/EzbnliqIs89kR KeSjAypOFWvKCQdHKN7i1YL4qZaJP34lnIi+0qWI4ZkoprkmxGVMF3s1PWqOztUWPC1F fGfgg1ysvjCVCLYiBs79Q7lm9AHemaNl6AwvJASNqPtkTDw1mIKjILtA4YKL+koYQvXE /2jg== X-Forwarded-Encrypted: i=1; AFNElJ9Tlb16ntxm//IpA+YT+y3Pg2a0HEhDfECk7dp+EzRNRl1YYofWGgWpocAL8WRxCWHaOFkRyLOtNdxJ@nongnu.org X-Gm-Message-State: AOJu0YwcU1A/0A+TO7egy4ycxbWIDnF3K7U94JbBnK7E2ZPFMv/F09jD KV2ttUf2ipQ9a14vBBVI5uK63/PDycYksZFoSOI/8+iKTR1OIbKsbp1xhr9r+0sAw0AqWOWZ1SV Ip9b+Yx6WcPaGb88AuJmEnwP/h0yQoA== X-Received: from dlbuy24.prod.google.com ([2002:a05:7022:1e18:b0:139:b07e:ef0b]) (user=nabihestefan job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:6b93:b0:137:9399:fc59 with SMTP id a92af1059eb24-139d0c892c7mr176562c88.21.1782245022526; Tue, 23 Jun 2026 13:03:42 -0700 (PDT) Date: Tue, 23 Jun 2026 20:03:38 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.55.0.rc0.799.gd6f94ed593-goog Message-ID: <20260623200338.3480560-1-nabihestefan@google.com> Subject: [PATCH v2] build: Use meson b_sanitize option for sanitizers From: Nabih Estefan To: pbonzini@redhat.com Cc: alex.bennee@linaro.org, laurent@vivier.eu, deller@gmx.de, pierrick.bouvier@oss.qualcomm.com, marcandre.lureau@redhat.com, berrange@redhat.com, philmd@mailo.com, zhao1.liu@intel.com, qemu-devel@nongnu.org, Nabih Estefan Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1249; envelope-from=3nuY6agwKCq8cPQXWThiTUPcVddVaT.RdbfTbj-STkTacdcVcj.dgV@flex--nabihestefan.bounces.google.com; helo=mail-dl1-x1249.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @google.com) X-ZM-MESSAGEID: 1782245046744158500 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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=3Daarch64-softmmu --enable-ubsan --enable-rust && make -j all` Before this change it will fail at the end of make, since the linker will h= ave different flags, after this change it will link properly Signed-off-by: Nabih Estefan --- 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(-) diff --git a/configure b/configure index d8bc10060e..579df2f2c8 100755 --- a/configure +++ b/configure @@ -281,6 +281,9 @@ cfi=3D"false" # which requires knowing whether --static is enabled. pie=3D"" static=3D"no" +ubsan=3D"false" +asan=3D"false" +tsan=3D"false" =20 # Preferred compiler: # ${CC} (if set) @@ -746,6 +749,18 @@ for opt do ;; --wasm64-32bit-address-limit) ;; + --enable-ubsan) ubsan=3D"true" + ;; + --disable-ubsan) ubsan=3D"false" + ;; + --enable-asan) asan=3D"true" + ;; + --disable-asan) asan=3D"false" + ;; + --enable-tsan) tsan=3D"true" + ;; + --disable-tsan) tsan=3D"false" + ;; # everything else has the same name in configure and meson --*) meson_option_parse "$opt" "$optarg" ;; @@ -1939,6 +1954,26 @@ if test "$skip_meson" =3D no; then =20 # QEMU options test "$rust" !=3D "disabled" && meson_option_add "-Drust=3D$rust" + + # Translate asan/ubsan/tsan to b_sanitize + sanitizers=3D"" + if test "$ubsan" =3D "true" && test "$asan" =3D "true"; then + sanitizers=3D"undefined,address" + elif test "$ubsan" =3D "true"; then + sanitizers=3D"undefined" + elif test "$asan" =3D "true"; then + sanitizers=3D"address" + fi + if test "$tsan" =3D "true"; then + if test -n "$sanitizers"; then + error_exit "TSAN is not supported with other sanitizers" + fi + sanitizers=3D"thread" + fi + if test -n "$sanitizers"; then + meson_option_add "-Db_sanitize=3D$sanitizers" + fi + test "$cfi" !=3D false && meson_option_add "-Dcfi=3D$cfi" "-Db_lto=3D$cf= i" test "$docs" !=3D auto && meson_option_add "-Ddocs=3D$docs" test -n "${LIB_FUZZING_ENGINE+xxx}" && meson_option_add "-Dfuzzing_engin= e=3D$LIB_FUZZING_ENGINE" diff --git a/meson.build b/meson.build index e026851309..417c27002c 100644 --- a/meson.build +++ b/meson.build @@ -541,43 +541,35 @@ if get_option('safe_stack') and coroutine_backend != =3D 'ucontext' error('SafeStack is only supported with the ucontext coroutine backend') endif =20 -if get_option('asan') - if cc.has_argument('-fsanitize=3Daddress') - qemu_cflags =3D ['-fsanitize=3Daddress'] + qemu_cflags - qemu_ldflags =3D ['-fsanitize=3Daddress'] + qemu_ldflags - else - error('Your compiler does not support -fsanitize=3Daddress') - endif +sanitize =3D get_option('b_sanitize') + +if sanitize.contains('address') + # Meson handles -fsanitize=3Daddress automatically endif =20 -if get_option('ubsan') +if sanitize.contains('undefined') # Detect static linking issue with ubsan: # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D84285 - if cc.links('int main(int argc, char **argv) { return argc + 1; }', - args: [qemu_ldflags, '-fsanitize=3Dundefined']) - qemu_cflags +=3D ['-fsanitize=3Dundefined'] - qemu_ldflags +=3D ['-fsanitize=3Dundefined'] - - # Suppress undefined behaviour from function call to mismatched type. - # In addition, tcg prologue does not emit function type prefix - # required by function call sanitizer. - if cc.has_argument('-fno-sanitize=3Dfunction') - qemu_cflags +=3D ['-fno-sanitize=3Dfunction'] - endif - else + if not cc.links('int main(int argc, char **argv) { return argc + 1; }', + args: [qemu_ldflags, '-fsanitize=3Dundefined']) error('Your compiler does not support -fsanitize=3Dundefined') endif + + # Suppress undefined behaviour from function call to mismatched type. + # In addition, tcg prologue does not emit function type prefix + # required by function call sanitizer. + if cc.has_argument('-fno-sanitize=3Dfunction') + qemu_cflags +=3D ['-fno-sanitize=3Dfunction'] + endif endif =20 -# Thread sanitizer is, for now, much noisier than the other sanitizers; -# keep it separate until that is not the case. -if get_option('tsan') - if get_option('asan') or get_option('ubsan') +if sanitize.contains('thread') + if sanitize.contains('address') or sanitize.contains('undefined') error('TSAN is not supported with other sanitizers') endif if not cc.has_function('__tsan_create_fiber', - args: '-fsanitize=3Dthread', - prefix: '#include ') + args: '-fsanitize=3Dthread', + prefix: '#include ') error('Cannot enable TSAN due to missing fiber annotation interface') endif tsan_warn_suppress =3D [] @@ -588,8 +580,7 @@ if get_option('tsan') if cc.has_argument('-Wno-tsan') tsan_warn_suppress =3D ['-Wno-tsan'] endif - qemu_cflags =3D ['-fsanitize=3Dthread'] + tsan_warn_suppress + qemu_cfla= gs - qemu_ldflags =3D ['-fsanitize=3Dthread'] + qemu_ldflags + qemu_cflags +=3D tsan_warn_suppress endif =20 # Detect support for PT_GNU_RELRO + DT_BIND_NOW. @@ -2482,7 +2473,7 @@ if have_tcg config_host_data.set('CONFIG_TCG_INTERPRETER', tcg_arch =3D=3D 'tci') endif config_host_data.set('CONFIG_TPM', have_tpm) -config_host_data.set('CONFIG_TSAN', get_option('tsan')) +config_host_data.set('CONFIG_TSAN', sanitize.contains('thread')) config_host_data.set('CONFIG_USB_LIBUSB', libusb.found()) config_host_data.set('CONFIG_VDE', vde.found()) config_host_data.set('CONFIG_VHOST', have_vhost) @@ -2639,7 +2630,7 @@ if rdma.found() endif =20 have_asan_fiber =3D false -if get_option('asan') and \ +if sanitize.contains('address') and \ not cc.has_function('__sanitizer_start_switch_fiber', args: '-fsanitize=3Daddress', prefix: '#include ') @@ -4768,7 +4759,9 @@ summary_info +=3D {'memory allocator': get_option('m= alloc')} summary_info +=3D {'avx2 optimization': config_host_data.get('CONFIG_AVX2_= OPT')} summary_info +=3D {'avx512bw optimization': config_host_data.get('CONFIG_A= VX512BW_OPT')} summary_info +=3D {'gcov': get_option('b_coverage')} -summary_info +=3D {'thread sanitizer': get_option('tsan')} +summary_info +=3D {'address sanitizer': sanitize.contains('address')} +summary_info +=3D {'undefined behavior sanitizer': sanitize.contains('unde= fined')} +summary_info +=3D {'thread sanitizer': sanitize.contains('thread')} summary_info +=3D {'CFI support': get_option('cfi')} if get_option('cfi') summary_info +=3D {'CFI debug support': get_option('cfi_debug')} diff --git a/meson_options.txt b/meson_options.txt index a07cb47d35..d61c0241cc 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -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, diff --git a/scripts/meson-buildoptions.sh b/scripts/meson-buildoptions.sh index c003985047..8d3b992ed7 100644 --- a/scripts/meson-buildoptions.sh +++ b/scripts/meson-buildoptions.sh @@ -21,7 +21,6 @@ meson_options_help() { printf "%s\n" ' --disable-relocatable toggle relocatable install' printf "%s\n" ' --docdir=3DVALUE Base directory for documenta= tion 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 to= ols instead of only' printf "%s\n" ' QEMU' @@ -54,8 +53,6 @@ meson_options_help() { printf "%s\n" ' --enable-trace-backends=3DCHOICES' printf "%s\n" ' Set available tracing backends= [log] (choices:' printf "%s\n" ' dtrace/ftrace/log/nop/simple/s= yslog/ust)' - printf "%s\n" ' --enable-tsan enable thread sanitizer' - printf "%s\n" ' --enable-ubsan enable undefined behaviour san= itizer' printf "%s\n" ' --firmwarepath=3DVALUES search PATH for firmware fil= es [share/qemu-' printf "%s\n" ' firmware]' printf "%s\n" ' --iasl=3DVALUE Path to ACPI disassembler' @@ -238,8 +235,6 @@ _meson_option_parse() { --disable-af-xdp) printf "%s" -Daf_xdp=3Ddisabled ;; --enable-alsa) printf "%s" -Dalsa=3Denabled ;; --disable-alsa) printf "%s" -Dalsa=3Ddisabled ;; - --enable-asan) printf "%s" -Dasan=3Dtrue ;; - --disable-asan) printf "%s" -Dasan=3Dfalse ;; --enable-attr) printf "%s" -Dattr=3Denabled ;; --disable-attr) printf "%s" -Dattr=3Ddisabled ;; --audio-drv-list=3D*) quote_sh "-Daudio_drv_list=3D$2" ;; @@ -521,14 +516,10 @@ _meson_option_parse() { --disable-tpm) printf "%s" -Dtpm=3Ddisabled ;; --enable-trace-backends=3D*) quote_sh "-Dtrace_backends=3D$2" ;; --with-trace-file=3D*) quote_sh "-Dtrace_file=3D$2" ;; - --enable-tsan) printf "%s" -Dtsan=3Dtrue ;; - --disable-tsan) printf "%s" -Dtsan=3Dfalse ;; --enable-u2f) printf "%s" -Du2f=3Denabled ;; --disable-u2f) printf "%s" -Du2f=3Ddisabled ;; --enable-uadk) printf "%s" -Duadk=3Denabled ;; --disable-uadk) printf "%s" -Duadk=3Ddisabled ;; - --enable-ubsan) printf "%s" -Dubsan=3Dtrue ;; - --disable-ubsan) printf "%s" -Dubsan=3Dfalse ;; --enable-usb-redir) printf "%s" -Dusb_redir=3Denabled ;; --disable-usb-redir) printf "%s" -Dusb_redir=3Ddisabled ;; --enable-valgrind) printf "%s" -Dvalgrind=3Denabled ;; diff --git a/tests/functional/x86_64/meson.build b/tests/functional/x86_64/= meson.build index 1ed10ad6c2..54af748f15 100644 --- a/tests/functional/x86_64/meson.build +++ b/tests/functional/x86_64/meson.build @@ -24,7 +24,7 @@ tests_x86_64_system_quick =3D [ # 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 +=3D [ 'memlock' ] endif =20 diff --git a/tests/unit/meson.build b/tests/unit/meson.build index 01cc540a45..b0ba64d0ec 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -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 !=3D 'windows' tests +=3D { 'test-char': ['socket-helpers.c', qom, io, chardev] --=20 2.55.0.rc0.799.gd6f94ed593-goog