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