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