* config/mn10300/mn10300.md (adddi3_degenerate): Remove bogus
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_internal_defs.h
blob26bea8bed91f98734d3758ce1b68263a2556af8c
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_SOLARIS && (defined(_LP64) || _FILE_OFFSET_BITS == 64)) || \
174 (SANITIZER_LINUX && defined(__x86_64__))
175 typedef u64 OFF_T;
176 #else
177 typedef uptr OFF_T;
178 #endif
179 typedef u64 OFF64_T;
181 #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
182 typedef uptr operator_new_size_type;
183 #else
184 # if SANITIZER_OPENBSD || defined(__s390__) && !defined(__s390x__)
185 // Special case: 31-bit s390 has unsigned long as size_t.
186 typedef unsigned long operator_new_size_type;
187 # else
188 typedef u32 operator_new_size_type;
189 # endif
190 #endif
192 typedef u64 tid_t;
194 // ----------- ATTENTION -------------
195 // This header should NOT include any other headers to avoid portability issues.
197 // Common defs.
198 #define INLINE inline
199 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
200 #define SANITIZER_WEAK_DEFAULT_IMPL \
201 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
202 #define SANITIZER_WEAK_CXX_DEFAULT_IMPL \
203 extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
205 // Platform-specific defs.
206 #if defined(_MSC_VER)
207 # define ALWAYS_INLINE __forceinline
208 // FIXME(timurrrr): do we need this on Windows?
209 # define ALIAS(x)
210 # define ALIGNED(x) __declspec(align(x))
211 # define FORMAT(f, a)
212 # define NOINLINE __declspec(noinline)
213 # define NORETURN __declspec(noreturn)
214 # define THREADLOCAL __declspec(thread)
215 # define LIKELY(x) (x)
216 # define UNLIKELY(x) (x)
217 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0
218 # define WARN_UNUSED_RESULT
219 #else // _MSC_VER
220 # define ALWAYS_INLINE inline __attribute__((always_inline))
221 # define ALIAS(x) __attribute__((alias(x)))
222 // Please only use the ALIGNED macro before the type.
223 // Using ALIGNED after the variable declaration is not portable!
224 # define ALIGNED(x) __attribute__((aligned(x)))
225 # define FORMAT(f, a) __attribute__((format(printf, f, a)))
226 # define NOINLINE __attribute__((noinline))
227 # define NORETURN __attribute__((noreturn))
228 # define THREADLOCAL __thread
229 # define LIKELY(x) __builtin_expect(!!(x), 1)
230 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
231 # if defined(__i386__) || defined(__x86_64__)
232 // __builtin_prefetch(x) generates prefetchnt0 on x86
233 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
234 # else
235 # define PREFETCH(x) __builtin_prefetch(x)
236 # endif
237 # define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
238 #endif // _MSC_VER
240 #if !defined(_MSC_VER) || defined(__clang__)
241 # define UNUSED __attribute__((unused))
242 # define USED __attribute__((used))
243 #else
244 # define UNUSED
245 # define USED
246 #endif
248 #if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)
249 # define NOEXCEPT noexcept
250 #else
251 # define NOEXCEPT throw()
252 #endif
254 // Unaligned versions of basic types.
255 typedef ALIGNED(1) u16 uu16;
256 typedef ALIGNED(1) u32 uu32;
257 typedef ALIGNED(1) u64 uu64;
258 typedef ALIGNED(1) s16 us16;
259 typedef ALIGNED(1) s32 us32;
260 typedef ALIGNED(1) s64 us64;
262 #if SANITIZER_WINDOWS
263 } // namespace __sanitizer
264 typedef unsigned long DWORD; // NOLINT
265 namespace __sanitizer {
266 typedef DWORD thread_return_t;
267 # define THREAD_CALLING_CONV __stdcall
268 #else // _WIN32
269 typedef void* thread_return_t;
270 # define THREAD_CALLING_CONV
271 #endif // _WIN32
272 typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
274 // NOTE: Functions below must be defined in each run-time.
275 void NORETURN Die();
277 void NORETURN CheckFailed(const char *file, int line, const char *cond,
278 u64 v1, u64 v2);
280 // Check macro
281 #define RAW_CHECK_MSG(expr, msg) do { \
282 if (UNLIKELY(!(expr))) { \
283 RawWrite(msg); \
284 Die(); \
286 } while (0)
288 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
290 #define CHECK_IMPL(c1, op, c2) \
291 do { \
292 __sanitizer::u64 v1 = (__sanitizer::u64)(c1); \
293 __sanitizer::u64 v2 = (__sanitizer::u64)(c2); \
294 if (UNLIKELY(!(v1 op v2))) \
295 __sanitizer::CheckFailed(__FILE__, __LINE__, \
296 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
297 } while (false) \
298 /**/
300 #define CHECK(a) CHECK_IMPL((a), !=, 0)
301 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
302 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
303 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
304 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
305 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
306 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
308 #if SANITIZER_DEBUG
309 #define DCHECK(a) CHECK(a)
310 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
311 #define DCHECK_NE(a, b) CHECK_NE(a, b)
312 #define DCHECK_LT(a, b) CHECK_LT(a, b)
313 #define DCHECK_LE(a, b) CHECK_LE(a, b)
314 #define DCHECK_GT(a, b) CHECK_GT(a, b)
315 #define DCHECK_GE(a, b) CHECK_GE(a, b)
316 #else
317 #define DCHECK(a)
318 #define DCHECK_EQ(a, b)
319 #define DCHECK_NE(a, b)
320 #define DCHECK_LT(a, b)
321 #define DCHECK_LE(a, b)
322 #define DCHECK_GT(a, b)
323 #define DCHECK_GE(a, b)
324 #endif
326 #define UNREACHABLE(msg) do { \
327 CHECK(0 && msg); \
328 Die(); \
329 } while (0)
331 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
333 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
335 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
337 #define IMPL_PASTE(a, b) a##b
338 #define IMPL_COMPILER_ASSERT(pred, line) \
339 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
341 // Limits for integral types. We have to redefine it in case we don't
342 // have stdint.h (like in Visual Studio 9).
343 #undef __INT64_C
344 #undef __UINT64_C
345 #if SANITIZER_WORDSIZE == 64
346 # define __INT64_C(c) c ## L
347 # define __UINT64_C(c) c ## UL
348 #else
349 # define __INT64_C(c) c ## LL
350 # define __UINT64_C(c) c ## ULL
351 #endif // SANITIZER_WORDSIZE == 64
352 #undef INT32_MIN
353 #define INT32_MIN (-2147483647-1)
354 #undef INT32_MAX
355 #define INT32_MAX (2147483647)
356 #undef UINT32_MAX
357 #define UINT32_MAX (4294967295U)
358 #undef INT64_MIN
359 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
360 #undef INT64_MAX
361 #define INT64_MAX (__INT64_C(9223372036854775807))
362 #undef UINT64_MAX
363 #define UINT64_MAX (__UINT64_C(18446744073709551615))
364 #undef UINTPTR_MAX
365 #if SANITIZER_WORDSIZE == 64
366 # define UINTPTR_MAX (18446744073709551615UL)
367 #else
368 # define UINTPTR_MAX (4294967295U)
369 #endif // SANITIZER_WORDSIZE == 64
371 enum LinkerInitialized { LINKER_INITIALIZED = 0 };
373 #if !defined(_MSC_VER) || defined(__clang__)
374 #if SANITIZER_S390_31
375 #define GET_CALLER_PC() \
376 (__sanitizer::uptr) __builtin_extract_return_addr(__builtin_return_address(0))
377 #else
378 #define GET_CALLER_PC() (__sanitizer::uptr) __builtin_return_address(0)
379 #endif
380 #define GET_CURRENT_FRAME() (__sanitizer::uptr) __builtin_frame_address(0)
381 inline void Trap() {
382 __builtin_trap();
384 #else
385 extern "C" void* _ReturnAddress(void);
386 extern "C" void* _AddressOfReturnAddress(void);
387 # pragma intrinsic(_ReturnAddress)
388 # pragma intrinsic(_AddressOfReturnAddress)
389 #define GET_CALLER_PC() (__sanitizer::uptr) _ReturnAddress()
390 // CaptureStackBackTrace doesn't need to know BP on Windows.
391 #define GET_CURRENT_FRAME() \
392 (((__sanitizer::uptr)_AddressOfReturnAddress()) + sizeof(__sanitizer::uptr))
394 extern "C" void __ud2(void);
395 # pragma intrinsic(__ud2)
396 inline void Trap() {
397 __ud2();
399 #endif
401 #define HANDLE_EINTR(res, f) \
403 int rverrno; \
404 do { \
405 res = (f); \
406 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
409 // Forces the compiler to generate a frame pointer in the function.
410 #define ENABLE_FRAME_POINTER \
411 do { \
412 volatile __sanitizer::uptr enable_fp; \
413 enable_fp = GET_CURRENT_FRAME(); \
414 (void)enable_fp; \
415 } while (0)
417 } // namespace __sanitizer
419 namespace __asan { using namespace __sanitizer; } // NOLINT
420 namespace __dsan { using namespace __sanitizer; } // NOLINT
421 namespace __dfsan { using namespace __sanitizer; } // NOLINT
422 namespace __esan { using namespace __sanitizer; } // NOLINT
423 namespace __lsan { using namespace __sanitizer; } // NOLINT
424 namespace __msan { using namespace __sanitizer; } // NOLINT
425 namespace __hwasan { using namespace __sanitizer; } // NOLINT
426 namespace __tsan { using namespace __sanitizer; } // NOLINT
427 namespace __scudo { using namespace __sanitizer; } // NOLINT
428 namespace __ubsan { using namespace __sanitizer; } // NOLINT
429 namespace __xray { using namespace __sanitizer; } // NOLINT
430 namespace __interception { using namespace __sanitizer; } // NOLINT
431 namespace __hwasan { using namespace __sanitizer; } // NOLINT
434 #endif // SANITIZER_DEFS_H