Coverage Report

Created: 2024-08-17 06:50

/src/systemd/src/fundamental/macro-fundamental.h
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
#pragma once
3
4
#ifndef SD_BOOT
5
#  include <assert.h>
6
#endif
7
8
#include <limits.h>
9
#include <stdbool.h>
10
#include <stddef.h>
11
#include <stdint.h>
12
13
#define _align_(x) __attribute__((__aligned__(x)))
14
#define _alignas_(x) __attribute__((__aligned__(__alignof__(x))))
15
#define _alignptr_ __attribute__((__aligned__(sizeof(void *))))
16
19.2k
#define _cleanup_(x) __attribute__((__cleanup__(x)))
17
#define _const_ __attribute__((__const__))
18
#define _deprecated_ __attribute__((__deprecated__))
19
#define _destructor_ __attribute__((__destructor__))
20
#define _hidden_ __attribute__((__visibility__("hidden")))
21
#define _likely_(x) (__builtin_expect(!!(x), 1))
22
#define _malloc_ __attribute__((__malloc__))
23
#define _noreturn_ _Noreturn
24
#define _packed_ __attribute__((__packed__))
25
#define _printf_(a, b) __attribute__((__format__(printf, a, b)))
26
#define _public_ __attribute__((__visibility__("default")))
27
#define _pure_ __attribute__((__pure__))
28
#define _retain_ __attribute__((__retain__))
29
#define _returns_nonnull_ __attribute__((__returns_nonnull__))
30
#define _section_(x) __attribute__((__section__(x)))
31
#define _sentinel_ __attribute__((__sentinel__))
32
36.8k
#define _unlikely_(x) (__builtin_expect(!!(x), 0))
33
#define _unused_ __attribute__((__unused__))
34
#define _used_ __attribute__((__used__))
35
#define _warn_unused_result_ __attribute__((__warn_unused_result__))
36
#define _weak_ __attribute__((__weak__))
37
#define _weakref_(x) __attribute__((__weakref__(#x)))
38
39
#ifdef __clang__
40
#  define _alloc_(...)
41
#else
42
#  define _alloc_(...) __attribute__((__alloc_size__(__VA_ARGS__)))
43
#endif
44
45
#if __GNUC__ >= 7 || (defined(__clang__) && __clang_major__ >= 10)
46
#  define _fallthrough_ __attribute__((__fallthrough__))
47
#else
48
#  define _fallthrough_
49
#endif
50
51
#define XSTRINGIFY(x) #x
52
#define STRINGIFY(x) XSTRINGIFY(x)
53
54
#ifndef __COVERITY__
55
158k
#  define VOID_0 ((void)0)
56
#else
57
#  define VOID_0 ((void*)0)
58
#endif
59
60
#define ELEMENTSOF(x)                                                   \
61
172k
        (__builtin_choose_expr(                                         \
62
172k
                !__builtin_types_compatible_p(typeof(x), typeof(&*(x))), \
63
172k
                sizeof(x)/sizeof((x)[0]),                               \
64
172k
                VOID_0))
65
66
12
#define XCONCATENATE(x, y) x ## y
67
48
#define CONCATENATE(x, y) XCONCATENATE(x, y)
68
69
#ifdef SD_BOOT
70
        _noreturn_ void efi_assert(const char *expr, const char *file, unsigned line, const char *function);
71
72
        #ifdef NDEBUG
73
                #define assert(expr)
74
                #define assert_not_reached() __builtin_unreachable()
75
        #else
76
                #define assert(expr) ({ _likely_(expr) ? VOID_0 : efi_assert(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); })
77
                #define assert_not_reached() efi_assert("Code should not be reached", __FILE__, __LINE__, __PRETTY_FUNCTION__)
78
        #endif
79
        #define static_assert _Static_assert
80
        #define assert_se(expr) ({ _likely_(expr) ? VOID_0 : efi_assert(#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__); })
