summaryrefslogtreecommitdiff
path: root/libsanitizer/ubsan/ubsan_init.cc
blob: 07f748109e725bb8bf73a66f4747fef525658e1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//===-- ubsan_init.cc -----------------------------------------------------===//
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Initialization of UBSan runtime.
//
//===----------------------------------------------------------------------===//

#include "ubsan_platform.h"
#if CAN_SANITIZE_UB
#include "ubsan_diag.h"
#include "ubsan_init.h"
#include "ubsan_flags.h"
#include "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_libc.h"
#include "sanitizer_common/sanitizer_mutex.h"
#include "sanitizer_common/sanitizer_symbolizer.h"

using namespace __ubsan;

static enum {
  UBSAN_MODE_UNKNOWN = 0,
  UBSAN_MODE_STANDALONE,
  UBSAN_MODE_PLUGIN
} ubsan_mode;
static StaticSpinMutex ubsan_init_mu;

static void CommonInit() {
  InitializeSuppressions();
}

static void CommonStandaloneInit() {
  SanitizerToolName = "UndefinedBehaviorSanitizer";
  InitializeFlags();
  CacheBinaryName();
  __sanitizer_set_report_path(common_flags()->log_path);
  AndroidLogInit();
  InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
  CommonInit();
  ubsan_mode = UBSAN_MODE_STANDALONE;
}

void __ubsan::InitAsStandalone() {
  if (SANITIZER_CAN_USE_PREINIT_ARRAY) {
    CHECK_EQ(UBSAN_MODE_UNKNOWN, ubsan_mode);
    CommonStandaloneInit();
    return;
  }
  SpinMutexLock l(&ubsan_init_mu);
  CHECK_NE(UBSAN_MODE_PLUGIN, ubsan_mode);
  if (ubsan_mode == UBSAN_MODE_UNKNOWN)
    CommonStandaloneInit();
}

void __ubsan::InitAsStandaloneIfNecessary() {
  if (SANITIZER_CAN_USE_PREINIT_ARRAY) {
    CHECK_NE(UBSAN_MODE_UNKNOWN, ubsan_mode);
    return;
  }
  SpinMutexLock l(&ubsan_init_mu);
  if (ubsan_mode == UBSAN_MODE_UNKNOWN)
    CommonStandaloneInit();
}

void __ubsan::InitAsPlugin() {
#if !SANITIZER_CAN_USE_PREINIT_ARRAY
  SpinMutexLock l(&ubsan_init_mu);
#endif
  CHECK_EQ(UBSAN_MODE_UNKNOWN, ubsan_mode);
  CommonInit();
  ubsan_mode = UBSAN_MODE_PLUGIN;
}

#endif  // CAN_SANITIZE_UB