xref: /freebsd/contrib/llvm-project/compiler-rt/lib/ubsan/ubsan_init.cpp (revision 68d75eff68281c1b445e3010bb975eae07aac225)
1*68d75effSDimitry Andric //===-- ubsan_init.cpp ----------------------------------------------------===//
2*68d75effSDimitry Andric //
3*68d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*68d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*68d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*68d75effSDimitry Andric //
7*68d75effSDimitry Andric //===----------------------------------------------------------------------===//
8*68d75effSDimitry Andric //
9*68d75effSDimitry Andric // Initialization of UBSan runtime.
10*68d75effSDimitry Andric //
11*68d75effSDimitry Andric //===----------------------------------------------------------------------===//
12*68d75effSDimitry Andric 
13*68d75effSDimitry Andric #include "ubsan_platform.h"
14*68d75effSDimitry Andric #if CAN_SANITIZE_UB
15*68d75effSDimitry Andric #include "ubsan_diag.h"
16*68d75effSDimitry Andric #include "ubsan_init.h"
17*68d75effSDimitry Andric #include "ubsan_flags.h"
18*68d75effSDimitry Andric #include "sanitizer_common/sanitizer_common.h"
19*68d75effSDimitry Andric #include "sanitizer_common/sanitizer_libc.h"
20*68d75effSDimitry Andric #include "sanitizer_common/sanitizer_mutex.h"
21*68d75effSDimitry Andric #include "sanitizer_common/sanitizer_symbolizer.h"
22*68d75effSDimitry Andric 
23*68d75effSDimitry Andric using namespace __ubsan;
24*68d75effSDimitry Andric 
25*68d75effSDimitry Andric const char *__ubsan::GetSanititizerToolName() {
26*68d75effSDimitry Andric   return "UndefinedBehaviorSanitizer";
27*68d75effSDimitry Andric }
28*68d75effSDimitry Andric 
29*68d75effSDimitry Andric static bool ubsan_initialized;
30*68d75effSDimitry Andric static StaticSpinMutex ubsan_init_mu;
31*68d75effSDimitry Andric 
32*68d75effSDimitry Andric static void CommonInit() {
33*68d75effSDimitry Andric   InitializeSuppressions();
34*68d75effSDimitry Andric }
35*68d75effSDimitry Andric 
36*68d75effSDimitry Andric static void CommonStandaloneInit() {
37*68d75effSDimitry Andric   SanitizerToolName = GetSanititizerToolName();
38*68d75effSDimitry Andric   CacheBinaryName();
39*68d75effSDimitry Andric   InitializeFlags();
40*68d75effSDimitry Andric   __sanitizer_set_report_path(common_flags()->log_path);
41*68d75effSDimitry Andric   AndroidLogInit();
42*68d75effSDimitry Andric   InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
43*68d75effSDimitry Andric   CommonInit();
44*68d75effSDimitry Andric }
45*68d75effSDimitry Andric 
46*68d75effSDimitry Andric void __ubsan::InitAsStandalone() {
47*68d75effSDimitry Andric   SpinMutexLock l(&ubsan_init_mu);
48*68d75effSDimitry Andric   if (!ubsan_initialized) {
49*68d75effSDimitry Andric     CommonStandaloneInit();
50*68d75effSDimitry Andric     ubsan_initialized = true;
51*68d75effSDimitry Andric   }
52*68d75effSDimitry Andric }
53*68d75effSDimitry Andric 
54*68d75effSDimitry Andric void __ubsan::InitAsStandaloneIfNecessary() { return InitAsStandalone(); }
55*68d75effSDimitry Andric 
56*68d75effSDimitry Andric void __ubsan::InitAsPlugin() {
57*68d75effSDimitry Andric   SpinMutexLock l(&ubsan_init_mu);
58*68d75effSDimitry Andric   if (!ubsan_initialized) {
59*68d75effSDimitry Andric     CommonInit();
60*68d75effSDimitry Andric     ubsan_initialized = true;
61*68d75effSDimitry Andric   }
62*68d75effSDimitry Andric }
63*68d75effSDimitry Andric 
64*68d75effSDimitry Andric #endif  // CAN_SANITIZE_UB
65