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