xref: /llvm-project/compiler-rt/lib/ubsan/ubsan_init.cpp (revision a0bb2e21c10bebcdb6bc6b8bc18f74dcf7c4b8b2)
146ba9697SNico Weber //===-- ubsan_init.cpp ----------------------------------------------------===//
246ba9697SNico Weber //
346ba9697SNico Weber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
446ba9697SNico Weber // See https://llvm.org/LICENSE.txt for license information.
546ba9697SNico Weber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
646ba9697SNico Weber //
746ba9697SNico Weber //===----------------------------------------------------------------------===//
846ba9697SNico Weber //
946ba9697SNico Weber // Initialization of UBSan runtime.
1046ba9697SNico Weber //
1146ba9697SNico Weber //===----------------------------------------------------------------------===//
1246ba9697SNico Weber 
1346ba9697SNico Weber #include "ubsan_platform.h"
1446ba9697SNico Weber #if CAN_SANITIZE_UB
1546ba9697SNico Weber #include "sanitizer_common/sanitizer_common.h"
16595d340dSDmitry Vyukov #include "sanitizer_common/sanitizer_interface_internal.h"
1746ba9697SNico Weber #include "sanitizer_common/sanitizer_libc.h"
1846ba9697SNico Weber #include "sanitizer_common/sanitizer_mutex.h"
1946ba9697SNico Weber #include "sanitizer_common/sanitizer_symbolizer.h"
20595d340dSDmitry Vyukov #include "ubsan_diag.h"
21595d340dSDmitry Vyukov #include "ubsan_flags.h"
22595d340dSDmitry Vyukov #include "ubsan_init.h"
2346ba9697SNico Weber 
2446ba9697SNico Weber using namespace __ubsan;
2546ba9697SNico Weber 
2646ba9697SNico Weber const char *__ubsan::GetSanititizerToolName() {
2746ba9697SNico Weber   return "UndefinedBehaviorSanitizer";
2846ba9697SNico Weber }
2946ba9697SNico Weber 
3046ba9697SNico Weber static bool ubsan_initialized;
3146ba9697SNico Weber static StaticSpinMutex ubsan_init_mu;
3246ba9697SNico Weber 
3346ba9697SNico Weber static void CommonInit() {
3446ba9697SNico Weber   InitializeSuppressions();
3546ba9697SNico Weber }
3646ba9697SNico Weber 
379059903fSEmily Shi static void UbsanDie() {
389059903fSEmily Shi   if (common_flags()->print_module_map >= 1)
399059903fSEmily Shi     DumpProcessMap();
409059903fSEmily Shi }
419059903fSEmily Shi 
4246ba9697SNico Weber static void CommonStandaloneInit() {
4346ba9697SNico Weber   SanitizerToolName = GetSanititizerToolName();
4446ba9697SNico Weber   CacheBinaryName();
4546ba9697SNico Weber   InitializeFlags();
4646ba9697SNico Weber   __sanitizer_set_report_path(common_flags()->log_path);
47*a0bb2e21SVitaly Buka   __sanitizer::InitializePlatformEarly();
4846ba9697SNico Weber   AndroidLogInit();
4946ba9697SNico Weber   InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
5046ba9697SNico Weber   CommonInit();
519059903fSEmily Shi 
529059903fSEmily Shi   // Only add die callback when running in standalone mode to avoid printing
539059903fSEmily Shi   // the same information from multiple sanitizers' output
549059903fSEmily Shi   AddDieCallback(UbsanDie);
55564530e5SDan Liew   Symbolizer::LateInitialize();
5646ba9697SNico Weber }
5746ba9697SNico Weber 
5846ba9697SNico Weber void __ubsan::InitAsStandalone() {
5946ba9697SNico Weber   SpinMutexLock l(&ubsan_init_mu);
6046ba9697SNico Weber   if (!ubsan_initialized) {
6146ba9697SNico Weber     CommonStandaloneInit();
6246ba9697SNico Weber     ubsan_initialized = true;
6346ba9697SNico Weber   }
6446ba9697SNico Weber }
6546ba9697SNico Weber 
6646ba9697SNico Weber void __ubsan::InitAsStandaloneIfNecessary() { return InitAsStandalone(); }
6746ba9697SNico Weber 
6846ba9697SNico Weber void __ubsan::InitAsPlugin() {
6946ba9697SNico Weber   SpinMutexLock l(&ubsan_init_mu);
7046ba9697SNico Weber   if (!ubsan_initialized) {
7146ba9697SNico Weber     CommonInit();
7246ba9697SNico Weber     ubsan_initialized = true;
7346ba9697SNico Weber   }
7446ba9697SNico Weber }
7546ba9697SNico Weber 
7646ba9697SNico Weber #endif  // CAN_SANITIZE_UB
77