2015-03-05 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libsanitizer / ubsan / ubsan_init.cc
blobf28fc811f50e952568b00592443c12a5168d7317
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 //===----------------------------------------------------------------------===//
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);
29 #endif
30 if (LIKELY(ubsan_inited))
31 return;
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
37 // in this function.
38 SanitizerToolName = "UndefinedBehaviorSanitizer";
39 InitializeCommonFlags();
41 // Initialize UBSan-specific flags.
42 InitializeFlags();
43 SuppressionContext::InitIfNecessary();
44 ubsan_inited = true;
47 #if SANITIZER_CAN_USE_PREINIT_ARRAY
48 __attribute__((section(".preinit_array"), used))
49 void (*__local_ubsan_preinit)(void) = __ubsan::InitIfNecessary;
50 #else
51 // Use a dynamic initializer.
52 class UbsanInitializer {
53 public:
54 UbsanInitializer() {
55 InitIfNecessary();
58 static UbsanInitializer ubsan_initializer;
59 #endif // SANITIZER_CAN_USE_PREINIT_ARRAY