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