[llvm] [CMake][ASAN] Add support for ADDRESS_SANITIZER_BUILD compile option (PR #83595)

Christian Ulmann via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 1 09:07:27 PST 2024


https://github.com/Dinistro created https://github.com/llvm/llvm-project/pull/83595

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.

This is a first attempt to resolve https://github.com/llvm/llvm-project/issues/83566, but it might require additional fixes for the other usages of `__has_feature()` in `Compiler.h`.

>From aa2a5abff7b3c7790f4863abae504f3cd426b35b Mon Sep 17 00:00:00 2001
From: Christian Ulmann <christian.ulmann at nextsilicon.com>
Date: Fri, 1 Mar 2024 16:50:30 +0000
Subject: [PATCH] [CMake][ASAN] Add support for ADDRESS_SANITIZER_BUILD 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.
---
 llvm/cmake/modules/HandleLLVMOptions.cmake | 13 +++++++++++++
 llvm/include/llvm/Support/Compiler.h       |  7 ++++---
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake
index 08ff49ded57a14..cec18e3a4a734a 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 8c315d255bb772..42121289864419 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



More information about the llvm-commits mailing list