asan_internal.h revision d4b5db8cb8a7a13bb5cc1d4ce53e8e088303c854
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#define ASAN_DEFAULT_FAILURE_EXITCODE 1 28 29#if defined(__linux__) 30# define ASAN_LINUX 1 31#else 32# define ASAN_LINUX 0 33#endif 34 35#if defined(__APPLE__) 36# define ASAN_MAC 1 37#else 38# define ASAN_MAC 0 39#endif 40 41#if defined(_WIN32) 42# define ASAN_WINDOWS 1 43#else 44# define ASAN_WINDOWS 0 45#endif 46 47#if defined(__ANDROID__) || defined(ANDROID) 48# define ASAN_ANDROID 1 49#else 50# define ASAN_ANDROID 0 51#endif 52 53 54#define ASAN_POSIX (ASAN_LINUX || ASAN_MAC) 55 56#if __has_feature(address_sanitizer) 57# error "The AddressSanitizer run-time should not be" 58 " instrumented by AddressSanitizer" 59#endif 60 61// Build-time configuration options. 62 63// If set, asan will install its own SEGV signal handler. 64#ifndef ASAN_NEEDS_SEGV 65# if ASAN_ANDROID == 1 66# define ASAN_NEEDS_SEGV 0 67# else 68# define ASAN_NEEDS_SEGV 1 69# endif 70#endif 71 72// If set, asan will intercept C++ exception api call(s). 73#ifndef ASAN_HAS_EXCEPTIONS 74# define ASAN_HAS_EXCEPTIONS 1 75#endif 76 77// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET 78// provided by the instrumented objects. Otherwise constants are used. 79#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET 80# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0 81#endif 82 83// If set, values like allocator chunk size, as well as defaults for some flags 84// will be changed towards less memory overhead. 85#ifndef ASAN_LOW_MEMORY 86# ifdef ASAN_ANDROID 87# define ASAN_LOW_MEMORY 1 88# else 89# define ASAN_LOW_MEMORY 0 90# endif 91#endif 92 93// All internal functions in asan reside inside the __asan namespace 94// to avoid namespace collisions with the user programs. 95// Seperate namespace also makes it simpler to distinguish the asan run-time 96// functions from the instrumented user code in a profile. 97namespace __asan { 98 99class AsanThread; 100using __sanitizer::StackTrace; 101 102// asan_rtl.cc 103void NORETURN ShowStatsAndAbort(); 104 105void ReplaceOperatorsNewAndDelete(); 106// asan_malloc_linux.cc / asan_malloc_mac.cc 107void ReplaceSystemMalloc(); 108 109// asan_linux.cc / asan_mac.cc / asan_win.cc 110void *AsanDoesNotSupportStaticLinkage(); 111 112void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp); 113 114void MaybeReexec(); 115bool AsanInterceptsSignal(int signum); 116void SetAlternateSignalStack(); 117void UnsetAlternateSignalStack(); 118void InstallSignalHandlers(); 119void ClearShadowMemoryForContext(void *context); 120void AsanPlatformThreadInit(); 121 122// Wrapper for TLS/TSD. 123void AsanTSDInit(void (*destructor)(void *tsd)); 124void *AsanTSDGet(); 125void AsanTSDSet(void *tsd); 126 127void AppendToErrorMessageBuffer(const char *buffer); 128 129// asan_poisoning.cc 130// Poisons the shadow memory for "size" bytes starting from "addr". 131void PoisonShadow(uptr addr, uptr size, u8 value); 132// Poisons the shadow memory for "redzone_size" bytes starting from 133// "addr + size". 134void PoisonShadowPartialRightRedzone(uptr addr, 135 uptr size, 136 uptr redzone_size, 137 u8 value); 138 139// Platfrom-specific options. 140#ifdef __APPLE__ 141bool PlatformHasDifferentMemcpyAndMemmove(); 142# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \ 143 (PlatformHasDifferentMemcpyAndMemmove()) 144#else 145# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true 146#endif // __APPLE__ 147 148extern int asan_inited; 149// Used to avoid infinite recursion in __asan_init(). 150extern bool asan_init_is_running; 151extern void (*death_callback)(void); 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 kAsanStackUseAfterScopeMagic = 0xf8; 165const int kAsanGlobalRedzoneMagic = 0xf9; 166const int kAsanInternalHeapMagic = 0xfe; 167 168static const uptr kCurrentStackFrameMagic = 0x41B58AB3; 169static const uptr kRetiredStackFrameMagic = 0x45E0360E; 170 171} // namespace __asan 172 173#endif // ASAN_INTERNAL_H 174