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 // For portability reasons we do not include stddef.h, stdint.h or any other
129 // system header, but we do need some basic types that are not defined
130 // in a portable way by the language itself.
131 namespace __sanitizer
{
134 // 64-bit Windows uses LLP64 data model.
135 typedef unsigned long long uptr
;
136 typedef signed long long sptr
;
138 typedef unsigned long uptr
;
139 typedef signed long sptr
;
140 #endif // defined(_WIN64)
141 #if defined(__x86_64__)
142 // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
143 // 64-bit pointer to unwind stack frame.
144 typedef unsigned long long uhwptr
;
148 typedef unsigned char u8
;
149 typedef unsigned short u16
;
150 typedef unsigned int u32
;
151 typedef unsigned long long u64
;
152 typedef signed char s8
;
153 typedef signed short s16
;
154 typedef signed int s32
;
155 typedef signed long long s64
;
156 #if SANITIZER_WINDOWS
157 // On Windows, files are HANDLE, which is a synonim of void*.
158 // Use void* to avoid including <windows.h> everywhere.
160 typedef unsigned error_t
;
165 #if SANITIZER_SOLARIS && !defined(_LP64)
171 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || \
173 (SANITIZER_SOLARIS && (defined(_LP64) || _FILE_OFFSET_BITS == 64)) || \
174 (SANITIZER_LINUX && defined(__x86_64__))
181 #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
182 typedef uptr operator_new_size_type
;
184 # if defined(__s390__) && !defined(__s390x__)
185 // Special case: 31-bit s390 has unsigned long as size_t.
186 typedef unsigned long operator_new_size_type
;
188 typedef u32 operator_new_size_type
;
194 // ----------- ATTENTION -------------
195 // This header should NOT include any other headers to avoid portability issues.
198 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
199 #define SANITIZER_WEAK_DEFAULT_IMPL \
200 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
201 #define SANITIZER_WEAK_CXX_DEFAULT_IMPL \
202 extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
204 // Platform-specific defs.
205 #if defined(_MSC_VER)
206 # define ALWAYS_INLINE __forceinline
207 // FIXME(timurrrr): do we need this on Windows?
209 # define ALIGNED(x) __declspec(align(x))
210 # define FORMAT(f, a)
211 # define NOINLINE __declspec(noinline)
212 # define NORETURN __declspec(noreturn)
213 # define THREADLOCAL __declspec(thread)
214 # define LIKELY(x) (x)
215 # define UNLIKELY(x) (x)
216 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0
217 # define WARN_UNUSED_RESULT
219 # define ALWAYS_INLINE inline __attribute__((always_inline))
220 # define ALIAS(x) __attribute__((alias(x)))
221 // Please only use the ALIGNED macro before the type.
222 // Using ALIGNED after the variable declaration is not portable!
223 # define ALIGNED(x) __attribute__((aligned(x)))
224 # define FORMAT(f, a) __attribute__((format(printf, f, a)))
225 # define NOINLINE __attribute__((noinline))
226 # define NORETURN __attribute__((noreturn))
227 # define THREADLOCAL __thread
228 # define LIKELY(x) __builtin_expect(!!(x), 1)
229 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
230 # if defined(__i386__) || defined(__x86_64__)
231 // __builtin_prefetch(x) generates prefetchnt0 on x86
232 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
234 # define PREFETCH(x) __builtin_prefetch(x)
236 # define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
239 #if !defined(_MSC_VER) || defined(__clang__)
240 # define UNUSED __attribute__((unused))
241 # define USED __attribute__((used))
247 #if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)
248 # define NOEXCEPT noexcept
250 # define NOEXCEPT throw()
253 // Unaligned versions of basic types.
254 typedef ALIGNED(1) u16 uu16
;
255 typedef ALIGNED(1) u32 uu32
;
256 typedef ALIGNED(1) u64 uu64
;
257 typedef ALIGNED(1) s16 us16
;
258 typedef ALIGNED(1) s32 us32
;
259 typedef ALIGNED(1) s64 us64
;
261 #if SANITIZER_WINDOWS
262 } // namespace __sanitizer
263 typedef unsigned long DWORD
;
264 namespace __sanitizer
{
265 typedef DWORD thread_return_t
;
266 # define THREAD_CALLING_CONV __stdcall
268 typedef void* thread_return_t
;
269 # define THREAD_CALLING_CONV
271 typedef thread_return_t (THREAD_CALLING_CONV
*thread_callback_t
)(void* arg
);
273 // NOTE: Functions below must be defined in each run-time.
276 void NORETURN
CheckFailed(const char *file
, int line
, const char *cond
,
280 #define RAW_CHECK_MSG(expr, msg) do { \
281 if (UNLIKELY(!(expr))) { \
287 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
289 #define CHECK_IMPL(c1, op, c2) \
291 __sanitizer::u64 v1 = (__sanitizer::u64)(c1); \
292 __sanitizer::u64 v2 = (__sanitizer::u64)(c2); \
293 if (UNLIKELY(!(v1 op v2))) \
294 __sanitizer::CheckFailed(__FILE__, __LINE__, \
295 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
299 #define CHECK(a) CHECK_IMPL((a), !=, 0)
300 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
301 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
302 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
303 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
304 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
305 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
308 #define DCHECK(a) CHECK(a)
309 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
310 #define DCHECK_NE(a, b) CHECK_NE(a, b)
311 #define DCHECK_LT(a, b) CHECK_LT(a, b)
312 #define DCHECK_LE(a, b) CHECK_LE(a, b)
313 #define DCHECK_GT(a, b) CHECK_GT(a, b)
314 #define DCHECK_GE(a, b) CHECK_GE(a, b)
317 #define DCHECK_EQ(a, b)
318 #define DCHECK_NE(a, b)
319 #define DCHECK_LT(a, b)
320 #define DCHECK_LE(a, b)
321 #define DCHECK_GT(a, b)
322 #define DCHECK_GE(a, b)
325 #define UNREACHABLE(msg) do { \
330 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
332 #define COMPILER_CHECK(pred) static_assert(pred, "")
334 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
336 // Limits for integral types. We have to redefine it in case we don't
337 // have stdint.h (like in Visual Studio 9).
340 #if SANITIZER_WORDSIZE == 64
341 # define __INT64_C(c) c ## L
342 # define __UINT64_C(c) c ## UL
344 # define __INT64_C(c) c ## LL
345 # define __UINT64_C(c) c ## ULL
346 #endif // SANITIZER_WORDSIZE == 64
348 #define INT32_MIN (-2147483647-1)
350 #define INT32_MAX (2147483647)
352 #define UINT32_MAX (4294967295U)
354 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
356 #define INT64_MAX (__INT64_C(9223372036854775807))
358 #define UINT64_MAX (__UINT64_C(18446744073709551615))
360 #if SANITIZER_WORDSIZE == 64
361 # define UINTPTR_MAX (18446744073709551615UL)
363 # define UINTPTR_MAX (4294967295U)
364 #endif // SANITIZER_WORDSIZE == 64
366 enum LinkerInitialized
{ LINKER_INITIALIZED
= 0 };
368 #if !defined(_MSC_VER) || defined(__clang__)
369 #if SANITIZER_S390_31
370 #define GET_CALLER_PC() \
371 (__sanitizer::uptr) __builtin_extract_return_addr(__builtin_return_address(0))
373 #define GET_CALLER_PC() (__sanitizer::uptr) __builtin_return_address(0)
375 #define GET_CURRENT_FRAME() (__sanitizer::uptr) __builtin_frame_address(0)
380 extern "C" void* _ReturnAddress(void);
381 extern "C" void* _AddressOfReturnAddress(void);
382 # pragma intrinsic(_ReturnAddress)
383 # pragma intrinsic(_AddressOfReturnAddress)
384 #define GET_CALLER_PC() (__sanitizer::uptr) _ReturnAddress()
385 // CaptureStackBackTrace doesn't need to know BP on Windows.
386 #define GET_CURRENT_FRAME() \
387 (((__sanitizer::uptr)_AddressOfReturnAddress()) + sizeof(__sanitizer::uptr))
389 extern "C" void __ud2(void);
390 # pragma intrinsic(__ud2)
396 #define HANDLE_EINTR(res, f) \
401 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
404 // Forces the compiler to generate a frame pointer in the function.
405 #define ENABLE_FRAME_POINTER \
407 volatile __sanitizer::uptr enable_fp; \
408 enable_fp = GET_CURRENT_FRAME(); \
412 } // namespace __sanitizer
415 using namespace __sanitizer
;
418 using namespace __sanitizer
;
421 using namespace __sanitizer
;
424 using namespace __sanitizer
;
427 using namespace __sanitizer
;
430 using namespace __sanitizer
;
433 using namespace __sanitizer
;
436 using namespace __sanitizer
;
439 using namespace __sanitizer
;
442 using namespace __sanitizer
;
444 namespace __interception
{
445 using namespace __sanitizer
;
448 using namespace __sanitizer
;
450 namespace __memprof
{
451 using namespace __sanitizer
;
454 #endif // SANITIZER_DEFS_H