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
17 // FIXME find out what we need on Windows. __declspec(dllexport) ?
18 # define SANITIZER_INTERFACE_ATTRIBUTE
19 # define SANITIZER_WEAK_ATTRIBUTE
20 #elif defined(SANITIZER_GO)
21 # define SANITIZER_INTERFACE_ATTRIBUTE
22 # define SANITIZER_WEAK_ATTRIBUTE
24 # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
25 # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
29 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
31 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
35 #if !defined(__has_feature)
36 # define __has_feature(x) 0
39 // For portability reasons we do not include stddef.h, stdint.h or any other
40 // system header, but we do need some basic types that are not defined
41 // in a portable way by the language itself.
42 namespace __sanitizer
{
45 // 64-bit Windows uses LLP64 data model.
46 typedef unsigned long long uptr
; // NOLINT
47 typedef signed long long sptr
; // NOLINT
49 typedef unsigned long uptr
; // NOLINT
50 typedef signed long sptr
; // NOLINT
51 #endif // defined(_WIN64)
52 #if defined(__x86_64__)
53 // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
54 // 64-bit pointer to unwind stack frame.
55 typedef unsigned long long uhwptr
; // NOLINT
57 typedef uptr uhwptr
; // NOLINT
59 typedef unsigned char u8
;
60 typedef unsigned short u16
; // NOLINT
61 typedef unsigned int u32
;
62 typedef unsigned long long u64
; // NOLINT
63 typedef signed char s8
;
64 typedef signed short s16
; // NOLINT
65 typedef signed int s32
;
66 typedef signed long long s64
; // NOLINT
69 } // namespace __sanitizer
72 // Tell the tools to write their reports to "path.<pid>" instead of stderr.
73 void __sanitizer_set_report_path(const char *path
)
74 SANITIZER_INTERFACE_ATTRIBUTE
;
76 // Tell the tools to write their reports to given file descriptor instead of
78 void __sanitizer_set_report_fd(int fd
)
79 SANITIZER_INTERFACE_ATTRIBUTE
;
81 // Notify the tools that the sandbox is going to be turned on. The reserved
82 // parameter will be used in the future to hold a structure with functions
83 // that the tools may call to bypass the sandbox.
84 void __sanitizer_sandbox_on_notify(void *reserved
)
85 SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
;
89 using namespace __sanitizer
; // NOLINT
90 // ----------- ATTENTION -------------
91 // This header should NOT include any other headers to avoid portability issues.
94 #define INLINE static inline
95 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
96 #define WEAK SANITIZER_WEAK_ATTRIBUTE
98 // Platform-specific defs.
100 # define ALWAYS_INLINE __declspec(forceinline)
101 // FIXME(timurrrr): do we need this on Windows?
103 # define ALIGNED(x) __declspec(align(x))
104 # define FORMAT(f, a)
105 # define NOINLINE __declspec(noinline)
106 # define NORETURN __declspec(noreturn)
107 # define THREADLOCAL __declspec(thread)
109 # define LIKELY(x) (x)
110 # define UNLIKELY(x) (x)
113 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */
115 # define ALWAYS_INLINE __attribute__((always_inline))
116 # define ALIAS(x) __attribute__((alias(x)))
117 # define ALIGNED(x) __attribute__((aligned(x)))
118 # define FORMAT(f, a) __attribute__((format(printf, f, a)))
119 # define NOINLINE __attribute__((noinline))
120 # define NORETURN __attribute__((noreturn))
121 # define THREADLOCAL __thread
122 # define NOTHROW throw()
123 # define LIKELY(x) __builtin_expect(!!(x), 1)
124 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
125 # define UNUSED __attribute__((unused))
126 # define USED __attribute__((used))
127 # if defined(__i386__) || defined(__x86_64__)
128 // __builtin_prefetch(x) generates prefetchnt0 on x86
129 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
131 # define PREFETCH(x) __builtin_prefetch(x)
136 typedef unsigned long DWORD
; // NOLINT
137 typedef DWORD thread_return_t
;
138 # define THREAD_CALLING_CONV __stdcall
140 typedef void* thread_return_t
;
141 # define THREAD_CALLING_CONV
143 typedef thread_return_t (THREAD_CALLING_CONV
*thread_callback_t
)(void* arg
);
145 #if __LP64__ || defined(_WIN64)
146 # define SANITIZER_WORDSIZE 64
148 # define SANITIZER_WORDSIZE 32
151 // NOTE: Functions below must be defined in each run-time.
152 namespace __sanitizer
{
154 void NORETURN
CheckFailed(const char *file
, int line
, const char *cond
,
156 } // namespace __sanitizer
159 #define RAW_CHECK_MSG(expr, msg) do { \
166 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
168 #define CHECK_IMPL(c1, op, c2) \
170 __sanitizer::u64 v1 = (u64)(c1); \
171 __sanitizer::u64 v2 = (u64)(c2); \
173 __sanitizer::CheckFailed(__FILE__, __LINE__, \
174 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
178 #define CHECK(a) CHECK_IMPL((a), !=, 0)
179 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
180 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
181 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
182 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
183 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
184 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
187 #define DCHECK(a) CHECK(a)
188 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
189 #define DCHECK_NE(a, b) CHECK_NE(a, b)
190 #define DCHECK_LT(a, b) CHECK_LT(a, b)
191 #define DCHECK_LE(a, b) CHECK_LE(a, b)
192 #define DCHECK_GT(a, b) CHECK_GT(a, b)
193 #define DCHECK_GE(a, b) CHECK_GE(a, b)
196 #define DCHECK_EQ(a, b)
197 #define DCHECK_NE(a, b)
198 #define DCHECK_LT(a, b)
199 #define DCHECK_LE(a, b)
200 #define DCHECK_GT(a, b)
201 #define DCHECK_GE(a, b)
204 #define UNREACHABLE(msg) do { \
209 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
211 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
213 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
215 #define IMPL_PASTE(a, b) a##b
216 #define IMPL_COMPILER_ASSERT(pred, line) \
217 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
219 // Limits for integral types. We have to redefine it in case we don't
220 // have stdint.h (like in Visual Studio 9).
223 #if SANITIZER_WORDSIZE == 64
224 # define __INT64_C(c) c ## L
225 # define __UINT64_C(c) c ## UL
227 # define __INT64_C(c) c ## LL
228 # define __UINT64_C(c) c ## ULL
229 #endif // SANITIZER_WORDSIZE == 64
231 #define INT32_MIN (-2147483647-1)
233 #define INT32_MAX (2147483647)
235 #define UINT32_MAX (4294967295U)
237 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
239 #define INT64_MAX (__INT64_C(9223372036854775807))
241 #define UINT64_MAX (__UINT64_C(18446744073709551615))
243 enum LinkerInitialized
{ LINKER_INITIALIZED
= 0 };
245 #if !defined(_MSC_VER) || defined(__clang__)
246 # define GET_CALLER_PC() (uptr)__builtin_return_address(0)
247 # define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
249 extern "C" void* _ReturnAddress(void);
250 # pragma intrinsic(_ReturnAddress)
251 # define GET_CALLER_PC() (uptr)_ReturnAddress()
252 // CaptureStackBackTrace doesn't need to know BP on Windows.
253 // FIXME: This macro is still used when printing error reports though it's not
254 // clear if the BP value is needed in the ASan reports on Windows.
255 # define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
258 #define HANDLE_EINTR(res, f) { \
261 } while (res == -1 && errno == EINTR); \
264 #endif // SANITIZER_DEFS_H