1 //===-- 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-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef SCUDO_INTERNAL_DEFS_H_ 10 #define SCUDO_INTERNAL_DEFS_H_ 11 12 #include "platform.h" 13 14 #include <stdint.h> 15 16 #ifndef SCUDO_DEBUG 17 #define SCUDO_DEBUG 0 18 #endif 19 20 #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0])) 21 22 // String related macros. 23 24 #define STRINGIFY_(S) #S 25 #define STRINGIFY(S) STRINGIFY_(S) 26 #define CONCATENATE_(S, C) S##C 27 #define CONCATENATE(S, C) CONCATENATE_(S, C) 28 29 // Attributes & builtins related macros. 30 31 #define INTERFACE __attribute__((visibility("default"))) 32 #define HIDDEN __attribute__((visibility("hidden"))) 33 #define WEAK __attribute__((weak)) 34 #define ALWAYS_INLINE inline __attribute__((always_inline)) 35 #define ALIAS(X) __attribute__((alias(X))) 36 #define FORMAT(F, A) __attribute__((format(printf, F, A))) 37 #define NOINLINE __attribute__((noinline)) 38 #define NORETURN __attribute__((noreturn)) 39 #define LIKELY(X) __builtin_expect(!!(X), 1) 40 #define UNLIKELY(X) __builtin_expect(!!(X), 0) 41 #if defined(__i386__) || defined(__x86_64__) 42 // __builtin_prefetch(X) generates prefetchnt0 on x86 43 #define PREFETCH(X) __asm__("prefetchnta (%0)" : : "r"(X)) 44 #else 45 #define PREFETCH(X) __builtin_prefetch(X) 46 #endif 47 #define UNUSED __attribute__((unused)) 48 #define USED __attribute__((used)) 49 #define NOEXCEPT noexcept 50 51 // This check is only available on Clang. This is essentially an alias of 52 // C++20's 'constinit' specifier which will take care of this when (if?) we can 53 // ask all libc's that use Scudo to compile us with C++20. Dynamic 54 // initialization is bad; Scudo is designed to be lazy-initializated on the 55 // first call to malloc/free (and friends), and this generally happens in the 56 // loader somewhere in libdl's init. After the loader is done, control is 57 // transferred to libc's initialization, and the dynamic initializers are run. 58 // If there's a dynamic initializer for Scudo, then it will clobber the 59 // already-initialized Scudo, and re-initialize all its members back to default 60 // values, causing various explosions. Unfortunately, marking 61 // scudo::Allocator<>'s constructor as 'constexpr' isn't sufficient to prevent 62 // dynamic initialization, as default initialization is fine under 'constexpr' 63 // (but not 'constinit'). Clang at -O0, and gcc at all opt levels will emit a 64 // dynamic initializer for any constant-initialized variables if there is a mix 65 // of default-initialized and constant-initialized variables. 66 // 67 // If you're looking at this because your build failed, you probably introduced 68 // a new member to scudo::Allocator<> (possibly transiently) that didn't have an 69 // initializer. The fix is easy - just add one. 70 #if defined(__has_attribute) 71 #if __has_attribute(require_constant_initialization) 72 #define SCUDO_REQUIRE_CONSTANT_INITIALIZATION \ 73 __attribute__((__require_constant_initialization__)) 74 #else 75 #define SCUDO_REQUIRE_CONSTANT_INITIALIZATION 76 #endif 77 #endif 78 79 namespace scudo { 80 81 typedef unsigned long uptr; 82 typedef unsigned char u8; 83 typedef unsigned short u16; 84 typedef unsigned int u32; 85 typedef unsigned long long u64; 86 typedef signed long sptr; 87 typedef signed char s8; 88 typedef signed short s16; 89 typedef signed int s32; 90 typedef signed long long s64; 91 92 // The following two functions have platform specific implementations. 93 void outputRaw(const char *Buffer); 94 void NORETURN die(); 95 96 #define RAW_CHECK_MSG(Expr, Msg) \ 97 do { \ 98 if (UNLIKELY(!(Expr))) { \ 99 outputRaw(Msg); \ 100 die(); \ 101 } \ 102 } while (false) 103 104 #define RAW_CHECK(Expr) RAW_CHECK_MSG(Expr, #Expr) 105 106 void NORETURN reportCheckFailed(const char *File, int Line, 107 const char *Condition, u64 Value1, u64 Value2); 108 109 #define CHECK_IMPL(C1, Op, C2) \ 110 do { \ 111 scudo::u64 V1 = (scudo::u64)(C1); \ 112 scudo::u64 V2 = (scudo::u64)(C2); \ 113 if (UNLIKELY(!(V1 Op V2))) { \ 114 scudo::reportCheckFailed(__FILE__, __LINE__, \ 115 "(" #C1 ") " #Op " (" #C2 ")", V1, V2); \ 116 scudo::die(); \ 117 } \ 118 } while (false) 119 120 #define CHECK(A) CHECK_IMPL((A), !=, 0) 121 #define CHECK_EQ(A, B) CHECK_IMPL((A), ==, (B)) 122 #define CHECK_NE(A, B) CHECK_IMPL((A), !=, (B)) 123 #define CHECK_LT(A, B) CHECK_IMPL((A), <, (B)) 124 #define CHECK_LE(A, B) CHECK_IMPL((A), <=, (B)) 125 #define CHECK_GT(A, B) CHECK_IMPL((A), >, (B)) 126 #define CHECK_GE(A, B) CHECK_IMPL((A), >=, (B)) 127 128 #if SCUDO_DEBUG 129 #define DCHECK(A) CHECK(A) 130 #define DCHECK_EQ(A, B) CHECK_EQ(A, B) 131 #define DCHECK_NE(A, B) CHECK_NE(A, B) 132 #define DCHECK_LT(A, B) CHECK_LT(A, B) 133 #define DCHECK_LE(A, B) CHECK_LE(A, B) 134 #define DCHECK_GT(A, B) CHECK_GT(A, B) 135 #define DCHECK_GE(A, B) CHECK_GE(A, B) 136 #else 137 #define DCHECK(A) \ 138 do { \ 139 } while (false) 140 #define DCHECK_EQ(A, B) \ 141 do { \ 142 } while (false) 143 #define DCHECK_NE(A, B) \ 144 do { \ 145 } while (false) 146 #define DCHECK_LT(A, B) \ 147 do { \ 148 } while (false) 149 #define DCHECK_LE(A, B) \ 150 do { \ 151 } while (false) 152 #define DCHECK_GT(A, B) \ 153 do { \ 154 } while (false) 155 #define DCHECK_GE(A, B) \ 156 do { \ 157 } while (false) 158 #endif 159 160 // The superfluous die() call effectively makes this macro NORETURN. 161 #define UNREACHABLE(Msg) \ 162 do { \ 163 CHECK(0 && Msg); \ 164 die(); \ 165 } while (0) 166 167 } // namespace scudo 168 169 #endif // SCUDO_INTERNAL_DEFS_H_ 170