1 | //===-- sanitizer_internal_defs.h -------------------------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file is shared between AddressSanitizer and ThreadSanitizer. |
10 | // It contains macro used in run-time libraries code. |
11 | //===----------------------------------------------------------------------===// |
12 | #ifndef SANITIZER_DEFS_H |
13 | #define SANITIZER_DEFS_H |
14 | |
15 | #include "sanitizer_platform.h" |
16 | |
17 | #ifndef SANITIZER_DEBUG |
18 | # define SANITIZER_DEBUG 0 |
19 | #endif |
20 | |
21 | #define SANITIZER_STRINGIFY_(S) #S |
22 | #define SANITIZER_STRINGIFY(S) SANITIZER_STRINGIFY_(S) |
23 | |
24 | // Only use SANITIZER_*ATTRIBUTE* before the function return type! |
25 | #if SANITIZER_WINDOWS |
26 | #if SANITIZER_IMPORT_INTERFACE |
27 | # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllimport) |
28 | #else |
29 | # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport) |
30 | #endif |
31 | # define SANITIZER_WEAK_ATTRIBUTE |
32 | #elif SANITIZER_GO |
33 | # define SANITIZER_INTERFACE_ATTRIBUTE |
34 | # define SANITIZER_WEAK_ATTRIBUTE |
35 | #else |
36 | # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default"))) |
37 | # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak)) |
38 | #endif |
39 | |
40 | //--------------------------- WEAK FUNCTIONS ---------------------------------// |
41 | // When working with weak functions, to simplify the code and make it more |
42 | // portable, when possible define a default implementation using this macro: |
43 | // |
44 | // SANITIZER_INTERFACE_WEAK_DEF(<return_type>, <name>, <parameter list>) |
45 | // |
46 | // For example: |
47 | // SANITIZER_INTERFACE_WEAK_DEF(bool, compare, int a, int b) { return a > b; } |
48 | // |
49 | #if SANITIZER_WINDOWS |
50 | #include "sanitizer_win_defs.h" |
51 | # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \ |
52 | WIN_WEAK_EXPORT_DEF(ReturnType, Name, __VA_ARGS__) |
53 | #else |
54 | # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \ |
55 | extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE \ |
56 | ReturnType Name(__VA_ARGS__) |
57 | #endif |
58 | |
59 | // SANITIZER_SUPPORTS_WEAK_HOOKS means that we support real weak functions that |
60 | // will evaluate to a null pointer when not defined. |
61 | #ifndef SANITIZER_SUPPORTS_WEAK_HOOKS |
62 | #if (SANITIZER_LINUX || SANITIZER_SOLARIS) && !SANITIZER_GO |
63 | # define SANITIZER_SUPPORTS_WEAK_HOOKS 1 |
64 | // Before Xcode 4.5, the Darwin linker doesn't reliably support undefined |
65 | // weak symbols. Mac OS X 10.9/Darwin 13 is the first release only supported |
66 | // by Xcode >= 4.5. |
67 | #elif SANITIZER_APPLE && \ |
68 | __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1090 && !SANITIZER_GO |
69 | # define SANITIZER_SUPPORTS_WEAK_HOOKS 1 |
70 | #else |
71 | # define SANITIZER_SUPPORTS_WEAK_HOOKS 0 |
72 | #endif |
73 | #endif // SANITIZER_SUPPORTS_WEAK_HOOKS |
74 | // For some weak hooks that will be called very often and we want to avoid the |
75 | // overhead of executing the default implementation when it is not necessary, |
76 | // we can use the flag SANITIZER_SUPPORTS_WEAK_HOOKS to only define the default |
77 | // implementation for platforms that doesn't support weak symbols. For example: |
78 | // |
79 | // #if !SANITIZER_SUPPORT_WEAK_HOOKS |
80 | // SANITIZER_INTERFACE_WEAK_DEF(bool, compare_hook, int a, int b) { |
81 | // return a > b; |
82 | // } |
83 | // #endif |
84 | // |
85 | // And then use it as: if (compare_hook) compare_hook(a, b); |
86 | //----------------------------------------------------------------------------// |
87 | |
88 | |
89 | // We can use .preinit_array section on Linux to call sanitizer initialization |
90 | // functions very early in the process startup (unless PIC macro is defined). |
91 | // |
92 | // On FreeBSD, .preinit_array functions are called with rtld_bind_lock writer |
93 | // lock held. It will lead to dead lock if unresolved PLT functions (which helds |
94 | // rtld_bind_lock reader lock) are called inside .preinit_array functions. |
95 | // |
96 | // FIXME: do we have anything like this on Mac? |
97 | #ifndef SANITIZER_CAN_USE_PREINIT_ARRAY |
98 | #if (SANITIZER_LINUX || SANITIZER_FUCHSIA || SANITIZER_NETBSD) && !defined(PIC) |
99 | #define SANITIZER_CAN_USE_PREINIT_ARRAY 1 |
100 | // Before Solaris 11.4, .preinit_array is fully supported only with GNU ld. |
101 | // FIXME: Check for those conditions. |
102 | #elif SANITIZER_SOLARIS && !defined(PIC) |
103 | # define SANITIZER_CAN_USE_PREINIT_ARRAY 1 |
104 | #else |
105 | # define SANITIZER_CAN_USE_PREINIT_ARRAY 0 |
106 | #endif |
107 | #endif // SANITIZER_CAN_USE_PREINIT_ARRAY |
108 | |
109 | // GCC does not understand __has_feature |
110 | #if !defined(__has_feature) |
111 | # define __has_feature(x) 0 |
112 | #endif |
113 | |
114 | // Older GCCs do not understand __has_attribute. |
115 | #if !defined(__has_attribute) |
116 | # define __has_attribute(x) 0 |
117 | #endif |
118 | |
119 | #if !defined(__has_cpp_attribute) |
120 | # define __has_cpp_attribute(x) 0 |
121 | #endif |
122 | |
123 | // For portability reasons we do not include stddef.h, stdint.h or any other |
124 | // system header, but we do need some basic types that are not defined |
125 | // in a portable way by the language itself. |
126 | namespace __sanitizer { |
127 | |
128 | #if defined(_WIN64) |
129 | // 64-bit Windows uses LLP64 data model. |
130 | typedef unsigned long long uptr; |
131 | typedef signed long long sptr; |
132 | #else |
133 | # if (SANITIZER_WORDSIZE == 64) || SANITIZER_APPLE || SANITIZER_WINDOWS |
134 | typedef unsigned long uptr; |
135 | typedef signed long sptr; |
136 | # else |
137 | typedef unsigned int uptr; |
138 | typedef signed int sptr; |
139 | # endif |
140 | #endif // defined(_WIN64) |
141 | #if defined(__x86_64__) |
142 | // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use |
143 | // 64-bit pointer to unwind stack frame. |
144 | typedef unsigned long long uhwptr; |
145 | #else |
146 | typedef uptr uhwptr; |
147 | #endif |
148 | typedef unsigned char u8; |
149 | typedef unsigned short u16; |
150 | typedef unsigned int u32; |
151 | typedef unsigned long long u64; |
152 | typedef signed char s8; |
153 | typedef signed short s16; |
154 | typedef signed int s32; |
155 | typedef signed long long s64; |
156 | #if SANITIZER_WINDOWS |
157 | // On Windows, files are HANDLE, which is a synonim of void*. |
158 | // Use void* to avoid including <windows.h> everywhere. |
159 | typedef void* fd_t; |
160 | typedef unsigned error_t; |
161 | #else |
162 | typedef int fd_t; |
163 | typedef int error_t; |
164 | #endif |
165 | #if SANITIZER_SOLARIS && !defined(_LP64) |
166 | typedef long pid_t; |
167 | #else |
168 | typedef int pid_t; |
169 | #endif |
170 | |
171 | #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_APPLE || \ |
172 | (SANITIZER_SOLARIS && (defined(_LP64) || _FILE_OFFSET_BITS == 64)) || \ |
173 | (SANITIZER_LINUX && !SANITIZER_GLIBC && !SANITIZER_ANDROID) || \ |
174 | (SANITIZER_LINUX && (defined(__x86_64__) || defined(__hexagon__))) |
175 | typedef u64 OFF_T; |
176 | #else |
177 | typedef uptr OFF_T; |
178 | #endif |
179 | typedef u64 OFF64_T; |
180 | |
181 | #if (SANITIZER_WORDSIZE == 64) || SANITIZER_APPLE |
182 | typedef uptr operator_new_size_type; |
183 | #else |
184 | # if defined(__s390__) && !defined(__s390x__) |
185 | // Special case: 31-bit s390 has unsigned long as size_t. |
186 | typedef unsigned long operator_new_size_type; |
187 | # else |
188 | typedef u32 operator_new_size_type; |
189 | # endif |
190 | #endif |
191 | |
192 | typedef u64 tid_t; |
193 | |
194 | // ----------- ATTENTION ------------- |
195 | // This header should NOT include any other headers to avoid portability issues. |
196 | |
197 | // Common defs. |
198 | #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE |
199 | #define SANITIZER_WEAK_DEFAULT_IMPL \ |
200 | extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE |
201 | #define SANITIZER_WEAK_CXX_DEFAULT_IMPL \ |
202 | extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE |
203 | |
204 | // Platform-specific defs. |
205 | #if defined(_MSC_VER) |
206 | # define ALWAYS_INLINE __forceinline |
207 | // FIXME(timurrrr): do we need this on Windows? |
208 | # define ALIAS(x) |
209 | # define ALIGNED(x) __declspec(align(x)) |
210 | # define FORMAT(f, a) |
211 | # define NOINLINE __declspec(noinline) |
212 | # define NORETURN __declspec(noreturn) |
213 | # define THREADLOCAL __declspec(thread) |
214 | # define LIKELY(x) (x) |
215 | # define UNLIKELY(x) (x) |
216 | # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0 |
217 | # define WARN_UNUSED_RESULT |
218 | #else // _MSC_VER |
219 | # define ALWAYS_INLINE inline __attribute__((always_inline)) |
220 | # define ALIAS(x) __attribute__((alias(x))) |
221 | // Please only use the ALIGNED macro before the type. |
222 | // Using ALIGNED after the variable declaration is not portable! |
223 | # define ALIGNED(x) __attribute__((aligned(x))) |
224 | # define FORMAT(f, a) __attribute__((format(printf, f, a))) |
225 | # define NOINLINE __attribute__((noinline)) |
226 | # define NORETURN __attribute__((noreturn)) |
227 | # define THREADLOCAL __thread |
228 | # define LIKELY(x) __builtin_expect(!!(x), 1) |
229 | # define UNLIKELY(x) __builtin_expect(!!(x), 0) |
230 | # if defined(__i386__) || defined(__x86_64__) |
231 | // __builtin_prefetch(x) generates prefetchnt0 on x86 |
232 | # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x)) |
233 | # else |
234 | # define PREFETCH(x) __builtin_prefetch(x) |
235 | # endif |
236 | # define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) |
237 | #endif // _MSC_VER |
238 | |
239 | #if !defined(_MSC_VER) || defined(__clang__) |
240 | # define UNUSED __attribute__((unused)) |
241 | # define USED __attribute__((used)) |
242 | #else |
243 | # define UNUSED |
244 | # define USED |
245 | #endif |
246 | |
247 | #if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900) |
248 | # define NOEXCEPT noexcept |
249 | #else |
250 | # define NOEXCEPT throw() |
251 | #endif |
252 | |
253 | #if __has_cpp_attribute(clang::fallthrough) |
254 | # define FALLTHROUGH [[clang::fallthrough]] |
255 | #elif __has_cpp_attribute(fallthrough) |
256 | # define FALLTHROUGH [[fallthrough]] |
257 | #else |
258 | # define FALLTHROUGH |
259 | #endif |
260 | |
261 | // Unaligned versions of basic types. |
262 | typedef ALIGNED(1) u16 uu16; |
263 | typedef ALIGNED(1) u32 uu32; |
264 | typedef ALIGNED(1) u64 uu64; |
265 | typedef ALIGNED(1) s16 us16; |
266 | typedef ALIGNED(1) s32 us32; |
267 | typedef ALIGNED(1) s64 us64; |
268 | |
269 | #if SANITIZER_WINDOWS |
270 | } // namespace __sanitizer |
271 | typedef unsigned long DWORD; |
272 | namespace __sanitizer { |
273 | typedef DWORD thread_return_t; |
274 | # define THREAD_CALLING_CONV __stdcall |
275 | #else // _WIN32 |
276 | typedef void* thread_return_t; |
277 | # define THREAD_CALLING_CONV |
278 | #endif // _WIN32 |
279 | typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg); |
280 | |
281 | // NOTE: Functions below must be defined in each run-time. |
282 | void NORETURN Die(); |
283 | |
284 | void NORETURN CheckFailed(const char *file, int line, const char *cond, |
285 | u64 v1, u64 v2); |
286 | |
287 | // Check macro |
288 | #define RAW_CHECK_MSG(expr, msg, ...) \ |
289 | do { \ |
290 | if (UNLIKELY(!(expr))) { \ |
291 | const char* msgs[] = {msg, __VA_ARGS__}; \ |
292 | for (const char* m : msgs) RawWrite(m); \ |
293 | Die(); \ |
294 | } \ |
295 | } while (0) |
296 | |
297 | #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr "\n", ) |
298 | #define RAW_CHECK_VA(expr, ...) RAW_CHECK_MSG(expr, #expr "\n", __VA_ARGS__) |
299 | |
300 | #define CHECK_IMPL(c1, op, c2) \ |
301 | do { \ |
302 | __sanitizer::u64 v1 = (__sanitizer::u64)(c1); \ |
303 | __sanitizer::u64 v2 = (__sanitizer::u64)(c2); \ |
304 | if (UNLIKELY(!(v1 op v2))) \ |
305 | __sanitizer::CheckFailed(__FILE__, __LINE__, \ |
306 | "(" #c1 ") " #op " (" #c2 ")", v1, v2); \ |
307 | } while (false) \ |
308 | /**/ |
309 | |
310 | #define CHECK(a) CHECK_IMPL((a), !=, 0) |
311 | #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b)) |
312 | #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b)) |
313 | #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b)) |
314 | #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b)) |
315 | #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b)) |
316 | #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b)) |
317 | |
318 | #if SANITIZER_DEBUG |
319 | #define DCHECK(a) CHECK(a) |
320 | #define DCHECK_EQ(a, b) CHECK_EQ(a, b) |
321 | #define DCHECK_NE(a, b) CHECK_NE(a, b) |
322 | #define DCHECK_LT(a, b) CHECK_LT(a, b) |
323 | #define DCHECK_LE(a, b) CHECK_LE(a, b) |
324 | #define DCHECK_GT(a, b) CHECK_GT(a, b) |
325 | #define DCHECK_GE(a, b) CHECK_GE(a, b) |
326 | #else |
327 | #define DCHECK(a) |
328 | #define DCHECK_EQ(a, b) |
329 | #define DCHECK_NE(a, b) |
330 | #define DCHECK_LT(a, b) |
331 | #define DCHECK_LE(a, b) |
332 | #define DCHECK_GT(a, b) |
333 | #define DCHECK_GE(a, b) |
334 | #endif |
335 | |
336 | #define UNREACHABLE(msg) do { \ |
337 | CHECK(0 && msg); \ |
338 | Die(); \ |
339 | } while (0) |
340 | |
341 | #define UNIMPLEMENTED() UNREACHABLE("unimplemented") |
342 | |
343 | #define COMPILER_CHECK(pred) static_assert(pred, "") |
344 | |
345 | #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) |
346 | |
347 | // Limits for integral types. We have to redefine it in case we don't |
348 | // have stdint.h (like in Visual Studio 9). |
349 | #undef __INT64_C |
350 | #undef __UINT64_C |
351 | #if SANITIZER_WORDSIZE == 64 |
352 | # define __INT64_C(c) c ## L |
353 | # define __UINT64_C(c) c ## UL |
354 | #else |
355 | # define __INT64_C(c) c ## LL |
356 | # define __UINT64_C(c) c ## ULL |
357 | #endif // SANITIZER_WORDSIZE == 64 |
358 | #undef INT32_MIN |
359 | #define INT32_MIN (-2147483647-1) |
360 | #undef INT32_MAX |
361 | #define INT32_MAX (2147483647) |
362 | #undef UINT32_MAX |
363 | #define UINT32_MAX (4294967295U) |
364 | #undef INT64_MIN |
365 | #define INT64_MIN (-__INT64_C(9223372036854775807)-1) |
366 | #undef INT64_MAX |
367 | #define INT64_MAX (__INT64_C(9223372036854775807)) |
368 | #undef UINT64_MAX |
369 | #define UINT64_MAX (__UINT64_C(18446744073709551615)) |
370 | #undef UINTPTR_MAX |
371 | #if SANITIZER_WORDSIZE == 64 |
372 | # define UINTPTR_MAX (18446744073709551615UL) |
373 | #else |
374 | # define UINTPTR_MAX (4294967295U) |
375 | #endif // SANITIZER_WORDSIZE == 64 |
376 | |
377 | enum LinkerInitialized { LINKER_INITIALIZED = 0 }; |
378 | |
379 | #if !defined(_MSC_VER) || defined(__clang__) |
380 | # define GET_CALLER_PC() \ |
381 | ((__sanitizer::uptr)__builtin_extract_return_addr( \ |
382 | __builtin_return_address(0))) |
383 | # define GET_CURRENT_FRAME() ((__sanitizer::uptr)__builtin_frame_address(0)) |
384 | inline void Trap() { |
385 | __builtin_trap(); |
386 | } |
387 | #else |
388 | extern "C" void* _ReturnAddress(void); |
389 | extern "C" void* _AddressOfReturnAddress(void); |
390 | # pragma intrinsic(_ReturnAddress) |
391 | # pragma intrinsic(_AddressOfReturnAddress) |
392 | # define GET_CALLER_PC() ((__sanitizer::uptr)_ReturnAddress()) |
393 | // CaptureStackBackTrace doesn't need to know BP on Windows. |
394 | # define GET_CURRENT_FRAME() \ |
395 | (((__sanitizer::uptr)_AddressOfReturnAddress()) + sizeof(__sanitizer::uptr)) |
396 | |
397 | extern "C" void __ud2(void); |
398 | # pragma intrinsic(__ud2) |
399 | inline void Trap() { |
400 | __ud2(); |
401 | } |
402 | #endif |
403 | |
404 | #define HANDLE_EINTR(res, f) \ |
405 | { \ |
406 | int rverrno; \ |
407 | do { \ |
408 | res = (f); \ |
409 | } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \ |
410 | } |
411 | |
412 | // Forces the compiler to generate a frame pointer in the function. |
413 | #define ENABLE_FRAME_POINTER \ |
414 | do { \ |
415 | volatile __sanitizer::uptr enable_fp; \ |
416 | enable_fp = GET_CURRENT_FRAME(); \ |
417 | (void)enable_fp; \ |
418 | } while (0) |
419 | |
420 | // Internal thread identifier allocated by ThreadRegistry. |
421 | typedef u32 Tid; |
422 | constexpr Tid kInvalidTid = -1; |
423 | constexpr Tid kMainTid = 0; |
424 | |
425 | // Stack depot stack identifier. |
426 | typedef u32 StackID; |
427 | const StackID kInvalidStackID = 0; |
428 | |
429 | } // namespace __sanitizer |
430 | |
431 | namespace __asan { |
432 | using namespace __sanitizer; |
433 | } |
434 | namespace __dsan { |
435 | using namespace __sanitizer; |
436 | } |
437 | namespace __dfsan { |
438 | using namespace __sanitizer; |
439 | } |
440 | namespace __lsan { |
441 | using namespace __sanitizer; |
442 | } |
443 | namespace __msan { |
444 | using namespace __sanitizer; |
445 | } |
446 | namespace __hwasan { |
447 | using namespace __sanitizer; |
448 | } |
449 | namespace __tsan { |
450 | using namespace __sanitizer; |
451 | } |
452 | namespace __scudo { |
453 | using namespace __sanitizer; |
454 | } |
455 | namespace __ubsan { |
456 | using namespace __sanitizer; |
457 | } |
458 | namespace __xray { |
459 | using namespace __sanitizer; |
460 | } |
461 | namespace __interception { |
462 | using namespace __sanitizer; |
463 | } |
464 | namespace __hwasan { |
465 | using namespace __sanitizer; |
466 | } |
467 | namespace __memprof { |
468 | using namespace __sanitizer; |
469 | } |
470 | |
471 | #endif // SANITIZER_DEFS_H |
472 | |