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