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