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 && (!defined(__GNUC__) || defined(__clang__))
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__))) || \
198 SANITIZER_WASI
199typedef u64 OFF_T;
200#else
201typedef uptr OFF_T;
202#endif
203typedef u64 OFF64_T;
204
205#ifdef __SIZE_TYPE__
206typedef __SIZE_TYPE__ usize;
207#else
208typedef uptr usize;
209#endif
210
211#if defined(__s390__) && !defined(__s390x__)
212typedef long ssize;
213#else
214typedef sptr ssize;
215#endif
216
217typedef u64 ThreadID;
218
219// ----------- ATTENTION -------------
220// This header should NOT include any other headers to avoid portability issues.
221
222// Common defs.
223#define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
224#define SANITIZER_WEAK_DEFAULT_IMPL \
225 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
226#define SANITIZER_WEAK_CXX_DEFAULT_IMPL \
227 extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
228
229// Platform-specific defs.
230#if defined(_MSC_VER)
231# define ALWAYS_INLINE __forceinline
232// FIXME(timurrrr): do we need this on Windows?
233# define ALIAS(x)
234# define ALIGNED(x) __declspec(align(x))
235# define FORMAT(f, a)
236# define NOINLINE __declspec(noinline)
237# define NORETURN __declspec(noreturn)
238# define THREADLOCAL __declspec(thread)
239# define LIKELY(x) (x)
240# define UNLIKELY(x) (x)
241# define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0
242# define WARN_UNUSED_RESULT
243#else // _MSC_VER
244# define ALWAYS_INLINE inline __attribute__((always_inline))
245# define ALIAS(x) __attribute__((alias(SANITIZER_STRINGIFY(x))))
246// Please only use the ALIGNED macro before the type.
247// Using ALIGNED after the variable declaration is not portable!
248# define ALIGNED(x) __attribute__((aligned(x)))
249# define FORMAT(f, a) __attribute__((format(printf, f, a)))
250# define NOINLINE __attribute__((noinline))
251# define NORETURN __attribute__((noreturn))
252# define THREADLOCAL __thread
253# define LIKELY(x) __builtin_expect(!!(x), 1)
254# define UNLIKELY(x) __builtin_expect(!!(x), 0)
255# if defined(__i386__) || defined(__x86_64__)
256// __builtin_prefetch(x) generates prefetchnt0 on x86
257# define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
258# else
259# define PREFETCH(x) __builtin_prefetch(x)
260# endif
261# define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
262#endif // _MSC_VER
263
264#if !defined(_MSC_VER) || defined(__clang__)
265# define UNUSED __attribute__((unused))
266# define USED __attribute__((used))
267#else
268# define UNUSED
269# define USED
270#endif
271
272#if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)
273# define NOEXCEPT noexcept
274#else
275# define NOEXCEPT throw()
276#endif
277
278#if __has_cpp_attribute(clang::fallthrough)
279# define FALLTHROUGH [[clang::fallthrough]]
280#elif __has_cpp_attribute(fallthrough)
281# define FALLTHROUGH [[fallthrough]]
282#else
283# define FALLTHROUGH
284#endif
285
286#if __has_attribute(uninitialized)
287# define UNINITIALIZED __attribute__((uninitialized))
288#else
289# define UNINITIALIZED
290#endif
291
292// Unaligned versions of basic types.
293typedef ALIGNED(1) u16 uu16;
294typedef ALIGNED(1) u32 uu32;
295typedef ALIGNED(1) u64 uu64;
296typedef ALIGNED(1) s16 us16;
297typedef ALIGNED(1) s32 us32;
298typedef ALIGNED(1) s64 us64;
299
300#if SANITIZER_WINDOWS
301} // namespace __sanitizer
302typedef unsigned long DWORD;
303namespace __sanitizer {
304typedef DWORD thread_return_t;
305# define THREAD_CALLING_CONV __stdcall
306#else // _WIN32
307typedef void* thread_return_t;
308# define THREAD_CALLING_CONV
309#endif // _WIN32
310typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
311
312// NOTE: Functions below must be defined in each run-time.
313void NORETURN Die();
314
315void NORETURN CheckFailed(const char *file, int line, const char *cond,
316 u64 v1, u64 v2);
317
318// Check macro
319#define RAW_CHECK_MSG(expr, msg, ...) \
320 do { \
321 if (UNLIKELY(!(expr))) { \
322 const char* msgs[] = {msg, __VA_ARGS__}; \
323 for (const char* m : msgs) RawWrite(m); \
324 Die(); \
325 } \
326 } while (0)
327
328#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr "\n", )
329#define RAW_CHECK_VA(expr, ...) RAW_CHECK_MSG(expr, #expr "\n", __VA_ARGS__)
330
331#define CHECK_IMPL(c1, op, c2) \
332 do { \
333 __sanitizer::u64 v1 = (__sanitizer::u64)(c1); \
334 __sanitizer::u64 v2 = (__sanitizer::u64)(c2); \
335 if (UNLIKELY(!(v1 op v2))) \
336 __sanitizer::CheckFailed(__FILE__, __LINE__, \
337 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
338 } while (false) \
339/**/
340
341#define CHECK(a) CHECK_IMPL((a), !=, 0)
342#define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
343#define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
344#define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
345#define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
346#define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
347#define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
348
349#if SANITIZER_DEBUG
350#define DCHECK(a) CHECK(a)
351#define DCHECK_EQ(a, b) CHECK_EQ(a, b)
352#define DCHECK_NE(a, b) CHECK_NE(a, b)
353#define DCHECK_LT(a, b) CHECK_LT(a, b)
354#define DCHECK_LE(a, b) CHECK_LE(a, b)
355#define DCHECK_GT(a, b) CHECK_GT(a, b)
356#define DCHECK_GE(a, b) CHECK_GE(a, b)
357#else
358#define DCHECK(a)
359#define DCHECK_EQ(a, b)
360#define DCHECK_NE(a, b)
361#define DCHECK_LT(a, b)
362#define DCHECK_LE(a, b)
363#define DCHECK_GT(a, b)
364#define DCHECK_GE(a, b)
365#endif
366
367#define UNREACHABLE(msg) do { \
368 CHECK(0 && msg); \
369 Die(); \
370} while (0)
371
372#define UNIMPLEMENTED() UNREACHABLE("unimplemented")
373
374#define COMPILER_CHECK(pred) static_assert(pred, "")
375
376#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
377
378// Limits for integral types. We have to redefine it in case we don't
379// have stdint.h (like in Visual Studio 9).
380#undef __INT64_C
381#undef __UINT64_C
382#if SANITIZER_WORDSIZE == 64
383# define __INT64_C(c) c ## L
384# define __UINT64_C(c) c ## UL
385#else
386# define __INT64_C(c) c ## LL
387# define __UINT64_C(c) c ## ULL
388#endif // SANITIZER_WORDSIZE == 64
389#undef INT32_MIN
390#define INT32_MIN (-2147483647-1)
391#undef INT32_MAX
392#define INT32_MAX (2147483647)
393#undef UINT32_MAX
394#define UINT32_MAX (4294967295U)
395#undef INT64_MIN
396#define INT64_MIN (-__INT64_C(9223372036854775807)-1)
397#undef INT64_MAX
398#define INT64_MAX (__INT64_C(9223372036854775807))
399#undef UINT64_MAX
400#define UINT64_MAX (__UINT64_C(18446744073709551615))
401#undef UINTPTR_MAX
402#if SANITIZER_WORDSIZE == 64
403# define UINTPTR_MAX (18446744073709551615UL)
404#else
405# define UINTPTR_MAX (4294967295U)
406#endif // SANITIZER_WORDSIZE == 64
407
408enum LinkerInitialized { LINKER_INITIALIZED = 0 };
409
410#if !defined(_MSC_VER) || defined(__clang__)
411# define GET_CALLER_PC() \
412 ((__sanitizer::uptr)__builtin_extract_return_addr( \
413 __builtin_return_address(0)))
414# define GET_CURRENT_FRAME() ((__sanitizer::uptr)__builtin_frame_address(0))
415inline void Trap() {
416 __builtin_trap();
417}
418#else
419extern "C" void* _ReturnAddress(void);
420extern "C" void* _AddressOfReturnAddress(void);
421# pragma intrinsic(_ReturnAddress)
422# pragma intrinsic(_AddressOfReturnAddress)
423# define GET_CALLER_PC() ((__sanitizer::uptr)_ReturnAddress())
424// CaptureStackBackTrace doesn't need to know BP on Windows.
425# define GET_CURRENT_FRAME() \
426 (((__sanitizer::uptr)_AddressOfReturnAddress()) + sizeof(__sanitizer::uptr))
427
428extern "C" void __ud2(void);
429# pragma intrinsic(__ud2)
430inline void Trap() {
431 __ud2();
432}
433#endif
434
435#define HANDLE_EINTR(res, f) \
436 { \
437 int rverrno; \
438 do { \
439 res = (f); \
440 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
441 }
442
443// Forces the compiler to generate a frame pointer in the function.
444#define ENABLE_FRAME_POINTER \
445 do { \
446 volatile __sanitizer::uptr enable_fp; \
447 enable_fp = GET_CURRENT_FRAME(); \
448 (void)enable_fp; \
449 } while (0)
450
451// Internal thread identifier allocated by ThreadRegistry.
452typedef u32 Tid;
453constexpr Tid kInvalidTid = -1;
454constexpr Tid kMainTid = 0;
455
456// Stack depot stack identifier.
457typedef u32 StackID;
458const StackID kInvalidStackID = 0;
459
460} // namespace __sanitizer
461
462namespace __asan {
463using namespace __sanitizer;
464}
465namespace __dsan {
466using namespace __sanitizer;
467}
468namespace __dfsan {
469using namespace __sanitizer;
470}
471namespace __lsan {
472using namespace __sanitizer;
473}
474namespace __msan {
475using namespace __sanitizer;
476}
477namespace __nsan {
478using namespace __sanitizer;
479}
480namespace __hwasan {
481using namespace __sanitizer;
482}
483namespace __tsan {
484using namespace __sanitizer;
485}
486namespace __scudo {
487using namespace __sanitizer;
488}
489namespace __ubsan {
490using namespace __sanitizer;
491}
492namespace __xray {
493using namespace __sanitizer;
494}
495namespace __interception {
496using namespace __sanitizer;
497}
498namespace __hwasan {
499using namespace __sanitizer;
500}
501namespace __memprof {
502using namespace __sanitizer;
503}
504namespace __copyprof {
505using namespace __sanitizer;
506}
507
508#endif // SANITIZER_DEFS_H
509