1 //===-- sanitizer_internal_defs.h -------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file is shared between AddressSanitizer and ThreadSanitizer.
10 // It contains macro used in run-time libraries code.
11 //===----------------------------------------------------------------------===//
12 #ifndef SANITIZER_DEFS_H
13 #define SANITIZER_DEFS_H
15 #include "sanitizer_platform.h"
16 #include "sanitizer_redefine_builtins.h"
18 // GCC does not understand __has_feature.
19 #if !defined(__has_feature)
20 #define __has_feature(x) 0
23 #ifndef SANITIZER_DEBUG
24 # define SANITIZER_DEBUG 0
27 #define SANITIZER_STRINGIFY_(S) #S
28 #define SANITIZER_STRINGIFY(S) SANITIZER_STRINGIFY_(S)
30 // Only use SANITIZER_*ATTRIBUTE* before the function return type!
32 #if SANITIZER_IMPORT_INTERFACE
33 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllimport)
35 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport)
37 # define SANITIZER_WEAK_ATTRIBUTE
38 # define SANITIZER_WEAK_IMPORT
40 # define SANITIZER_INTERFACE_ATTRIBUTE
41 # define SANITIZER_WEAK_ATTRIBUTE
42 # define SANITIZER_WEAK_IMPORT
44 # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
45 # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
47 # define SANITIZER_WEAK_IMPORT extern "C" __attribute((weak_import))
49 # define SANITIZER_WEAK_IMPORT extern "C" SANITIZER_WEAK_ATTRIBUTE
50 # endif // SANITIZER_APPLE
51 #endif // SANITIZER_WINDOWS
53 //--------------------------- WEAK FUNCTIONS ---------------------------------//
54 // When working with weak functions, to simplify the code and make it more
55 // portable, when possible define a default implementation using this macro:
57 // SANITIZER_INTERFACE_WEAK_DEF(<return_type>, <name>, <parameter list>)
60 // SANITIZER_INTERFACE_WEAK_DEF(bool, compare, int a, int b) { return a > b; }
63 #include "sanitizer_win_defs.h"
64 # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \
65 WIN_WEAK_EXPORT_DEF(ReturnType, Name, __VA_ARGS__)
67 # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \
68 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE \
69 ReturnType Name(__VA_ARGS__)
72 // SANITIZER_SUPPORTS_WEAK_HOOKS means that we support real weak functions that
73 // will evaluate to a null pointer when not defined.
74 #ifndef SANITIZER_SUPPORTS_WEAK_HOOKS
75 #if (SANITIZER_LINUX || SANITIZER_SOLARIS) && !SANITIZER_GO
76 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
77 // Before Xcode 4.5, the Darwin linker doesn't reliably support undefined
78 // weak symbols. Mac OS X 10.9/Darwin 13 is the first release only supported
80 #elif SANITIZER_APPLE && \
81 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1090 && !SANITIZER_GO
82 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
84 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
86 #endif // SANITIZER_SUPPORTS_WEAK_HOOKS
87 // For some weak hooks that will be called very often and we want to avoid the
88 // overhead of executing the default implementation when it is not necessary,
89 // we can use the flag SANITIZER_SUPPORTS_WEAK_HOOKS to only define the default
90 // implementation for platforms that doesn't support weak symbols. For example:
92 // #if !SANITIZER_SUPPORT_WEAK_HOOKS
93 // SANITIZER_INTERFACE_WEAK_DEF(bool, compare_hook, int a, int b) {
98 // And then use it as: if (compare_hook) compare_hook(a, b);
99 //----------------------------------------------------------------------------//
102 // We can use .preinit_array section on Linux to call sanitizer initialization
103 // functions very early in the process startup (unless PIC macro is defined).
105 // On FreeBSD, .preinit_array functions are called with rtld_bind_lock writer
106 // lock held. It will lead to dead lock if unresolved PLT functions (which helds
107 // rtld_bind_lock reader lock) are called inside .preinit_array functions.
109 // FIXME: do we have anything like this on Mac?
110 #ifndef SANITIZER_CAN_USE_PREINIT_ARRAY
111 #if (SANITIZER_LINUX || SANITIZER_FUCHSIA || SANITIZER_NETBSD) && !defined(PIC)
112 #define SANITIZER_CAN_USE_PREINIT_ARRAY 1
113 // Before Solaris 11.4, .preinit_array is fully supported only with GNU ld.
114 // FIXME: Check for those conditions.
115 #elif SANITIZER_SOLARIS && !defined(PIC)
116 # define SANITIZER_CAN_USE_PREINIT_ARRAY 1
118 # define SANITIZER_CAN_USE_PREINIT_ARRAY 0
120 #endif // SANITIZER_CAN_USE_PREINIT_ARRAY
122 // GCC does not understand __has_feature
123 #if !defined(__has_feature)
124 # define __has_feature(x) 0
127 // Older GCCs do not understand __has_attribute.
128 #if !defined(__has_attribute)
129 # define __has_attribute(x) 0
132 #if !defined(__has_cpp_attribute)
133 # define __has_cpp_attribute(x) 0
136 // For portability reasons we do not include stddef.h, stdint.h or any other
137 // system header, but we do need some basic types that are not defined
138 // in a portable way by the language itself.
139 namespace __sanitizer
{
141 #if defined(__UINTPTR_TYPE__)
142 # if defined(__arm__) && defined(__linux__)
143 // Linux Arm headers redefine __UINTPTR_TYPE__ and disagree with clang/gcc.
144 typedef unsigned int uptr
;
147 typedef __UINTPTR_TYPE__ uptr
;
148 typedef __INTPTR_TYPE__ sptr
;
150 #elif defined(_WIN64)
151 // 64-bit Windows uses LLP64 data model.
152 typedef unsigned long long uptr
;
153 typedef signed long long sptr
;
154 #elif defined(_WIN32)
155 typedef unsigned int uptr
;
156 typedef signed int sptr
;
158 # error Unsupported compiler, missing __UINTPTR_TYPE__
159 #endif // defined(__UINTPTR_TYPE__)
160 #if defined(__x86_64__)
161 // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
162 // 64-bit pointer to unwind stack frame.
163 typedef unsigned long long uhwptr
;
167 typedef unsigned char u8
;
168 typedef unsigned short u16
;
169 typedef unsigned int u32
;
170 typedef unsigned long long u64
;
171 typedef signed char s8
;
172 typedef signed short s16
;
173 typedef signed int s32
;
174 typedef signed long long s64
;
175 #if SANITIZER_WINDOWS
176 // On Windows, files are HANDLE, which is a synonim of void*.
177 // Use void* to avoid including <windows.h> everywhere.
179 typedef unsigned error_t
;
184 #if SANITIZER_SOLARIS && !defined(_LP64)
190 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_APPLE || \
191 (SANITIZER_SOLARIS && (defined(_LP64) || _FILE_OFFSET_BITS == 64)) || \
192 (SANITIZER_LINUX && !SANITIZER_GLIBC && !SANITIZER_ANDROID) || \
193 (SANITIZER_LINUX && (defined(__x86_64__) || defined(__hexagon__)))
201 typedef __SIZE_TYPE__ usize
;
208 // ----------- ATTENTION -------------
209 // This header should NOT include any other headers to avoid portability issues.
212 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
213 #define SANITIZER_WEAK_DEFAULT_IMPL \
214 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
215 #define SANITIZER_WEAK_CXX_DEFAULT_IMPL \
216 extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
218 // Platform-specific defs.
219 #if defined(_MSC_VER)
220 # define ALWAYS_INLINE __forceinline
221 // FIXME(timurrrr): do we need this on Windows?
223 # define ALIGNED(x) __declspec(align(x))
224 # define FORMAT(f, a)
225 # define NOINLINE __declspec(noinline)
226 # define NORETURN __declspec(noreturn)
227 # define THREADLOCAL __declspec(thread)
228 # define LIKELY(x) (x)
229 # define UNLIKELY(x) (x)
230 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0
231 # define WARN_UNUSED_RESULT
233 # define ALWAYS_INLINE inline __attribute__((always_inline))
234 # define ALIAS(x) __attribute__((alias(SANITIZER_STRINGIFY(x))))
235 // Please only use the ALIGNED macro before the type.
236 // Using ALIGNED after the variable declaration is not portable!
237 # define ALIGNED(x) __attribute__((aligned(x)))
238 # define FORMAT(f, a) __attribute__((format(printf, f, a)))
239 # define NOINLINE __attribute__((noinline))
240 # define NORETURN __attribute__((noreturn))
241 # define THREADLOCAL __thread
242 # define LIKELY(x) __builtin_expect(!!(x), 1)
243 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
244 # if defined(__i386__) || defined(__x86_64__)
245 // __builtin_prefetch(x) generates prefetchnt0 on x86
246 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
248 # define PREFETCH(x) __builtin_prefetch(x)
250 # define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
253 #if !defined(_MSC_VER) || defined(__clang__)
254 # define UNUSED __attribute__((unused))
255 # define USED __attribute__((used))
261 #if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)
262 # define NOEXCEPT noexcept
264 # define NOEXCEPT throw()
267 #if __has_cpp_attribute(clang::fallthrough)
268 # define FALLTHROUGH [[clang::fallthrough]]
269 #elif __has_cpp_attribute(fallthrough)
270 # define FALLTHROUGH [[fallthrough]]
275 #if __has_attribute(uninitialized)
276 # define UNINITIALIZED __attribute__((uninitialized))
278 # define UNINITIALIZED
281 // Unaligned versions of basic types.
282 typedef ALIGNED(1) u16 uu16
;
283 typedef ALIGNED(1) u32 uu32
;
284 typedef ALIGNED(1) u64 uu64
;
285 typedef ALIGNED(1) s16 us16
;
286 typedef ALIGNED(1) s32 us32
;
287 typedef ALIGNED(1) s64 us64
;
289 #if SANITIZER_WINDOWS
290 } // namespace __sanitizer
291 typedef unsigned long DWORD
;
292 namespace __sanitizer
{
293 typedef DWORD thread_return_t
;
294 # define THREAD_CALLING_CONV __stdcall
296 typedef void* thread_return_t
;
297 # define THREAD_CALLING_CONV
299 typedef thread_return_t (THREAD_CALLING_CONV
*thread_callback_t
)(void* arg
);
301 // NOTE: Functions below must be defined in each run-time.
304 void NORETURN
CheckFailed(const char *file
, int line
, const char *cond
,
308 #define RAW_CHECK_MSG(expr, msg, ...) \
310 if (UNLIKELY(!(expr))) { \
311 const char* msgs[] = {msg, __VA_ARGS__}; \
312 for (const char* m : msgs) RawWrite(m); \
317 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr "\n", )
318 #define RAW_CHECK_VA(expr, ...) RAW_CHECK_MSG(expr, #expr "\n", __VA_ARGS__)
320 #define CHECK_IMPL(c1, op, c2) \
322 __sanitizer::u64 v1 = (__sanitizer::u64)(c1); \
323 __sanitizer::u64 v2 = (__sanitizer::u64)(c2); \
324 if (UNLIKELY(!(v1 op v2))) \
325 __sanitizer::CheckFailed(__FILE__, __LINE__, \
326 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
330 #define CHECK(a) CHECK_IMPL((a), !=, 0)
331 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
332 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
333 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
334 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
335 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
336 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
339 #define DCHECK(a) CHECK(a)
340 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
341 #define DCHECK_NE(a, b) CHECK_NE(a, b)
342 #define DCHECK_LT(a, b) CHECK_LT(a, b)
343 #define DCHECK_LE(a, b) CHECK_LE(a, b)
344 #define DCHECK_GT(a, b) CHECK_GT(a, b)
345 #define DCHECK_GE(a, b) CHECK_GE(a, b)
348 #define DCHECK_EQ(a, b)
349 #define DCHECK_NE(a, b)
350 #define DCHECK_LT(a, b)
351 #define DCHECK_LE(a, b)
352 #define DCHECK_GT(a, b)
353 #define DCHECK_GE(a, b)
356 #define UNREACHABLE(msg) do { \
361 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
363 #define COMPILER_CHECK(pred) static_assert(pred, "")
365 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
367 // Limits for integral types. We have to redefine it in case we don't
368 // have stdint.h (like in Visual Studio 9).
371 #if SANITIZER_WORDSIZE == 64
372 # define __INT64_C(c) c ## L
373 # define __UINT64_C(c) c ## UL
375 # define __INT64_C(c) c ## LL
376 # define __UINT64_C(c) c ## ULL
377 #endif // SANITIZER_WORDSIZE == 64
379 #define INT32_MIN (-2147483647-1)
381 #define INT32_MAX (2147483647)
383 #define UINT32_MAX (4294967295U)
385 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
387 #define INT64_MAX (__INT64_C(9223372036854775807))
389 #define UINT64_MAX (__UINT64_C(18446744073709551615))
391 #if SANITIZER_WORDSIZE == 64
392 # define UINTPTR_MAX (18446744073709551615UL)
394 # define UINTPTR_MAX (4294967295U)
395 #endif // SANITIZER_WORDSIZE == 64
397 enum LinkerInitialized
{ LINKER_INITIALIZED
= 0 };
399 #if !defined(_MSC_VER) || defined(__clang__)
400 # define GET_CALLER_PC() \
401 ((__sanitizer::uptr)__builtin_extract_return_addr( \
402 __builtin_return_address(0)))
403 # define GET_CURRENT_FRAME() ((__sanitizer::uptr)__builtin_frame_address(0))
408 extern "C" void* _ReturnAddress(void);
409 extern "C" void* _AddressOfReturnAddress(void);
410 # pragma intrinsic(_ReturnAddress)
411 # pragma intrinsic(_AddressOfReturnAddress)
412 # define GET_CALLER_PC() ((__sanitizer::uptr)_ReturnAddress())
413 // CaptureStackBackTrace doesn't need to know BP on Windows.
414 # define GET_CURRENT_FRAME() \
415 (((__sanitizer::uptr)_AddressOfReturnAddress()) + sizeof(__sanitizer::uptr))
417 extern "C" void __ud2(void);
418 # pragma intrinsic(__ud2)
424 #define HANDLE_EINTR(res, f) \
429 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
432 // Forces the compiler to generate a frame pointer in the function.
433 #define ENABLE_FRAME_POINTER \
435 volatile __sanitizer::uptr enable_fp; \
436 enable_fp = GET_CURRENT_FRAME(); \
440 // Internal thread identifier allocated by ThreadRegistry.
442 constexpr Tid kInvalidTid
= -1;
443 constexpr Tid kMainTid
= 0;
445 // Stack depot stack identifier.
447 const StackID kInvalidStackID
= 0;
449 } // namespace __sanitizer
452 using namespace __sanitizer
;
455 using namespace __sanitizer
;
458 using namespace __sanitizer
;
461 using namespace __sanitizer
;
464 using namespace __sanitizer
;
467 using namespace __sanitizer
;
470 using namespace __sanitizer
;
473 using namespace __sanitizer
;
476 using namespace __sanitizer
;
479 using namespace __sanitizer
;
482 using namespace __sanitizer
;
484 namespace __interception
{
485 using namespace __sanitizer
;
488 using namespace __sanitizer
;
490 namespace __memprof
{
491 using namespace __sanitizer
;
494 #endif // SANITIZER_DEFS_H