10b57cec5SDimitry Andric //===-- internal_defs.h -----------------------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #ifndef SCUDO_INTERNAL_DEFS_H_ 100b57cec5SDimitry Andric #define SCUDO_INTERNAL_DEFS_H_ 110b57cec5SDimitry Andric 120b57cec5SDimitry Andric #include "platform.h" 130b57cec5SDimitry Andric 140b57cec5SDimitry Andric #include <stdint.h> 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric #ifndef SCUDO_DEBUG 170b57cec5SDimitry Andric #define SCUDO_DEBUG 0 180b57cec5SDimitry Andric #endif 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0])) 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric // String related macros. 230b57cec5SDimitry Andric 240b57cec5SDimitry Andric #define STRINGIFY_(S) #S 250b57cec5SDimitry Andric #define STRINGIFY(S) STRINGIFY_(S) 260b57cec5SDimitry Andric #define CONCATENATE_(S, C) S##C 270b57cec5SDimitry Andric #define CONCATENATE(S, C) CONCATENATE_(S, C) 280b57cec5SDimitry Andric 290b57cec5SDimitry Andric // Attributes & builtins related macros. 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric #define INTERFACE __attribute__((visibility("default"))) 325ffd83dbSDimitry Andric #define HIDDEN __attribute__((visibility("hidden"))) 330b57cec5SDimitry Andric #define WEAK __attribute__((weak)) 340b57cec5SDimitry Andric #define ALWAYS_INLINE inline __attribute__((always_inline)) 350b57cec5SDimitry Andric #define ALIAS(X) __attribute__((alias(X))) 360b57cec5SDimitry Andric #define FORMAT(F, A) __attribute__((format(printf, F, A))) 370b57cec5SDimitry Andric #define NOINLINE __attribute__((noinline)) 380b57cec5SDimitry Andric #define NORETURN __attribute__((noreturn)) 390b57cec5SDimitry Andric #define LIKELY(X) __builtin_expect(!!(X), 1) 400b57cec5SDimitry Andric #define UNLIKELY(X) __builtin_expect(!!(X), 0) 410b57cec5SDimitry Andric #if defined(__i386__) || defined(__x86_64__) 420b57cec5SDimitry Andric // __builtin_prefetch(X) generates prefetchnt0 on x86 430b57cec5SDimitry Andric #define PREFETCH(X) __asm__("prefetchnta (%0)" : : "r"(X)) 440b57cec5SDimitry Andric #else 450b57cec5SDimitry Andric #define PREFETCH(X) __builtin_prefetch(X) 460b57cec5SDimitry Andric #endif 470b57cec5SDimitry Andric #define UNUSED __attribute__((unused)) 480b57cec5SDimitry Andric #define USED __attribute__((used)) 490b57cec5SDimitry Andric #define NOEXCEPT noexcept 500b57cec5SDimitry Andric 51fe6060f1SDimitry Andric // This check is only available on Clang. This is essentially an alias of 52fe6060f1SDimitry Andric // C++20's 'constinit' specifier which will take care of this when (if?) we can 53fe6060f1SDimitry Andric // ask all libc's that use Scudo to compile us with C++20. Dynamic 54fe6060f1SDimitry Andric // initialization is bad; Scudo is designed to be lazy-initializated on the 55fe6060f1SDimitry Andric // first call to malloc/free (and friends), and this generally happens in the 56fe6060f1SDimitry Andric // loader somewhere in libdl's init. After the loader is done, control is 57fe6060f1SDimitry Andric // transferred to libc's initialization, and the dynamic initializers are run. 58fe6060f1SDimitry Andric // If there's a dynamic initializer for Scudo, then it will clobber the 59fe6060f1SDimitry Andric // already-initialized Scudo, and re-initialize all its members back to default 60fe6060f1SDimitry Andric // values, causing various explosions. Unfortunately, marking 61fe6060f1SDimitry Andric // scudo::Allocator<>'s constructor as 'constexpr' isn't sufficient to prevent 62fe6060f1SDimitry Andric // dynamic initialization, as default initialization is fine under 'constexpr' 63fe6060f1SDimitry Andric // (but not 'constinit'). Clang at -O0, and gcc at all opt levels will emit a 64fe6060f1SDimitry Andric // dynamic initializer for any constant-initialized variables if there is a mix 65fe6060f1SDimitry Andric // of default-initialized and constant-initialized variables. 66fe6060f1SDimitry Andric // 67fe6060f1SDimitry Andric // If you're looking at this because your build failed, you probably introduced 68fe6060f1SDimitry Andric // a new member to scudo::Allocator<> (possibly transiently) that didn't have an 69fe6060f1SDimitry Andric // initializer. The fix is easy - just add one. 70fe6060f1SDimitry Andric #if defined(__has_attribute) 71fe6060f1SDimitry Andric #if __has_attribute(require_constant_initialization) 72fe6060f1SDimitry Andric #define SCUDO_REQUIRE_CONSTANT_INITIALIZATION \ 73fe6060f1SDimitry Andric __attribute__((__require_constant_initialization__)) 74fe6060f1SDimitry Andric #else 75fe6060f1SDimitry Andric #define SCUDO_REQUIRE_CONSTANT_INITIALIZATION 76fe6060f1SDimitry Andric #endif 77fe6060f1SDimitry Andric #endif 78fe6060f1SDimitry Andric 790b57cec5SDimitry Andric namespace scudo { 800b57cec5SDimitry Andric 81349cc55cSDimitry Andric typedef uintptr_t uptr; 82349cc55cSDimitry Andric typedef uint8_t u8; 83349cc55cSDimitry Andric typedef uint16_t u16; 84349cc55cSDimitry Andric typedef uint32_t u32; 85349cc55cSDimitry Andric typedef uint64_t u64; 86349cc55cSDimitry Andric typedef intptr_t sptr; 87349cc55cSDimitry Andric typedef int8_t s8; 88349cc55cSDimitry Andric typedef int16_t s16; 89349cc55cSDimitry Andric typedef int32_t s32; 90349cc55cSDimitry Andric typedef int64_t s64; 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric // The following two functions have platform specific implementations. 930b57cec5SDimitry Andric void outputRaw(const char *Buffer); 940b57cec5SDimitry Andric void NORETURN die(); 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric #define RAW_CHECK_MSG(Expr, Msg) \ 970b57cec5SDimitry Andric do { \ 980b57cec5SDimitry Andric if (UNLIKELY(!(Expr))) { \ 990b57cec5SDimitry Andric outputRaw(Msg); \ 1000b57cec5SDimitry Andric die(); \ 1010b57cec5SDimitry Andric } \ 1020b57cec5SDimitry Andric } while (false) 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric #define RAW_CHECK(Expr) RAW_CHECK_MSG(Expr, #Expr) 1050b57cec5SDimitry Andric 1060b57cec5SDimitry Andric void NORETURN reportCheckFailed(const char *File, int Line, 1070b57cec5SDimitry Andric const char *Condition, u64 Value1, u64 Value2); 1080b57cec5SDimitry Andric #define CHECK_IMPL(C1, Op, C2) \ 1090b57cec5SDimitry Andric do { \ 110fe6060f1SDimitry Andric if (UNLIKELY(!(C1 Op C2))) { \ 111fe6060f1SDimitry Andric scudo::reportCheckFailed(__FILE__, __LINE__, #C1 " " #Op " " #C2, \ 112fe6060f1SDimitry Andric (scudo::u64)C1, (scudo::u64)C2); \ 113480093f4SDimitry Andric scudo::die(); \ 1140b57cec5SDimitry Andric } \ 1150b57cec5SDimitry Andric } while (false) 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric #define CHECK(A) CHECK_IMPL((A), !=, 0) 1180b57cec5SDimitry Andric #define CHECK_EQ(A, B) CHECK_IMPL((A), ==, (B)) 1190b57cec5SDimitry Andric #define CHECK_NE(A, B) CHECK_IMPL((A), !=, (B)) 1200b57cec5SDimitry Andric #define CHECK_LT(A, B) CHECK_IMPL((A), <, (B)) 1210b57cec5SDimitry Andric #define CHECK_LE(A, B) CHECK_IMPL((A), <=, (B)) 1220b57cec5SDimitry Andric #define CHECK_GT(A, B) CHECK_IMPL((A), >, (B)) 1230b57cec5SDimitry Andric #define CHECK_GE(A, B) CHECK_IMPL((A), >=, (B)) 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric #if SCUDO_DEBUG 1260b57cec5SDimitry Andric #define DCHECK(A) CHECK(A) 1270b57cec5SDimitry Andric #define DCHECK_EQ(A, B) CHECK_EQ(A, B) 1280b57cec5SDimitry Andric #define DCHECK_NE(A, B) CHECK_NE(A, B) 1290b57cec5SDimitry Andric #define DCHECK_LT(A, B) CHECK_LT(A, B) 1300b57cec5SDimitry Andric #define DCHECK_LE(A, B) CHECK_LE(A, B) 1310b57cec5SDimitry Andric #define DCHECK_GT(A, B) CHECK_GT(A, B) 1320b57cec5SDimitry Andric #define DCHECK_GE(A, B) CHECK_GE(A, B) 1330b57cec5SDimitry Andric #else 134fe6060f1SDimitry Andric #define DCHECK(A) \ 135fe6060f1SDimitry Andric do { \ 136*bdd1243dSDimitry Andric } while (false && (A)) 137fe6060f1SDimitry Andric #define DCHECK_EQ(A, B) \ 138fe6060f1SDimitry Andric do { \ 139*bdd1243dSDimitry Andric } while (false && (A) == (B)) 140fe6060f1SDimitry Andric #define DCHECK_NE(A, B) \ 141fe6060f1SDimitry Andric do { \ 142*bdd1243dSDimitry Andric } while (false && (A) != (B)) 143fe6060f1SDimitry Andric #define DCHECK_LT(A, B) \ 144fe6060f1SDimitry Andric do { \ 145*bdd1243dSDimitry Andric } while (false && (A) < (B)) 146fe6060f1SDimitry Andric #define DCHECK_LE(A, B) \ 147fe6060f1SDimitry Andric do { \ 148*bdd1243dSDimitry Andric } while (false && (A) <= (B)) 149fe6060f1SDimitry Andric #define DCHECK_GT(A, B) \ 150fe6060f1SDimitry Andric do { \ 151*bdd1243dSDimitry Andric } while (false && (A) > (B)) 152fe6060f1SDimitry Andric #define DCHECK_GE(A, B) \ 153fe6060f1SDimitry Andric do { \ 154*bdd1243dSDimitry Andric } while (false && (A) >= (B)) 1550b57cec5SDimitry Andric #endif 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric // The superfluous die() call effectively makes this macro NORETURN. 1580b57cec5SDimitry Andric #define UNREACHABLE(Msg) \ 1590b57cec5SDimitry Andric do { \ 1600b57cec5SDimitry Andric CHECK(0 && Msg); \ 1610b57cec5SDimitry Andric die(); \ 1620b57cec5SDimitry Andric } while (0) 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric } // namespace scudo 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric #endif // SCUDO_INTERNAL_DEFS_H_ 167