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