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"
17 #ifndef SANITIZER_DEBUG
18 # define SANITIZER_DEBUG 0
21 #define SANITIZER_STRINGIFY_(S) #S
22 #define SANITIZER_STRINGIFY(S) SANITIZER_STRINGIFY_(S)
24 // Only use SANITIZER_*ATTRIBUTE* before the function return type!
26 #if SANITIZER_IMPORT_INTERFACE
27 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllimport)
29 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport)
31 # define SANITIZER_WEAK_ATTRIBUTE
33 # define SANITIZER_INTERFACE_ATTRIBUTE
34 # define SANITIZER_WEAK_ATTRIBUTE
36 # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
37 # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
40 // TLS is handled differently on different platforms
41 #if SANITIZER_LINUX || SANITIZER_NETBSD || \
43 # define SANITIZER_TLS_INITIAL_EXEC_ATTRIBUTE \
44 __attribute__((tls_model("initial-exec"))) thread_local
46 # define SANITIZER_TLS_INITIAL_EXEC_ATTRIBUTE
49 //--------------------------- WEAK FUNCTIONS ---------------------------------//
50 // When working with weak functions, to simplify the code and make it more
51 // portable, when possible define a default implementation using this macro:
53 // SANITIZER_INTERFACE_WEAK_DEF(<return_type>, <name>, <parameter list>)
56 // SANITIZER_INTERFACE_WEAK_DEF(bool, compare, int a, int b) { return a > b; }
59 #include "sanitizer_win_defs.h"
60 # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \
61 WIN_WEAK_EXPORT_DEF(ReturnType, Name, __VA_ARGS__)
63 # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \
64 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE \
65 ReturnType Name(__VA_ARGS__)
68 // SANITIZER_SUPPORTS_WEAK_HOOKS means that we support real weak functions that
69 // will evaluate to a null pointer when not defined.
70 #ifndef SANITIZER_SUPPORTS_WEAK_HOOKS
71 #if (SANITIZER_LINUX || SANITIZER_SOLARIS) && !SANITIZER_GO
72 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
73 // Before Xcode 4.5, the Darwin linker doesn't reliably support undefined
74 // weak symbols. Mac OS X 10.9/Darwin 13 is the first release only supported
76 #elif SANITIZER_MAC && \
77 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1090 && !SANITIZER_GO
78 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
80 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
82 #endif // SANITIZER_SUPPORTS_WEAK_HOOKS
83 // For some weak hooks that will be called very often and we want to avoid the
84 // overhead of executing the default implementation when it is not necessary,
85 // we can use the flag SANITIZER_SUPPORTS_WEAK_HOOKS to only define the default
86 // implementation for platforms that doesn't support weak symbols. For example:
88 // #if !SANITIZER_SUPPORT_WEAK_HOOKS
89 // SANITIZER_INTERFACE_WEAK_DEF(bool, compare_hook, int a, int b) {
94 // And then use it as: if (compare_hook) compare_hook(a, b);
95 //----------------------------------------------------------------------------//
98 // We can use .preinit_array section on Linux to call sanitizer initialization
99 // functions very early in the process startup (unless PIC macro is defined).
101 // On FreeBSD, .preinit_array functions are called with rtld_bind_lock writer
102 // lock held. It will lead to dead lock if unresolved PLT functions (which helds
103 // rtld_bind_lock reader lock) are called inside .preinit_array functions.
105 // FIXME: do we have anything like this on Mac?
106 #ifndef SANITIZER_CAN_USE_PREINIT_ARRAY
107 #if (SANITIZER_LINUX || SANITIZER_FUCHSIA || SANITIZER_NETBSD) && !defined(PIC)
108 #define SANITIZER_CAN_USE_PREINIT_ARRAY 1
109 // Before Solaris 11.4, .preinit_array is fully supported only with GNU ld.
110 // FIXME: Check for those conditions.
111 #elif SANITIZER_SOLARIS && !defined(PIC)
112 # define SANITIZER_CAN_USE_PREINIT_ARRAY 1
114 # define SANITIZER_CAN_USE_PREINIT_ARRAY 0
116 #endif // SANITIZER_CAN_USE_PREINIT_ARRAY
118 // GCC does not understand __has_feature
119 #if !defined(__has_feature)
120 # define __has_feature(x) 0
123 // Older GCCs do not understand __has_attribute.
124 #if !defined(__has_attribute)
125 # define __has_attribute(x) 0
128 #if !defined(__has_cpp_attribute)
129 # define __has_cpp_attribute(x) 0
132 // For portability reasons we do not include stddef.h, stdint.h or any other
133 // system header, but we do need some basic types that are not defined
134 // in a portable way by the language itself.
135 namespace __sanitizer
{
138 // 64-bit Windows uses LLP64 data model.
139 typedef unsigned long long uptr
;
140 typedef signed long long sptr
;
142 # if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC || SANITIZER_WINDOWS
143 typedef unsigned long uptr
;
144 typedef signed long sptr
;
146 typedef unsigned int uptr
;
147 typedef signed int sptr
;
149 #endif // defined(_WIN64)
150 #if defined(__x86_64__)
151 // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
152 // 64-bit pointer to unwind stack frame.
153 typedef unsigned long long uhwptr
;
157 typedef unsigned char u8
;
158 typedef unsigned short u16
;
159 typedef unsigned int u32
;
160 typedef unsigned long long u64
;
161 typedef signed char s8
;
162 typedef signed short s16
;
163 typedef signed int s32
;
164 typedef signed long long s64
;
165 #if SANITIZER_WINDOWS
166 // On Windows, files are HANDLE, which is a synonim of void*.
167 // Use void* to avoid including <windows.h> everywhere.
169 typedef unsigned error_t
;
174 #if SANITIZER_SOLARIS && !defined(_LP64)
180 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_MAC || \
181 (SANITIZER_SOLARIS && (defined(_LP64) || _FILE_OFFSET_BITS == 64)) || \
182 (SANITIZER_LINUX && !SANITIZER_GLIBC && !SANITIZER_ANDROID) || \
183 (SANITIZER_LINUX && (defined(__x86_64__) || defined(__hexagon__)))
190 #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
191 typedef uptr operator_new_size_type
;
193 # if defined(__s390__) && !defined(__s390x__)
194 // Special case: 31-bit s390 has unsigned long as size_t.
195 typedef unsigned long operator_new_size_type
;
197 typedef u32 operator_new_size_type
;
203 // ----------- ATTENTION -------------
204 // This header should NOT include any other headers to avoid portability issues.
207 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
208 #define SANITIZER_WEAK_DEFAULT_IMPL \
209 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
210 #define SANITIZER_WEAK_CXX_DEFAULT_IMPL \
211 extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
213 // Platform-specific defs.
214 #if defined(_MSC_VER)
215 # define ALWAYS_INLINE __forceinline
216 // FIXME(timurrrr): do we need this on Windows?
218 # define ALIGNED(x) __declspec(align(x))
219 # define FORMAT(f, a)
220 # define NOINLINE __declspec(noinline)
221 # define NORETURN __declspec(noreturn)
222 # define THREADLOCAL __declspec(thread)
223 # define LIKELY(x) (x)
224 # define UNLIKELY(x) (x)
225 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0
226 # define WARN_UNUSED_RESULT
228 # define ALWAYS_INLINE inline __attribute__((always_inline))
229 # define ALIAS(x) __attribute__((alias(x)))
230 // Please only use the ALIGNED macro before the type.
231 // Using ALIGNED after the variable declaration is not portable!
232 # define ALIGNED(x) __attribute__((aligned(x)))
233 # define FORMAT(f, a) __attribute__((format(printf, f, a)))
234 # define NOINLINE __attribute__((noinline))
235 # define NORETURN __attribute__((noreturn))
236 # define THREADLOCAL __thread
237 # define LIKELY(x) __builtin_expect(!!(x), 1)
238 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
239 # if defined(__i386__) || defined(__x86_64__)
240 // __builtin_prefetch(x) generates prefetchnt0 on x86
241 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
243 # define PREFETCH(x) __builtin_prefetch(x)
245 # define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
248 #if !defined(_MSC_VER) || defined(__clang__)
249 # define UNUSED __attribute__((unused))
250 # define USED __attribute__((used))
256 #if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)
257 # define NOEXCEPT noexcept
259 # define NOEXCEPT throw()
262 #if __has_cpp_attribute(clang::fallthrough)
263 # define FALLTHROUGH [[clang::fallthrough]]
268 // Unaligned versions of basic types.
269 typedef ALIGNED(1) u16 uu16
;
270 typedef ALIGNED(1) u32 uu32
;
271 typedef ALIGNED(1) u64 uu64
;
272 typedef ALIGNED(1) s16 us16
;
273 typedef ALIGNED(1) s32 us32
;
274 typedef ALIGNED(1) s64 us64
;
276 #if SANITIZER_WINDOWS
277 } // namespace __sanitizer
278 typedef unsigned long DWORD
;
279 namespace __sanitizer
{
280 typedef DWORD thread_return_t
;
281 # define THREAD_CALLING_CONV __stdcall
283 typedef void* thread_return_t
;
284 # define THREAD_CALLING_CONV
286 typedef thread_return_t (THREAD_CALLING_CONV
*thread_callback_t
)(void* arg
);
288 // NOTE: Functions below must be defined in each run-time.
291 void NORETURN
CheckFailed(const char *file
, int line
, const char *cond
,
295 #define RAW_CHECK_MSG(expr, msg, ...) \
297 if (UNLIKELY(!(expr))) { \
298 const char* msgs[] = {msg, __VA_ARGS__}; \
299 for (const char* m : msgs) RawWrite(m); \
304 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr "\n", )
305 #define RAW_CHECK_VA(expr, ...) RAW_CHECK_MSG(expr, #expr "\n", __VA_ARGS__)
307 #define CHECK_IMPL(c1, op, c2) \
309 __sanitizer::u64 v1 = (__sanitizer::u64)(c1); \
310 __sanitizer::u64 v2 = (__sanitizer::u64)(c2); \
311 if (UNLIKELY(!(v1 op v2))) \
312 __sanitizer::CheckFailed(__FILE__, __LINE__, \
313 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
317 #define CHECK(a) CHECK_IMPL((a), !=, 0)
318 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
319 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
320 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
321 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
322 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
323 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
326 #define DCHECK(a) CHECK(a)
327 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
328 #define DCHECK_NE(a, b) CHECK_NE(a, b)
329 #define DCHECK_LT(a, b) CHECK_LT(a, b)
330 #define DCHECK_LE(a, b) CHECK_LE(a, b)
331 #define DCHECK_GT(a, b) CHECK_GT(a, b)
332 #define DCHECK_GE(a, b) CHECK_GE(a, b)
335 #define DCHECK_EQ(a, b)
336 #define DCHECK_NE(a, b)
337 #define DCHECK_LT(a, b)
338 #define DCHECK_LE(a, b)
339 #define DCHECK_GT(a, b)
340 #define DCHECK_GE(a, b)
343 #define UNREACHABLE(msg) do { \
348 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
350 #define COMPILER_CHECK(pred) static_assert(pred, "")
352 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
354 // Limits for integral types. We have to redefine it in case we don't
355 // have stdint.h (like in Visual Studio 9).
358 #if SANITIZER_WORDSIZE == 64
359 # define __INT64_C(c) c ## L
360 # define __UINT64_C(c) c ## UL
362 # define __INT64_C(c) c ## LL
363 # define __UINT64_C(c) c ## ULL
364 #endif // SANITIZER_WORDSIZE == 64
366 #define INT32_MIN (-2147483647-1)
368 #define INT32_MAX (2147483647)
370 #define UINT32_MAX (4294967295U)
372 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
374 #define INT64_MAX (__INT64_C(9223372036854775807))
376 #define UINT64_MAX (__UINT64_C(18446744073709551615))
378 #if SANITIZER_WORDSIZE == 64
379 # define UINTPTR_MAX (18446744073709551615UL)
381 # define UINTPTR_MAX (4294967295U)
382 #endif // SANITIZER_WORDSIZE == 64
384 enum LinkerInitialized
{ LINKER_INITIALIZED
= 0 };
386 #if !defined(_MSC_VER) || defined(__clang__)
387 #if SANITIZER_S390_31
388 #define GET_CALLER_PC() \
389 (__sanitizer::uptr) __builtin_extract_return_addr(__builtin_return_address(0))
391 #define GET_CALLER_PC() (__sanitizer::uptr) __builtin_return_address(0)
393 #define GET_CURRENT_FRAME() (__sanitizer::uptr) __builtin_frame_address(0)
398 extern "C" void* _ReturnAddress(void);
399 extern "C" void* _AddressOfReturnAddress(void);
400 # pragma intrinsic(_ReturnAddress)
401 # pragma intrinsic(_AddressOfReturnAddress)
402 #define GET_CALLER_PC() (__sanitizer::uptr) _ReturnAddress()
403 // CaptureStackBackTrace doesn't need to know BP on Windows.
404 #define GET_CURRENT_FRAME() \
405 (((__sanitizer::uptr)_AddressOfReturnAddress()) + sizeof(__sanitizer::uptr))
407 extern "C" void __ud2(void);
408 # pragma intrinsic(__ud2)
414 #define HANDLE_EINTR(res, f) \
419 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
422 // Forces the compiler to generate a frame pointer in the function.
423 #define ENABLE_FRAME_POINTER \
425 volatile __sanitizer::uptr enable_fp; \
426 enable_fp = GET_CURRENT_FRAME(); \
430 // Internal thread identifier allocated by ThreadRegistry.
432 constexpr Tid kInvalidTid
= -1;
433 constexpr Tid kMainTid
= 0;
435 // Stack depot stack identifier.
437 const StackID kInvalidStackID
= 0;
439 } // namespace __sanitizer
442 using namespace __sanitizer
;
445 using namespace __sanitizer
;
448 using namespace __sanitizer
;
451 using namespace __sanitizer
;
454 using namespace __sanitizer
;
457 using namespace __sanitizer
;
460 using namespace __sanitizer
;
463 using namespace __sanitizer
;
466 using namespace __sanitizer
;
469 using namespace __sanitizer
;
471 namespace __interception
{
472 using namespace __sanitizer
;
475 using namespace __sanitizer
;
477 namespace __memprof
{
478 using namespace __sanitizer
;
481 #endif // SANITIZER_DEFS_H