[C++ PATCH] some cleanups
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_internal_defs.h
blobedd6a21c1221cf3d8ddb6c066670dd0acd38e693
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 #if SANITIZER_IMPORT_INTERFACE
23 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllimport)
24 #else
25 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport)
26 #endif
27 # define SANITIZER_WEAK_ATTRIBUTE
28 #elif SANITIZER_GO
29 # define SANITIZER_INTERFACE_ATTRIBUTE
30 # define SANITIZER_WEAK_ATTRIBUTE
31 #else
32 # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
33 # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
34 #endif
36 // TLS is handled differently on different platforms
37 #if SANITIZER_LINUX
38 # define SANITIZER_TLS_INITIAL_EXEC_ATTRIBUTE \
39 __attribute__((tls_model("initial-exec"))) thread_local
40 #else
41 # define SANITIZER_TLS_INITIAL_EXEC_ATTRIBUTE
42 #endif
44 //--------------------------- WEAK FUNCTIONS ---------------------------------//
45 // When working with weak functions, to simplify the code and make it more
46 // portable, when possible define a default implementation using this macro:
48 // SANITIZER_INTERFACE_WEAK_DEF(<return_type>, <name>, <parameter list>)
50 // For example:
51 // SANITIZER_INTERFACE_WEAK_DEF(bool, compare, int a, int b) { return a > b; }
53 #if SANITIZER_WINDOWS
54 #include "sanitizer_win_defs.h"
55 # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \
56 WIN_WEAK_EXPORT_DEF(ReturnType, Name, __VA_ARGS__)
57 #else
58 # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \
59 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE \
60 ReturnType Name(__VA_ARGS__)
61 #endif
63 // SANITIZER_SUPPORTS_WEAK_HOOKS means that we support real weak functions that
64 // will evaluate to a null pointer when not defined.
65 #ifndef SANITIZER_SUPPORTS_WEAK_HOOKS
66 #if SANITIZER_LINUX && !SANITIZER_GO
67 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
68 // Before Xcode 4.5, the Darwin linker doesn't reliably support undefined
69 // weak symbols. Mac OS X 10.9/Darwin 13 is the first release only supported
70 // by Xcode >= 4.5.
71 #elif SANITIZER_MAC && \
72 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1090 && !SANITIZER_GO
73 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
74 #else
75 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
76 #endif
77 #endif // SANITIZER_SUPPORTS_WEAK_HOOKS
78 // For some weak hooks that will be called very often and we want to avoid the
79 // overhead of executing the default implementation when it is not necessary,
80 // we can use the flag SANITIZER_SUPPORTS_WEAK_HOOKS to only define the default
81 // implementation for platforms that doesn't support weak symbols. For example:
83 // #if !SANITIZER_SUPPORT_WEAK_HOOKS
84 // SANITIZER_INTERFACE_WEAK_DEF(bool, compare_hook, int a, int b) {
85 // return a > b;
86 // }
87 // #endif
89 // And then use it as: if (compare_hook) compare_hook(a, b);
90 //----------------------------------------------------------------------------//
93 // We can use .preinit_array section on Linux to call sanitizer initialization
94 // functions very early in the process startup (unless PIC macro is defined).
95 // FIXME: do we have anything like this on Mac?
96 #if SANITIZER_LINUX && !SANITIZER_ANDROID && !defined(PIC)
97 # define SANITIZER_CAN_USE_PREINIT_ARRAY 1
98 #else
99 # define SANITIZER_CAN_USE_PREINIT_ARRAY 0
100 #endif
102 // GCC does not understand __has_feature
103 #if !defined(__has_feature)
104 # define __has_feature(x) 0
105 #endif
107 // For portability reasons we do not include stddef.h, stdint.h or any other
108 // system header, but we do need some basic types that are not defined
109 // in a portable way by the language itself.
110 namespace __sanitizer {
112 #if defined(_WIN64)
113 // 64-bit Windows uses LLP64 data model.
114 typedef unsigned long long uptr; // NOLINT
115 typedef signed long long sptr; // NOLINT
116 #else
117 typedef unsigned long uptr; // NOLINT
118 typedef signed long sptr; // NOLINT
119 #endif // defined(_WIN64)
120 #if defined(__x86_64__)
121 // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
122 // 64-bit pointer to unwind stack frame.
123 typedef unsigned long long uhwptr; // NOLINT
124 #else
125 typedef uptr uhwptr; // NOLINT
126 #endif
127 typedef unsigned char u8;
128 typedef unsigned short u16; // NOLINT
129 typedef unsigned int u32;
130 typedef unsigned long long u64; // NOLINT
131 typedef signed char s8;
132 typedef signed short s16; // NOLINT
133 typedef signed int s32;
134 typedef signed long long s64; // NOLINT
135 #if SANITIZER_WINDOWS
136 // On Windows, files are HANDLE, which is a synonim of void*.
137 // Use void* to avoid including <windows.h> everywhere.
138 typedef void* fd_t;
139 typedef unsigned error_t;
140 #else
141 typedef int fd_t;
142 typedef int error_t;
143 #endif
144 typedef int pid_t;
146 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_MAC || \
147 (SANITIZER_LINUX && defined(__x86_64__))
148 typedef u64 OFF_T;
149 #else
150 typedef uptr OFF_T;
151 #endif
152 typedef u64 OFF64_T;
154 #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
155 typedef uptr operator_new_size_type;
156 #else
157 # if defined(__s390__) && !defined(__s390x__)
158 // Special case: 31-bit s390 has unsigned long as size_t.
159 typedef unsigned long operator_new_size_type;
160 # else
161 typedef u32 operator_new_size_type;
162 # endif
163 #endif
165 #if SANITIZER_MAC
166 // On Darwin, thread IDs are 64-bit even on 32-bit systems.
167 typedef u64 tid_t;
168 #else
169 typedef uptr tid_t;
170 #endif
172 // ----------- ATTENTION -------------
173 // This header should NOT include any other headers to avoid portability issues.
175 // Common defs.
176 #define INLINE inline
177 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
178 #define SANITIZER_WEAK_DEFAULT_IMPL \
179 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
180 #define SANITIZER_WEAK_CXX_DEFAULT_IMPL \
181 extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
183 // Platform-specific defs.
184 #if defined(_MSC_VER)
185 # define ALWAYS_INLINE __forceinline
186 // FIXME(timurrrr): do we need this on Windows?
187 # define ALIAS(x)
188 # define ALIGNED(x) __declspec(align(x))
189 # define FORMAT(f, a)
190 # define NOINLINE __declspec(noinline)
191 # define NORETURN __declspec(noreturn)
192 # define THREADLOCAL __declspec(thread)
193 # define LIKELY(x) (x)
194 # define UNLIKELY(x) (x)
195 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0
196 #else // _MSC_VER
197 # define ALWAYS_INLINE inline __attribute__((always_inline))
198 # define ALIAS(x) __attribute__((alias(x)))
199 // Please only use the ALIGNED macro before the type.
200 // Using ALIGNED after the variable declaration is not portable!
201 # define ALIGNED(x) __attribute__((aligned(x)))
202 # define FORMAT(f, a) __attribute__((format(printf, f, a)))
203 # define NOINLINE __attribute__((noinline))
204 # define NORETURN __attribute__((noreturn))
205 # define THREADLOCAL __thread
206 # define LIKELY(x) __builtin_expect(!!(x), 1)
207 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
208 # if defined(__i386__) || defined(__x86_64__)
209 // __builtin_prefetch(x) generates prefetchnt0 on x86
210 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
211 # else
212 # define PREFETCH(x) __builtin_prefetch(x)
213 # endif
214 #endif // _MSC_VER
216 #if !defined(_MSC_VER) || defined(__clang__)
217 # define UNUSED __attribute__((unused))
218 # define USED __attribute__((used))
219 #else
220 # define UNUSED
221 # define USED
222 #endif
224 #if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)
225 # define NOEXCEPT noexcept
226 #else
227 # define NOEXCEPT throw()
228 #endif
230 // Unaligned versions of basic types.
231 typedef ALIGNED(1) u16 uu16;
232 typedef ALIGNED(1) u32 uu32;
233 typedef ALIGNED(1) u64 uu64;
234 typedef ALIGNED(1) s16 us16;
235 typedef ALIGNED(1) s32 us32;
236 typedef ALIGNED(1) s64 us64;
238 #if SANITIZER_WINDOWS
239 } // namespace __sanitizer
240 typedef unsigned long DWORD; // NOLINT
241 namespace __sanitizer {
242 typedef DWORD thread_return_t;
243 # define THREAD_CALLING_CONV __stdcall
244 #else // _WIN32
245 typedef void* thread_return_t;
246 # define THREAD_CALLING_CONV
247 #endif // _WIN32
248 typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
250 // NOTE: Functions below must be defined in each run-time.
251 void NORETURN Die();
253 // FIXME: No, this shouldn't be in the sanitizer interface.
254 SANITIZER_INTERFACE_ATTRIBUTE
255 void NORETURN CheckFailed(const char *file, int line, const char *cond,
256 u64 v1, u64 v2);
258 // Check macro
259 #define RAW_CHECK_MSG(expr, msg) do { \
260 if (UNLIKELY(!(expr))) { \
261 RawWrite(msg); \
262 Die(); \
264 } while (0)
266 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
268 #define CHECK_IMPL(c1, op, c2) \
269 do { \
270 __sanitizer::u64 v1 = (__sanitizer::u64)(c1); \
271 __sanitizer::u64 v2 = (__sanitizer::u64)(c2); \
272 if (UNLIKELY(!(v1 op v2))) \
273 __sanitizer::CheckFailed(__FILE__, __LINE__, \
274 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
275 } while (false) \
276 /**/
278 #define CHECK(a) CHECK_IMPL((a), !=, 0)
279 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
280 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
281 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
282 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
283 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
284 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
286 #if SANITIZER_DEBUG
287 #define DCHECK(a) CHECK(a)
288 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
289 #define DCHECK_NE(a, b) CHECK_NE(a, b)
290 #define DCHECK_LT(a, b) CHECK_LT(a, b)
291 #define DCHECK_LE(a, b) CHECK_LE(a, b)
292 #define DCHECK_GT(a, b) CHECK_GT(a, b)
293 #define DCHECK_GE(a, b) CHECK_GE(a, b)
294 #else
295 #define DCHECK(a)
296 #define DCHECK_EQ(a, b)
297 #define DCHECK_NE(a, b)
298 #define DCHECK_LT(a, b)
299 #define DCHECK_LE(a, b)
300 #define DCHECK_GT(a, b)
301 #define DCHECK_GE(a, b)
302 #endif
304 #define UNREACHABLE(msg) do { \
305 CHECK(0 && msg); \
306 Die(); \
307 } while (0)
309 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
311 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
313 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
315 #define IMPL_PASTE(a, b) a##b
316 #define IMPL_COMPILER_ASSERT(pred, line) \
317 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
319 // Limits for integral types. We have to redefine it in case we don't
320 // have stdint.h (like in Visual Studio 9).
321 #undef __INT64_C
322 #undef __UINT64_C
323 #if SANITIZER_WORDSIZE == 64
324 # define __INT64_C(c) c ## L
325 # define __UINT64_C(c) c ## UL
326 #else
327 # define __INT64_C(c) c ## LL
328 # define __UINT64_C(c) c ## ULL
329 #endif // SANITIZER_WORDSIZE == 64
330 #undef INT32_MIN
331 #define INT32_MIN (-2147483647-1)
332 #undef INT32_MAX
333 #define INT32_MAX (2147483647)
334 #undef UINT32_MAX
335 #define UINT32_MAX (4294967295U)
336 #undef INT64_MIN
337 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
338 #undef INT64_MAX
339 #define INT64_MAX (__INT64_C(9223372036854775807))
340 #undef UINT64_MAX
341 #define UINT64_MAX (__UINT64_C(18446744073709551615))
343 enum LinkerInitialized { LINKER_INITIALIZED = 0 };
345 #if !defined(_MSC_VER) || defined(__clang__)
346 #if SANITIZER_S390_31
347 #define GET_CALLER_PC() \
348 (__sanitizer::uptr) __builtin_extract_return_addr(__builtin_return_address(0))
349 #else
350 #define GET_CALLER_PC() (__sanitizer::uptr) __builtin_return_address(0)
351 #endif
352 #define GET_CURRENT_FRAME() (__sanitizer::uptr) __builtin_frame_address(0)
353 inline void Trap() {
354 __builtin_trap();
356 #else
357 extern "C" void* _ReturnAddress(void);
358 extern "C" void* _AddressOfReturnAddress(void);
359 # pragma intrinsic(_ReturnAddress)
360 # pragma intrinsic(_AddressOfReturnAddress)
361 #define GET_CALLER_PC() (__sanitizer::uptr) _ReturnAddress()
362 // CaptureStackBackTrace doesn't need to know BP on Windows.
363 #define GET_CURRENT_FRAME() \
364 (((__sanitizer::uptr)_AddressOfReturnAddress()) + sizeof(__sanitizer::uptr))
366 extern "C" void __ud2(void);
367 # pragma intrinsic(__ud2)
368 inline void Trap() {
369 __ud2();
371 #endif
373 #define HANDLE_EINTR(res, f) \
375 int rverrno; \
376 do { \
377 res = (f); \
378 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
381 // Forces the compiler to generate a frame pointer in the function.
382 #define ENABLE_FRAME_POINTER \
383 do { \
384 volatile __sanitizer::uptr enable_fp; \
385 enable_fp = GET_CURRENT_FRAME(); \
386 (void)enable_fp; \
387 } while (0)
389 } // namespace __sanitizer
391 namespace __asan { using namespace __sanitizer; } // NOLINT
392 namespace __dsan { using namespace __sanitizer; } // NOLINT
393 namespace __dfsan { using namespace __sanitizer; } // NOLINT
394 namespace __esan { using namespace __sanitizer; } // NOLINT
395 namespace __lsan { using namespace __sanitizer; } // NOLINT
396 namespace __msan { using namespace __sanitizer; } // NOLINT
397 namespace __tsan { using namespace __sanitizer; } // NOLINT
398 namespace __scudo { using namespace __sanitizer; } // NOLINT
399 namespace __ubsan { using namespace __sanitizer; } // NOLINT
400 namespace __xray { using namespace __sanitizer; } // NOLINT
401 namespace __interception { using namespace __sanitizer; } // NOLINT
404 #endif // SANITIZER_DEFS_H