* config/i386/i386.c (ix86_expand_prologue): Tighten assert
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_internal_defs.h
blobef405dec04854d551fcc26c172c60ea16b418949
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 #if SANITIZER_IMPORT_INTERFACE
23 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllimport)
24 #else
25 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport)
26 #endif
27 # define SANITIZER_WEAK_ATTRIBUTE
28 #elif SANITIZER_GO
29 # define SANITIZER_INTERFACE_ATTRIBUTE
30 # define SANITIZER_WEAK_ATTRIBUTE
31 #else
32 # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
33 # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
34 #endif
36 // TLS is handled differently on different platforms
37 #if SANITIZER_LINUX
38 # define SANITIZER_TLS_INITIAL_EXEC_ATTRIBUTE \
39 __attribute__((tls_model("initial-exec"))) thread_local
40 #else
41 # define SANITIZER_TLS_INITIAL_EXEC_ATTRIBUTE
42 #endif
44 //--------------------------- WEAK FUNCTIONS ---------------------------------//
45 // When working with weak functions, to simplify the code and make it more
46 // portable, when possible define a default implementation using this macro:
48 // SANITIZER_INTERFACE_WEAK_DEF(<return_type>, <name>, <parameter list>)
50 // For example:
51 // SANITIZER_INTERFACE_WEAK_DEF(bool, compare, int a, int b) { return a > b; }
53 #if SANITIZER_WINDOWS
54 #include "sanitizer_win_defs.h"
55 # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \
56 WIN_WEAK_EXPORT_DEF(ReturnType, Name, __VA_ARGS__)
57 #else
58 # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \
59 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE \
60 ReturnType Name(__VA_ARGS__)
61 #endif
63 // SANITIZER_SUPPORTS_WEAK_HOOKS means that we support real weak functions that
64 // will evaluate to a null pointer when not defined.
65 #ifndef SANITIZER_SUPPORTS_WEAK_HOOKS
66 #if (SANITIZER_LINUX || SANITIZER_MAC) && !SANITIZER_GO
67 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
68 #else
69 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
70 #endif
71 #endif // SANITIZER_SUPPORTS_WEAK_HOOKS
72 // For some weak hooks that will be called very often and we want to avoid the
73 // overhead of executing the default implementation when it is not necessary,
74 // we can use the flag SANITIZER_SUPPORTS_WEAK_HOOKS to only define the default
75 // implementation for platforms that doesn't support weak symbols. For example:
77 // #if !SANITIZER_SUPPORT_WEAK_HOOKS
78 // SANITIZER_INTERFACE_WEAK_DEF(bool, compare_hook, int a, int b) {
79 // return a > b;
80 // }
81 // #endif
83 // And then use it as: if (compare_hook) compare_hook(a, b);
84 //----------------------------------------------------------------------------//
87 // We can use .preinit_array section on Linux to call sanitizer initialization
88 // functions very early in the process startup (unless PIC macro is defined).
89 // FIXME: do we have anything like this on Mac?
90 #if SANITIZER_LINUX && !SANITIZER_ANDROID && !defined(PIC)
91 # define SANITIZER_CAN_USE_PREINIT_ARRAY 1
92 #else
93 # define SANITIZER_CAN_USE_PREINIT_ARRAY 0
94 #endif
96 // GCC does not understand __has_feature
97 #if !defined(__has_feature)
98 # define __has_feature(x) 0
99 #endif
101 // For portability reasons we do not include stddef.h, stdint.h or any other
102 // system header, but we do need some basic types that are not defined
103 // in a portable way by the language itself.
104 namespace __sanitizer {
106 #if defined(_WIN64)
107 // 64-bit Windows uses LLP64 data model.
108 typedef unsigned long long uptr; // NOLINT
109 typedef signed long long sptr; // NOLINT
110 #else
111 typedef unsigned long uptr; // NOLINT
112 typedef signed long sptr; // NOLINT
113 #endif // defined(_WIN64)
114 #if defined(__x86_64__)
115 // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
116 // 64-bit pointer to unwind stack frame.
117 typedef unsigned long long uhwptr; // NOLINT
118 #else
119 typedef uptr uhwptr; // NOLINT
120 #endif
121 typedef unsigned char u8;
122 typedef unsigned short u16; // NOLINT
123 typedef unsigned int u32;
124 typedef unsigned long long u64; // NOLINT
125 typedef signed char s8;
126 typedef signed short s16; // NOLINT
127 typedef signed int s32;
128 typedef signed long long s64; // NOLINT
129 #if SANITIZER_WINDOWS
130 // On Windows, files are HANDLE, which is a synonim of void*.
131 // Use void* to avoid including <windows.h> everywhere.
132 typedef void* fd_t;
133 typedef unsigned error_t;
134 #else
135 typedef int fd_t;
136 typedef int error_t;
137 #endif
138 typedef int pid_t;
140 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_MAC || \
141 (SANITIZER_LINUX && defined(__x86_64__))
142 typedef u64 OFF_T;
143 #else
144 typedef uptr OFF_T;
145 #endif
146 typedef u64 OFF64_T;
148 #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
149 typedef uptr operator_new_size_type;
150 #else
151 # if defined(__s390__) && !defined(__s390x__)
152 // Special case: 31-bit s390 has unsigned long as size_t.
153 typedef unsigned long operator_new_size_type;
154 # else
155 typedef u32 operator_new_size_type;
156 # endif
157 #endif
159 #if SANITIZER_MAC
160 // On Darwin, thread IDs are 64-bit even on 32-bit systems.
161 typedef u64 tid_t;
162 #else
163 typedef uptr tid_t;
164 #endif
166 // ----------- ATTENTION -------------
167 // This header should NOT include any other headers to avoid portability issues.
169 // Common defs.
170 #define INLINE inline
171 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
172 #define SANITIZER_WEAK_DEFAULT_IMPL \
173 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
174 #define SANITIZER_WEAK_CXX_DEFAULT_IMPL \
175 extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
177 // Platform-specific defs.
178 #if defined(_MSC_VER)
179 # define ALWAYS_INLINE __forceinline
180 // FIXME(timurrrr): do we need this on Windows?
181 # define ALIAS(x)
182 # define ALIGNED(x) __declspec(align(x))
183 # define FORMAT(f, a)
184 # define NOINLINE __declspec(noinline)
185 # define NORETURN __declspec(noreturn)
186 # define THREADLOCAL __declspec(thread)
187 # define LIKELY(x) (x)
188 # define UNLIKELY(x) (x)
189 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0
190 #else // _MSC_VER
191 # define ALWAYS_INLINE inline __attribute__((always_inline))
192 # define ALIAS(x) __attribute__((alias(x)))
193 // Please only use the ALIGNED macro before the type.
194 // Using ALIGNED after the variable declaration is not portable!
195 # define ALIGNED(x) __attribute__((aligned(x)))
196 # define FORMAT(f, a) __attribute__((format(printf, f, a)))
197 # define NOINLINE __attribute__((noinline))
198 # define NORETURN __attribute__((noreturn))
199 # define THREADLOCAL __thread
200 # define LIKELY(x) __builtin_expect(!!(x), 1)
201 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
202 # if defined(__i386__) || defined(__x86_64__)
203 // __builtin_prefetch(x) generates prefetchnt0 on x86
204 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
205 # else
206 # define PREFETCH(x) __builtin_prefetch(x)
207 # endif
208 #endif // _MSC_VER
210 #if !defined(_MSC_VER) || defined(__clang__)
211 # define UNUSED __attribute__((unused))
212 # define USED __attribute__((used))
213 #else
214 # define UNUSED
215 # define USED
216 #endif
218 #if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)
219 # define NOEXCEPT noexcept
220 #else
221 # define NOEXCEPT throw()
222 #endif
224 // Unaligned versions of basic types.
225 typedef ALIGNED(1) u16 uu16;
226 typedef ALIGNED(1) u32 uu32;
227 typedef ALIGNED(1) u64 uu64;
228 typedef ALIGNED(1) s16 us16;
229 typedef ALIGNED(1) s32 us32;
230 typedef ALIGNED(1) s64 us64;
232 #if SANITIZER_WINDOWS
233 } // namespace __sanitizer
234 typedef unsigned long DWORD; // NOLINT
235 namespace __sanitizer {
236 typedef DWORD thread_return_t;
237 # define THREAD_CALLING_CONV __stdcall
238 #else // _WIN32
239 typedef void* thread_return_t;
240 # define THREAD_CALLING_CONV
241 #endif // _WIN32
242 typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
244 // NOTE: Functions below must be defined in each run-time.
245 void NORETURN Die();
247 // FIXME: No, this shouldn't be in the sanitizer interface.
248 SANITIZER_INTERFACE_ATTRIBUTE
249 void NORETURN CheckFailed(const char *file, int line, const char *cond,
250 u64 v1, u64 v2);
252 // Check macro
253 #define RAW_CHECK_MSG(expr, msg) do { \
254 if (UNLIKELY(!(expr))) { \
255 RawWrite(msg); \
256 Die(); \
258 } while (0)
260 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
262 #define CHECK_IMPL(c1, op, c2) \
263 do { \
264 __sanitizer::u64 v1 = (__sanitizer::u64)(c1); \
265 __sanitizer::u64 v2 = (__sanitizer::u64)(c2); \
266 if (UNLIKELY(!(v1 op v2))) \
267 __sanitizer::CheckFailed(__FILE__, __LINE__, \
268 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
269 } while (false) \
270 /**/
272 #define CHECK(a) CHECK_IMPL((a), !=, 0)
273 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
274 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
275 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
276 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
277 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
278 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
280 #if SANITIZER_DEBUG
281 #define DCHECK(a) CHECK(a)
282 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
283 #define DCHECK_NE(a, b) CHECK_NE(a, b)
284 #define DCHECK_LT(a, b) CHECK_LT(a, b)
285 #define DCHECK_LE(a, b) CHECK_LE(a, b)
286 #define DCHECK_GT(a, b) CHECK_GT(a, b)
287 #define DCHECK_GE(a, b) CHECK_GE(a, b)
288 #else
289 #define DCHECK(a)
290 #define DCHECK_EQ(a, b)
291 #define DCHECK_NE(a, b)
292 #define DCHECK_LT(a, b)
293 #define DCHECK_LE(a, b)
294 #define DCHECK_GT(a, b)
295 #define DCHECK_GE(a, b)
296 #endif
298 #define UNREACHABLE(msg) do { \
299 CHECK(0 && msg); \
300 Die(); \
301 } while (0)
303 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
305 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
307 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
309 #define IMPL_PASTE(a, b) a##b
310 #define IMPL_COMPILER_ASSERT(pred, line) \
311 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
313 // Limits for integral types. We have to redefine it in case we don't
314 // have stdint.h (like in Visual Studio 9).
315 #undef __INT64_C
316 #undef __UINT64_C
317 #if SANITIZER_WORDSIZE == 64
318 # define __INT64_C(c) c ## L
319 # define __UINT64_C(c) c ## UL
320 #else
321 # define __INT64_C(c) c ## LL
322 # define __UINT64_C(c) c ## ULL
323 #endif // SANITIZER_WORDSIZE == 64
324 #undef INT32_MIN
325 #define INT32_MIN (-2147483647-1)
326 #undef INT32_MAX
327 #define INT32_MAX (2147483647)
328 #undef UINT32_MAX
329 #define UINT32_MAX (4294967295U)
330 #undef INT64_MIN
331 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
332 #undef INT64_MAX
333 #define INT64_MAX (__INT64_C(9223372036854775807))
334 #undef UINT64_MAX
335 #define UINT64_MAX (__UINT64_C(18446744073709551615))
337 enum LinkerInitialized { LINKER_INITIALIZED = 0 };
339 #if !defined(_MSC_VER) || defined(__clang__)
340 #if SANITIZER_S390_31
341 #define GET_CALLER_PC() \
342 (__sanitizer::uptr) __builtin_extract_return_addr(__builtin_return_address(0))
343 #else
344 #define GET_CALLER_PC() (__sanitizer::uptr) __builtin_return_address(0)
345 #endif
346 #define GET_CURRENT_FRAME() (__sanitizer::uptr) __builtin_frame_address(0)
347 inline void Trap() {
348 __builtin_trap();
350 #else
351 extern "C" void* _ReturnAddress(void);
352 extern "C" void* _AddressOfReturnAddress(void);
353 # pragma intrinsic(_ReturnAddress)
354 # pragma intrinsic(_AddressOfReturnAddress)
355 #define GET_CALLER_PC() (__sanitizer::uptr) _ReturnAddress()
356 // CaptureStackBackTrace doesn't need to know BP on Windows.
357 #define GET_CURRENT_FRAME() \
358 (((__sanitizer::uptr)_AddressOfReturnAddress()) + sizeof(__sanitizer::uptr))
360 extern "C" void __ud2(void);
361 # pragma intrinsic(__ud2)
362 inline void Trap() {
363 __ud2();
365 #endif
367 #define HANDLE_EINTR(res, f) \
369 int rverrno; \
370 do { \
371 res = (f); \
372 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
375 // Forces the compiler to generate a frame pointer in the function.
376 #define ENABLE_FRAME_POINTER \
377 do { \
378 volatile __sanitizer::uptr enable_fp; \
379 enable_fp = GET_CURRENT_FRAME(); \
380 (void)enable_fp; \
381 } while (0)
383 } // namespace __sanitizer
385 namespace __asan { using namespace __sanitizer; } // NOLINT
386 namespace __dsan { using namespace __sanitizer; } // NOLINT
387 namespace __dfsan { using namespace __sanitizer; } // NOLINT
388 namespace __esan { using namespace __sanitizer; } // NOLINT
389 namespace __lsan { using namespace __sanitizer; } // NOLINT
390 namespace __msan { using namespace __sanitizer; } // NOLINT
391 namespace __tsan { using namespace __sanitizer; } // NOLINT
392 namespace __scudo { using namespace __sanitizer; } // NOLINT
393 namespace __ubsan { using namespace __sanitizer; } // NOLINT
394 namespace __xray { using namespace __sanitizer; } // NOLINT
395 namespace __interception { using namespace __sanitizer; } // NOLINT
398 #endif // SANITIZER_DEFS_H