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