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