]>
Commit | Line | Data |
---|---|---|
f35db108 WM |
1 | //===-- sanitizer_internal_defs.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 | // It contains macro used in run-time libraries code. | |
10 | //===----------------------------------------------------------------------===// | |
11 | #ifndef SANITIZER_DEFS_H | |
12 | #define SANITIZER_DEFS_H | |
13 | ||
ef1b3fda KS |
14 | #include "sanitizer_platform.h" |
15 | ||
696d846a MO |
16 | #ifndef SANITIZER_DEBUG |
17 | # define SANITIZER_DEBUG 0 | |
18 | #endif | |
19 | ||
ef1b3fda KS |
20 | // Only use SANITIZER_*ATTRIBUTE* before the function return type! |
21 | #if SANITIZER_WINDOWS | |
22 | # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport) | |
23 | // FIXME find out what we need on Windows, if anything. | |
b4ab7d34 | 24 | # define SANITIZER_WEAK_ATTRIBUTE |
10189819 | 25 | #elif SANITIZER_GO |
b4ab7d34 KS |
26 | # define SANITIZER_INTERFACE_ATTRIBUTE |
27 | # define SANITIZER_WEAK_ATTRIBUTE | |
28 | #else | |
29 | # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default"))) | |
30 | # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak)) | |
31 | #endif | |
32 | ||
10189819 | 33 | #if (SANITIZER_LINUX || SANITIZER_WINDOWS) && !SANITIZER_GO |
b4ab7d34 KS |
34 | # define SANITIZER_SUPPORTS_WEAK_HOOKS 1 |
35 | #else | |
36 | # define SANITIZER_SUPPORTS_WEAK_HOOKS 0 | |
37 | #endif | |
38 | ||
866e32ad KS |
39 | // We can use .preinit_array section on Linux to call sanitizer initialization |
40 | // functions very early in the process startup (unless PIC macro is defined). | |
41 | // FIXME: do we have anything like this on Mac? | |
42 | #if SANITIZER_LINUX && !SANITIZER_ANDROID && !defined(PIC) | |
43 | # define SANITIZER_CAN_USE_PREINIT_ARRAY 1 | |
44 | #else | |
45 | # define SANITIZER_CAN_USE_PREINIT_ARRAY 0 | |
df77f0e4 KS |
46 | #endif |
47 | ||
7df59255 | 48 | // GCC does not understand __has_feature |
b4ab7d34 KS |
49 | #if !defined(__has_feature) |
50 | # define __has_feature(x) 0 | |
51 | #endif | |
52 | ||
53 | // For portability reasons we do not include stddef.h, stdint.h or any other | |
54 | // system header, but we do need some basic types that are not defined | |
55 | // in a portable way by the language itself. | |
56 | namespace __sanitizer { | |
57 | ||
58 | #if defined(_WIN64) | |
59 | // 64-bit Windows uses LLP64 data model. | |
60 | typedef unsigned long long uptr; // NOLINT | |
61 | typedef signed long long sptr; // NOLINT | |
62 | #else | |
63 | typedef unsigned long uptr; // NOLINT | |
64 | typedef signed long sptr; // NOLINT | |
65 | #endif // defined(_WIN64) | |
66 | #if defined(__x86_64__) | |
dee5ea7a | 67 | // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use |
b4ab7d34 KS |
68 | // 64-bit pointer to unwind stack frame. |
69 | typedef unsigned long long uhwptr; // NOLINT | |
70 | #else | |
71 | typedef uptr uhwptr; // NOLINT | |
72 | #endif | |
73 | typedef unsigned char u8; | |
74 | typedef unsigned short u16; // NOLINT | |
75 | typedef unsigned int u32; | |
76 | typedef unsigned long long u64; // NOLINT | |
77 | typedef signed char s8; | |
78 | typedef signed short s16; // NOLINT | |
79 | typedef signed int s32; | |
80 | typedef signed long long s64; // NOLINT | |
696d846a MO |
81 | #if SANITIZER_WINDOWS |
82 | // On Windows, files are HANDLE, which is a synonim of void*. | |
83 | // Use void* to avoid including <windows.h> everywhere. | |
84 | typedef void* fd_t; | |
85 | typedef unsigned error_t; | |
86 | #else | |
b4ab7d34 | 87 | typedef int fd_t; |
696d846a MO |
88 | typedef int error_t; |
89 | #endif | |
10189819 | 90 | typedef int pid_t; |
b4ab7d34 | 91 | |
ef1b3fda KS |
92 | // WARNING: OFF_T may be different from OS type off_t, depending on the value of |
93 | // _FILE_OFFSET_BITS. This definition of OFF_T matches the ABI of system calls | |
94 | // like pread and mmap, as opposed to pread64 and mmap64. | |
696d846a MO |
95 | // FreeBSD, Mac and Linux/x86-64 are special. |
96 | #if SANITIZER_FREEBSD || SANITIZER_MAC || \ | |
97 | (SANITIZER_LINUX && defined(__x86_64__)) | |
ef1b3fda KS |
98 | typedef u64 OFF_T; |
99 | #else | |
100 | typedef uptr OFF_T; | |
101 | #endif | |
102 | typedef u64 OFF64_T; | |
df77f0e4 KS |
103 | |
104 | #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC | |
105 | typedef uptr operator_new_size_type; | |
106 | #else | |
10189819 MO |
107 | # if defined(__s390__) && !defined(__s390x__) |
108 | // Special case: 31-bit s390 has unsigned long as size_t. | |
109 | typedef unsigned long operator_new_size_type; | |
110 | # else | |
df77f0e4 | 111 | typedef u32 operator_new_size_type; |
10189819 | 112 | # endif |
df77f0e4 | 113 | #endif |
b4ab7d34 | 114 | |
b4ab7d34 | 115 | |
f35db108 WM |
116 | // ----------- ATTENTION ------------- |
117 | // This header should NOT include any other headers to avoid portability issues. | |
118 | ||
119 | // Common defs. | |
ef1b3fda | 120 | #define INLINE inline |
f35db108 | 121 | #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE |
10189819 MO |
122 | #define SANITIZER_WEAK_DEFAULT_IMPL \ |
123 | extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE | |
124 | #define SANITIZER_WEAK_CXX_DEFAULT_IMPL \ | |
125 | extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE | |
f35db108 WM |
126 | |
127 | // Platform-specific defs. | |
e297eb60 | 128 | #if defined(_MSC_VER) |
ef1b3fda | 129 | # define ALWAYS_INLINE __forceinline |
f35db108 WM |
130 | // FIXME(timurrrr): do we need this on Windows? |
131 | # define ALIAS(x) | |
132 | # define ALIGNED(x) __declspec(align(x)) | |
133 | # define FORMAT(f, a) | |
134 | # define NOINLINE __declspec(noinline) | |
135 | # define NORETURN __declspec(noreturn) | |
136 | # define THREADLOCAL __declspec(thread) | |
e297eb60 KS |
137 | # define LIKELY(x) (x) |
138 | # define UNLIKELY(x) (x) | |
10189819 | 139 | # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0 |
e297eb60 | 140 | #else // _MSC_VER |
ef1b3fda | 141 | # define ALWAYS_INLINE inline __attribute__((always_inline)) |
f35db108 | 142 | # define ALIAS(x) __attribute__((alias(x))) |
ef1b3fda KS |
143 | // Please only use the ALIGNED macro before the type. |
144 | // Using ALIGNED after the variable declaration is not portable! | |
f35db108 WM |
145 | # define ALIGNED(x) __attribute__((aligned(x))) |
146 | # define FORMAT(f, a) __attribute__((format(printf, f, a))) | |
147 | # define NOINLINE __attribute__((noinline)) | |
148 | # define NORETURN __attribute__((noreturn)) | |
149 | # define THREADLOCAL __thread | |
f35db108 WM |
150 | # define LIKELY(x) __builtin_expect(!!(x), 1) |
151 | # define UNLIKELY(x) __builtin_expect(!!(x), 0) | |
2660d12d KS |
152 | # if defined(__i386__) || defined(__x86_64__) |
153 | // __builtin_prefetch(x) generates prefetchnt0 on x86 | |
154 | # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x)) | |
155 | # else | |
156 | # define PREFETCH(x) __builtin_prefetch(x) | |
157 | # endif | |
e297eb60 | 158 | #endif // _MSC_VER |
f35db108 | 159 | |
dee5ea7a KS |
160 | #if !defined(_MSC_VER) || defined(__clang__) |
161 | # define UNUSED __attribute__((unused)) | |
162 | # define USED __attribute__((used)) | |
163 | #else | |
164 | # define UNUSED | |
165 | # define USED | |
166 | #endif | |
167 | ||
696d846a MO |
168 | #if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900) |
169 | # define NOEXCEPT noexcept | |
170 | #else | |
171 | # define NOEXCEPT throw() | |
172 | #endif | |
173 | ||
ef1b3fda KS |
174 | // Unaligned versions of basic types. |
175 | typedef ALIGNED(1) u16 uu16; | |
176 | typedef ALIGNED(1) u32 uu32; | |
177 | typedef ALIGNED(1) u64 uu64; | |
178 | typedef ALIGNED(1) s16 us16; | |
179 | typedef ALIGNED(1) s32 us32; | |
180 | typedef ALIGNED(1) s64 us64; | |
181 | ||
182 | #if SANITIZER_WINDOWS | |
10189819 | 183 | } // namespace __sanitizer |
e297eb60 | 184 | typedef unsigned long DWORD; // NOLINT |
10189819 | 185 | namespace __sanitizer { |
f35db108 WM |
186 | typedef DWORD thread_return_t; |
187 | # define THREAD_CALLING_CONV __stdcall | |
188 | #else // _WIN32 | |
189 | typedef void* thread_return_t; | |
190 | # define THREAD_CALLING_CONV | |
191 | #endif // _WIN32 | |
192 | typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg); | |
193 | ||
f35db108 | 194 | // NOTE: Functions below must be defined in each run-time. |
f35db108 | 195 | void NORETURN Die(); |
ef1b3fda KS |
196 | |
197 | // FIXME: No, this shouldn't be in the sanitizer interface. | |
198 | SANITIZER_INTERFACE_ATTRIBUTE | |
f35db108 WM |
199 | void NORETURN CheckFailed(const char *file, int line, const char *cond, |
200 | u64 v1, u64 v2); | |
f35db108 WM |
201 | |
202 | // Check macro | |
203 | #define RAW_CHECK_MSG(expr, msg) do { \ | |
dee5ea7a | 204 | if (UNLIKELY(!(expr))) { \ |
f35db108 WM |
205 | RawWrite(msg); \ |
206 | Die(); \ | |
207 | } \ | |
208 | } while (0) | |
209 | ||
210 | #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr) | |
211 | ||
212 | #define CHECK_IMPL(c1, op, c2) \ | |
213 | do { \ | |
214 | __sanitizer::u64 v1 = (u64)(c1); \ | |
215 | __sanitizer::u64 v2 = (u64)(c2); \ | |
dee5ea7a | 216 | if (UNLIKELY(!(v1 op v2))) \ |
f35db108 WM |
217 | __sanitizer::CheckFailed(__FILE__, __LINE__, \ |
218 | "(" #c1 ") " #op " (" #c2 ")", v1, v2); \ | |
219 | } while (false) \ | |
220 | /**/ | |
221 | ||
222 | #define CHECK(a) CHECK_IMPL((a), !=, 0) | |
223 | #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b)) | |
224 | #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b)) | |
225 | #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b)) | |
226 | #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b)) | |
227 | #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b)) | |
228 | #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b)) | |
229 | ||
696d846a | 230 | #if SANITIZER_DEBUG |
f35db108 WM |
231 | #define DCHECK(a) CHECK(a) |
232 | #define DCHECK_EQ(a, b) CHECK_EQ(a, b) | |
233 | #define DCHECK_NE(a, b) CHECK_NE(a, b) | |
234 | #define DCHECK_LT(a, b) CHECK_LT(a, b) | |
235 | #define DCHECK_LE(a, b) CHECK_LE(a, b) | |
236 | #define DCHECK_GT(a, b) CHECK_GT(a, b) | |
237 | #define DCHECK_GE(a, b) CHECK_GE(a, b) | |
238 | #else | |
239 | #define DCHECK(a) | |
240 | #define DCHECK_EQ(a, b) | |
241 | #define DCHECK_NE(a, b) | |
242 | #define DCHECK_LT(a, b) | |
243 | #define DCHECK_LE(a, b) | |
244 | #define DCHECK_GT(a, b) | |
245 | #define DCHECK_GE(a, b) | |
246 | #endif | |
247 | ||
e297eb60 KS |
248 | #define UNREACHABLE(msg) do { \ |
249 | CHECK(0 && msg); \ | |
250 | Die(); \ | |
251 | } while (0) | |
252 | ||
253 | #define UNIMPLEMENTED() UNREACHABLE("unimplemented") | |
f35db108 WM |
254 | |
255 | #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__) | |
256 | ||
257 | #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) | |
258 | ||
259 | #define IMPL_PASTE(a, b) a##b | |
260 | #define IMPL_COMPILER_ASSERT(pred, line) \ | |
261 | typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1] | |
262 | ||
263 | // Limits for integral types. We have to redefine it in case we don't | |
264 | // have stdint.h (like in Visual Studio 9). | |
265 | #undef __INT64_C | |
266 | #undef __UINT64_C | |
e297eb60 | 267 | #if SANITIZER_WORDSIZE == 64 |
f35db108 WM |
268 | # define __INT64_C(c) c ## L |
269 | # define __UINT64_C(c) c ## UL | |
270 | #else | |
271 | # define __INT64_C(c) c ## LL | |
272 | # define __UINT64_C(c) c ## ULL | |
e297eb60 | 273 | #endif // SANITIZER_WORDSIZE == 64 |
f35db108 WM |
274 | #undef INT32_MIN |
275 | #define INT32_MIN (-2147483647-1) | |
276 | #undef INT32_MAX | |
277 | #define INT32_MAX (2147483647) | |
278 | #undef UINT32_MAX | |
279 | #define UINT32_MAX (4294967295U) | |
280 | #undef INT64_MIN | |
281 | #define INT64_MIN (-__INT64_C(9223372036854775807)-1) | |
282 | #undef INT64_MAX | |
283 | #define INT64_MAX (__INT64_C(9223372036854775807)) | |
284 | #undef UINT64_MAX | |
285 | #define UINT64_MAX (__UINT64_C(18446744073709551615)) | |
286 | ||
287 | enum LinkerInitialized { LINKER_INITIALIZED = 0 }; | |
288 | ||
289 | #if !defined(_MSC_VER) || defined(__clang__) | |
1509524b JJ |
290 | # if SANITIZER_S390_31 |
291 | # define GET_CALLER_PC() \ | |
292 | (uptr)__builtin_extract_return_addr(__builtin_return_address(0)) | |
293 | # else | |
294 | # define GET_CALLER_PC() (uptr)__builtin_return_address(0) | |
295 | # endif | |
f35db108 | 296 | # define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0) |
10189819 MO |
297 | inline void Trap() { |
298 | __builtin_trap(); | |
299 | } | |
f35db108 WM |
300 | #else |
301 | extern "C" void* _ReturnAddress(void); | |
10189819 | 302 | extern "C" void* _AddressOfReturnAddress(void); |
f35db108 | 303 | # pragma intrinsic(_ReturnAddress) |
10189819 | 304 | # pragma intrinsic(_AddressOfReturnAddress) |
f35db108 WM |
305 | # define GET_CALLER_PC() (uptr)_ReturnAddress() |
306 | // CaptureStackBackTrace doesn't need to know BP on Windows. | |
10189819 MO |
307 | # define GET_CURRENT_FRAME() (((uptr)_AddressOfReturnAddress()) + sizeof(uptr)) |
308 | ||
309 | extern "C" void __ud2(void); | |
310 | # pragma intrinsic(__ud2) | |
311 | inline void Trap() { | |
312 | __ud2(); | |
313 | } | |
f35db108 WM |
314 | #endif |
315 | ||
ef1b3fda KS |
316 | #define HANDLE_EINTR(res, f) \ |
317 | { \ | |
318 | int rverrno; \ | |
319 | do { \ | |
320 | res = (f); \ | |
321 | } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \ | |
f35db108 WM |
322 | } |
323 | ||
696d846a MO |
324 | // Forces the compiler to generate a frame pointer in the function. |
325 | #define ENABLE_FRAME_POINTER \ | |
326 | do { \ | |
327 | volatile uptr enable_fp; \ | |
328 | enable_fp = GET_CURRENT_FRAME(); \ | |
329 | (void)enable_fp; \ | |
330 | } while (0) | |
331 | ||
10189819 MO |
332 | } // namespace __sanitizer |
333 | ||
334 | namespace __asan { using namespace __sanitizer; } // NOLINT | |
335 | namespace __dsan { using namespace __sanitizer; } // NOLINT | |
336 | namespace __dfsan { using namespace __sanitizer; } // NOLINT | |
337 | namespace __esan { using namespace __sanitizer; } // NOLINT | |
338 | namespace __lsan { using namespace __sanitizer; } // NOLINT | |
339 | namespace __msan { using namespace __sanitizer; } // NOLINT | |
340 | namespace __tsan { using namespace __sanitizer; } // NOLINT | |
341 | namespace __scudo { using namespace __sanitizer; } // NOLINT | |
342 | namespace __ubsan { using namespace __sanitizer; } // NOLINT | |
343 | namespace __xray { using namespace __sanitizer; } // NOLINT | |
344 | namespace __interception { using namespace __sanitizer; } // NOLINT | |
345 | ||
346 | ||
f35db108 | 347 | #endif // SANITIZER_DEFS_H |