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 14 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" 21 22 #if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32) 23 # error "This operating system is not supported by AddressSanitizer" 24 #endif 25 26 #define ASAN_DEFAULT_FAILURE_EXITCODE 1 27 28 #if defined(__linux__) 29 # define ASAN_LINUX 1 30 #else 31 # define ASAN_LINUX 0 32 #endif 33 34 #if defined(__APPLE__) 35 # define ASAN_MAC 1 36 #else 37 # define ASAN_MAC 0 38 #endif 39 40 #if defined(_WIN32) 41 # define ASAN_WINDOWS 1 42 #else 43 # define ASAN_WINDOWS 0 44 #endif 45 46 #if defined(__ANDROID__) || defined(ANDROID) 47 # define ASAN_ANDROID 1 48 #else 49 # define ASAN_ANDROID 0 50 #endif 51 52 53 #define ASAN_POSIX (ASAN_LINUX || ASAN_MAC) 54 55 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) 56 # error "The AddressSanitizer run-time should not be" 57 " instrumented by AddressSanitizer" 58 #endif 59 60 // Build-time configuration options. 61 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 66 # else 67 # define ASAN_NEEDS_SEGV 1 68 # endif 69 #endif 70 71 // If set, asan will intercept C++ exception api call(s). 72 #ifndef ASAN_HAS_EXCEPTIONS 73 # define ASAN_HAS_EXCEPTIONS 1 74 #endif 75 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 80 #endif 81 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 87 #else 88 # define ASAN_LOW_MEMORY 0 89 # endif 90 #endif 91 92 #ifndef ASAN_USE_PREINIT_ARRAY 93 # define ASAN_USE_PREINIT_ARRAY (ASAN_LINUX && !ASAN_ANDROID) 94 #endif 95 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. 100 namespace __asan { 101 102 class AsanThread; 103 using __sanitizer::StackTrace; 104 105 // asan_rtl.cc 106 void NORETURN ShowStatsAndAbort(); 107 108 void ReplaceOperatorsNewAndDelete(); 109 // asan_malloc_linux.cc / asan_malloc_mac.cc 110 void ReplaceSystemMalloc(); 111 112 // asan_linux.cc / asan_mac.cc / asan_win.cc 113 void *AsanDoesNotSupportStaticLinkage(); 114 115 void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp); 116 117 void MaybeReexec(); 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(); 124 125 // Wrapper for TLS/TSD. 126 void AsanTSDInit(void (*destructor)(void *tsd)); 127 void *AsanTSDGet(); 128 void AsanTSDSet(void *tsd); 129 130 void AppendToErrorMessageBuffer(const char *buffer); 131 132 // asan_poisoning.cc 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 136 // "addr + size". 137 void PoisonShadowPartialRightRedzone(uptr addr, 138 uptr size, 139 uptr redzone_size, 140 u8 value); 141 142 // Platfrom-specific options. 143 #ifdef __APPLE__ 144 bool PlatformHasDifferentMemcpyAndMemmove(); 145 # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \ 146 (PlatformHasDifferentMemcpyAndMemmove()) 147 #else 148 # define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true 149 #endif // __APPLE__ 150 151 // Add convenient macro for interface functions that may be represented as 152 // weak hooks. 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() 159 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); 164 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; 179 180 static const uptr kCurrentStackFrameMagic = 0x41B58AB3; 181 static const uptr kRetiredStackFrameMagic = 0x45E0360E; 182 183 } // namespace __asan 184 185 #endif // ASAN_INTERNAL_H 186