ubsan_init.cc revision 288943
133965Sjdp//===-- ubsan_init.cc -----------------------------------------------------===//
233965Sjdp//
333965Sjdp//                     The LLVM Compiler Infrastructure
433965Sjdp//
533965Sjdp// This file is distributed under the University of Illinois Open Source
633965Sjdp// 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
24using namespace __ubsan;
25
26static enum {
27  UBSAN_MODE_UNKNOWN = 0,
28  UBSAN_MODE_STANDALONE,
29  UBSAN_MODE_PLUGIN
30} ubsan_mode;
31static StaticSpinMutex ubsan_init_mu;
32
33static void CommonInit() {
34  InitializeSuppressions();
35}
36
37static void CommonStandaloneInit() {
38  SanitizerToolName = "UndefinedBehaviorSanitizer";
39  InitializeFlags();
40  CacheBinaryName();
41  __sanitizer_set_report_path(common_flags()->log_path);
42  InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
43  CommonInit();
44  ubsan_mode = UBSAN_MODE_STANDALONE;
45}
46
47void __ubsan::InitAsStandalone() {
48  if (SANITIZER_CAN_USE_PREINIT_ARRAY) {
49    CHECK_EQ(UBSAN_MODE_UNKNOWN, ubsan_mode);
50    CommonStandaloneInit();
51    return;
52  }
53  SpinMutexLock l(&ubsan_init_mu);
54  CHECK_NE(UBSAN_MODE_PLUGIN, ubsan_mode);
55  if (ubsan_mode == UBSAN_MODE_UNKNOWN)
56    CommonStandaloneInit();
57}
58
59void __ubsan::InitAsStandaloneIfNecessary() {
60  if (SANITIZER_CAN_USE_PREINIT_ARRAY) {
61    CHECK_NE(UBSAN_MODE_UNKNOWN, ubsan_mode);
62    return;
63  }
64  SpinMutexLock l(&ubsan_init_mu);
65  if (ubsan_mode == UBSAN_MODE_UNKNOWN)
66    CommonStandaloneInit();
67}
68
69void __ubsan::InitAsPlugin() {
70#if !SANITIZER_CAN_USE_PREINIT_ARRAY
71  SpinMutexLock l(&ubsan_init_mu);
72#endif
73  CHECK_EQ(UBSAN_MODE_UNKNOWN, ubsan_mode);
74  CommonInit();
75  ubsan_mode = UBSAN_MODE_PLUGIN;
76}
77
78#endif  // CAN_SANITIZE_UB
79