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
24using namespace __ubsan;
25
26const char *__ubsan::GetSanititizerToolName() {
27  return "UndefinedBehaviorSanitizer";
28}
29
30static bool ubsan_initialized;
31static StaticSpinMutex ubsan_init_mu;
32
33static void CommonInit() {
34  InitializeSuppressions();
35}
36
37static void CommonStandaloneInit() {
38  SanitizerToolName = GetSanititizerToolName();
39  CacheBinaryName();
40  InitializeFlags();
41  __sanitizer_set_report_path(common_flags()->log_path);
42  AndroidLogInit();
43  InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
44  CommonInit();
45}
46
47void __ubsan::InitAsStandalone() {
48  SpinMutexLock l(&ubsan_init_mu);
49  if (!ubsan_initialized) {
50    CommonStandaloneInit();
51    ubsan_initialized = true;
52  }
53}
54
55void __ubsan::InitAsStandaloneIfNecessary() { return InitAsStandalone(); }
56
57void __ubsan::InitAsPlugin() {
58  SpinMutexLock l(&ubsan_init_mu);
59  if (!ubsan_initialized) {
60    CommonInit();
61    ubsan_initialized = true;
62  }
63}
64
65#endif  // CAN_SANITIZE_UB
66