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;
27 UBSAN_MODE_UNKNOWN = 0,
28 UBSAN_MODE_STANDALONE,
31 static StaticSpinMutex ubsan_init_mu;
33 static void CommonInit() {
34 InitializeSuppressions();
37 static void CommonStandaloneInit() {
38 SanitizerToolName = "UndefinedBehaviorSanitizer";
41 __sanitizer_set_report_path(common_flags()->log_path);
43 InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
45 ubsan_mode = UBSAN_MODE_STANDALONE;
48 void __ubsan::InitAsStandalone() {
49 if (SANITIZER_CAN_USE_PREINIT_ARRAY) {
50 CHECK_EQ(UBSAN_MODE_UNKNOWN, ubsan_mode);
51 CommonStandaloneInit();
54 SpinMutexLock l(&ubsan_init_mu);
55 CHECK_NE(UBSAN_MODE_PLUGIN, ubsan_mode);
56 if (ubsan_mode == UBSAN_MODE_UNKNOWN)
57 CommonStandaloneInit();
60 void __ubsan::InitAsStandaloneIfNecessary() {
61 if (SANITIZER_CAN_USE_PREINIT_ARRAY) {
62 CHECK_NE(UBSAN_MODE_UNKNOWN, ubsan_mode);
65 SpinMutexLock l(&ubsan_init_mu);
66 if (ubsan_mode == UBSAN_MODE_UNKNOWN)
67 CommonStandaloneInit();
70 void __ubsan::InitAsPlugin() {
71 #if !SANITIZER_CAN_USE_PREINIT_ARRAY
72 SpinMutexLock l(&ubsan_init_mu);
74 CHECK_EQ(UBSAN_MODE_UNKNOWN, ubsan_mode);
76 ubsan_mode = UBSAN_MODE_PLUGIN;
79 #endif // CAN_SANITIZE_UB