[compiler-rt] [sanitizer] Internalize .preinit_array variables (PR #98584)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 11 20:47:14 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Fangrui Song (MaskRay)

<details>
<summary>Changes</summary>

We can use an internal linkage variable to make it clear the variable is
not exported. The special section .preinit_array is a GC root.


---
Full diff: https://github.com/llvm/llvm-project/pull/98584.diff


8 Files Affected:

- (modified) compiler-rt/lib/asan/asan_preinit.cpp (+4-6) 
- (modified) compiler-rt/lib/hwasan/hwasan_preinit.cpp (+2-4) 
- (modified) compiler-rt/lib/lsan/lsan_preinit.cpp (+4-4) 
- (modified) compiler-rt/lib/memprof/memprof_preinit.cpp (+2-2) 
- (modified) compiler-rt/lib/rtsan/rtsan_preinit.cpp (+2-4) 
- (modified) compiler-rt/lib/tsan/rtl/tsan_preinit.cpp (+2-4) 
- (modified) compiler-rt/lib/ubsan/ubsan_init_standalone_preinit.cpp (+2-2) 
- (modified) compiler-rt/test/tsan/Linux/check_preinit.cpp (+1-1) 


``````````diff
diff --git a/compiler-rt/lib/asan/asan_preinit.cpp b/compiler-rt/lib/asan/asan_preinit.cpp
index b07556ec96f8f..142f40058afe1 100644
--- a/compiler-rt/lib/asan/asan_preinit.cpp
+++ b/compiler-rt/lib/asan/asan_preinit.cpp
@@ -15,10 +15,8 @@
 using namespace __asan;
 
 #if SANITIZER_CAN_USE_PREINIT_ARRAY
-  // The symbol is called __local_asan_preinit, because it's not intended to be
-  // exported.
-  // This code linked into the main executable when -fsanitize=address is in
-  // the link flags. It can only use exported interface functions.
-  __attribute__((section(".preinit_array"), used))
-  void (*__local_asan_preinit)(void) = __asan_init;
+// This code linked into the main executable when -fsanitize=address is in
+// the link flags. It can only use exported interface functions.
+__attribute__((section(".preinit_array"), used)) static auto preinit =
+    __asan_init;
 #endif
diff --git a/compiler-rt/lib/hwasan/hwasan_preinit.cpp b/compiler-rt/lib/hwasan/hwasan_preinit.cpp
index 8c9c95f413be3..b463d92bdb594 100644
--- a/compiler-rt/lib/hwasan/hwasan_preinit.cpp
+++ b/compiler-rt/lib/hwasan/hwasan_preinit.cpp
@@ -14,10 +14,8 @@
 #include "sanitizer_common/sanitizer_internal_defs.h"
 
 #if SANITIZER_CAN_USE_PREINIT_ARRAY
-// The symbol is called __local_hwasan_preinit, because it's not intended to
-// be exported.
 // This code linked into the main executable when -fsanitize=hwaddress is in
 // the link flags. It can only use exported interface functions.
-__attribute__((section(".preinit_array"), used)) static void (
-    *__local_hwasan_preinit)(void) = __hwasan_init;
+__attribute__((section(".preinit_array"), used)) static auto preinit =
+    __hwasan_init;
 #endif
diff --git a/compiler-rt/lib/lsan/lsan_preinit.cpp b/compiler-rt/lib/lsan/lsan_preinit.cpp
index cd94e1e8718e6..0591e99810e18 100644
--- a/compiler-rt/lib/lsan/lsan_preinit.cpp
+++ b/compiler-rt/lib/lsan/lsan_preinit.cpp
@@ -14,8 +14,8 @@
 #include "lsan.h"
 
 #if SANITIZER_CAN_USE_PREINIT_ARRAY
-  // We force __lsan_init to be called before anyone else by placing it into
-  // .preinit_array section.
-  __attribute__((section(".preinit_array"), used))
-  void (*__local_lsan_preinit)(void) = __lsan_init;
+// We force __lsan_init to be called before anyone else by placing it into
+// .preinit_array section.
+__attribute__((section(".preinit_array"), used)) static auto preinit =
+    __lsan_init;
 #endif
diff --git a/compiler-rt/lib/memprof/memprof_preinit.cpp b/compiler-rt/lib/memprof/memprof_preinit.cpp
index 7092cd4ee5564..9c585906a4057 100644
--- a/compiler-rt/lib/memprof/memprof_preinit.cpp
+++ b/compiler-rt/lib/memprof/memprof_preinit.cpp
@@ -18,6 +18,6 @@ using namespace __memprof;
 // The symbol is called __local_memprof_preinit, because it's not intended to
 // be exported. This code linked into the main executable when -fmemory-profile
 // is in the link flags. It can only use exported interface functions.
-__attribute__((section(".preinit_array"),
-               used)) void (*__local_memprof_preinit)(void) = __memprof_preinit;
+__attribute__((section(".preinit_array"), used)) static auto preinit =
+    __memprof_preinit;
 #endif
diff --git a/compiler-rt/lib/rtsan/rtsan_preinit.cpp b/compiler-rt/lib/rtsan/rtsan_preinit.cpp
index 8cea61c3ea8b7..a5667057e3774 100644
--- a/compiler-rt/lib/rtsan/rtsan_preinit.cpp
+++ b/compiler-rt/lib/rtsan/rtsan_preinit.cpp
@@ -13,11 +13,9 @@
 
 #if SANITIZER_CAN_USE_PREINIT_ARRAY
 
-// The symbol is called __local_rtsan_preinit, because it's not intended to be
-// exported.
 // This code is linked into the main executable when -fsanitize=realtime is in
 // the link flags. It can only use exported interface functions.
-__attribute__((section(".preinit_array"),
-               used)) void (*__local_rtsan_preinit)(void) = __rtsan_init;
+__attribute__((section(".preinit_array"), used)) static auto preinit =
+    __rtsan_init;
 
 #endif
diff --git a/compiler-rt/lib/tsan/rtl/tsan_preinit.cpp b/compiler-rt/lib/tsan/rtl/tsan_preinit.cpp
index 205bdbf93b201..979d95cce47b8 100644
--- a/compiler-rt/lib/tsan/rtl/tsan_preinit.cpp
+++ b/compiler-rt/lib/tsan/rtl/tsan_preinit.cpp
@@ -16,11 +16,9 @@
 
 #if SANITIZER_CAN_USE_PREINIT_ARRAY
 
-// The symbol is called __local_tsan_preinit, because it's not intended to be
-// exported.
 // This code linked into the main executable when -fsanitize=thread is in
 // the link flags. It can only use exported interface functions.
-__attribute__((section(".preinit_array"), used))
-void (*__local_tsan_preinit)(void) = __tsan_init;
+__attribute__((section(".preinit_array"), used)) static auto preinit =
+    __tsan_init;
 
 #endif
diff --git a/compiler-rt/lib/ubsan/ubsan_init_standalone_preinit.cpp b/compiler-rt/lib/ubsan/ubsan_init_standalone_preinit.cpp
index fabbf919a4022..8a2a631834b94 100644
--- a/compiler-rt/lib/ubsan/ubsan_init_standalone_preinit.cpp
+++ b/compiler-rt/lib/ubsan/ubsan_init_standalone_preinit.cpp
@@ -30,6 +30,6 @@ static void PreInitAsStandalone() {
 
 } // namespace __ubsan
 
-__attribute__((section(".preinit_array"), used)) void (*__local_ubsan_preinit)(
-    void) = __ubsan::PreInitAsStandalone;
+__attribute__((section(".preinit_array"), used)) static auto preinit =
+    __ubsan::PreInitAsStandalone;
 #endif // SANITIZER_CAN_USE_PREINIT_ARRAY
diff --git a/compiler-rt/test/tsan/Linux/check_preinit.cpp b/compiler-rt/test/tsan/Linux/check_preinit.cpp
index b5f63d3d4b9e3..3b197139f3672 100644
--- a/compiler-rt/test/tsan/Linux/check_preinit.cpp
+++ b/compiler-rt/test/tsan/Linux/check_preinit.cpp
@@ -2,7 +2,7 @@
 // RUN:  %t.so && \
 // RUN:   %clang_tsan -O1 %s %t.so -o %t && %run %t 2>&1 | FileCheck %s
 // RUN: llvm-objdump -t %t | FileCheck %s --check-prefix=CHECK-DUMP
-// CHECK-DUMP:  {{[.]preinit_array.*__local_tsan_preinit}}
+// CHECK-DUMP:  {{[.]preinit_array.*preinit}}
 
 // SANITIZER_CAN_USE_PREINIT_ARRAY is undefined on android.
 // UNSUPPORTED: android

``````````

</details>


https://github.com/llvm/llvm-project/pull/98584


More information about the llvm-commits mailing list