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