[compiler-rt] [asan] Implement address sanitizer on AIX: build configuration (PR #139583)

Jake Egan via llvm-commits llvm-commits at lists.llvm.org
Mon May 26 06:57:36 PDT 2025


https://github.com/jakeegan updated https://github.com/llvm/llvm-project/pull/139583

>From f2f6191cb56f8de1e075542b27eb89c732c88f6e Mon Sep 17 00:00:00 2001
From: Jake Egan <jake.egan at ibm.com>
Date: Mon, 12 May 2025 13:06:15 -0400
Subject: [PATCH 1/2] build config

---
 compiler-rt/cmake/Modules/AddCompilerRT.cmake |  18 +++
 compiler-rt/lib/asan/CMakeLists.txt           |  10 ++
 .../lib/asan/asan.link_with_main_exec.txt     | 115 ++++++++++++++++++
 .../lib/asan/asan_cxx.link_with_main_exec.txt |  21 ++++
 4 files changed, 164 insertions(+)
 create mode 100644 compiler-rt/lib/asan/asan.link_with_main_exec.txt
 create mode 100644 compiler-rt/lib/asan/asan_cxx.link_with_main_exec.txt

diff --git a/compiler-rt/cmake/Modules/AddCompilerRT.cmake b/compiler-rt/cmake/Modules/AddCompilerRT.cmake
index d346b0ec01b03..d475e08912a49 100644
--- a/compiler-rt/cmake/Modules/AddCompilerRT.cmake
+++ b/compiler-rt/cmake/Modules/AddCompilerRT.cmake
@@ -582,6 +582,24 @@ macro(add_compiler_rt_script name)
     DESTINATION ${COMPILER_RT_INSTALL_BINARY_DIR})
 endmacro(add_compiler_rt_script src name)
 
+
+macro(add_compiler_rt_cfg target_name file_name component)
+  set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
+  get_compiler_rt_output_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} output_dir)
+  set(dst_file "${output_dir}/${file_name}")
+  add_custom_command(OUTPUT ${dst_file}
+    DEPENDS ${src_file}
+    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${src_file} ${dst_file}
+    COMMENT "Copying ${file_name}...")
+  add_custom_target(${target_name} DEPENDS ${dst_file})
+  install(FILES ${file_name}
+    DESTINATION ${COMPILER_RT_INSTALL_LIBRARY_DIR}
+    COMPONENT ${component})
+  add_dependencies(${component} ${target_name})
+
+  set_target_properties(${target_name} PROPERTIES FOLDER "Compiler-RT Misc")
+endmacro()
+
 # Builds custom version of libc++ and installs it in <prefix>.
 # Can be used to build sanitized versions of libc++ for running unit tests.
 # add_custom_libcxx(<name> <prefix>
diff --git a/compiler-rt/lib/asan/CMakeLists.txt b/compiler-rt/lib/asan/CMakeLists.txt
index e2f39f224df9c..18762e26cfa92 100644
--- a/compiler-rt/lib/asan/CMakeLists.txt
+++ b/compiler-rt/lib/asan/CMakeLists.txt
@@ -281,6 +281,8 @@ else()
       PARENT_TARGET asan)
   endif()
 
+  # On AIX, we only need the static libraries.
+  if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
   foreach(arch ${ASAN_SUPPORTED_ARCH})
     if (COMPILER_RT_HAS_VERSION_SCRIPT)
       if(WIN32)
@@ -382,10 +384,18 @@ else()
       endif()
     endif()
   endforeach()
+  endif()
 endif()
 
 add_compiler_rt_resource_file(asan_ignorelist asan_ignorelist.txt asan)
 
+# On AIX, we need to put asan.link_with_main_exec.txt and asan_cxx.link_with_main_exec.txt
+# to the build and install dir.
+if (${CMAKE_SYSTEM_NAME} MATCHES "AIX")
+  add_compiler_rt_cfg(asan_symbols asan.link_with_main_exec.txt asan)
+  add_compiler_rt_cfg(asan_cxx_symbols asan_cxx.link_with_main_exec.txt asan)
+endif()
+
 add_subdirectory(scripts)
 
 if(COMPILER_RT_INCLUDE_TESTS)
