[gcc]
[official-gcc.git] / libsanitizer / ubsan / ubsan_init.cc
blob5da4122c07abdab6aac54c1938e11c6af5d1fde9
1 //===-- ubsan_init.cc -----------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // Initialization of UBSan runtime.
9 //
10 //===----------------------------------------------------------------------===//
12 #include "ubsan_platform.h"
13 #if CAN_SANITIZE_UB
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;
24 static enum {
25 UBSAN_MODE_UNKNOWN = 0,
26 UBSAN_MODE_STANDALONE,
27 UBSAN_MODE_PLUGIN
28 } ubsan_mode;
29 static StaticSpinMutex ubsan_init_mu;
31 static void CommonInit() {
32 InitializeSuppressions();
35 static void CommonStandaloneInit() {
36 SanitizerToolName = "UndefinedBehaviorSanitizer";
37 InitializeFlags();
38 CacheBinaryName();
39 __sanitizer_set_report_path(common_flags()->log_path);
40 InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
41 CommonInit();
42 ubsan_mode = UBSAN_MODE_STANDALONE;
45 void __ubsan::InitAsStandalone() {
46 if (SANITIZER_CAN_USE_PREINIT_ARRAY) {
47 CHECK_EQ(UBSAN_MODE_UNKNOWN, ubsan_mode);
48 CommonStandaloneInit();
49 return;
51 SpinMutexLock l(&ubsan_init_mu);
52 CHECK_NE(UBSAN_MODE_PLUGIN, ubsan_mode);
53 if (ubsan_mode == UBSAN_MODE_UNKNOWN)
54 CommonStandaloneInit();
57 void __ubsan::InitAsStandaloneIfNecessary() {
58 if (SANITIZER_CAN_USE_PREINIT_ARRAY) {
59 CHECK_NE(UBSAN_MODE_UNKNOWN, ubsan_mode);
60 return;
62 SpinMutexLock l(&ubsan_init_mu);
63 if (ubsan_mode == UBSAN_MODE_UNKNOWN)
64 CommonStandaloneInit();
67 void __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;
76 #endif // CAN_SANITIZE_UB