[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: https://github.com/llvm/llvm-project/pull/98584
This commit is contained in:
Fangrui Song 2024-07-12 11:15:46 -07:00 committed by GitHub
parent a288d8dad3
commit bb8230bb2b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 27 additions and 36 deletions

View File

@ -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 section is linked into the main executable when -fsanitize=hwaddress is
// specified to perform initialization at a very early stage.
__attribute__((section(".preinit_array"), used)) static auto preinit =
__asan_init;
#endif

View File

@ -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;
// This section is linked into the main executable when -fsanitize=hwaddress is
// specified to perform initialization at a very early stage.
__attribute__((section(".preinit_array"), used)) static auto preinit =
__hwasan_init;
#endif

View File

@ -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;
// This section is linked into the main executable when -fsanitize=leak is
// specified to perform initialization at a very early stage.
__attribute__((section(".preinit_array"), used)) static auto preinit =
__lsan_init;
#endif

View File

@ -15,9 +15,8 @@
using namespace __memprof;
#if SANITIZER_CAN_USE_PREINIT_ARRAY
// 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;
// This section is linked into the main executable when -fmemory-profile is
// specified to perform initialization at a very early stage.
__attribute__((section(".preinit_array"), used)) static auto preinit =
__memprof_preinit;
#endif

View File

@ -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;
// This section is linked into the main executable when -fsanitize=realtime is
// specified to perform initialization at a very early stage.
__attribute__((section(".preinit_array"), used)) static auto preinit =
__rtsan_init;
#endif

View File

@ -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;
// This section is linked into the main executable when -fsanitize=thread is
// specified to perform initialization at a very early stage.
__attribute__((section(".preinit_array"), used)) static auto preinit =
__tsan_init;
#endif

View File

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

View File

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