asan_internal.h revision 94b5036ee6ba866e1702848855b6d687d1e70afa
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_internal_defs.h" 18#include "sanitizer_common/sanitizer_libc.h" 19 20#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32) 21# error "This operating system is not supported by AddressSanitizer" 22#endif 23 24#if defined(_WIN32) 25extern "C" void* _ReturnAddress(void); 26# pragma intrinsic(_ReturnAddress) 27#endif // defined(_WIN32) 28 29// Limits for integral types. We have to redefine it in case we don't 30// have stdint.h (like in Visual Studio 9). 31#if __WORDSIZE == 64 32# define __INT64_C(c) c ## L 33# define __UINT64_C(c) c ## UL 34#else 35# define __INT64_C(c) c ## LL 36# define __UINT64_C(c) c ## ULL 37#endif // __WORDSIZE == 64 38#undef INT32_MIN 39#define INT32_MIN (-2147483647-1) 40#undef INT32_MAX 41#define INT32_MAX (2147483647) 42#undef UINT32_MAX 43#define UINT32_MAX (4294967295U) 44#undef INT64_MIN 45#define INT64_MIN (-__INT64_C(9223372036854775807)-1) 46#undef INT64_MAX 47#define INT64_MAX (__INT64_C(9223372036854775807)) 48#undef UINT64_MAX 49#define UINT64_MAX (__UINT64_C(18446744073709551615)) 50 51#define ASAN_DEFAULT_FAILURE_EXITCODE 1 52 53#if defined(__linux__) 54# define ASAN_LINUX 1 55#else 56# define ASAN_LINUX 0 57#endif 58 59#if defined(__APPLE__) 60# define ASAN_MAC 1 61#else 62# define ASAN_MAC 0 63#endif 64 65#if defined(_WIN32) 66# define ASAN_WINDOWS 1 67#else 68# define ASAN_WINDOWS 0 69#endif 70 71#define ASAN_POSIX (ASAN_LINUX || ASAN_MAC) 72 73#if __has_feature(address_sanitizer) 74# error "The AddressSanitizer run-time should not be" 75 " instrumented by AddressSanitizer" 76#endif 77 78// Build-time configuration options. 79 80// If set, asan will install its own SEGV signal handler. 81#ifndef ASAN_NEEDS_SEGV 82# define ASAN_NEEDS_SEGV 1 83#endif 84 85// If set, asan will intercept C++ exception api call(s). 86#ifndef ASAN_HAS_EXCEPTIONS 87# define ASAN_HAS_EXCEPTIONS 1 88#endif 89 90// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET 91// provided by the instrumented objects. Otherwise constants are used. 92#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET 93# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0 94#endif 95 96// If set, values like allocator chunk size, as well as defaults for some flags 97// will be changed towards less memory overhead. 98#ifndef ASAN_LOW_MEMORY 99# define ASAN_LOW_MEMORY 0 100#endif 101 102// All internal functions in asan reside inside the __asan namespace 103// to avoid namespace collisions with the user programs. 104// Seperate namespace also makes it simpler to distinguish the asan run-time 105// functions from the instrumented user code in a profile. 106namespace __asan { 107 108class AsanThread; 109struct AsanStackTrace; 110 111// asan_rtl.cc 112void NORETURN CheckFailed(const char *cond, const char *file, int line); 113void NORETURN ShowStatsAndAbort(); 114 115// asan_globals.cc 116bool DescribeAddrIfGlobal(uptr addr); 117 118void ReplaceOperatorsNewAndDelete(); 119// asan_malloc_linux.cc / asan_malloc_mac.cc 120void ReplaceSystemMalloc(); 121 122void OutOfMemoryMessageAndDie(const char *mem_type, uptr size); 123 124// asan_linux.cc / asan_mac.cc / asan_win.cc 125void *AsanDoesNotSupportStaticLinkage(); 126bool AsanShadowRangeIsAvailable(); 127const char *AsanGetEnv(const char *name); 128void AsanDumpProcessMap(); 129 130void *AsanMmapFixedNoReserve(uptr fixed_addr, uptr size); 131void *AsanMmapFixedReserve(uptr fixed_addr, uptr size); 132void *AsanMprotect(uptr fixed_addr, uptr size); 133void *AsanMmapSomewhereOrDie(uptr size, const char *where); 134void AsanUnmapOrDie(void *ptr, uptr size); 135 136void AsanDisableCoreDumper(); 137void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp); 138 139bool AsanInterceptsSignal(int signum); 140void SetAlternateSignalStack(); 141void UnsetAlternateSignalStack(); 142void InstallSignalHandlers(); 143int GetPid(); 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; 212 213extern int asan_inited; 214// Used to avoid infinite recursion in __asan_init(). 215extern bool asan_init_is_running; 216 217enum LinkerInitialized { LINKER_INITIALIZED = 0 }; 218 219void NORETURN AsanDie(); 220void SleepForSeconds(int seconds); 221void NORETURN Exit(int exitcode); 222void NORETURN Abort(); 223int Atexit(void (*function)(void)); 224 225#define CHECK(cond) do { if (!(cond)) { \ 226 CheckFailed(#cond, __FILE__, __LINE__); \ 227}}while(0) 228 229#define RAW_CHECK_MSG(expr, msg) do { \ 230 if (!(expr)) { \ 231 RawWrite(msg); \ 232 AsanDie(); \ 233 } \ 234} while (0) 235 236#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr) 237 238#define UNIMPLEMENTED() CHECK("unimplemented" && 0) 239 240#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) 241 242const uptr kWordSize = __WORDSIZE / 8; 243const uptr kWordSizeInBits = 8 * kWordSize; 244const uptr kPageSizeBits = 12; 245const uptr kPageSize = 1UL << kPageSizeBits; 246 247#if !defined(_WIN32) || defined(__clang__) 248# define GET_CALLER_PC() (uptr)__builtin_return_address(0) 249# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0) 250#else 251# define GET_CALLER_PC() (uptr)_ReturnAddress() 252// CaptureStackBackTrace doesn't need to know BP on Windows. 253// FIXME: This macro is still used when printing error reports though it's not 254// clear if the BP value is needed in the ASan reports on Windows. 255# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF 256#endif 257 258#ifndef _WIN32 259const uptr kMmapGranularity = kPageSize; 260# define THREAD_CALLING_CONV 261typedef void* thread_return_t; 262#else 263const uptr kMmapGranularity = 1UL << 16; 264# define THREAD_CALLING_CONV __stdcall 265typedef DWORD thread_return_t; 266 267# ifndef ASAN_USE_EXTERNAL_SYMBOLIZER 268# define ASAN_USE_EXTERNAL_SYMBOLIZER __asan_WinSymbolize 269bool __asan_WinSymbolize(const void *addr, char *out_buffer, int buffer_size); 270# endif 271#endif 272 273typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg); 274 275// These magic values are written to shadow for better error reporting. 276const int kAsanHeapLeftRedzoneMagic = 0xfa; 277const int kAsanHeapRightRedzoneMagic = 0xfb; 278const int kAsanHeapFreeMagic = 0xfd; 279const int kAsanStackLeftRedzoneMagic = 0xf1; 280const int kAsanStackMidRedzoneMagic = 0xf2; 281const int kAsanStackRightRedzoneMagic = 0xf3; 282const int kAsanStackPartialRedzoneMagic = 0xf4; 283const int kAsanStackAfterReturnMagic = 0xf5; 284const int kAsanUserPoisonedMemoryMagic = 0xf7; 285const int kAsanGlobalRedzoneMagic = 0xf9; 286const int kAsanInternalHeapMagic = 0xfe; 287 288static const uptr kCurrentStackFrameMagic = 0x41B58AB3; 289static const uptr kRetiredStackFrameMagic = 0x45E0360E; 290 291// --------------------------- Bit twiddling ------- {{{1 292inline bool IsPowerOfTwo(uptr x) { 293 return (x & (x - 1)) == 0; 294} 295 296inline uptr RoundUpTo(uptr size, uptr boundary) { 297 CHECK(IsPowerOfTwo(boundary)); 298 return (size + boundary - 1) & ~(boundary - 1); 299} 300 301// -------------------------- LowLevelAllocator ----- {{{1 302// A simple low-level memory allocator for internal use. 303class LowLevelAllocator { 304 public: 305 explicit LowLevelAllocator(LinkerInitialized) {} 306 // 'size' must be a power of two. 307 // Requires an external lock. 308 void *Allocate(uptr size); 309 private: 310 char *allocated_end_; 311 char *allocated_current_; 312}; 313 314} // namespace __asan 315 316#endif // ASAN_INTERNAL_H 317