1 //===-- internal_defs.h -----------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef SCUDO_INTERNAL_DEFS_H_
10 #define SCUDO_INTERNAL_DEFS_H_
20 #define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
22 // String related macros.
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)
29 // Attributes & builtins related macros.
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))
45 #define PREFETCH(X) __builtin_prefetch(X)
47 #define UNUSED __attribute__((unused))
48 #define USED __attribute__((used))
49 #define NOEXCEPT noexcept
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.
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__))
75 #define SCUDO_REQUIRE_CONSTANT_INITIALIZATION
81 typedef uintptr_t uptr
;
86 typedef intptr_t sptr
;
92 // The following two functions have platform specific implementations.
93 void outputRaw(const char *Buffer
);
96 #define RAW_CHECK_MSG(Expr, Msg) \
98 if (UNLIKELY(!(Expr))) { \
104 #define RAW_CHECK(Expr) RAW_CHECK_MSG(Expr, #Expr)
106 void NORETURN
reportCheckFailed(const char *File
, int Line
,
107 const char *Condition
, u64 Value1
, u64 Value2
);
108 #define CHECK_IMPL(C1, Op, C2) \
110 if (UNLIKELY(!(C1 Op C2))) { \
111 scudo::reportCheckFailed(__FILE__, __LINE__, #C1 " " #Op " " #C2, \
112 (scudo::u64)C1, (scudo::u64)C2); \
117 #define CHECK(A) CHECK_IMPL((A), !=, 0)
118 #define CHECK_EQ(A, B) CHECK_IMPL((A), ==, (B))
119 #define CHECK_NE(A, B) CHECK_IMPL((A), !=, (B))
120 #define CHECK_LT(A, B) CHECK_IMPL((A), <, (B))
121 #define CHECK_LE(A, B) CHECK_IMPL((A), <=, (B))
122 #define CHECK_GT(A, B) CHECK_IMPL((A), >, (B))
123 #define CHECK_GE(A, B) CHECK_IMPL((A), >=, (B))
126 #define DCHECK(A) CHECK(A)
127 #define DCHECK_EQ(A, B) CHECK_EQ(A, B)
128 #define DCHECK_NE(A, B) CHECK_NE(A, B)
129 #define DCHECK_LT(A, B) CHECK_LT(A, B)
130 #define DCHECK_LE(A, B) CHECK_LE(A, B)
131 #define DCHECK_GT(A, B) CHECK_GT(A, B)
132 #define DCHECK_GE(A, B) CHECK_GE(A, B)
136 } while (false && (A))
137 #define DCHECK_EQ(A, B) \
139 } while (false && (A) == (B))
140 #define DCHECK_NE(A, B) \
142 } while (false && (A) != (B))
143 #define DCHECK_LT(A, B) \
145 } while (false && (A) < (B))
146 #define DCHECK_LE(A, B) \
148 } while (false && (A) <= (B))
149 #define DCHECK_GT(A, B) \
151 } while (false && (A) > (B))
152 #define DCHECK_GE(A, B) \
154 } while (false && (A) >= (B))
157 // The superfluous die() call effectively makes this macro NORETURN.
158 #define UNREACHABLE(Msg) \
166 #endif // SCUDO_INTERNAL_DEFS_H_