summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--compiler-rt/lib/ubsan/CMakeLists.txt10
-rw-r--r--compiler-rt/lib/ubsan/ubsan_init.cc36
-rw-r--r--compiler-rt/lib/ubsan/ubsan_init_standalone.cc7
-rw-r--r--compiler-rt/lib/ubsan/ubsan_init_standalone_preinit.cc26
4 files changed, 41 insertions, 38 deletions
diff --git a/compiler-rt/lib/ubsan/CMakeLists.txt b/compiler-rt/lib/ubsan/CMakeLists.txt
index a29c0eed9d3..4a2957c1872 100644
--- a/compiler-rt/lib/ubsan/CMakeLists.txt
+++ b/compiler-rt/lib/ubsan/CMakeLists.txt
@@ -130,15 +130,16 @@ else()
endif()
if(COMPILER_RT_HAS_UBSAN)
- # Initializer of standalone UBSan runtime.
add_compiler_rt_object_libraries(RTUbsan_standalone
ARCHS ${UBSAN_SUPPORTED_ARCH}
- SOURCES ${UBSAN_STANDALONE_SOURCES} CFLAGS ${UBSAN_STANDALONE_CFLAGS})
+ SOURCES ${UBSAN_STANDALONE_SOURCES}
+ CFLAGS ${UBSAN_STANDALONE_CFLAGS})
# Standalone UBSan runtimes.
add_compiler_rt_runtime(clang_rt.ubsan_standalone
STATIC
ARCHS ${UBSAN_SUPPORTED_ARCH}
+ SOURCES ubsan_init_standalone_preinit.cc
OBJECT_LIBS RTSanitizerCommon
RTSanitizerCommonLibc
RTUbsan
@@ -158,8 +159,9 @@ else()
SHARED
ARCHS ${UBSAN_SUPPORTED_ARCH}
OBJECT_LIBS RTSanitizerCommon
- RTSanitizerCommonLibc
- RTUbsan
+ RTSanitizerCommonLibc
+ RTUbsan
+ RTUbsan_standalone
CFLAGS ${UBSAN_CFLAGS}
LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS}
LINK_LIBS ${UBSAN_DYNAMIC_LIBS}
diff --git a/compiler-rt/lib/ubsan/ubsan_init.cc b/compiler-rt/lib/ubsan/ubsan_init.cc
index d7433a5c989..32fc434ad2b 100644
--- a/compiler-rt/lib/ubsan/ubsan_init.cc
+++ b/compiler-rt/lib/ubsan/ubsan_init.cc
@@ -27,11 +27,7 @@ const char *__ubsan::GetSanititizerToolName() {
return "UndefinedBehaviorSanitizer";
}
-static enum {
- UBSAN_MODE_UNKNOWN = 0,
- UBSAN_MODE_STANDALONE,
- UBSAN_MODE_PLUGIN
-} ubsan_mode;
+static bool ubsan_initialized;
static StaticSpinMutex ubsan_init_mu;
static void CommonInit() {
@@ -46,38 +42,24 @@ static void CommonStandaloneInit() {
AndroidLogInit();
InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
CommonInit();
- ubsan_mode = UBSAN_MODE_STANDALONE;
}
void __ubsan::InitAsStandalone() {
- if (SANITIZER_CAN_USE_PREINIT_ARRAY) {
- CHECK_EQ(UBSAN_MODE_UNKNOWN, ubsan_mode);
- CommonStandaloneInit();
- return;
- }
SpinMutexLock l(&ubsan_init_mu);
- CHECK_NE(UBSAN_MODE_PLUGIN, ubsan_mode);
- if (ubsan_mode == UBSAN_MODE_UNKNOWN)
+ if (!ubsan_initialized) {
CommonStandaloneInit();
-}
-
-void __ubsan::InitAsStandaloneIfNecessary() {
- if (SANITIZER_CAN_USE_PREINIT_ARRAY) {
- CHECK_NE(UBSAN_MODE_UNKNOWN, ubsan_mode);
- return;
+ ubsan_initialized = true;
}
- SpinMutexLock l(&ubsan_init_mu);
- if (ubsan_mode == UBSAN_MODE_UNKNOWN)
- CommonStandaloneInit();
}
+void __ubsan::InitAsStandaloneIfNecessary() { return InitAsStandalone(); }
+
void __ubsan::InitAsPlugin() {
-#if !SANITIZER_CAN_USE_PREINIT_ARRAY
SpinMutexLock l(&ubsan_init_mu);
-#endif
- CHECK_EQ(UBSAN_MODE_UNKNOWN, ubsan_mode);
- CommonInit();
- ubsan_mode = UBSAN_MODE_PLUGIN;
+ if (!ubsan_initialized) {
+ CommonInit();
+ ubsan_initialized = true;
+ }
}
#endif // CAN_SANITIZE_UB
diff --git a/compiler-rt/lib/ubsan/ubsan_init_standalone.cc b/compiler-rt/lib/ubsan/ubsan_init_standalone.cc
index ff1a20efea3..8e999e3aca1 100644
--- a/compiler-rt/lib/ubsan/ubsan_init_standalone.cc
+++ b/compiler-rt/lib/ubsan/ubsan_init_standalone.cc
@@ -19,11 +19,6 @@
#include "sanitizer_common/sanitizer_internal_defs.h"
#include "ubsan_init.h"
-#if SANITIZER_CAN_USE_PREINIT_ARRAY
-__attribute__((section(".preinit_array"), used))
-void (*__local_ubsan_preinit)(void) = __ubsan::InitAsStandalone;
-#else
-// Use a dynamic initializer.
class UbsanStandaloneInitializer {
public:
UbsanStandaloneInitializer() {
@@ -31,5 +26,3 @@ class UbsanStandaloneInitializer {
}
};
static UbsanStandaloneInitializer ubsan_standalone_initializer;
-#endif // SANITIZER_CAN_USE_PREINIT_ARRAY
-
diff --git a/compiler-rt/lib/ubsan/ubsan_init_standalone_preinit.cc b/compiler-rt/lib/ubsan/ubsan_init_standalone_preinit.cc
new file mode 100644
index 00000000000..229ecc5c8df
--- /dev/null
+++ b/compiler-rt/lib/ubsan/ubsan_init_standalone_preinit.cc
@@ -0,0 +1,26 @@
+//===-- ubsan_init_standalone_preinit.cc
+//------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Initialization of standalone UBSan runtime.
+//
+//===----------------------------------------------------------------------===//
+
+#include "ubsan_platform.h"
+#if !CAN_SANITIZE_UB
+#error "UBSan is not supported on this platform!"
+#endif
+
+#include "sanitizer_common/sanitizer_internal_defs.h"
+#include "ubsan_init.h"
+
+#if SANITIZER_CAN_USE_PREINIT_ARRAY
+__attribute__((section(".preinit_array"), used)) void (*__local_ubsan_preinit)(
+ void) = __ubsan::InitAsStandalone;
+#endif // SANITIZER_CAN_USE_PREINIT_ARRAY
OpenPOWER on IntegriCloud