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 "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"
24 using namespace __ubsan
;
26 const char *__ubsan::GetSanititizerToolName() {
27 return "UndefinedBehaviorSanitizer";
30 static bool ubsan_initialized
;
31 static StaticSpinMutex ubsan_init_mu
;
33 static void CommonInit() {
34 InitializeSuppressions();
37 static void UbsanDie() {
38 if (common_flags()->print_module_map
>= 1)
42 static void CommonStandaloneInit() {
43 SanitizerToolName
= GetSanititizerToolName();
46 __sanitizer::InitializePlatformEarly();
47 __sanitizer_set_report_path(common_flags()->log_path
);
49 InitializeCoverage(common_flags()->coverage
, common_flags()->coverage_dir
);
52 // Only add die callback when running in standalone mode to avoid printing
53 // the same information from multiple sanitizers' output
54 AddDieCallback(UbsanDie
);
55 Symbolizer::LateInitialize();
58 void __ubsan::InitAsStandalone() {
59 SpinMutexLock
l(&ubsan_init_mu
);
60 if (!ubsan_initialized
) {
61 CommonStandaloneInit();
62 ubsan_initialized
= true;
66 void __ubsan::InitAsStandaloneIfNecessary() { return InitAsStandalone(); }
68 void __ubsan::InitAsPlugin() {
69 SpinMutexLock
l(&ubsan_init_mu
);
70 if (!ubsan_initialized
) {
72 ubsan_initialized
= true;
76 #endif // CAN_SANITIZE_UB