[gcc/testsuite]
[official-gcc.git] / libsanitizer / asan / asan_internal.h
blob15a28ff777bba7d0e63b29e5e1019ee4de4de2ab
1 //===-- asan_internal.h -----------------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // ASan-private header which defines various general utilities.
11 //===----------------------------------------------------------------------===//
12 #ifndef ASAN_INTERNAL_H
13 #define ASAN_INTERNAL_H
15 #include "asan_flags.h"
16 #include "asan_interface_internal.h"
17 #include "sanitizer_common/sanitizer_common.h"
18 #include "sanitizer_common/sanitizer_internal_defs.h"
19 #include "sanitizer_common/sanitizer_stacktrace.h"
20 #include "sanitizer_common/sanitizer_libc.h"
22 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
23 # error "The AddressSanitizer run-time should not be"
24 " instrumented by AddressSanitizer"
25 #endif
27 // Build-time configuration options.
29 // If set, asan will intercept C++ exception api call(s).
30 #ifndef ASAN_HAS_EXCEPTIONS
31 # define ASAN_HAS_EXCEPTIONS 1
32 #endif
34 // If set, values like allocator chunk size, as well as defaults for some flags
35 // will be changed towards less memory overhead.
36 #ifndef ASAN_LOW_MEMORY
37 # if SANITIZER_IOS || (SANITIZER_WORDSIZE == 32)
38 # define ASAN_LOW_MEMORY 1
39 # else
40 # define ASAN_LOW_MEMORY 0
41 # endif
42 #endif
44 #ifndef ASAN_DYNAMIC
45 # ifdef PIC
46 # define ASAN_DYNAMIC 1
47 # else
48 # define ASAN_DYNAMIC 0
49 # endif
50 #endif
52 // All internal functions in asan reside inside the __asan namespace
53 // to avoid namespace collisions with the user programs.
54 // Separate namespace also makes it simpler to distinguish the asan run-time
55 // functions from the instrumented user code in a profile.
56 namespace __asan {
58 class AsanThread;
59 using __sanitizer::StackTrace;
61 void AsanInitFromRtl();
63 // asan_win.cc
64 void InitializePlatformExceptionHandlers();
66 // asan_win.cc / asan_posix.cc
67 const char *DescribeSignalOrException(int signo);
69 // asan_rtl.cc
70 void NORETURN ShowStatsAndAbort();
72 // asan_malloc_linux.cc / asan_malloc_mac.cc
73 void ReplaceSystemMalloc();
75 // asan_linux.cc / asan_mac.cc / asan_win.cc
76 void *AsanDoesNotSupportStaticLinkage();
77 void AsanCheckDynamicRTPrereqs();
78 void AsanCheckIncompatibleRT();
80 // Support function for __asan_(un)register_image_globals. Searches for the
81 // loaded image containing `needle' and then enumerates all global metadata
82 // structures declared in that image, applying `op' (e.g.,
83 // __asan_(un)register_globals) to them.
84 typedef void (*globals_op_fptr)(__asan_global *, uptr);
85 void AsanApplyToGlobals(globals_op_fptr op, const void *needle);
87 void AsanOnDeadlySignal(int, void *siginfo, void *context);
89 void ReadContextStack(void *context, uptr *stack, uptr *ssize);
90 void StopInitOrderChecking();
92 // Wrapper for TLS/TSD.
93 void AsanTSDInit(void (*destructor)(void *tsd));
94 void *AsanTSDGet();
95 void AsanTSDSet(void *tsd);
96 void PlatformTSDDtor(void *tsd);
98 void AppendToErrorMessageBuffer(const char *buffer);
100 void *AsanDlSymNext(const char *sym);
102 void ReserveShadowMemoryRange(uptr beg, uptr end, const char *name);
104 // Platform-specific options.
105 #if SANITIZER_MAC
106 bool PlatformHasDifferentMemcpyAndMemmove();
107 # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
108 (PlatformHasDifferentMemcpyAndMemmove())
109 #elif SANITIZER_WINDOWS64
110 # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE false
111 #else
112 # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
113 #endif // SANITIZER_MAC
115 // Add convenient macro for interface functions that may be represented as
116 // weak hooks.
117 #define ASAN_MALLOC_HOOK(ptr, size) \
118 do { \
119 if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(ptr, size); \
120 RunMallocHooks(ptr, size); \
121 } while (false)
122 #define ASAN_FREE_HOOK(ptr) \
123 do { \
124 if (&__sanitizer_free_hook) __sanitizer_free_hook(ptr); \
125 RunFreeHooks(ptr); \
126 } while (false)
127 #define ASAN_ON_ERROR() \
128 if (&__asan_on_error) __asan_on_error()
130 extern int asan_inited;
131 // Used to avoid infinite recursion in __asan_init().
132 extern bool asan_init_is_running;
133 extern void (*death_callback)(void);
134 // These magic values are written to shadow for better error reporting.
135 const int kAsanHeapLeftRedzoneMagic = 0xfa;
136 const int kAsanHeapFreeMagic = 0xfd;
137 const int kAsanStackLeftRedzoneMagic = 0xf1;
138 const int kAsanStackMidRedzoneMagic = 0xf2;
139 const int kAsanStackRightRedzoneMagic = 0xf3;
140 const int kAsanStackAfterReturnMagic = 0xf5;
141 const int kAsanInitializationOrderMagic = 0xf6;
142 const int kAsanUserPoisonedMemoryMagic = 0xf7;
143 const int kAsanContiguousContainerOOBMagic = 0xfc;
144 const int kAsanStackUseAfterScopeMagic = 0xf8;
145 const int kAsanGlobalRedzoneMagic = 0xf9;
146 const int kAsanInternalHeapMagic = 0xfe;
147 const int kAsanArrayCookieMagic = 0xac;
148 const int kAsanIntraObjectRedzone = 0xbb;
149 const int kAsanAllocaLeftMagic = 0xca;
150 const int kAsanAllocaRightMagic = 0xcb;
152 static const uptr kCurrentStackFrameMagic = 0x41B58AB3;
153 static const uptr kRetiredStackFrameMagic = 0x45E0360E;
155 } // namespace __asan
157 #endif // ASAN_INTERNAL_H