1 //===-- ubsan_init.cc -----------------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // Initialization of UBSan runtime.
9 //
10 //===----------------------------------------------------------------------===//
11 
12 #include "ubsan_platform.h"
13 #if CAN_SANITIZE_UB
14 #include "ubsan_diag.h"
15 #include "ubsan_init.h"
16 #include "ubsan_flags.h"
17 #include "sanitizer_common/sanitizer_common.h"
18 #include "sanitizer_common/sanitizer_libc.h"
19 #include "sanitizer_common/sanitizer_mutex.h"
20 #include "sanitizer_common/sanitizer_symbolizer.h"
21 
22 using namespace __ubsan;
23 
GetSanititizerToolName()24 const char *__ubsan::GetSanititizerToolName() {
25   return "UndefinedBehaviorSanitizer";
26 }
27 
28 static bool ubsan_initialized;
29 static StaticSpinMutex ubsan_init_mu;
30 
CommonInit()31 static void CommonInit() {
32   InitializeSuppressions();
33 }
34 
CommonStandaloneInit()35 static void CommonStandaloneInit() {
36   SanitizerToolName = GetSanititizerToolName();
37   CacheBinaryName();
38   InitializeFlags();
39   __sanitizer_set_report_path(common_flags()->log_path);
40   AndroidLogInit();
41   InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
42   CommonInit();
43 }
44 
InitAsStandalone()45 void __ubsan::InitAsStandalone() {
46   SpinMutexLock l(&ubsan_init_mu);
47   if (!ubsan_initialized) {
48     CommonStandaloneInit();
49     ubsan_initialized = true;
50   }
51 }
52 
InitAsStandaloneIfNecessary()53 void __ubsan::InitAsStandaloneIfNecessary() { return InitAsStandalone(); }
54 
InitAsPlugin()55 void __ubsan::InitAsPlugin() {
56   SpinMutexLock l(&ubsan_init_mu);
57   if (!ubsan_initialized) {
58     CommonInit();
59     ubsan_initialized = true;
60   }
61 }
62 
63 #endif  // CAN_SANITIZE_UB
64