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 #if !defined(__has_cpp_attribute) 129 # define __has_cpp_attribute(x) 0 130 #endif 131 132 // For portability reasons we do not include stddef.h, stdint.h or any other 133 // system header, but we do need some basic types that are not defined 134 // in a portable way by the language itself. 135 namespace __sanitizer { 136 137 #if defined(_WIN64) 138 // 64-bit Windows uses LLP64 data model. 139 typedef unsigned long long uptr; 140 typedef signed long long sptr; 141 #else 142 # if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC || SANITIZER_WINDOWS || \ 143 (SANITIZER_NETBSD && \ 144 (defined(__sparc__) || defined(__hppa__) || defined(__arm__) || \ 145 (defined(__mips__) && !defined(__mips_o32)) || defined(__vax__))) 146 typedef unsigned long uptr; 147 typedef signed long sptr; 148 # else 149 typedef unsigned int uptr; 150 typedef signed int sptr; 151 # endif 152 #endif // defined(_WIN64) 153 #if defined(__x86_64__) 154 // Since x32 uses ILP32 data model in 64-bit hardware mode, we must use 155 // 64-bit pointer to unwind stack frame. 156 typedef unsigned long long uhwptr; 157 #else 158 typedef uptr uhwptr; 159 #endif 160 typedef unsigned char u8; 161 typedef unsigned short u16; 162 typedef unsigned int u32; 163 typedef unsigned long long u64; 164 typedef signed char s8; 165 typedef signed short s16; 166 typedef signed int s32; 167 typedef signed long long s64; 168 #if SANITIZER_WINDOWS 169 // On Windows, files are HANDLE, which is a synonim of void*. 170 // Use void* to avoid including <windows.h> everywhere. 171 typedef void* fd_t; 172 typedef unsigned error_t; 173 #else 174 typedef int fd_t; 175 typedef int error_t; 176 #endif 177 #if SANITIZER_SOLARIS && !defined(_LP64) 178 typedef long pid_t; 179 #else 180 typedef int pid_t; 181 #endif 182 183 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_MAC || \ 184 (SANITIZER_SOLARIS && (defined(_LP64) || _FILE_OFFSET_BITS == 64)) || \ 185 (SANITIZER_LINUX && (defined(__x86_64__) || defined(__hexagon__))) 186 typedef u64 OFF_T; 187 #else 188 typedef uptr OFF_T; 189 #endif 190 typedef u64 OFF64_T; 191 192 #if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC 193 typedef uptr operator_new_size_type; 194 #elifdef __SIZE_TYPE__ 195 typedef __SIZE_TYPE__ operator_new_size_type; 196 #else 197 # if (defined(__s390__) && !defined(__s390x__)) || \ 198 (SANITIZER_NETBSD && (defined(__sparc__) || defined(__hppa__) || defined(__mips__))) 199 // Special case: 31-bit s390 has unsigned long as size_t. 200 typedef unsigned long operator_new_size_type; 201 # else 202 typedef u32 operator_new_size_type; 203 # endif 204 #endif 205 206 typedef u64 tid_t; 207 208 // ----------- ATTENTION ------------- 209 // This header should NOT include any other headers to avoid portability issues. 210 211 // Common defs. 212 #define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE 213 #define SANITIZER_WEAK_DEFAULT_IMPL \ 214 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE 215 #define SANITIZER_WEAK_CXX_DEFAULT_IMPL \ 216 extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE 217 218 // Platform-specific defs. 219 #if defined(_MSC_VER) 220 # define ALWAYS_INLINE __forceinline 221 // FIXME(timurrrr): do we need this on Windows? 222 # define ALIAS(x) 223 # define ALIGNED(x) __declspec(align(x)) 224 # define FORMAT(f, a) 225 # define NOINLINE __declspec(noinline) 226 # define NORETURN __declspec(noreturn) 227 # define THREADLOCAL __declspec(thread) 228 # define LIKELY(x) (x) 229 # define UNLIKELY(x) (x) 230 # define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0 231 # define WARN_UNUSED_RESULT 232 #else // _MSC_VER 233 # define ALWAYS_INLINE inline __attribute__((always_inline)) 234 # define ALIAS(x) __attribute__((alias(x))) 235 // Please only use the ALIGNED macro before the type. 236 // Using ALIGNED after the variable declaration is not portable! 237 # define ALIGNED(x) __attribute__((aligned(x))) 238 # define FORMAT(f, a) __attribute__((format(printf, f, a))) 239 # define NOINLINE __attribute__((noinline)) 240 # define NORETURN __attribute__((noreturn)) 241 # define THREADLOCAL __thread 242 # define LIKELY(x) __builtin_expect(!!(x), 1) 243 # define UNLIKELY(x) __builtin_expect(!!(x), 0) 244 # if defined(__i386__) || defined(__x86_64__) 245 // __builtin_prefetch(x) generates prefetchnt0 on x86 246 # define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x)) 247 # else 248 # define PREFETCH(x) __builtin_prefetch(x) 249 # endif 250 # define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) 251 #endif // _MSC_VER 252 253 #if !defined(_MSC_VER) || defined(__clang__) 254 # define UNUSED __attribute__((unused)) 255 # define USED __attribute__((used)) 256 #else 257 # define UNUSED 258 # define USED 259 #endif 260 261 #if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900) 262 # define NOEXCEPT noexcept 263 #else 264 # define NOEXCEPT throw() 265 #endif 266 267 #if __has_cpp_attribute(clang::fallthrough) 268 # define FALLTHROUGH [[clang::fallthrough]] 269 #else 270 # define FALLTHROUGH 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", __VA_ARGS__) 310 311 #define CHECK_IMPL(c1, op, c2) \ 312 do { \ 313 __sanitizer::u64 v1 = (__sanitizer::u64)(c1); \ 314 __sanitizer::u64 v2 = (__sanitizer::u64)(c2); \ 315 if (UNLIKELY(!(v1 op v2))) \ 316 __sanitizer::CheckFailed(__FILE__, __LINE__, \ 317 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \ 318 } while (false) \ 319 /**/ 320 321 #define CHECK(a) CHECK_IMPL((a), !=, 0) 322 #define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b)) 323 #define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b)) 324 #define CHECK_LT(a, b) CHECK_IMPL((a), <, (b)) 325 #define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b)) 326 #define CHECK_GT(a, b) CHECK_IMPL((a), >, (b)) 327 #define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b)) 328 329 #if SANITIZER_DEBUG 330 #define DCHECK(a) CHECK(a) 331 #define DCHECK_EQ(a, b) CHECK_EQ(a, b) 332 #define DCHECK_NE(a, b) CHECK_NE(a, b) 333 #define DCHECK_LT(a, b) CHECK_LT(a, b) 334 #define DCHECK_LE(a, b) CHECK_LE(a, b) 335 #define DCHECK_GT(a, b) CHECK_GT(a, b) 336 #define DCHECK_GE(a, b) CHECK_GE(a, b) 337 #else 338 #define DCHECK(a) 339 #define DCHECK_EQ(a, b) 340 #define DCHECK_NE(a, b) 341 #define DCHECK_LT(a, b) 342 #define DCHECK_LE(a, b) 343 #define DCHECK_GT(a, b) 344 #define DCHECK_GE(a, b) 345 #endif 346 347 #define UNREACHABLE(msg) do { \ 348 CHECK(0 && msg); \ 349 Die(); \ 350 } while (0) 351 352 #define UNIMPLEMENTED() UNREACHABLE("unimplemented") 353 354 #define COMPILER_CHECK(pred) static_assert(pred, "") 355 356 #define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0])) 357 358 // Limits for integral types. We have to redefine it in case we don't 359 // have stdint.h (like in Visual Studio 9). 360 #undef __INT64_C 361 #undef __UINT64_C 362 #if SANITIZER_WORDSIZE == 64 363 # define __INT64_C(c) c ## L 364 # define __UINT64_C(c) c ## UL 365 #else 366 # define __INT64_C(c) c ## LL 367 # define __UINT64_C(c) c ## ULL 368 #endif // SANITIZER_WORDSIZE == 64 369 #undef INT32_MIN 370 #define INT32_MIN (-2147483647-1) 371 #undef INT32_MAX 372 #define INT32_MAX (2147483647) 373 #undef UINT32_MAX 374 #define UINT32_MAX (4294967295U) 375 #undef INT64_MIN 376 #define INT64_MIN (-__INT64_C(9223372036854775807)-1) 377 #undef INT64_MAX 378 #define INT64_MAX (__INT64_C(9223372036854775807)) 379 #undef UINT64_MAX 380 #define UINT64_MAX (__UINT64_C(18446744073709551615)) 381 #undef UINTPTR_MAX 382 #if SANITIZER_WORDSIZE == 64 383 # define UINTPTR_MAX (18446744073709551615UL) 384 #else 385 # define UINTPTR_MAX (4294967295U) 386 #endif // SANITIZER_WORDSIZE == 64 387 388 enum LinkerInitialized { LINKER_INITIALIZED = 0 }; 389 390 #if !defined(_MSC_VER) || defined(__clang__) 391 #if SANITIZER_S390_31 392 #define GET_CALLER_PC() \ 393 (__sanitizer::uptr) __builtin_extract_return_addr(__builtin_return_address(0)) 394 #else 395 #define GET_CALLER_PC() (__sanitizer::uptr) __builtin_return_address(0) 396 #endif 397 #define GET_CURRENT_FRAME() (__sanitizer::uptr) __builtin_frame_address(0) 398 inline void Trap() { 399 __builtin_trap(); 400 } 401 #else 402 extern "C" void* _ReturnAddress(void); 403 extern "C" void* _AddressOfReturnAddress(void); 404 # pragma intrinsic(_ReturnAddress) 405 # pragma intrinsic(_AddressOfReturnAddress) 406 #define GET_CALLER_PC() (__sanitizer::uptr) _ReturnAddress() 407 // CaptureStackBackTrace doesn't need to know BP on Windows. 408 #define GET_CURRENT_FRAME() \ 409 (((__sanitizer::uptr)_AddressOfReturnAddress()) + sizeof(__sanitizer::uptr)) 410 411 extern "C" void __ud2(void); 412 # pragma intrinsic(__ud2) 413 inline void Trap() { 414 __ud2(); 415 } 416 #endif 417 418 #define HANDLE_EINTR(res, f) \ 419 { \ 420 int rverrno; \ 421 do { \ 422 res = (f); \ 423 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \ 424 } 425 426 // Forces the compiler to generate a frame pointer in the function. 427 #define ENABLE_FRAME_POINTER \ 428 do { \ 429 volatile __sanitizer::uptr enable_fp; \ 430 enable_fp = GET_CURRENT_FRAME(); \ 431 (void)enable_fp; \ 432 } while (0) 433 434 // Internal thread identifier allocated by ThreadRegistry. 435 typedef u32 Tid; 436 constexpr Tid kInvalidTid = -1; 437 constexpr Tid kMainTid = 0; 438 439 // Stack depot stack identifier. 440 typedef u32 StackID; 441 const StackID kInvalidStackID = 0; 442 443 } // namespace __sanitizer 444 445 namespace __asan { 446 using namespace __sanitizer; 447 } 448 namespace __dsan { 449 using namespace __sanitizer; 450 } 451 namespace __dfsan { 452 using namespace __sanitizer; 453 } 454 namespace __lsan { 455 using namespace __sanitizer; 456 } 457 namespace __msan { 458 using namespace __sanitizer; 459 } 460 namespace __hwasan { 461 using namespace __sanitizer; 462 } 463 namespace __tsan { 464 using namespace __sanitizer; 465 } 466 namespace __scudo { 467 using namespace __sanitizer; 468 } 469 namespace __ubsan { 470 using namespace __sanitizer; 471 } 472 namespace __xray { 473 using namespace __sanitizer; 474 } 475 namespace __interception { 476 using namespace __sanitizer; 477 } 478 namespace __hwasan { 479 using namespace __sanitizer; 480 } 481 namespace __memprof { 482 using namespace __sanitizer; 483 } 484 485 #endif // SANITIZER_DEFS_H 486