sanitizer_internal_defs.h revision 6fb47af2d2d305adbfc3d41bea589d1527a364a9
13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===-- sanitizer_internal_defs.h -------------------------------*- C++ -*-===//
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//                     The LLVM Compiler Infrastructure
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// This file is distributed under the University of Illinois Open Source
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// License. See LICENSE.TXT for details.
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===----------------------------------------------------------------------===//
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// This file is shared between AddressSanitizer and ThreadSanitizer.
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// It contains macro used in run-time libraries code.
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry//===----------------------------------------------------------------------===//
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#ifndef SANITIZER_DEFS_H
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#define SANITIZER_DEFS_H
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if defined(_WIN32)
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// FIXME find out what we need on Windows. __declspec(dllexport) ?
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry# define SANITIZER_INTERFACE_ATTRIBUTE
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry# define SANITIZER_WEAK_ATTRIBUTE
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#elif defined(SANITIZER_GO)
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry# define SANITIZER_INTERFACE_ATTRIBUTE
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry# define SANITIZER_WEAK_ATTRIBUTE
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#else
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry# define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry# define SANITIZER_WEAK_ATTRIBUTE  __attribute__((weak))
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#ifdef __linux__
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry# define SANITIZER_SUPPORTS_WEAK_HOOKS 1
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#else
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry# define SANITIZER_SUPPORTS_WEAK_HOOKS 0
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// GCC does not understand __has_feature
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if !defined(__has_feature)
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry# define __has_feature(x) 0
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// For portability reasons we do not include stddef.h, stdint.h or any other
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// system header, but we do need some basic types that are not defined
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// in a portable way by the language itself.
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace __sanitizer {
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if defined(_WIN64)
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// 64-bit Windows uses LLP64 data model.
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytypedef unsigned long long uptr;  // NOLINT
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytypedef signed   long long sptr;  // NOLINT
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#else
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytypedef unsigned long uptr;  // NOLINT
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyrytypedef signed   long sptr;  // NOLINT
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#endif  // defined(_WIN64)
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#if defined(__x86_64__)
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// 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// WARNING: OFF_T may be different from OS type off_t, depending on the value of
70// _FILE_OFFSET_BITS. This definition of OFF_T matches the ABI of system calls
71// like pread and mmap, as opposed to pread64 and mmap64.
72// Mac and Linux/x86-64 are special.
73#if defined(__APPLE__) || (defined(__linux__) && defined(__x86_64__))
74typedef u64 OFF_T;
75#else
76typedef uptr OFF_T;
77#endif
78typedef u64  OFF64_T;
79}  // namespace __sanitizer
80
81extern "C" {
82  // Tell the tools to write their reports to "path.<pid>" instead of stderr.
83  void __sanitizer_set_report_path(const char *path)
84      SANITIZER_INTERFACE_ATTRIBUTE;
85
86  // Tell the tools to write their reports to given file descriptor instead of
87  // stderr.
88  void __sanitizer_set_report_fd(int fd)
89      SANITIZER_INTERFACE_ATTRIBUTE;
90
91  // Notify the tools that the sandbox is going to be turned on. The reserved
92  // parameter will be used in the future to hold a structure with functions
93  // that the tools may call to bypass the sandbox.
94  void __sanitizer_sandbox_on_notify(void *reserved)
95      SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE;
96
97  // This function is called by the tool when it has just finished reporting
98  // an error. 'error_summary' is a one-line string that summarizes
99  // the error message. This function can be overridden by the client.
100  void __sanitizer_report_error_summary(const char *error_summary)
101      SANITIZER_WEAK_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE;
102}  // extern "C"
103
104
105using namespace __sanitizer;  // NOLINT
106// ----------- ATTENTION -------------
107// This header should NOT include any other headers to avoid portability issues.
108
109// Common defs.
110#define INLINE static inline
111#define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
112#define WEAK SANITIZER_WEAK_ATTRIBUTE
113
114// Platform-specific defs.
115#if defined(_MSC_VER)
116# define ALWAYS_INLINE __declspec(forceinline)
117// FIXME(timurrrr): do we need this on Windows?
118# define ALIAS(x)
119# define ALIGNED(x) __declspec(align(x))
120# define FORMAT(f, a)
121# define NOINLINE __declspec(noinline)
122# define NORETURN __declspec(noreturn)
123# define THREADLOCAL   __declspec(thread)
124# define NOTHROW
125# define LIKELY(x) (x)
126# define UNLIKELY(x) (x)
127# define UNUSED
128# define USED
129# define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */
130#else  // _MSC_VER
131# define ALWAYS_INLINE __attribute__((always_inline))
132# define ALIAS(x) __attribute__((alias(x)))
133# define ALIGNED(x) __attribute__((aligned(x)))
134# define FORMAT(f, a)  __attribute__((format(printf, f, a)))
135# define NOINLINE __attribute__((noinline))
136# define NORETURN  __attribute__((noreturn))
137# define THREADLOCAL   __thread
138# define NOTHROW throw()
139# define LIKELY(x)     __builtin_expect(!!(x), 1)
140# define UNLIKELY(x)   __builtin_expect(!!(x), 0)
141# define UNUSED __attribute__((unused))
142# define USED __attribute__((used))
143# if defined(__i386__) || defined(__x86_64__)
144// __builtin_prefetch(x) generates prefetchnt0 on x86
145#  define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
146# else
147#  define PREFETCH(x) __builtin_prefetch(x)
148# endif
149#endif  // _MSC_VER
150
151#if defined(_WIN32)
152typedef unsigned long DWORD;  // NOLINT
153typedef DWORD thread_return_t;
154# define THREAD_CALLING_CONV __stdcall
155#else  // _WIN32
156typedef void* thread_return_t;
157# define THREAD_CALLING_CONV
158#endif  // _WIN32
159typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
160
161#if __LP64__ || defined(_WIN64)
162#  define SANITIZER_WORDSIZE 64
163#else
164#  define SANITIZER_WORDSIZE 32
165#endif
166
167// NOTE: Functions below must be defined in each run-time.
168namespace __sanitizer {
169void NORETURN Die();
170void NORETURN CheckFailed(const char *file, int line, const char *cond,
171                          u64 v1, u64 v2);
172}  // namespace __sanitizer
173
174// Check macro
175#define RAW_CHECK_MSG(expr, msg) do { \
176  if (!(expr)) { \
177    RawWrite(msg); \
178    Die(); \
179  } \
180} while (0)
181
182#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
183
184#define CHECK_IMPL(c1, op, c2) \
185  do { \
186    __sanitizer::u64 v1 = (u64)(c1); \
187    __sanitizer::u64 v2 = (u64)(c2); \
188    if (!(v1 op v2)) \
189      __sanitizer::CheckFailed(__FILE__, __LINE__, \
190        "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
191  } while (false) \
192/**/
193
194#define CHECK(a)       CHECK_IMPL((a), !=, 0)
195#define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
196#define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
197#define CHECK_LT(a, b) CHECK_IMPL((a), <,  (b))
198#define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
199#define CHECK_GT(a, b) CHECK_IMPL((a), >,  (b))
200#define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
201
202#if TSAN_DEBUG
203#define DCHECK(a)       CHECK(a)
204#define DCHECK_EQ(a, b) CHECK_EQ(a, b)
205#define DCHECK_NE(a, b) CHECK_NE(a, b)
206#define DCHECK_LT(a, b) CHECK_LT(a, b)
207#define DCHECK_LE(a, b) CHECK_LE(a, b)
208#define DCHECK_GT(a, b) CHECK_GT(a, b)
209#define DCHECK_GE(a, b) CHECK_GE(a, b)
210#else
211#define DCHECK(a)
212#define DCHECK_EQ(a, b)
213#define DCHECK_NE(a, b)
214#define DCHECK_LT(a, b)
215#define DCHECK_LE(a, b)
216#define DCHECK_GT(a, b)
217#define DCHECK_GE(a, b)
218#endif
219
220#define UNREACHABLE(msg) do { \
221  CHECK(0 && msg); \
222  Die(); \
223} while (0)
224
225#define UNIMPLEMENTED() UNREACHABLE("unimplemented")
226
227#define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
228
229#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
230
231#define IMPL_PASTE(a, b) a##b
232#define IMPL_COMPILER_ASSERT(pred, line) \
233    typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
234
235// Limits for integral types. We have to redefine it in case we don't
236// have stdint.h (like in Visual Studio 9).
237#undef __INT64_C
238#undef __UINT64_C
239#if SANITIZER_WORDSIZE == 64
240# define __INT64_C(c)  c ## L
241# define __UINT64_C(c) c ## UL
242#else
243# define __INT64_C(c)  c ## LL
244# define __UINT64_C(c) c ## ULL
245#endif  // SANITIZER_WORDSIZE == 64
246#undef INT32_MIN
247#define INT32_MIN              (-2147483647-1)
248#undef INT32_MAX
249#define INT32_MAX              (2147483647)
250#undef UINT32_MAX
251#define UINT32_MAX             (4294967295U)
252#undef INT64_MIN
253#define INT64_MIN              (-__INT64_C(9223372036854775807)-1)
254#undef INT64_MAX
255#define INT64_MAX              (__INT64_C(9223372036854775807))
256#undef UINT64_MAX
257#define UINT64_MAX             (__UINT64_C(18446744073709551615))
258
259enum LinkerInitialized { LINKER_INITIALIZED = 0 };
260
261#if !defined(_MSC_VER) || defined(__clang__)
262# define GET_CALLER_PC() (uptr)__builtin_return_address(0)
263# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
264#else
265extern "C" void* _ReturnAddress(void);
266# pragma intrinsic(_ReturnAddress)
267# define GET_CALLER_PC() (uptr)_ReturnAddress()
268// CaptureStackBackTrace doesn't need to know BP on Windows.
269// FIXME: This macro is still used when printing error reports though it's not
270// clear if the BP value is needed in the ASan reports on Windows.
271# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
272#endif
273
274#define HANDLE_EINTR(res, f) {                               \
275  do {                                                                  \
276    res = (f);                                                         \
277  } while (res == -1 && errno == EINTR); \
278  }
279
280#endif  // SANITIZER_DEFS_H
281