sanitizer_internal_defs.h revision e31eca900a1f8849af75100c2d92e838d79d0920
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// GCC does not understand __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;
68
69}  // namespace __sanitizer
70
71extern "C" {
72  // Tell the tools to write their reports to "path.<pid>" instead of stderr.
73  void __sanitizer_set_report_path(const char *path)
74      SANITIZER_INTERFACE_ATTRIBUTE;
75
76  // Tell the tools to write their reports to given file descriptor instead of
77  // stderr.
78  void __sanitizer_set_report_fd(int fd)
79      SANITIZER_INTERFACE_ATTRIBUTE;
80
81  // Notify the tools that the sandbox is going to be turned on. The reserved
82  // parameter will be used in the future to hold a structure with functions
83  // that the tools may call to bypass the sandbox.
84  void __sanitizer_sandbox_on_notify(void *reserved)
85      SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE;
86
87  // This function is called by the tool when it has just finished reporting
88  // an error. 'error_summary' is a one-line string that summarizes
89  // the error message. This function can be overridden by the client.
90  void __sanitizer_report_error_summary(const char *error_summary)
91      SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE;
92}  // extern "C"
93
94
95using namespace __sanitizer;  // NOLINT
96// ----------- ATTENTION -------------
97// This header should NOT include any other headers to avoid portability issues.
98
99// Common defs.
100#define INLINE static inline
101#define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
102#define WEAK SANITIZER_WEAK_ATTRIBUTE
103
104// Platform-specific defs.
105#if defined(_MSC_VER)
106# define ALWAYS_INLINE __declspec(forceinline)
107// FIXME(timurrrr): do we need this on Windows?
108# define ALIAS(x)
109# define ALIGNED(x) __declspec(align(x))
110# define FORMAT(f, a)
111# define NOINLINE __declspec(noinline)
112# define NORETURN __declspec(noreturn)
113# define THREADLOCAL   __declspec(thread)
114# define NOTHROW
115# define LIKELY(x) (x)
116# define UNLIKELY(x) (x)
117# define UNUSED
118# define USED
119# define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */
120#else  // _MSC_VER
121# define ALWAYS_INLINE __attribute__((always_inline))
122# define ALIAS(x) __attribute__((alias(x)))
123# define ALIGNED(x) __attribute__((aligned(x)))
124# define FORMAT(f, a)  __attribute__((format(printf, f, a)))
125# define NOINLINE __attribute__((noinline))
126# define NORETURN  __attribute__((noreturn))
127# define THREADLOCAL   __thread
128# define NOTHROW throw()
129# define LIKELY(x)     __builtin_expect(!!(x), 1)
130# define UNLIKELY(x)   __builtin_expect(!!(x), 0)
131# define UNUSED __attribute__((unused))
132# define USED __attribute__((used))
133# if defined(__i386__) || defined(__x86_64__)
134// __builtin_prefetch(x) generates prefetchnt0 on x86
135#  define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
136# else
137#  define PREFETCH(x) __builtin_prefetch(x)
138# endif
139#endif  // _MSC_VER
140
141#if defined(_WIN32)
142typedef unsigned long DWORD;  // NOLINT
143typedef DWORD thread_return_t;
144# define THREAD_CALLING_CONV __stdcall
145#else  // _WIN32
146typedef void* thread_return_t;
147# define THREAD_CALLING_CONV
148#endif  // _WIN32
149typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
150
151#if __LP64__ || defined(_WIN64)
152#  define SANITIZER_WORDSIZE 64
153#else
154#  define SANITIZER_WORDSIZE 32
155#endif
156
157// NOTE: Functions below must be defined in each run-time.
158namespace __sanitizer {
159void NORETURN Die();
160void NORETURN CheckFailed(const char *file, int line, const char *cond,
161                          u64 v1, u64 v2);
162}  // namespace __sanitizer
163
164// Check macro
165#define RAW_CHECK_MSG(expr, msg) do { \
166  if (!(expr)) { \
167    RawWrite(msg); \
168    Die(); \
169  } \
170} while (0)
171
172#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
173
174#define CHECK_IMPL(c1, op, c2) \
175  do { \
176    __sanitizer::u64 v1 = (u64)(c1); \
177    __sanitizer::u64 v2 = (u64)(c2); \
178    if (!(v1 op v2)) \
179      __sanitizer::CheckFailed(__FILE__, __LINE__, \
180        "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
181  } while (false) \
182/**/
183
184#define CHECK(a)       CHECK_IMPL((a), !=, 0)
185#define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
186#define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
187#define CHECK_LT(a, b) CHECK_IMPL((a), <,  (b))
188#define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
189#define CHECK_GT(a, b) CHECK_IMPL((a), >,  (b))
190#define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
191
192#if TSAN_DEBUG
193#define DCHECK(a)       CHECK(a)
194#define DCHECK_EQ(a, b) CHECK_EQ(a, b)
195#define DCHECK_NE(a, b) CHECK_NE(a, b)
196#define DCHECK_LT(a, b) CHECK_LT(a, b)
197#define DCHECK_LE(a, b) CHECK_LE(a, b)
198#define DCHECK_GT(a, b) CHECK_GT(a, b)
199#define DCHECK_GE(a, b) CHECK_GE(a, b)
200#else
201#define DCHECK(a)
202#define DCHECK_EQ(a, b)
203#define DCHECK_NE(a, b)
204#define DCHECK_LT(a, b)
205#define DCHECK_LE(a, b)
206#define DCHECK_GT(a, b)
207#define DCHECK_GE(a, b)
208#endif
209
210#define UNREACHABLE(msg) do { \
211  CHECK(0 && msg); \
212  Die(); \
213} while (0)
214
215#define UNIMPLEMENTED() UNREACHABLE("unimplemented")
216
217#define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
218
219#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
220
221#define IMPL_PASTE(a, b) a##b
222#define IMPL_COMPILER_ASSERT(pred, line) \
223    typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
224
225// Limits for integral types. We have to redefine it in case we don't
226// have stdint.h (like in Visual Studio 9).
227#undef __INT64_C
228#undef __UINT64_C
229#if SANITIZER_WORDSIZE == 64
230# define __INT64_C(c)  c ## L
231# define __UINT64_C(c) c ## UL
232#else
233# define __INT64_C(c)  c ## LL
234# define __UINT64_C(c) c ## ULL
235#endif  // SANITIZER_WORDSIZE == 64
236#undef INT32_MIN
237#define INT32_MIN              (-2147483647-1)
238#undef INT32_MAX
239#define INT32_MAX              (2147483647)
240#undef UINT32_MAX
241#define UINT32_MAX             (4294967295U)
242#undef INT64_MIN
243#define INT64_MIN              (-__INT64_C(9223372036854775807)-1)
244#undef INT64_MAX
245#define INT64_MAX              (__INT64_C(9223372036854775807))
246#undef UINT64_MAX
247#define UINT64_MAX             (__UINT64_C(18446744073709551615))
248
249enum LinkerInitialized { LINKER_INITIALIZED = 0 };
250
251#if !defined(_MSC_VER) || defined(__clang__)
252# define GET_CALLER_PC() (uptr)__builtin_return_address(0)
253# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
254#else
255extern "C" void* _ReturnAddress(void);
256# pragma intrinsic(_ReturnAddress)
257# define GET_CALLER_PC() (uptr)_ReturnAddress()
258// CaptureStackBackTrace doesn't need to know BP on Windows.
259// FIXME: This macro is still used when printing error reports though it's not
260// clear if the BP value is needed in the ASan reports on Windows.
261# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
262#endif
263
264#define HANDLE_EINTR(res, f) {                               \
265  do {                                                                  \
266    res = (f);                                                         \
267  } while (res == -1 && errno == EINTR); \
268  }
269
270#endif  // SANITIZER_DEFS_H
271