[compiler-rt] [sanitizer] Internalize .preinit_array variables (PR #98584)
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 11 20:46:46 PDT 2024
https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/98584
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.
>From 56d1dc456f8e6c88385b5fa73791942c8b4cdf7c Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Thu, 11 Jul 2024 20:46:36 -0700
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
=?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.5-bogner
---
compiler-rt/lib/asan/asan_preinit.cpp | 10 ++++------
compiler-rt/lib/hwasan/hwasan_preinit.cpp | 6 ++----
compiler-rt/lib/lsan/lsan_preinit.cpp | 8 ++++----
compiler-rt/lib/memprof/memprof_preinit.cpp | 4 ++--
compiler-rt/lib/rtsan/rtsan_preinit.cpp | 6 ++----
compiler-rt/lib/tsan/rtl/tsan_preinit.cpp | 6 ++----
.../lib/ubsan/ubsan_init_standalone_preinit.cpp | 4 ++--
compiler-rt/test/tsan/Linux/check_preinit.cpp | 2 +-
8 files changed, 19 insertions(+), 27 deletions(-)
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
More information about the llvm-commits
mailing list