Add support for ARMv8-R architecture
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_internal_defs.h
blob676ade143d8863d219880e9fc0b822c614b51219
1 //===-- sanitizer_internal_defs.h -------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is shared between AddressSanitizer and ThreadSanitizer.
9 // It contains macro used in run-time libraries code.
10 //===----------------------------------------------------------------------===//
11 #ifndef SANITIZER_DEFS_H
12 #define SANITIZER_DEFS_H
14 #include "sanitizer_platform.h"
16 #ifndef SANITIZER_DEBUG
17 # define SANITIZER_DEBUG 0
18 #endif
20 // Only use SANITIZER_*ATTRIBUTE* before the function return type!
21 #if SANITIZER_WINDOWS
22 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport)
23 // FIXME find out what we need on Windows, if anything.
24 # define SANITIZER_WEAK_ATTRIBUTE
25 #elif SANITIZER_GO
26 # define SANITIZER_INTERFACE_ATTRIBUTE
27 # define SANITIZER_WEAK_ATTRIBUTE
28 #else
29 # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
30 # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
31 #endif
33 #if (SANITIZER_LINUX || SANITIZER_WINDOWS) && !SANITIZER_GO
34 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
35 #else
36 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
37 #endif
39 // We can use .preinit_array section on Linux to call sanitizer initialization
40 // functions very early in the process startup (unless PIC macro is defined).
41 // FIXME: do we have anything like this on Mac?
42 #if SANITIZER_LINUX && !SANITIZER_ANDROID && !defined(PIC)
43 # define SANITIZER_CAN_USE_PREINIT_ARRAY 1
44 #else
45 # define SANITIZER_CAN_USE_PREINIT_ARRAY 0
46 #endif
48 // GCC does not understand __has_feature
49 #if !defined(__has_feature)
50 # define __has_feature(x) 0
51 #endif
53 // For portability reasons we do not include stddef.h, stdint.h or any other
54 // system header, but we do need some basic types that are not defined
55 // in a portable way by the language itself.
56 namespace __sanitizer {
58 #if defined(_WIN64)
59 // 64-bit Windows uses LLP64 data model.
60 typedef unsigned long long uptr; // NOLINT
61 typedef signed long long sptr; // NOLINT
62 #else
63 typedef unsigned long uptr; // NOLINT
64 typedef signed long sptr; // NOLINT
65 #endif // defined(_WIN64)
66 #if defined(__x86_64__)
67 // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
68 // 64-bit pointer to unwind stack frame.
69 typedef unsigned long long uhwptr; // NOLINT
70 #else
71 typedef uptr uhwptr; // NOLINT
72 #endif
73 typedef unsigned char u8;
74 typedef unsigned short u16; // NOLINT
75 typedef unsigned int u32;
76 typedef unsigned long long u64; // NOLINT
77 typedef signed char s8;
78 typedef signed short s16; // NOLINT
79 typedef signed int s32;
80 typedef signed long long s64; // NOLINT
81 #if SANITIZER_WINDOWS
82 // On Windows, files are HANDLE, which is a synonim of void*.
83 // Use void* to avoid including <windows.h> everywhere.
84 typedef void* fd_t;
85 typedef unsigned error_t;
86 #else
87 typedef int fd_t;
88 typedef int error_t;
89 #endif
90 typedef int pid_t;
92 // WARNING: OFF_T may be different from OS type off_t, depending on the value of
93 // _FILE_OFFSET_BITS. This definition of OFF_T matches the ABI of system calls
94 // like pread and mmap, as opposed to pread64 and mmap64.
95 // FreeBSD, Mac and Linux/x86-64 are special.
96 #if SANITIZER_FREEBSD || SANITIZER_MAC || \
97 (SANITIZER_LINUX && defined(__x86_64__))
98 typedef u64 OFF_T;
99 #else
100 typedef uptr OFF_T;
101 #endif
102 typedef u64 OFF64_T;
104 #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
105 typedef uptr operator_new_size_type;
106 #else
107 # if defined(__s390__) && !defined(__s390x__)
108 // Special case: 31-bit s390 has unsigned long as size_t.
109 typedef unsigned long operator_new_size_type;
110 # else
111 typedef u32 operator_new_size_type;
112 # endif
113 #endif
116 // ----------- ATTENTION -------------
117 // This header should NOT include any other headers to avoid portability issues.
119 // Common defs.
120 #define INLINE inline
121 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
122 #define SANITIZER_WEAK_DEFAULT_IMPL \
123 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
124 #define SANITIZER_WEAK_CXX_DEFAULT_IMPL \
125 extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
127 // Platform-specific defs.
128 #if defined(_MSC_VER)
129 # define ALWAYS_INLINE __forceinline
130 // FIXME(timurrrr): do we need this on Windows?
131 # define ALIAS(x)
132 # define ALIGNED(x) __declspec(align(x))
133 # define FORMAT(f, a)
134 # define NOINLINE __declspec(noinline)
135 # define NORETURN __declspec(noreturn)
136 # define THREADLOCAL __declspec(thread)
137 # define LIKELY(x) (x)
138 # define UNLIKELY(x) (x)
139 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0
140 #else // _MSC_VER
141 # define ALWAYS_INLINE inline __attribute__((always_inline))
142 # define ALIAS(x) __attribute__((alias(x)))
143 // Please only use the ALIGNED macro before the type.
144 // Using ALIGNED after the variable declaration is not portable!
145 # define ALIGNED(x) __attribute__((aligned(x)))
146 # define FORMAT(f, a) __attribute__((format(printf, f, a)))
147 # define NOINLINE __attribute__((noinline))
148 # define NORETURN __attribute__((noreturn))
149 # define THREADLOCAL __thread
150 # define LIKELY(x) __builtin_expect(!!(x), 1)
151 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
152 # if defined(__i386__) || defined(__x86_64__)
153 // __builtin_prefetch(x) generates prefetchnt0 on x86
154 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
155 # else
156 # define PREFETCH(x) __builtin_prefetch(x)
157 # endif
158 #endif // _MSC_VER
160 #if !defined(_MSC_VER) || defined(__clang__)
161 # define UNUSED __attribute__((unused))
162 # define USED __attribute__((used))
163 #else
164 # define UNUSED
165 # define USED
166 #endif
168 #if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)
169 # define NOEXCEPT noexcept
170 #else
171 # define NOEXCEPT throw()
172 #endif
174 // Unaligned versions of basic types.
175 typedef ALIGNED(1) u16 uu16;
176 typedef ALIGNED(1) u32 uu32;
177 typedef ALIGNED(1) u64 uu64;
178 typedef ALIGNED(1) s16 us16;
179 typedef ALIGNED(1) s32 us32;
180 typedef ALIGNED(1) s64 us64;
182 #if SANITIZER_WINDOWS
183 } // namespace __sanitizer
184 typedef unsigned long DWORD; // NOLINT
185 namespace __sanitizer {
186 typedef DWORD thread_return_t;
187 # define THREAD_CALLING_CONV __stdcall
188 #else // _WIN32
189 typedef void* thread_return_t;
190 # define THREAD_CALLING_CONV
191 #endif // _WIN32
192 typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
194 // NOTE: Functions below must be defined in each run-time.
195 void NORETURN Die();
197 // FIXME: No, this shouldn't be in the sanitizer interface.
198 SANITIZER_INTERFACE_ATTRIBUTE
199 void NORETURN CheckFailed(const char *file, int line, const char *cond,
200 u64 v1, u64 v2);
202 // Check macro
203 #define RAW_CHECK_MSG(expr, msg) do { \
204 if (UNLIKELY(!(expr))) { \
205 RawWrite(msg); \
206 Die(); \
208 } while (0)
210 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
212 #define CHECK_IMPL(c1, op, c2) \
213 do { \
214 __sanitizer::u64 v1 = (u64)(c1); \
215 __sanitizer::u64 v2 = (u64)(c2); \
216 if (UNLIKELY(!(v1 op v2))) \
217 __sanitizer::CheckFailed(__FILE__, __LINE__, \
218 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
219 } while (false) \
220 /**/
222 #define CHECK(a) CHECK_IMPL((a), !=, 0)
223 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
224 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
225 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
226 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
227 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
228 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
230 #if SANITIZER_DEBUG
231 #define DCHECK(a) CHECK(a)
232 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
233 #define DCHECK_NE(a, b) CHECK_NE(a, b)
234 #define DCHECK_LT(a, b) CHECK_LT(a, b)
235 #define DCHECK_LE(a, b) CHECK_LE(a, b)
236 #define DCHECK_GT(a, b) CHECK_GT(a, b)
237 #define DCHECK_GE(a, b) CHECK_GE(a, b)
238 #else
239 #define DCHECK(a)
240 #define DCHECK_EQ(a, b)
241 #define DCHECK_NE(a, b)
242 #define DCHECK_LT(a, b)
243 #define DCHECK_LE(a, b)
244 #define DCHECK_GT(a, b)
245 #define DCHECK_GE(a, b)
246 #endif
248 #define UNREACHABLE(msg) do { \
249 CHECK(0 && msg); \
250 Die(); \
251 } while (0)
253 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
255 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
257 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
259 #define IMPL_PASTE(a, b) a##b
260 #define IMPL_COMPILER_ASSERT(pred, line) \
261 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
263 // Limits for integral types. We have to redefine it in case we don't
264 // have stdint.h (like in Visual Studio 9).
265 #undef __INT64_C
266 #undef __UINT64_C
267 #if SANITIZER_WORDSIZE == 64
268 # define __INT64_C(c) c ## L
269 # define __UINT64_C(c) c ## UL
270 #else
271 # define __INT64_C(c) c ## LL
272 # define __UINT64_C(c) c ## ULL
273 #endif // SANITIZER_WORDSIZE == 64
274 #undef INT32_MIN
275 #define INT32_MIN (-2147483647-1)
276 #undef INT32_MAX
277 #define INT32_MAX (2147483647)
278 #undef UINT32_MAX
279 #define UINT32_MAX (4294967295U)
280 #undef INT64_MIN
281 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
282 #undef INT64_MAX
283 #define INT64_MAX (__INT64_C(9223372036854775807))
284 #undef UINT64_MAX
285 #define UINT64_MAX (__UINT64_C(18446744073709551615))
287 enum LinkerInitialized { LINKER_INITIALIZED = 0 };
289 #if !defined(_MSC_VER) || defined(__clang__)
290 # if SANITIZER_S390_31
291 # define GET_CALLER_PC() \
292 (uptr)__builtin_extract_return_addr(__builtin_return_address(0))
293 # else
294 # define GET_CALLER_PC() (uptr)__builtin_return_address(0)
295 # endif
296 # define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
297 inline void Trap() {
298 __builtin_trap();
300 #else
301 extern "C" void* _ReturnAddress(void);
302 extern "C" void* _AddressOfReturnAddress(void);
303 # pragma intrinsic(_ReturnAddress)
304 # pragma intrinsic(_AddressOfReturnAddress)
305 # define GET_CALLER_PC() (uptr)_ReturnAddress()
306 // CaptureStackBackTrace doesn't need to know BP on Windows.
307 # define GET_CURRENT_FRAME() (((uptr)_AddressOfReturnAddress()) + sizeof(uptr))
309 extern "C" void __ud2(void);
310 # pragma intrinsic(__ud2)
311 inline void Trap() {
312 __ud2();
314 #endif
316 #define HANDLE_EINTR(res, f) \
318 int rverrno; \
319 do { \
320 res = (f); \
321 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
324 // Forces the compiler to generate a frame pointer in the function.
325 #define ENABLE_FRAME_POINTER \
326 do { \
327 volatile uptr enable_fp; \
328 enable_fp = GET_CURRENT_FRAME(); \
329 (void)enable_fp; \
330 } while (0)
332 } // namespace __sanitizer
334 namespace __asan { using namespace __sanitizer; } // NOLINT
335 namespace __dsan { using namespace __sanitizer; } // NOLINT
336 namespace __dfsan { using namespace __sanitizer; } // NOLINT
337 namespace __esan { using namespace __sanitizer; } // NOLINT
338 namespace __lsan { using namespace __sanitizer; } // NOLINT
339 namespace __msan { using namespace __sanitizer; } // NOLINT
340 namespace __tsan { using namespace __sanitizer; } // NOLINT
341 namespace __scudo { using namespace __sanitizer; } // NOLINT
342 namespace __ubsan { using namespace __sanitizer; } // NOLINT
343 namespace __xray { using namespace __sanitizer; } // NOLINT
344 namespace __interception { using namespace __sanitizer; } // NOLINT
347 #endif // SANITIZER_DEFS_H