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