Alexey Samsonov | 6eb53d6 | 2014-07-25 22:24:34 +0000 | [diff] [blame] | 1 | //===-- ubsan_init.cc -----------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Initialization of UBSan runtime. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Alexey Samsonov | 0781e98 | 2015-03-26 17:26:04 +0000 | [diff] [blame] | 14 | #include "ubsan_platform.h" |
| 15 | #if CAN_SANITIZE_UB |
Alexey Samsonov | d1c3186 | 2015-02-20 17:41:59 +0000 | [diff] [blame] | 16 | #include "ubsan_diag.h" |
Alexey Samsonov | 6eb53d6 | 2014-07-25 22:24:34 +0000 | [diff] [blame] | 17 | #include "ubsan_init.h" |
| 18 | #include "ubsan_flags.h" |
| 19 | #include "sanitizer_common/sanitizer_common.h" |
| 20 | #include "sanitizer_common/sanitizer_libc.h" |
Alexey Samsonov | 6eb53d6 | 2014-07-25 22:24:34 +0000 | [diff] [blame] | 21 | #include "sanitizer_common/sanitizer_mutex.h" |
Alexey Samsonov | e87e5ce | 2014-07-26 01:41:45 +0000 | [diff] [blame] | 22 | #include "sanitizer_common/sanitizer_symbolizer.h" |
Alexey Samsonov | 6eb53d6 | 2014-07-25 22:24:34 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace __ubsan; |
| 25 | |
Alexey Samsonov | 09dce3b | 2015-04-01 22:42:36 +0000 | [diff] [blame] | 26 | static enum { |
| 27 | UBSAN_MODE_UNKNOWN = 0, |
| 28 | UBSAN_MODE_STANDALONE, |
| 29 | UBSAN_MODE_PLUGIN |
| 30 | } ubsan_mode; |
| 31 | static StaticSpinMutex ubsan_init_mu; |
Alexey Samsonov | 6eb53d6 | 2014-07-25 22:24:34 +0000 | [diff] [blame] | 32 | |
Alexey Samsonov | 09dce3b | 2015-04-01 22:42:36 +0000 | [diff] [blame] | 33 | static void CommonInit() { |
Alexey Samsonov | d1c3186 | 2015-02-20 17:41:59 +0000 | [diff] [blame] | 34 | InitializeSuppressions(); |
Alexey Samsonov | 6eb53d6 | 2014-07-25 22:24:34 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Alexey Samsonov | 09dce3b | 2015-04-01 22:42:36 +0000 | [diff] [blame] | 37 | static void CommonStandaloneInit() { |
| 38 | SanitizerToolName = "UndefinedBehaviorSanitizer"; |
| 39 | InitializeFlags(); |
Yury Gribov | c019a57 | 2015-06-04 07:29:43 +0000 | [diff] [blame] | 40 | CacheBinaryName(); |
Hal Finkel | 0423483 | 2015-04-19 20:16:13 +0000 | [diff] [blame] | 41 | __sanitizer_set_report_path(common_flags()->log_path); |
Alexey Samsonov | 09dce3b | 2015-04-01 22:42:36 +0000 | [diff] [blame] | 42 | InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir); |
| 43 | CommonInit(); |
| 44 | ubsan_mode = UBSAN_MODE_STANDALONE; |
| 45 | } |
| 46 | |
| 47 | void __ubsan::InitAsStandalone() { |
| 48 | if (SANITIZER_CAN_USE_PREINIT_ARRAY) { |
| 49 | CHECK_EQ(UBSAN_MODE_UNKNOWN, ubsan_mode); |
| 50 | CommonStandaloneInit(); |
| 51 | return; |
Alexey Samsonov | 6eb53d6 | 2014-07-25 22:24:34 +0000 | [diff] [blame] | 52 | } |
Alexey Samsonov | 09dce3b | 2015-04-01 22:42:36 +0000 | [diff] [blame] | 53 | SpinMutexLock l(&ubsan_init_mu); |
| 54 | CHECK_NE(UBSAN_MODE_PLUGIN, ubsan_mode); |
| 55 | if (ubsan_mode == UBSAN_MODE_UNKNOWN) |
| 56 | CommonStandaloneInit(); |
| 57 | } |
| 58 | |
| 59 | void __ubsan::InitAsStandaloneIfNecessary() { |
| 60 | if (SANITIZER_CAN_USE_PREINIT_ARRAY) { |
| 61 | CHECK_NE(UBSAN_MODE_UNKNOWN, ubsan_mode); |
| 62 | return; |
| 63 | } |
| 64 | SpinMutexLock l(&ubsan_init_mu); |
| 65 | if (ubsan_mode == UBSAN_MODE_UNKNOWN) |
| 66 | CommonStandaloneInit(); |
| 67 | } |
| 68 | |
| 69 | void __ubsan::InitAsPlugin() { |
| 70 | #if !SANITIZER_CAN_USE_PREINIT_ARRAY |
| 71 | SpinMutexLock l(&ubsan_init_mu); |
| 72 | #endif |
| 73 | CHECK_EQ(UBSAN_MODE_UNKNOWN, ubsan_mode); |
| 74 | CommonInit(); |
| 75 | ubsan_mode = UBSAN_MODE_PLUGIN; |
| 76 | } |
Alexey Samsonov | 0781e98 | 2015-03-26 17:26:04 +0000 | [diff] [blame] | 77 | |
| 78 | #endif // CAN_SANITIZE_UB |