501 lines · c
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-exception6//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_H13#define SANITIZER_DEFS_H14 15#include "sanitizer_platform.h"16#include "sanitizer_redefine_builtins.h"17 18// GCC does not understand __has_feature.19#if !defined(__has_feature)20#define __has_feature(x) 021#endif22 23#ifndef SANITIZER_DEBUG24# define SANITIZER_DEBUG 025#endif26 27#define SANITIZER_STRINGIFY_(S) #S28#define SANITIZER_STRINGIFY(S) SANITIZER_STRINGIFY_(S)29 30// Only use SANITIZER_*ATTRIBUTE* before the function return type!31#if SANITIZER_WINDOWS32#if SANITIZER_IMPORT_INTERFACE33# define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllimport)34#else35# define SANITIZER_INTERFACE_ATTRIBUTE __declspec(dllexport)36#endif37# define SANITIZER_WEAK_ATTRIBUTE38# define SANITIZER_WEAK_IMPORT39#elif SANITIZER_GO40# define SANITIZER_INTERFACE_ATTRIBUTE41# define SANITIZER_WEAK_ATTRIBUTE42# define SANITIZER_WEAK_IMPORT43#else44# define SANITIZER_INTERFACE_ATTRIBUTE __attribute__((visibility("default")))45# define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak))46# if SANITIZER_APPLE47# define SANITIZER_WEAK_IMPORT extern "C" __attribute((weak_import))48# else49# define SANITIZER_WEAK_IMPORT extern "C" SANITIZER_WEAK_ATTRIBUTE50# endif // SANITIZER_APPLE51#endif // SANITIZER_WINDOWS52 53//--------------------------- WEAK FUNCTIONS ---------------------------------//54// When working with weak functions, to simplify the code and make it more55// portable, when possible define a default implementation using this macro:56//57// SANITIZER_INTERFACE_WEAK_DEF(<return_type>, <name>, <parameter list>)58//59// For example:60// SANITIZER_INTERFACE_WEAK_DEF(bool, compare, int a, int b) { return a > b; }61//62#if SANITIZER_WINDOWS63#include "sanitizer_win_defs.h"64# define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \65 WIN_WEAK_EXPORT_DEF(ReturnType, Name, __VA_ARGS__)66#else67# define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...) \68 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE \69 ReturnType Name(__VA_ARGS__)70#endif71 72// SANITIZER_SUPPORTS_WEAK_HOOKS means that we support real weak functions that73// will evaluate to a null pointer when not defined.74#ifndef SANITIZER_SUPPORTS_WEAK_HOOKS75#if (SANITIZER_LINUX || SANITIZER_SOLARIS) && !SANITIZER_GO76# define SANITIZER_SUPPORTS_WEAK_HOOKS 177// Before Xcode 4.5, the Darwin linker doesn't reliably support undefined78// weak symbols. Mac OS X 10.9/Darwin 13 is the first release only supported79// by Xcode >= 4.5.80#elif SANITIZER_APPLE && \81 __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1090 && !SANITIZER_GO82# define SANITIZER_SUPPORTS_WEAK_HOOKS 183#else84# define SANITIZER_SUPPORTS_WEAK_HOOKS 085#endif86#endif // SANITIZER_SUPPORTS_WEAK_HOOKS87// For some weak hooks that will be called very often and we want to avoid the88// overhead of executing the default implementation when it is not necessary,89// we can use the flag SANITIZER_SUPPORTS_WEAK_HOOKS to only define the default90// implementation for platforms that doesn't support weak symbols. For example:91//92// #if !SANITIZER_SUPPORT_WEAK_HOOKS93// SANITIZER_INTERFACE_WEAK_DEF(bool, compare_hook, int a, int b) {94// return a > b;95// }96// #endif97//98// And then use it as: if (compare_hook) compare_hook(a, b);99//----------------------------------------------------------------------------//100 101 102// We can use .preinit_array section on Linux to call sanitizer initialization103// functions very early in the process startup (unless PIC macro is defined).104//105// On FreeBSD, .preinit_array functions are called with rtld_bind_lock writer106// lock held. It will lead to dead lock if unresolved PLT functions (which helds107// rtld_bind_lock reader lock) are called inside .preinit_array functions.108//109// FIXME: do we have anything like this on Mac?110#ifndef SANITIZER_CAN_USE_PREINIT_ARRAY111#if (SANITIZER_LINUX || SANITIZER_FUCHSIA || SANITIZER_NETBSD) && !defined(PIC)112#define SANITIZER_CAN_USE_PREINIT_ARRAY 1113// Before Solaris 11.4, .preinit_array is fully supported only with GNU ld.114// FIXME: Check for those conditions.115#elif SANITIZER_SOLARIS && !defined(PIC)116# define SANITIZER_CAN_USE_PREINIT_ARRAY 1117#else118# define SANITIZER_CAN_USE_PREINIT_ARRAY 0119#endif120#endif // SANITIZER_CAN_USE_PREINIT_ARRAY121 122// GCC does not understand __has_feature123#if !defined(__has_feature)124# define __has_feature(x) 0125#endif126 127// Older GCCs do not understand __has_attribute.128#if !defined(__has_attribute)129# define __has_attribute(x) 0130#endif131 132#if !defined(__has_cpp_attribute)133# define __has_cpp_attribute(x) 0134#endif135 136// For portability reasons we do not include stddef.h, stdint.h or any other137// system header, but we do need some basic types that are not defined138// in a portable way by the language itself.139namespace __sanitizer {140 141#if defined(__UINTPTR_TYPE__)142# if defined(__arm__) && defined(__linux__)143// Linux Arm headers redefine __UINTPTR_TYPE__ and disagree with clang/gcc.144typedef unsigned int uptr;145typedef int sptr;146# else147typedef __UINTPTR_TYPE__ uptr;148typedef __INTPTR_TYPE__ sptr;149# endif150#elif defined(_WIN64)151// 64-bit Windows uses LLP64 data model.152typedef unsigned long long uptr;153typedef signed long long sptr;154#elif defined(_WIN32)155typedef unsigned int uptr;156typedef signed int sptr;157#else158# error Unsupported compiler, missing __UINTPTR_TYPE__159#endif // defined(__UINTPTR_TYPE__)160#if defined(__x86_64__)161// Since x32 uses ILP32 data model in 64-bit hardware mode, we must use162// 64-bit pointer to unwind stack frame.163typedef unsigned long long uhwptr;164#else165typedef uptr uhwptr;166#endif167typedef unsigned char u8;168typedef unsigned short u16;169typedef unsigned int u32;170typedef unsigned long long u64;171typedef signed char s8;172typedef signed short s16;173typedef signed int s32;174typedef signed long long s64;175#if SANITIZER_WINDOWS176// On Windows, files are HANDLE, which is a synonim of void*.177// Use void* to avoid including <windows.h> everywhere.178typedef void* fd_t;179typedef unsigned error_t;180#else181typedef int fd_t;182typedef int error_t;183#endif184#if SANITIZER_SOLARIS && !defined(_LP64)185typedef long pid_t;186#else187typedef int pid_t;188#endif189 190#if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_APPLE || \191 (SANITIZER_SOLARIS && (defined(_LP64) || _FILE_OFFSET_BITS == 64)) || \192 (SANITIZER_LINUX && !SANITIZER_GLIBC && !SANITIZER_ANDROID) || \193 (SANITIZER_LINUX && (defined(__x86_64__) || defined(__hexagon__)))194typedef u64 OFF_T;195#else196typedef uptr OFF_T;197#endif198typedef u64 OFF64_T;199 200#ifdef __SIZE_TYPE__201typedef __SIZE_TYPE__ usize;202#else203typedef uptr usize;204#endif205 206#if defined(__s390__) && !defined(__s390x__)207typedef long ssize;208#else209typedef sptr ssize;210#endif211 212typedef u64 ThreadID;213 214// ----------- ATTENTION -------------215// This header should NOT include any other headers to avoid portability issues.216 217// Common defs.218#define INTERFACE_ATTRIBUTE SANITIZER_INTERFACE_ATTRIBUTE219#define SANITIZER_WEAK_DEFAULT_IMPL \220 extern "C" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE221#define SANITIZER_WEAK_CXX_DEFAULT_IMPL \222 extern "C++" SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE NOINLINE223 224// Platform-specific defs.225#if defined(_MSC_VER)226# define ALWAYS_INLINE __forceinline227// FIXME(timurrrr): do we need this on Windows?228# define ALIAS(x)229# define ALIGNED(x) __declspec(align(x))230# define FORMAT(f, a)231# define NOINLINE __declspec(noinline)232# define NORETURN __declspec(noreturn)233# define THREADLOCAL __declspec(thread)234# define LIKELY(x) (x)235# define UNLIKELY(x) (x)236# define PREFETCH(x) /* _mm_prefetch(x, _MM_HINT_NTA) */ (void)0237# define WARN_UNUSED_RESULT238#else // _MSC_VER239# define ALWAYS_INLINE inline __attribute__((always_inline))240# define ALIAS(x) __attribute__((alias(SANITIZER_STRINGIFY(x))))241// Please only use the ALIGNED macro before the type.242// Using ALIGNED after the variable declaration is not portable!243# define ALIGNED(x) __attribute__((aligned(x)))244# define FORMAT(f, a) __attribute__((format(printf, f, a)))245# define NOINLINE __attribute__((noinline))246# define NORETURN __attribute__((noreturn))247# define THREADLOCAL __thread248# define LIKELY(x) __builtin_expect(!!(x), 1)249# define UNLIKELY(x) __builtin_expect(!!(x), 0)250# if defined(__i386__) || defined(__x86_64__)251// __builtin_prefetch(x) generates prefetchnt0 on x86252# define PREFETCH(x) __asm__("prefetchnta (%0)" : : "r" (x))253# else254# define PREFETCH(x) __builtin_prefetch(x)255# endif256# define WARN_UNUSED_RESULT __attribute__((warn_unused_result))257#endif // _MSC_VER258 259#if !defined(_MSC_VER) || defined(__clang__)260# define UNUSED __attribute__((unused))261# define USED __attribute__((used))262#else263# define UNUSED264# define USED265#endif266 267#if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)268# define NOEXCEPT noexcept269#else270# define NOEXCEPT throw()271#endif272 273#if __has_cpp_attribute(clang::fallthrough)274# define FALLTHROUGH [[clang::fallthrough]]275#elif __has_cpp_attribute(fallthrough)276# define FALLTHROUGH [[fallthrough]]277#else278# define FALLTHROUGH279#endif280 281#if __has_attribute(uninitialized)282# define UNINITIALIZED __attribute__((uninitialized))283#else284# define UNINITIALIZED285#endif286 287// Unaligned versions of basic types.288typedef ALIGNED(1) u16 uu16;289typedef ALIGNED(1) u32 uu32;290typedef ALIGNED(1) u64 uu64;291typedef ALIGNED(1) s16 us16;292typedef ALIGNED(1) s32 us32;293typedef ALIGNED(1) s64 us64;294 295#if SANITIZER_WINDOWS296} // namespace __sanitizer297typedef unsigned long DWORD;298namespace __sanitizer {299typedef DWORD thread_return_t;300# define THREAD_CALLING_CONV __stdcall301#else // _WIN32302typedef void* thread_return_t;303# define THREAD_CALLING_CONV304#endif // _WIN32305typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);306 307// NOTE: Functions below must be defined in each run-time.308void NORETURN Die();309 310void NORETURN CheckFailed(const char *file, int line, const char *cond,311 u64 v1, u64 v2);312 313// Check macro314#define RAW_CHECK_MSG(expr, msg, ...) \315 do { \316 if (UNLIKELY(!(expr))) { \317 const char* msgs[] = {msg, __VA_ARGS__}; \318 for (const char* m : msgs) RawWrite(m); \319 Die(); \320 } \321 } while (0)322 323#define RAW_CHECK(expr) RAW_CHECK_MSG(expr, #expr "\n", )324#define RAW_CHECK_VA(expr, ...) RAW_CHECK_MSG(expr, #expr "\n", __VA_ARGS__)325 326#define CHECK_IMPL(c1, op, c2) \327 do { \328 __sanitizer::u64 v1 = (__sanitizer::u64)(c1); \329 __sanitizer::u64 v2 = (__sanitizer::u64)(c2); \330 if (UNLIKELY(!(v1 op v2))) \331 __sanitizer::CheckFailed(__FILE__, __LINE__, \332 "(" #c1 ") " #op " (" #c2 ")", v1, v2); \333 } while (false) \334/**/335 336#define CHECK(a) CHECK_IMPL((a), !=, 0)337#define CHECK_EQ(a, b) CHECK_IMPL((a), ==, (b))338#define CHECK_NE(a, b) CHECK_IMPL((a), !=, (b))339#define CHECK_LT(a, b) CHECK_IMPL((a), <, (b))340#define CHECK_LE(a, b) CHECK_IMPL((a), <=, (b))341#define CHECK_GT(a, b) CHECK_IMPL((a), >, (b))342#define CHECK_GE(a, b) CHECK_IMPL((a), >=, (b))343 344#if SANITIZER_DEBUG345#define DCHECK(a) CHECK(a)346#define DCHECK_EQ(a, b) CHECK_EQ(a, b)347#define DCHECK_NE(a, b) CHECK_NE(a, b)348#define DCHECK_LT(a, b) CHECK_LT(a, b)349#define DCHECK_LE(a, b) CHECK_LE(a, b)350#define DCHECK_GT(a, b) CHECK_GT(a, b)351#define DCHECK_GE(a, b) CHECK_GE(a, b)352#else353#define DCHECK(a)354#define DCHECK_EQ(a, b)355#define DCHECK_NE(a, b)356#define DCHECK_LT(a, b)357#define DCHECK_LE(a, b)358#define DCHECK_GT(a, b)359#define DCHECK_GE(a, b)360#endif361 362#define UNREACHABLE(msg) do { \363 CHECK(0 && msg); \364 Die(); \365} while (0)366 367#define UNIMPLEMENTED() UNREACHABLE("unimplemented")368 369#define COMPILER_CHECK(pred) static_assert(pred, "")370 371#define ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))372 373// Limits for integral types. We have to redefine it in case we don't374// have stdint.h (like in Visual Studio 9).375#undef __INT64_C376#undef __UINT64_C377#if SANITIZER_WORDSIZE == 64378# define __INT64_C(c) c ## L379# define __UINT64_C(c) c ## UL380#else381# define __INT64_C(c) c ## LL382# define __UINT64_C(c) c ## ULL383#endif // SANITIZER_WORDSIZE == 64384#undef INT32_MIN385#define INT32_MIN (-2147483647-1)386#undef INT32_MAX387#define INT32_MAX (2147483647)388#undef UINT32_MAX389#define UINT32_MAX (4294967295U)390#undef INT64_MIN391#define INT64_MIN (-__INT64_C(9223372036854775807)-1)392#undef INT64_MAX393#define INT64_MAX (__INT64_C(9223372036854775807))394#undef UINT64_MAX395#define UINT64_MAX (__UINT64_C(18446744073709551615))396#undef UINTPTR_MAX397#if SANITIZER_WORDSIZE == 64398# define UINTPTR_MAX (18446744073709551615UL)399#else400# define UINTPTR_MAX (4294967295U)401#endif // SANITIZER_WORDSIZE == 64402 403enum LinkerInitialized { LINKER_INITIALIZED = 0 };404 405#if !defined(_MSC_VER) || defined(__clang__)406# define GET_CALLER_PC() \407 ((__sanitizer::uptr)__builtin_extract_return_addr( \408 __builtin_return_address(0)))409# define GET_CURRENT_FRAME() ((__sanitizer::uptr)__builtin_frame_address(0))410inline void Trap() {411 __builtin_trap();412}413#else414extern "C" void* _ReturnAddress(void);415extern "C" void* _AddressOfReturnAddress(void);416# pragma intrinsic(_ReturnAddress)417# pragma intrinsic(_AddressOfReturnAddress)418# define GET_CALLER_PC() ((__sanitizer::uptr)_ReturnAddress())419// CaptureStackBackTrace doesn't need to know BP on Windows.420# define GET_CURRENT_FRAME() \421 (((__sanitizer::uptr)_AddressOfReturnAddress()) + sizeof(__sanitizer::uptr))422 423extern "C" void __ud2(void);424# pragma intrinsic(__ud2)425inline void Trap() {426 __ud2();427}428#endif429 430#define HANDLE_EINTR(res, f) \431 { \432 int rverrno; \433 do { \434 res = (f); \435 } while (internal_iserror(res, &rverrno) && rverrno == EINTR); \436 }437 438// Forces the compiler to generate a frame pointer in the function.439#define ENABLE_FRAME_POINTER \440 do { \441 volatile __sanitizer::uptr enable_fp; \442 enable_fp = GET_CURRENT_FRAME(); \443 (void)enable_fp; \444 } while (0)445 446// Internal thread identifier allocated by ThreadRegistry.447typedef u32 Tid;448constexpr Tid kInvalidTid = -1;449constexpr Tid kMainTid = 0;450 451// Stack depot stack identifier.452typedef u32 StackID;453const StackID kInvalidStackID = 0;454 455} // namespace __sanitizer456 457namespace __asan {458using namespace __sanitizer;459}460namespace __dsan {461using namespace __sanitizer;462}463namespace __dfsan {464using namespace __sanitizer;465}466namespace __lsan {467using namespace __sanitizer;468}469namespace __msan {470using namespace __sanitizer;471}472namespace __nsan {473using namespace __sanitizer;474}475namespace __hwasan {476using namespace __sanitizer;477}478namespace __tsan {479using namespace __sanitizer;480}481namespace __scudo {482using namespace __sanitizer;483}484namespace __ubsan {485using namespace __sanitizer;486}487namespace __xray {488using namespace __sanitizer;489}490namespace __interception {491using namespace __sanitizer;492}493namespace __hwasan {494using namespace __sanitizer;495}496namespace __memprof {497using namespace __sanitizer;498}499 500#endif // SANITIZER_DEFS_H501