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 
17 #ifndef SANITIZER_DEBUG
18 # define SANITIZER_DEBUG 0
19 #endif
20 
21 #define SANITIZER_STRINGIFY_(S) #S
22 #define SANITIZER_STRINGIFY(S) SANITIZER_STRINGIFY_(S)
23 
24 // Only use SANITIZER_*ATTRIBUTE* before the function return type!
25 #if SANITIZER_WINDOWS
26 #if SANITIZER_IMPORT_INTERFACE
27 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllimport)
28 #else
29 # define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport)
30 #endif
31 # define SANITIZER_WEAK_ATTRIBUTE
32 #elif SANITIZER_GO
33 # define SANITIZER_INTERFACE_ATTRIBUTE
34 # define SANITIZER_WEAK_ATTRIBUTE
35 #else
36 # define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))
37 # define SANITIZER_WEAK_ATTRIBUTE  __attribute__((weak))
38 #endif
39 
40 // TLS is handled differently on different platforms
41 #if SANITIZER_LINUX || SANITIZER_NETBSD || \
42   SANITIZER_FREEBSD || SANITIZER_OPENBSD
43 # define SANITIZER_TLS_INITIAL_EXEC_ATTRIBUTE \
44     __attribute__((tls_model("initial-exec"))) thread_local
45 #else
46 # define SANITIZER_TLS_INITIAL_EXEC_ATTRIBUTE
47 #endif
48 
49 //--------------------------- WEAK FUNCTIONS ---------------------------------//
50 // When working with weak functions, to simplify the code and make it more
51 // portable, when possible define a default implementation using this macro:
52 //
53 // SANITIZER_INTERFACE_WEAK_DEF(<return_type>, <name>, <parameter list>)
54 //
55 // For example:
56 //   SANITIZER_INTERFACE_WEAK_DEF(bool, compare, int a, int b) { return a > b; }
57 //
58 #if SANITIZER_WINDOWS
59 #include "sanitizer_win_defs.h"
60 # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...)                   \
61   WIN_WEAK_EXPORT_DEF(ReturnType, Name, __VA_ARGS__)
62 #else
63 # define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...)                   \
64   extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE            \
65   ReturnType Name(__VA_ARGS__)
66 #endif
67 
68 // SANITIZER_SUPPORTS_WEAK_HOOKS means that we support real weak functions that
69 // will evaluate to a null pointer when not defined.
70 #ifndef SANITIZER_SUPPORTS_WEAK_HOOKS
71 #if (SANITIZER_LINUX || SANITIZER_SOLARIS) && !SANITIZER_GO
72 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
73 // Before Xcode 4.5, the Darwin linker doesn't reliably support undefined
74 // weak symbols.  Mac OS X 10.9/Darwin 13 is the first release only supported
75 // by Xcode >= 4.5.
76 #elif SANITIZER_MAC && \
77     __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1090 && !SANITIZER_GO
78 # define SANITIZER_SUPPORTS_WEAK_HOOKS 1
79 #else
80 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
81 #endif
82 #endif // SANITIZER_SUPPORTS_WEAK_HOOKS
83 // For some weak hooks that will be called very often and we want to avoid the
84 // overhead of executing the default implementation when it is not necessary,
85 // we can use the flag SANITIZER_SUPPORTS_WEAK_HOOKS to only define the default
86 // implementation for platforms that doesn't support weak symbols. For example:
87 //
88 //   #if !SANITIZER_SUPPORT_WEAK_HOOKS
89 //     SANITIZER_INTERFACE_WEAK_DEF(bool, compare_hook, int a, int b) {
90 //       return a > b;
91 //     }
92 //   #endif
93 //
94 // And then use it as: if (compare_hook) compare_hook(a, b);
95 //----------------------------------------------------------------------------//
96 
97 
98 // We can use .preinit_array section on Linux to call sanitizer initialization
99 // functions very early in the process startup (unless PIC macro is defined).
100 //
101 // On FreeBSD, .preinit_array functions are called with rtld_bind_lock writer
102 // lock held. It will lead to dead lock if unresolved PLT functions (which helds
103 // rtld_bind_lock reader lock) are called inside .preinit_array functions.
104 //
105 // FIXME: do we have anything like this on Mac?
106 #ifndef SANITIZER_CAN_USE_PREINIT_ARRAY
107 #if ((SANITIZER_LINUX && !SANITIZER_ANDROID) || SANITIZER_OPENBSD || \
108      SANITIZER_FUCHSIA || SANITIZER_NETBSD) && !defined(PIC)
109 #define SANITIZER_CAN_USE_PREINIT_ARRAY 1
110 // Before Solaris 11.4, .preinit_array is fully supported only with GNU ld.
111 // FIXME: Check for those conditions.
112 #elif SANITIZER_SOLARIS && !defined(PIC)
113 # define SANITIZER_CAN_USE_PREINIT_ARRAY 1
114 #else
115 # define SANITIZER_CAN_USE_PREINIT_ARRAY 0
116 #endif
117 #endif  // SANITIZER_CAN_USE_PREINIT_ARRAY
118 
119 // GCC does not understand __has_feature
120 #if !defined(__has_feature)
121 # define __has_feature(x) 0
122 #endif
123 
124 // Older GCCs do not understand __has_attribute.
125 #if !defined(__has_attribute)
126 # define __has_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 typedef unsigned long uptr;
140 typedef signed long sptr;
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 || \
173     SANITIZER_OPENBSD || SANITIZER_MAC || \
174     (SANITIZER_SOLARIS && (defined(_LP64) || _FILE_OFFSET_BITS == 64)) || \
175     (SANITIZER_LINUX && defined(__x86_64__))
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_MAC
183 typedef uptr operator_new_size_type;
184 #else
185 # if SANITIZER_OPENBSD || defined(__s390__) && !defined(__s390x__) || \
186      SANITIZER_EMSCRIPTEN
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;
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)
Trap()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)
Trap()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 {
424 using namespace __sanitizer;
425 }
426 namespace __dsan {
427 using namespace __sanitizer;
428 }
429 namespace __dfsan {
430 using namespace __sanitizer;
431 }
432 namespace __lsan {
433 using namespace __sanitizer;
434 }
435 namespace __msan {
436 using namespace __sanitizer;
437 }
438 namespace __hwasan {
439 using namespace __sanitizer;
440 }
441 namespace __tsan {
442 using namespace __sanitizer;
443 }
444 namespace __scudo {
445 using namespace __sanitizer;
446 }
447 namespace __ubsan {
448 using namespace __sanitizer;
449 }
450 namespace __xray {
451 using namespace __sanitizer;
452 }
453 namespace __interception {
454 using namespace __sanitizer;
455 }
456 namespace __hwasan {
457 using namespace __sanitizer;
458 }
459 
460 #endif  // SANITIZER_DEFS_H
461