2012-10-29 Wei Mi <wmi@google.com>
[official-gcc.git] / libasan / sanitizer_common / sanitizer_common.h
blobcddefd7ea090036e53539b134ace52081090df26
1 //===-- sanitizer_common.h --------------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is shared between AddressSanitizer and ThreadSanitizer
9 // run-time libraries.
10 // It declares common functions and classes that are used in both runtimes.
11 // Implementation of some functions are provided in sanitizer_common, while
12 // others must be defined by run-time library itself.
13 //===----------------------------------------------------------------------===//
14 #ifndef SANITIZER_COMMON_H
15 #define SANITIZER_COMMON_H
17 #include "sanitizer_internal_defs.h"
19 namespace __sanitizer {
21 // Constants.
22 const uptr kWordSize = __WORDSIZE / 8;
23 const uptr kWordSizeInBits = 8 * kWordSize;
24 const uptr kPageSizeBits = 12;
25 const uptr kPageSize = 1UL << kPageSizeBits;
26 const uptr kCacheLineSize = 64;
27 #ifndef _WIN32
28 const uptr kMmapGranularity = kPageSize;
29 #else
30 const uptr kMmapGranularity = 1UL << 16;
31 #endif
33 // Threads
34 int GetPid();
35 uptr GetTid();
36 uptr GetThreadSelf();
37 void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
38 uptr *stack_bottom);
40 // Memory management
41 void *MmapOrDie(uptr size, const char *mem_type);
42 void UnmapOrDie(void *addr, uptr size);
43 void *MmapFixedNoReserve(uptr fixed_addr, uptr size);
44 void *Mprotect(uptr fixed_addr, uptr size);
45 // Used to check if we can map shadow memory to a fixed location.
46 bool MemoryRangeIsAvailable(uptr range_start, uptr range_end);
48 // Internal allocator
49 void *InternalAlloc(uptr size);
50 void InternalFree(void *p);
51 // Given the pointer p into a valid allocated block,
52 // returns a pointer to the beginning of the block.
53 void *InternalAllocBlock(void *p);
55 // InternalScopedBuffer can be used instead of large stack arrays to
56 // keep frame size low.
57 // FIXME: use InternalAlloc instead of MmapOrDie once
58 // InternalAlloc is made libc-free.
59 template<typename T>
60 class InternalScopedBuffer {
61 public:
62 explicit InternalScopedBuffer(uptr cnt) {
63 cnt_ = cnt;
64 ptr_ = (T*)MmapOrDie(cnt * sizeof(T), "InternalScopedBuffer");
66 ~InternalScopedBuffer() {
67 UnmapOrDie(ptr_, cnt_ * sizeof(T));
69 T &operator[](uptr i) { return ptr_[i]; }
70 T *data() { return ptr_; }
71 uptr size() { return cnt_ * sizeof(T); }
73 private:
74 T *ptr_;
75 uptr cnt_;
76 // Disallow evil constructors.
77 InternalScopedBuffer(const InternalScopedBuffer&);
78 void operator=(const InternalScopedBuffer&);
81 // Simple low-level (mmap-based) allocator for internal use. Doesn't have
82 // constructor, so all instances of LowLevelAllocator should be
83 // linker initialized.
84 class LowLevelAllocator {
85 public:
86 // Requires an external lock.
87 void *Allocate(uptr size);
88 private:
89 char *allocated_end_;
90 char *allocated_current_;
92 typedef void (*LowLevelAllocateCallback)(uptr ptr, uptr size);
93 // Allows to register tool-specific callbacks for LowLevelAllocator.
94 // Passing NULL removes the callback.
95 void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback);
97 // IO
98 void RawWrite(const char *buffer);
99 void Printf(const char *format, ...);
100 void Report(const char *format, ...);
101 void SetPrintfAndReportCallback(void (*callback)(const char *));
103 // Opens the file 'file_name" and reads up to 'max_len' bytes.
104 // The resulting buffer is mmaped and stored in '*buff'.
105 // The size of the mmaped region is stored in '*buff_size',
106 // Returns the number of read bytes or 0 if file can not be opened.
107 uptr ReadFileToBuffer(const char *file_name, char **buff,
108 uptr *buff_size, uptr max_len);
109 // Maps given file to virtual memory, and returns pointer to it
110 // (or NULL if the mapping failes). Stores the size of mmaped region
111 // in '*buff_size'.
112 void *MapFileToMemory(const char *file_name, uptr *buff_size);
114 // OS
115 void DisableCoreDumper();
116 void DumpProcessMap();
117 const char *GetEnv(const char *name);
118 const char *GetPwd();
119 void ReExec();
120 bool StackSizeIsUnlimited();
121 void SetStackSizeLimitInBytes(uptr limit);
123 // Other
124 void SleepForSeconds(int seconds);
125 void SleepForMillis(int millis);
126 int Atexit(void (*function)(void));
127 void SortArray(uptr *array, uptr size);
129 // Exit
130 void NORETURN Abort();
131 void NORETURN Exit(int exitcode);
132 void NORETURN Die();
133 void NORETURN SANITIZER_INTERFACE_ATTRIBUTE
134 CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2);
136 // Specific tools may override behavior of "Die" and "CheckFailed" functions
137 // to do tool-specific job.
138 void SetDieCallback(void (*callback)(void));
139 typedef void (*CheckFailedCallbackType)(const char *, int, const char *,
140 u64, u64);
141 void SetCheckFailedCallback(CheckFailedCallbackType callback);
143 // Math
144 INLINE bool IsPowerOfTwo(uptr x) {
145 return (x & (x - 1)) == 0;
147 INLINE uptr RoundUpTo(uptr size, uptr boundary) {
148 CHECK(IsPowerOfTwo(boundary));
149 return (size + boundary - 1) & ~(boundary - 1);
151 // Don't use std::min, std::max or std::swap, to minimize dependency
152 // on libstdc++.
153 template<class T> T Min(T a, T b) { return a < b ? a : b; }
154 template<class T> T Max(T a, T b) { return a > b ? a : b; }
155 template<class T> void Swap(T& a, T& b) {
156 T tmp = a;
157 a = b;
158 b = tmp;
161 // Char handling
162 INLINE bool IsSpace(int c) {
163 return (c == ' ') || (c == '\n') || (c == '\t') ||
164 (c == '\f') || (c == '\r') || (c == '\v');
166 INLINE bool IsDigit(int c) {
167 return (c >= '0') && (c <= '9');
169 INLINE int ToLower(int c) {
170 return (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c;
173 #if __WORDSIZE == 64
174 # define FIRST_32_SECOND_64(a, b) (b)
175 #else
176 # define FIRST_32_SECOND_64(a, b) (a)
177 #endif
179 } // namespace __sanitizer
181 #endif // SANITIZER_COMMON_H