llvm/compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h

//===-- sanitizer_internal_defs.h -------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file is shared between AddressSanitizer and ThreadSanitizer.
// It contains macro used in run-time libraries code.
//===----------------------------------------------------------------------===//
#ifndef SANITIZER_DEFS_H
#define SANITIZER_DEFS_H

#include "sanitizer_platform.h"
#include "sanitizer_redefine_builtins.h"

// GCC does not understand __has_feature.
#if !defined(__has_feature)
#define __has_feature
#endif

#ifndef SANITIZER_DEBUG
#define SANITIZER_DEBUG
#endif

#define SANITIZER_STRINGIFY_(S)
#define SANITIZER_STRINGIFY(S)

// Only use SANITIZER_*ATTRIBUTE* before the function return type!
#if SANITIZER_WINDOWS
#if SANITIZER_IMPORT_INTERFACE
#define SANITIZER_INTERFACE_ATTRIBUTE
#else
#define SANITIZER_INTERFACE_ATTRIBUTE
#endif
#define SANITIZER_WEAK_ATTRIBUTE
#define SANITIZER_WEAK_IMPORT
#elif SANITIZER_GO
#define SANITIZER_INTERFACE_ATTRIBUTE
#define SANITIZER_WEAK_ATTRIBUTE
#define SANITIZER_WEAK_IMPORT
#else
#define SANITIZER_INTERFACE_ATTRIBUTE
#define SANITIZER_WEAK_ATTRIBUTE
#  if SANITIZER_APPLE
#define SANITIZER_WEAK_IMPORT
#  else
#define SANITIZER_WEAK_IMPORT
#  endif  // SANITIZER_APPLE
#endif    // SANITIZER_WINDOWS

//--------------------------- WEAK FUNCTIONS ---------------------------------//
// When working with weak functions, to simplify the code and make it more
// portable, when possible define a default implementation using this macro:
//
// SANITIZER_INTERFACE_WEAK_DEF(<return_type>, <name>, <parameter list>)
//
// For example:
//   SANITIZER_INTERFACE_WEAK_DEF(bool, compare, int a, int b) { return a > b; }
//
#if SANITIZER_WINDOWS
#include "sanitizer_win_defs.h"
#define SANITIZER_INTERFACE_WEAK_DEF
#else
#define SANITIZER_INTERFACE_WEAK_DEF(ReturnType, Name, ...)
#endif

// SANITIZER_SUPPORTS_WEAK_HOOKS means that we support real weak functions that
// will evaluate to a null pointer when not defined.
#ifndef SANITIZER_SUPPORTS_WEAK_HOOKS
#if (SANITIZER_LINUX || SANITIZER_SOLARIS) && !SANITIZER_GO
#define SANITIZER_SUPPORTS_WEAK_HOOKS
// Before Xcode 4.5, the Darwin linker doesn't reliably support undefined
// weak symbols.  Mac OS X 10.9/Darwin 13 is the first release only supported
// by Xcode >= 4.5.
#elif SANITIZER_APPLE && \
    __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1090 && !SANITIZER_GO
#define SANITIZER_SUPPORTS_WEAK_HOOKS
#else
#define SANITIZER_SUPPORTS_WEAK_HOOKS
#endif
#endif // SANITIZER_SUPPORTS_WEAK_HOOKS
// For some weak hooks that will be called very often and we want to avoid the
// overhead of executing the default implementation when it is not necessary,
// we can use the flag SANITIZER_SUPPORTS_WEAK_HOOKS to only define the default
// implementation for platforms that doesn't support weak symbols. For example:
//
//   #if !SANITIZER_SUPPORT_WEAK_HOOKS
//     SANITIZER_INTERFACE_WEAK_DEF(bool, compare_hook, int a, int b) {
//       return a > b;
//     }
//   #endif
//
// And then use it as: if (compare_hook) compare_hook(a, b);
//----------------------------------------------------------------------------//


// We can use .preinit_array section on Linux to call sanitizer initialization
// functions very early in the process startup (unless PIC macro is defined).
//
// On FreeBSD, .preinit_array functions are called with rtld_bind_lock writer
// lock held. It will lead to dead lock if unresolved PLT functions (which helds
// rtld_bind_lock reader lock) are called inside .preinit_array functions.
//
// FIXME: do we have anything like this on Mac?
#ifndef SANITIZER_CAN_USE_PREINIT_ARRAY
#if (SANITIZER_LINUX || SANITIZER_FUCHSIA || SANITIZER_NETBSD) && !defined(PIC)
#define SANITIZER_CAN_USE_PREINIT_ARRAY
// Before Solaris 11.4, .preinit_array is fully supported only with GNU ld.
// FIXME: Check for those conditions.
#elif SANITIZER_SOLARIS && !defined(PIC)
#define SANITIZER_CAN_USE_PREINIT_ARRAY
#else
#define SANITIZER_CAN_USE_PREINIT_ARRAY
#endif
#endif  // SANITIZER_CAN_USE_PREINIT_ARRAY