81
#endif
82
83
/* This passes the argument through after (if asserts are enabled) checking that it is not null. */
84
2.13k
#define ASSERT_PTR(expr) _ASSERT_PTR(expr, UNIQ_T(_expr_, UNIQ), assert)
85
#define ASSERT_SE_PTR(expr) _ASSERT_PTR(expr, UNIQ_T(_expr_, UNIQ), assert_se)
86
#define _ASSERT_PTR(expr, var, check)      \
87
2.13k
        ({                                 \
88
2.13k
                typeof(expr) var = (expr); \
89
2.13k
                check(var);                \
90
2.13k
                var;                       \
91
2.13k
        })
92
93
#define ASSERT_NONNEG(expr)                              \
94
        ({                                               \
95
                typeof(expr) _expr_ = (expr), _zero = 0; \
96
                assert(_expr_ >= _zero);                 \
97
                _expr_;                                  \
98
        })
99
100
#define ASSERT_SE_NONNEG(expr)                           \
101
        ({                                               \
102
                typeof(expr) _expr_ = (expr), _zero = 0; \
103
                assert_se(_expr_ >= _zero);              \
104
                _expr_;                                  \
105
        })
106
107
371
#define assert_cc(expr) static_assert(expr, #expr)
108
109
110
48
#define UNIQ_T(x, uniq) CONCATENATE(__unique_prefix_, CONCATENATE(x, uniq))
111
#define UNIQ __COUNTER__
112
113
/* Note that this works differently from pthread_once(): this macro does
114
 * not synchronize code execution, i.e. code that is run conditionalized
115
 * on this macro will run concurrently to all other code conditionalized
116
 * the same way, there's no ordering or completion enforced. */
117
#define ONCE __ONCE(UNIQ_T(_once_, UNIQ))
118
#define __ONCE(o)                                                  \
119
        ({                                                         \
120
                static bool (o) = false;                           \
121
                __atomic_exchange_n(&(o), true, __ATOMIC_SEQ_CST); \
122
        })
123
124
#undef MAX
125
#define MAX(a, b) __MAX(UNIQ, (a), UNIQ, (b))
126
#define __MAX(aq, a, bq, b)                             \
127
        ({                                              \
128
                const typeof(a) UNIQ_T(A, aq) = (a);    \
129
                const typeof(b) UNIQ_T(B, bq) = (b);    \
130
                UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \
131
        })
132
133
#define IS_UNSIGNED_INTEGER_TYPE(type) \
134
        (__builtin_types_compatible_p(typeof(type), unsigned char) ||   \
135
         __builtin_types_compatible_p(typeof(type), unsigned short) ||  \
136
         __builtin_types_compatible_p(typeof(type), unsigned) ||        \
137
         __builtin_types_compatible_p(typeof(type), unsigned long) ||   \
138
         __builtin_types_compatible_p(typeof(type), unsigned long long))
139
140
#define IS_SIGNED_INTEGER_TYPE(type) \
141
        (__builtin_types_compatible_p(typeof(type), signed char) ||   \
142
         __builtin_types_compatible_p(typeof(type), signed short) ||  \
143
         __builtin_types_compatible_p(typeof(type), signed) ||        \
144
         __builtin_types_compatible_p(typeof(type), signed long) ||   \
145
         __builtin_types_compatible_p(typeof(type), signed long long))
146
147
/* Evaluates to (void) if _A or _B are not constant or of different types (being integers of different sizes
148
 * is also OK as long as the signedness matches) */
149
#define CONST_MAX(_A, _B) \
150
        (__builtin_choose_expr(                                         \
151
                __builtin_constant_p(_A) &&                             \
152
                __builtin_constant_p(_B) &&                             \
153
                (__builtin_types_compatible_p(typeof(_A), typeof(_B)) || \
154
                 (IS_UNSIGNED_INTEGER_TYPE(_A) && IS_UNSIGNED_INTEGER_TYPE(_B)) || \
155
                 (IS_SIGNED_INTEGER_TYPE(_A) && IS_SIGNED_INTEGER_TYPE(_B))), \
156
                ((_A) > (_B)) ? (_A) : (_B),                            \
157
                VOID_0))
158
159
/* takes two types and returns the size of the larger one */
160
#define MAXSIZE(A, B) (sizeof(union _packed_ { typeof(A) a; typeof(B) b; }))
161
162
#define MAX3(x, y, z)                                   \
163
        ({                                              \
164
                const typeof(x) _c = MAX(x, y);         \
165
                MAX(_c, z);                             \
166
        })
