summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Ulmann <christian.ulmann@nextsilicon.com>2024-03-01 16:50:30 +0000
committerChristian Ulmann <christian.ulmann@nextsilicon.com>2024-03-01 16:50:30 +0000
commitaa2a5abff7b3c7790f4863abae504f3cd426b35b (patch)
treee23d20e1aadea9742196cc6b3abb1715286775e7
parentdca32a3b594b3c91f9766a9312b5d82534910fa1 (diff)
[CMake][ASAN] Add support for ADDRESS_SANITIZER_BUILD compile optionupstream/users/dinistro/asan-compile-option
This commit introduces the `LLVM_ADDRESS_SANITIZER_BUILD` compile option that is set to indicate if LLVM has been built with ASAN enabled or not. This is a necessary addition to properly control the declarations of `__asan_*` helpers in header files. Previously, some of these declarations relied on `__has_feature(address_sanitizer)`, which can lead to differences between in-tree compilation units and down-stream usages of the same headers. The reproducer of this issue involved templates, which then lead to violation of the ODR due to having two different instances of the same template.
-rw-r--r--llvm/cmake/modules/HandleLLVMOptions.cmake13
-rw-r--r--llvm/include/llvm/Support/Compiler.h7
2 files changed, 17 insertions, 3 deletions
diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake
index 08ff49ded57a..cec18e3a4a73 100644
--- a/llvm/cmake/modules/HandleLLVMOptions.cmake
+++ b/llvm/cmake/modules/HandleLLVMOptions.cmake
@@ -940,6 +940,7 @@ if(LLVM_USE_SANITIZER)
if (LLVM_USE_SANITIZER STREQUAL "Address")
append_common_sanitizer_flags()
append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
+ set(LLVM_ADDRESS_SANITIZER_BUILD ON)
elseif (LLVM_USE_SANITIZER STREQUAL "HWAddress")
append_common_sanitizer_flags()
append("-fsanitize=hwaddress" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
@@ -961,6 +962,7 @@ if(LLVM_USE_SANITIZER)
LLVM_USE_SANITIZER STREQUAL "Undefined;Address")
append_common_sanitizer_flags()
append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
+ set(LLVM_ADDRESS_SANITIZER_BUILD ON)
append("${LLVM_UBSAN_FLAGS}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
elseif (LLVM_USE_SANITIZER STREQUAL "Leaks")
append_common_sanitizer_flags()
@@ -972,6 +974,7 @@ if(LLVM_USE_SANITIZER)
if (LLVM_USE_SANITIZER STREQUAL "Address")
append_common_sanitizer_flags()
append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
+ set(LLVM_ADDRESS_SANITIZER_BUILD ON)
elseif (LLVM_USE_SANITIZER STREQUAL "Undefined")
append_common_sanitizer_flags()
append("${LLVM_UBSAN_FLAGS}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
@@ -979,6 +982,7 @@ if(LLVM_USE_SANITIZER)
LLVM_USE_SANITIZER STREQUAL "Undefined;Address")
append_common_sanitizer_flags()
append("-fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
+ set(LLVM_ADDRESS_SANITIZER_BUILD ON)
append("${LLVM_UBSAN_FLAGS}" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
else()
message(FATAL_ERROR "This sanitizer not yet supported in a MinGW environment: ${LLVM_USE_SANITIZER}")
@@ -1008,11 +1012,13 @@ if(LLVM_USE_SANITIZER)
append("clang_rt.asan_dynamic-${arch}.lib /wholearchive:clang_rt.asan_dynamic_runtime_thunk-${arch}.lib"
CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
endif()
+ set(LLVM_ADDRESS_SANITIZER_BUILD ON)
endif()
endif()
if (LLVM_USE_SANITIZER MATCHES ".*Address.*")
if (NOT CLANG_CL)
append("/fsanitize=address" CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
+ set(LLVM_ADDRESS_SANITIZER_BUILD ON)
# Not compatible with /RTC flags.
foreach (flags_opt_to_scrub
CMAKE_CXX_FLAGS_${uppercase_CMAKE_BUILD_TYPE} CMAKE_C_FLAGS_${uppercase_CMAKE_BUILD_TYPE})
@@ -1046,6 +1052,13 @@ if(LLVM_USE_SANITIZER)
endif()
endif()
+# Add a compile option to indicate if LLVM is building with address sanitizers.
+if(LLVM_ADDRESS_SANITIZER_BUILD)
+ add_compile_definitions(LLVM_ADDRESS_SANITIZER_BUILD=1)
+else()
+ add_compile_definitions(LLVM_ADDRESS_SANITIZER_BUILD=0)
+endif()
+
# Turn on -gsplit-dwarf if requested in debug builds.
if (LLVM_USE_SPLIT_DWARF AND
((uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") OR
diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h
index 8c315d255bb7..421212898644 100644
--- a/llvm/include/llvm/Support/Compiler.h
+++ b/llvm/include/llvm/Support/Compiler.h
@@ -438,8 +438,10 @@
/// \macro LLVM_ADDRESS_SANITIZER_BUILD
/// Whether LLVM itself is built with AddressSanitizer instrumentation.
-#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
-# define LLVM_ADDRESS_SANITIZER_BUILD 1
+#ifndef LLVM_ADDRESS_SANITIZER_BUILD
+# define LLVM_ADDRESS_SANITIZER_BUILD 0
+#endif
+#if LLVM_ADDRESS_SANITIZER_BUILD
#if __has_include(<sanitizer/asan_interface.h>)
# include <sanitizer/asan_interface.h>
#else
@@ -455,7 +457,6 @@ void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
#endif
#endif
#else
-# define LLVM_ADDRESS_SANITIZER_BUILD 0
# define __asan_poison_memory_region(p, size)
# define __asan_unpoison_memory_region(p, size)
#endif