111
|
1 //===-- sanitizer_internal_defs.h -------------------------------*- C++ -*-===//
|
|
2 //
|
|
3 // This file is distributed under the University of Illinois Open Source
|
|
4 // License. See LICENSE.TXT for details.
|
|
5 //
|
|
6 //===----------------------------------------------------------------------===//
|
|
7 //
|
|
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
|
|
13
|
|
14 #include "sanitizer_platform.h"
|
|
15
|
|
16 #ifndef SANITIZER_DEBUG
|
|
17 # define SANITIZER_DEBUG 0
|
|
18 #endif
|
|
19
|
|
20 // Only use SANITIZER_*ATTRIBUTE* before the function return type!
|
|
21 #if SANITIZER_WINDOWS
|
|
22 #if SANITIZER_IMPORT_INTERFACE
|
|
23 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllimport)
|
|
24 #else
|
|
25 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport)
|
|
26 #endif
|
|
27 # define SANITIZER_WEAK_ATTRIBUTE
|
|
28 #elif SANITIZER_GO
|
|
29 # define SANITIZER_INTERFACE_ATTRIBUTE
|
|
30 # define SANITIZER_WEAK_ATTRIBUTE
|
|
31 #else
|
|
32 # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
|
|
33 # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))
|
|
34 #endif
|
|
35
|
|
36 // TLS is handled differently on different platforms
|
|
37 #if SANITIZER_LINUX
|
|
38 # define SANITIZER_TLS_INITIAL_EXEC_ATTRIBUTE \
|
|
39 __attribute__((tls_model("initial-exec"))) thread_local
|
|
40 #else
|
|
41 # define SANITIZER_TLS_INITIAL_EXEC_ATTRIBUTE
|
|
42 #endif
|
|
43
|
|
44 //--------------------------- WEAK FUNCTIONS ---------------------------------//
|
|
45 // When working with weak functions, to simplify the code and make it more
|
|
46 // portable, when possible define a default implementation using this macro:
|
|
47 //
|
|
48 // SANITIZER_INTERFACE_WEAK_DEF(<return_type>, <name>, <parameter list>)
|
|
49 //
|
|
50 // For example:
|
|
51 // SANITIZER_INTERFACE_WEAK_DEF(bool, compare, int a, int b) { return a > b; }
|
|
52 //
|
|
53 #if SANITIZER_WINDOWS
|
|
54 #include "sanitizer_win_defs.h"
|
|
55 # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \
|
|
56 WIN_WEAK_EXPORT_DEF(ReturnType, Name, __VA_ARGS__)
|
|
57 #else
|
|
58 # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \
|
|
59 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE \
|
|
60 ReturnType Name(__VA_ARGS__)
|
|
61 #endif
|
|
62
|
|
63 // SANITIZER_SUPPORTS_WEAK_HOOKS means that we support real weak functions that
|
|
64 // will evaluate to a null pointer when not defined.
|
|
65 #ifndef SANITIZER_SUPPORTS_WEAK_HOOKS
|
131
|
66 #if SANITIZER_LINUX && !SANITIZER_GO
|
|
67 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
|
|
68 // Before Xcode 4.5, the Darwin linker doesn't reliably support undefined
|
|
69 // weak symbols. Mac OS X 10.9/Darwin 13 is the first release only supported
|
|
70 // by Xcode >= 4.5.
|
|
71 #elif SANITIZER_MAC && \
|
|
72 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1090 && !SANITIZER_GO
|
111
|
73 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
|
|
74 #else
|
|
75 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
|
|
76 #endif
|
|
77 #endif // SANITIZER_SUPPORTS_WEAK_HOOKS
|
|
78 // For some weak hooks that will be called very often and we want to avoid the
|
|
79 // overhead of executing the default implementation when it is not necessary,
|
|
80 // we can use the flag SANITIZER_SUPPORTS_WEAK_HOOKS to only define the default
|
|
81 // implementation for platforms that doesn't support weak symbols. For example:
|
|
82 //
|
|
83 // #if !SANITIZER_SUPPORT_WEAK_HOOKS
|
|
84 // SANITIZER_INTERFACE_WEAK_DEF(bool, compare_hook, int a, int b) {
|
|
85 // return a > b;
|
|
86 // }
|
|
87 // #endif
|
|
88 //
|
|
89 // And then use it as: if (compare_hook) compare_hook(a, b);
|
|
90 //----------------------------------------------------------------------------//
|
|
91
|
|
92
|
|
93 // We can use .preinit_array section on Linux to call sanitizer initialization
|
|
94 // functions very early in the process startup (unless PIC macro is defined).
|
|
95 // FIXME: do we have anything like this on Mac?
|
|
96 #if SANITIZER_LINUX && !SANITIZER_ANDROID && !defined(PIC)
|
|
97 # define SANITIZER_CAN_USE_PREINIT_ARRAY 1
|
|
98 #else
|
|
99 # define SANITIZER_CAN_USE_PREINIT_ARRAY 0
|
|
100 #endif
|
|
101
|
|
102 // GCC does not understand __has_feature
|
|
103 #if !defined(__has_feature)
|
|
104 # define __has_feature(x) 0
|
|
105 #endif
|
|
106
|
131
|
107 // Older GCCs do not understand __has_attribute.
|
|
108 #if !defined(__has_attribute)
|
|
109 # define __has_attribute(x) 0
|
|
110 #endif
|
|
111
|
111
|
112 // For portability reasons we do not include stddef.h, stdint.h or any other
|
|
113 // system header, but we do need some basic types that are not defined
|
|
114 // in a portable way by the language itself.
|
|
115 namespace __sanitizer {
|
|
116
|
|
117 #if defined(_WIN64)
|
|
118 // 64-bit Windows uses LLP64 data model.
|
|
119 typedef unsigned long long uptr; // NOLINT
|
|
120 typedef signed long long sptr; // NOLINT
|
|
121 #else
|
|
122 typedef unsigned long uptr; // NOLINT
|
|
123 typedef signed long sptr; // NOLINT
|
|
124 #endif // defined(_WIN64)
|
|
125 #if defined(__x86_64__)
|
|
126 // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
|
|
127 // 64-bit pointer to unwind stack frame.
|
|
128 typedef unsigned long long uhwptr; // NOLINT
|
|
129 #else
|
|
130 typedef uptr uhwptr; // NOLINT
|
|
131 #endif
|
|
132 typedef unsigned char u8;
|
|
133 typedef unsigned short u16; // NOLINT
|
|
134 typedef unsigned int u32;
|
|
135 typedef unsigned long long u64; // NOLINT
|
|
136 typedef signed char s8;
|
|
137 typedef signed short s16; // NOLINT
|
|
138 typedef signed int s32;
|
|
139 typedef signed long long s64; // NOLINT
|
|
140 #if SANITIZER_WINDOWS
|
|
141 // On Windows, files are HANDLE, which is a synonim of void*.
|
|
142 // Use void* to avoid including <windows.h> everywhere.
|
|
143 typedef void* fd_t;
|
|
144 typedef unsigned error_t;
|
|
145 #else
|
|
146 typedef int fd_t;
|
|
147 typedef int error_t;
|
|
148 #endif
|
|
149 typedef int pid_t;
|
|
150
|
|
151 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_MAC || \
|
|
152 (SANITIZER_LINUX && defined(__x86_64__))
|
|
153 typedef u64 OFF_T;
|
|
154 #else
|
|
155 typedef uptr OFF_T;
|
|
156 #endif
|
|
157 typedef u64 OFF64_T;
|
|
158
|
|
159 #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
|
|
160 typedef uptr operator_new_size_type;
|
|
161 #else
|
|
162 # if defined(__s390__) && !defined(__s390x__)
|
|
163 // Special case: 31-bit s390 has unsigned long as size_t.
|
|
164 typedef unsigned long operator_new_size_type;
|
|
165 # else
|
|
166 typedef u32 operator_new_size_type;
|
|
167 # endif
|
|
168 #endif
|
|
169
|
|
170 #if SANITIZER_MAC
|
|
171 // On Darwin, thread IDs are 64-bit even on 32-bit systems.
|
|
172 typedef u64 tid_t;
|
|
173 #else
|
|
174 typedef uptr tid_t;
|
|
175 #endif
|
|
176
|
|
177 // ----------- ATTENTION -------------
|
|
178 // This header should NOT include any other headers to avoid portability issues.
|
|
179
|
|
180 // Common defs.
|
|
181 #define INLINE inline
|
|
182 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
|
|
183 #define SANITIZER_WEAK_DEFAULT_IMPL \
|
|
184 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
|
|
185 #define SANITIZER_WEAK_CXX_DEFAULT_IMPL \
|
|
186 extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
|
|
187
|
|
188 // Platform-specific defs.
|
|
189 #if defined(_MSC_VER)
|
|
190 # define ALWAYS_INLINE __forceinline
|
|
191 // FIXME(timurrrr): do we need this on Windows?
|
|
192 # define ALIAS(x)
|
|
193 # define ALIGNED(x) __declspec(align(x))
|
|
194 # define FORMAT(f, a)
|
|
195 # define NOINLINE __declspec(noinline)
|
|
196 # define NORETURN __declspec(noreturn)
|
|
197 # define THREADLOCAL __declspec(thread)
|
|
198 # define LIKELY(x) (x)
|
|
199 # define UNLIKELY(x) (x)
|
|
200 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0
|
|
201 #else // _MSC_VER
|
|
202 # define ALWAYS_INLINE inline __attribute__((always_inline))
|
|
203 # define ALIAS(x) __attribute__((alias(x)))
|
|
204 // Please only use the ALIGNED macro before the type.
|
|
205 // Using ALIGNED after the variable declaration is not portable!
|
|
206 # define ALIGNED(x) __attribute__((aligned(x)))
|
|
207 # define FORMAT(f, a) __attribute__((format(printf, f, a)))
|
|
208 # define NOINLINE __attribute__((noinline))
|
|
209 # define NORETURN __attribute__((noreturn))
|
|
210 # define THREADLOCAL __thread
|
|
211 # define LIKELY(x) __builtin_expect(!!(x), 1)
|
|
212 # define UNLIKELY(x) __builtin_expect(!!(x), 0)
|
|
213 # if defined(__i386__) || defined(__x86_64__)
|
|
214 // __builtin_prefetch(x) generates prefetchnt0 on x86
|
|
215 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
|
|
216 # else
|
|
217 # define PREFETCH(x) __builtin_prefetch(x)
|
|
218 # endif
|
|
219 #endif // _MSC_VER
|
|
220
|
|
221 #if !defined(_MSC_VER) || defined(__clang__)
|
|
222 # define UNUSED __attribute__((unused))
|
|
223 # define USED __attribute__((used))
|
|
224 #else
|
|
225 # define UNUSED
|
|
226 # define USED
|
|
227 #endif
|
|
228
|
|
229 #if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)
|
|
230 # define NOEXCEPT noexcept
|
|
231 #else
|
|
232 # define NOEXCEPT throw()
|
|
233 #endif
|
|
234
|
|
235 // Unaligned versions of basic types.
|
|
236 typedef ALIGNED(1) u16 uu16;
|
|
237 typedef ALIGNED(1) u32 uu32;
|
|
238 typedef ALIGNED(1) u64 uu64;
|
|
239 typedef ALIGNED(1) s16 us16;
|
|
240 typedef ALIGNED(1) s32 us32;
|
|
241 typedef ALIGNED(1) s64 us64;
|
|
242
|
|
243 #if SANITIZER_WINDOWS
|
|
244 } // namespace __sanitizer
|
|
245 typedef unsigned long DWORD; // NOLINT
|
|
246 namespace __sanitizer {
|
|
247 typedef DWORD thread_return_t;
|
|
248 # define THREAD_CALLING_CONV __stdcall
|
|
249 #else // _WIN32
|
|
250 typedef void* thread_return_t;
|
|
251 # define THREAD_CALLING_CONV
|
|
252 #endif // _WIN32
|
|
253 typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
|
|
254
|
|
255 // NOTE: Functions below must be defined in each run-time.
|
|
256 void NORETURN Die();
|
|
257
|
|
258 // FIXME: No, this shouldn't be in the sanitizer interface.
|
|
259 SANITIZER_INTERFACE_ATTRIBUTE
|
|
260 void NORETURN CheckFailed(const char *file, int line, const char *cond,
|
|
261 u64 v1, u64 v2);
|
|
262
|
|
263 // Check macro
|
|
264 #define RAW_CHECK_MSG(expr, msg) do { \
|
|
265 if (UNLIKELY(!(expr))) { \
|
|
266 RawWrite(msg); \
|
|
267 Die(); \
|
|
268 } \
|
|
269 } while (0)
|
|
270
|
|
271 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
|
|
272
|
|
273 #define CHECK_IMPL(c1, op, c2) \
|
|
274 do { \
|
|
275 __sanitizer::u64 v1 = (__sanitizer::u64)(c1); \
|
|
276 __sanitizer::u64 v2 = (__sanitizer::u64)(c2); \
|
|
277 if (UNLIKELY(!(v1 op v2))) \
|
|
278 __sanitizer::CheckFailed(__FILE__, __LINE__, \
|
|
279 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
|
|
280 } while (false) \
|
|
281 /**/
|
|
282
|
|
283 #define CHECK(a) CHECK_IMPL((a), !=, 0)
|
|
284 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
|
|
285 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
|
|
286 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))
|
|
287 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
|
|
288 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))
|
|
289 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
|
|
290
|
|
291 #if SANITIZER_DEBUG
|
|
292 #define DCHECK(a) CHECK(a)
|
|
293 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
|
|
294 #define DCHECK_NE(a, b) CHECK_NE(a, b)
|
|
295 #define DCHECK_LT(a, b) CHECK_LT(a, b)
|
|
296 #define DCHECK_LE(a, b) CHECK_LE(a, b)
|
|
297 #define DCHECK_GT(a, b) CHECK_GT(a, b)
|
|
298 #define DCHECK_GE(a, b) CHECK_GE(a, b)
|
|
299 #else
|
|
300 #define DCHECK(a)
|
|
301 #define DCHECK_EQ(a, b)
|
|
302 #define DCHECK_NE(a, b)
|
|
303 #define DCHECK_LT(a, b)
|
|
304 #define DCHECK_LE(a, b)
|
|
305 #define DCHECK_GT(a, b)
|
|
306 #define DCHECK_GE(a, b)
|
|
307 #endif
|
|
308
|
|
309 #define UNREACHABLE(msg) do { \
|
|
310 CHECK(0 && msg); \
|
|
311 Die(); \
|
|
312 } while (0)
|
|
313
|
|
314 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
|
|
315
|
|
316 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
|
|
317
|
|
318 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
|
|
319
|
|
320 #define IMPL_PASTE(a, b) a##b
|
|
321 #define IMPL_COMPILER_ASSERT(pred, line) \
|
|
322 typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
|
|
323
|
|
324 // Limits for integral types. We have to redefine it in case we don't
|
|
325 // have stdint.h (like in Visual Studio 9).
|
|
326 #undef __INT64_C
|
|
327 #undef __UINT64_C
|
|
328 #if SANITIZER_WORDSIZE == 64
|
|
329 # define __INT64_C(c) c ## L
|
|
330 # define __UINT64_C(c) c ## UL
|
|
331 #else
|
|
332 # define __INT64_C(c) c ## LL
|
|
333 # define __UINT64_C(c) c ## ULL
|
|
334 #endif // SANITIZER_WORDSIZE == 64
|
|
335 #undef INT32_MIN
|
|
336 #define INT32_MIN (-2147483647-1)
|
|
337 #undef INT32_MAX
|
|
338 #define INT32_MAX (2147483647)
|
|
339 #undef UINT32_MAX
|
|
340 #define UINT32_MAX (4294967295U)
|
|
341 #undef INT64_MIN
|
|
342 #define INT64_MIN (-__INT64_C(9223372036854775807)-1)
|
|
343 #undef INT64_MAX
|
|
344 #define INT64_MAX (__INT64_C(9223372036854775807))
|
|
345 #undef UINT64_MAX
|
|
346 #define UINT64_MAX (__UINT64_C(18446744073709551615))
|
|
347
|
|
348 enum LinkerInitialized { LINKER_INITIALIZED = 0 };
|
|
349
|
|
350 #if !defined(_MSC_VER) || defined(__clang__)
|
|
351 #if SANITIZER_S390_31
|
|
352 #define GET_CALLER_PC() \
|
|
353 (__sanitizer::uptr) __builtin_extract_return_addr(__builtin_return_address(0))
|
|
354 #else
|
|
355 #define GET_CALLER_PC() (__sanitizer::uptr) __builtin_return_address(0)
|
|
356 #endif
|
|
357 #define GET_CURRENT_FRAME() (__sanitizer::uptr) __builtin_frame_address(0)
|
|
358 inline void Trap() {
|
|
359 __builtin_trap();
|
|
360 }
|
|
361 #else
|
|
362 extern "C" void* _ReturnAddress(void);
|
|
363 extern "C" void* _AddressOfReturnAddress(void);
|
|
364 # pragma intrinsic(_ReturnAddress)
|
|
365 # pragma intrinsic(_AddressOfReturnAddress)
|
|
366 #define GET_CALLER_PC() (__sanitizer::uptr) _ReturnAddress()
|
|
367 // CaptureStackBackTrace doesn't need to know BP on Windows.
|
|
368 #define GET_CURRENT_FRAME() \
|
|
369 (((__sanitizer::uptr)_AddressOfReturnAddress()) + sizeof(__sanitizer::uptr))
|
|
370
|
|
371 extern "C" void __ud2(void);
|
|
372 # pragma intrinsic(__ud2)
|
|
373 inline void Trap() {
|
|
374 __ud2();
|
|
375 }
|
|
376 #endif
|
|
377
|
|
378 #define HANDLE_EINTR(res, f) \
|
|
379 { \
|
|
380 int rverrno; \
|
|
381 do { \
|
|
382 res = (f); \
|
|
383 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
|
|
384 }
|
|
385
|
|
386 // Forces the compiler to generate a frame pointer in the function.
|
|
387 #define ENABLE_FRAME_POINTER \
|
|
388 do { \
|
|
389 volatile __sanitizer::uptr enable_fp; \
|
|
390 enable_fp = GET_CURRENT_FRAME(); \
|
|
391 (void)enable_fp; \
|
|
392 } while (0)
|
|
393
|
|
394 } // namespace __sanitizer
|
|
395
|
|
396 namespace __asan { using namespace __sanitizer; } // NOLINT
|
|
397 namespace __dsan { using namespace __sanitizer; } // NOLINT
|
|
398 namespace __dfsan { using namespace __sanitizer; } // NOLINT
|
|
399 namespace __esan { using namespace __sanitizer; } // NOLINT
|
|
400 namespace __lsan { using namespace __sanitizer; } // NOLINT
|
|
401 namespace __msan { using namespace __sanitizer; } // NOLINT
|
|
402 namespace __tsan { using namespace __sanitizer; } // NOLINT
|
|
403 namespace __scudo { using namespace __sanitizer; } // NOLINT
|
|
404 namespace __ubsan { using namespace __sanitizer; } // NOLINT
|
|
405 namespace __xray { using namespace __sanitizer; } // NOLINT
|
|
406 namespace __interception { using namespace __sanitizer; } // NOLINT
|
|
407
|
|
408
|
|
409 #endif // SANITIZER_DEFS_H
|