// GCC does not understand __has_feature
#if !defined(__has_feature)
#define __has_feature
#endif

// Older GCCs do not understand __has_attribute.
#if !defined(__has_attribute)
#define __has_attribute
#endif

#if !defined(__has_cpp_attribute)
#define __has_cpp_attribute
#endif

// For portability reasons we do not include stddef.h, stdint.h or any other
// system header, but we do need some basic types that are not defined
// in a portable way by the language itself.
namespace __sanitizer {

#if defined(_WIN64)
// 64-bit Windows uses LLP64 data model.
typedef unsigned long long uptr;
typedef signed long long sptr;
#else
#  if (SANITIZER_WORDSIZE == 64) || SANITIZER_APPLE
uptr;
sptr;
#  else
typedef unsigned int uptr;
typedef signed int sptr;
#  endif
#endif  // defined(_WIN64)
#if defined(__x86_64__)
// Since x32 uses ILP32 data model in 64-bit hardware mode, we must use
// 64-bit pointer to unwind stack frame.
uhwptr;
#else
typedef uptr uhwptr;
#endif
u8;
u16;
u32;
u64;
s8;
s16;
s32;
s64;
#if SANITIZER_WINDOWS
// On Windows, files are HANDLE, which is a synonim of void*.
// Use void* to avoid including <windows.h> everywhere.
typedef void* fd_t;
typedef unsigned error_t;
#else
fd_t;
error_t;
#endif
#if SANITIZER_SOLARIS && !defined(_LP64)
typedef long pid_t;
#else
pid_t;
#endif

#if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_APPLE ||             \
    (SANITIZER_SOLARIS && (defined(_LP64) || _FILE_OFFSET_BITS == 64)) || \
    (SANITIZER_LINUX && !SANITIZER_GLIBC && !SANITIZER_ANDROID) ||        \
    (SANITIZER_LINUX && (defined(__x86_64__) || defined(__hexagon__)))
OFF_T;
#else
typedef uptr OFF_T;
#endif
OFF64_T;

#ifdef __SIZE_TYPE__
usize;
#else
typedef uptr usize;
#endif

tid_t;

// ----------- ATTENTION -------------
// This header should NOT include any other headers to avoid portability issues.

// Common defs.
#define INTERFACE_ATTRIBUTE
#define SANITIZER_WEAK_DEFAULT_IMPL
#define SANITIZER_WEAK_CXX_DEFAULT_IMPL

// Platform-specific defs.
#if defined(_MSC_VER)
#define ALWAYS_INLINE
// FIXME(timurrrr): do we need this on Windows?
#define ALIAS
#define ALIGNED
#define FORMAT
#define NOINLINE
#define NORETURN
#define THREADLOCAL
#define LIKELY
#define UNLIKELY
#define PREFETCH
#define WARN_UNUSED_RESULT
#else  // _MSC_VER
#define ALWAYS_INLINE
#define ALIAS(x)
// Please only use the ALIGNED macro before the type.
// Using ALIGNED after the variable declaration is not portable!
#define ALIGNED(x)
#define FORMAT(f, a)
#define NOINLINE
#define NORETURN
#define THREADLOCAL
#define LIKELY(x)
#define UNLIKELY(x)
# if defined(__i386__) || defined(__x86_64__)
// __builtin_prefetch(x) generates prefetchnt0 on x86
#define PREFETCH(x)
# else
#define PREFETCH
# endif
#define WARN_UNUSED_RESULT
#endif  // _MSC_VER

#if !defined(_MSC_VER) || defined(__clang__)
#define UNUSED
#define USED
#else
#define UNUSED
#define USED
#endif

#if !defined(_MSC_VER) || defined(__clang__) || MSC_PREREQ(1900)
#define NOEXCEPT
#else
#define NOEXCEPT
#endif

#if __has_cpp_attribute(clang::fallthrough)
#define FALLTHROUGH
#elif __has_cpp_attribute(fallthrough)
#define FALLTHROUGH
#else
#define FALLTHROUGH
#endif

#if __has_attribute(uninitialized)
#define UNINITIALIZED
#else
#define UNINITIALIZED
#endif

// Unaligned versions of basic types.
uu16;
uu32;
uu64;
us16;
us32;
us64;

#if SANITIZER_WINDOWS
}  // namespace __sanitizer
typedef unsigned long DWORD;
namespace __sanitizer {
typedef DWORD thread_return_t;
#define THREAD_CALLING_CONV
#else  // _WIN32
thread_return_t;
#define THREAD_CALLING_CONV
#endif  // _WIN32
thread_callback_t;

// NOTE: Functions below must be defined in each run-time.
void NORETURN Die();

void NORETURN CheckFailed(const char *file, int line, const char *cond,
                          u64 v1, u64 v2);

// Check macro
#define RAW_CHECK_MSG(expr, msg, ...)

#define RAW_CHECK(expr)
#define RAW_CHECK_VA(expr, ...)

#define CHECK_IMPL(c1, op, c2)

#define CHECK(a)
#define CHECK_EQ(a, b)
#define CHECK_NE(a, b)
#define CHECK_LT(a, b)
#define CHECK_LE(a, b)
#define CHECK_GT(a, b)
#define CHECK_GE(a, b)

#if SANITIZER_DEBUG
#define DCHECK
#define DCHECK_EQ
#define DCHECK_NE
#define DCHECK_LT
#define DCHECK_LE
#define DCHECK_GT
#define DCHECK_GE
#else
#define DCHECK(a)
#define DCHECK_EQ(a, b)
#define DCHECK_NE(a, b)
#define DCHECK_LT(a, b)
#define DCHECK_LE(a, b)
#define DCHECK_GT(a, b)
#define DCHECK_GE(a, b)
#endif

#define UNREACHABLE(msg)

#define UNIMPLEMENTED()

#define COMPILER_CHECK(pred)

#define ARRAY_SIZE(a)

// Limits for integral types. We have to redefine it in case we don't
// have stdint.h (like in Visual Studio 9).
#undef __INT64_C
#undef __UINT64_C
#if SANITIZER_WORDSIZE == 64
#define __INT64_C(c)
#define __UINT64_C(c)
#else
#define __INT64_C
#define __UINT64_C
#endif  // SANITIZER_WORDSIZE == 64
#undef INT32_MIN
#define INT32_MIN
#undef INT32_MAX
#define INT32_MAX
#undef UINT32_MAX
#define UINT32_MAX
#undef INT64_MIN
#define INT64_MIN
#undef INT64_MAX
#define INT64_MAX
#undef UINT64_MAX
#define UINT64_MAX
#undef UINTPTR_MAX
#if SANITIZER_WORDSIZE == 64
#define UINTPTR_MAX
#else
#define UINTPTR_MAX
#endif  // SANITIZER_WORDSIZE == 64

enum LinkerInitialized {};

#if !defined(_MSC_VER) || defined(__clang__)
#define GET_CALLER_PC()
#define GET_CURRENT_FRAME()
inline void Trap() {}
#else
extern "C" void* _ReturnAddress(void);
extern "C" void* _AddressOfReturnAddress(void);
# pragma intrinsic(_ReturnAddress)
# pragma intrinsic(_AddressOfReturnAddress)
#define GET_CALLER_PC
// CaptureStackBackTrace doesn't need to know BP on Windows.
#define GET_CURRENT_FRAME

extern "C" void __ud2(void);
#  pragma intrinsic(__ud2)
inline void Trap() {
  __ud2();
}
#endif

#define HANDLE_EINTR(res, f)

// Forces the compiler to generate a frame pointer in the function.
#define ENABLE_FRAME_POINTER

// Internal thread identifier allocated by ThreadRegistry.
Tid;
constexpr Tid kInvalidTid =;
constexpr Tid kMainTid =;

// Stack depot stack identifier.
StackID;
const StackID kInvalidStackID =;

}  // namespace __sanitizer

namespace __asan {
usingnamespace__sanitizer;
}
namespace __dsan {
usingnamespace__sanitizer;
}
namespace __dfsan {
usingnamespace__sanitizer;
}
namespace __lsan {
usingnamespace__sanitizer;
}
namespace __msan {
usingnamespace__sanitizer;
}
namespace __nsan {
usingnamespace__sanitizer;
}
namespace __hwasan {
usingnamespace__sanitizer;
}
namespace __tsan {
usingnamespace__sanitizer;
}
namespace __scudo {
usingnamespace__sanitizer;
}
namespace __ubsan {
usingnamespace__sanitizer;
}
namespace __xray {
usingnamespace__sanitizer;
}
namespace __interception {
usingnamespace__sanitizer;
}
namespace __hwasan {
usingnamespace__sanitizer;
}
namespace __memprof {
usingnamespace__sanitizer;
}

#endif  // SANITIZER_DEFS_H