diff --git a/compiler-rt/lib/asan/asan.link_with_main_exec.txt b/compiler-rt/lib/asan/asan.link_with_main_exec.txt
new file mode 100644
index 0000000000000..5efc48c262369
--- /dev/null
+++ b/compiler-rt/lib/asan/asan.link_with_main_exec.txt
@@ -0,0 +1,115 @@
+#! .
+__asan_report_load_n
+__asan_loadN
+__asan_report_load1
+__asan_load1
+__asan_report_load2
+__asan_load2
+__asan_report_load4
+__asan_load4
+__asan_report_load8
+__asan_load8
+__asan_report_load16
+__asan_load16
+__asan_report_store_n
+__asan_storeN
+__asan_report_store1
+__asan_store1
+__asan_report_store2
+__asan_store2
+__asan_report_store4
+__asan_store4
+__asan_report_store8
+__asan_store8
+__asan_report_store16
+__asan_store16
+__asan_report_exp_load_n
+__asan_exp_loadN
+__asan_report_exp_load1
+__asan_exp_load1
+__asan_report_exp_load2
+__asan_exp_load2
+__asan_report_exp_load4
+__asan_exp_load4
+__asan_report_exp_load8
+__asan_exp_load8
+__asan_report_exp_load16
+__asan_exp_load16
+__asan_report_exp_store_n
+__asan_exp_storeN
+__asan_report_exp_store1
+__asan_exp_store1
+__asan_report_exp_store2
+__asan_exp_store2
+__asan_report_exp_store4
+__asan_exp_store4
+__asan_report_exp_store8
+__asan_exp_store8
+__asan_report_exp_store16
+__asan_exp_store16
+__asan_memmove
+__asan_memcpy
+__asan_memset
+__asan_handle_no_return
+__sanitizer_ptr_cmp
+__sanitizer_ptr_sub
+__asan_before_dynamic_init
+__asan_after_dynamic_init
+__asan_register_globals
+__asan_unregister_globals
+__asan_register_image_globals
+__asan_unregister_image_globals
+__asan_register_elf_globals
+__asan_unregister_elf_globals
+__asan_init
+__asan_version_mismatch_check_v8
+__asan_stack_malloc_0
+__asan_stack_malloc_1
+__asan_stack_malloc_2
+__asan_stack_malloc_3
+__asan_stack_malloc_4
+__asan_stack_malloc_5
+__asan_stack_malloc_6
+__asan_stack_malloc_7
+__asan_stack_malloc_8
+__asan_stack_malloc_9
+__asan_stack_malloc_10
+__asan_stack_malloc_always_0
+__asan_stack_malloc_always_1
+__asan_stack_malloc_always_2
+__asan_stack_malloc_always_3
+__asan_stack_malloc_always_4
+__asan_stack_malloc_always_5
+__asan_stack_malloc_always_6
+__asan_stack_malloc_always_7
+__asan_stack_malloc_always_8
+__asan_stack_malloc_always_9
+__asan_stack_malloc_always_10
+__asan_stack_free_0
+__asan_stack_free_1
+__asan_stack_free_2
+__asan_stack_free_3
+__asan_stack_free_4
+__asan_stack_free_5
+__asan_stack_free_6
+__asan_stack_free_7
+__asan_stack_free_8
+__asan_stack_free_9
+__asan_stack_free_10
+__asan_set_shadow_00
+__asan_set_shadow_01
+__asan_set_shadow_02
+__asan_set_shadow_03
+__asan_set_shadow_04
+__asan_set_shadow_05
+__asan_set_shadow_06
+__asan_set_shadow_07
+__asan_set_shadow_f1
+__asan_set_shadow_f2
+__asan_set_shadow_f3
+__asan_set_shadow_f5
+__asan_set_shadow_f8
+__asan_poison_stack_memory
+__asan_unpoison_stack_memory
+__asan_option_detect_stack_use_after_return
+__asan_shadow_memory_dynamic_address
diff --git a/compiler-rt/lib/asan/asan_cxx.link_with_main_exec.txt b/compiler-rt/lib/asan/asan_cxx.link_with_main_exec.txt
new file mode 100644
index 0000000000000..7387f8173e859
--- /dev/null
+++ b/compiler-rt/lib/asan/asan_cxx.link_with_main_exec.txt
@@ -0,0 +1,21 @@
+#! .
+_ZdaPv
+_ZdaPvRKSt9nothrow_t
+_ZdaPvSt11align_val_t
+_ZdaPvSt11align_val_tRKSt9nothrow_t
+_ZdaPvm
+_ZdaPvmSt11align_val_t
+_ZdlPv
+_ZdlPvRKSt9nothrow_t
+_ZdlPvSt11align_val_t
+_ZdlPvSt11align_val_tRKSt9nothrow_t
+_ZdlPvm
+_ZdlPvmSt11align_val_t
+_Znam
+_ZnamRKSt9nothrow_t
+_ZnamSt11align_val_t
+_ZnamSt11align_val_tRKSt9nothrow_t
+_Znwm
+_ZnwmRKSt9nothrow_t
+_ZnwmSt11align_val_t
+_ZnwmSt11align_val_tRKSt9nothrow_t

