Skip to content

Commit

Permalink
[nsan] Add nsan_preinit.cpp and make it static library only
Browse files Browse the repository at this point in the history
#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 .
  • Loading branch information
MaskRay committed Jul 11, 2024
1 parent cd6750f commit 2cec041
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
11 changes: 10 additions & 1 deletion compiler-rt/lib/nsan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ set(NSAN_SOURCES
nsan_suppressions.cpp
)

set(NSAN_PREINIT_SOURCES
nsan_preinit.cpp)

set(NSAN_HEADERS
nsan.h
nsan_flags.h
Expand Down Expand Up @@ -61,6 +64,12 @@ if(NOT APPLE)
ADDITIONAL_HEADERS ${NSAN_HEADERS}
CFLAGS ${NSAN_CFLAGS})

add_compiler_rt_object_libraries(RTNsan_preinit
ARCHS ${NSAN_SUPPORTED_ARCH}
SOURCES ${NSAN_PREINIT_SOURCES}
ADDITIONAL_HEADERS ${NSAN_HEADERS}
CFLAGS ${NSAN_CFLAGS})

file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp "")
add_compiler_rt_object_libraries(RTNsan_dynamic_version_script_dummy
ARCHS ${NSAN_SUPPORTED_ARCH}
Expand All @@ -72,7 +81,7 @@ add_compiler_rt_runtime(
clang_rt.nsan
STATIC
ARCHS ${NSAN_SUPPORTED_ARCH}
OBJECT_LIBS RTNsan
OBJECT_LIBS RTNsan_preinit RTNsan
${NSAN_COMMON_RUNTIME_OBJECT_LIBS}
CFLAGS ${NSAN_CFLAGS}
PARENT_TARGET nsan)
Expand Down
7 changes: 1 addition & 6 deletions compiler-rt/lib/nsan/nsan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __nsan_dump_shadow_args() {
bool __nsan::nsan_initialized;
bool __nsan::nsan_init_is_running;

extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __nsan_init() {
extern "C" void __nsan_init() {
CHECK(!nsan_init_is_running);
if (nsan_initialized)
return;
Expand All @@ -801,8 +801,3 @@ extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __nsan_init() {
nsan_init_is_running = false;
nsan_initialized = true;
}

#if SANITIZER_CAN_USE_PREINIT_ARRAY
__attribute__((section(".preinit_array"),
used)) static void (*nsan_init_ptr)() = __nsan_init;
#endif
2 changes: 2 additions & 0 deletions compiler-rt/lib/nsan/nsan.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ using __sanitizer::uptr;
// Private nsan interface. Used e.g. by interceptors.
extern "C" {

void __nsan_init();

// This marks the shadow type of the given block of application memory as
// unknown.
// printf-free (see comment in nsan_interceptors.cc).
Expand Down
21 changes: 21 additions & 0 deletions compiler-rt/lib/nsan/nsan_preinit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===- nsan_preinit.cpp ---------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Call __nsan_init early using ELF DT_PREINIT_ARRAY.
//
//===----------------------------------------------------------------------===//

#include "nsan/nsan.h"
#include "sanitizer_common/sanitizer_internal_defs.h"

#if SANITIZER_CAN_USE_PREINIT_ARRAY

__attribute__((section(".preinit_array"), used)) static auto nsan_preinit =
__nsan_init;

#endif

0 comments on commit 2cec041

Please sign in to comment.