167
168
#define MAX4(x, y, z, a)                                \
169
        ({                                              \
170
                const typeof(x) _d = MAX3(x, y, z);     \
171
                MAX(_d, a);                             \
172
        })
173
174
#undef MIN
175
12
#define MIN(a, b) __MIN(UNIQ, (a), UNIQ, (b))
176
#define __MIN(aq, a, bq, b)                             \
177
12
        ({                                              \
178
12
                const typeof(a) UNIQ_T(A, aq) = (a);    \
179
12
                const typeof(b) UNIQ_T(B, bq) = (b);    \
180
12
                UNIQ_T(A, aq) < UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \
181
12
        })
182
183
/* evaluates to (void) if _A or _B are not constant or of different types */
184
#define CONST_MIN(_A, _B) \
185
        (__builtin_choose_expr(                                         \
186
                __builtin_constant_p(_A) &&                             \
187
                __builtin_constant_p(_B) &&                             \
188
                __builtin_types_compatible_p(typeof(_A), typeof(_B)),   \
189
                ((_A) < (_B)) ? (_A) : (_B),                            \
190
                VOID_0))
191
192
#define MIN3(x, y, z)                                   \
193
        ({                                              \
194
                const typeof(x) _c = MIN(x, y);         \
195
                MIN(_c, z);                             \
196
        })
197
198
/* Returns true if the passed integer is a positive power of two */
199
#define CONST_ISPOWEROF2(x)                     \
200
        ((x) > 0 && ((x) & ((x) - 1)) == 0)
201
202
#define ISPOWEROF2(x)                                                  \
203
        __builtin_choose_expr(                                         \
204
                __builtin_constant_p(x),                               \
205
                CONST_ISPOWEROF2(x),                                   \
206
                ({                                                     \
207
                        const typeof(x) _x = (x);                      \
208
                        CONST_ISPOWEROF2(_x);                          \
209
                }))
210
211
#define LESS_BY(a, b) __LESS_BY(UNIQ, (a), UNIQ, (b))
212
#define __LESS_BY(aq, a, bq, b)                         \
213
        ({                                              \
214
                const typeof(a) UNIQ_T(A, aq) = (a);    \
215
                const typeof(b) UNIQ_T(B, bq) = (b);    \
216
                UNIQ_T(A, aq) > UNIQ_T(B, bq) ? UNIQ_T(A, aq) - UNIQ_T(B, bq) : 0; \
217
        })
218
219
#define CMP(a, b) __CMP(UNIQ, (a), UNIQ, (b))
220
#define __CMP(aq, a, bq, b)                             \
221
        ({                                              \
222
                const typeof(a) UNIQ_T(A, aq) = (a);    \
223
                const typeof(b) UNIQ_T(B, bq) = (b);    \
224
                UNIQ_T(A, aq) < UNIQ_T(B, bq) ? -1 :    \
225
                UNIQ_T(A, aq) > UNIQ_T(B, bq) ? 1 : 0;  \
226
        })
227
228
#undef CLAMP
229
#define CLAMP(x, low, high) __CLAMP(UNIQ, (x), UNIQ, (low), UNIQ, (high))
230
#define __CLAMP(xq, x, lowq, low, highq, high)                          \
231
        ({                                                              \
232
                const typeof(x) UNIQ_T(X, xq) = (x);                    \
233
                const typeof(low) UNIQ_T(LOW, lowq) = (low);            \
234
                const typeof(high) UNIQ_T(HIGH, highq) = (high);        \
235
                        UNIQ_T(X, xq) > UNIQ_T(HIGH, highq) ?           \
236
                                UNIQ_T(HIGH, highq) :                   \
237
                                UNIQ_T(X, xq) < UNIQ_T(LOW, lowq) ?     \
238
                                        UNIQ_T(LOW, lowq) :             \
239
                                        UNIQ_T(X, xq);                  \
240
        })
