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