Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sanitizer] Internalize .preinit_array variables #98584

Merged

Conversation

MaskRay
Copy link
Member

@MaskRay MaskRay commented Jul 12, 2024

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.

Created using spr 1.3.5-bogner
@MaskRay MaskRay requested a review from vitalybuka July 12, 2024 03:46
@llvmbot llvmbot added compiler-rt compiler-rt:asan Address sanitizer compiler-rt:tsan Thread sanitizer compiler-rt:ubsan Undefined behavior sanitizer compiler-rt:hwasan Hardware-assisted address sanitizer PGO Profile Guided Optimizations compiler-rt:lsan Leak sanitizer compiler-rt:sanitizer labels Jul 12, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jul 12, 2024

@llvm/pr-subscribers-pgo

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

Author: Fangrui Song (MaskRay)

Changes

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 --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

Created using spr 1.3.5-bogner
@MaskRay MaskRay merged commit bb8230b into main Jul 12, 2024
4 of 5 checks passed
@MaskRay MaskRay deleted the users/MaskRay/spr/sanitizer-internalize-preinit_array-variables branch July 12, 2024 18:15
aaryanshukla pushed a commit to aaryanshukla/llvm-project that referenced this pull request Jul 14, 2024
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.

Pull Request: llvm#98584
aaryanshukla pushed a commit to aaryanshukla/llvm-project that referenced this pull request Jul 16, 2024
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.

Pull Request: llvm#98584
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler-rt:asan Address sanitizer compiler-rt:hwasan Hardware-assisted address sanitizer compiler-rt:lsan Leak sanitizer compiler-rt:sanitizer compiler-rt:tsan Thread sanitizer compiler-rt:ubsan Undefined behavior sanitizer compiler-rt PGO Profile Guided Optimizations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants