Remove assert in get_def_bb_for_const
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_internal_defs.h
blobd76ed7570a7e732fab579925af0d06dc19501f15
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 defined(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) && !defined(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
91 // WARNING: OFF_T may be different from OS type off_t, depending on the value of
92 // _FILE_OFFSET_BITS. This definition of OFF_T matches the ABI of system calls
93 // like pread and mmap, as opposed to pread64 and mmap64.
94 // FreeBSD, Mac and Linux/x86-64 are special.
95 #if SANITIZER_FREEBSD || SANITIZER_MAC || \
96 (SANITIZER_LINUX && defined(__x86_64__))
97 typedef u64 OFF_T;
98 #else
99 typedef uptr OFF_T;
100 #endif
101 typedef u64 OFF64_T;
103 #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
104 typedef uptr operator_new_size_type;
105 #else
106 typedef u32 operator_new_size_type;
107 #endif
108 } // namespace __sanitizer
111 using namespace __sanitizer; // NOLINT
112 // ----------- ATTENTION -------------
113 // This header should NOT include any other headers to avoid portability issues.
115 // Common defs.
116 #define INLINE inline
117 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
118 #define WEAK SANITIZER_WEAK_ATTRIBUTE
120 // Platform-specific defs.
121 #if defined(_MSC_VER)
122 # define ALWAYS_INLINE __forceinline
123 // FIXME(timurrrr): do we need this on Windows?
124 # define ALIAS(x)
125 # define ALIGNED(x) __declspec(align(x))
126 # define FORMAT(f, a)
127 # define NOINLINE __declspec(noinline)
128 # define NORETURN __declspec(noreturn)
129 # define THREADLOCAL __declspec(thread)
130 # define LIKELY(x) (x)
131 # define UNLIKELY(x) (x)
132 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */
133 #else // _MSC_VER
134 # define ALWAYS_INLINE inline __attribute__((always_inline))
135 # define ALIAS(x) __attribute__((alias(x)))
136 // Please only use the ALIGNED macro before the type.
137 // Using ALIGNED after the variable declaration is not portable!
138 # define ALIGNED(x) __attribute__((aligned(x)))
139 # define FORMAT(f, a) __attribute__((format(printf, f, a)))
140 # define NOINLINE __attribute__((noinline))
141 # define NORETURN __attribute__((noreturn))
142 # define THREADLOCAL __thread
143 # define LIKELY(x) __builtin_expect(!!(x), 1)
144 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
145 # if defined(__i386__) || defined(__x86_64__)
146 // __builtin_prefetch(x) generates prefetchnt0 on x86
147 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
148 # else
149 # define PREFETCH(x) __builtin_prefetch(x)
150 # endif
151 #endif // _MSC_VER
153 #if !defined(_MSC_VER) || defined(__clang__)
154 # define UNUSED __attribute__((unused))
155 # define USED __attribute__((used))
156 #else
157 # define UNUSED
158 # define USED
159 #endif
161 #if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)
162 # define NOEXCEPT noexcept
163 #else
164 # define NOEXCEPT throw()
165 #endif
167 // Unaligned versions of basic types.
168 typedef ALIGNED(1) u16 uu16;
169 typedef ALIGNED(1) u32 uu32;
170 typedef ALIGNED(1) u64 uu64;
171 typedef ALIGNED(1) s16 us16;
172 typedef ALIGNED(1) s32 us32;
173 typedef ALIGNED(1) s64 us64;
175 #if SANITIZER_WINDOWS
176 typedef unsigned long DWORD; // NOLINT
177 typedef DWORD thread_return_t;
178 # define THREAD_CALLING_CONV __stdcall
179 #else // _WIN32
180 typedef void* thread_return_t;
181 # define THREAD_CALLING_CONV
182 #endif // _WIN32
183 typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
185 // NOTE: Functions below must be defined in each run-time.
186 namespace __sanitizer {
187 void NORETURN Die();
189 // FIXME: No, this shouldn't be in the sanitizer interface.
190 SANITIZER_INTERFACE_ATTRIBUTE
191 void NORETURN CheckFailed(const char *file, int line, const char *cond,
192 u64 v1, u64 v2);
193 } // namespace __sanitizer
195 // Check macro
196 #define RAW_CHECK_MSG(expr, msg) do { \
197 if (UNLIKELY(!(expr))) { \
198 RawWrite(msg); \
199 Die(); \
201 } while (0)
203 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
205 #define CHECK_IMPL(c1, op, c2) \
206 do { \
207 __sanitizer::u64 v1 = (u64)(c1); \
208 __sanitizer::u64 v2 = (u64)(c2); \
209 if (UNLIKELY(!(v1 op v2))) \
210 __sanitizer::CheckFailed(__FILE__, __LINE__, \
211 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
212 } while (false) \
213 /**/
215 #define CHECK(a) CHECK_IMPL((a), !=, 0)
216 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
217 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
218 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
219 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
220 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
221 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
223 #if SANITIZER_DEBUG
224 #define DCHECK(a) CHECK(a)
225 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
226 #define DCHECK_NE(a, b) CHECK_NE(a, b)
227 #define DCHECK_LT(a, b) CHECK_LT(a, b)
228 #define DCHECK_LE(a, b) CHECK_LE(a, b)
229 #define DCHECK_GT(a, b) CHECK_GT(a, b)
230 #define DCHECK_GE(a, b) CHECK_GE(a, b)
231 #else
232 #define DCHECK(a)
233 #define DCHECK_EQ(a, b)
234 #define DCHECK_NE(a, b)
235 #define DCHECK_LT(a, b)
236 #define DCHECK_LE(a, b)
237 #define DCHECK_GT(a, b)
238 #define DCHECK_GE(a, b)
239 #endif
241 #define UNREACHABLE(msg) do { \
242 CHECK(0 && msg); \
243 Die(); \
244 } while (0)
246 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
248 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
250 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
252 #define IMPL_PASTE(a, b) a##b
253 #define IMPL_COMPILER_ASSERT(pred, line) \
254 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
256 // Limits for integral types. We have to redefine it in case we don't
257 // have stdint.h (like in Visual Studio 9).
258 #undef __INT64_C
259 #undef __UINT64_C
260 #if SANITIZER_WORDSIZE == 64
261 # define __INT64_C(c) c ## L
262 # define __UINT64_C(c) c ## UL
263 #else
264 # define __INT64_C(c) c ## LL
265 # define __UINT64_C(c) c ## ULL
266 #endif // SANITIZER_WORDSIZE == 64
267 #undef INT32_MIN
268 #define INT32_MIN (-2147483647-1)
269 #undef INT32_MAX
270 #define INT32_MAX (2147483647)
271 #undef UINT32_MAX
272 #define UINT32_MAX (4294967295U)
273 #undef INT64_MIN
274 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
275 #undef INT64_MAX
276 #define INT64_MAX (__INT64_C(9223372036854775807))
277 #undef UINT64_MAX
278 #define UINT64_MAX (__UINT64_C(18446744073709551615))
280 enum LinkerInitialized { LINKER_INITIALIZED = 0 };
282 #if !defined(_MSC_VER) || defined(__clang__)
283 # define GET_CALLER_PC() (uptr)__builtin_return_address(0)
284 # define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
285 #else
286 extern "C" void* _ReturnAddress(void);
287 # pragma intrinsic(_ReturnAddress)
288 # define GET_CALLER_PC() (uptr)_ReturnAddress()
289 // CaptureStackBackTrace doesn't need to know BP on Windows.
290 // FIXME: This macro is still used when printing error reports though it's not
291 // clear if the BP value is needed in the ASan reports on Windows.
292 # define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
293 #endif
295 #define HANDLE_EINTR(res, f) \
297 int rverrno; \
298 do { \
299 res = (f); \
300 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
303 // Forces the compiler to generate a frame pointer in the function.
304 #define ENABLE_FRAME_POINTER \
305 do { \
306 volatile uptr enable_fp; \
307 enable_fp = GET_CURRENT_FRAME(); \
308 (void)enable_fp; \
309 } while (0)
311 #endif // SANITIZER_DEFS_H