Daily bump.
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_internal_defs.h
blob0dab7c2f6c7dc060e9fa8f5073ba9837c9a79a5c
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 // Only use SANITIZER_*ATTRIBUTE* before the function return type!
17 #if SANITIZER_WINDOWS
18 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport)
19 // FIXME find out what we need on Windows, if anything.
20 # define SANITIZER_WEAK_ATTRIBUTE
21 #elif defined(SANITIZER_GO)
22 # define SANITIZER_INTERFACE_ATTRIBUTE
23 # define SANITIZER_WEAK_ATTRIBUTE
24 #else
25 # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
26 # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
27 #endif
29 #if SANITIZER_LINUX && !defined(SANITIZER_GO)
30 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
31 #else
32 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
33 #endif
35 #if __LP64__ || defined(_WIN64)
36 # define SANITIZER_WORDSIZE 64
37 #else
38 # define SANITIZER_WORDSIZE 32
39 #endif
41 // GCC does not understand __has_feature
42 #if !defined(__has_feature)
43 # define __has_feature(x) 0
44 #endif
46 // For portability reasons we do not include stddef.h, stdint.h or any other
47 // system header, but we do need some basic types that are not defined
48 // in a portable way by the language itself.
49 namespace __sanitizer {
51 #if defined(_WIN64)
52 // 64-bit Windows uses LLP64 data model.
53 typedef unsigned long long uptr; // NOLINT
54 typedef signed long long sptr; // NOLINT
55 #else
56 typedef unsigned long uptr; // NOLINT
57 typedef signed long sptr; // NOLINT
58 #endif // defined(_WIN64)
59 #if defined(__x86_64__)
60 // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
61 // 64-bit pointer to unwind stack frame.
62 typedef unsigned long long uhwptr; // NOLINT
63 #else
64 typedef uptr uhwptr; // NOLINT
65 #endif
66 typedef unsigned char u8;
67 typedef unsigned short u16; // NOLINT
68 typedef unsigned int u32;
69 typedef unsigned long long u64; // NOLINT
70 typedef signed char s8;
71 typedef signed short s16; // NOLINT
72 typedef signed int s32;
73 typedef signed long long s64; // NOLINT
74 typedef int fd_t;
76 // WARNING: OFF_T may be different from OS type off_t, depending on the value of
77 // _FILE_OFFSET_BITS. This definition of OFF_T matches the ABI of system calls
78 // like pread and mmap, as opposed to pread64 and mmap64.
79 // Mac and Linux/x86-64 are special.
80 #if SANITIZER_MAC || (SANITIZER_LINUX && defined(__x86_64__))
81 typedef u64 OFF_T;
82 #else
83 typedef uptr OFF_T;
84 #endif
85 typedef u64 OFF64_T;
87 #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
88 typedef uptr operator_new_size_type;
89 #else
90 typedef u32 operator_new_size_type;
91 #endif
92 } // namespace __sanitizer
94 extern "C" {
95 // Tell the tools to write their reports to "path.<pid>" instead of stderr.
96 // The special values are "stdout" and "stderr".
97 SANITIZER_INTERFACE_ATTRIBUTE
98 void __sanitizer_set_report_path(const char *path);
100 // Notify the tools that the sandbox is going to be turned on. The reserved
101 // parameter will be used in the future to hold a structure with functions
102 // that the tools may call to bypass the sandbox.
103 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
104 void __sanitizer_sandbox_on_notify(void *reserved);
106 // This function is called by the tool when it has just finished reporting
107 // an error. 'error_summary' is a one-line string that summarizes
108 // the error message. This function can be overridden by the client.
109 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
110 void __sanitizer_report_error_summary(const char *error_summary);
112 SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump();
113 SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov(void *pc);
114 SANITIZER_INTERFACE_ATTRIBUTE
115 void __sanitizer_annotate_contiguous_container(const void *beg,
116 const void *end,
117 const void *old_mid,
118 const void *new_mid);
119 } // extern "C"
122 using namespace __sanitizer; // NOLINT
123 // ----------- ATTENTION -------------
124 // This header should NOT include any other headers to avoid portability issues.
126 // Common defs.
127 #define INLINE inline
128 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
129 #define WEAK SANITIZER_WEAK_ATTRIBUTE
131 // Platform-specific defs.
132 #if defined(_MSC_VER)
133 # define ALWAYS_INLINE __forceinline
134 // FIXME(timurrrr): do we need this on Windows?
135 # define ALIAS(x)
136 # define ALIGNED(x) __declspec(align(x))
137 # define FORMAT(f, a)
138 # define NOINLINE __declspec(noinline)
139 # define NORETURN __declspec(noreturn)
140 # define THREADLOCAL __declspec(thread)
141 # define NOTHROW
142 # define LIKELY(x) (x)
143 # define UNLIKELY(x) (x)
144 # define UNUSED
145 # define USED
146 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */
147 #else // _MSC_VER
148 # define ALWAYS_INLINE inline __attribute__((always_inline))
149 # define ALIAS(x) __attribute__((alias(x)))
150 // Please only use the ALIGNED macro before the type.
151 // Using ALIGNED after the variable declaration is not portable!
152 # define ALIGNED(x) __attribute__((aligned(x)))
153 # define FORMAT(f, a) __attribute__((format(printf, f, a)))
154 # define NOINLINE __attribute__((noinline))
155 # define NORETURN __attribute__((noreturn))
156 # define THREADLOCAL __thread
157 # define NOTHROW throw()
158 # define LIKELY(x) __builtin_expect(!!(x), 1)
159 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
160 # define UNUSED __attribute__((unused))
161 # define USED __attribute__((used))
162 # if defined(__i386__) || defined(__x86_64__)
163 // __builtin_prefetch(x) generates prefetchnt0 on x86
164 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
165 # else
166 # define PREFETCH(x) __builtin_prefetch(x)
167 # endif
168 #endif // _MSC_VER
170 // Unaligned versions of basic types.
171 typedef ALIGNED(1) u16 uu16;
172 typedef ALIGNED(1) u32 uu32;
173 typedef ALIGNED(1) u64 uu64;
174 typedef ALIGNED(1) s16 us16;
175 typedef ALIGNED(1) s32 us32;
176 typedef ALIGNED(1) s64 us64;
178 #if SANITIZER_WINDOWS
179 typedef unsigned long DWORD; // NOLINT
180 typedef DWORD thread_return_t;
181 # define THREAD_CALLING_CONV __stdcall
182 #else // _WIN32
183 typedef void* thread_return_t;
184 # define THREAD_CALLING_CONV
185 #endif // _WIN32
186 typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
188 // NOTE: Functions below must be defined in each run-time.
189 namespace __sanitizer {
190 void NORETURN Die();
192 // FIXME: No, this shouldn't be in the sanitizer interface.
193 SANITIZER_INTERFACE_ATTRIBUTE
194 void NORETURN CheckFailed(const char *file, int line, const char *cond,
195 u64 v1, u64 v2);
196 } // namespace __sanitizer
198 // Check macro
199 #define RAW_CHECK_MSG(expr, msg) do { \
200 if (!(expr)) { \
201 RawWrite(msg); \
202 Die(); \
204 } while (0)
206 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
208 #define CHECK_IMPL(c1, op, c2) \
209 do { \
210 __sanitizer::u64 v1 = (u64)(c1); \
211 __sanitizer::u64 v2 = (u64)(c2); \
212 if (!(v1 op v2)) \
213 __sanitizer::CheckFailed(__FILE__, __LINE__, \
214 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
215 } while (false) \
216 /**/
218 #define CHECK(a) CHECK_IMPL((a), !=, 0)
219 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
220 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
221 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
222 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
223 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
224 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
226 #if TSAN_DEBUG
227 #define DCHECK(a) CHECK(a)
228 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
229 #define DCHECK_NE(a, b) CHECK_NE(a, b)
230 #define DCHECK_LT(a, b) CHECK_LT(a, b)
231 #define DCHECK_LE(a, b) CHECK_LE(a, b)
232 #define DCHECK_GT(a, b) CHECK_GT(a, b)
233 #define DCHECK_GE(a, b) CHECK_GE(a, b)
234 #else
235 #define DCHECK(a)
236 #define DCHECK_EQ(a, b)
237 #define DCHECK_NE(a, b)
238 #define DCHECK_LT(a, b)
239 #define DCHECK_LE(a, b)
240 #define DCHECK_GT(a, b)
241 #define DCHECK_GE(a, b)
242 #endif
244 #define UNREACHABLE(msg) do { \
245 CHECK(0 && msg); \
246 Die(); \
247 } while (0)
249 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
251 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
253 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
255 #define IMPL_PASTE(a, b) a##b
256 #define IMPL_COMPILER_ASSERT(pred, line) \
257 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
259 // Limits for integral types. We have to redefine it in case we don't
260 // have stdint.h (like in Visual Studio 9).
261 #undef __INT64_C
262 #undef __UINT64_C
263 #if SANITIZER_WORDSIZE == 64
264 # define __INT64_C(c) c ## L
265 # define __UINT64_C(c) c ## UL
266 #else
267 # define __INT64_C(c) c ## LL
268 # define __UINT64_C(c) c ## ULL
269 #endif // SANITIZER_WORDSIZE == 64
270 #undef INT32_MIN
271 #define INT32_MIN (-2147483647-1)
272 #undef INT32_MAX
273 #define INT32_MAX (2147483647)
274 #undef UINT32_MAX
275 #define UINT32_MAX (4294967295U)
276 #undef INT64_MIN
277 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
278 #undef INT64_MAX
279 #define INT64_MAX (__INT64_C(9223372036854775807))
280 #undef UINT64_MAX
281 #define UINT64_MAX (__UINT64_C(18446744073709551615))
283 enum LinkerInitialized { LINKER_INITIALIZED = 0 };
285 #if !defined(_MSC_VER) || defined(__clang__)
286 # define GET_CALLER_PC() (uptr)__builtin_return_address(0)
287 # define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
288 #else
289 extern "C" void* _ReturnAddress(void);
290 # pragma intrinsic(_ReturnAddress)
291 # define GET_CALLER_PC() (uptr)_ReturnAddress()
292 // CaptureStackBackTrace doesn't need to know BP on Windows.
293 // FIXME: This macro is still used when printing error reports though it's not
294 // clear if the BP value is needed in the ASan reports on Windows.
295 # define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
296 #endif
298 #define HANDLE_EINTR(res, f) \
300 int rverrno; \
301 do { \
302 res = (f); \
303 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
306 #endif // SANITIZER_DEFS_H