Skip to content

Commit bb8230b

Browse files
authored
[sanitizer] Internalize .preinit_array variables
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: #98584
1 parent a288d8d commit bb8230b

File tree

8 files changed

+27
-36
lines changed

8 files changed

+27
-36
lines changed

compiler-rt/lib/asan/asan_preinit.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
using namespace __asan;
1616

1717
#if SANITIZER_CAN_USE_PREINIT_ARRAY
18-
// The symbol is called __local_asan_preinit, because it's not intended to be
19-
// exported.
20-
// This code linked into the main executable when -fsanitize=address is in
21-
// the link flags. It can only use exported interface functions.
22-
__attribute__((section(".preinit_array"), used))
23-
void (*__local_asan_preinit)(void) = __asan_init;
18+
// This section is linked into the main executable when -fsanitize=hwaddress is
19+
// specified to perform initialization at a very early stage.
20+
__attribute__((section(".preinit_array"), used)) static auto preinit =
21+
__asan_init;
2422
#endif

compiler-rt/lib/hwasan/hwasan_preinit.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
#include "sanitizer_common/sanitizer_internal_defs.h"
1515

1616
#if SANITIZER_CAN_USE_PREINIT_ARRAY
17-
// The symbol is called __local_hwasan_preinit, because it's not intended to
18-
// be exported.
19-
// This code linked into the main executable when -fsanitize=hwaddress is in
20-
// the link flags. It can only use exported interface functions.
21-
__attribute__((section(".preinit_array"), used)) static void (
22-
*__local_hwasan_preinit)(void) = __hwasan_init;
17+
// This section is linked into the main executable when -fsanitize=hwaddress is
18+
// specified to perform initialization at a very early stage.
19+
__attribute__((section(".preinit_array"), used)) static auto preinit =
20+
__hwasan_init;
2321
#endif

compiler-rt/lib/lsan/lsan_preinit.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
#include "lsan.h"
1515

1616
#if SANITIZER_CAN_USE_PREINIT_ARRAY
17-
// We force __lsan_init to be called before anyone else by placing it into
18-
// .preinit_array section.
19-
__attribute__((section(".preinit_array"), used))
20-
void (*__local_lsan_preinit)(void) = __lsan_init;
17+
// This section is linked into the main executable when -fsanitize=leak is
18+
// specified to perform initialization at a very early stage.
19+
__attribute__((section(".preinit_array"), used)) static auto preinit =
20+
__lsan_init;
2121
#endif

compiler-rt/lib/memprof/memprof_preinit.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
using namespace __memprof;
1616

1717
#if SANITIZER_CAN_USE_PREINIT_ARRAY
18-
// The symbol is called __local_memprof_preinit, because it's not intended to
19-
// be exported. This code linked into the main executable when -fmemory-profile
20-
// is in the link flags. It can only use exported interface functions.
21-
__attribute__((section(".preinit_array"),
22-
used)) void (*__local_memprof_preinit)(void) = __memprof_preinit;
18+
// This section is linked into the main executable when -fmemory-profile is
19+
// specified to perform initialization at a very early stage.
20+
__attribute__((section(".preinit_array"), used)) static auto preinit =
21+
__memprof_preinit;
2322
#endif

compiler-rt/lib/rtsan/rtsan_preinit.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313

1414
#if SANITIZER_CAN_USE_PREINIT_ARRAY
1515

16-
// The symbol is called __local_rtsan_preinit, because it's not intended to be
17-
// exported.
18-
// This code is linked into the main executable when -fsanitize=realtime is in
19-
// the link flags. It can only use exported interface functions.
20-
__attribute__((section(".preinit_array"),
21-
used)) void (*__local_rtsan_preinit)(void) = __rtsan_init;
16+
// This section is linked into the main executable when -fsanitize=realtime is
17+
// specified to perform initialization at a very early stage.
18+
__attribute__((section(".preinit_array"), used)) static auto preinit =
19+
__rtsan_init;
2220

2321
#endif

compiler-rt/lib/tsan/rtl/tsan_preinit.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616

1717
#if SANITIZER_CAN_USE_PREINIT_ARRAY
1818

19-
// The symbol is called __local_tsan_preinit, because it's not intended to be
20-
// exported.
21-
// This code linked into the main executable when -fsanitize=thread is in
22-
// the link flags. It can only use exported interface functions.
23-
__attribute__((section(".preinit_array"), used))
24-
void (*__local_tsan_preinit)(void) = __tsan_init;
19+
// This section is linked into the main executable when -fsanitize=thread is
20+
// specified to perform initialization at a very early stage.
21+
__attribute__((section(".preinit_array"), used)) static auto preinit =
22+
__tsan_init;
2523

2624
#endif

compiler-rt/lib/ubsan/ubsan_init_standalone_preinit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ static void PreInitAsStandalone() {
3030

3131
} // namespace __ubsan
3232

33-
__attribute__((section(".preinit_array"), used)) void (*__local_ubsan_preinit)(
34-
void) = __ubsan::PreInitAsStandalone;
33+
__attribute__((section(".preinit_array"), used)) static auto preinit =
34+
__ubsan::PreInitAsStandalone;
3535
#endif // SANITIZER_CAN_USE_PREINIT_ARRAY

compiler-rt/test/tsan/Linux/check_preinit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// RUN: %t.so && \
33
// RUN: %clang_tsan -O1 %s %t.so -o %t && %run %t 2>&1 | FileCheck %s
44
// RUN: llvm-objdump -t %t | FileCheck %s --check-prefix=CHECK-DUMP
5-
// CHECK-DUMP: {{[.]preinit_array.*__local_tsan_preinit}}
5+
// CHECK-DUMP: {{[.]preinit_array.*preinit}}
66

77
// SANITIZER_CAN_USE_PREINIT_ARRAY is undefined on android.
88
// UNSUPPORTED: android

0 commit comments

Comments
 (0)