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