]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h
Merge ACPICA 20170531.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_internal_defs.h
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 #ifndef SANITIZER_DEBUG
19 # define SANITIZER_DEBUG 0
20 #endif
21
22 // Only use SANITIZER_*ATTRIBUTE* before the function return type!
23 #if SANITIZER_WINDOWS
24 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport)
25 // FIXME find out what we need on Windows, if anything.
26 # define SANITIZER_WEAK_ATTRIBUTE
27 #elif SANITIZER_GO
28 # define SANITIZER_INTERFACE_ATTRIBUTE
29 # define SANITIZER_WEAK_ATTRIBUTE
30 #else
31 # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
32 # define SANITIZER_WEAK_ATTRIBUTE  __attribute__((weak))
33 #endif
34
35 #if (SANITIZER_LINUX || SANITIZER_MAC || SANITIZER_WINDOWS) && !SANITIZER_GO
36 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
37 #else
38 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
39 #endif
40
41 // We can use .preinit_array section on Linux to call sanitizer initialization
42 // functions very early in the process startup (unless PIC macro is defined).
43 // FIXME: do we have anything like this on Mac?
44 #if SANITIZER_LINUX && !SANITIZER_ANDROID && !defined(PIC)
45 # define SANITIZER_CAN_USE_PREINIT_ARRAY 1
46 #else
47 # define SANITIZER_CAN_USE_PREINIT_ARRAY 0
48 #endif
49
50 // GCC does not understand __has_feature
51 #if !defined(__has_feature)
52 # define __has_feature(x) 0
53 #endif
54
55 // For portability reasons we do not include stddef.h, stdint.h or any other
56 // system header, but we do need some basic types that are not defined
57 // in a portable way by the language itself.
58 namespace __sanitizer {
59
60 #if defined(_WIN64)
61 // 64-bit Windows uses LLP64 data model.
62 typedef unsigned long long uptr;  // NOLINT
63 typedef signed   long long sptr;  // NOLINT
64 #else
65 typedef unsigned long uptr;  // NOLINT
66 typedef signed   long sptr;  // NOLINT
67 #endif  // defined(_WIN64)
68 #if defined(__x86_64__)
69 // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
70 // 64-bit pointer to unwind stack frame.
71 typedef unsigned long long uhwptr;  // NOLINT
72 #else
73 typedef uptr uhwptr;   // NOLINT
74 #endif
75 typedef unsigned char u8;
76 typedef unsigned short u16;  // NOLINT
77 typedef unsigned int u32;
78 typedef unsigned long long u64;  // NOLINT
79 typedef signed   char s8;
80 typedef signed   short s16;  // NOLINT
81 typedef signed   int s32;
82 typedef signed   long long s64;  // NOLINT
83 #if SANITIZER_WINDOWS
84 // On Windows, files are HANDLE, which is a synonim of void*.
85 // Use void* to avoid including <windows.h> everywhere.
86 typedef void* fd_t;
87 typedef unsigned error_t;
88 #else
89 typedef int fd_t;
90 typedef int error_t;
91 #endif
92 typedef int pid_t;
93
94 // WARNING: OFF_T may be different from OS type off_t, depending on the value of
95 // _FILE_OFFSET_BITS. This definition of OFF_T matches the ABI of system calls
96 // like pread and mmap, as opposed to pread64 and mmap64.
97 // FreeBSD, Mac and Linux/x86-64 are special.
98 #if SANITIZER_FREEBSD || SANITIZER_MAC || \
99   (SANITIZER_LINUX && defined(__x86_64__))
100 typedef u64 OFF_T;
101 #else
102 typedef uptr OFF_T;
103 #endif
104 typedef u64  OFF64_T;
105
106 #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
107 typedef uptr operator_new_size_type;
108 #else
109 # if defined(__s390__) && !defined(__s390x__)
110 // Special case: 31-bit s390 has unsigned long as size_t.
111 typedef unsigned long operator_new_size_type;
112 # else
113 typedef u32 operator_new_size_type;
114 # endif
115 #endif
116
117
118 // ----------- ATTENTION -------------
119 // This header should NOT include any other headers to avoid portability issues.
120
121 // Common defs.
122 #define INLINE inline
123 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
124 #define SANITIZER_WEAK_DEFAULT_IMPL \
125   extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
126 #define SANITIZER_WEAK_CXX_DEFAULT_IMPL \
127   extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
128
129 // Platform-specific defs.
130 #if defined(_MSC_VER)
131 # define ALWAYS_INLINE __forceinline
132 // FIXME(timurrrr): do we need this on Windows?
133 # define ALIAS(x)
134 # define ALIGNED(x) __declspec(align(x))
135 # define FORMAT(f, a)
136 # define NOINLINE __declspec(noinline)
137 # define NORETURN __declspec(noreturn)
138 # define THREADLOCAL   __declspec(thread)
139 # define LIKELY(x) (x)
140 # define UNLIKELY(x) (x)
141 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0
142 #else  // _MSC_VER
143 # define ALWAYS_INLINE inline __attribute__((always_inline))
144 # define ALIAS(x) __attribute__((alias(x)))
145 // Please only use the ALIGNED macro before the type.
146 // Using ALIGNED after the variable declaration is not portable!
147 # define ALIGNED(x) __attribute__((aligned(x)))
148 # define FORMAT(f, a)  __attribute__((format(printf, f, a)))
149 # define NOINLINE __attribute__((noinline))
150 # define NORETURN  __attribute__((noreturn))
151 # define THREADLOCAL   __thread
152 # define LIKELY(x)     __builtin_expect(!!(x), 1)
153 # define UNLIKELY(x)   __builtin_expect(!!(x), 0)
154 # if defined(__i386__) || defined(__x86_64__)
155 // __builtin_prefetch(x) generates prefetchnt0 on x86
156 #  define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
157 # else
158 #  define PREFETCH(x) __builtin_prefetch(x)
159 # endif
160 #endif  // _MSC_VER
161
162 #if !defined(_MSC_VER) || defined(__clang__)
163 # define UNUSED __attribute__((unused))
164 # define USED __attribute__((used))
165 #else
166 # define UNUSED
167 # define USED
168 #endif
169
170 #if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)
171 # define NOEXCEPT noexcept
172 #else
173 # define NOEXCEPT throw()
174 #endif
175
176 // Unaligned versions of basic types.
177 typedef ALIGNED(1) u16 uu16;
178 typedef ALIGNED(1) u32 uu32;
179 typedef ALIGNED(1) u64 uu64;
180 typedef ALIGNED(1) s16 us16;
181 typedef ALIGNED(1) s32 us32;
182 typedef ALIGNED(1) s64 us64;
183
184 #if SANITIZER_WINDOWS
185 }  // namespace __sanitizer
186 typedef unsigned long DWORD;  // NOLINT
187 namespace __sanitizer {
188 typedef DWORD thread_return_t;
189 # define THREAD_CALLING_CONV __stdcall
190 #else  // _WIN32
191 typedef void* thread_return_t;
192 # define THREAD_CALLING_CONV
193 #endif  // _WIN32
194 typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
195
196 // NOTE: Functions below must be defined in each run-time.
197 void NORETURN Die();
198
199 // FIXME: No, this shouldn't be in the sanitizer interface.
200 SANITIZER_INTERFACE_ATTRIBUTE
201 void NORETURN CheckFailed(const char *file, int line, const char *cond,
202                           u64 v1, u64 v2);
203
204 // Check macro
205 #define RAW_CHECK_MSG(expr, msg) do { \
206   if (UNLIKELY(!(expr))) { \
207     RawWrite(msg); \
208     Die(); \
209   } \
210 } while (0)
211
212 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
213
214 #define CHECK_IMPL(c1, op, c2) \
215   do { \
216     __sanitizer::u64 v1 = (u64)(c1); \
217     __sanitizer::u64 v2 = (u64)(c2); \
218     if (UNLIKELY(!(v1 op v2))) \
219       __sanitizer::CheckFailed(__FILE__, __LINE__, \
220         "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
221   } while (false) \
222 /**/
223
224 #define CHECK(a)       CHECK_IMPL((a), !=, 0)
225 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
226 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
227 #define CHECK_LT(a, b) CHECK_IMPL((a), <,  (b))
228 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
229 #define CHECK_GT(a, b) CHECK_IMPL((a), >,  (b))
230 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
231
232 #if SANITIZER_DEBUG
233 #define DCHECK(a)       CHECK(a)
234 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
235 #define DCHECK_NE(a, b) CHECK_NE(a, b)
236 #define DCHECK_LT(a, b) CHECK_LT(a, b)
237 #define DCHECK_LE(a, b) CHECK_LE(a, b)
238 #define DCHECK_GT(a, b) CHECK_GT(a, b)
239 #define DCHECK_GE(a, b) CHECK_GE(a, b)
240 #else
241 #define DCHECK(a)
242 #define DCHECK_EQ(a, b)
243 #define DCHECK_NE(a, b)
244 #define DCHECK_LT(a, b)
245 #define DCHECK_LE(a, b)
246 #define DCHECK_GT(a, b)
247 #define DCHECK_GE(a, b)
248 #endif
249
250 #define UNREACHABLE(msg) do { \
251   CHECK(0 && msg); \
252   Die(); \
253 } while (0)
254
255 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
256
257 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
258
259 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
260
261 #define IMPL_PASTE(a, b) a##b
262 #define IMPL_COMPILER_ASSERT(pred, line) \
263     typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
264
265 // Limits for integral types. We have to redefine it in case we don't
266 // have stdint.h (like in Visual Studio 9).
267 #undef __INT64_C
268 #undef __UINT64_C
269 #if SANITIZER_WORDSIZE == 64
270 # define __INT64_C(c)  c ## L
271 # define __UINT64_C(c) c ## UL
272 #else
273 # define __INT64_C(c)  c ## LL
274 # define __UINT64_C(c) c ## ULL
275 #endif  // SANITIZER_WORDSIZE == 64
276 #undef INT32_MIN
277 #define INT32_MIN              (-2147483647-1)
278 #undef INT32_MAX
279 #define INT32_MAX              (2147483647)
280 #undef UINT32_MAX
281 #define UINT32_MAX             (4294967295U)
282 #undef INT64_MIN
283 #define INT64_MIN              (-__INT64_C(9223372036854775807)-1)
284 #undef INT64_MAX
285 #define INT64_MAX              (__INT64_C(9223372036854775807))
286 #undef UINT64_MAX
287 #define UINT64_MAX             (__UINT64_C(18446744073709551615))
288
289 enum LinkerInitialized { LINKER_INITIALIZED = 0 };
290
291 #if !defined(_MSC_VER) || defined(__clang__)
292 #define GET_CALLER_PC() (__sanitizer::uptr) __builtin_return_address(0)
293 #define GET_CURRENT_FRAME() (__sanitizer::uptr) __builtin_frame_address(0)
294 inline void Trap() {
295   __builtin_trap();
296 }
297 #else
298 extern "C" void* _ReturnAddress(void);
299 extern "C" void* _AddressOfReturnAddress(void);
300 # pragma intrinsic(_ReturnAddress)
301 # pragma intrinsic(_AddressOfReturnAddress)
302 #define GET_CALLER_PC() (__sanitizer::uptr) _ReturnAddress()
303 // CaptureStackBackTrace doesn't need to know BP on Windows.
304 #define GET_CURRENT_FRAME() \
305   (((__sanitizer::uptr)_AddressOfReturnAddress()) + sizeof(__sanitizer::uptr))
306
307 extern "C" void __ud2(void);
308 # pragma intrinsic(__ud2)
309 inline void Trap() {
310   __ud2();
311 }
312 #endif
313
314 #define HANDLE_EINTR(res, f)                                       \
315   {                                                                \
316     int rverrno;                                                   \
317     do {                                                           \
318       res = (f);                                                   \
319     } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
320   }
321
322 // Forces the compiler to generate a frame pointer in the function.
323 #define ENABLE_FRAME_POINTER              \
324   do {                                    \
325     volatile __sanitizer::uptr enable_fp; \
326     enable_fp = GET_CURRENT_FRAME();      \
327     (void)enable_fp;                      \
328   } while (0)
329
330 }  // namespace __sanitizer
331
332 namespace __asan  { using namespace __sanitizer; }  // NOLINT
333 namespace __dsan  { using namespace __sanitizer; }  // NOLINT
334 namespace __dfsan { using namespace __sanitizer; }  // NOLINT
335 namespace __esan  { using namespace __sanitizer; }  // NOLINT
336 namespace __lsan  { using namespace __sanitizer; }  // NOLINT
337 namespace __msan  { using namespace __sanitizer; }  // NOLINT
338 namespace __tsan  { using namespace __sanitizer; }  // NOLINT
339 namespace __scudo { using namespace __sanitizer; }  // NOLINT
340 namespace __ubsan { using namespace __sanitizer; }  // NOLINT
341 namespace __xray  { using namespace __sanitizer; }  // NOLINT
342 namespace __interception  { using namespace __sanitizer; }  // NOLINT
343
344
345 #endif  // SANITIZER_DEFS_H