Path: blob/main/contrib/llvm-project/compiler-rt/lib/scudo/standalone/internal_defs.h
35291 views
//===-- internal_defs.h -----------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef SCUDO_INTERNAL_DEFS_H_9#define SCUDO_INTERNAL_DEFS_H_1011#include "platform.h"1213#include <stdint.h>1415#ifndef SCUDO_DEBUG16#define SCUDO_DEBUG 017#endif1819#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))2021// String related macros.2223#define STRINGIFY_(S) #S24#define STRINGIFY(S) STRINGIFY_(S)25#define CONCATENATE_(S, C) S##C26#define CONCATENATE(S, C) CONCATENATE_(S, C)2728// Attributes & builtins related macros.2930#define INTERFACE __attribute__((visibility("default")))31#define HIDDEN __attribute__((visibility("hidden")))32#define WEAK __attribute__((weak))33#define ALWAYS_INLINE inline __attribute__((always_inline))34#define ALIAS(X) __attribute__((alias(X)))35#define FORMAT(F, A) __attribute__((format(printf, F, A)))36#define NOINLINE __attribute__((noinline))37#define NORETURN __attribute__((noreturn))38#define LIKELY(X) __builtin_expect(!!(X), 1)39#define UNLIKELY(X) __builtin_expect(!!(X), 0)40#if defined(__i386__) || defined(__x86_64__)41// __builtin_prefetch(X) generates prefetchnt0 on x8642#define PREFETCH(X) __asm__("prefetchnta (%0)" : : "r"(X))43#else44#define PREFETCH(X) __builtin_prefetch(X)45#endif46#define UNUSED __attribute__((unused))47#define USED __attribute__((used))48#define NOEXCEPT noexcept4950// This check is only available on Clang. This is essentially an alias of51// C++20's 'constinit' specifier which will take care of this when (if?) we can52// ask all libc's that use Scudo to compile us with C++20. Dynamic53// initialization is bad; Scudo is designed to be lazy-initializated on the54// first call to malloc/free (and friends), and this generally happens in the55// loader somewhere in libdl's init. After the loader is done, control is56// transferred to libc's initialization, and the dynamic initializers are run.57// If there's a dynamic initializer for Scudo, then it will clobber the58// already-initialized Scudo, and re-initialize all its members back to default59// values, causing various explosions. Unfortunately, marking60// scudo::Allocator<>'s constructor as 'constexpr' isn't sufficient to prevent61// dynamic initialization, as default initialization is fine under 'constexpr'62// (but not 'constinit'). Clang at -O0, and gcc at all opt levels will emit a63// dynamic initializer for any constant-initialized variables if there is a mix64// of default-initialized and constant-initialized variables.65//66// If you're looking at this because your build failed, you probably introduced67// a new member to scudo::Allocator<> (possibly transiently) that didn't have an68// initializer. The fix is easy - just add one.69#if defined(__has_attribute)70#if __has_attribute(require_constant_initialization)71#define SCUDO_REQUIRE_CONSTANT_INITIALIZATION \72__attribute__((__require_constant_initialization__))73#else74#define SCUDO_REQUIRE_CONSTANT_INITIALIZATION75#endif76#endif7778namespace scudo {7980typedef uintptr_t uptr;81typedef uint8_t u8;82typedef uint16_t u16;83typedef uint32_t u32;84typedef uint64_t u64;85typedef intptr_t sptr;86typedef int8_t s8;87typedef int16_t s16;88typedef int32_t s32;89typedef int64_t s64;9091// The following two functions have platform specific implementations.92void outputRaw(const char *Buffer);93void NORETURN die();9495#define RAW_CHECK_MSG(Expr, Msg) \96do { \97if (UNLIKELY(!(Expr))) { \98outputRaw(Msg); \99die(); \100} \101} while (false)102103#define RAW_CHECK(Expr) RAW_CHECK_MSG(Expr, #Expr)104105void NORETURN reportCheckFailed(const char *File, int Line,106const char *Condition, u64 Value1, u64 Value2);107#define CHECK_IMPL(C1, Op, C2) \108do { \109if (UNLIKELY(!(C1 Op C2))) { \110scudo::reportCheckFailed(__FILE__, __LINE__, #C1 " " #Op " " #C2, \111(scudo::u64)C1, (scudo::u64)C2); \112scudo::die(); \113} \114} while (false)115116#define CHECK(A) CHECK_IMPL((A), !=, 0)117#define CHECK_EQ(A, B) CHECK_IMPL((A), ==, (B))118#define CHECK_NE(A, B) CHECK_IMPL((A), !=, (B))119#define CHECK_LT(A, B) CHECK_IMPL((A), <, (B))120#define CHECK_LE(A, B) CHECK_IMPL((A), <=, (B))121#define CHECK_GT(A, B) CHECK_IMPL((A), >, (B))122#define CHECK_GE(A, B) CHECK_IMPL((A), >=, (B))123124#if SCUDO_DEBUG125#define DCHECK(A) CHECK(A)126#define DCHECK_EQ(A, B) CHECK_EQ(A, B)127#define DCHECK_NE(A, B) CHECK_NE(A, B)128#define DCHECK_LT(A, B) CHECK_LT(A, B)129#define DCHECK_LE(A, B) CHECK_LE(A, B)130#define DCHECK_GT(A, B) CHECK_GT(A, B)131#define DCHECK_GE(A, B) CHECK_GE(A, B)132#else133#define DCHECK(A) \134do { \135} while (false && (A))136#define DCHECK_EQ(A, B) \137do { \138} while (false && (A) == (B))139#define DCHECK_NE(A, B) \140do { \141} while (false && (A) != (B))142#define DCHECK_LT(A, B) \143do { \144} while (false && (A) < (B))145#define DCHECK_LE(A, B) \146do { \147} while (false && (A) <= (B))148#define DCHECK_GT(A, B) \149do { \150} while (false && (A) > (B))151#define DCHECK_GE(A, B) \152do { \153} while (false && (A) >= (B))154#endif155156// The superfluous die() call effectively makes this macro NORETURN.157#define UNREACHABLE(Msg) \158do { \159CHECK(0 && Msg); \160die(); \161} while (0)162163} // namespace scudo164165#endif // SCUDO_INTERNAL_DEFS_H_166167168