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