| 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 | d1c3186 | 2015-02-20 17:41:59 +0000 | [diff] [blame] | 14 | #include "ubsan_diag.h" |
| Alexey Samsonov | 6eb53d6 | 2014-07-25 22:24:34 +0000 | [diff] [blame] | 15 | #include "ubsan_init.h" |
| 16 | #include "ubsan_flags.h" |
| 17 | #include "sanitizer_common/sanitizer_common.h" |
| 18 | #include "sanitizer_common/sanitizer_libc.h" |
| Alexey Samsonov | 6eb53d6 | 2014-07-25 22:24:34 +0000 | [diff] [blame] | 19 | #include "sanitizer_common/sanitizer_mutex.h" |
| Alexey Samsonov | e87e5ce | 2014-07-26 01:41:45 +0000 | [diff] [blame] | 20 | #include "sanitizer_common/sanitizer_symbolizer.h" |
| Alexey Samsonov | 6eb53d6 | 2014-07-25 22:24:34 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace __ubsan; |
| 23 | |
| 24 | static bool ubsan_inited; |
| 25 | |
| 26 | void __ubsan::InitIfNecessary() { |
| 27 | #if !SANITIZER_CAN_USE_PREINIT_ARRAY |
| 28 | // No need to lock mutex if we're initializing from preinit array. |
| 29 | static StaticSpinMutex init_mu; |
| 30 | SpinMutexLock l(&init_mu); |
| 31 | #endif |
| 32 | if (LIKELY(ubsan_inited)) |
| 33 | return; |
| Evgeniy Stepanov | f294d5b | 2015-01-15 15:13:43 +0000 | [diff] [blame] | 34 | bool standalone = false; |
| Alexey Samsonov | 6eb53d6 | 2014-07-25 22:24:34 +0000 | [diff] [blame] | 35 | if (0 == internal_strcmp(SanitizerToolName, "SanitizerTool")) { |
| Alexey Samsonov | 74f07ed | 2014-09-12 01:26:57 +0000 | [diff] [blame] | 36 | // WARNING: If this condition holds, then either UBSan runs in a standalone |
| 37 | // mode, or initializer for another sanitizer hasn't run yet. In a latter |
| 38 | // case, another sanitizer will overwrite "SanitizerToolName" and reparse |
| 39 | // common flags. It means, that we are not allowed to *use* common flags |
| 40 | // in this function. |
| Alexey Samsonov | 6eb53d6 | 2014-07-25 22:24:34 +0000 | [diff] [blame] | 41 | SanitizerToolName = "UndefinedBehaviorSanitizer"; |
| Evgeniy Stepanov | f294d5b | 2015-01-15 15:13:43 +0000 | [diff] [blame] | 42 | standalone = true; |
| Alexey Samsonov | 6eb53d6 | 2014-07-25 22:24:34 +0000 | [diff] [blame] | 43 | } |
| Alexey Samsonov | 74f07ed | 2014-09-12 01:26:57 +0000 | [diff] [blame] | 44 | // Initialize UBSan-specific flags. |
| Evgeniy Stepanov | f294d5b | 2015-01-15 15:13:43 +0000 | [diff] [blame] | 45 | InitializeFlags(standalone); |
| Alexey Samsonov | d1c3186 | 2015-02-20 17:41:59 +0000 | [diff] [blame] | 46 | InitializeSuppressions(); |
| Kostya Serebryany | fb42a5a | 2015-01-06 01:31:23 +0000 | [diff] [blame] | 47 | InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir); |
| Alexey Samsonov | 6eb53d6 | 2014-07-25 22:24:34 +0000 | [diff] [blame] | 48 | ubsan_inited = true; |
| 49 | } |
| 50 | |
| 51 | #if SANITIZER_CAN_USE_PREINIT_ARRAY |
| 52 | __attribute__((section(".preinit_array"), used)) |
| 53 | void (*__local_ubsan_preinit)(void) = __ubsan::InitIfNecessary; |
| 54 | #else |
| 55 | // Use a dynamic initializer. |
| 56 | class UbsanInitializer { |
| 57 | public: |
| 58 | UbsanInitializer() { |
| 59 | InitIfNecessary(); |
| 60 | } |
| 61 | }; |
| 62 | static UbsanInitializer ubsan_initializer; |
| 63 | #endif // SANITIZER_CAN_USE_PREINIT_ARRAY |