asan_internal.h revision 230c3be6cdd094a187f48e27ba0961dbeee70344
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 "sanitizer_common/sanitizer_common.h" 18#include "sanitizer_common/sanitizer_internal_defs.h" 19#include "sanitizer_common/sanitizer_libc.h" 20 21#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32) 22# error "This operating system is not supported by AddressSanitizer" 23#endif 24 25#if defined(_WIN32) 26extern "C" void* _ReturnAddress(void); 27# pragma intrinsic(_ReturnAddress) 28#endif // defined(_WIN32) 29 30// Limits for integral types. We have to redefine it in case we don't 31// have stdint.h (like in Visual Studio 9). 32#if __WORDSIZE == 64 33# define __INT64_C(c) c ## L 34# define __UINT64_C(c) c ## UL 35#else 36# define __INT64_C(c) c ## LL 37# define __UINT64_C(c) c ## ULL 38#endif // __WORDSIZE == 64 39#undef INT32_MIN 40#define INT32_MIN (-2147483647-1) 41#undef INT32_MAX 42#define INT32_MAX (2147483647) 43#undef UINT32_MAX 44#define UINT32_MAX (4294967295U) 45#undef INT64_MIN 46#define INT64_MIN (-__INT64_C(9223372036854775807)-1) 47#undef INT64_MAX 48#define INT64_MAX (__INT64_C(9223372036854775807)) 49#undef UINT64_MAX 50#define UINT64_MAX (__UINT64_C(18446744073709551615)) 51 52#define ASAN_DEFAULT_FAILURE_EXITCODE 1 53 54#if defined(__linux__) 55# define ASAN_LINUX 1 56#else 57# define ASAN_LINUX 0 58#endif 59 60#if defined(__APPLE__) 61# define ASAN_MAC 1 62#else 63# define ASAN_MAC 0 64#endif 65 66#if defined(_WIN32) 67# define ASAN_WINDOWS 1 68#else 69# define ASAN_WINDOWS 0 70#endif 71 72#define ASAN_POSIX (ASAN_LINUX || ASAN_MAC) 73 74#if __has_feature(address_sanitizer) 75# error "The AddressSanitizer run-time should not be" 76 " instrumented by AddressSanitizer" 77#endif 78 79// Build-time configuration options. 80 81// If set, asan will install its own SEGV signal handler. 82#ifndef ASAN_NEEDS_SEGV 83# define ASAN_NEEDS_SEGV 1 84#endif 85 86// If set, asan will intercept C++ exception api call(s). 87#ifndef ASAN_HAS_EXCEPTIONS 88# define ASAN_HAS_EXCEPTIONS 1 89#endif 90 91// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET 92// provided by the instrumented objects. Otherwise constants are used. 93#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET 94# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0 95#endif 96 97// If set, values like allocator chunk size, as well as defaults for some flags 98// will be changed towards less memory overhead. 99#ifndef ASAN_LOW_MEMORY 100# define ASAN_LOW_MEMORY 0 101#endif 102 103// All internal functions in asan reside inside the __asan namespace 104// to avoid namespace collisions with the user programs. 105// Seperate namespace also makes it simpler to distinguish the asan run-time 106// functions from the instrumented user code in a profile. 107namespace __asan { 108 109class AsanThread; 110struct AsanStackTrace; 111 112// asan_rtl.cc 113void NORETURN CheckFailed(const char *cond, const char *file, int line); 114void NORETURN ShowStatsAndAbort(); 115 116// asan_globals.cc 117bool DescribeAddrIfGlobal(uptr addr); 118 119void ReplaceOperatorsNewAndDelete(); 120// asan_malloc_linux.cc / asan_malloc_mac.cc 121void ReplaceSystemMalloc(); 122 123void OutOfMemoryMessageAndDie(const char *mem_type, uptr size); 124 125// asan_linux.cc / asan_mac.cc / asan_win.cc 126void *AsanDoesNotSupportStaticLinkage(); 127bool AsanShadowRangeIsAvailable(); 128const char *AsanGetEnv(const char *name); 129void AsanDumpProcessMap(); 130 131void *AsanMmapFixedNoReserve(uptr fixed_addr, uptr size); 132void *AsanMmapFixedReserve(uptr fixed_addr, uptr size); 133void *AsanMprotect(uptr fixed_addr, uptr size); 134void *AsanMmapSomewhereOrDie(uptr size, const char *where); 135void AsanUnmapOrDie(void *ptr, uptr size); 136 137void AsanDisableCoreDumper(); 138void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp); 139 140bool AsanInterceptsSignal(int signum); 141void SetAlternateSignalStack(); 142void UnsetAlternateSignalStack(); 143void InstallSignalHandlers(); 144uptr GetThreadSelf(); 145int AtomicInc(int *a); 146u16 AtomicExchange(u16 *a, u16 new_val); 147 148// Wrapper for TLS/TSD. 149void AsanTSDInit(void (*destructor)(void *tsd)); 150void *AsanTSDGet(); 151void AsanTSDSet(void *tsd); 152 153// Opens the file 'file_name" and reads up to 'max_len' bytes. 154// The resulting buffer is mmaped and stored in '*buff'. 155// The size of the mmaped region is stored in '*buff_size', 156// Returns the number of read bytes or 0 if file can not be opened. 157uptr ReadFileToBuffer(const char *file_name, char **buff, 158 uptr *buff_size, uptr max_len); 159 160// asan_printf.cc 161void RawWrite(const char *buffer); 162int SNPrintf(char *buffer, uptr length, const char *format, ...); 163void Printf(const char *format, ...); 164void Report(const char *format, ...); 165 166// Don't use std::min and std::max, to minimize dependency on libstdc++. 167template<class T> T Min(T a, T b) { return a < b ? a : b; } 168template<class T> T Max(T a, T b) { return a > b ? a : b; } 169 170void SortArray(uptr *array, uptr size); 171 172// asan_poisoning.cc 173// Poisons the shadow memory for "size" bytes starting from "addr". 174void PoisonShadow(uptr addr, uptr size, u8 value); 175// Poisons the shadow memory for "redzone_size" bytes starting from 176// "addr + size". 177void PoisonShadowPartialRightRedzone(uptr addr, 178 uptr size, 179 uptr redzone_size, 180 u8 value); 181 182// Platfrom-specific options. 183#ifdef __APPLE__ 184bool PlatformHasDifferentMemcpyAndMemmove(); 185# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \ 186 (PlatformHasDifferentMemcpyAndMemmove()) 187#else 188# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true 189#endif // __APPLE__ 190 191extern uptr FLAG_quarantine_size; 192extern s64 FLAG_demangle; 193extern bool FLAG_symbolize; 194extern s64 FLAG_v; 195extern uptr FLAG_redzone; 196extern s64 FLAG_debug; 197extern bool FLAG_poison_shadow; 198extern s64 FLAG_report_globals; 199extern uptr FLAG_malloc_context_size; 200extern bool FLAG_replace_str; 201extern bool FLAG_replace_intrin; 202extern bool FLAG_replace_cfallocator; 203extern bool FLAG_fast_unwind; 204extern bool FLAG_use_fake_stack; 205extern uptr FLAG_max_malloc_fill_size; 206extern s64 FLAG_exitcode; 207extern bool FLAG_allow_user_poisoning; 208extern s64 FLAG_sleep_before_dying; 209extern bool FLAG_handle_segv; 210extern bool FLAG_use_sigaltstack; 211extern bool FLAG_check_malloc_usable_size; 212extern bool FLAG_unmap_shadow_on_exit; 213extern bool FLAG_abort_on_error; 214 215extern int asan_inited; 216// Used to avoid infinite recursion in __asan_init(). 217extern bool asan_init_is_running; 218extern void (*death_callback)(void); 219 220enum LinkerInitialized { LINKER_INITIALIZED = 0 }; 221 222void SleepForSeconds(int seconds); 223void NORETURN Exit(int exitcode); 224void NORETURN Abort(); 225int Atexit(void (*function)(void)); 226 227#define CHECK(cond) do { if (!(cond)) { \ 228 CheckFailed(#cond, __FILE__, __LINE__); \ 229}}while(0) 230 231#define RAW_CHECK_MSG(expr, msg) do { \ 232 if (!(expr)) { \ 233 RawWrite(msg); \ 234 Die(); \ 235 } \ 236} while (0) 237 238#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr) 239 240#define UNIMPLEMENTED() CHECK("unimplemented" && 0) 241 242#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) 243 244#if !defined(_WIN32) || defined(__clang__) 245# define GET_CALLER_PC() (uptr)__builtin_return_address(0) 246# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0) 247#else 248# define GET_CALLER_PC() (uptr)_ReturnAddress() 249// CaptureStackBackTrace doesn't need to know BP on Windows. 250// FIXME: This macro is still used when printing error reports though it's not 251// clear if the BP value is needed in the ASan reports on Windows. 252# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF 253#endif 254 255#ifndef _WIN32 256const uptr kMmapGranularity = kPageSize; 257# define THREAD_CALLING_CONV 258typedef void* thread_return_t; 259#else 260const uptr kMmapGranularity = 1UL << 16; 261# define THREAD_CALLING_CONV __stdcall 262typedef DWORD thread_return_t; 263 264# ifndef ASAN_USE_EXTERNAL_SYMBOLIZER 265# define ASAN_USE_EXTERNAL_SYMBOLIZER __asan_WinSymbolize 266bool __asan_WinSymbolize(const void *addr, char *out_buffer, int buffer_size); 267# endif 268#endif 269 270typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg); 271 272// These magic values are written to shadow for better error reporting. 273const int kAsanHeapLeftRedzoneMagic = 0xfa; 274const int kAsanHeapRightRedzoneMagic = 0xfb; 275const int kAsanHeapFreeMagic = 0xfd; 276const int kAsanStackLeftRedzoneMagic = 0xf1; 277const int kAsanStackMidRedzoneMagic = 0xf2; 278const int kAsanStackRightRedzoneMagic = 0xf3; 279const int kAsanStackPartialRedzoneMagic = 0xf4; 280const int kAsanStackAfterReturnMagic = 0xf5; 281const int kAsanUserPoisonedMemoryMagic = 0xf7; 282const int kAsanGlobalRedzoneMagic = 0xf9; 283const int kAsanInternalHeapMagic = 0xfe; 284 285static const uptr kCurrentStackFrameMagic = 0x41B58AB3; 286static const uptr kRetiredStackFrameMagic = 0x45E0360E; 287 288// -------------------------- LowLevelAllocator ----- {{{1 289// A simple low-level memory allocator for internal use. 290class LowLevelAllocator { 291 public: 292 explicit LowLevelAllocator(LinkerInitialized) {} 293 // 'size' must be a power of two. 294 // Requires an external lock. 295 void *Allocate(uptr size); 296 private: 297 char *allocated_end_; 298 char *allocated_current_; 299}; 300 301} // namespace __asan 302 303#endif // ASAN_INTERNAL_H 304