1 //===-- ubsan_init.cc -----------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Initialization of UBSan runtime. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "ubsan_platform.h" 15 #if CAN_SANITIZE_UB 16 #include "ubsan_diag.h" 17 #include "ubsan_init.h" 18 #include "ubsan_flags.h" 19 #include "sanitizer_common/sanitizer_common.h" 20 #include "sanitizer_common/sanitizer_libc.h" 21 #include "sanitizer_common/sanitizer_mutex.h" 22 #include "sanitizer_common/sanitizer_symbolizer.h" 23 24 using namespace __ubsan; 25 26 static enum { 27 UBSAN_MODE_UNKNOWN = 0, 28 UBSAN_MODE_STANDALONE, 29 UBSAN_MODE_PLUGIN 30 } ubsan_mode; 31 static StaticSpinMutex ubsan_init_mu; 32 CommonInit()33static void CommonInit() { 34 InitializeSuppressions(); 35 } 36 CommonStandaloneInit()37static void CommonStandaloneInit() { 38 SanitizerToolName = "UndefinedBehaviorSanitizer"; 39 InitializeFlags(); 40 InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir); 41 CommonInit(); 42 ubsan_mode = UBSAN_MODE_STANDALONE; 43 } 44 InitAsStandalone()45void __ubsan::InitAsStandalone() { 46 if (SANITIZER_CAN_USE_PREINIT_ARRAY) { 47 CHECK_EQ(UBSAN_MODE_UNKNOWN, ubsan_mode); 48 CommonStandaloneInit(); 49 return; 50 } 51 SpinMutexLock l(&ubsan_init_mu); 52 CHECK_NE(UBSAN_MODE_PLUGIN, ubsan_mode); 53 if (ubsan_mode == UBSAN_MODE_UNKNOWN) 54 CommonStandaloneInit(); 55 } 56 InitAsStandaloneIfNecessary()57void __ubsan::InitAsStandaloneIfNecessary() { 58 if (SANITIZER_CAN_USE_PREINIT_ARRAY) { 59 CHECK_NE(UBSAN_MODE_UNKNOWN, ubsan_mode); 60 return; 61 } 62 SpinMutexLock l(&ubsan_init_mu); 63 if (ubsan_mode == UBSAN_MODE_UNKNOWN) 64 CommonStandaloneInit(); 65 } 66 InitAsPlugin()67void __ubsan::InitAsPlugin() { 68 #if !SANITIZER_CAN_USE_PREINIT_ARRAY 69 SpinMutexLock l(&ubsan_init_mu); 70 #endif 71 CHECK_EQ(UBSAN_MODE_UNKNOWN, ubsan_mode); 72 CommonInit(); 73 ubsan_mode = UBSAN_MODE_PLUGIN; 74 } 75 76 #endif // CAN_SANITIZE_UB 77