241
242
/* [(x + y - 1) / y] suffers from an integer overflow, even though the
243
 * computation should be possible in the given type. Therefore, we use
244
 * [x / y + !!(x % y)]. Note that on "Real CPUs" a division returns both the
245
 * quotient and the remainder, so both should be equally fast. */
246
#define DIV_ROUND_UP(x, y) __DIV_ROUND_UP(UNIQ, (x), UNIQ, (y))
247
#define __DIV_ROUND_UP(xq, x, yq, y)                                    \
248
        ({                                                              \
249
                const typeof(x) UNIQ_T(X, xq) = (x);                    \
250
                const typeof(y) UNIQ_T(Y, yq) = (y);                    \
251
                (UNIQ_T(X, xq) / UNIQ_T(Y, yq) + !!(UNIQ_T(X, xq) % UNIQ_T(Y, yq))); \
252
        })
253
254
4
#define CASE_F(X) case X:
255
2
#define CASE_F_1(CASE, X) CASE_F(X)
256
2
#define CASE_F_2(CASE, X, ...)  CASE(X) CASE_F_1(CASE, __VA_ARGS__)
257
0
#define CASE_F_3(CASE, X, ...)  CASE(X) CASE_F_2(CASE, __VA_ARGS__)
258
#define CASE_F_4(CASE, X, ...)  CASE(X) CASE_F_3(CASE, __VA_ARGS__)
259
#define CASE_F_5(CASE, X, ...)  CASE(X) CASE_F_4(CASE, __VA_ARGS__)
260
#define CASE_F_6(CASE, X, ...)  CASE(X) CASE_F_5(CASE, __VA_ARGS__)
261
#define CASE_F_7(CASE, X, ...)  CASE(X) CASE_F_6(CASE, __VA_ARGS__)
262
#define CASE_F_8(CASE, X, ...)  CASE(X) CASE_F_7(CASE, __VA_ARGS__)
263
#define CASE_F_9(CASE, X, ...)  CASE(X) CASE_F_8(CASE, __VA_ARGS__)
264
#define CASE_F_10(CASE, X, ...) CASE(X) CASE_F_9(CASE, __VA_ARGS__)
265
#define CASE_F_11(CASE, X, ...) CASE(X) CASE_F_10(CASE, __VA_ARGS__)
266
#define CASE_F_12(CASE, X, ...) CASE(X) CASE_F_11(CASE, __VA_ARGS__)
267
#define CASE_F_13(CASE, X, ...) CASE(X) CASE_F_12(CASE, __VA_ARGS__)
268
#define CASE_F_14(CASE, X, ...) CASE(X) CASE_F_13(CASE, __VA_ARGS__)
269
#define CASE_F_15(CASE, X, ...) CASE(X) CASE_F_14(CASE, __VA_ARGS__)
270
#define CASE_F_16(CASE, X, ...) CASE(X) CASE_F_15(CASE, __VA_ARGS__)
271
#define CASE_F_17(CASE, X, ...) CASE(X) CASE_F_16(CASE, __VA_ARGS__)
272
#define CASE_F_18(CASE, X, ...) CASE(X) CASE_F_17(CASE, __VA_ARGS__)
273
#define CASE_F_19(CASE, X, ...) CASE(X) CASE_F_18(CASE, __VA_ARGS__)
274
#define CASE_F_20(CASE, X, ...) CASE(X) CASE_F_19(CASE, __VA_ARGS__)
275
276
0
#define GET_CASE_F(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,_11,_12,_13,_14,_15,_16,_17,_18,_19,_20,NAME,...) NAME
277
#define FOR_EACH_MAKE_CASE(...) \
278
0
        GET_CASE_F(__VA_ARGS__,CASE_F_20,CASE_F_19,CASE_F_18,CASE_F_17,CASE_F_16,CASE_F_15,CASE_F_14,CASE_F_13,CASE_F_12,CASE_F_11, \
279
2
                               CASE_F_10,CASE_F_9,CASE_F_8,CASE_F_7,CASE_F_6,CASE_F_5,CASE_F_4,CASE_F_3,CASE_F_2,CASE_F_1) \
280
2
                   (CASE_F,__VA_ARGS__)
