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
79namespace scudo {
80
81typedef uintptr_t uptr;
82typedef uint8_t u8;
83typedef uint16_t u16;
84typedef uint32_t u32;
85typedef uint64_t u64;
86typedef intptr_t sptr;
87typedef int8_t s8;
88typedef int16_t s16;
89typedef int32_t s32;
90typedef int64_t s64;
91
92// The following two functions have platform specific implementations.
93void outputRaw(const char *Buffer);
94void 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
106void NORETURN reportCheckFailed(const char *File, int Line,
107 const char *Condition, u64 Value1, u64 Value2);
108#define CHECK_IMPL(C1, Op, C2) \
109 do { \
110 if (UNLIKELY(!(C1 Op C2))) { \
111 scudo::reportCheckFailed(__FILE__, __LINE__, #C1 " " #Op " " #C2, \
112 (scudo::u64)C1, (scudo::u64)C2); \
113 scudo::die(); \
114 } \
115 } while (false)
116
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))
124
125#if SCUDO_DEBUG
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)
133#else
134#define DCHECK(A) \
135 do { \
136 } while (false && (A))
137#define DCHECK_EQ(A, B) \
138 do { \
139 } while (false && (A) == (B))
140#define DCHECK_NE(A, B) \
141 do { \
142 } while (false && (A) != (B))
143#define DCHECK_LT(A, B) \
144 do { \
145 } while (false && (A) < (B))
146#define DCHECK_LE(A, B) \
147 do { \
148 } while (false && (A) <= (B))
149#define DCHECK_GT(A, B) \
150 do { \
151 } while (false && (A) > (B))
152#define DCHECK_GE(A, B) \
153 do { \
154 } while (false && (A) >= (B))
155#endif
156
157// The superfluous die() call effectively makes this macro NORETURN.
158#define UNREACHABLE(Msg) \
159 do { \
160 CHECK(0 && Msg); \
161 die(); \
162 } while (0)
163
164} // namespace scudo
165
166#endif // SCUDO_INTERNAL_DEFS_H_
167