sanitizer_common.h revision 84a996fc1057ffea9213608c47a54c3d3d3aed02
1//===-- sanitizer_common.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 shared between AddressSanitizer and ThreadSanitizer 11// run-time libraries. 12// It declares common functions and classes that are used in both runtimes. 13// Implementation of some functions are provided in sanitizer_common, while 14// others must be defined by run-time library itself. 15//===----------------------------------------------------------------------===// 16#ifndef SANITIZER_COMMON_H 17#define SANITIZER_COMMON_H 18 19#include "sanitizer_internal_defs.h" 20 21namespace __sanitizer { 22 23// Constants. 24const uptr kWordSize = SANITIZER_WORDSIZE / 8; 25const uptr kWordSizeInBits = 8 * kWordSize; 26 27#if defined(__powerpc__) || defined(__powerpc64__) 28const uptr kCacheLineSize = 128; 29#else 30const uptr kCacheLineSize = 64; 31#endif 32 33uptr GetPageSize(); 34uptr GetPageSizeCached(); 35uptr GetMmapGranularity(); 36// Threads 37int GetPid(); 38uptr GetTid(); 39uptr GetThreadSelf(); 40void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top, 41 uptr *stack_bottom); 42 43// Memory management 44void *MmapOrDie(uptr size, const char *mem_type); 45void UnmapOrDie(void *addr, uptr size); 46void *MmapFixedNoReserve(uptr fixed_addr, uptr size); 47void *Mprotect(uptr fixed_addr, uptr size); 48// Map aligned chunk of address space; size and alignment are powers of two. 49void *MmapAlignedOrDie(uptr size, uptr alignment, const char *mem_type); 50// Used to check if we can map shadow memory to a fixed location. 51bool MemoryRangeIsAvailable(uptr range_start, uptr range_end); 52 53// Internal allocator 54void *InternalAlloc(uptr size); 55void InternalFree(void *p); 56 57// InternalScopedBuffer can be used instead of large stack arrays to 58// keep frame size low. 59// FIXME: use InternalAlloc instead of MmapOrDie once 60// InternalAlloc is made libc-free. 61template<typename T> 62class InternalScopedBuffer { 63 public: 64 explicit InternalScopedBuffer(uptr cnt) { 65 cnt_ = cnt; 66 ptr_ = (T*)MmapOrDie(cnt * sizeof(T), "InternalScopedBuffer"); 67 } 68 ~InternalScopedBuffer() { 69 UnmapOrDie(ptr_, cnt_ * sizeof(T)); 70 } 71 T &operator[](uptr i) { return ptr_[i]; } 72 T *data() { return ptr_; } 73 uptr size() { return cnt_ * sizeof(T); } 74 75 private: 76 T *ptr_; 77 uptr cnt_; 78 // Disallow evil constructors. 79 InternalScopedBuffer(const InternalScopedBuffer&); 80 void operator=(const InternalScopedBuffer&); 81}; 82 83// Simple low-level (mmap-based) allocator for internal use. Doesn't have 84// constructor, so all instances of LowLevelAllocator should be 85// linker initialized. 86class LowLevelAllocator { 87 public: 88 // Requires an external lock. 89 void *Allocate(uptr size); 90 private: 91 char *allocated_end_; 92 char *allocated_current_; 93}; 94typedef void (*LowLevelAllocateCallback)(uptr ptr, uptr size); 95// Allows to register tool-specific callbacks for LowLevelAllocator. 96// Passing NULL removes the callback. 97void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback); 98 99// IO 100void RawWrite(const char *buffer); 101bool PrintsToTty(); 102void Printf(const char *format, ...); 103void Report(const char *format, ...); 104void SetPrintfAndReportCallback(void (*callback)(const char *)); 105 106// Opens the file 'file_name" and reads up to 'max_len' bytes. 107// The resulting buffer is mmaped and stored in '*buff'. 108// The size of the mmaped region is stored in '*buff_size', 109// Returns the number of read bytes or 0 if file can not be opened. 110uptr ReadFileToBuffer(const char *file_name, char **buff, 111 uptr *buff_size, uptr max_len); 112// Maps given file to virtual memory, and returns pointer to it 113// (or NULL if the mapping failes). Stores the size of mmaped region 114// in '*buff_size'. 115void *MapFileToMemory(const char *file_name, uptr *buff_size); 116 117// OS 118void DisableCoreDumper(); 119void DumpProcessMap(); 120bool FileExists(const char *filename); 121const char *GetEnv(const char *name); 122const char *GetPwd(); 123void ReExec(); 124bool StackSizeIsUnlimited(); 125void SetStackSizeLimitInBytes(uptr limit); 126void PrepareForSandboxing(); 127 128// Other 129void SleepForSeconds(int seconds); 130void SleepForMillis(int millis); 131int Atexit(void (*function)(void)); 132void SortArray(uptr *array, uptr size); 133 134// Exit 135void NORETURN Abort(); 136void NORETURN Exit(int exitcode); 137void NORETURN Die(); 138void NORETURN SANITIZER_INTERFACE_ATTRIBUTE 139CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2); 140 141// Set the name of the current thread to 'name', return true on succees. 142// The name may be truncated to a system-dependent limit. 143bool SanitizerSetThreadName(const char *name); 144// Get the name of the current thread (no more than max_len bytes), 145// return true on succees. name should have space for at least max_len+1 bytes. 146bool SanitizerGetThreadName(char *name, int max_len); 147 148// Specific tools may override behavior of "Die" and "CheckFailed" functions 149// to do tool-specific job. 150void SetDieCallback(void (*callback)(void)); 151typedef void (*CheckFailedCallbackType)(const char *, int, const char *, 152 u64, u64); 153void SetCheckFailedCallback(CheckFailedCallbackType callback); 154 155// Math 156INLINE bool IsPowerOfTwo(uptr x) { 157 return (x & (x - 1)) == 0; 158} 159INLINE uptr RoundUpTo(uptr size, uptr boundary) { 160 CHECK(IsPowerOfTwo(boundary)); 161 return (size + boundary - 1) & ~(boundary - 1); 162} 163INLINE bool IsAligned(uptr a, uptr alignment) { 164 return (a & (alignment - 1)) == 0; 165} 166// Don't use std::min, std::max or std::swap, to minimize dependency 167// on libstdc++. 168template<class T> T Min(T a, T b) { return a < b ? a : b; } 169template<class T> T Max(T a, T b) { return a > b ? a : b; } 170template<class T> void Swap(T& a, T& b) { 171 T tmp = a; 172 a = b; 173 b = tmp; 174} 175 176// Char handling 177INLINE bool IsSpace(int c) { 178 return (c == ' ') || (c == '\n') || (c == '\t') || 179 (c == '\f') || (c == '\r') || (c == '\v'); 180} 181INLINE bool IsDigit(int c) { 182 return (c >= '0') && (c <= '9'); 183} 184INLINE int ToLower(int c) { 185 return (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c; 186} 187 188#if SANITIZER_WORDSIZE == 64 189# define FIRST_32_SECOND_64(a, b) (b) 190#else 191# define FIRST_32_SECOND_64(a, b) (a) 192#endif 193 194} // namespace __sanitizer 195 196#endif // SANITIZER_COMMON_H 197