regex_constants.h: Add underlying `unsigned int` for enum syntax_option_type.
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_internal_defs.h
blob577c9a9c17fcbbe9e84365a3572eac7899b69088
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 #if defined(_WIN32)
15 // FIXME find out what we need on Windows. __declspec(dllexport) ?
16 # define SANITIZER_INTERFACE_ATTRIBUTE
17 # define SANITIZER_WEAK_ATTRIBUTE
18 #elif defined(SANITIZER_GO)
19 # define SANITIZER_INTERFACE_ATTRIBUTE
20 # define SANITIZER_WEAK_ATTRIBUTE
21 #else
22 # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
23 # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
24 #endif
26 #ifdef __linux__
27 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
28 #else
29 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
30 #endif
32 // GCC does not understand __has_feature
33 #if !defined(__has_feature)
34 # define __has_feature(x) 0
35 #endif
37 // For portability reasons we do not include stddef.h, stdint.h or any other
38 // system header, but we do need some basic types that are not defined
39 // in a portable way by the language itself.
40 namespace __sanitizer {
42 #if defined(_WIN64)
43 // 64-bit Windows uses LLP64 data model.
44 typedef unsigned long long uptr; // NOLINT
45 typedef signed long long sptr; // NOLINT
46 #else
47 typedef unsigned long uptr; // NOLINT
48 typedef signed long sptr; // NOLINT
49 #endif // defined(_WIN64)
50 #if defined(__x86_64__)
51 // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
52 // 64-bit pointer to unwind stack frame.
53 typedef unsigned long long uhwptr; // NOLINT
54 #else
55 typedef uptr uhwptr; // NOLINT
56 #endif
57 typedef unsigned char u8;
58 typedef unsigned short u16; // NOLINT
59 typedef unsigned int u32;
60 typedef unsigned long long u64; // NOLINT
61 typedef signed char s8;
62 typedef signed short s16; // NOLINT
63 typedef signed int s32;
64 typedef signed long long s64; // NOLINT
65 typedef int fd_t;
67 } // namespace __sanitizer
69 extern "C" {
70 // Tell the tools to write their reports to "path.<pid>" instead of stderr.
71 void __sanitizer_set_report_path(const char *path)
72 SANITIZER_INTERFACE_ATTRIBUTE;
74 // Tell the tools to write their reports to given file descriptor instead of
75 // stderr.
76 void __sanitizer_set_report_fd(int fd)
77 SANITIZER_INTERFACE_ATTRIBUTE;
79 // Notify the tools that the sandbox is going to be turned on. The reserved
80 // parameter will be used in the future to hold a structure with functions
81 // that the tools may call to bypass the sandbox.
82 void __sanitizer_sandbox_on_notify(void *reserved)
83 SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE;
85 // This function is called by the tool when it has just finished reporting
86 // an error. 'error_summary' is a one-line string that summarizes
87 // the error message. This function can be overridden by the client.
88 void __sanitizer_report_error_summary(const char *error_summary)
89 SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE;
90 } // extern "C"
93 using namespace __sanitizer; // NOLINT
94 // ----------- ATTENTION -------------
95 // This header should NOT include any other headers to avoid portability issues.
97 // Common defs.
98 #define INLINE static inline
99 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
100 #define WEAK SANITIZER_WEAK_ATTRIBUTE
102 // Platform-specific defs.
103 #if defined(_MSC_VER)
104 # define ALWAYS_INLINE __declspec(forceinline)
105 // FIXME(timurrrr): do we need this on Windows?
106 # define ALIAS(x)
107 # define ALIGNED(x) __declspec(align(x))
108 # define FORMAT(f, a)
109 # define NOINLINE __declspec(noinline)
110 # define NORETURN __declspec(noreturn)
111 # define THREADLOCAL __declspec(thread)
112 # define NOTHROW
113 # define LIKELY(x) (x)
114 # define UNLIKELY(x) (x)
115 # define UNUSED
116 # define USED
117 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */
118 #else // _MSC_VER
119 # define ALWAYS_INLINE __attribute__((always_inline))
120 # define ALIAS(x) __attribute__((alias(x)))
121 # define ALIGNED(x) __attribute__((aligned(x)))
122 # define FORMAT(f, a) __attribute__((format(printf, f, a)))
123 # define NOINLINE __attribute__((noinline))
124 # define NORETURN __attribute__((noreturn))
125 # define THREADLOCAL __thread
126 # define NOTHROW throw()
127 # define LIKELY(x) __builtin_expect(!!(x), 1)
128 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
129 # define UNUSED __attribute__((unused))
130 # define USED __attribute__((used))
131 # if defined(__i386__) || defined(__x86_64__)
132 // __builtin_prefetch(x) generates prefetchnt0 on x86
133 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
134 # else
135 # define PREFETCH(x) __builtin_prefetch(x)
136 # endif
137 #endif // _MSC_VER
139 #if defined(_WIN32)
140 typedef unsigned long DWORD; // NOLINT
141 typedef DWORD thread_return_t;
142 # define THREAD_CALLING_CONV __stdcall
143 #else // _WIN32
144 typedef void* thread_return_t;
145 # define THREAD_CALLING_CONV
146 #endif // _WIN32
147 typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
149 #if __LP64__ || defined(_WIN64)
150 # define SANITIZER_WORDSIZE 64
151 #else
152 # define SANITIZER_WORDSIZE 32
153 #endif
155 // NOTE: Functions below must be defined in each run-time.
156 namespace __sanitizer {
157 void NORETURN Die();
158 void NORETURN CheckFailed(const char *file, int line, const char *cond,
159 u64 v1, u64 v2);
160 } // namespace __sanitizer
162 // Check macro
163 #define RAW_CHECK_MSG(expr, msg) do { \
164 if (!(expr)) { \
165 RawWrite(msg); \
166 Die(); \
168 } while (0)
170 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
172 #define CHECK_IMPL(c1, op, c2) \
173 do { \
174 __sanitizer::u64 v1 = (u64)(c1); \
175 __sanitizer::u64 v2 = (u64)(c2); \
176 if (!(v1 op v2)) \
177 __sanitizer::CheckFailed(__FILE__, __LINE__, \
178 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
179 } while (false) \
180 /**/
182 #define CHECK(a) CHECK_IMPL((a), !=, 0)
183 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
184 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
185 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
186 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
187 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
188 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
190 #if TSAN_DEBUG
191 #define DCHECK(a) CHECK(a)
192 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
193 #define DCHECK_NE(a, b) CHECK_NE(a, b)
194 #define DCHECK_LT(a, b) CHECK_LT(a, b)
195 #define DCHECK_LE(a, b) CHECK_LE(a, b)
196 #define DCHECK_GT(a, b) CHECK_GT(a, b)
197 #define DCHECK_GE(a, b) CHECK_GE(a, b)
198 #else
199 #define DCHECK(a)
200 #define DCHECK_EQ(a, b)
201 #define DCHECK_NE(a, b)
202 #define DCHECK_LT(a, b)
203 #define DCHECK_LE(a, b)
204 #define DCHECK_GT(a, b)
205 #define DCHECK_GE(a, b)
206 #endif
208 #define UNREACHABLE(msg) do { \
209 CHECK(0 && msg); \
210 Die(); \
211 } while (0)
213 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
215 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
217 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
219 #define IMPL_PASTE(a, b) a##b
220 #define IMPL_COMPILER_ASSERT(pred, line) \
221 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
223 // Limits for integral types. We have to redefine it in case we don't
224 // have stdint.h (like in Visual Studio 9).
225 #undef __INT64_C
226 #undef __UINT64_C
227 #if SANITIZER_WORDSIZE == 64
228 # define __INT64_C(c) c ## L
229 # define __UINT64_C(c) c ## UL
230 #else
231 # define __INT64_C(c) c ## LL
232 # define __UINT64_C(c) c ## ULL
233 #endif // SANITIZER_WORDSIZE == 64
234 #undef INT32_MIN
235 #define INT32_MIN (-2147483647-1)
236 #undef INT32_MAX
237 #define INT32_MAX (2147483647)
238 #undef UINT32_MAX
239 #define UINT32_MAX (4294967295U)
240 #undef INT64_MIN
241 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
242 #undef INT64_MAX
243 #define INT64_MAX (__INT64_C(9223372036854775807))
244 #undef UINT64_MAX
245 #define UINT64_MAX (__UINT64_C(18446744073709551615))
247 enum LinkerInitialized { LINKER_INITIALIZED = 0 };
249 #if !defined(_MSC_VER) || defined(__clang__)
250 # define GET_CALLER_PC() (uptr)__builtin_return_address(0)
251 # define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
252 #else
253 extern "C" void* _ReturnAddress(void);
254 # pragma intrinsic(_ReturnAddress)
255 # define GET_CALLER_PC() (uptr)_ReturnAddress()
256 // CaptureStackBackTrace doesn't need to know BP on Windows.
257 // FIXME: This macro is still used when printing error reports though it's not
258 // clear if the BP value is needed in the ASan reports on Windows.
259 # define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
260 #endif
262 #define HANDLE_EINTR(res, f) { \
263 do { \
264 res = (f); \
265 } while (res == -1 && errno == EINTR); \
268 #endif // SANITIZER_DEFS_H