sanitizer_internal_defs.h revision 5af39e50366f1aacbebc284f572f08ad1ad07357
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/common_interface_defs.h"
17using namespace __sanitizer;  // NOLINT
18// ----------- ATTENTION -------------
19// This header should NOT include any other headers to avoid portability issues.
20
21// Common defs.
22#define INLINE static inline
23#define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
24#define WEAK SANITIZER_WEAK_ATTRIBUTE
25
26// Platform-specific defs.
27#if defined(_MSC_VER)
28# define ALWAYS_INLINE __declspec(forceinline)
29// FIXME(timurrrr): do we need this on Windows?
30# define ALIAS(x)
31# define ALIGNED(x) __declspec(align(x))
32# define FORMAT(f, a)
33# define NOINLINE __declspec(noinline)
34# define NORETURN __declspec(noreturn)
35# define THREADLOCAL   __declspec(thread)
36# define NOTHROW
37# define LIKELY(x) (x)
38# define UNLIKELY(x) (x)
39# define UNUSED
40# define USED
41#else  // _MSC_VER
42# define ALWAYS_INLINE __attribute__((always_inline))
43# define ALIAS(x) __attribute__((alias(x)))
44# define ALIGNED(x) __attribute__((aligned(x)))
45# define FORMAT(f, a)  __attribute__((format(printf, f, a)))
46# define NOINLINE __attribute__((noinline))
47# define NORETURN  __attribute__((noreturn))
48# define THREADLOCAL   __thread
49# define NOTHROW throw()
50# define LIKELY(x)     __builtin_expect(!!(x), 1)
51# define UNLIKELY(x)   __builtin_expect(!!(x), 0)
52# define UNUSED __attribute__((unused))
53# define USED __attribute__((used))
54#endif  // _MSC_VER
55
56#if defined(_WIN32)
57typedef unsigned long DWORD;  // NOLINT
58typedef DWORD thread_return_t;
59# define THREAD_CALLING_CONV __stdcall
60#else  // _WIN32
61typedef void* thread_return_t;
62# define THREAD_CALLING_CONV
63#endif  // _WIN32
64typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
65
66#if __LP64__ || defined(_WIN64)
67#  define SANITIZER_WORDSIZE 64
68#else
69#  define SANITIZER_WORDSIZE 32
70#endif
71
72// NOTE: Functions below must be defined in each run-time.
73namespace __sanitizer {
74void NORETURN Die();
75void NORETURN CheckFailed(const char *file, int line, const char *cond,
76                          u64 v1, u64 v2);
77}  // namespace __sanitizer
78
79// Check macro
80#define RAW_CHECK_MSG(expr, msg) do { \
81  if (!(expr)) { \
82    RawWrite(msg); \
83    Die(); \
84  } \
85} while (0)
86
87#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
88
89#define CHECK_IMPL(c1, op, c2) \
90  do { \
91    __sanitizer::u64 v1 = (u64)(c1); \
92    __sanitizer::u64 v2 = (u64)(c2); \
93    if (!(v1 op v2)) \
94      __sanitizer::CheckFailed(__FILE__, __LINE__, \
95        "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
96  } while (false) \
97/**/
98
99#define CHECK(a)       CHECK_IMPL((a), !=, 0)
100#define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
101#define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
102#define CHECK_LT(a, b) CHECK_IMPL((a), <,  (b))
103#define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
104#define CHECK_GT(a, b) CHECK_IMPL((a), >,  (b))
105#define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
106
107#if TSAN_DEBUG
108#define DCHECK(a)       CHECK(a)
109#define DCHECK_EQ(a, b) CHECK_EQ(a, b)
110#define DCHECK_NE(a, b) CHECK_NE(a, b)
111#define DCHECK_LT(a, b) CHECK_LT(a, b)
112#define DCHECK_LE(a, b) CHECK_LE(a, b)
113#define DCHECK_GT(a, b) CHECK_GT(a, b)
114#define DCHECK_GE(a, b) CHECK_GE(a, b)
115#else
116#define DCHECK(a)
117#define DCHECK_EQ(a, b)
118#define DCHECK_NE(a, b)
119#define DCHECK_LT(a, b)
120#define DCHECK_LE(a, b)
121#define DCHECK_GT(a, b)
122#define DCHECK_GE(a, b)
123#endif
124
125#define UNREACHABLE(msg) do { \
126  CHECK(0 && msg); \
127  Die(); \
128} while (0)
129
130#define UNIMPLEMENTED() UNREACHABLE("unimplemented")
131
132#define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
133
134#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
135
136#define IMPL_PASTE(a, b) a##b
137#define IMPL_COMPILER_ASSERT(pred, line) \
138    typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
139
140// Limits for integral types. We have to redefine it in case we don't
141// have stdint.h (like in Visual Studio 9).
142#undef __INT64_C
143#undef __UINT64_C
144#if SANITIZER_WORDSIZE == 64
145# define __INT64_C(c)  c ## L
146# define __UINT64_C(c) c ## UL
147#else
148# define __INT64_C(c)  c ## LL
149# define __UINT64_C(c) c ## ULL
150#endif  // SANITIZER_WORDSIZE == 64
151#undef INT32_MIN
152#define INT32_MIN              (-2147483647-1)
153#undef INT32_MAX
154#define INT32_MAX              (2147483647)
155#undef UINT32_MAX
156#define UINT32_MAX             (4294967295U)
157#undef INT64_MIN
158#define INT64_MIN              (-__INT64_C(9223372036854775807)-1)
159#undef INT64_MAX
160#define INT64_MAX              (__INT64_C(9223372036854775807))
161#undef UINT64_MAX
162#define UINT64_MAX             (__UINT64_C(18446744073709551615))
163
164enum LinkerInitialized { LINKER_INITIALIZED = 0 };
165
166#if !defined(_MSC_VER) || defined(__clang__)
167# define GET_CALLER_PC() (uptr)__builtin_return_address(0)
168# define GET_CURRENT_FRAME() (uptr)__builtin_frame_address(0)
169#else
170extern "C" void* _ReturnAddress(void);
171# pragma intrinsic(_ReturnAddress)
172# define GET_CALLER_PC() (uptr)_ReturnAddress()
173// CaptureStackBackTrace doesn't need to know BP on Windows.
174// FIXME: This macro is still used when printing error reports though it's not
175// clear if the BP value is needed in the ASan reports on Windows.
176# define GET_CURRENT_FRAME() (uptr)0xDEADBEEF
177#endif
178
179#define HANDLE_EINTR(res, f) {                               \
180  do {                                                                  \
181    res = (f);                                                         \
182  } while (res == -1 && errno == EINTR); \
183  }
184
185#endif  // SANITIZER_DEFS_H
186