1 //===-- ubsan_init.cc -----------------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // Initialization of UBSan runtime.
10 //===----------------------------------------------------------------------===//
12 #include "ubsan_init.h"
13 #include "ubsan_flags.h"
14 #include "sanitizer_common/sanitizer_common.h"
15 #include "sanitizer_common/sanitizer_libc.h"
16 #include "sanitizer_common/sanitizer_mutex.h"
17 #include "sanitizer_common/sanitizer_suppressions.h"
18 #include "sanitizer_common/sanitizer_symbolizer.h"
20 using namespace __ubsan
;
22 static bool ubsan_inited
;
24 void __ubsan::InitIfNecessary() {
25 #if !SANITIZER_CAN_USE_PREINIT_ARRAY
26 // No need to lock mutex if we're initializing from preinit array.
27 static StaticSpinMutex init_mu
;
28 SpinMutexLock
l(&init_mu
);
30 if (LIKELY(ubsan_inited
))
32 if (0 == internal_strcmp(SanitizerToolName
, "SanitizerTool")) {
33 // WARNING: If this condition holds, then either UBSan runs in a standalone
34 // mode, or initializer for another sanitizer hasn't run yet. In a latter
35 // case, another sanitizer will overwrite "SanitizerToolName" and reparse
36 // common flags. It means, that we are not allowed to *use* common flags
38 SanitizerToolName
= "UndefinedBehaviorSanitizer";
39 InitializeCommonFlags();
41 // Initialize UBSan-specific flags.
43 SuppressionContext::InitIfNecessary();
47 #if SANITIZER_CAN_USE_PREINIT_ARRAY
48 __attribute__((section(".preinit_array"), used
))
49 void (*__local_ubsan_preinit
)(void) = __ubsan::InitIfNecessary
;
51 // Use a dynamic initializer.
52 class UbsanInitializer
{
58 static UbsanInitializer ubsan_initializer
;
59 #endif // SANITIZER_CAN_USE_PREINIT_ARRAY