]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/ubsan/ubsan_init.cc
Merge compiler-rt r291274.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / ubsan / ubsan_init.cc
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
33 static void CommonInit() {
34   InitializeSuppressions();
35 }
36
37 static void CommonStandaloneInit() {
38   SanitizerToolName = "UndefinedBehaviorSanitizer";
39   InitializeFlags();
40   CacheBinaryName();
41   __sanitizer_set_report_path(common_flags()->log_path);
42   AndroidLogInit();
43   InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
44   CommonInit();
45   ubsan_mode = UBSAN_MODE_STANDALONE;
46 }
47
48 void __ubsan::InitAsStandalone() {
49   if (SANITIZER_CAN_USE_PREINIT_ARRAY) {
50     CHECK_EQ(UBSAN_MODE_UNKNOWN, ubsan_mode);
51     CommonStandaloneInit();
52     return;
53   }
54   SpinMutexLock l(&ubsan_init_mu);
55   CHECK_NE(UBSAN_MODE_PLUGIN, ubsan_mode);
56   if (ubsan_mode == UBSAN_MODE_UNKNOWN)
57     CommonStandaloneInit();
58 }
59
60 void __ubsan::InitAsStandaloneIfNecessary() {
61   if (SANITIZER_CAN_USE_PREINIT_ARRAY) {
62     CHECK_NE(UBSAN_MODE_UNKNOWN, ubsan_mode);
63     return;
64   }
65   SpinMutexLock l(&ubsan_init_mu);
66   if (ubsan_mode == UBSAN_MODE_UNKNOWN)
67     CommonStandaloneInit();
68 }
69
70 void __ubsan::InitAsPlugin() {
71 #if !SANITIZER_CAN_USE_PREINIT_ARRAY
72   SpinMutexLock l(&ubsan_init_mu);
73 #endif
74   CHECK_EQ(UBSAN_MODE_UNKNOWN, ubsan_mode);
75   CommonInit();
76   ubsan_mode = UBSAN_MODE_PLUGIN;
77 }
78
79 #endif  // CAN_SANITIZE_UB