1 //===-- ubsan_init.cc -----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Initialization of UBSan runtime.
12 //===----------------------------------------------------------------------===//
14 #include "ubsan_platform.h"
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"
24 using namespace __ubsan;
26 const char *__ubsan::GetSanititizerToolName() {
27 return "UndefinedBehaviorSanitizer";
31 UBSAN_MODE_UNKNOWN = 0,
32 UBSAN_MODE_STANDALONE,
35 static StaticSpinMutex ubsan_init_mu;
37 static void CommonInit() {
38 InitializeSuppressions();
41 static void CommonStandaloneInit() {
42 SanitizerToolName = GetSanititizerToolName();
45 __sanitizer_set_report_path(common_flags()->log_path);
47 InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
49 ubsan_mode = UBSAN_MODE_STANDALONE;
52 void __ubsan::InitAsStandalone() {
53 if (SANITIZER_CAN_USE_PREINIT_ARRAY) {
54 CHECK_EQ(UBSAN_MODE_UNKNOWN, ubsan_mode);
55 CommonStandaloneInit();
58 SpinMutexLock l(&ubsan_init_mu);
59 CHECK_NE(UBSAN_MODE_PLUGIN, ubsan_mode);
60 if (ubsan_mode == UBSAN_MODE_UNKNOWN)
61 CommonStandaloneInit();
64 void __ubsan::InitAsStandaloneIfNecessary() {
65 if (SANITIZER_CAN_USE_PREINIT_ARRAY) {
66 CHECK_NE(UBSAN_MODE_UNKNOWN, ubsan_mode);
69 SpinMutexLock l(&ubsan_init_mu);
70 if (ubsan_mode == UBSAN_MODE_UNKNOWN)
71 CommonStandaloneInit();
74 void __ubsan::InitAsPlugin() {
75 #if !SANITIZER_CAN_USE_PREINIT_ARRAY
76 SpinMutexLock l(&ubsan_init_mu);
78 CHECK_EQ(UBSAN_MODE_UNKNOWN, ubsan_mode);
80 ubsan_mode = UBSAN_MODE_PLUGIN;
83 #endif // CAN_SANITIZE_UB