>From dfaa111872453656a75162d7a82c34d9ab67a481 Mon Sep 17 00:00:00 2001
From: Jake Egan <jake.egan at ibm.com>
Date: Sun, 25 May 2025 23:34:46 -0400
Subject: [PATCH 2/2] Update to user per target runtime dir and address
 comments

---
 compiler-rt/cmake/Modules/AddCompilerRT.cmake |  4 ++--
 compiler-rt/lib/asan/CMakeLists.txt           | 14 +++++++-------
 2 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/compiler-rt/cmake/Modules/AddCompilerRT.cmake b/compiler-rt/cmake/Modules/AddCompilerRT.cmake
index d475e08912a49..04856d86fa1ae 100644
--- a/compiler-rt/cmake/Modules/AddCompilerRT.cmake
+++ b/compiler-rt/cmake/Modules/AddCompilerRT.cmake
@@ -583,9 +583,9 @@ macro(add_compiler_rt_script name)
 endmacro(add_compiler_rt_script src name)
 
 
-macro(add_compiler_rt_cfg target_name file_name component)
+macro(add_compiler_rt_cfg target_name file_name component arch)
   set(src_file "${CMAKE_CURRENT_SOURCE_DIR}/${file_name}")
-  get_compiler_rt_output_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} output_dir)
+  get_compiler_rt_output_dir(${arch} output_dir)
   set(dst_file "${output_dir}/${file_name}")
   add_custom_command(OUTPUT ${dst_file}
     DEPENDS ${src_file}
diff --git a/compiler-rt/lib/asan/CMakeLists.txt b/compiler-rt/lib/asan/CMakeLists.txt
index 18762e26cfa92..5b58f3507d855 100644
--- a/compiler-rt/lib/asan/CMakeLists.txt
+++ b/compiler-rt/lib/asan/CMakeLists.txt
@@ -281,8 +281,6 @@ else()
       PARENT_TARGET asan)
   endif()
 
-  # On AIX, we only need the static libraries.
-  if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
   foreach(arch ${ASAN_SUPPORTED_ARCH})
     if (COMPILER_RT_HAS_VERSION_SCRIPT)
       if(WIN32)
@@ -384,16 +382,18 @@ else()
       endif()
     endif()
   endforeach()
-  endif()
 endif()
 
 add_compiler_rt_resource_file(asan_ignorelist asan_ignorelist.txt asan)
 
-# On AIX, we need to put asan.link_with_main_exec.txt and asan_cxx.link_with_main_exec.txt
-# to the build and install dir.
+# 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 to resolve undefined sanitizer symbols at runtime.
 if (${CMAKE_SYSTEM_NAME} MATCHES "AIX")
-  add_compiler_rt_cfg(asan_symbols asan.link_with_main_exec.txt asan)
-  add_compiler_rt_cfg(asan_cxx_symbols asan_cxx.link_with_main_exec.txt asan)
+  foreach(arch ${ASAN_SUPPORTED_ARCH})
+    add_compiler_rt_cfg(asan_symbols_${arch} asan.link_with_main_exec.txt asan ${arch})
+    add_compiler_rt_cfg(asan_cxx_symbols_${arch} asan_cxx.link_with_main_exec.txt asan ${arch})
+  endforeach()
 endif()
 
 add_subdirectory(scripts)



More information about the llvm-commits mailing list