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 CacheBinaryName(); 41 __sanitizer_set_report_path(common_flags()->log_path); 42 InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir); 43 CommonInit(); 44 ubsan_mode = UBSAN_MODE_STANDALONE; 45 } 46 InitAsStandalone()47void __ubsan::InitAsStandalone() { 48 if (SANITIZER_CAN_USE_PREINIT_ARRAY) { 49 CHECK_EQ(UBSAN_MODE_UNKNOWN, ubsan_mode); 50 CommonStandaloneInit(); 51 return; 52 } 53 SpinMutexLock l(&ubsan_init_mu); 54 CHECK_NE(UBSAN_MODE_PLUGIN, ubsan_mode); 55 if (ubsan_mode == UBSAN_MODE_UNKNOWN) 56 CommonStandaloneInit(); 57 } 58 InitAsStandaloneIfNecessary()59void __ubsan::InitAsStandaloneIfNecessary() { 60 if (SANITIZER_CAN_USE_PREINIT_ARRAY) { 61 CHECK_NE(UBSAN_MODE_UNKNOWN, ubsan_mode); 62 return; 63 } 64 SpinMutexLock l(&ubsan_init_mu); 65 if (ubsan_mode == UBSAN_MODE_UNKNOWN) 66 CommonStandaloneInit(); 67 } 68 InitAsPlugin()69void __ubsan::InitAsPlugin() { 70 #if !SANITIZER_CAN_USE_PREINIT_ARRAY 71 SpinMutexLock l(&ubsan_init_mu); 72 #endif 73 CHECK_EQ(UBSAN_MODE_UNKNOWN, ubsan_mode); 74 CommonInit(); 75 ubsan_mode = UBSAN_MODE_PLUGIN; 76 } 77 78 #endif // CAN_SANITIZE_UB 79