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