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 // Only use SANITIZER_*ATTRIBUTE* before the function return type!
20 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport)
21 // FIXME find out what we need on Windows, if anything.
22 # define SANITIZER_WEAK_ATTRIBUTE
23 #elif defined(SANITIZER_GO)
24 # define SANITIZER_INTERFACE_ATTRIBUTE
25 # define SANITIZER_WEAK_ATTRIBUTE
27 # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
28 # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
31 #if SANITIZER_LINUX && !defined(SANITIZER_GO)
32 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
34 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
37 // GCC does not understand __has_feature
38 #if !defined(__has_feature)
39 # define __has_feature(x) 0
42 // For portability reasons we do not include stddef.h, stdint.h or any other
43 // system header, but we do need some basic types that are not defined
44 // in a portable way by the language itself.
45 namespace __sanitizer
{
48 // 64-bit Windows uses LLP64 data model.
49 typedef unsigned long long uptr
; // NOLINT
50 typedef signed long long sptr
; // NOLINT
52 typedef unsigned long uptr
; // NOLINT
53 typedef signed long sptr
; // NOLINT
54 #endif // defined(_WIN64)
55 #if defined(__x86_64__)
56 // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
57 // 64-bit pointer to unwind stack frame.
58 typedef unsigned long long uhwptr
; // NOLINT
60 typedef uptr uhwptr
; // NOLINT
62 typedef unsigned char u8
;
63 typedef unsigned short u16
; // NOLINT
64 typedef unsigned int u32
;
65 typedef unsigned long long u64
; // NOLINT
66 typedef signed char s8
;
67 typedef signed short s16
; // NOLINT
68 typedef signed int s32
;
69 typedef signed long long s64
; // NOLINT
72 // WARNING: OFF_T may be different from OS type off_t, depending on the value of
73 // _FILE_OFFSET_BITS. This definition of OFF_T matches the ABI of system calls
74 // like pread and mmap, as opposed to pread64 and mmap64.
75 // Mac and Linux/x86-64 are special.
76 #if SANITIZER_MAC || (SANITIZER_LINUX && defined(__x86_64__))
82 } // namespace __sanitizer
85 // Tell the tools to write their reports to "path.<pid>" instead of stderr.
86 SANITIZER_INTERFACE_ATTRIBUTE
87 void __sanitizer_set_report_path(const char *path
);
89 // Tell the tools to write their reports to given file descriptor instead of
91 SANITIZER_INTERFACE_ATTRIBUTE
92 void __sanitizer_set_report_fd(int fd
);
94 // Notify the tools that the sandbox is going to be turned on. The reserved
95 // parameter will be used in the future to hold a structure with functions
96 // that the tools may call to bypass the sandbox.
97 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
98 void __sanitizer_sandbox_on_notify(void *reserved
);
100 // This function is called by the tool when it has just finished reporting
101 // an error. 'error_summary' is a one-line string that summarizes
102 // the error message. This function can be overridden by the client.
103 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
104 void __sanitizer_report_error_summary(const char *error_summary
);
108 using namespace __sanitizer
; // NOLINT
109 // ----------- ATTENTION -------------
110 // This header should NOT include any other headers to avoid portability issues.
113 #define INLINE inline
114 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
115 #define WEAK SANITIZER_WEAK_ATTRIBUTE
117 // Platform-specific defs.
118 #if defined(_MSC_VER)
119 # define ALWAYS_INLINE __forceinline
120 // FIXME(timurrrr): do we need this on Windows?
122 # define ALIGNED(x) __declspec(align(x))
123 # define FORMAT(f, a)
124 # define NOINLINE __declspec(noinline)
125 # define NORETURN __declspec(noreturn)
126 # define THREADLOCAL __declspec(thread)
128 # define LIKELY(x) (x)
129 # define UNLIKELY(x) (x)
132 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */
134 # define ALWAYS_INLINE inline __attribute__((always_inline))
135 # define ALIAS(x) __attribute__((alias(x)))
136 // Please only use the ALIGNED macro before the type.
137 // Using ALIGNED after the variable declaration is not portable!
138 # define ALIGNED(x) __attribute__((aligned(x)))
139 # define FORMAT(f, a) __attribute__((format(printf, f, a)))
140 # define NOINLINE __attribute__((noinline))
141 # define NORETURN __attribute__((noreturn))
142 # define THREADLOCAL __thread
143 # define NOTHROW throw()
144 # define LIKELY(x) __builtin_expect(!!(x), 1)
145 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
146 # define UNUSED __attribute__((unused))
147 # define USED __attribute__((used))
148 # if defined(__i386__) || defined(__x86_64__)
149 // __builtin_prefetch(x) generates prefetchnt0 on x86
150 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
152 # define PREFETCH(x) __builtin_prefetch(x)
156 // Unaligned versions of basic types.
157 typedef ALIGNED(1) u16 uu16
;
158 typedef ALIGNED(1) u32 uu32
;
159 typedef ALIGNED(1) u64 uu64
;
160 typedef ALIGNED(1) s16 us16
;
161 typedef ALIGNED(1) s32 us32
;
162 typedef ALIGNED(1) s64 us64
;
164 #if SANITIZER_WINDOWS
165 typedef unsigned long DWORD
; // NOLINT
166 typedef DWORD thread_return_t
;
167 # define THREAD_CALLING_CONV __stdcall
169 typedef void* thread_return_t
;
170 # define THREAD_CALLING_CONV
172 typedef thread_return_t (THREAD_CALLING_CONV
*thread_callback_t
)(void* arg
);
174 #if __LP64__ || defined(_WIN64)
175 # define SANITIZER_WORDSIZE 64
177 # define SANITIZER_WORDSIZE 32
180 // NOTE: Functions below must be defined in each run-time.
181 namespace __sanitizer
{
183 void NORETURN
CheckFailed(const char *file
, int line
, const char *cond
,
185 } // namespace __sanitizer
188 #define RAW_CHECK_MSG(expr, msg) do { \
195 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
197 #define CHECK_IMPL(c1, op, c2) \
199 __sanitizer::u64 v1 = (u64)(c1); \
200 __sanitizer::u64 v2 = (u64)(c2); \
202 __sanitizer::CheckFailed(__FILE__, __LINE__, \
203 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
207 #define CHECK(a) CHECK_IMPL((a), !=, 0)
208 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
209 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
210 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
211 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
212 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
213 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
216 #define DCHECK(a) CHECK(a)
217 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
218 #define DCHECK_NE(a, b) CHECK_NE(a, b)
219 #define DCHECK_LT(a, b) CHECK_LT(a, b)
220 #define DCHECK_LE(a, b) CHECK_LE(a, b)
221 #define DCHECK_GT(a, b) CHECK_GT(a, b)
222 #define DCHECK_GE(a, b) CHECK_GE(a, b)
225 #define DCHECK_EQ(a, b)
226 #define DCHECK_NE(a, b)
227 #define DCHECK_LT(a, b)
228 #define DCHECK_LE(a, b)
229 #define DCHECK_GT(a, b)
230 #define DCHECK_GE(a, b)
233 #define UNREACHABLE(msg) do { \
238 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
240 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
242 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
244 #define IMPL_PASTE(a, b) a##b
245 #define IMPL_COMPILER_ASSERT(pred, line) \
246 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
248 // Limits for integral types. We have to redefine it in case we don't
249 // have stdint.h (like in Visual Studio 9).
252 #if SANITIZER_WORDSIZE == 64
253 # define __INT64_C(c) c ## L
254 # define __UINT64_C(c) c ## UL
256 # define __INT64_C(c) c ## LL
257 # define __UINT64_C(c) c ## ULL
258 #endif // SANITIZER_WORDSIZE == 64
260 #define INT32_MIN (-2147483647-1)
262 #define INT32_MAX (2147483647)
264 #define UINT32_MAX (4294967295U)
266 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
268 #define INT64_MAX (__INT64_C(9223372036854775807))
270 #define UINT64_MAX (__UINT64_C(18446744073709551615))
272 enum LinkerInitialized
{ LINKER_INITIALIZED
= 0 };
274 #if !defined(_MSC_VER) || defined(__clang__)
275 # define GET_CALLER_PC() (uptr)__builtin_return_address(0)
276 # define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
278 extern "C" void* _ReturnAddress(void);
279 # pragma intrinsic(_ReturnAddress)
280 # define GET_CALLER_PC() (uptr)_ReturnAddress()
281 // CaptureStackBackTrace doesn't need to know BP on Windows.
282 // FIXME: This macro is still used when printing error reports though it's not
283 // clear if the BP value is needed in the ASan reports on Windows.
284 # define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
287 #define HANDLE_EINTR(res, f) \
292 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
295 #endif // SANITIZER_DEFS_H