281
282
#define IN_SET(x, ...)                                                  \
283
472
        ({                                                              \
284
472
                bool _found = false;                                    \
285
472
                /* If the build breaks in the line below, you need to extend the case macros. (We use "long double" as  \
286
472
                 * type for the array, in the hope that checkers such as ubsan don't complain that the initializers for \
287
472
                 * the array are not representable by the base type. Ideally we'd use typeof(x) as base type, but that  \
288
472
                 * doesn't work, as we want to use this on bitfields and gcc refuses typeof() on bitfields.) */         \
289
472
                static const long double __assert_in_set[] _unused_ = { __VA_ARGS__ }; \
290
472
                assert_cc(ELEMENTSOF(__assert_in_set) <= 20);           \
291
472
                switch (x) {                                            \
292
2
                FOR_EACH_MAKE_CASE(__VA_ARGS__)                         \
293
2
                        _found = true;                                  \
294
2
                       break;                                           \
295
234
                default:                                                \
296
234
                        break;                                          \
297
472
                }                                                       \
298
472
                _found;                                                 \
299
236
        })
300
301
/* Takes inspiration from Rust's Option::take() method: reads and returns a pointer, but at the same time
302
 * resets it to NULL. See: https://doc.rust-lang.org/std/option/enum.Option.html#method.take */
303
#define TAKE_PTR(ptr)                           \
304
6.00k
        ({                                      \
305
6.00k
                typeof(ptr) *_pptr_ = &(ptr);   \
306
6.00k
                typeof(ptr) _ptr_ = *_pptr_;    \
307
6.00k
                *_pptr_ = NULL;                 \
308
6.00k
                _ptr_;                          \
309
6.00k
        })
310
311
/*
312
 * STRLEN - return the length of a string literal, minus the trailing NUL byte.
313
 *          Contrary to strlen(), this is a constant expression.
314
 * @x: a string literal.
315
 */
316
0
#define STRLEN(x) (sizeof(""x"") - sizeof(typeof(x[0])))
317
318
#define mfree(memory)                           \
319
10.5k
        ({                                      \
320
10.5k
                free(memory);                   \
321
10.5k
                (typeof(memory)) NULL;          \
322
10.5k
        })
323
324
0
static inline size_t ALIGN_TO(size_t l, size_t ali) {
325
0
        assert(ISPOWEROF2(ali));
326
0
327
0
        if (l > SIZE_MAX - (ali - 1))
328
0
                return SIZE_MAX; /* indicate overflow */
329
0
330
0
        return ((l + ali - 1) & ~(ali - 1));
331
0
}
Unexecuted instantiation: fuzz-link-parser.c:ALIGN_TO
Unexecuted instantiation: link-config.c:ALIGN_TO
Unexecuted instantiation: link-config-gperf.c:ALIGN_TO
332
333
#define ALIGN4(l) ALIGN_TO(l, 4)
334
#define ALIGN8(l) ALIGN_TO(l, 8)
335
#ifndef SD_BOOT
336
/* libefi also provides ALIGN, and we do not use them in sd-boot explicitly. */
337
#define ALIGN(l)  ALIGN_TO(l, sizeof(void*))
338
#define ALIGN_PTR(p) ((void*) ALIGN((uintptr_t) (p)))
339
#endif
340
341
/* Same as ALIGN_TO but callable in constant contexts. */
342
#define CONST_ALIGN_TO(l, ali)                                         \
343
        __builtin_choose_expr(                                         \
344
                __builtin_constant_p(l) &&                             \
345
                __builtin_constant_p(ali) &&                           \
346
                CONST_ISPOWEROF2(ali) &&                               \
347
                (l <= SIZE_MAX - (ali - 1)),      /* overflow? */      \
348
                ((l) + (ali) - 1) & ~((ali) - 1),                      \
349
                VOID_0)
350
351
#define UPDATE_FLAG(orig, flag, b)                      \
352
        ((b) ? ((orig) | (flag)) : ((orig) & ~(flag)))
353
#define SET_FLAG(v, flag, b) \
354
        (v) = UPDATE_FLAG(v, flag, b)
355
#define FLAGS_SET(v, flags) \
356
4
        ((~(v) & (flags)) == 0)