1 //===-- sanitizer_internal_defs.h -------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is shared between AddressSanitizer and ThreadSanitizer.
9 // It contains macro used in run-time libraries code.
10 //===----------------------------------------------------------------------===//
11 #ifndef SANITIZER_DEFS_H
12 #define SANITIZER_DEFS_H
15 // FIXME find out what we need on Windows. __declspec(dllexport) ?
16 # define SANITIZER_INTERFACE_ATTRIBUTE
17 # define SANITIZER_WEAK_ATTRIBUTE
18 #elif defined(SANITIZER_GO)
19 # define SANITIZER_INTERFACE_ATTRIBUTE
20 # define SANITIZER_WEAK_ATTRIBUTE
22 # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
23 # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
27 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
29 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
32 // GCC does not understand __has_feature
33 #if !defined(__has_feature)
34 # define __has_feature(x) 0
37 // For portability reasons we do not include stddef.h, stdint.h or any other
38 // system header, but we do need some basic types that are not defined
39 // in a portable way by the language itself.
40 namespace __sanitizer
{
43 // 64-bit Windows uses LLP64 data model.
44 typedef unsigned long long uptr
; // NOLINT
45 typedef signed long long sptr
; // NOLINT
47 typedef unsigned long uptr
; // NOLINT
48 typedef signed long sptr
; // NOLINT
49 #endif // defined(_WIN64)
50 #if defined(__x86_64__)
51 // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
52 // 64-bit pointer to unwind stack frame.
53 typedef unsigned long long uhwptr
; // NOLINT
55 typedef uptr uhwptr
; // NOLINT
57 typedef unsigned char u8
;
58 typedef unsigned short u16
; // NOLINT
59 typedef unsigned int u32
;
60 typedef unsigned long long u64
; // NOLINT
61 typedef signed char s8
;
62 typedef signed short s16
; // NOLINT
63 typedef signed int s32
;
64 typedef signed long long s64
; // NOLINT
67 } // namespace __sanitizer
70 // Tell the tools to write their reports to "path.<pid>" instead of stderr.
71 void __sanitizer_set_report_path(const char *path
)
72 SANITIZER_INTERFACE_ATTRIBUTE
;
74 // Tell the tools to write their reports to given file descriptor instead of
76 void __sanitizer_set_report_fd(int fd
)
77 SANITIZER_INTERFACE_ATTRIBUTE
;
79 // Notify the tools that the sandbox is going to be turned on. The reserved
80 // parameter will be used in the future to hold a structure with functions
81 // that the tools may call to bypass the sandbox.
82 void __sanitizer_sandbox_on_notify(void *reserved
)
83 SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
;
85 // This function is called by the tool when it has just finished reporting
86 // an error. 'error_summary' is a one-line string that summarizes
87 // the error message. This function can be overridden by the client.
88 void __sanitizer_report_error_summary(const char *error_summary
)
89 SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
;
93 using namespace __sanitizer
; // NOLINT
94 // ----------- ATTENTION -------------
95 // This header should NOT include any other headers to avoid portability issues.
98 #define INLINE static inline
99 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
100 #define WEAK SANITIZER_WEAK_ATTRIBUTE
102 // Platform-specific defs.
103 #if defined(_MSC_VER)
104 # define ALWAYS_INLINE __declspec(forceinline)
105 // FIXME(timurrrr): do we need this on Windows?
107 # define ALIGNED(x) __declspec(align(x))
108 # define FORMAT(f, a)
109 # define NOINLINE __declspec(noinline)
110 # define NORETURN __declspec(noreturn)
111 # define THREADLOCAL __declspec(thread)
113 # define LIKELY(x) (x)
114 # define UNLIKELY(x) (x)
117 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */
119 # define ALWAYS_INLINE __attribute__((always_inline))
120 # define ALIAS(x) __attribute__((alias(x)))
121 # define ALIGNED(x) __attribute__((aligned(x)))
122 # define FORMAT(f, a) __attribute__((format(printf, f, a)))
123 # define NOINLINE __attribute__((noinline))
124 # define NORETURN __attribute__((noreturn))
125 # define THREADLOCAL __thread
126 # define NOTHROW throw()
127 # define LIKELY(x) __builtin_expect(!!(x), 1)
128 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
129 # define UNUSED __attribute__((unused))
130 # define USED __attribute__((used))
131 # if defined(__i386__) || defined(__x86_64__)
132 // __builtin_prefetch(x) generates prefetchnt0 on x86
133 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
135 # define PREFETCH(x) __builtin_prefetch(x)
140 typedef unsigned long DWORD
; // NOLINT
141 typedef DWORD thread_return_t
;
142 # define THREAD_CALLING_CONV __stdcall
144 typedef void* thread_return_t
;
145 # define THREAD_CALLING_CONV
147 typedef thread_return_t (THREAD_CALLING_CONV
*thread_callback_t
)(void* arg
);
149 #if __LP64__ || defined(_WIN64)
150 # define SANITIZER_WORDSIZE 64
152 # define SANITIZER_WORDSIZE 32
155 // NOTE: Functions below must be defined in each run-time.
156 namespace __sanitizer
{
158 void NORETURN
CheckFailed(const char *file
, int line
, const char *cond
,
160 } // namespace __sanitizer
163 #define RAW_CHECK_MSG(expr, msg) do { \
170 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
172 #define CHECK_IMPL(c1, op, c2) \
174 __sanitizer::u64 v1 = (u64)(c1); \
175 __sanitizer::u64 v2 = (u64)(c2); \
177 __sanitizer::CheckFailed(__FILE__, __LINE__, \
178 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
182 #define CHECK(a) CHECK_IMPL((a), !=, 0)
183 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
184 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
185 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
186 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
187 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
188 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
191 #define DCHECK(a) CHECK(a)
192 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
193 #define DCHECK_NE(a, b) CHECK_NE(a, b)
194 #define DCHECK_LT(a, b) CHECK_LT(a, b)
195 #define DCHECK_LE(a, b) CHECK_LE(a, b)
196 #define DCHECK_GT(a, b) CHECK_GT(a, b)
197 #define DCHECK_GE(a, b) CHECK_GE(a, b)
200 #define DCHECK_EQ(a, b)
201 #define DCHECK_NE(a, b)
202 #define DCHECK_LT(a, b)
203 #define DCHECK_LE(a, b)
204 #define DCHECK_GT(a, b)
205 #define DCHECK_GE(a, b)
208 #define UNREACHABLE(msg) do { \
213 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
215 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
217 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
219 #define IMPL_PASTE(a, b) a##b
220 #define IMPL_COMPILER_ASSERT(pred, line) \
221 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
223 // Limits for integral types. We have to redefine it in case we don't
224 // have stdint.h (like in Visual Studio 9).
227 #if SANITIZER_WORDSIZE == 64
228 # define __INT64_C(c) c ## L
229 # define __UINT64_C(c) c ## UL
231 # define __INT64_C(c) c ## LL
232 # define __UINT64_C(c) c ## ULL
233 #endif // SANITIZER_WORDSIZE == 64
235 #define INT32_MIN (-2147483647-1)
237 #define INT32_MAX (2147483647)
239 #define UINT32_MAX (4294967295U)
241 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
243 #define INT64_MAX (__INT64_C(9223372036854775807))
245 #define UINT64_MAX (__UINT64_C(18446744073709551615))
247 enum LinkerInitialized
{ LINKER_INITIALIZED
= 0 };
249 #if !defined(_MSC_VER) || defined(__clang__)
250 # define GET_CALLER_PC() (uptr)__builtin_return_address(0)
251 # define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
253 extern "C" void* _ReturnAddress(void);
254 # pragma intrinsic(_ReturnAddress)
255 # define GET_CALLER_PC() (uptr)_ReturnAddress()
256 // CaptureStackBackTrace doesn't need to know BP on Windows.
257 // FIXME: This macro is still used when printing error reports though it's not
258 // clear if the BP value is needed in the ASan reports on Windows.
259 # define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
262 #define HANDLE_EINTR(res, f) { \
265 } while (res == -1 && errno == EINTR); \
268 #endif // SANITIZER_DEFS_H