Define SANITIZER_INTERFACE_ATTRIBUTE on Windows and fix all the places where SANITIZE...
[blocksruntime.git] / lib / sanitizer_common / sanitizer_internal_defs.h
blob60e076f7790065afc8e3511017361581fc9fbfa0
1 //===-- sanitizer_internal_defs.h -------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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!
19 #if SANITIZER_WINDOWS
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
26 #else
27 # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
28 # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
29 #endif
31 #if SANITIZER_LINUX && !defined(SANITIZER_GO)
32 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
33 #else
34 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
35 #endif
37 // GCC does not understand __has_feature
38 #if !defined(__has_feature)
39 # define __has_feature(x) 0
40 #endif
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 {
47 #if defined(_WIN64)
48 // 64-bit Windows uses LLP64 data model.
49 typedef unsigned long long uptr; // NOLINT
50 typedef signed long long sptr; // NOLINT
51 #else
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
59 #else
60 typedef uptr uhwptr; // NOLINT
61 #endif
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
70 typedef int fd_t;
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__))
77 typedef u64 OFF_T;
78 #else
79 typedef uptr OFF_T;
80 #endif
81 typedef u64 OFF64_T;
82 } // namespace __sanitizer
84 extern "C" {
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
90 // stderr.
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);
105 } // extern "C"
108 using namespace __sanitizer; // NOLINT
109 // ----------- ATTENTION -------------
110 // This header should NOT include any other headers to avoid portability issues.
112 // Common defs.
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?
121 # define ALIAS(x)
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)
127 # define NOTHROW
128 # define LIKELY(x) (x)
129 # define UNLIKELY(x) (x)
130 # define UNUSED
131 # define USED
132 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */
133 #else // _MSC_VER
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))
151 # else
152 # define PREFETCH(x) __builtin_prefetch(x)
153 # endif
154 #endif // _MSC_VER
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
168 #else // _WIN32
169 typedef void* thread_return_t;
170 # define THREAD_CALLING_CONV
171 #endif // _WIN32
172 typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
174 #if __LP64__ || defined(_WIN64)
175 # define SANITIZER_WORDSIZE 64
176 #else
177 # define SANITIZER_WORDSIZE 32
178 #endif
180 // NOTE: Functions below must be defined in each run-time.
181 namespace __sanitizer {
182 void NORETURN Die();
183 void NORETURN CheckFailed(const char *file, int line, const char *cond,
184 u64 v1, u64 v2);
185 } // namespace __sanitizer
187 // Check macro
188 #define RAW_CHECK_MSG(expr, msg) do { \
189 if (!(expr)) { \
190 RawWrite(msg); \
191 Die(); \
193 } while (0)
195 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
197 #define CHECK_IMPL(c1, op, c2) \
198 do { \
199 __sanitizer::u64 v1 = (u64)(c1); \
200 __sanitizer::u64 v2 = (u64)(c2); \
201 if (!(v1 op v2)) \
202 __sanitizer::CheckFailed(__FILE__, __LINE__, \
203 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
204 } while (false) \
205 /**/
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))
215 #if TSAN_DEBUG
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)
223 #else
224 #define DCHECK(a)
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)
231 #endif
233 #define UNREACHABLE(msg) do { \
234 CHECK(0 && msg); \
235 Die(); \
236 } while (0)
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).
250 #undef __INT64_C
251 #undef __UINT64_C
252 #if SANITIZER_WORDSIZE == 64
253 # define __INT64_C(c) c ## L
254 # define __UINT64_C(c) c ## UL
255 #else
256 # define __INT64_C(c) c ## LL
257 # define __UINT64_C(c) c ## ULL
258 #endif // SANITIZER_WORDSIZE == 64
259 #undef INT32_MIN
260 #define INT32_MIN (-2147483647-1)
261 #undef INT32_MAX
262 #define INT32_MAX (2147483647)
263 #undef UINT32_MAX
264 #define UINT32_MAX (4294967295U)
265 #undef INT64_MIN
266 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
267 #undef INT64_MAX
268 #define INT64_MAX (__INT64_C(9223372036854775807))
269 #undef UINT64_MAX
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)
277 #else
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
285 #endif
287 #define HANDLE_EINTR(res, f) \
289 int rverrno; \
290 do { \
291 res = (f); \
292 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
295 #endif // SANITIZER_DEFS_H