1 //===-- asan_internal.h -----------------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
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 !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
23 # error "This operating system is not supported by AddressSanitizer"
26 #define ASAN_DEFAULT_FAILURE_EXITCODE 1
28 #if defined(__linux__)
34 #if defined(__APPLE__)
41 # define ASAN_WINDOWS 1
43 # define ASAN_WINDOWS 0
46 #if defined(__ANDROID__) || defined(ANDROID)
47 # define ASAN_ANDROID 1
49 # define ASAN_ANDROID 0
53 #define ASAN_POSIX (ASAN_LINUX || ASAN_MAC)
55 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
56 # error "The AddressSanitizer run-time should not be"
57 " instrumented by AddressSanitizer"
60 // Build-time configuration options.
62 // If set, asan will install its own SEGV signal handler.
63 #ifndef ASAN_NEEDS_SEGV
64 # if ASAN_ANDROID == 1
65 # define ASAN_NEEDS_SEGV 0
67 # define ASAN_NEEDS_SEGV 1
71 // If set, asan will intercept C++ exception api call(s).
72 #ifndef ASAN_HAS_EXCEPTIONS
73 # define ASAN_HAS_EXCEPTIONS 1
76 // If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET
77 // provided by the instrumented objects. Otherwise constants are used.
78 #ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET
79 # define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0
82 // If set, values like allocator chunk size, as well as defaults for some flags
83 // will be changed towards less memory overhead.
84 #ifndef ASAN_LOW_MEMORY
85 #if SANITIZER_WORDSIZE == 32
86 # define ASAN_LOW_MEMORY 1
88 # define ASAN_LOW_MEMORY 0
92 #ifndef ASAN_USE_PREINIT_ARRAY
93 # define ASAN_USE_PREINIT_ARRAY (ASAN_LINUX && !ASAN_ANDROID)
96 // All internal functions in asan reside inside the __asan namespace
97 // to avoid namespace collisions with the user programs.
98 // Seperate namespace also makes it simpler to distinguish the asan run-time
99 // functions from the instrumented user code in a profile.
103 using __sanitizer::StackTrace
;
106 void NORETURN
ShowStatsAndAbort();
108 void ReplaceOperatorsNewAndDelete();
109 // asan_malloc_linux.cc / asan_malloc_mac.cc
110 void ReplaceSystemMalloc();
112 // asan_linux.cc / asan_mac.cc / asan_win.cc
113 void *AsanDoesNotSupportStaticLinkage();
115 void GetPcSpBp(void *context
, uptr
*pc
, uptr
*sp
, uptr
*bp
);
118 bool AsanInterceptsSignal(int signum
);
119 void SetAlternateSignalStack();
120 void UnsetAlternateSignalStack();
121 void InstallSignalHandlers();
122 void ReadContextStack(void *context
, uptr
*stack
, uptr
*ssize
);
123 void AsanPlatformThreadInit();
125 // Wrapper for TLS/TSD.
126 void AsanTSDInit(void (*destructor
)(void *tsd
));
128 void AsanTSDSet(void *tsd
);
130 void AppendToErrorMessageBuffer(const char *buffer
);
133 // Poisons the shadow memory for "size" bytes starting from "addr".
134 void PoisonShadow(uptr addr
, uptr size
, u8 value
);
135 // Poisons the shadow memory for "redzone_size" bytes starting from
137 void PoisonShadowPartialRightRedzone(uptr addr
,
142 // Platfrom-specific options.
144 bool PlatformHasDifferentMemcpyAndMemmove();
145 # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \
146 (PlatformHasDifferentMemcpyAndMemmove())
148 # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true
151 // Add convenient macro for interface functions that may be represented as
153 #define ASAN_MALLOC_HOOK(ptr, size) \
154 if (&__asan_malloc_hook) __asan_malloc_hook(ptr, size)
155 #define ASAN_FREE_HOOK(ptr) \
156 if (&__asan_free_hook) __asan_free_hook(ptr)
157 #define ASAN_ON_ERROR() \
158 if (&__asan_on_error) __asan_on_error()
160 extern int asan_inited
;
161 // Used to avoid infinite recursion in __asan_init().
162 extern bool asan_init_is_running
;
163 extern void (*death_callback
)(void);
165 // These magic values are written to shadow for better error reporting.
166 const int kAsanHeapLeftRedzoneMagic
= 0xfa;
167 const int kAsanHeapRightRedzoneMagic
= 0xfb;
168 const int kAsanHeapFreeMagic
= 0xfd;
169 const int kAsanStackLeftRedzoneMagic
= 0xf1;
170 const int kAsanStackMidRedzoneMagic
= 0xf2;
171 const int kAsanStackRightRedzoneMagic
= 0xf3;
172 const int kAsanStackPartialRedzoneMagic
= 0xf4;
173 const int kAsanStackAfterReturnMagic
= 0xf5;
174 const int kAsanInitializationOrderMagic
= 0xf6;
175 const int kAsanUserPoisonedMemoryMagic
= 0xf7;
176 const int kAsanStackUseAfterScopeMagic
= 0xf8;
177 const int kAsanGlobalRedzoneMagic
= 0xf9;
178 const int kAsanInternalHeapMagic
= 0xfe;
180 static const uptr kCurrentStackFrameMagic
= 0x41B58AB3;
181 static const uptr kRetiredStackFrameMagic
= 0x45E0360E;
183 } // namespace __asan
185 #endif // ASAN_INTERNAL_H