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