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 // 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 #ifndef INLINE
200 #define INLINE inline
201 #endif
202 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE
203 #define SANITIZER_WEAK_DEFAULT_IMPL \
204   extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
205 #define SANITIZER_WEAK_CXX_DEFAULT_IMPL \
206   extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE
207 
208 // Platform-specific defs.
209 #if defined(_MSC_VER)
210 # define ALWAYS_INLINE __forceinline
211 // FIXME(timurrrr): do we need this on Windows?
212 # define ALIAS(x)
213 # define ALIGNED(x) __declspec(align(x))
214 # define FORMAT(f, a)
215 # define NOINLINE __declspec(noinline)
216 # define NORETURN __declspec(noreturn)
217 # define THREADLOCAL   __declspec(thread)
218 # define LIKELY(x) (x)
219 # define UNLIKELY(x) (x)
220 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0
221 # define WARN_UNUSED_RESULT
222 #else  // _MSC_VER
223 # define ALWAYS_INLINE inline __attribute__((always_inline))
224 # define ALIAS(x) __attribute__((alias(x)))
225 // Please only use the ALIGNED macro before the type.
226 // Using ALIGNED after the variable declaration is not portable!
227 # define ALIGNED(x) __attribute__((aligned(x)))
228 # define FORMAT(f, a)  __attribute__((format(printf, f, a)))
229 # define NOINLINE __attribute__((noinline))
230 # define NORETURN  __attribute__((noreturn))
231 # define THREADLOCAL   __thread
232 # define LIKELY(x)     __builtin_expect(!!(x), 1)
233 # define UNLIKELY(x)   __builtin_expect(!!(x), 0)
234 # if defined(__i386__) || defined(__x86_64__)
235 // __builtin_prefetch(x) generates prefetchnt0 on x86
236 #  define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))
237 # else
238 #  define PREFETCH(x) __builtin_prefetch(x)
239 # endif
240 # define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
241 #endif  // _MSC_VER
242 
243 #if !defined(_MSC_VER) || defined(__clang__)
244 # define UNUSED __attribute__((unused))
245 # define USED __attribute__((used))
246 #else
247 # define UNUSED
248 # define USED
249 #endif
250 
251 #if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)
252 # define NOEXCEPT noexcept
253 #else
254 # define NOEXCEPT throw()
255 #endif
256 
257 // Unaligned versions of basic types.
258 typedef ALIGNED(1) u16 uu16;
259 typedef ALIGNED(1) u32 uu32;
260 typedef ALIGNED(1) u64 uu64;
261 typedef ALIGNED(1) s16 us16;
262 typedef ALIGNED(1) s32 us32;
263 typedef ALIGNED(1) s64 us64;
264 
265 #if SANITIZER_WINDOWS
266 }  // namespace __sanitizer
267 typedef unsigned long DWORD;
268 namespace __sanitizer {
269 typedef DWORD thread_return_t;
270 # define THREAD_CALLING_CONV __stdcall
271 #else  // _WIN32
272 typedef void* thread_return_t;
273 # define THREAD_CALLING_CONV
274 #endif  // _WIN32
275 typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
276 
277 // NOTE: Functions below must be defined in each run-time.
278 void NORETURN Die();
279 
280 void NORETURN CheckFailed(const char *file, int line, const char *cond,
281                           u64 v1, u64 v2);
282 
283 // Check macro
284 #define RAW_CHECK_MSG(expr, msg) do { \
285   if (UNLIKELY(!(expr))) { \
286     RawWrite(msg); \
287     Die(); \
288   } \
289 } while (0)
290 
291 #define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr)
292 
293 #define CHECK_IMPL(c1, op, c2) \
294   do { \
295     __sanitizer::u64 v1 = (__sanitizer::u64)(c1); \
296     __sanitizer::u64 v2 = (__sanitizer::u64)(c2); \
297     if (UNLIKELY(!(v1 op v2))) \
298       __sanitizer::CheckFailed(__FILE__, __LINE__, \
299         "(" #c1 ") " #op " (" #c2 ")", v1, v2); \
300   } while (false) \
301 /**/
302 
303 #define CHECK(a)       CHECK_IMPL((a), !=, 0)
304 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))
305 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))
306 #define CHECK_LT(a, b) CHECK_IMPL((a), <,  (b))
307 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))
308 #define CHECK_GT(a, b) CHECK_IMPL((a), >,  (b))
309 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))
310 
311 #if SANITIZER_DEBUG
312 #define DCHECK(a)       CHECK(a)
313 #define DCHECK_EQ(a, b) CHECK_EQ(a, b)
314 #define DCHECK_NE(a, b) CHECK_NE(a, b)
315 #define DCHECK_LT(a, b) CHECK_LT(a, b)
316 #define DCHECK_LE(a, b) CHECK_LE(a, b)
317 #define DCHECK_GT(a, b) CHECK_GT(a, b)
318 #define DCHECK_GE(a, b) CHECK_GE(a, b)
319 #else
320 #define DCHECK(a)
321 #define DCHECK_EQ(a, b)
322 #define DCHECK_NE(a, b)
323 #define DCHECK_LT(a, b)
324 #define DCHECK_LE(a, b)
325 #define DCHECK_GT(a, b)
326 #define DCHECK_GE(a, b)
327 #endif
328 
329 #define UNREACHABLE(msg) do { \
330   CHECK(0 && msg); \
331   Die(); \
332 } while (0)
333 
334 #define UNIMPLEMENTED() UNREACHABLE("unimplemented")
335 
336 #define COMPILER_CHECK(pred) IMPL_COMPILER_ASSERT(pred, __LINE__)
337 
338 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
339 
340 #define IMPL_PASTE(a, b) a##b
341 #define IMPL_COMPILER_ASSERT(pred, line) \
342     typedef char IMPL_PASTE(assertion_failed_##_, line)[2*(int)(pred)-1]
343 
344 // Limits for integral types. We have to redefine it in case we don't
345 // have stdint.h (like in Visual Studio 9).
346 #undef __INT64_C
347 #undef __UINT64_C
348 #if SANITIZER_WORDSIZE == 64
349 # define __INT64_C(c)  c ## L
350 # define __UINT64_C(c) c ## UL
351 #else
352 # define __INT64_C(c)  c ## LL
353 # define __UINT64_C(c) c ## ULL
354 #endif  // SANITIZER_WORDSIZE == 64
355 #undef INT32_MIN
356 #define INT32_MIN              (-2147483647-1)
357 #undef INT32_MAX
358 #define INT32_MAX              (2147483647)
359 #undef UINT32_MAX
360 #define UINT32_MAX             (4294967295U)
361 #undef INT64_MIN
362 #define INT64_MIN              (-__INT64_C(9223372036854775807)-1)
363 #undef INT64_MAX
364 #define INT64_MAX              (__INT64_C(9223372036854775807))
365 #undef UINT64_MAX
366 #define UINT64_MAX             (__UINT64_C(18446744073709551615))
367 #undef UINTPTR_MAX
368 #if SANITIZER_WORDSIZE == 64
369 # define UINTPTR_MAX           (18446744073709551615UL)
370 #else
371 # define UINTPTR_MAX           (4294967295U)
372 #endif  // SANITIZER_WORDSIZE == 64
373 
374 enum LinkerInitialized { LINKER_INITIALIZED = 0 };
375 
376 #if !defined(_MSC_VER) || defined(__clang__)
377 #if SANITIZER_S390_31
378 #define GET_CALLER_PC() \
379   (__sanitizer::uptr) __builtin_extract_return_addr(__builtin_return_address(0))
380 #else
381 #define GET_CALLER_PC() (__sanitizer::uptr) __builtin_return_address(0)
382 #endif
383 #define GET_CURRENT_FRAME() (__sanitizer::uptr) __builtin_frame_address(0)
384 inline void Trap() {
385   __builtin_trap();
386 }
387 #else
388 extern "C" void* _ReturnAddress(void);
389 extern "C" void* _AddressOfReturnAddress(void);
390 # pragma intrinsic(_ReturnAddress)
391 # pragma intrinsic(_AddressOfReturnAddress)
392 #define GET_CALLER_PC() (__sanitizer::uptr) _ReturnAddress()
393 // CaptureStackBackTrace doesn't need to know BP on Windows.
394 #define GET_CURRENT_FRAME() \
395   (((__sanitizer::uptr)_AddressOfReturnAddress()) + sizeof(__sanitizer::uptr))
396 
397 extern "C" void __ud2(void);
398 # pragma intrinsic(__ud2)
399 inline void Trap() {
400   __ud2();
401 }
402 #endif
403 
404 #define HANDLE_EINTR(res, f)                                       \
405   {                                                                \
406     int rverrno;                                                   \
407     do {                                                           \
408       res = (f);                                                   \
409     } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \
410   }
411 
412 // Forces the compiler to generate a frame pointer in the function.
413 #define ENABLE_FRAME_POINTER              \
414   do {                                    \
415     volatile __sanitizer::uptr enable_fp; \
416     enable_fp = GET_CURRENT_FRAME();      \
417     (void)enable_fp;                      \
418   } while (0)
419 
420 }  // namespace __sanitizer
421 
422 namespace __asan {
423 using namespace __sanitizer;
424 }
425 namespace __dsan {
426 using namespace __sanitizer;
427 }
428 namespace __dfsan {
429 using namespace __sanitizer;
430 }
431 namespace __lsan {
432 using namespace __sanitizer;
433 }
434 namespace __msan {
435 using namespace __sanitizer;
436 }
437 namespace __hwasan {
438 using namespace __sanitizer;
439 }
440 namespace __tsan {
441 using namespace __sanitizer;
442 }
443 namespace __scudo {
444 using namespace __sanitizer;
445 }
446 namespace __ubsan {
447 using namespace __sanitizer;
448 }
449 namespace __xray {
450 using namespace __sanitizer;
451 }
452 namespace __interception {
453 using namespace __sanitizer;
454 }
455 namespace __hwasan {
456 using namespace __sanitizer;
457 }
458 
459 #endif  // SANITIZER_DEFS_H
460