[compiler-rt] 8b45994 - [asan] Enable exceptions for asan_new_delete.cpp translation unit (#200719)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 8 11:02:32 PDT 2026


Author: Justin T. Gibbs
Date: 2026-06-08T11:02:27-07:00
New Revision: 8b45994309c19752dd369e86487461f93c00532b

URL: https://github.com/llvm/llvm-project/commit/8b45994309c19752dd369e86487461f93c00532b
DIFF: https://github.com/llvm/llvm-project/commit/8b45994309c19752dd369e86487461f93c00532b.diff

LOG: [asan] Enable exceptions for asan_new_delete.cpp translation unit (#200719)

Groundwork for #196388.

Preparation for a follow-up change that lets the throwing operator new
wrappers in asan_new_delete.cpp throw std::bad_alloc on OOM. To support
that, asan_new_delete.cpp must be compiled with -fexceptions and must be
able to include <new> (i.e. without -nostdinc++). The rest of the ASan
runtime stays compiled with -fno-exceptions and -nostdinc++.

NOTE: Comments in CMakeLists.txt are written assuming the follow-on
      changes to use exceptions land, thus avoiding the need to update
      this file again.

Build-system changes:

* Define ASAN_CXX_CFLAGS as ASAN_CFLAGS minus -fno-exceptions and
-nostdinc++ plus -fexceptions. Define ASAN_DYNAMIC_CXX_CFLAGS likewise
relative to ASAN_DYNAMIC_CFLAGS. RTTI stays disabled; libstdc++ /
libc++abi supply the bad_alloc typeinfo.

* Static build: switch RTAsan_cxx (which already contains only
${ASAN_CXX_SOURCES} = asan_new_delete.cpp) from ASAN_CFLAGS to
ASAN_CXX_CFLAGS. RTAsan (the C slice) is unchanged.

* Dynamic build: split the previous single RTAsan_dynamic object library
into two — RTAsan_dynamic keeps ${ASAN_SOURCES} (no change in flags) and
a new RTAsan_dynamic_cxx carries ${ASAN_CXX_SOURCES} with
ASAN_DYNAMIC_CXX_CFLAGS. Both shared clang_rt.asan link rules pull in
RTAsan_dynamic_cxx alongside RTAsan_dynamic so the linked .so retains
the same set of object files; only the compile flags for
asan_new_delete.cpp change.

NFC at runtime — the asan_new_delete.cpp source as of this commit does
not yet use exceptions or include <new>, so the new flags are inert. The
follow-up commit makes use of them.

Assisted by: Claude Opus 4.7

Added: 
    

Modified: 
    compiler-rt/cmake/config-ix.cmake
    compiler-rt/lib/asan/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/compiler-rt/cmake/config-ix.cmake b/compiler-rt/cmake/config-ix.cmake
index d1d89c0a29f8a..2610abcd984fc 100644
--- a/compiler-rt/cmake/config-ix.cmake
+++ b/compiler-rt/cmake/config-ix.cmake
@@ -95,6 +95,7 @@ check_cxx_compiler_flag(-fPIC                COMPILER_RT_HAS_FPIC_FLAG)
 check_cxx_compiler_flag(-fPIE                COMPILER_RT_HAS_FPIE_FLAG)
 check_cxx_compiler_flag(-fno-builtin         COMPILER_RT_HAS_FNO_BUILTIN_FLAG)
 check_cxx_compiler_flag(-fno-exceptions      COMPILER_RT_HAS_FNO_EXCEPTIONS_FLAG)
+check_cxx_compiler_flag(-fexceptions         COMPILER_RT_HAS_FEXCEPTIONS_FLAG)
 check_cxx_compiler_flag(-fomit-frame-pointer COMPILER_RT_HAS_FOMIT_FRAME_POINTER_FLAG)
 check_cxx_compiler_flag(-funwind-tables      COMPILER_RT_HAS_FUNWIND_TABLES_FLAG)
 check_cxx_compiler_flag(-fno-stack-protector COMPILER_RT_HAS_FNO_STACK_PROTECTOR_FLAG)

diff  --git a/compiler-rt/lib/asan/CMakeLists.txt b/compiler-rt/lib/asan/CMakeLists.txt
index 6085f18426dff..e2c3996f356f7 100644
--- a/compiler-rt/lib/asan/CMakeLists.txt
+++ b/compiler-rt/lib/asan/CMakeLists.txt
@@ -143,6 +143,19 @@ set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
 append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
   -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
 
+# asan_new_delete.cpp throws std::bad_alloc, so the translation units that
+# build it need -fexceptions and access to <new> (drop -nostdinc++). Only
+# the C++ slices (RTAsan_cxx in the static build and RTAsan_dynamic_cxx in
+# the dynamic build) opt into these flags — the rest of the runtime keeps
+# -fno-exceptions / -nostdinc++.
+set(ASAN_CXX_CFLAGS ${ASAN_CFLAGS})
+list(REMOVE_ITEM ASAN_CXX_CFLAGS -fno-exceptions -nostdinc++)
+append_list_if(COMPILER_RT_HAS_FEXCEPTIONS_FLAG -fexceptions ASAN_CXX_CFLAGS)
+set(ASAN_DYNAMIC_CXX_CFLAGS ${ASAN_DYNAMIC_CFLAGS})
+list(REMOVE_ITEM ASAN_DYNAMIC_CXX_CFLAGS -fno-exceptions -nostdinc++)
+append_list_if(COMPILER_RT_HAS_FEXCEPTIONS_FLAG -fexceptions
+  ASAN_DYNAMIC_CXX_CFLAGS)
+
 # LLVM turns /OPT:ICF back on when LLVM_ENABLE_PDBs is set
 # we _REALLY_ need to turn it back off for ASAN, because the way
 # asan emulates weak functions from DLLs requires NOICF
@@ -165,11 +178,20 @@ append_list_if(MINGW "${MINGW_LIBRARIES}" ASAN_DYNAMIC_LIBS)
 add_compiler_rt_object_libraries(RTAsan_dynamic
   OS ${SANITIZER_COMMON_SUPPORTED_OS}
   ARCHS ${ASAN_SUPPORTED_ARCH}
-  SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
+  SOURCES ${ASAN_SOURCES}
   ADDITIONAL_HEADERS ${ASAN_HEADERS}
   CFLAGS ${ASAN_DYNAMIC_CFLAGS}
   DEFS ${ASAN_DYNAMIC_DEFINITIONS})
 
+# Separate object library for the dynamic build's C++ slice.
+add_compiler_rt_object_libraries(RTAsan_dynamic_cxx
+  OS ${SANITIZER_COMMON_SUPPORTED_OS}
+  ARCHS ${ASAN_SUPPORTED_ARCH}
+  SOURCES ${ASAN_CXX_SOURCES}
+  ADDITIONAL_HEADERS ${ASAN_HEADERS}
+  CFLAGS ${ASAN_DYNAMIC_CXX_CFLAGS}
+  DEFS ${ASAN_DYNAMIC_DEFINITIONS})
+
 if(NOT APPLE)
   add_compiler_rt_object_libraries(RTAsan
     ARCHS ${ASAN_SUPPORTED_ARCH}
@@ -181,7 +203,7 @@ if(NOT APPLE)
     ARCHS ${ASAN_SUPPORTED_ARCH}
     SOURCES ${ASAN_CXX_SOURCES}
     ADDITIONAL_HEADERS ${ASAN_HEADERS}
-    CFLAGS ${ASAN_CFLAGS}
+    CFLAGS ${ASAN_CXX_CFLAGS}
     DEFS ${ASAN_COMMON_DEFINITIONS})
   add_compiler_rt_object_libraries(RTAsan_static
     ARCHS ${ASAN_SUPPORTED_ARCH}
@@ -219,6 +241,7 @@ if(APPLE)
     OS ${SANITIZER_COMMON_SUPPORTED_OS}
     ARCHS ${ASAN_SUPPORTED_ARCH}
     OBJECT_LIBS RTAsan_dynamic
+                RTAsan_dynamic_cxx
                 RTInterception
                 RTSanitizerCommon
                 RTSanitizerCommonLibc
@@ -319,6 +342,7 @@ else()
       ARCHS ${arch}
       OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
               RTAsan_dynamic
+              RTAsan_dynamic_cxx
               # The only purpose of RTAsan_dynamic_version_script_dummy is to
               # carry a dependency of the shared runtime on the version script.
               # Replacing it with a straightforward
@@ -394,7 +418,7 @@ endif()
 
 add_compiler_rt_resource_file(asan_ignorelist asan_ignorelist.txt asan)
 
-# On AIX, static sanitizer libraries are not added to the DSO, so we need to put 
+# On AIX, static sanitizer libraries are not added to the DSO, so we need to put
 # asan.link_with_main_exec.txt and asan_cxx.link_with_main_exec.txt to the build
 # and install dir for use in resolving undefined sanitizer symbols at runtime.
 if ("${CMAKE_SYSTEM_NAME}" MATCHES "AIX")


        


More information about the llvm-commits mailing list