asan_internal.h revision e31eca900a1f8849af75100c2d92e838d79d0920
1//===-- asan_internal.h -----------------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file is a part of AddressSanitizer, an address sanity checker. 11// 12// ASan-private header which defines various general utilities. 13//===----------------------------------------------------------------------===// 14#ifndef ASAN_INTERNAL_H 15#define ASAN_INTERNAL_H 16 17#include "asan_flags.h" 18#include "asan_interface_internal.h" 19#include "sanitizer_common/sanitizer_common.h" 20#include "sanitizer_common/sanitizer_internal_defs.h" 21#include "sanitizer_common/sanitizer_stacktrace.h" 22#include "sanitizer_common/sanitizer_libc.h" 23 24#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32) 25# error "This operating system is not supported by AddressSanitizer" 26#endif 27 28#define ASAN_DEFAULT_FAILURE_EXITCODE 1 29 30#if defined(__linux__) 31# define ASAN_LINUX 1 32#else 33# define ASAN_LINUX 0 34#endif 35 36#if defined(__APPLE__) 37# define ASAN_MAC 1 38#else 39# define ASAN_MAC 0 40#endif 41 42#if defined(_WIN32) 43# define ASAN_WINDOWS 1 44#else 45# define ASAN_WINDOWS 0 46#endif 47 48#if defined(__ANDROID__) || defined(ANDROID) 49# define ASAN_ANDROID 1 50#else 51# define ASAN_ANDROID 0 52#endif 53 54 55#define ASAN_POSIX (ASAN_LINUX || ASAN_MAC) 56 57#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__) 58# error "The AddressSanitizer run-time should not be" 59 " instrumented by AddressSanitizer" 60#endif 61 62// Build-time configuration options. 63 64// If set, asan will install its own SEGV signal handler. 65#ifndef ASAN_NEEDS_SEGV 66# if ASAN_ANDROID == 1 67# define ASAN_NEEDS_SEGV 0 68# else 69# define ASAN_NEEDS_SEGV 1 70# endif 71#endif 72 73// If set, asan will intercept C++ exception api call(s). 74#ifndef ASAN_HAS_EXCEPTIONS 75# define ASAN_HAS_EXCEPTIONS 1 76#endif 77 78// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET 79// provided by the instrumented objects. Otherwise constants are used. 80#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET 81# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0 82#endif 83 84// If set, values like allocator chunk size, as well as defaults for some flags 85// will be changed towards less memory overhead. 86#ifndef ASAN_LOW_MEMORY 87#if SANITIZER_WORDSIZE == 32 88# define ASAN_LOW_MEMORY 1 89#else 90# define ASAN_LOW_MEMORY 0 91# endif 92#endif 93 94// All internal functions in asan reside inside the __asan namespace 95// to avoid namespace collisions with the user programs. 96// Seperate namespace also makes it simpler to distinguish the asan run-time 97// functions from the instrumented user code in a profile. 98namespace __asan { 99 100class AsanThread; 101using __sanitizer::StackTrace; 102 103// asan_rtl.cc 104void NORETURN ShowStatsAndAbort(); 105 106void ReplaceOperatorsNewAndDelete(); 107// asan_malloc_linux.cc / asan_malloc_mac.cc 108void ReplaceSystemMalloc(); 109 110// asan_linux.cc / asan_mac.cc / asan_win.cc 111void *AsanDoesNotSupportStaticLinkage(); 112 113void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp); 114 115void MaybeReexec(); 116bool AsanInterceptsSignal(int signum); 117void SetAlternateSignalStack(); 118void UnsetAlternateSignalStack(); 119void InstallSignalHandlers(); 120void ReadContextStack(void *context, uptr *stack, uptr *ssize); 121void AsanPlatformThreadInit(); 122 123// Wrapper for TLS/TSD. 124void AsanTSDInit(void (*destructor)(void *tsd)); 125void *AsanTSDGet(); 126void AsanTSDSet(void *tsd); 127 128void AppendToErrorMessageBuffer(const char *buffer); 129 130// asan_poisoning.cc 131// Poisons the shadow memory for "size" bytes starting from "addr". 132void PoisonShadow(uptr addr, uptr size, u8 value); 133// Poisons the shadow memory for "redzone_size" bytes starting from 134// "addr + size". 135void PoisonShadowPartialRightRedzone(uptr addr, 136 uptr size, 137 uptr redzone_size, 138 u8 value); 139 140// Platfrom-specific options. 141#ifdef __APPLE__ 142bool PlatformHasDifferentMemcpyAndMemmove(); 143# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \ 144 (PlatformHasDifferentMemcpyAndMemmove()) 145#else 146# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true 147#endif // __APPLE__ 148 149// Add convenient macro for interface functions that may be represented as 150// weak hooks. 151#define ASAN_MALLOC_HOOK(ptr, size) \ 152 if (&__asan_malloc_hook) __asan_malloc_hook(ptr, size) 153#define ASAN_FREE_HOOK(ptr) \ 154 if (&__asan_free_hook) __asan_free_hook(ptr) 155#define ASAN_ON_ERROR() \ 156 if (&__asan_on_error) __asan_on_error() 157 158extern int asan_inited; 159// Used to avoid infinite recursion in __asan_init(). 160extern bool asan_init_is_running; 161extern void (*death_callback)(void); 162 163// These magic values are written to shadow for better error reporting. 164const int kAsanHeapLeftRedzoneMagic = 0xfa; 165const int kAsanHeapRightRedzoneMagic = 0xfb; 166const int kAsanHeapFreeMagic = 0xfd; 167const int kAsanStackLeftRedzoneMagic = 0xf1; 168const int kAsanStackMidRedzoneMagic = 0xf2; 169const int kAsanStackRightRedzoneMagic = 0xf3; 170const int kAsanStackPartialRedzoneMagic = 0xf4; 171const int kAsanStackAfterReturnMagic = 0xf5; 172const int kAsanInitializationOrderMagic = 0xf6; 173const int kAsanUserPoisonedMemoryMagic = 0xf7; 174const int kAsanStackUseAfterScopeMagic = 0xf8; 175const int kAsanGlobalRedzoneMagic = 0xf9; 176const int kAsanInternalHeapMagic = 0xfe; 177 178static const uptr kCurrentStackFrameMagic = 0x41B58AB3; 179static const uptr kRetiredStackFrameMagic = 0x45E0360E; 180 181} // namespace __asan 182 183#endif // ASAN_INTERNAL_H 184