xref: /freebsd/contrib/llvm-project/compiler-rt/lib/ubsan/ubsan_init.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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 "ubsan_diag.h"
1668d75effSDimitry Andric #include "ubsan_init.h"
1768d75effSDimitry Andric #include "ubsan_flags.h"
1868d75effSDimitry Andric #include "sanitizer_common/sanitizer_common.h"
1968d75effSDimitry Andric #include "sanitizer_common/sanitizer_libc.h"
2068d75effSDimitry Andric #include "sanitizer_common/sanitizer_mutex.h"
2168d75effSDimitry Andric #include "sanitizer_common/sanitizer_symbolizer.h"
2268d75effSDimitry Andric 
2368d75effSDimitry Andric using namespace __ubsan;
2468d75effSDimitry Andric 
2568d75effSDimitry Andric const char *__ubsan::GetSanititizerToolName() {
2668d75effSDimitry Andric   return "UndefinedBehaviorSanitizer";
2768d75effSDimitry Andric }
2868d75effSDimitry Andric 
2968d75effSDimitry Andric static bool ubsan_initialized;
3068d75effSDimitry Andric static StaticSpinMutex ubsan_init_mu;
3168d75effSDimitry Andric 
3268d75effSDimitry Andric static void CommonInit() {
3368d75effSDimitry Andric   InitializeSuppressions();
3468d75effSDimitry Andric }
3568d75effSDimitry Andric 
3668d75effSDimitry Andric static void CommonStandaloneInit() {
3768d75effSDimitry Andric   SanitizerToolName = GetSanititizerToolName();
3868d75effSDimitry Andric   CacheBinaryName();
3968d75effSDimitry Andric   InitializeFlags();
40*5ffd83dbSDimitry Andric   __sanitizer::InitializePlatformEarly();
4168d75effSDimitry Andric   __sanitizer_set_report_path(common_flags()->log_path);
4268d75effSDimitry Andric   AndroidLogInit();
4368d75effSDimitry Andric   InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
4468d75effSDimitry Andric   CommonInit();
45*5ffd83dbSDimitry Andric   Symbolizer::LateInitialize();
4668d75effSDimitry Andric }
4768d75effSDimitry Andric 
4868d75effSDimitry Andric void __ubsan::InitAsStandalone() {
4968d75effSDimitry Andric   SpinMutexLock l(&ubsan_init_mu);
5068d75effSDimitry Andric   if (!ubsan_initialized) {
5168d75effSDimitry Andric     CommonStandaloneInit();
5268d75effSDimitry Andric     ubsan_initialized = true;
5368d75effSDimitry Andric   }
5468d75effSDimitry Andric }
5568d75effSDimitry Andric 
5668d75effSDimitry Andric void __ubsan::InitAsStandaloneIfNecessary() { return InitAsStandalone(); }
5768d75effSDimitry Andric 
5868d75effSDimitry Andric void __ubsan::InitAsPlugin() {
5968d75effSDimitry Andric   SpinMutexLock l(&ubsan_init_mu);
6068d75effSDimitry Andric   if (!ubsan_initialized) {
6168d75effSDimitry Andric     CommonInit();
6268d75effSDimitry Andric     ubsan_initialized = true;
6368d75effSDimitry Andric   }
6468d75effSDimitry Andric }
6568d75effSDimitry Andric 
6668d75effSDimitry Andric #endif  // CAN_SANITIZE_UB
67