1 //===-- ubsan_init.cc -----------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // Initialization of UBSan runtime.
10 //===----------------------------------------------------------------------===//
12 #include "ubsan_platform.h"
14 #include "ubsan_diag.h"
15 #include "ubsan_init.h"
16 #include "ubsan_flags.h"
17 #include "sanitizer_common/sanitizer_common.h"
18 #include "sanitizer_common/sanitizer_libc.h"
19 #include "sanitizer_common/sanitizer_mutex.h"
20 #include "sanitizer_common/sanitizer_symbolizer.h"
22 using namespace __ubsan
;
25 UBSAN_MODE_UNKNOWN
= 0,
26 UBSAN_MODE_STANDALONE
,
29 static StaticSpinMutex ubsan_init_mu
;
31 static void CommonInit() {
32 InitializeSuppressions();
35 static void CommonStandaloneInit() {
36 SanitizerToolName
= "UndefinedBehaviorSanitizer";
39 __sanitizer_set_report_path(common_flags()->log_path
);
41 InitializeCoverage(common_flags()->coverage
, common_flags()->coverage_dir
);
43 ubsan_mode
= UBSAN_MODE_STANDALONE
;
46 void __ubsan::InitAsStandalone() {
47 if (SANITIZER_CAN_USE_PREINIT_ARRAY
) {
48 CHECK_EQ(UBSAN_MODE_UNKNOWN
, ubsan_mode
);
49 CommonStandaloneInit();
52 SpinMutexLock
l(&ubsan_init_mu
);
53 CHECK_NE(UBSAN_MODE_PLUGIN
, ubsan_mode
);
54 if (ubsan_mode
== UBSAN_MODE_UNKNOWN
)
55 CommonStandaloneInit();
58 void __ubsan::InitAsStandaloneIfNecessary() {
59 if (SANITIZER_CAN_USE_PREINIT_ARRAY
) {
60 CHECK_NE(UBSAN_MODE_UNKNOWN
, ubsan_mode
);
63 SpinMutexLock
l(&ubsan_init_mu
);
64 if (ubsan_mode
== UBSAN_MODE_UNKNOWN
)
65 CommonStandaloneInit();
68 void __ubsan::InitAsPlugin() {
69 #if !SANITIZER_CAN_USE_PREINIT_ARRAY
70 SpinMutexLock
l(&ubsan_init_mu
);
72 CHECK_EQ(UBSAN_MODE_UNKNOWN
, ubsan_mode
);
74 ubsan_mode
= UBSAN_MODE_PLUGIN
;
77 #endif // CAN_SANITIZE_UB