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 __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) 23 # error "The AddressSanitizer run-time should not be" 24 " instrumented by AddressSanitizer" 25 #endif 26 27 // Build-time configuration options. 28 29 // If set, asan will intercept C++ exception api call(s). 30 #ifndef ASAN_HAS_EXCEPTIONS 31 # define ASAN_HAS_EXCEPTIONS 1 32 #endif 33 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_ANDROID 38 # define ASAN_LOW_MEMORY 1 39 # else 40 # define ASAN_LOW_MEMORY 0 41 # endif 42 #endif 43 44 #ifndef ASAN_DYNAMIC 45 # ifdef PIC 46 # define ASAN_DYNAMIC 1 47 # else 48 # define ASAN_DYNAMIC 0 49 # endif 50 #endif 51 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 { 57 58 class AsanThread; 59 using __sanitizer::StackTrace; 60 61 void AsanInitFromRtl(); 62 63 // asan_win.cc 64 void InitializePlatformExceptionHandlers(); 65 // Returns whether an address is a valid allocated system heap block. 66 // 'addr' must point to the beginning of the block. 67 bool IsSystemHeapAddress(uptr addr); 68 69 // asan_rtl.cc 70 void PrintAddressSpaceLayout(); 71 void NORETURN ShowStatsAndAbort(); 72 73 // asan_shadow_setup.cc 74 void InitializeShadowMemory(); 75 76 // asan_malloc_linux.cc / asan_malloc_mac.cc 77 void ReplaceSystemMalloc(); 78 79 // asan_linux.cc / asan_mac.cc / asan_win.cc 80 uptr FindDynamicShadowStart(); 81 void *AsanDoesNotSupportStaticLinkage(); 82 void AsanCheckDynamicRTPrereqs(); 83 void AsanCheckIncompatibleRT(); 84 85 // asan_thread.cc 86 AsanThread *CreateMainThread(); 87 88 // Support function for __asan_(un)register_image_globals. Searches for the 89 // loaded image containing `needle' and then enumerates all global metadata 90 // structures declared in that image, applying `op' (e.g., 91 // __asan_(un)register_globals) to them. 92 typedef void (*globals_op_fptr)(__asan_global *, uptr); 93 void AsanApplyToGlobals(globals_op_fptr op, const void *needle); 94 95 void AsanOnDeadlySignal(int, void *siginfo, void *context); 96 97 void ReadContextStack(void *context, uptr *stack, uptr *ssize); 98 void StopInitOrderChecking(); 99 100 // Wrapper for TLS/TSD. 101 void AsanTSDInit(void (*destructor)(void *tsd)); 102 void *AsanTSDGet(); 103 void AsanTSDSet(void *tsd); 104 void PlatformTSDDtor(void *tsd); 105 106 void AppendToErrorMessageBuffer(const char *buffer); 107 108 void *AsanDlSymNext(const char *sym); 109 110 void ReserveShadowMemoryRange(uptr beg, uptr end, const char *name); 111 112 // Add convenient macro for interface functions that may be represented as 113 // weak hooks. 114 #define ASAN_MALLOC_HOOK(ptr, size) \ 115 do { \ 116 if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(ptr, size); \ 117 RunMallocHooks(ptr, size); \ 118 } while (false) 119 #define ASAN_FREE_HOOK(ptr) \ 120 do { \ 121 if (&__sanitizer_free_hook) __sanitizer_free_hook(ptr); \ 122 RunFreeHooks(ptr); \ 123 } while (false) 124 #define ASAN_ON_ERROR() \ 125 if (&__asan_on_error) __asan_on_error() 126 127 extern int asan_inited; 128 // Used to avoid infinite recursion in __asan_init(). 129 extern bool asan_init_is_running; 130 extern void (*death_callback)(void); 131 // These magic values are written to shadow for better error reporting. 132 const int kAsanHeapLeftRedzoneMagic = 0xfa; 133 const int kAsanHeapFreeMagic = 0xfd; 134 const int kAsanStackLeftRedzoneMagic = 0xf1; 135 const int kAsanStackMidRedzoneMagic = 0xf2; 136 const int kAsanStackRightRedzoneMagic = 0xf3; 137 const int kAsanStackAfterReturnMagic = 0xf5; 138 const int kAsanInitializationOrderMagic = 0xf6; 139 const int kAsanUserPoisonedMemoryMagic = 0xf7; 140 const int kAsanContiguousContainerOOBMagic = 0xfc; 141 const int kAsanStackUseAfterScopeMagic = 0xf8; 142 const int kAsanGlobalRedzoneMagic = 0xf9; 143 const int kAsanInternalHeapMagic = 0xfe; 144 const int kAsanArrayCookieMagic = 0xac; 145 const int kAsanIntraObjectRedzone = 0xbb; 146 const int kAsanAllocaLeftMagic = 0xca; 147 const int kAsanAllocaRightMagic = 0xcb; 148 149 static const uptr kCurrentStackFrameMagic = 0x41B58AB3; 150 static const uptr kRetiredStackFrameMagic = 0x45E0360E; 151 152 } // namespace __asan 153 154 #endif // ASAN_INTERNAL_H 155