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 || \
109 SANITIZER_FUCHSIA) && !defined(PIC)
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_SOLARIS && (defined(_LP64) || _FILE_OFFSET_BITS == 64)) || \
176 (SANITIZER_LINUX && defined(__x86_64__))
183 #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
184 typedef uptr operator_new_size_type;
186 # if SANITIZER_OPENBSD || defined(__s390__) && !defined(__s390x__)
187 // Special case: 31-bit s390 has unsigned long as size_t.
188 typedef unsigned long operator_new_size_type;
190 typedef u32 operator_new_size_type;
196 // ----------- ATTENTION -------------
197 // This header should NOT include any other headers to avoid portability issues.
201 #define INLINE inline
203 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
204 #define SANITIZER_WEAK_DEFAULT_IMPL \
205 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
206 #define SANITIZER_WEAK_CXX_DEFAULT_IMPL \
207 extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
209 // Platform-specific defs.
210 #if defined(_MSC_VER)
211 # define ALWAYS_INLINE __forceinline
212 // FIXME(timurrrr): do we need this on Windows?
214 # define ALIGNED(x) __declspec(align(x))
215 # define FORMAT(f, a)
216 # define NOINLINE __declspec(noinline)
217 # define NORETURN __declspec(noreturn)
218 # define THREADLOCAL __declspec(thread)
219 # define LIKELY(x) (x)
220 # define UNLIKELY(x) (x)
221 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0
222 # define WARN_UNUSED_RESULT
224 # define ALWAYS_INLINE inline __attribute__((always_inline))
225 # define ALIAS(x) __attribute__((alias(x)))
226 // Please only use the ALIGNED macro before the type.
227 // Using ALIGNED after the variable declaration is not portable!
228 # define ALIGNED(x) __attribute__((aligned(x)))
229 # define FORMAT(f, a) __attribute__((format(printf, f, a)))
230 # define NOINLINE __attribute__((noinline))
231 # define NORETURN __attribute__((noreturn))
232 # define THREADLOCAL __thread
233 # define LIKELY(x) __builtin_expect(!!(x), 1)
234 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
235 # if defined(__i386__) || defined(__x86_64__)
236 // __builtin_prefetch(x) generates prefetchnt0 on x86
237 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
239 # define PREFETCH(x) __builtin_prefetch(x)
241 # define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
244 #if !defined(_MSC_VER) || defined(__clang__)
245 # define UNUSED __attribute__((unused))
246 # define USED __attribute__((used))
252 #if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)
253 # define NOEXCEPT noexcept
255 # define NOEXCEPT throw()
258 // Unaligned versions of basic types.
259 typedef ALIGNED(1) u16 uu16;
260 typedef ALIGNED(1) u32 uu32;
261 typedef ALIGNED(1) u64 uu64;
262 typedef ALIGNED(1) s16 us16;
263 typedef ALIGNED(1) s32 us32;
264 typedef ALIGNED(1) s64 us64;
266 #if SANITIZER_WINDOWS
267 } // namespace __sanitizer
268 typedef unsigned long DWORD; // NOLINT
269 namespace __sanitizer {
270 typedef DWORD thread_return_t;
271 # define THREAD_CALLING_CONV __stdcall
273 typedef void* thread_return_t;
274 # define THREAD_CALLING_CONV
276 typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
278 // NOTE: Functions below must be defined in each run-time.
281 void NORETURN CheckFailed(const char *file, int line, const char *cond,
285 #define RAW_CHECK_MSG(expr, msg) do { \
286 if (UNLIKELY(!(expr))) { \
292 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
294 #define CHECK_IMPL(c1, op, c2) \
296 __sanitizer::u64 v1 = (__sanitizer::u64)(c1); \
297 __sanitizer::u64 v2 = (__sanitizer::u64)(c2); \
298 if (UNLIKELY(!(v1 op v2))) \
299 __sanitizer::CheckFailed(__FILE__, __LINE__, \
300 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
304 #define CHECK(a) CHECK_IMPL((a), !=, 0)
305 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
306 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
307 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
308 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
309 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
310 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
313 #define DCHECK(a) CHECK(a)
314 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
315 #define DCHECK_NE(a, b) CHECK_NE(a, b)
316 #define DCHECK_LT(a, b) CHECK_LT(a, b)
317 #define DCHECK_LE(a, b) CHECK_LE(a, b)
318 #define DCHECK_GT(a, b) CHECK_GT(a, b)
319 #define DCHECK_GE(a, b) CHECK_GE(a, b)
322 #define DCHECK_EQ(a, b)
323 #define DCHECK_NE(a, b)
324 #define DCHECK_LT(a, b)
325 #define DCHECK_LE(a, b)
326 #define DCHECK_GT(a, b)
327 #define DCHECK_GE(a, b)
330 #define UNREACHABLE(msg) do { \
335 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
337 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
339 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
341 #define IMPL_PASTE(a, b) a##b
342 #define IMPL_COMPILER_ASSERT(pred, line) \
343 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
345 // Limits for integral types. We have to redefine it in case we don't
346 // have stdint.h (like in Visual Studio 9).
349 #if SANITIZER_WORDSIZE == 64
350 # define __INT64_C(c) c ## L
351 # define __UINT64_C(c) c ## UL
353 # define __INT64_C(c) c ## LL
354 # define __UINT64_C(c) c ## ULL
355 #endif // SANITIZER_WORDSIZE == 64
357 #define INT32_MIN (-2147483647-1)
359 #define INT32_MAX (2147483647)
361 #define UINT32_MAX (4294967295U)
363 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
365 #define INT64_MAX (__INT64_C(9223372036854775807))
367 #define UINT64_MAX (__UINT64_C(18446744073709551615))
369 #if SANITIZER_WORDSIZE == 64
370 # define UINTPTR_MAX (18446744073709551615UL)
372 # define UINTPTR_MAX (4294967295U)
373 #endif // SANITIZER_WORDSIZE == 64
375 enum LinkerInitialized { LINKER_INITIALIZED = 0 };
377 #if !defined(_MSC_VER) || defined(__clang__)
378 #if SANITIZER_S390_31
379 #define GET_CALLER_PC() \
380 (__sanitizer::uptr) __builtin_extract_return_addr(__builtin_return_address(0))
382 #define GET_CALLER_PC() (__sanitizer::uptr) __builtin_return_address(0)
384 #define GET_CURRENT_FRAME() (__sanitizer::uptr) __builtin_frame_address(0)
389 extern "C" void* _ReturnAddress(void);
390 extern "C" void* _AddressOfReturnAddress(void);
391 # pragma intrinsic(_ReturnAddress)
392 # pragma intrinsic(_AddressOfReturnAddress)
393 #define GET_CALLER_PC() (__sanitizer::uptr) _ReturnAddress()
394 // CaptureStackBackTrace doesn't need to know BP on Windows.
395 #define GET_CURRENT_FRAME() \
396 (((__sanitizer::uptr)_AddressOfReturnAddress()) + sizeof(__sanitizer::uptr))
398 extern "C" void __ud2(void);
399 # pragma intrinsic(__ud2)
405 #define HANDLE_EINTR(res, f) \
410 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
413 // Forces the compiler to generate a frame pointer in the function.
414 #define ENABLE_FRAME_POINTER \
416 volatile __sanitizer::uptr enable_fp; \
417 enable_fp = GET_CURRENT_FRAME(); \
421 } // namespace __sanitizer
423 namespace __asan { using namespace __sanitizer; } // NOLINT
424 namespace __dsan { using namespace __sanitizer; } // NOLINT
425 namespace __dfsan { using namespace __sanitizer; } // NOLINT
426 namespace __esan { using namespace __sanitizer; } // NOLINT
427 namespace __lsan { using namespace __sanitizer; } // NOLINT
428 namespace __msan { using namespace __sanitizer; } // NOLINT
429 namespace __hwasan { using namespace __sanitizer; } // NOLINT
430 namespace __tsan { using namespace __sanitizer; } // NOLINT
431 namespace __scudo { using namespace __sanitizer; } // NOLINT
432 namespace __ubsan { using namespace __sanitizer; } // NOLINT
433 namespace __xray { using namespace __sanitizer; } // NOLINT
434 namespace __interception { using namespace __sanitizer; } // NOLINT
435 namespace __hwasan { using namespace __sanitizer; } // NOLINT
438 #endif // SANITIZER_DEFS_H