1 //===-- sanitizer_internal_defs.h -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is shared between AddressSanitizer and ThreadSanitizer.
11 // It contains macro used in run-time libraries code.
12 //===----------------------------------------------------------------------===//
13 #ifndef SANITIZER_DEFS_H
14 #define SANITIZER_DEFS_H
16 #include "sanitizer_platform.h"
18 #ifndef SANITIZER_DEBUG
19 # define SANITIZER_DEBUG 0
22 #define SANITIZER_STRINGIFY_(S) #S
23 #define SANITIZER_STRINGIFY(S) SANITIZER_STRINGIFY_(S)
25 // Only use SANITIZER_*ATTRIBUTE* before the function return type!
27 #if SANITIZER_IMPORT_INTERFACE
28 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllimport)
30 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport)
32 # define SANITIZER_WEAK_ATTRIBUTE
34 # define SANITIZER_INTERFACE_ATTRIBUTE
35 # define SANITIZER_WEAK_ATTRIBUTE
37 # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
38 # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
41 // TLS is handled differently on different platforms
42 #if SANITIZER_LINUX || SANITIZER_NETBSD || \
43 SANITIZER_FREEBSD || SANITIZER_OPENBSD
44 # define SANITIZER_TLS_INITIAL_EXEC_ATTRIBUTE \
45 __attribute__((tls_model("initial-exec"))) thread_local
47 # define SANITIZER_TLS_INITIAL_EXEC_ATTRIBUTE
50 //--------------------------- WEAK FUNCTIONS ---------------------------------//
51 // When working with weak functions, to simplify the code and make it more
52 // portable, when possible define a default implementation using this macro:
54 // SANITIZER_INTERFACE_WEAK_DEF(<return_type>, <name>, <parameter list>)
57 // SANITIZER_INTERFACE_WEAK_DEF(bool, compare, int a, int b) { return a > b; }
60 #include "sanitizer_win_defs.h"
61 # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \
62 WIN_WEAK_EXPORT_DEF(ReturnType, Name, __VA_ARGS__)
64 # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \
65 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE \
66 ReturnType Name(__VA_ARGS__)
69 // SANITIZER_SUPPORTS_WEAK_HOOKS means that we support real weak functions that
70 // will evaluate to a null pointer when not defined.
71 #ifndef SANITIZER_SUPPORTS_WEAK_HOOKS
72 #if (SANITIZER_LINUX || SANITIZER_SOLARIS) && !SANITIZER_GO
73 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
74 // Before Xcode 4.5, the Darwin linker doesn't reliably support undefined
75 // weak symbols. Mac OS X 10.9/Darwin 13 is the first release only supported
77 #elif SANITIZER_MAC && \
78 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1090 && !SANITIZER_GO
79 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
81 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
83 #endif // SANITIZER_SUPPORTS_WEAK_HOOKS
84 // For some weak hooks that will be called very often and we want to avoid the
85 // overhead of executing the default implementation when it is not necessary,
86 // we can use the flag SANITIZER_SUPPORTS_WEAK_HOOKS to only define the default
87 // implementation for platforms that doesn't support weak symbols. For example:
89 // #if !SANITIZER_SUPPORT_WEAK_HOOKS
90 // SANITIZER_INTERFACE_WEAK_DEF(bool, compare_hook, int a, int b) {
95 // And then use it as: if (compare_hook) compare_hook(a, b);
96 //----------------------------------------------------------------------------//
99 // We can use .preinit_array section on Linux to call sanitizer initialization
100 // functions very early in the process startup (unless PIC macro is defined).
102 // On FreeBSD, .preinit_array functions are called with rtld_bind_lock writer
103 // lock held. It will lead to dead lock if unresolved PLT functions (which helds
104 // rtld_bind_lock reader lock) are called inside .preinit_array functions.
106 // FIXME: do we have anything like this on Mac?
107 #ifndef SANITIZER_CAN_USE_PREINIT_ARRAY
108 #if ((SANITIZER_LINUX && !SANITIZER_ANDROID) || SANITIZER_OPENBSD) && \
110 #define SANITIZER_CAN_USE_PREINIT_ARRAY 1
111 // Before Solaris 11.4, .preinit_array is fully supported only with GNU ld.
112 // FIXME: Check for those conditions.
113 #elif SANITIZER_SOLARIS && !defined(PIC)
114 # define SANITIZER_CAN_USE_PREINIT_ARRAY 1
116 # define SANITIZER_CAN_USE_PREINIT_ARRAY 0
118 #endif // SANITIZER_CAN_USE_PREINIT_ARRAY
120 // GCC does not understand __has_feature
121 #if !defined(__has_feature)
122 # define __has_feature(x) 0
125 // Older GCCs do not understand __has_attribute.
126 #if !defined(__has_attribute)
127 # define __has_attribute(x) 0
130 // For portability reasons we do not include stddef.h, stdint.h or any other
131 // system header, but we do need some basic types that are not defined
132 // in a portable way by the language itself.
133 namespace __sanitizer {
136 // 64-bit Windows uses LLP64 data model.
137 typedef unsigned long long uptr; // NOLINT
138 typedef signed long long sptr; // NOLINT
140 typedef unsigned long uptr; // NOLINT
141 typedef signed long sptr; // NOLINT
142 #endif // defined(_WIN64)
143 #if defined(__x86_64__)
144 // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
145 // 64-bit pointer to unwind stack frame.
146 typedef unsigned long long uhwptr; // NOLINT
148 typedef uptr uhwptr; // NOLINT
150 typedef unsigned char u8;
151 typedef unsigned short u16; // NOLINT
152 typedef unsigned int u32;
153 typedef unsigned long long u64; // NOLINT
154 typedef signed char s8;
155 typedef signed short s16; // NOLINT
156 typedef signed int s32;
157 typedef signed long long s64; // NOLINT
158 #if SANITIZER_WINDOWS
159 // On Windows, files are HANDLE, which is a synonim of void*.
160 // Use void* to avoid including <windows.h> everywhere.
162 typedef unsigned error_t;
167 #if SANITIZER_SOLARIS && !defined(_LP64)
173 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || \
174 SANITIZER_OPENBSD || SANITIZER_MAC || \
175 (SANITIZER_LINUX && defined(__x86_64__))
182 #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
183 typedef uptr operator_new_size_type;
185 # if SANITIZER_OPENBSD || defined(__s390__) && !defined(__s390x__)
186 // Special case: 31-bit s390 has unsigned long as size_t.
187 typedef unsigned long operator_new_size_type;
189 typedef u32 operator_new_size_type;
195 // ----------- ATTENTION -------------
196 // This header should NOT include any other headers to avoid portability issues.
199 #define INLINE inline
200 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
201 #define SANITIZER_WEAK_DEFAULT_IMPL \
202 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
203 #define SANITIZER_WEAK_CXX_DEFAULT_IMPL \
204 extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
206 // Platform-specific defs.
207 #if defined(_MSC_VER)
208 # define ALWAYS_INLINE __forceinline
209 // FIXME(timurrrr): do we need this on Windows?
211 # define ALIGNED(x) __declspec(align(x))
212 # define FORMAT(f, a)
213 # define NOINLINE __declspec(noinline)
214 # define NORETURN __declspec(noreturn)
215 # define THREADLOCAL __declspec(thread)
216 # define LIKELY(x) (x)
217 # define UNLIKELY(x) (x)
218 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0
219 # define WARN_UNUSED_RESULT
221 # define ALWAYS_INLINE inline __attribute__((always_inline))
222 # define ALIAS(x) __attribute__((alias(x)))
223 // Please only use the ALIGNED macro before the type.
224 // Using ALIGNED after the variable declaration is not portable!
225 # define ALIGNED(x) __attribute__((aligned(x)))
226 # define FORMAT(f, a) __attribute__((format(printf, f, a)))
227 # define NOINLINE __attribute__((noinline))
228 # define NORETURN __attribute__((noreturn))
229 # define THREADLOCAL __thread
230 # define LIKELY(x) __builtin_expect(!!(x), 1)
231 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
232 # if defined(__i386__) || defined(__x86_64__)
233 // __builtin_prefetch(x) generates prefetchnt0 on x86
234 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
236 # define PREFETCH(x) __builtin_prefetch(x)
238 # define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
241 #if !defined(_MSC_VER) || defined(__clang__)
242 # define UNUSED __attribute__((unused))
243 # define USED __attribute__((used))
249 #if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)
250 # define NOEXCEPT noexcept
252 # define NOEXCEPT throw()
255 // Unaligned versions of basic types.
256 typedef ALIGNED(1) u16 uu16;
257 typedef ALIGNED(1) u32 uu32;
258 typedef ALIGNED(1) u64 uu64;
259 typedef ALIGNED(1) s16 us16;
260 typedef ALIGNED(1) s32 us32;
261 typedef ALIGNED(1) s64 us64;
263 #if SANITIZER_WINDOWS
264 } // namespace __sanitizer
265 typedef unsigned long DWORD; // NOLINT
266 namespace __sanitizer {
267 typedef DWORD thread_return_t;
268 # define THREAD_CALLING_CONV __stdcall
270 typedef void* thread_return_t;
271 # define THREAD_CALLING_CONV
273 typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
275 // NOTE: Functions below must be defined in each run-time.
278 // FIXME: No, this shouldn't be in the sanitizer interface.
279 SANITIZER_INTERFACE_ATTRIBUTE
280 void NORETURN CheckFailed(const char *file, int line, const char *cond,
284 #define RAW_CHECK_MSG(expr, msg) do { \
285 if (UNLIKELY(!(expr))) { \
291 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
293 #define CHECK_IMPL(c1, op, c2) \
295 __sanitizer::u64 v1 = (__sanitizer::u64)(c1); \
296 __sanitizer::u64 v2 = (__sanitizer::u64)(c2); \
297 if (UNLIKELY(!(v1 op v2))) \
298 __sanitizer::CheckFailed(__FILE__, __LINE__, \
299 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
303 #define CHECK(a) CHECK_IMPL((a), !=, 0)
304 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
305 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
306 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
307 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
308 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
309 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
312 #define DCHECK(a) CHECK(a)
313 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
314 #define DCHECK_NE(a, b) CHECK_NE(a, b)
315 #define DCHECK_LT(a, b) CHECK_LT(a, b)
316 #define DCHECK_LE(a, b) CHECK_LE(a, b)
317 #define DCHECK_GT(a, b) CHECK_GT(a, b)
318 #define DCHECK_GE(a, b) CHECK_GE(a, b)
321 #define DCHECK_EQ(a, b)
322 #define DCHECK_NE(a, b)
323 #define DCHECK_LT(a, b)
324 #define DCHECK_LE(a, b)
325 #define DCHECK_GT(a, b)
326 #define DCHECK_GE(a, b)
329 #define UNREACHABLE(msg) do { \
334 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
336 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
338 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
340 #define IMPL_PASTE(a, b) a##b
341 #define IMPL_COMPILER_ASSERT(pred, line) \
342 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
344 // Limits for integral types. We have to redefine it in case we don't
345 // have stdint.h (like in Visual Studio 9).
348 #if SANITIZER_WORDSIZE == 64
349 # define __INT64_C(c) c ## L
350 # define __UINT64_C(c) c ## UL
352 # define __INT64_C(c) c ## LL
353 # define __UINT64_C(c) c ## ULL
354 #endif // SANITIZER_WORDSIZE == 64
356 #define INT32_MIN (-2147483647-1)
358 #define INT32_MAX (2147483647)
360 #define UINT32_MAX (4294967295U)
362 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
364 #define INT64_MAX (__INT64_C(9223372036854775807))
366 #define UINT64_MAX (__UINT64_C(18446744073709551615))
368 #if SANITIZER_WORDSIZE == 64
369 # define UINTPTR_MAX (18446744073709551615UL)
371 # define UINTPTR_MAX (4294967295U)
372 #endif // SANITIZER_WORDSIZE == 64
374 enum LinkerInitialized { LINKER_INITIALIZED = 0 };
376 #if !defined(_MSC_VER) || defined(__clang__)
377 #if SANITIZER_S390_31
378 #define GET_CALLER_PC() \
379 (__sanitizer::uptr) __builtin_extract_return_addr(__builtin_return_address(0))
381 #define GET_CALLER_PC() (__sanitizer::uptr) __builtin_return_address(0)
383 #define GET_CURRENT_FRAME() (__sanitizer::uptr) __builtin_frame_address(0)
388 extern "C" void* _ReturnAddress(void);
389 extern "C" void* _AddressOfReturnAddress(void);
390 # pragma intrinsic(_ReturnAddress)
391 # pragma intrinsic(_AddressOfReturnAddress)
392 #define GET_CALLER_PC() (__sanitizer::uptr) _ReturnAddress()
393 // CaptureStackBackTrace doesn't need to know BP on Windows.
394 #define GET_CURRENT_FRAME() \
395 (((__sanitizer::uptr)_AddressOfReturnAddress()) + sizeof(__sanitizer::uptr))
397 extern "C" void __ud2(void);
398 # pragma intrinsic(__ud2)
404 #define HANDLE_EINTR(res, f) \
409 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
412 // Forces the compiler to generate a frame pointer in the function.
413 #define ENABLE_FRAME_POINTER \
415 volatile __sanitizer::uptr enable_fp; \
416 enable_fp = GET_CURRENT_FRAME(); \
420 } // namespace __sanitizer
422 namespace __asan { using namespace __sanitizer; } // NOLINT
423 namespace __dsan { using namespace __sanitizer; } // NOLINT
424 namespace __dfsan { using namespace __sanitizer; } // NOLINT
425 namespace __esan { using namespace __sanitizer; } // NOLINT
426 namespace __lsan { using namespace __sanitizer; } // NOLINT
427 namespace __msan { using namespace __sanitizer; } // NOLINT
428 namespace __hwasan { using namespace __sanitizer; } // NOLINT
429 namespace __tsan { using namespace __sanitizer; } // NOLINT
430 namespace __scudo { using namespace __sanitizer; } // NOLINT
431 namespace __ubsan { using namespace __sanitizer; } // NOLINT
432 namespace __xray { using namespace __sanitizer; } // NOLINT
433 namespace __interception { using namespace __sanitizer; } // NOLINT
436 #endif // SANITIZER_DEFS_H