147f0d136SKostya Kortchinsky //===-- internal_defs.h -----------------------------------------*- C++ -*-===// 247f0d136SKostya Kortchinsky // 347f0d136SKostya Kortchinsky // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 447f0d136SKostya Kortchinsky // See https://llvm.org/LICENSE.txt for license information. 547f0d136SKostya Kortchinsky // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 647f0d136SKostya Kortchinsky // 747f0d136SKostya Kortchinsky //===----------------------------------------------------------------------===// 847f0d136SKostya Kortchinsky 947f0d136SKostya Kortchinsky #ifndef SCUDO_INTERNAL_DEFS_H_ 1047f0d136SKostya Kortchinsky #define SCUDO_INTERNAL_DEFS_H_ 1147f0d136SKostya Kortchinsky 1247f0d136SKostya Kortchinsky #include "platform.h" 1347f0d136SKostya Kortchinsky 1441aba567SKostya Kortchinsky #include <stdint.h> 1541aba567SKostya Kortchinsky 1647f0d136SKostya Kortchinsky #ifndef SCUDO_DEBUG 1747f0d136SKostya Kortchinsky #define SCUDO_DEBUG 0 1847f0d136SKostya Kortchinsky #endif 1947f0d136SKostya Kortchinsky 2052bfd673SKostya Kortchinsky #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0])) 2147f0d136SKostya Kortchinsky 2247f0d136SKostya Kortchinsky // String related macros. 2347f0d136SKostya Kortchinsky 2447f0d136SKostya Kortchinsky #define STRINGIFY_(S) #S 2547f0d136SKostya Kortchinsky #define STRINGIFY(S) STRINGIFY_(S) 2647f0d136SKostya Kortchinsky #define CONCATENATE_(S, C) S##C 2747f0d136SKostya Kortchinsky #define CONCATENATE(S, C) CONCATENATE_(S, C) 2847f0d136SKostya Kortchinsky 2947f0d136SKostya Kortchinsky // Attributes & builtins related macros. 3047f0d136SKostya Kortchinsky 3147f0d136SKostya Kortchinsky #define INTERFACE __attribute__((visibility("default"))) 32681773f2SPeter Collingbourne #define HIDDEN __attribute__((visibility("hidden"))) 3347f0d136SKostya Kortchinsky #define WEAK __attribute__((weak)) 3447f0d136SKostya Kortchinsky #define ALWAYS_INLINE inline __attribute__((always_inline)) 35485dbc23SKostya Kortchinsky #define ALIAS(X) __attribute__((alias(X))) 36485dbc23SKostya Kortchinsky #define FORMAT(F, A) __attribute__((format(printf, F, A))) 3747f0d136SKostya Kortchinsky #define NOINLINE __attribute__((noinline)) 3847f0d136SKostya Kortchinsky #define NORETURN __attribute__((noreturn)) 39485dbc23SKostya Kortchinsky #define LIKELY(X) __builtin_expect(!!(X), 1) 40485dbc23SKostya Kortchinsky #define UNLIKELY(X) __builtin_expect(!!(X), 0) 4147f0d136SKostya Kortchinsky #if defined(__i386__) || defined(__x86_64__) 42485dbc23SKostya Kortchinsky // __builtin_prefetch(X) generates prefetchnt0 on x86 43485dbc23SKostya Kortchinsky #define PREFETCH(X) __asm__("prefetchnta (%0)" : : "r"(X)) 4447f0d136SKostya Kortchinsky #else 45485dbc23SKostya Kortchinsky #define PREFETCH(X) __builtin_prefetch(X) 4647f0d136SKostya Kortchinsky #endif 4747f0d136SKostya Kortchinsky #define UNUSED __attribute__((unused)) 4847f0d136SKostya Kortchinsky #define USED __attribute__((used)) 4947f0d136SKostya Kortchinsky #define NOEXCEPT noexcept 5047f0d136SKostya Kortchinsky 51d56ef852SVitaly Buka // This check is only available on Clang. This is essentially an alias of 52d56ef852SVitaly Buka // C++20's 'constinit' specifier which will take care of this when (if?) we can 53d56ef852SVitaly Buka // ask all libc's that use Scudo to compile us with C++20. Dynamic 54d56ef852SVitaly Buka // initialization is bad; Scudo is designed to be lazy-initializated on the 55d56ef852SVitaly Buka // first call to malloc/free (and friends), and this generally happens in the 56d56ef852SVitaly Buka // loader somewhere in libdl's init. After the loader is done, control is 57d56ef852SVitaly Buka // transferred to libc's initialization, and the dynamic initializers are run. 58d56ef852SVitaly Buka // If there's a dynamic initializer for Scudo, then it will clobber the 59d56ef852SVitaly Buka // already-initialized Scudo, and re-initialize all its members back to default 60d56ef852SVitaly Buka // values, causing various explosions. Unfortunately, marking 61d56ef852SVitaly Buka // scudo::Allocator<>'s constructor as 'constexpr' isn't sufficient to prevent 62d56ef852SVitaly Buka // dynamic initialization, as default initialization is fine under 'constexpr' 63d56ef852SVitaly Buka // (but not 'constinit'). Clang at -O0, and gcc at all opt levels will emit a 64d56ef852SVitaly Buka // dynamic initializer for any constant-initialized variables if there is a mix 65d56ef852SVitaly Buka // of default-initialized and constant-initialized variables. 66d56ef852SVitaly Buka // 67d56ef852SVitaly Buka // If you're looking at this because your build failed, you probably introduced 68d56ef852SVitaly Buka // a new member to scudo::Allocator<> (possibly transiently) that didn't have an 69d56ef852SVitaly Buka // initializer. The fix is easy - just add one. 70d56ef852SVitaly Buka #if defined(__has_attribute) 71d56ef852SVitaly Buka #if __has_attribute(require_constant_initialization) 72d56ef852SVitaly Buka #define SCUDO_REQUIRE_CONSTANT_INITIALIZATION \ 73d56ef852SVitaly Buka __attribute__((__require_constant_initialization__)) 74d56ef852SVitaly Buka #else 75d56ef852SVitaly Buka #define SCUDO_REQUIRE_CONSTANT_INITIALIZATION 76d56ef852SVitaly Buka #endif 77d56ef852SVitaly Buka #endif 78d56ef852SVitaly Buka 7947f0d136SKostya Kortchinsky namespace scudo { 8047f0d136SKostya Kortchinsky 81b2aaafb8SKostya Kortchinsky typedef uintptr_t uptr; 82b2aaafb8SKostya Kortchinsky typedef uint8_t u8; 83b2aaafb8SKostya Kortchinsky typedef uint16_t u16; 84b2aaafb8SKostya Kortchinsky typedef uint32_t u32; 85b2aaafb8SKostya Kortchinsky typedef uint64_t u64; 86b2aaafb8SKostya Kortchinsky typedef intptr_t sptr; 87b2aaafb8SKostya Kortchinsky typedef int8_t s8; 88b2aaafb8SKostya Kortchinsky typedef int16_t s16; 89b2aaafb8SKostya Kortchinsky typedef int32_t s32; 90b2aaafb8SKostya Kortchinsky typedef int64_t s64; 9147f0d136SKostya Kortchinsky 9241aba567SKostya Kortchinsky // The following two functions have platform specific implementations. 9341aba567SKostya Kortchinsky void outputRaw(const char *Buffer); 9441aba567SKostya Kortchinsky void NORETURN die(); 9547f0d136SKostya Kortchinsky 9641aba567SKostya Kortchinsky #define RAW_CHECK_MSG(Expr, Msg) \ 9741aba567SKostya Kortchinsky do { \ 9841aba567SKostya Kortchinsky if (UNLIKELY(!(Expr))) { \ 9941aba567SKostya Kortchinsky outputRaw(Msg); \ 10041aba567SKostya Kortchinsky die(); \ 10141aba567SKostya Kortchinsky } \ 10241aba567SKostya Kortchinsky } while (false) 10347f0d136SKostya Kortchinsky 10441aba567SKostya Kortchinsky #define RAW_CHECK(Expr) RAW_CHECK_MSG(Expr, #Expr) 10547f0d136SKostya Kortchinsky 106485dbc23SKostya Kortchinsky void NORETURN reportCheckFailed(const char *File, int Line, 107485dbc23SKostya Kortchinsky const char *Condition, u64 Value1, u64 Value2); 108485dbc23SKostya Kortchinsky #define CHECK_IMPL(C1, Op, C2) \ 10947f0d136SKostya Kortchinsky do { \ 1108e30b55cSVitaly Buka if (UNLIKELY(!(C1 Op C2))) { \ 1118e30b55cSVitaly Buka scudo::reportCheckFailed(__FILE__, __LINE__, #C1 " " #Op " " #C2, \ 1128e30b55cSVitaly Buka (scudo::u64)C1, (scudo::u64)C2); \ 113f018246cSKostya Kortchinsky scudo::die(); \ 11441aba567SKostya Kortchinsky } \ 11547f0d136SKostya Kortchinsky } while (false) 11647f0d136SKostya Kortchinsky 117485dbc23SKostya Kortchinsky #define CHECK(A) CHECK_IMPL((A), !=, 0) 118485dbc23SKostya Kortchinsky #define CHECK_EQ(A, B) CHECK_IMPL((A), ==, (B)) 119485dbc23SKostya Kortchinsky #define CHECK_NE(A, B) CHECK_IMPL((A), !=, (B)) 120485dbc23SKostya Kortchinsky #define CHECK_LT(A, B) CHECK_IMPL((A), <, (B)) 121485dbc23SKostya Kortchinsky #define CHECK_LE(A, B) CHECK_IMPL((A), <=, (B)) 122485dbc23SKostya Kortchinsky #define CHECK_GT(A, B) CHECK_IMPL((A), >, (B)) 123485dbc23SKostya Kortchinsky #define CHECK_GE(A, B) CHECK_IMPL((A), >=, (B)) 12447f0d136SKostya Kortchinsky 12547f0d136SKostya Kortchinsky #if SCUDO_DEBUG 126485dbc23SKostya Kortchinsky #define DCHECK(A) CHECK(A) 127485dbc23SKostya Kortchinsky #define DCHECK_EQ(A, B) CHECK_EQ(A, B) 128485dbc23SKostya Kortchinsky #define DCHECK_NE(A, B) CHECK_NE(A, B) 129485dbc23SKostya Kortchinsky #define DCHECK_LT(A, B) CHECK_LT(A, B) 130485dbc23SKostya Kortchinsky #define DCHECK_LE(A, B) CHECK_LE(A, B) 131485dbc23SKostya Kortchinsky #define DCHECK_GT(A, B) CHECK_GT(A, B) 132485dbc23SKostya Kortchinsky #define DCHECK_GE(A, B) CHECK_GE(A, B) 13347f0d136SKostya Kortchinsky #else 134d9b2641aSRoland McGrath #define DCHECK(A) \ 135d9b2641aSRoland McGrath do { \ 136*f3f4bc81SFlorian Mayer } while (false && (A)) 137d9b2641aSRoland McGrath #define DCHECK_EQ(A, B) \ 138d9b2641aSRoland McGrath do { \ 139*f3f4bc81SFlorian Mayer } while (false && (A) == (B)) 140d9b2641aSRoland McGrath #define DCHECK_NE(A, B) \ 141d9b2641aSRoland McGrath do { \ 142*f3f4bc81SFlorian Mayer } while (false && (A) != (B)) 143d9b2641aSRoland McGrath #define DCHECK_LT(A, B) \ 144d9b2641aSRoland McGrath do { \ 145*f3f4bc81SFlorian Mayer } while (false && (A) < (B)) 146d9b2641aSRoland McGrath #define DCHECK_LE(A, B) \ 147d9b2641aSRoland McGrath do { \ 148*f3f4bc81SFlorian Mayer } while (false && (A) <= (B)) 149d9b2641aSRoland McGrath #define DCHECK_GT(A, B) \ 150d9b2641aSRoland McGrath do { \ 151*f3f4bc81SFlorian Mayer } while (false && (A) > (B)) 152d9b2641aSRoland McGrath #define DCHECK_GE(A, B) \ 153d9b2641aSRoland McGrath do { \ 154*f3f4bc81SFlorian Mayer } while (false && (A) >= (B)) 15547f0d136SKostya Kortchinsky #endif 15647f0d136SKostya Kortchinsky 15741aba567SKostya Kortchinsky // The superfluous die() call effectively makes this macro NORETURN. 158485dbc23SKostya Kortchinsky #define UNREACHABLE(Msg) \ 15947f0d136SKostya Kortchinsky do { \ 160485dbc23SKostya Kortchinsky CHECK(0 && Msg); \ 16141aba567SKostya Kortchinsky die(); \ 16241aba567SKostya Kortchinsky } while (0) 16341aba567SKostya Kortchinsky 16447f0d136SKostya Kortchinsky } // namespace scudo 16547f0d136SKostya Kortchinsky 16647f0d136SKostya Kortchinsky #endif // SCUDO_INTERNAL_DEFS_H_ 167