Update LOCAL_PATCHES after libsanitizer merge.
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_internal_defs.h
blob8bd1bcacac107fc7cc3743d21dbb7a87c7c0f144
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 #define SANITIZER_STRINGIFY_(S) #S
21 #define SANITIZER_STRINGIFY(S) SANITIZER_STRINGIFY_(S)
23 // Only use SANITIZER_*ATTRIBUTE* before the function return type!
24 #if SANITIZER_WINDOWS
25 #if SANITIZER_IMPORT_INTERFACE
26 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllimport)
27 #else
28 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport)
29 #endif
30 # define SANITIZER_WEAK_ATTRIBUTE
31 #elif SANITIZER_GO
32 # define SANITIZER_INTERFACE_ATTRIBUTE
33 # define SANITIZER_WEAK_ATTRIBUTE
34 #else
35 # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
36 # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
37 #endif
39 // TLS is handled differently on different platforms
40 #if SANITIZER_LINUX || SANITIZER_NETBSD || \
41 SANITIZER_FREEBSD || SANITIZER_OPENBSD
42 # define SANITIZER_TLS_INITIAL_EXEC_ATTRIBUTE \
43 __attribute__((tls_model("initial-exec"))) thread_local
44 #else
45 # define SANITIZER_TLS_INITIAL_EXEC_ATTRIBUTE
46 #endif
48 //--------------------------- WEAK FUNCTIONS ---------------------------------//
49 // When working with weak functions, to simplify the code and make it more
50 // portable, when possible define a default implementation using this macro:
52 // SANITIZER_INTERFACE_WEAK_DEF(<return_type>, <name>, <parameter list>)
54 // For example:
55 // SANITIZER_INTERFACE_WEAK_DEF(bool, compare, int a, int b) { return a > b; }
57 #if SANITIZER_WINDOWS
58 #include "sanitizer_win_defs.h"
59 # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \
60 WIN_WEAK_EXPORT_DEF(ReturnType, Name, __VA_ARGS__)
61 #else
62 # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \
63 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE \
64 ReturnType Name(__VA_ARGS__)
65 #endif
67 // SANITIZER_SUPPORTS_WEAK_HOOKS means that we support real weak functions that
68 // will evaluate to a null pointer when not defined.
69 #ifndef SANITIZER_SUPPORTS_WEAK_HOOKS
70 #if (SANITIZER_LINUX || SANITIZER_SOLARIS) && !SANITIZER_GO
71 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
72 // Before Xcode 4.5, the Darwin linker doesn't reliably support undefined
73 // weak symbols. Mac OS X 10.9/Darwin 13 is the first release only supported
74 // by Xcode >= 4.5.
75 #elif SANITIZER_MAC && \
76 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1090 && !SANITIZER_GO
77 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
78 #else
79 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
80 #endif
81 #endif // SANITIZER_SUPPORTS_WEAK_HOOKS
82 // For some weak hooks that will be called very often and we want to avoid the
83 // overhead of executing the default implementation when it is not necessary,
84 // we can use the flag SANITIZER_SUPPORTS_WEAK_HOOKS to only define the default
85 // implementation for platforms that doesn't support weak symbols. For example:
87 // #if !SANITIZER_SUPPORT_WEAK_HOOKS
88 // SANITIZER_INTERFACE_WEAK_DEF(bool, compare_hook, int a, int b) {
89 // return a > b;
90 // }
91 // #endif
93 // And then use it as: if (compare_hook) compare_hook(a, b);
94 //----------------------------------------------------------------------------//
97 // We can use .preinit_array section on Linux to call sanitizer initialization
98 // functions very early in the process startup (unless PIC macro is defined).
100 // On FreeBSD, .preinit_array functions are called with rtld_bind_lock writer
101 // lock held. It will lead to dead lock if unresolved PLT functions (which helds
102 // rtld_bind_lock reader lock) are called inside .preinit_array functions.
104 // FIXME: do we have anything like this on Mac?
105 #ifndef SANITIZER_CAN_USE_PREINIT_ARRAY
106 #if ((SANITIZER_LINUX && !SANITIZER_ANDROID) || SANITIZER_OPENBSD || \
107 SANITIZER_FUCHSIA) && !defined(PIC)
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)
112 # define SANITIZER_CAN_USE_PREINIT_ARRAY 1
113 #else
114 # define SANITIZER_CAN_USE_PREINIT_ARRAY 0
115 #endif
116 #endif // SANITIZER_CAN_USE_PREINIT_ARRAY
118 // GCC does not understand __has_feature
119 #if !defined(__has_feature)
120 # define __has_feature(x) 0
121 #endif
123 // Older GCCs do not understand __has_attribute.
124 #if !defined(__has_attribute)
125 # define __has_attribute(x) 0
126 #endif
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.
131 namespace __sanitizer {
133 #if defined(_WIN64)
134 // 64-bit Windows uses LLP64 data model.
135 typedef unsigned long long uptr; // NOLINT
136 typedef signed long long sptr; // NOLINT
137 #else
138 typedef unsigned long uptr; // NOLINT
139 typedef signed long sptr; // NOLINT
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.
144 typedef unsigned long long uhwptr; // NOLINT
145 #else
146 typedef uptr uhwptr; // NOLINT
147 #endif
148 typedef unsigned char u8;
149 typedef unsigned short u16; // NOLINT
150 typedef unsigned int u32;
151 typedef unsigned long long u64; // NOLINT
152 typedef signed char s8;
153 typedef signed short s16; // NOLINT
154 typedef signed int s32;
155 typedef signed long long s64; // NOLINT
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.
159 typedef void* fd_t;
160 typedef unsigned error_t;
161 #else
162 typedef int fd_t;
163 typedef int error_t;
164 #endif
165 #if SANITIZER_SOLARIS && !defined(_LP64)
166 typedef long pid_t;
167 #else
168 typedef int pid_t;
169 #endif
171 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || \
172 SANITIZER_OPENBSD || SANITIZER_MAC || \
173 (SANITIZER_LINUX && defined(__x86_64__))
174 typedef u64 OFF_T;
175 #else
176 typedef uptr OFF_T;
177 #endif
178 typedef u64 OFF64_T;
180 #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
181 typedef uptr operator_new_size_type;
182 #else
183 # if SANITIZER_OPENBSD || defined(__s390__) && !defined(__s390x__)
184 // Special case: 31-bit s390 has unsigned long as size_t.
185 typedef unsigned long operator_new_size_type;
186 # else
187 typedef u32 operator_new_size_type;
188 # endif
189 #endif
191 typedef u64 tid_t;
193 // ----------- ATTENTION -------------
194 // This header should NOT include any other headers to avoid portability issues.
196 // Common defs.
197 #define INLINE inline
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
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
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
247 #if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)
248 # define NOEXCEPT noexcept
249 #else
250 # define NOEXCEPT throw()
251 #endif
253 // Unaligned versions of basic types.
254 typedef ALIGNED(1) u16 uu16;
255 typedef ALIGNED(1) u32 uu32;
256 typedef ALIGNED(1) u64 uu64;
257 typedef ALIGNED(1) s16 us16;
258 typedef ALIGNED(1) s32 us32;
259 typedef ALIGNED(1) s64 us64;
261 #if SANITIZER_WINDOWS
262 } // namespace __sanitizer
263 typedef unsigned long DWORD; // NOLINT
264 namespace __sanitizer {
265 typedef DWORD thread_return_t;
266 # define THREAD_CALLING_CONV __stdcall
267 #else // _WIN32
268 typedef void* thread_return_t;
269 # define THREAD_CALLING_CONV
270 #endif // _WIN32
271 typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
273 // NOTE: Functions below must be defined in each run-time.
274 void NORETURN Die();
276 void NORETURN CheckFailed(const char *file, int line, const char *cond,
277 u64 v1, u64 v2);
279 // Check macro
280 #define RAW_CHECK_MSG(expr, msg) do { \
281 if (UNLIKELY(!(expr))) { \
282 RawWrite(msg); \
283 Die(); \
285 } while (0)
287 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
289 #define CHECK_IMPL(c1, op, c2) \
290 do { \
291 __sanitizer::u64 v1 = (__sanitizer::u64)(c1); \
292 __sanitizer::u64 v2 = (__sanitizer::u64)(c2); \
293 if (UNLIKELY(!(v1 op v2))) \
294 __sanitizer::CheckFailed(__FILE__, __LINE__, \
295 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
296 } while (false) \
297 /**/
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))
307 #if SANITIZER_DEBUG
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
325 #define UNREACHABLE(msg) do { \
326 CHECK(0 && msg); \
327 Die(); \
328 } while (0)
330 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
332 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
334 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
336 #define IMPL_PASTE(a, b) a##b
337 #define IMPL_COMPILER_ASSERT(pred, line) \
338 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
340 // Limits for integral types. We have to redefine it in case we don't
341 // have stdint.h (like in Visual Studio 9).
342 #undef __INT64_C
343 #undef __UINT64_C
344 #if SANITIZER_WORDSIZE == 64
345 # define __INT64_C(c) c ## L
346 # define __UINT64_C(c) c ## UL
347 #else
348 # define __INT64_C(c) c ## LL
349 # define __UINT64_C(c) c ## ULL
350 #endif // SANITIZER_WORDSIZE == 64
351 #undef INT32_MIN
352 #define INT32_MIN (-2147483647-1)
353 #undef INT32_MAX
354 #define INT32_MAX (2147483647)
355 #undef UINT32_MAX
356 #define UINT32_MAX (4294967295U)
357 #undef INT64_MIN
358 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
359 #undef INT64_MAX
360 #define INT64_MAX (__INT64_C(9223372036854775807))
361 #undef UINT64_MAX
362 #define UINT64_MAX (__UINT64_C(18446744073709551615))
363 #undef UINTPTR_MAX
364 #if SANITIZER_WORDSIZE == 64
365 # define UINTPTR_MAX (18446744073709551615UL)
366 #else
367 # define UINTPTR_MAX (4294967295U)
368 #endif // SANITIZER_WORDSIZE == 64
370 enum LinkerInitialized { LINKER_INITIALIZED = 0 };
372 #if !defined(_MSC_VER) || defined(__clang__)
373 #if SANITIZER_S390_31
374 #define GET_CALLER_PC() \
375 (__sanitizer::uptr) __builtin_extract_return_addr(__builtin_return_address(0))
376 #else
377 #define GET_CALLER_PC() (__sanitizer::uptr) __builtin_return_address(0)
378 #endif
379 #define GET_CURRENT_FRAME() (__sanitizer::uptr) __builtin_frame_address(0)
380 inline void Trap() {
381 __builtin_trap();
383 #else
384 extern "C" void* _ReturnAddress(void);
385 extern "C" void* _AddressOfReturnAddress(void);
386 # pragma intrinsic(_ReturnAddress)
387 # pragma intrinsic(_AddressOfReturnAddress)
388 #define GET_CALLER_PC() (__sanitizer::uptr) _ReturnAddress()
389 // CaptureStackBackTrace doesn't need to know BP on Windows.
390 #define GET_CURRENT_FRAME() \
391 (((__sanitizer::uptr)_AddressOfReturnAddress()) + sizeof(__sanitizer::uptr))
393 extern "C" void __ud2(void);
394 # pragma intrinsic(__ud2)
395 inline void Trap() {
396 __ud2();
398 #endif
400 #define HANDLE_EINTR(res, f) \
402 int rverrno; \
403 do { \
404 res = (f); \
405 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
408 // Forces the compiler to generate a frame pointer in the function.
409 #define ENABLE_FRAME_POINTER \
410 do { \
411 volatile __sanitizer::uptr enable_fp; \
412 enable_fp = GET_CURRENT_FRAME(); \
413 (void)enable_fp; \
414 } while (0)
416 } // namespace __sanitizer
418 namespace __asan { using namespace __sanitizer; } // NOLINT
419 namespace __dsan { using namespace __sanitizer; } // NOLINT
420 namespace __dfsan { using namespace __sanitizer; } // NOLINT
421 namespace __esan { using namespace __sanitizer; } // NOLINT
422 namespace __lsan { using namespace __sanitizer; } // NOLINT
423 namespace __msan { using namespace __sanitizer; } // NOLINT
424 namespace __hwasan { using namespace __sanitizer; } // NOLINT
425 namespace __tsan { using namespace __sanitizer; } // NOLINT
426 namespace __scudo { using namespace __sanitizer; } // NOLINT
427 namespace __ubsan { using namespace __sanitizer; } // NOLINT
428 namespace __xray { using namespace __sanitizer; } // NOLINT
429 namespace __interception { using namespace __sanitizer; } // NOLINT
430 namespace __hwasan { using namespace __sanitizer; } // NOLINT
433 #endif // SANITIZER_DEFS_H