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.
126namespace __sanitizer {
127
128#if defined(_WIN64)
129// 64-bit Windows uses LLP64 data model.
130typedef unsigned long long uptr;
131typedef signed long long sptr;
132#else
133# if (SANITIZER_WORDSIZE == 64) || SANITIZER_APPLE || SANITIZER_WINDOWS
134typedef unsigned long uptr;
135typedef signed long sptr;
136# else
137typedef unsigned int uptr;
138typedef 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.
144typedef unsigned long long uhwptr;
145#else
146typedef uptr uhwptr;
147#endif
148typedef unsigned char u8;
149typedef unsigned short u16;
150typedef unsigned int u32;
151typedef unsigned long long u64;
152typedef signed char s8;
153typedef signed short s16;
154typedef signed int s32;
155typedef 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.
159typedef void* fd_t;
160typedef unsigned error_t;
161#else
162typedef int fd_t;
163typedef int error_t;
164#endif
165#if SANITIZER_SOLARIS && !defined(_LP64)
166typedef long pid_t;
167#else
168typedef 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__)))
175typedef u64 OFF_T;
176#else
177typedef uptr OFF_T;
178#endif
179typedef u64 OFF64_T;
180
181#if (SANITIZER_WORDSIZE == 64) || SANITIZER_APPLE
182typedef 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.
186typedef unsigned long operator_new_size_type;
187# else
188typedef u32 operator_new_size_type;
189# endif
190#endif
191
192typedef 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.
262typedef ALIGNED(1) u16 uu16;
263typedef ALIGNED(1) u32 uu32;
264typedef ALIGNED(1) u64 uu64;
265typedef ALIGNED(1) s16 us16;
266typedef ALIGNED(1) s32 us32;
267typedef ALIGNED(1) s64 us64;
268
269#if SANITIZER_WINDOWS
270} // namespace __sanitizer
271typedef unsigned long DWORD;
272namespace __sanitizer {
273typedef DWORD thread_return_t;
274# define THREAD_CALLING_CONV __stdcall
275#else // _WIN32
276typedef void* thread_return_t;
277# define THREAD_CALLING_CONV
278#endif // _WIN32
279typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
280
281// NOTE: Functions below must be defined in each run-time.
282void NORETURN Die();
283
284void 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
377enum 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))
384inline void Trap() {
385 __builtin_trap();
386}
387#else
388extern "C" void* _ReturnAddress(void);
389extern "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
397extern "C" void __ud2(void);
398# pragma intrinsic(__ud2)
399inline 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.
421typedef u32 Tid;
422constexpr Tid kInvalidTid = -1;
423constexpr Tid kMainTid = 0;
424
425// Stack depot stack identifier.
426typedef u32 StackID;
427const StackID kInvalidStackID = 0;
428
429} // namespace __sanitizer
430
431namespace __asan {
432using namespace __sanitizer;
433}
434namespace __dsan {
435using namespace __sanitizer;
436}
437namespace __dfsan {
438using namespace __sanitizer;
439}
440namespace __lsan {
441using namespace __sanitizer;
442}
443namespace __msan {
444using namespace __sanitizer;
445}
446namespace __hwasan {
447using namespace __sanitizer;
448}
449namespace __tsan {
450using namespace __sanitizer;
451}
452namespace __scudo {
453using namespace __sanitizer;
454}
455namespace __ubsan {
456using namespace __sanitizer;
457}
458namespace __xray {
459using namespace __sanitizer;
460}
461namespace __interception {
462using namespace __sanitizer;
463}
464namespace __hwasan {
465using namespace __sanitizer;
466}
467namespace __memprof {
468using namespace __sanitizer;
469}
470
471#endif // SANITIZER_DEFS_H
472

source code of compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h