1 //===-- ubsan_init.cpp ----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Initialization of UBSan runtime.
11 //===----------------------------------------------------------------------===//
13 #include "ubsan_platform.h"
15 #include "ubsan_diag.h"
16 #include "ubsan_init.h"
17 #include "ubsan_flags.h"
18 #include "sanitizer_common/sanitizer_common.h"
19 #include "sanitizer_common/sanitizer_libc.h"
20 #include "sanitizer_common/sanitizer_mutex.h"
21 #include "sanitizer_common/sanitizer_symbolizer.h"
23 using namespace __ubsan
;
25 const char *__ubsan::GetSanititizerToolName() {
26 return "UndefinedBehaviorSanitizer";
29 static bool ubsan_initialized
;
30 static StaticSpinMutex ubsan_init_mu
;
32 static void CommonInit() {
33 InitializeSuppressions();
36 static void UbsanDie() {
37 if (common_flags()->print_module_map
>= 1)
41 static void CommonStandaloneInit() {
42 SanitizerToolName
= GetSanititizerToolName();
45 __sanitizer::InitializePlatformEarly();
46 __sanitizer_set_report_path(common_flags()->log_path
);
48 InitializeCoverage(common_flags()->coverage
, common_flags()->coverage_dir
);
51 // Only add die callback when running in standalone mode to avoid printing
52 // the same information from multiple sanitizers' output
53 AddDieCallback(UbsanDie
);
54 Symbolizer::LateInitialize();
57 void __ubsan::InitAsStandalone() {
58 SpinMutexLock
l(&ubsan_init_mu
);
59 if (!ubsan_initialized
) {
60 CommonStandaloneInit();
61 ubsan_initialized
= true;
65 void __ubsan::InitAsStandaloneIfNecessary() { return InitAsStandalone(); }
67 void __ubsan::InitAsPlugin() {
68 SpinMutexLock
l(&ubsan_init_mu
);
69 if (!ubsan_initialized
) {
71 ubsan_initialized
= true;
75 #endif // CAN_SANITIZE_UB