Index: compiler-rt/lib/ubsan/CMakeLists.txt =================================================================== --- compiler-rt/lib/ubsan/CMakeLists.txt +++ compiler-rt/lib/ubsan/CMakeLists.txt @@ -130,15 +130,16 @@ 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 @@ 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} Index: compiler-rt/lib/ubsan/ubsan_init.cc =================================================================== --- compiler-rt/lib/ubsan/ubsan_init.cc +++ compiler-rt/lib/ubsan/ubsan_init.cc @@ -27,11 +27,7 @@ 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 @@ 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 Index: compiler-rt/lib/ubsan/ubsan_init_standalone.cc =================================================================== --- compiler-rt/lib/ubsan/ubsan_init_standalone.cc +++ 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 @@ } }; static UbsanStandaloneInitializer ubsan_standalone_initializer; -#endif // SANITIZER_CAN_USE_PREINIT_ARRAY - Index: compiler-rt/lib/ubsan/ubsan_init_standalone_preinit.cc =================================================================== --- compiler-rt/lib/ubsan/ubsan_init_standalone_preinit.cc +++ compiler-rt/lib/ubsan/ubsan_init_standalone_preinit.cc @@ -1,4 +1,5 @@ -//===-- ubsan_init_standalone.cc ------------------------------------------===// +//===-- ubsan_init_standalone_preinit.cc +//------------------------------------------===// // // The LLVM Compiler Infrastructure // @@ -13,23 +14,13 @@ #include "ubsan_platform.h" #if !CAN_SANITIZE_UB -# error "UBSan is not supported on this platform!" +#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; -#else -// Use a dynamic initializer. -class UbsanStandaloneInitializer { - public: - UbsanStandaloneInitializer() { - __ubsan::InitAsStandalone(); - } -}; -static UbsanStandaloneInitializer ubsan_standalone_initializer; -#endif // SANITIZER_CAN_USE_PREINIT_ARRAY - +__attribute__((section(".preinit_array"), used)) void (*__local_ubsan_preinit)( + void) = __ubsan::InitAsStandalone; +#endif // SANITIZER_CAN_USE_PREINIT_ARRAY