[compiler-rt] r313851 - [ubsan] Split ubsan_init_standalone

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 20 17:35:22 PDT 2017


Author: vitalybuka
Date: Wed Sep 20 17:35:22 2017
New Revision: 313851

URL: http://llvm.org/viewvc/llvm-project?rev=313851&view=rev
Log:
[ubsan] Split ubsan_init_standalone

Summary:
On Linux we may need preinit_array in static lib and
ubsan_standalone_initializer in shared lib.

Reviewers: eugenis

Subscribers: kubamracek, llvm-commits, mgorny

Differential Revision: https://reviews.llvm.org/D38013

Added:
    compiler-rt/trunk/lib/ubsan/ubsan_init_standalone_preinit.cc
Modified:
    compiler-rt/trunk/lib/ubsan/CMakeLists.txt
    compiler-rt/trunk/lib/ubsan/ubsan_init.cc
    compiler-rt/trunk/lib/ubsan/ubsan_init_standalone.cc

Modified: compiler-rt/trunk/lib/ubsan/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/CMakeLists.txt?rev=313851&r1=313850&r2=313851&view=diff
==============================================================================
--- compiler-rt/trunk/lib/ubsan/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/ubsan/CMakeLists.txt Wed Sep 20 17:35:22 2017
@@ -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}

Modified: compiler-rt/trunk/lib/ubsan/ubsan_init.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/ubsan_init.cc?rev=313851&r1=313850&r2=313851&view=diff
==============================================================================
--- compiler-rt/trunk/lib/ubsan/ubsan_init.cc (original)
+++ compiler-rt/trunk/lib/ubsan/ubsan_init.cc Wed Sep 20 17:35:22 2017
@@ -27,11 +27,7 @@ const char *__ubsan::GetSanititizerToolN
   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

Modified: compiler-rt/trunk/lib/ubsan/ubsan_init_standalone.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/ubsan_init_standalone.cc?rev=313851&r1=313850&r2=313851&view=diff
==============================================================================
--- compiler-rt/trunk/lib/ubsan/ubsan_init_standalone.cc (original)
+++ compiler-rt/trunk/lib/ubsan/ubsan_init_standalone.cc Wed Sep 20 17:35:22 2017
@@ -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
-

Added: compiler-rt/trunk/lib/ubsan/ubsan_init_standalone_preinit.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/ubsan/ubsan_init_standalone_preinit.cc?rev=313851&view=auto
==============================================================================
--- compiler-rt/trunk/lib/ubsan/ubsan_init_standalone_preinit.cc (added)
+++ compiler-rt/trunk/lib/ubsan/ubsan_init_standalone_preinit.cc Wed Sep 20 17:35:22 2017
@@ -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




More information about the llvm-commits mailing list