xref: /freebsd/contrib/llvm-project/compiler-rt/lib/ubsan/ubsan_init.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
168d75effSDimitry Andric //===-- ubsan_init.cpp ----------------------------------------------------===//
268d75effSDimitry Andric //
368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
668d75effSDimitry Andric //
768d75effSDimitry Andric //===----------------------------------------------------------------------===//
868d75effSDimitry Andric //
968d75effSDimitry Andric // Initialization of UBSan runtime.
1068d75effSDimitry Andric //
1168d75effSDimitry Andric //===----------------------------------------------------------------------===//
1268d75effSDimitry Andric 
1368d75effSDimitry Andric #include "ubsan_platform.h"
1468d75effSDimitry Andric #if CAN_SANITIZE_UB
1568d75effSDimitry Andric #include "sanitizer_common/sanitizer_common.h"
16*81ad6265SDimitry Andric #include "sanitizer_common/sanitizer_interface_internal.h"
1768d75effSDimitry Andric #include "sanitizer_common/sanitizer_libc.h"
1868d75effSDimitry Andric #include "sanitizer_common/sanitizer_mutex.h"
1968d75effSDimitry Andric #include "sanitizer_common/sanitizer_symbolizer.h"
20*81ad6265SDimitry Andric #include "ubsan_diag.h"
21*81ad6265SDimitry Andric #include "ubsan_flags.h"
22*81ad6265SDimitry Andric #include "ubsan_init.h"
2368d75effSDimitry Andric 
2468d75effSDimitry Andric using namespace __ubsan;
2568d75effSDimitry Andric 
GetSanititizerToolName()2668d75effSDimitry Andric const char *__ubsan::GetSanititizerToolName() {
2768d75effSDimitry Andric   return "UndefinedBehaviorSanitizer";
2868d75effSDimitry Andric }
2968d75effSDimitry Andric 
3068d75effSDimitry Andric static bool ubsan_initialized;
3168d75effSDimitry Andric static StaticSpinMutex ubsan_init_mu;
3268d75effSDimitry Andric 
CommonInit()3368d75effSDimitry Andric static void CommonInit() {
3468d75effSDimitry Andric   InitializeSuppressions();
3568d75effSDimitry Andric }
3668d75effSDimitry Andric 
UbsanDie()37fe6060f1SDimitry Andric static void UbsanDie() {
38fe6060f1SDimitry Andric   if (common_flags()->print_module_map >= 1)
39fe6060f1SDimitry Andric     DumpProcessMap();
40fe6060f1SDimitry Andric }
41fe6060f1SDimitry Andric 
CommonStandaloneInit()4268d75effSDimitry Andric static void CommonStandaloneInit() {
4368d75effSDimitry Andric   SanitizerToolName = GetSanititizerToolName();
4468d75effSDimitry Andric   CacheBinaryName();
4568d75effSDimitry Andric   InitializeFlags();
465ffd83dbSDimitry Andric   __sanitizer::InitializePlatformEarly();
4768d75effSDimitry Andric   __sanitizer_set_report_path(common_flags()->log_path);
4868d75effSDimitry Andric   AndroidLogInit();
4968d75effSDimitry Andric   InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
5068d75effSDimitry Andric   CommonInit();
51fe6060f1SDimitry Andric 
52fe6060f1SDimitry Andric   // Only add die callback when running in standalone mode to avoid printing
53fe6060f1SDimitry Andric   // the same information from multiple sanitizers' output
54fe6060f1SDimitry Andric   AddDieCallback(UbsanDie);
555ffd83dbSDimitry Andric   Symbolizer::LateInitialize();
5668d75effSDimitry Andric }
5768d75effSDimitry Andric 
InitAsStandalone()5868d75effSDimitry Andric void __ubsan::InitAsStandalone() {
5968d75effSDimitry Andric   SpinMutexLock l(&ubsan_init_mu);
6068d75effSDimitry Andric   if (!ubsan_initialized) {
6168d75effSDimitry Andric     CommonStandaloneInit();
6268d75effSDimitry Andric     ubsan_initialized = true;
6368d75effSDimitry Andric   }
6468d75effSDimitry Andric }
6568d75effSDimitry Andric 
InitAsStandaloneIfNecessary()6668d75effSDimitry Andric void __ubsan::InitAsStandaloneIfNecessary() { return InitAsStandalone(); }
6768d75effSDimitry Andric 
InitAsPlugin()6868d75effSDimitry Andric void __ubsan::InitAsPlugin() {
6968d75effSDimitry Andric   SpinMutexLock l(&ubsan_init_mu);
7068d75effSDimitry Andric   if (!ubsan_initialized) {
7168d75effSDimitry Andric     CommonInit();
7268d75effSDimitry Andric     ubsan_initialized = true;
7368d75effSDimitry Andric   }
7468d75effSDimitry Andric }
7568d75effSDimitry Andric 
7668d75effSDimitry Andric #endif  // CAN_SANITIZE_UB
77