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