asan_internal.h revision 16e0075746b21ed866ec3be21ef0d1e46f0efed5
1ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//===-- asan_internal.h -----------------------------------------*- C++ -*-===// 285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// 3ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// The LLVM Compiler Infrastructure 4ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// 5ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// This file is distributed under the University of Illinois Open Source 6ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// License. See LICENSE.TXT for details. 7ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// 8ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//===----------------------------------------------------------------------===// 9ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// 10ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// This file is a part of AddressSanitizer, an address sanity checker. 11ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// 12ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// ASan-private header which defines various general utilities. 13ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru//===----------------------------------------------------------------------===// 14ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#ifndef ASAN_INTERNAL_H 15ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define ASAN_INTERNAL_H 16ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 17ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include "sanitizer_common/sanitizer_libc.h" 18ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 19ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32) 20ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# error "This operating system is not supported by AddressSanitizer" 21ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif 22ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 23ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#include <stddef.h> // for size_t, uintptr_t, etc. 24ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 25ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if defined(_WIN32) 26ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# if defined(__clang__) 27ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querutypedef int intptr_t; 28ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querutypedef unsigned int uintptr_t; 29ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# endif 30ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 31ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// There's no <stdint.h> in Visual Studio 9, so we have to define [u]int*_t. 32ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querutypedef unsigned __int8 uint8_t; 33ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querutypedef unsigned __int16 uint16_t; 34ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querutypedef unsigned __int32 uint32_t; 35ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querutypedef unsigned __int64 uint64_t; 36ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querutypedef __int8 int8_t; 37ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querutypedef __int16 int16_t; 38ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querutypedef __int32 int32_t; 39ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querutypedef __int64 int64_t; 40ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querutypedef unsigned long DWORD; // NOLINT 41ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 42ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern "C" void* _ReturnAddress(void); 43ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# pragma intrinsic(_ReturnAddress) 44ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 45ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define ALIAS(x) // TODO(timurrrr): do we need this on Windows? 46ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define ALIGNED(x) __declspec(align(x)) 47ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define NOINLINE __declspec(noinline) 48ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define NORETURN __declspec(noreturn) 49ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 50ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define ASAN_INTERFACE_ATTRIBUTE // TODO(timurrrr): do we need this on Win? 51ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#else // defined(_WIN32) 52ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# include <stdint.h> // for __WORDSIZE 53ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 54ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define ALIAS(x) __attribute__((alias(x))) 55ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define ALIGNED(x) __attribute__((aligned(x))) 56ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define NOINLINE __attribute__((noinline)) 57ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define NORETURN __attribute__((noreturn)) 5885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 5985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho# define ASAN_INTERFACE_ATTRIBUTE __attribute__((visibility("default"))) 6085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#endif // defined(_WIN32) 6185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 6285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// If __WORDSIZE was undefined by the platform, define it in terms of the 6385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// compiler built-ins __LP64__ and _WIN64. 6485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#ifndef __WORDSIZE 6585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#if __LP64__ || defined(_WIN64) 6685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define __WORDSIZE 64 6785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#else 6885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define __WORDSIZE 32 6985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#endif 7085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#endif 7185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 7285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// Limits for integral types. We have to redefine it in case we don't 7385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// have stdint.h (like in Visual Studio 9). 7485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#if __WORDSIZE == 64 7585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho# define __INT64_C(c) c ## L 7685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho# define __UINT64_C(c) c ## UL 7785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#else 7885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho# define __INT64_C(c) c ## LL 7985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho# define __UINT64_C(c) c ## ULL 8085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#endif // __WORDSIZE == 64 8185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#undef INT32_MIN 8285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define INT32_MIN (-2147483647-1) 83ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#undef INT32_MAX 84ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define INT32_MAX (2147483647) 85ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#undef UINT32_MAX 86ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define UINT32_MAX (4294967295U) 87ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#undef INT64_MIN 88ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define INT64_MIN (-__INT64_C(9223372036854775807)-1) 89ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#undef INT64_MAX 90ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define INT64_MAX (__INT64_C(9223372036854775807)) 91ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#undef UINT64_MAX 92ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define UINT64_MAX (__UINT64_C(18446744073709551615)) 93ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 94ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define ASAN_DEFAULT_FAILURE_EXITCODE 1 95ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 96ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if defined(__linux__) 97ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define ASAN_LINUX 1 98ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#else 99ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define ASAN_LINUX 0 100ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif 101ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 102ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if defined(__APPLE__) 103ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define ASAN_MAC 1 104ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#else 105ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define ASAN_MAC 0 106ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif 107ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 108ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if defined(_WIN32) 109ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define ASAN_WINDOWS 1 110ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#else 111ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define ASAN_WINDOWS 0 112ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif 11385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 114ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define ASAN_POSIX (ASAN_LINUX || ASAN_MAC) 115ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 116ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if !defined(__has_feature) 117ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define __has_feature(x) 0 118ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif 119ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 120ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#if __has_feature(address_sanitizer) 121ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# error "The AddressSanitizer run-time should not be" 122ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru " instrumented by AddressSanitizer" 123ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif 124ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 125ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Build-time configuration options. 126ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 127ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// If set, asan will install its own SEGV signal handler. 128ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#ifndef ASAN_NEEDS_SEGV 129ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define ASAN_NEEDS_SEGV 1 130ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif 131ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 132ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// If set, asan will intercept C++ exception api call(s). 133ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#ifndef ASAN_HAS_EXCEPTIONS 134ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define ASAN_HAS_EXCEPTIONS 1 135ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif 136ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 137ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// If set, asan uses the values of SHADOW_SCALE and SHADOW_OFFSET 138ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// provided by the instrumented objects. Otherwise constants are used. 139ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#ifndef ASAN_FLEXIBLE_MAPPING_AND_OFFSET 140ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define ASAN_FLEXIBLE_MAPPING_AND_OFFSET 0 141ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif 142ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 143ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// If set, values like allocator chunk size, as well as defaults for some flags 144ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// will be changed towards less memory overhead. 145ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#ifndef ASAN_LOW_MEMORY 146ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define ASAN_LOW_MEMORY 0 147ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif 148ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 149ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// All internal functions in asan reside inside the __asan namespace 150ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// to avoid namespace collisions with the user programs. 151ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Seperate namespace also makes it simpler to distinguish the asan run-time 152ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// functions from the instrumented user code in a profile. 153ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querunamespace __asan { 154ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 155ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruclass AsanThread; 156ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustruct AsanStackTrace; 157ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 158ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// asan_rtl.cc 159ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid NORETURN CheckFailed(const char *cond, const char *file, int line); 160ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid NORETURN ShowStatsAndAbort(); 161ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 162ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// asan_globals.cc 163ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querubool DescribeAddrIfGlobal(uintptr_t addr); 164ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 165ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid ReplaceOperatorsNewAndDelete(); 166ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// asan_malloc_linux.cc / asan_malloc_mac.cc 167ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid ReplaceSystemMalloc(); 168ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 169ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid OutOfMemoryMessageAndDie(const char *mem_type, size_t size); 170ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 171ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// asan_linux.cc / asan_mac.cc / asan_win.cc 172ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid *AsanDoesNotSupportStaticLinkage(); 173ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querubool AsanShadowRangeIsAvailable(); 174ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint AsanOpenReadonly(const char* filename); 175ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruconst char *AsanGetEnv(const char *name); 176ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid AsanDumpProcessMap(); 177ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 178ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid *AsanMmapFixedNoReserve(uintptr_t fixed_addr, size_t size); 179ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid *AsanMmapFixedReserve(uintptr_t fixed_addr, size_t size); 180ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid *AsanMprotect(uintptr_t fixed_addr, size_t size); 181ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid *AsanMmapSomewhereOrDie(size_t size, const char *where); 182ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid AsanUnmapOrDie(void *ptr, size_t size); 183ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 184ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid AsanDisableCoreDumper(); 185ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid GetPcSpBp(void *context, uintptr_t *pc, uintptr_t *sp, uintptr_t *bp); 186ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 187ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querusize_t AsanRead(int fd, void *buf, size_t count); 188ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querusize_t AsanWrite(int fd, const void *buf, size_t count); 189ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint AsanClose(int fd); 190ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 191ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querubool AsanInterceptsSignal(int signum); 192ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid SetAlternateSignalStack(); 193ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid UnsetAlternateSignalStack(); 194ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid InstallSignalHandlers(); 195ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint GetPid(); 196ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuintptr_t GetThreadSelf(); 197ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint AtomicInc(int *a); 198ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruuint16_t AtomicExchange(uint16_t *a, uint16_t new_val); 199ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 200ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Wrapper for TLS/TSD. 201ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid AsanTSDInit(void (*destructor)(void *tsd)); 202ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid *AsanTSDGet(); 203ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid AsanTSDSet(void *tsd); 204ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 205ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Opens the file 'file_name" and reads up to 'max_len' bytes. 206ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// The resulting buffer is mmaped and stored in '*buff'. 207ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// The size of the mmaped region is stored in '*buff_size', 208ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Returns the number of read bytes or 0 if file can not be opened. 209ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querusize_t ReadFileToBuffer(const char *file_name, char **buff, 210ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru size_t *buff_size, size_t max_len); 211ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 212ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// asan_printf.cc 213ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid RawWrite(const char *buffer); 214ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint SNPrintf(char *buffer, size_t length, const char *format, ...); 215ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid Printf(const char *format, ...); 216ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint SScanf(const char *str, const char *format, ...); 217ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid Report(const char *format, ...); 218ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 219ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Don't use std::min and std::max, to minimize dependency on libstdc++. 220ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querutemplate<class T> T Min(T a, T b) { return a < b ? a : b; } 221ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querutemplate<class T> T Max(T a, T b) { return a > b ? a : b; } 222ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 223ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid SortArray(uintptr_t *array, size_t size); 224ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 225ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// asan_poisoning.cc 226ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Poisons the shadow memory for "size" bytes starting from "addr". 227ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid PoisonShadow(uintptr_t addr, size_t size, uint8_t value); 228ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Poisons the shadow memory for "redzone_size" bytes starting from 229ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// "addr + size". 230ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid PoisonShadowPartialRightRedzone(uintptr_t addr, 231ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uintptr_t size, 232ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uintptr_t redzone_size, 233ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru uint8_t value); 234ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 235ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Platfrom-specific options. 236ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#ifdef __APPLE__ 237ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querubool PlatformHasDifferentMemcpyAndMemmove(); 238ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE \ 239ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru (PlatformHasDifferentMemcpyAndMemmove()) 24085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#else 241ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru# define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE true 242ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif // __APPLE__ 243ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 244ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern size_t FLAG_quarantine_size; 245ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern int64_t FLAG_demangle; 246ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern bool FLAG_symbolize; 24785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoextern int64_t FLAG_v; 248ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern size_t FLAG_redzone; 249ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern int64_t FLAG_debug; 250ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern bool FLAG_poison_shadow; 251ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern int64_t FLAG_report_globals; 252ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern size_t FLAG_malloc_context_size; 253ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern bool FLAG_replace_str; 254ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern bool FLAG_replace_intrin; 255ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern bool FLAG_replace_cfallocator; 256ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern bool FLAG_fast_unwind; 257ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern bool FLAG_use_fake_stack; 258ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern size_t FLAG_max_malloc_fill_size; 259ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern int64_t FLAG_exitcode; 260ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern bool FLAG_allow_user_poisoning; 261ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern int64_t FLAG_sleep_before_dying; 262ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern bool FLAG_handle_segv; 263ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern bool FLAG_use_sigaltstack; 264ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern bool FLAG_check_malloc_usable_size; 265ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 266ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern int asan_inited; 267ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// Used to avoid infinite recursion in __asan_init(). 268ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruextern bool asan_init_is_running; 269ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 270ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruenum LinkerInitialized { LINKER_INITIALIZED = 0 }; 271ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 272ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid NORETURN AsanDie(); 273ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid SleepForSeconds(int seconds); 274ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid NORETURN Exit(int exitcode); 275ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruvoid NORETURN Abort(); 276ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruint Atexit(void (*function)(void)); 277ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 278ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define CHECK(cond) do { if (!(cond)) { \ 279ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru CheckFailed(#cond, __FILE__, __LINE__); \ 280ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}}while(0) 281ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 282ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define RAW_CHECK_MSG(expr, msg) do { \ 283ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru if (!(expr)) { \ 284ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru RawWrite(msg); \ 285ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru AsanDie(); \ 286ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru } \ 287ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} while (0) 288ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 289ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr) 290ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 29185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define UNIMPLEMENTED() CHECK("unimplemented" && 0) 29285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 29385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#define ASAN_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) 29485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 29585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoconst size_t kWordSize = __WORDSIZE / 8; 29685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoconst size_t kWordSizeInBits = 8 * kWordSize; 29785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoconst size_t kPageSizeBits = 12; 29885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoconst size_t kPageSize = 1UL << kPageSizeBits; 29985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 30085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#if !defined(_WIN32) || defined(__clang__) 30185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho# define GET_CALLER_PC() (uintptr_t)__builtin_return_address(0) 30285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho# define GET_CURRENT_FRAME() (uintptr_t)__builtin_frame_address(0) 30385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#else 30485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho# define GET_CALLER_PC() (uintptr_t)_ReturnAddress() 30585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// CaptureStackBackTrace doesn't need to know BP on Windows. 30685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// FIXME: This macro is still used when printing error reports though it's not 30785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho// clear if the BP value is needed in the ASan reports on Windows. 30885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho# define GET_CURRENT_FRAME() (uintptr_t)0xDEADBEEF 30985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#endif 31085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 311ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#ifndef _WIN32 31285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoconst size_t kMmapGranularity = kPageSize; 31385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho# define THREAD_CALLING_CONV 31485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hotypedef void* thread_return_t; 31585bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#else 31685bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoconst size_t kMmapGranularity = 1UL << 16; 31785bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho# define THREAD_CALLING_CONV __stdcall 31885bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hotypedef DWORD thread_return_t; 31985bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho 32085bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho# ifndef ASAN_USE_EXTERNAL_SYMBOLIZER 32185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho# define ASAN_USE_EXTERNAL_SYMBOLIZER __asan_WinSymbolize 32285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hobool __asan_WinSymbolize(const void *addr, char *out_buffer, int buffer_size); 32385bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho# endif 32485bf2e2fbc60a9f938064abc8127d61da7d19882Claire Ho#endif 325ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 326ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querutypedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg); 327ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 328ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// These magic values are written to shadow for better error reporting. 329ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruconst int kAsanHeapLeftRedzoneMagic = 0xfa; 330ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruconst int kAsanHeapRightRedzoneMagic = 0xfb; 33185bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoconst int kAsanHeapFreeMagic = 0xfd; 33285bf2e2fbc60a9f938064abc8127d61da7d19882Claire Hoconst int kAsanStackLeftRedzoneMagic = 0xf1; 333ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruconst int kAsanStackMidRedzoneMagic = 0xf2; 334ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruconst int kAsanStackRightRedzoneMagic = 0xf3; 335ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruconst int kAsanStackPartialRedzoneMagic = 0xf4; 336ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruconst int kAsanStackAfterReturnMagic = 0xf5; 337ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruconst int kAsanUserPoisonedMemoryMagic = 0xf7; 338ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruconst int kAsanGlobalRedzoneMagic = 0xf9; 339ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruconst int kAsanInternalHeapMagic = 0xfe; 340ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 341ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const uintptr_t kCurrentStackFrameMagic = 0x41B58AB3; 342ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Querustatic const uintptr_t kRetiredStackFrameMagic = 0x45E0360E; 343ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 344ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// --------------------------- Bit twiddling ------- {{{1 345ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruinline bool IsPowerOfTwo(size_t x) { 346ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return (x & (x - 1)) == 0; 347ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 348ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 349ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruinline size_t RoundUpTo(size_t size, size_t boundary) { 350ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru CHECK(IsPowerOfTwo(boundary)); 351ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru return (size + boundary - 1) & ~(boundary - 1); 352ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} 353ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 354ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// -------------------------- LowLevelAllocator ----- {{{1 355ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru// A simple low-level memory allocator for internal use. 356ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queruclass LowLevelAllocator { 357ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru public: 358ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru explicit LowLevelAllocator(LinkerInitialized) {} 359ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // 'size' must be a power of two. 360ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru // Requires an external lock. 361ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru void *Allocate(size_t size); 362ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru private: 363ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru char *allocated_end_; 364ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru char *allocated_current_; 365ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru}; 366ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 367ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru} // namespace __asan 368ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru 369ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru#endif // ASAN_INTERNAL_H 370ac04d0bbe12b3ef54518635711412f178cb4d16Jean-Baptiste Queru