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