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