llvm-project
76 строк · 2.1 Кб
1//===-- ubsan_init.cpp ----------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Initialization of UBSan runtime.
10//
11//===----------------------------------------------------------------------===//
12
13#include "ubsan_platform.h"
14#if CAN_SANITIZE_UB
15#include "sanitizer_common/sanitizer_common.h"
16#include "sanitizer_common/sanitizer_interface_internal.h"
17#include "sanitizer_common/sanitizer_libc.h"
18#include "sanitizer_common/sanitizer_mutex.h"
19#include "sanitizer_common/sanitizer_symbolizer.h"
20#include "ubsan_diag.h"
21#include "ubsan_flags.h"
22#include "ubsan_init.h"
23
24using namespace __ubsan;
25
26const char *__ubsan::GetSanititizerToolName() {
27return "UndefinedBehaviorSanitizer";
28}
29
30static bool ubsan_initialized;
31static StaticSpinMutex ubsan_init_mu;
32
33static void CommonInit() {
34InitializeSuppressions();
35}
36
37static void UbsanDie() {
38if (common_flags()->print_module_map >= 1)
39DumpProcessMap();
40}
41
42static void CommonStandaloneInit() {
43SanitizerToolName = GetSanititizerToolName();
44CacheBinaryName();
45InitializeFlags();
46__sanitizer::InitializePlatformEarly();
47__sanitizer_set_report_path(common_flags()->log_path);
48AndroidLogInit();
49InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
50CommonInit();
51
52// Only add die callback when running in standalone mode to avoid printing
53// the same information from multiple sanitizers' output
54AddDieCallback(UbsanDie);
55Symbolizer::LateInitialize();
56}
57
58void __ubsan::InitAsStandalone() {
59SpinMutexLock l(&ubsan_init_mu);
60if (!ubsan_initialized) {
61CommonStandaloneInit();
62ubsan_initialized = true;
63}
64}
65
66void __ubsan::InitAsStandaloneIfNecessary() { return InitAsStandalone(); }
67
68void __ubsan::InitAsPlugin() {
69SpinMutexLock l(&ubsan_init_mu);
70if (!ubsan_initialized) {
71CommonInit();
72ubsan_initialized = true;
73}
74}
75
76#endif // CAN_SANITIZE_UB
77