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 | |
| 14 | #include "ubsan_init.h" |
| 15 | #include "ubsan_flags.h" |
| 16 | #include "sanitizer_common/sanitizer_common.h" |
| 17 | #include "sanitizer_common/sanitizer_libc.h" |
| 18 | #include "sanitizer_common/sanitizer_flags.h" |
| 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; |
| 34 | if (0 == internal_strcmp(SanitizerToolName, "SanitizerTool")) { |
| 35 | // WARNING: If this condition holds, then either UBSan runs in a standalone |
| 36 | // mode, or initializer for another sanitizer hasn't run yet. In a latter |
| 37 | // case, another sanitizer will overwrite "SanitizerToolName" and reparse |
| 38 | // common flags. It means, that we are not allowed to *use* common flags |
| 39 | // in this function. |
| 40 | SanitizerToolName = "UndefinedBehaviorSanitizer"; |
| 41 | CommonFlags *cf = common_flags(); |
| 42 | SetCommonFlagsDefaults(cf); |
| 43 | cf->print_summary = false; |
| 44 | // Common flags may only be modified via UBSAN_OPTIONS. |
| 45 | ParseCommonFlagsFromString(cf, GetEnv("UBSAN_OPTIONS")); |
Alexey Samsonov | e87e5ce | 2014-07-26 01:41:45 +0000 | [diff] [blame^] | 46 | Symbolizer::GetOrInit(); |
Alexey Samsonov | 6eb53d6 | 2014-07-25 22:24:34 +0000 | [diff] [blame] | 47 | } |
| 48 | // Initialize UBSan-specific flags. |
| 49 | InitializeFlags(); |
| 50 | ubsan_inited = true; |
| 51 | } |
| 52 | |
| 53 | #if SANITIZER_CAN_USE_PREINIT_ARRAY |
| 54 | __attribute__((section(".preinit_array"), used)) |
| 55 | void (*__local_ubsan_preinit)(void) = __ubsan::InitIfNecessary; |
| 56 | #else |
| 57 | // Use a dynamic initializer. |
| 58 | class UbsanInitializer { |
| 59 | public: |
| 60 | UbsanInitializer() { |
| 61 | InitIfNecessary(); |
| 62 | } |
| 63 | }; |
| 64 | static UbsanInitializer ubsan_initializer; |
| 65 | #endif // SANITIZER_CAN_USE_PREINIT_ARRAY |