]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
c2f1db8f | 2 | #pragma once |
60918275 | 3 | |
0c15577a | 4 | #include "macro-fundamental.h" /* IWYU pragma: export */ |
e5bc5f1f | 5 | |
8b5ce495 DDM |
6 | #if !defined(HAS_FEATURE_MEMORY_SANITIZER) |
7 | # if defined(__has_feature) | |
8 | # if __has_feature(memory_sanitizer) | |
9 | # define HAS_FEATURE_MEMORY_SANITIZER 1 | |
10 | # endif | |
11 | # endif | |
12 | # if !defined(HAS_FEATURE_MEMORY_SANITIZER) | |
13 | # define HAS_FEATURE_MEMORY_SANITIZER 0 | |
14 | # endif | |
15 | #endif | |
16 | ||
17 | #if !defined(HAS_FEATURE_ADDRESS_SANITIZER) | |
18 | # ifdef __SANITIZE_ADDRESS__ | |
19 | # define HAS_FEATURE_ADDRESS_SANITIZER 1 | |
20 | # elif defined(__has_feature) | |
21 | # if __has_feature(address_sanitizer) | |
22 | # define HAS_FEATURE_ADDRESS_SANITIZER 1 | |
23 | # endif | |
24 | # endif | |
25 | # if !defined(HAS_FEATURE_ADDRESS_SANITIZER) | |
26 | # define HAS_FEATURE_ADDRESS_SANITIZER 0 | |
27 | # endif | |
28 | #endif | |
29 | ||
026c2677 LP |
30 | /* Note: on GCC "no_sanitize_address" is a function attribute only, on llvm it may also be applied to global |
31 | * variables. We define a specific macro which knows this. Note that on GCC we don't need this decorator so much, since | |
7227dd81 | 32 | * our primary use case for this attribute is registration structures placed in named ELF sections which shall not be |
026c2677 LP |
33 | * padded, but GCC doesn't pad those anyway if AddressSanitizer is enabled. */ |
34 | #if HAS_FEATURE_ADDRESS_SANITIZER && defined(__clang__) | |
35 | #define _variable_no_sanitize_address_ __attribute__((__no_sanitize_address__)) | |
36 | #else | |
37 | #define _variable_no_sanitize_address_ | |
38 | #endif | |
39 | ||
8e2fa6e2 LP |
40 | /* Apparently there's no has_feature() call defined to check for ubsan, hence let's define this |
41 | * unconditionally on llvm */ | |
42 | #if defined(__clang__) | |
43 | #define _function_no_sanitize_float_cast_overflow_ __attribute__((no_sanitize("float-cast-overflow"))) | |
44 | #else | |
45 | #define _function_no_sanitize_float_cast_overflow_ | |
46 | #endif | |
47 | ||
3be6ab5c | 48 | /* test harness */ |
49e5de64 ZJS |
49 | #define EXIT_TEST_SKIP 77 |
50 | ||
ffee7b97 YW |
51 | static inline uint64_t u64_multiply_safe(uint64_t a, uint64_t b) { |
52 | if (_unlikely_(a != 0 && b > (UINT64_MAX / a))) | |
53 | return 0; /* overflow */ | |
54 | ||
55 | return a * b; | |
56 | } | |
57 | ||
625e870b DH |
58 | /* align to next higher power-of-2 (except for: 0 => 0, overflow => 0) */ |
59 | static inline unsigned long ALIGN_POWER2(unsigned long u) { | |
85c267af LP |
60 | |
61 | /* Avoid subtraction overflow */ | |
62 | if (u == 0) | |
63 | return 0; | |
64 | ||
625e870b DH |
65 | /* clz(0) is undefined */ |
66 | if (u == 1) | |
67 | return 1; | |
68 | ||
69 | /* left-shift overflow is undefined */ | |
70 | if (__builtin_clzl(u - 1UL) < 1) | |
71 | return 0; | |
72 | ||
73 | return 1UL << (sizeof(u) * 8 - __builtin_clzl(u - 1UL)); | |
74 | } | |
75 | ||
bbc98d32 KS |
76 | /* |
77 | * container_of - cast a member of a structure out to the containing structure | |
78 | * @ptr: the pointer to the member. | |
79 | * @type: the type of the container struct this is embedded in. | |
80 | * @member: the name of the member within the struct. | |
bbc98d32 | 81 | */ |
fb835651 DH |
82 | #define container_of(ptr, type, member) __container_of(UNIQ, (ptr), type, member) |
83 | #define __container_of(uniq, ptr, type, member) \ | |
117efe06 | 84 | ({ \ |
fb835651 | 85 | const typeof( ((type*)0)->member ) *UNIQ_T(A, uniq) = (ptr); \ |
117efe06 | 86 | (type*)( (char *)UNIQ_T(A, uniq) - offsetof(type, member) ); \ |
fb835651 | 87 | }) |
bbc98d32 | 88 | |
a3dc3547 KS |
89 | #define PTR_TO_INT(p) ((int) ((intptr_t) (p))) |
90 | #define INT_TO_PTR(u) ((void *) ((intptr_t) (u))) | |
14cb109d | 91 | #define PTR_TO_UINT(p) ((unsigned) ((uintptr_t) (p))) |
a3dc3547 | 92 | #define UINT_TO_PTR(u) ((void *) ((uintptr_t) (u))) |
60918275 | 93 | |
a3dc3547 KS |
94 | #define PTR_TO_LONG(p) ((long) ((intptr_t) (p))) |
95 | #define LONG_TO_PTR(u) ((void *) ((intptr_t) (u))) | |
c6c18be3 | 96 | #define PTR_TO_ULONG(p) ((unsigned long) ((uintptr_t) (p))) |
a3dc3547 | 97 | #define ULONG_TO_PTR(u) ((void *) ((uintptr_t) (u))) |
c6c18be3 | 98 | |
4081756a YW |
99 | #define PTR_TO_UINT8(p) ((uint8_t) ((uintptr_t) (p))) |
100 | #define UINT8_TO_PTR(u) ((void *) ((uintptr_t) (u))) | |
101 | ||
a3dc3547 KS |
102 | #define PTR_TO_INT32(p) ((int32_t) ((intptr_t) (p))) |
103 | #define INT32_TO_PTR(u) ((void *) ((intptr_t) (u))) | |
104 | #define PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p))) | |
105 | #define UINT32_TO_PTR(u) ((void *) ((uintptr_t) (u))) | |
60918275 | 106 | |
a3dc3547 KS |
107 | #define PTR_TO_INT64(p) ((int64_t) ((intptr_t) (p))) |
108 | #define INT64_TO_PTR(u) ((void *) ((intptr_t) (u))) | |
109 | #define PTR_TO_UINT64(p) ((uint64_t) ((uintptr_t) (p))) | |
110 | #define UINT64_TO_PTR(u) ((void *) ((uintptr_t) (u))) | |
c6c18be3 | 111 | |
a9c55a88 LP |
112 | #define CHAR_TO_STR(x) ((char[2]) { x, 0 }) |
113 | ||
034c6ed7 LP |
114 | #define char_array_0(x) x[sizeof(x)-1] = 0; |
115 | ||
37232d55 LB |
116 | /* Maximum buffer size needed for formatting an unsigned integer type as hex, including space for '0x' |
117 | * prefix and trailing NUL suffix. */ | |
118 | #define HEXADECIMAL_STR_MAX(type) (2 + sizeof(type) * 2 + 1) | |
119 | ||
56da8d5a LP |
120 | /* Returns the number of chars needed to format variables of the specified type as a decimal string. Adds in |
121 | * extra space for a negative '-' prefix for signed types. Includes space for the trailing NUL. */ | |
fa70beaa | 122 | #define DECIMAL_STR_MAX(type) \ |
56da8d5a LP |
123 | ((size_t) IS_SIGNED_INTEGER_TYPE(type) + 1U + \ |
124 | (sizeof(type) <= 1 ? 3U : \ | |
d3e40294 ZJS |
125 | sizeof(type) <= 2 ? 5U : \ |
126 | sizeof(type) <= 4 ? 10U : \ | |
56da8d5a | 127 | sizeof(type) <= 8 ? (IS_SIGNED_INTEGER_TYPE(type) ? 19U : 20U) : sizeof(int[-2*(sizeof(type) > 8)]))) |
fa70beaa | 128 | |
92463840 LP |
129 | /* Returns the number of chars needed to format the specified integer value. It's hence more specific than |
130 | * DECIMAL_STR_MAX() which answers the same question for all possible values of the specified type. Does | |
131 | * *not* include space for a trailing NUL. (If you wonder why we special case _x_ == 0 here: it's to trick | |
132 | * out gcc's -Wtype-limits, which would complain on comparing an unsigned type with < 0, otherwise. By | |
133 | * special-casing == 0 here first, we can use <= 0 instead of < 0 to trick out gcc.) */ | |
e3dd9ea8 FS |
134 | #define DECIMAL_STR_WIDTH(x) \ |
135 | ({ \ | |
136 | typeof(x) _x_ = (x); \ | |
92463840 LP |
137 | size_t ans; \ |
138 | if (_x_ == 0) \ | |
139 | ans = 1; \ | |
140 | else { \ | |
141 | ans = _x_ <= 0 ? 2 : 1; \ | |
142 | while ((_x_ /= 10) != 0) \ | |
143 | ans++; \ | |
144 | } \ | |
e3dd9ea8 | 145 | ans; \ |
0d1dbeb3 LP |
146 | }) |
147 | ||
35aa04e9 LP |
148 | #define SWAP_TWO(x, y) do { \ |
149 | typeof(x) _t = (x); \ | |
150 | (x) = (y); \ | |
151 | (y) = (_t); \ | |
152 | } while (false) | |
153 | ||
46bf625a ZJS |
154 | #define STRV_MAKE(...) ((char**) ((const char*[]) { __VA_ARGS__, NULL })) |
155 | #define STRV_MAKE_EMPTY ((char*[1]) { NULL }) | |
8b8024f1 | 156 | #define STRV_MAKE_CONST(...) ((const char* const*) ((const char*[]) { __VA_ARGS__, NULL })) |
46bf625a | 157 | |
66032ef4 LP |
158 | /* Pointers range from NULL to POINTER_MAX */ |
159 | #define POINTER_MAX ((void*) UINTPTR_MAX) | |
160 | ||
ed50f18c LP |
161 | /* A macro to force copying of a variable from memory. This is useful whenever we want to read something from |
162 | * memory and want to make sure the compiler won't optimize away the destination variable for us. It's not | |
163 | * supposed to be a full CPU memory barrier, i.e. CPU is still allowed to reorder the reads, but it is not | |
164 | * allowed to remove our local copies of the variables. We want this to work for unaligned memory, hence | |
165 | * memcpy() is great for our purposes. */ | |
166 | #define READ_NOW(x) \ | |
167 | ({ \ | |
168 | typeof(x) _copy; \ | |
169 | memcpy(&_copy, &(x), sizeof(_copy)); \ | |
170 | asm volatile ("" : : : "memory"); \ | |
171 | _copy; \ | |
172 | }) | |
173 | ||
8b0c4347 ZJS |
174 | #define saturate_add(x, y, limit) \ |
175 | ({ \ | |
176 | typeof(limit) _x = (x); \ | |
177 | typeof(limit) _y = (y); \ | |
178 | _x > (limit) || _y >= (limit) - _x ? (limit) : _x + _y; \ | |
179 | }) | |
180 | ||
b0e3d799 | 181 | static inline size_t size_add(size_t x, size_t y) { |
8b0c4347 | 182 | return saturate_add(x, y, SIZE_MAX); |
b0e3d799 | 183 | } |
7c502303 | 184 | |
30fd9a2d | 185 | /* A little helper for subtracting 1 off a pointer in a safe UB-free way. This is intended to be used for |
50996f04 LP |
186 | * loops that count down from a high pointer until some base. A naive loop would implement this like this: |
187 | * | |
188 | * for (p = end-1; p >= base; p--) … | |
189 | * | |
190 | * But this is not safe because p before the base is UB in C. With this macro the loop becomes this instead: | |
191 | * | |
192 | * for (p = PTR_SUB1(end, base); p; p = PTR_SUB1(p, base)) … | |
193 | * | |
194 | * And is free from UB! */ | |
195 | #define PTR_SUB1(p, base) \ | |
196 | ({ \ | |
197 | typeof(p) _q = (p); \ | |
198 | _q && _q > (base) ? &_q[-1] : NULL; \ | |
199 | }) | |
200 | ||
e9a46668 | 201 | /* Iterate through each argument passed. All must be the same type as 'entry' or must be implicitly |
94d82b59 | 202 | * convertible. The iteration variable 'entry' must already be defined. */ |
e9a46668 MY |
203 | #define FOREACH_ARGUMENT(entry, ...) \ |
204 | _FOREACH_ARGUMENT(entry, UNIQ_T(_entries_, UNIQ), UNIQ_T(_current_, UNIQ), UNIQ_T(_va_sentinel_, UNIQ), ##__VA_ARGS__) | |
205 | #define _FOREACH_ARGUMENT(entry, _entries_, _current_, _va_sentinel_, ...) \ | |
dc571ccc FS |
206 | for (typeof(entry) _va_sentinel_[1] = {}, _entries_[] = { __VA_ARGS__ __VA_OPT__(,) _va_sentinel_[0] }, *_current_ = _entries_; \ |
207 | ((long)(_current_ - _entries_) < (long)(ELEMENTSOF(_entries_) - 1)) && ({ entry = *_current_; true; }); \ | |
e179f2d8 | 208 | _current_++) |