sanitizer_internal_defs.h revision ee7cc4454421a176d23442382afd9a01d36e7ad4
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#if defined(_WIN32)
17// FIXME find out what we need on Windows. __declspec(dllexport) ?
18# define SANITIZER_INTERFACE_ATTRIBUTE
19# define SANITIZER_WEAK_ATTRIBUTE
20#elif defined(SANITIZER_GO)
21# define SANITIZER_INTERFACE_ATTRIBUTE
22# define SANITIZER_WEAK_ATTRIBUTE
23#else
24# define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
25# define SANITIZER_WEAK_ATTRIBUTE  __attribute__((weak))
26#endif
27
28#ifdef __linux__
29# define SANITIZER_SUPPORTS_WEAK_HOOKS 1
30#else
31# define SANITIZER_SUPPORTS_WEAK_HOOKS 0
32#endif
33
34// __has_feature
35#if !defined(__has_feature)
36# define __has_feature(x) 0
37#endif
38
39// For portability reasons we do not include stddef.h, stdint.h or any other
40// system header, but we do need some basic types that are not defined
41// in a portable way by the language itself.
42namespace __sanitizer {
43
44#if defined(_WIN64)
45// 64-bit Windows uses LLP64 data model.
46typedef unsigned long long uptr;  // NOLINT
47typedef signed   long long sptr;  // NOLINT
48#else
49typedef unsigned long uptr;  // NOLINT
50typedef signed   long sptr;  // NOLINT
51#endif  // defined(_WIN64)
52#if defined(__x86_64__)
53// Since x32 uses ILP32 data model in 64-bit hardware mode,  we must use
54// 64-bit pointer to unwind stack frame.
55typedef unsigned long long uhwptr;  // NOLINT
56#else
57typedef uptr uhwptr;   // NOLINT
58#endif
59typedef unsigned char u8;
60typedef unsigned short u16;  // NOLINT
61typedef unsigned int u32;
62typedef unsigned long long u64;  // NOLINT
63typedef signed   char s8;
64typedef signed   short s16;  // NOLINT
65typedef signed   int s32;
66typedef signed   long long s64;  // NOLINT
67typedef int fd_t;
68typedef u32 mode_t;
69
70}  // namespace __sanitizer
71
72extern "C" {
73  // Tell the tools to write their reports to "path.<pid>" instead of stderr.
74  void __sanitizer_set_report_path(const char *path)
75      SANITIZER_INTERFACE_ATTRIBUTE;
76
77  // Tell the tools to write their reports to given file descriptor instead of
78  // stderr.
79  void __sanitizer_set_report_fd(int fd)
80      SANITIZER_INTERFACE_ATTRIBUTE;
81
82  // Notify the tools that the sandbox is going to be turned on. The reserved
83  // parameter will be used in the future to hold a structure with functions
84  // that the tools may call to bypass the sandbox.
85  void __sanitizer_sandbox_on_notify(void *reserved)
86      SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE;
87}  // extern "C"
88
89
90using namespace __sanitizer;  // NOLINT
91// ----------- ATTENTION -------------
92// This header should NOT include any other headers to avoid portability issues.
93
94// Common defs.
95#define INLINE static inline
96#define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
97#define WEAK SANITIZER_WEAK_ATTRIBUTE
98
99// Platform-specific defs.
100#if defined(_MSC_VER)
101# define ALWAYS_INLINE __declspec(forceinline)
102// FIXME(timurrrr): do we need this on Windows?
103# define ALIAS(x)
104# define ALIGNED(x) __declspec(align(x))
105# define FORMAT(f, a)
106# define NOINLINE __declspec(noinline)
107# define NORETURN __declspec(noreturn)
108# define THREADLOCAL   __declspec(thread)
109# define NOTHROW
110# define LIKELY(x) (x)
111# define UNLIKELY(x) (x)
112# define UNUSED
113# define USED
114# define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */
115#else  // _MSC_VER
116# define ALWAYS_INLINE __attribute__((always_inline))
117# define ALIAS(x) __attribute__((alias(x)))
118# define ALIGNED(x) __attribute__((aligned(x)))
119# define FORMAT(f, a)  __attribute__((format(printf, f, a)))
120# define NOINLINE __attribute__((noinline))
121# define NORETURN  __attribute__((noreturn))
122# define THREADLOCAL   __thread
123# define NOTHROW throw()
124# define LIKELY(x)     __builtin_expect(!!(x), 1)
125# define UNLIKELY(x)   __builtin_expect(!!(x), 0)
126# define UNUSED __attribute__((unused))
127# define USED __attribute__((used))
128# if defined(__i386__) || defined(__x86_64__)
129// __builtin_prefetch(x) generates prefetchnt0 on x86
130#  define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
131# else
132#  define PREFETCH(x) __builtin_prefetch(x)
133# endif
134#endif  // _MSC_VER
135
136#if defined(_WIN32)
137typedef unsigned long DWORD;  // NOLINT
138typedef DWORD thread_return_t;
139# define THREAD_CALLING_CONV __stdcall
140#else  // _WIN32
141typedef void* thread_return_t;
142# define THREAD_CALLING_CONV
143#endif  // _WIN32
144typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
145
146#if __LP64__ || defined(_WIN64)
147#  define SANITIZER_WORDSIZE 64
148#else
149#  define SANITIZER_WORDSIZE 32
150#endif
151
152// NOTE: Functions below must be defined in each run-time.
153namespace __sanitizer {
154void NORETURN Die();
155void NORETURN CheckFailed(const char *file, int line, const char *cond,
156                          u64 v1, u64 v2);
157}  // namespace __sanitizer
158
159// Check macro
160#define RAW_CHECK_MSG(expr, msg) do { \
161  if (!(expr)) { \
162    RawWrite(msg); \
163    Die(); \
164  } \
165} while (0)
166
167#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
168
169#define CHECK_IMPL(c1, op, c2) \
170  do { \
171    __sanitizer::u64 v1 = (u64)(c1); \
172    __sanitizer::u64 v2 = (u64)(c2); \
173    if (!(v1 op v2)) \
174      __sanitizer::CheckFailed(__FILE__, __LINE__, \
175        "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
176  } while (false) \
177/**/
178
179#define CHECK(a)       CHECK_IMPL((a), !=, 0)
180#define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
181#define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
182#define CHECK_LT(a, b) CHECK_IMPL((a), <,  (b))
183#define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
184#define CHECK_GT(a, b) CHECK_IMPL((a), >,  (b))
185#define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
186
187#if TSAN_DEBUG
188#define DCHECK(a)       CHECK(a)
189#define DCHECK_EQ(a, b) CHECK_EQ(a, b)
190#define DCHECK_NE(a, b) CHECK_NE(a, b)
191#define DCHECK_LT(a, b) CHECK_LT(a, b)
192#define DCHECK_LE(a, b) CHECK_LE(a, b)
193#define DCHECK_GT(a, b) CHECK_GT(a, b)
194#define DCHECK_GE(a, b) CHECK_GE(a, b)
195#else
196#define DCHECK(a)
197#define DCHECK_EQ(a, b)
198#define DCHECK_NE(a, b)
199#define DCHECK_LT(a, b)
200#define DCHECK_LE(a, b)
201#define DCHECK_GT(a, b)
202#define DCHECK_GE(a, b)
203#endif
204
205#define UNREACHABLE(msg) do { \
206  CHECK(0 && msg); \
207  Die(); \
208} while (0)
209
210#define UNIMPLEMENTED() UNREACHABLE("unimplemented")
211
212#define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
213
214#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
215
216#define IMPL_PASTE(a, b) a##b
217#define IMPL_COMPILER_ASSERT(pred, line) \
218    typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
219
220// Limits for integral types. We have to redefine it in case we don't
221// have stdint.h (like in Visual Studio 9).
222#undef __INT64_C
223#undef __UINT64_C
224#if SANITIZER_WORDSIZE == 64
225# define __INT64_C(c)  c ## L
226# define __UINT64_C(c) c ## UL
227#else
228# define __INT64_C(c)  c ## LL
229# define __UINT64_C(c) c ## ULL
230#endif  // SANITIZER_WORDSIZE == 64
231#undef INT32_MIN
232#define INT32_MIN              (-2147483647-1)
233#undef INT32_MAX
234#define INT32_MAX              (2147483647)
235#undef UINT32_MAX
236#define UINT32_MAX             (4294967295U)
237#undef INT64_MIN
238#define INT64_MIN              (-__INT64_C(9223372036854775807)-1)
239#undef INT64_MAX
240#define INT64_MAX              (__INT64_C(9223372036854775807))
241#undef UINT64_MAX
242#define UINT64_MAX             (__UINT64_C(18446744073709551615))
243
244enum LinkerInitialized { LINKER_INITIALIZED = 0 };
245
246#if !defined(_MSC_VER) || defined(__clang__)
247# define GET_CALLER_PC() (uptr)__builtin_return_address(0)
248# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
249#else
250extern "C" void* _ReturnAddress(void);
251# pragma intrinsic(_ReturnAddress)
252# define GET_CALLER_PC() (uptr)_ReturnAddress()
253// CaptureStackBackTrace doesn't need to know BP on Windows.
254// FIXME: This macro is still used when printing error reports though it's not
255// clear if the BP value is needed in the ASan reports on Windows.
256# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
257#endif
258
259#define HANDLE_EINTR(res, f) {                               \
260  do {                                                                  \
261    res = (f);                                                         \
262  } while (res == -1 && errno == EINTR); \
263  }
264
265#endif  // SANITIZER_DEFS_H
266