Commit 4e6a31e9fc for bind

commit 4e6a31e9fcc79d4c13201605d6d45b7fe1bee75b
Author: Michal Nowak <mnowak@isc.org>
Date:   Tue Aug 11 10:40:40 2026 +0000

    Configure the sanitizer runtimes whenever they are in use

    Compiler wrappers can enable sanitizers without exposing their flags to
    Meson, so runtime configuration must also inspect compiler features.

    Assisted-by: Claude:claude-opus-5
    Assisted-by: Codex:gpt-6

diff --git a/meson.build b/meson.build
index f1c9a72f0f..000cb59c15 100644
--- a/meson.build
+++ b/meson.build
@@ -285,13 +285,61 @@ lsan_options = 'use_unaligned=1'
 tsan_options = 'disable_coredump=0:second_deadlock_stack=1:atexit_sleep_ms=1000:history_size=7:log_exe_name=true:log_path=tsan'
 ubsan_options = 'halt_on_error=1:abort_on_error=1:disable_coredump=0'

+# A sanitizer does not have to come from -Db_sanitize: the libfuzzer backend
+# turns ASan and UBSan on with project arguments of its own, and OSS-Fuzz hands
+# the flags over in CFLAGS.  The runtimes have to be configured either way.
+asan_enabled = sanitizer.contains('address')
+lsan_enabled = sanitizer.contains('leak')
+tsan_enabled = sanitizer.contains('thread')
+ubsan_enabled = sanitizer.contains('undefined')
+
+if fuzzing_backend_opt == 'libfuzzer'
+    asan_enabled = true
+    ubsan_enabled = true
+endif
+
+foreach arg : get_option('c_args')
+    if arg.startswith('-fsanitize=')
+        asan_enabled = asan_enabled or arg.contains('address')
+        lsan_enabled = lsan_enabled or arg.contains('leak')
+        tsan_enabled = tsan_enabled or arg.contains('thread')
+        ubsan_enabled = ubsan_enabled or arg.contains('undefined')
+    endif
+endforeach
+
+# Compiler wrappers can add sanitizer flags without exposing them in c_args.
+# Keep the option/argument checks above for compilers without __has_feature.
+compiler_sanitizers = []
+foreach name, feature : {
+    'address': 'address_sanitizer',
+    'leak': 'leak_sanitizer',
+    'thread': 'thread_sanitizer',
+}
+    sanitizer_probe = '''
+#ifndef __has_feature
+#define __has_feature(x) 0
+#endif
+#if defined(__SANITIZE_@0@__) || __has_feature(@1@)
+#define BIND_SANITIZER_ENABLED 1
+#else
+#define BIND_SANITIZER_ENABLED 0
+#endif
+'''.format(name.to_upper(), feature)
+    if cc.get_define('BIND_SANITIZER_ENABLED', prefix: sanitizer_probe) == '1'
+        compiler_sanitizers += name
+    endif
+endforeach
+asan_enabled = asan_enabled or compiler_sanitizers.contains('address')
+lsan_enabled = lsan_enabled or compiler_sanitizers.contains('leak')
+tsan_enabled = tsan_enabled or compiler_sanitizers.contains('thread')
+
 # Refer to sanitize.c.in for the justification
 config_sanitize = configuration_data()

-config_sanitize.set10('BIND_ASAN_ENABLED', sanitizer.contains('address'))
-config_sanitize.set10('BIND_LSAN_ENABLED', sanitizer.contains('leak'))
-config_sanitize.set10('BIND_TSAN_ENABLED', sanitizer.contains('thread'))
-config_sanitize.set10('BIND_UBSAN_ENABLED', sanitizer.contains('undefined'))
+config_sanitize.set10('BIND_ASAN_ENABLED', asan_enabled)
+config_sanitize.set10('BIND_LSAN_ENABLED', lsan_enabled)
+config_sanitize.set10('BIND_TSAN_ENABLED', tsan_enabled)
+config_sanitize.set10('BIND_UBSAN_ENABLED', ubsan_enabled)
 config_sanitize.set('BIND_ASAN_OPTIONS', asan_options)
 config_sanitize.set('BIND_LSAN_OPTIONS', lsan_options)
 config_sanitize.set('BIND_TSAN_OPTIONS', tsan_options)
diff --git a/sanitize.c.in b/sanitize.c.in
index 932824896b..693dd9ae2b 100644
--- a/sanitize.c.in
+++ b/sanitize.c.in
@@ -26,16 +26,15 @@
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wmissing-prototypes"

-/*
- * QP trie nodes contain pointers that are not always naturally aligned.
- * Always provide these defaults: OSS-Fuzz can enable sanitizers through
- * compiler flags without setting Meson's b_sanitize option.
- */
+/* ASAN will usually also enable LSAN */
+#if @BIND_ASAN_ENABLED@ || @BIND_LSAN_ENABLED@
+/* QP trie nodes contain pointers that are not always naturally aligned. */
 __attribute__((no_sanitize("address", "leak", "thread", "undefined")))
 const char *
 __lsan_default_options(void) {
 	return "@BIND_LSAN_OPTIONS@";
 }
+#endif

 #if @BIND_ASAN_ENABLED@
 __attribute__((no_sanitize("address", "leak", "thread", "undefined")))