Skip to content

Commit 2cec041

Browse files
committed
[nsan] Add nsan_preinit.cpp and make it static library only
#94322 defines .preinit_array to initialize nsan early. DT_PREINIT_ARRAY can only be used with the main executable. GNU ld would complain when a DSO has .preinit_array .
1 parent cd6750f commit 2cec041

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

compiler-rt/lib/nsan/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ set(NSAN_SOURCES
1111
nsan_suppressions.cpp
1212
)
1313

14+
set(NSAN_PREINIT_SOURCES
15+
nsan_preinit.cpp)
16+
1417
set(NSAN_HEADERS
1518
nsan.h
1619
nsan_flags.h
@@ -61,6 +64,12 @@ if(NOT APPLE)
6164
ADDITIONAL_HEADERS ${NSAN_HEADERS}
6265
CFLAGS ${NSAN_CFLAGS})
6366

67+
add_compiler_rt_object_libraries(RTNsan_preinit
68+
ARCHS ${NSAN_SUPPORTED_ARCH}
69+
SOURCES ${NSAN_PREINIT_SOURCES}
70+
ADDITIONAL_HEADERS ${NSAN_HEADERS}
71+
CFLAGS ${NSAN_CFLAGS})
72+
6473
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp "")
6574
add_compiler_rt_object_libraries(RTNsan_dynamic_version_script_dummy
6675
ARCHS ${NSAN_SUPPORTED_ARCH}
@@ -72,7 +81,7 @@ add_compiler_rt_runtime(
7281
clang_rt.nsan
7382
STATIC
7483
ARCHS ${NSAN_SUPPORTED_ARCH}
75-
OBJECT_LIBS RTNsan
84+
OBJECT_LIBS RTNsan_preinit RTNsan
7685
${NSAN_COMMON_RUNTIME_OBJECT_LIBS}
7786
CFLAGS ${NSAN_CFLAGS}
7887
PARENT_TARGET nsan)

compiler-rt/lib/nsan/nsan.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __nsan_dump_shadow_args() {
779779
bool __nsan::nsan_initialized;
780780
bool __nsan::nsan_init_is_running;
781781

782-
extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __nsan_init() {
782+
extern "C" void __nsan_init() {
783783
CHECK(!nsan_init_is_running);
784784
if (nsan_initialized)
785785
return;
@@ -801,8 +801,3 @@ extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __nsan_init() {
801801
nsan_init_is_running = false;
802802
nsan_initialized = true;
803803
}
804-
805-
#if SANITIZER_CAN_USE_PREINIT_ARRAY
806-
__attribute__((section(".preinit_array"),
807-
used)) static void (*nsan_init_ptr)() = __nsan_init;
808-
#endif

compiler-rt/lib/nsan/nsan.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ using __sanitizer::uptr;
3232
// Private nsan interface. Used e.g. by interceptors.
3333
extern "C" {
3434

35+
void __nsan_init();
36+
3537
// This marks the shadow type of the given block of application memory as
3638
// unknown.
3739
// printf-free (see comment in nsan_interceptors.cc).

compiler-rt/lib/nsan/nsan_preinit.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===- nsan_preinit.cpp ---------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Call __nsan_init early using ELF DT_PREINIT_ARRAY.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "nsan/nsan.h"
14+
#include "sanitizer_common/sanitizer_internal_defs.h"
15+
16+
#if SANITIZER_CAN_USE_PREINIT_ARRAY
17+
18+
__attribute__((section(".preinit_array"), used)) static auto nsan_preinit =
19+
__nsan_init;
20+
21+
#endif

0 commit comments

Comments
 (0)