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