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 // We can use .preinit_array section on Linux to call sanitizer initialization
36 // functions very early in the process startup (unless PIC macro is defined).
37 // FIXME: do we have anything like this on Mac?
38 #if SANITIZER_LINUX && !SANITIZER_ANDROID && !defined(PIC)
39 # define SANITIZER_CAN_USE_PREINIT_ARRAY 1
41 # define SANITIZER_CAN_USE_PREINIT_ARRAY 0
44 // GCC does not understand __has_feature
45 #if !defined(__has_feature)
46 # define __has_feature(x) 0
49 // For portability reasons we do not include stddef.h, stdint.h or any other
50 // system header, but we do need some basic types that are not defined
51 // in a portable way by the language itself.
52 namespace __sanitizer
{
55 // 64-bit Windows uses LLP64 data model.
56 typedef unsigned long long uptr
; // NOLINT
57 typedef signed long long sptr
; // NOLINT
59 typedef unsigned long uptr
; // NOLINT
60 typedef signed long sptr
; // NOLINT
61 #endif // defined(_WIN64)
62 #if defined(__x86_64__)
63 // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
64 // 64-bit pointer to unwind stack frame.
65 typedef unsigned long long uhwptr
; // NOLINT
67 typedef uptr uhwptr
; // NOLINT
69 typedef unsigned char u8
;
70 typedef unsigned short u16
; // NOLINT
71 typedef unsigned int u32
;
72 typedef unsigned long long u64
; // NOLINT
73 typedef signed char s8
;
74 typedef signed short s16
; // NOLINT
75 typedef signed int s32
;
76 typedef signed long long s64
; // NOLINT
79 // WARNING: OFF_T may be different from OS type off_t, depending on the value of
80 // _FILE_OFFSET_BITS. This definition of OFF_T matches the ABI of system calls
81 // like pread and mmap, as opposed to pread64 and mmap64.
82 // Mac and Linux/x86-64 are special.
83 #if SANITIZER_MAC || (SANITIZER_LINUX && defined(__x86_64__))
90 #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
91 typedef uptr operator_new_size_type
;
93 typedef u32 operator_new_size_type
;
95 } // namespace __sanitizer
98 // Tell the tools to write their reports to "path.<pid>" instead of stderr.
99 // The special values are "stdout" and "stderr".
100 SANITIZER_INTERFACE_ATTRIBUTE
101 void __sanitizer_set_report_path(const char *path
);
104 int coverage_sandboxed
;
105 __sanitizer::sptr coverage_fd
;
106 unsigned int coverage_max_block_size
;
107 } __sanitizer_sandbox_arguments
;
109 // Notify the tools that the sandbox is going to be turned on.
110 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
void
111 __sanitizer_sandbox_on_notify(__sanitizer_sandbox_arguments
*args
);
113 // This function is called by the tool when it has just finished reporting
114 // an error. 'error_summary' is a one-line string that summarizes
115 // the error message. This function can be overridden by the client.
116 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
117 void __sanitizer_report_error_summary(const char *error_summary
);
119 SANITIZER_INTERFACE_ATTRIBUTE
void __sanitizer_cov_dump();
120 SANITIZER_INTERFACE_ATTRIBUTE
void __sanitizer_cov_init();
121 SANITIZER_INTERFACE_ATTRIBUTE
void __sanitizer_cov();
122 SANITIZER_INTERFACE_ATTRIBUTE
123 void __sanitizer_annotate_contiguous_container(const void *beg
,
126 const void *new_mid
);
127 SANITIZER_INTERFACE_ATTRIBUTE
128 int __sanitizer_verify_contiguous_container(const void *beg
, const void *mid
,
133 using namespace __sanitizer
; // NOLINT
134 // ----------- ATTENTION -------------
135 // This header should NOT include any other headers to avoid portability issues.
138 #define INLINE inline
139 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
140 #define WEAK SANITIZER_WEAK_ATTRIBUTE
142 // Platform-specific defs.
143 #if defined(_MSC_VER)
144 # define ALWAYS_INLINE __forceinline
145 // FIXME(timurrrr): do we need this on Windows?
147 # define ALIGNED(x) __declspec(align(x))
148 # define FORMAT(f, a)
149 # define NOINLINE __declspec(noinline)
150 # define NORETURN __declspec(noreturn)
151 # define THREADLOCAL __declspec(thread)
153 # define LIKELY(x) (x)
154 # define UNLIKELY(x) (x)
155 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */
157 # define ALWAYS_INLINE inline __attribute__((always_inline))
158 # define ALIAS(x) __attribute__((alias(x)))
159 // Please only use the ALIGNED macro before the type.
160 // Using ALIGNED after the variable declaration is not portable!
161 # define ALIGNED(x) __attribute__((aligned(x)))
162 # define FORMAT(f, a) __attribute__((format(printf, f, a)))
163 # define NOINLINE __attribute__((noinline))
164 # define NORETURN __attribute__((noreturn))
165 # define THREADLOCAL __thread
166 # define NOTHROW throw()
167 # define LIKELY(x) __builtin_expect(!!(x), 1)
168 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
169 # if defined(__i386__) || defined(__x86_64__)
170 // __builtin_prefetch(x) generates prefetchnt0 on x86
171 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
173 # define PREFETCH(x) __builtin_prefetch(x)
177 #if !defined(_MSC_VER) || defined(__clang__)
178 # define UNUSED __attribute__((unused))
179 # define USED __attribute__((used))
185 // Unaligned versions of basic types.
186 typedef ALIGNED(1) u16 uu16
;
187 typedef ALIGNED(1) u32 uu32
;
188 typedef ALIGNED(1) u64 uu64
;
189 typedef ALIGNED(1) s16 us16
;
190 typedef ALIGNED(1) s32 us32
;
191 typedef ALIGNED(1) s64 us64
;
193 #if SANITIZER_WINDOWS
194 typedef unsigned long DWORD
; // NOLINT
195 typedef DWORD thread_return_t
;
196 # define THREAD_CALLING_CONV __stdcall
198 typedef void* thread_return_t
;
199 # define THREAD_CALLING_CONV
201 typedef thread_return_t (THREAD_CALLING_CONV
*thread_callback_t
)(void* arg
);
203 // NOTE: Functions below must be defined in each run-time.
204 namespace __sanitizer
{
207 // FIXME: No, this shouldn't be in the sanitizer interface.
208 SANITIZER_INTERFACE_ATTRIBUTE
209 void NORETURN
CheckFailed(const char *file
, int line
, const char *cond
,
211 } // namespace __sanitizer
214 #define RAW_CHECK_MSG(expr, msg) do { \
215 if (UNLIKELY(!(expr))) { \
221 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
223 #define CHECK_IMPL(c1, op, c2) \
225 __sanitizer::u64 v1 = (u64)(c1); \
226 __sanitizer::u64 v2 = (u64)(c2); \
227 if (UNLIKELY(!(v1 op v2))) \
228 __sanitizer::CheckFailed(__FILE__, __LINE__, \
229 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
233 #define CHECK(a) CHECK_IMPL((a), !=, 0)
234 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
235 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
236 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
237 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
238 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
239 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
242 #define DCHECK(a) CHECK(a)
243 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
244 #define DCHECK_NE(a, b) CHECK_NE(a, b)
245 #define DCHECK_LT(a, b) CHECK_LT(a, b)
246 #define DCHECK_LE(a, b) CHECK_LE(a, b)
247 #define DCHECK_GT(a, b) CHECK_GT(a, b)
248 #define DCHECK_GE(a, b) CHECK_GE(a, b)
251 #define DCHECK_EQ(a, b)
252 #define DCHECK_NE(a, b)
253 #define DCHECK_LT(a, b)
254 #define DCHECK_LE(a, b)
255 #define DCHECK_GT(a, b)
256 #define DCHECK_GE(a, b)
259 #define UNREACHABLE(msg) do { \
264 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
266 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
268 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
270 #define IMPL_PASTE(a, b) a##b
271 #define IMPL_COMPILER_ASSERT(pred, line) \
272 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
274 // Limits for integral types. We have to redefine it in case we don't
275 // have stdint.h (like in Visual Studio 9).
278 #if SANITIZER_WORDSIZE == 64
279 # define __INT64_C(c) c ## L
280 # define __UINT64_C(c) c ## UL
282 # define __INT64_C(c) c ## LL
283 # define __UINT64_C(c) c ## ULL
284 #endif // SANITIZER_WORDSIZE == 64
286 #define INT32_MIN (-2147483647-1)
288 #define INT32_MAX (2147483647)
290 #define UINT32_MAX (4294967295U)
292 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
294 #define INT64_MAX (__INT64_C(9223372036854775807))
296 #define UINT64_MAX (__UINT64_C(18446744073709551615))
298 enum LinkerInitialized
{ LINKER_INITIALIZED
= 0 };
300 #if !defined(_MSC_VER) || defined(__clang__)
301 # define GET_CALLER_PC() (uptr)__builtin_return_address(0)
302 # define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
304 extern "C" void* _ReturnAddress(void);
305 # pragma intrinsic(_ReturnAddress)
306 # define GET_CALLER_PC() (uptr)_ReturnAddress()
307 // CaptureStackBackTrace doesn't need to know BP on Windows.
308 // FIXME: This macro is still used when printing error reports though it's not
309 // clear if the BP value is needed in the ASan reports on Windows.
310 # define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
313 #define HANDLE_EINTR(res, f) \
318 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
321 #endif // SANITIZER_DEFS_H