[compiler-rt] [sanitizers] Do not define __has_feature in sanitizer/common_interface_defs.h (PR #66628)
Jonathan Wakely via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 18 03:34:56 PDT 2023
https://github.com/jwakely created https://github.com/llvm/llvm-project/pull/66628
Public headers intended for user code should not define `__has_feature`, because this can break preprocessor checks done later in user code, e.g. if they test `#ifdef __has_feature` to check for real support in the compiler.
Replace the only use in the public header with a check for it being supported before trying to use it. Define the fallback definition in the internal headers, so that other internal sanitizer headers can continue to use it as preferred.
This resolves a bug reported to GCC as https://gcc.gnu.org/PR109882
>From b67dd2579c465c07e62d031c600c10299512e27c Mon Sep 17 00:00:00 2001
From: Jonathan Wakely <jwakely at redhat.com>
Date: Thu, 18 May 2023 13:22:23 +0100
Subject: [PATCH] [sanitizers] Do not define __has_feature in
sanitizer/common_interface_defs.h
Public headers intended for user code should not define __has_feature,
because this can break preprocessor checks done later in user code, e.g.
if they test #ifdef __has_feature to check for real support in the
compiler.
Replace the only use in the public header with a check for it being
supported before trying to use it. Define the fallback definition in the
internal headers, so that other internal sanitizer headers can continue
to use it as preferred.
This resolves a bug reported to GCC as https://gcc.gnu.org/PR109882
---
compiler-rt/include/sanitizer/asan_interface.h | 11 ++++++++++-
compiler-rt/include/sanitizer/common_interface_defs.h | 5 -----
.../lib/sanitizer_common/sanitizer_internal_defs.h | 5 +++++
3 files changed, 15 insertions(+), 6 deletions(-)
diff --git a/compiler-rt/include/sanitizer/asan_interface.h b/compiler-rt/include/sanitizer/asan_interface.h
index 9bff21c117b39a2..186269ad69448bd 100644
--- a/compiler-rt/include/sanitizer/asan_interface.h
+++ b/compiler-rt/include/sanitizer/asan_interface.h
@@ -48,7 +48,15 @@ void __asan_poison_memory_region(void const volatile *addr, size_t size);
void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
// Macros provided for convenience.
-#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
+#ifdef __has_feature
+#if __has_feature(address_sanitizer)
+#define ASAN_DEFINE_REGION_MACROS
+#endif
+#elif defined(__SANITIZE_ADDRESS__)
+#define ASAN_DEFINE_REGION_MACROS
+#endif
+
+#ifdef ASAN_DEFINE_REGION_MACROS
/// Marks a memory region as unaddressable.
///
/// \note Macro provided for convenience; defined as a no-op if ASan is not
@@ -74,6 +82,7 @@ void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
#define ASAN_UNPOISON_MEMORY_REGION(addr, size) \
((void)(addr), (void)(size))
#endif
+#undef ASAN_DEFINE_REGION_MACROS
/// Checks if an address is poisoned.
///
diff --git a/compiler-rt/include/sanitizer/common_interface_defs.h b/compiler-rt/include/sanitizer/common_interface_defs.h
index 983df7cea16ed90..6078d695a4a601e 100644
--- a/compiler-rt/include/sanitizer/common_interface_defs.h
+++ b/compiler-rt/include/sanitizer/common_interface_defs.h
@@ -15,11 +15,6 @@
#include <stddef.h>
#include <stdint.h>
-// GCC does not understand __has_feature.
-#if !defined(__has_feature)
-#define __has_feature(x) 0
-#endif
-
#ifdef __cplusplus
extern "C" {
#endif
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h b/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h
index 552d65067944f5b..3809669dd48bb3b 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h
@@ -15,6 +15,11 @@
#include "sanitizer_platform.h"
#include "sanitizer_redefine_builtins.h"
+// GCC does not understand __has_feature.
+#if !defined(__has_feature)
+#define __has_feature(x) 0
+#endif
+
#ifndef SANITIZER_DEBUG
# define SANITIZER_DEBUG 0
#endif
More information about the llvm-commits
mailing list