asan_internal.h revision 1b5ea8fbbef73f5d9b41dbb26a21b9a0f4d1445e
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 "sanitizer_common/sanitizer_common.h" 19#include "sanitizer_common/sanitizer_internal_defs.h" 20#include "sanitizer_common/sanitizer_stacktrace.h" 21#include "sanitizer_common/sanitizer_libc.h" 22 23#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32) 24# error "This operating system is not supported by AddressSanitizer" 25#endif 26 27#if defined(_WIN32) 28extern "C" void* _ReturnAddress(void); 29# pragma intrinsic(_ReturnAddress) 30#endif // defined(_WIN32) 31 32#define ASAN_DEFAULT_FAILURE_EXITCODE 1 33 34#if defined(__linux__) 35# define ASAN_LINUX 1 36#else 37# define ASAN_LINUX 0 38#endif 39 40#if defined(__APPLE__) 41# define ASAN_MAC 1 42#else 43# define ASAN_MAC 0 44#endif 45 46#if defined(_WIN32) 47# define ASAN_WINDOWS 1 48#else 49# define ASAN_WINDOWS 0 50#endif 51 52#if defined(__ANDROID__) || defined(ANDROID) 53# define ASAN_ANDROID 1 54#else 55# define ASAN_ANDROID 0 56#endif 57 58 59#define ASAN_POSIX (ASAN_LINUX || ASAN_MAC) 60 61#if __has_feature(address_sanitizer) 62# error "The AddressSanitizer run-time should not be" 63 " instrumented by AddressSanitizer" 64#endif 65 66// Build-time configuration options. 67 68// If set, asan will install its own SEGV signal handler. 69#ifndef ASAN_NEEDS_SEGV 70# define ASAN_NEEDS_SEGV 1 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# define ASAN_LOW_MEMORY 0 88#endif 89 90// All internal functions in asan reside inside the __asan namespace 91// to avoid namespace collisions with the user programs. 92// Seperate namespace also makes it simpler to distinguish the asan run-time 93// functions from the instrumented user code in a profile. 94namespace __asan { 95 96class AsanThread; 97using __sanitizer::StackTrace; 98 99// asan_rtl.cc 100void NORETURN ShowStatsAndAbort(); 101 102void ReplaceOperatorsNewAndDelete(); 103// asan_malloc_linux.cc / asan_malloc_mac.cc 104void ReplaceSystemMalloc(); 105 106// asan_linux.cc / asan_mac.cc / asan_win.cc 107void *AsanDoesNotSupportStaticLinkage(); 108 109void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp); 110 111void MaybeReexec(); 112bool AsanInterceptsSignal(int signum); 113void SetAlternateSignalStack(); 114void UnsetAlternateSignalStack(); 115void InstallSignalHandlers(); 116void AsanPlatformThreadInit(); 117 118// Wrapper for TLS/TSD. 119void AsanTSDInit(void (*destructor)(void *tsd)); 120void *AsanTSDGet(); 121void AsanTSDSet(void *tsd); 122 123void AppendToErrorMessageBuffer(const char *buffer); 124 125// asan_poisoning.cc 126// Poisons the shadow memory for "size" bytes starting from "addr". 127void PoisonShadow(uptr addr, uptr size, u8 value); 128// Poisons the shadow memory for "redzone_size" bytes starting from 129// "addr + size". 130void PoisonShadowPartialRightRedzone(uptr addr, 131 uptr size, 132 uptr redzone_size, 133 u8 value); 134 135// Platfrom-specific options. 136#ifdef __APPLE__ 137bool PlatformHasDifferentMemcpyAndMemmove(); 138# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \ 139 (PlatformHasDifferentMemcpyAndMemmove()) 140#else 141# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true 142#endif // __APPLE__ 143 144extern int asan_inited; 145// Used to avoid infinite recursion in __asan_init(). 146extern bool asan_init_is_running; 147extern void (*death_callback)(void); 148 149#ifdef _WIN32 150bool WinSymbolize(const void *addr, char *out_buffer, int buffer_size); 151#endif // _WIN32 152 153// These magic values are written to shadow for better error reporting. 154const int kAsanHeapLeftRedzoneMagic = 0xfa; 155const int kAsanHeapRightRedzoneMagic = 0xfb; 156const int kAsanHeapFreeMagic = 0xfd; 157const int kAsanStackLeftRedzoneMagic = 0xf1; 158const int kAsanStackMidRedzoneMagic = 0xf2; 159const int kAsanStackRightRedzoneMagic = 0xf3; 160const int kAsanStackPartialRedzoneMagic = 0xf4; 161const int kAsanStackAfterReturnMagic = 0xf5; 162const int kAsanInitializationOrderMagic = 0xf6; 163const int kAsanUserPoisonedMemoryMagic = 0xf7; 164const int kAsanGlobalRedzoneMagic = 0xf9; 165const int kAsanInternalHeapMagic = 0xfe; 166 167static const uptr kCurrentStackFrameMagic = 0x41B58AB3; 168static const uptr kRetiredStackFrameMagic = 0x45E0360E; 169 170} // namespace __asan 171 172#endif // ASAN_INTERNAL_H 173