summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAngus Gratton <angus@redyak.com.au>2025-08-06 10:19:43 +1000
committerAngus Gratton <angus@redyak.com.au>2025-08-19 10:33:40 +1000
commit3faf2298537ee34648d27a662529ff2aa73cec1c (patch)
treef12a1e6ea5cc97c6b249e80a3c6a6d9ec03fa082
parentd81d56cc4d7c44978761f967841bcb6e8e84b54d (diff)
py/misc: Add a way to detect sanitizer builds.
Clang and gcc>=14 can use __has_feature() to detect if a sanitizer is enabled, but older GCC has no mechanism - need to set a macro explicitly for this to be recognised. Necessary for increasing some resource limits in sanitizer builds. Important not to use to avoid real issues! This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
-rw-r--r--py/misc.h24
-rwxr-xr-xtools/ci.sh6
2 files changed, 28 insertions, 2 deletions
diff --git a/py/misc.h b/py/misc.h
index 5c1cc2f7a..081163cad 100644
--- a/py/misc.h
+++ b/py/misc.h
@@ -43,6 +43,11 @@ typedef unsigned int uint;
#ifndef __has_builtin
#define __has_builtin(x) (0)
#endif
+#ifndef __has_feature
+// This macro is supported by Clang and gcc>=14
+#define __has_feature(x) (0)
+#endif
+
/** generic ops *************************************************/
@@ -538,4 +543,23 @@ inline static bool mp_sub_ll_overflow(long long int lhs, long long int rhs, long
}
#endif
+
+// Helper macros for detecting if sanitizers are enabled
+//
+// Use sparingly, not for masking issues reported by sanitizers!
+//
+// Can be detected automatically in Clang and gcc>=14, need to be
+// set manually otherwise.
+#ifndef MP_UBSAN
+#define MP_UBSAN __has_feature(undefined_behavior_sanitizer)
+#endif
+
+#ifndef MP_ASAN
+#define MP_ASAN __has_feature(address_sanitizer)
+#endif
+
+#ifndef MP_SANITIZER_BUILD
+#define MP_SANITIZER_BUILD (MP_UBSAN || MP_ASAN)
+#endif
+
#endif // MICROPY_INCLUDED_PY_MISC_H
diff --git a/tools/ci.sh b/tools/ci.sh
index 095d96cf3..e165cb2cf 100755
--- a/tools/ci.sh
+++ b/tools/ci.sh
@@ -518,12 +518,14 @@ CI_UNIX_OPTS_QEMU_RISCV64=(
)
CI_UNIX_OPTS_SANITIZE_ADDRESS=(
- CFLAGS_EXTRA="-fsanitize=address --param asan-use-after-return=0"
+ # Macro MP_ASAN allows detecting ASan on gcc<=13
+ CFLAGS_EXTRA="-fsanitize=address --param asan-use-after-return=0 -DMP_ASAN=1"
LDFLAGS_EXTRA="-fsanitize=address --param asan-use-after-return=0"
)
CI_UNIX_OPTS_SANITIZE_UNDEFINED=(
- CFLAGS_EXTRA="-fsanitize=undefined -fno-sanitize=nonnull-attribute"
+ # Macro MP_UBSAN allows detecting UBSan on gcc<=13
+ CFLAGS_EXTRA="-fsanitize=undefined -fno-sanitize=nonnull-attribute -DMP_UBSAN=1"
LDFLAGS_EXTRA="-fsanitize=undefined -fno-sanitize=nonnull-attribute"
)