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
14 #include "sanitizer_platform.h"
16 // Only use SANITIZER_*ATTRIBUTE* before the function return type!
18 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport)
19 // FIXME find out what we need on Windows, if anything.
20 # define SANITIZER_WEAK_ATTRIBUTE
21 #elif defined(SANITIZER_GO)
22 # define SANITIZER_INTERFACE_ATTRIBUTE
23 # define SANITIZER_WEAK_ATTRIBUTE
25 # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
26 # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
29 #if SANITIZER_LINUX && !defined(SANITIZER_GO)
30 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
32 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
35 // If set, the tool will install its own SEGV signal handler.
36 #ifndef SANITIZER_NEEDS_SEGV
37 # define SANITIZER_NEEDS_SEGV 1
40 // GCC does not understand __has_feature
41 #if !defined(__has_feature)
42 # define __has_feature(x) 0
45 // For portability reasons we do not include stddef.h, stdint.h or any other
46 // system header, but we do need some basic types that are not defined
47 // in a portable way by the language itself.
48 namespace __sanitizer
{
51 // 64-bit Windows uses LLP64 data model.
52 typedef unsigned long long uptr
; // NOLINT
53 typedef signed long long sptr
; // NOLINT
55 typedef unsigned long uptr
; // NOLINT
56 typedef signed long sptr
; // NOLINT
57 #endif // defined(_WIN64)
58 #if defined(__x86_64__)
59 // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
60 // 64-bit pointer to unwind stack frame.
61 typedef unsigned long long uhwptr
; // NOLINT
63 typedef uptr uhwptr
; // NOLINT
65 typedef unsigned char u8
;
66 typedef unsigned short u16
; // NOLINT
67 typedef unsigned int u32
;
68 typedef unsigned long long u64
; // NOLINT
69 typedef signed char s8
;
70 typedef signed short s16
; // NOLINT
71 typedef signed int s32
;
72 typedef signed long long s64
; // NOLINT
75 // WARNING: OFF_T may be different from OS type off_t, depending on the value of
76 // _FILE_OFFSET_BITS. This definition of OFF_T matches the ABI of system calls
77 // like pread and mmap, as opposed to pread64 and mmap64.
78 // Mac and Linux/x86-64 are special.
79 #if SANITIZER_MAC || (SANITIZER_LINUX && defined(__x86_64__))
86 #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
87 typedef uptr operator_new_size_type
;
89 typedef u32 operator_new_size_type
;
91 } // namespace __sanitizer
94 // Tell the tools to write their reports to "path.<pid>" instead of stderr.
95 // The special values are "stdout" and "stderr".
96 SANITIZER_INTERFACE_ATTRIBUTE
97 void __sanitizer_set_report_path(const char *path
);
100 int coverage_sandboxed
;
101 __sanitizer::sptr coverage_fd
;
102 unsigned int coverage_max_block_size
;
103 } __sanitizer_sandbox_arguments
;
105 // Notify the tools that the sandbox is going to be turned on.
106 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
void
107 __sanitizer_sandbox_on_notify(__sanitizer_sandbox_arguments
*args
);
109 // This function is called by the tool when it has just finished reporting
110 // an error. 'error_summary' is a one-line string that summarizes
111 // the error message. This function can be overridden by the client.
112 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
113 void __sanitizer_report_error_summary(const char *error_summary
);
115 SANITIZER_INTERFACE_ATTRIBUTE
void __sanitizer_cov_dump();
116 SANITIZER_INTERFACE_ATTRIBUTE
void __sanitizer_cov_init();
117 SANITIZER_INTERFACE_ATTRIBUTE
void __sanitizer_cov();
118 SANITIZER_INTERFACE_ATTRIBUTE
119 void __sanitizer_annotate_contiguous_container(const void *beg
,
122 const void *new_mid
);
123 SANITIZER_INTERFACE_ATTRIBUTE
124 int __sanitizer_verify_contiguous_container(const void *beg
, const void *mid
,
129 using namespace __sanitizer
; // NOLINT
130 // ----------- ATTENTION -------------
131 // This header should NOT include any other headers to avoid portability issues.
134 #define INLINE inline
135 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
136 #define WEAK SANITIZER_WEAK_ATTRIBUTE
138 // Platform-specific defs.
139 #if defined(_MSC_VER)
140 # define ALWAYS_INLINE __forceinline
141 // FIXME(timurrrr): do we need this on Windows?
143 # define ALIGNED(x) __declspec(align(x))
144 # define FORMAT(f, a)
145 # define NOINLINE __declspec(noinline)
146 # define NORETURN __declspec(noreturn)
147 # define THREADLOCAL __declspec(thread)
149 # define LIKELY(x) (x)
150 # define UNLIKELY(x) (x)
151 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */
153 # define ALWAYS_INLINE inline __attribute__((always_inline))
154 # define ALIAS(x) __attribute__((alias(x)))
155 // Please only use the ALIGNED macro before the type.
156 // Using ALIGNED after the variable declaration is not portable!
157 # define ALIGNED(x) __attribute__((aligned(x)))
158 # define FORMAT(f, a) __attribute__((format(printf, f, a)))
159 # define NOINLINE __attribute__((noinline))
160 # define NORETURN __attribute__((noreturn))
161 # define THREADLOCAL __thread
162 # define NOTHROW throw()
163 # define LIKELY(x) __builtin_expect(!!(x), 1)
164 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
165 # if defined(__i386__) || defined(__x86_64__)
166 // __builtin_prefetch(x) generates prefetchnt0 on x86
167 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
169 # define PREFETCH(x) __builtin_prefetch(x)
173 #if !defined(_MSC_VER) || defined(__clang__)
174 # define UNUSED __attribute__((unused))
175 # define USED __attribute__((used))
181 // Unaligned versions of basic types.
182 typedef ALIGNED(1) u16 uu16
;
183 typedef ALIGNED(1) u32 uu32
;
184 typedef ALIGNED(1) u64 uu64
;
185 typedef ALIGNED(1) s16 us16
;
186 typedef ALIGNED(1) s32 us32
;
187 typedef ALIGNED(1) s64 us64
;
189 #if SANITIZER_WINDOWS
190 typedef unsigned long DWORD
; // NOLINT
191 typedef DWORD thread_return_t
;
192 # define THREAD_CALLING_CONV __stdcall
194 typedef void* thread_return_t
;
195 # define THREAD_CALLING_CONV
197 typedef thread_return_t (THREAD_CALLING_CONV
*thread_callback_t
)(void* arg
);
199 // NOTE: Functions below must be defined in each run-time.
200 namespace __sanitizer
{
203 // FIXME: No, this shouldn't be in the sanitizer interface.
204 SANITIZER_INTERFACE_ATTRIBUTE
205 void NORETURN
CheckFailed(const char *file
, int line
, const char *cond
,
207 } // namespace __sanitizer
210 #define RAW_CHECK_MSG(expr, msg) do { \
211 if (UNLIKELY(!(expr))) { \
217 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
219 #define CHECK_IMPL(c1, op, c2) \
221 __sanitizer::u64 v1 = (u64)(c1); \
222 __sanitizer::u64 v2 = (u64)(c2); \
223 if (UNLIKELY(!(v1 op v2))) \
224 __sanitizer::CheckFailed(__FILE__, __LINE__, \
225 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
229 #define CHECK(a) CHECK_IMPL((a), !=, 0)
230 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
231 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
232 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
233 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
234 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
235 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
238 #define DCHECK(a) CHECK(a)
239 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
240 #define DCHECK_NE(a, b) CHECK_NE(a, b)
241 #define DCHECK_LT(a, b) CHECK_LT(a, b)
242 #define DCHECK_LE(a, b) CHECK_LE(a, b)
243 #define DCHECK_GT(a, b) CHECK_GT(a, b)
244 #define DCHECK_GE(a, b) CHECK_GE(a, b)
247 #define DCHECK_EQ(a, b)
248 #define DCHECK_NE(a, b)
249 #define DCHECK_LT(a, b)
250 #define DCHECK_LE(a, b)
251 #define DCHECK_GT(a, b)
252 #define DCHECK_GE(a, b)
255 #define UNREACHABLE(msg) do { \
260 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
262 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
264 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
266 #define IMPL_PASTE(a, b) a##b
267 #define IMPL_COMPILER_ASSERT(pred, line) \
268 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
270 // Limits for integral types. We have to redefine it in case we don't
271 // have stdint.h (like in Visual Studio 9).
274 #if SANITIZER_WORDSIZE == 64
275 # define __INT64_C(c) c ## L
276 # define __UINT64_C(c) c ## UL
278 # define __INT64_C(c) c ## LL
279 # define __UINT64_C(c) c ## ULL
280 #endif // SANITIZER_WORDSIZE == 64
282 #define INT32_MIN (-2147483647-1)
284 #define INT32_MAX (2147483647)
286 #define UINT32_MAX (4294967295U)
288 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
290 #define INT64_MAX (__INT64_C(9223372036854775807))
292 #define UINT64_MAX (__UINT64_C(18446744073709551615))
294 enum LinkerInitialized
{ LINKER_INITIALIZED
= 0 };
296 #if !defined(_MSC_VER) || defined(__clang__)
297 # define GET_CALLER_PC() (uptr)__builtin_return_address(0)
298 # define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
300 extern "C" void* _ReturnAddress(void);
301 # pragma intrinsic(_ReturnAddress)
302 # define GET_CALLER_PC() (uptr)_ReturnAddress()
303 // CaptureStackBackTrace doesn't need to know BP on Windows.
304 // FIXME: This macro is still used when printing error reports though it's not
305 // clear if the BP value is needed in the ASan reports on Windows.
306 # define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
309 #define HANDLE_EINTR(res, f) \
314 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
317 #endif // SANITIZER_DEFS_H