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