XenevaOS
Loading...
Searching...
No Matches
overflow.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0 OR MIT */
2#ifndef __LINUX_OVERFLOW_H
3#define __LINUX_OVERFLOW_H
4
5#include <linux/compiler.h>
6#include <linux/limits.h>
7#include <linux/const.h>
8
9/*
10 * We need to compute the minimum and maximum values representable in a given
11 * type. These macros may also be useful elsewhere. It would seem more obvious
12 * to do something like:
13 *
14 * #define type_min(T) (T)(is_signed_type(T) ? (T)1 << (8*sizeof(T)-1) : 0)
15 * #define type_max(T) (T)(is_signed_type(T) ? ((T)1 << (8*sizeof(T)-1)) - 1 : ~(T)0)
16 *
17 * Unfortunately, the middle expressions, strictly speaking, have
18 * undefined behaviour, and at least some versions of gcc warn about
19 * the type_max expression (but not if -fsanitize=undefined is in
20 * effect; in that case, the warning is deferred to runtime...).
21 *
22 * The slightly excessive casting in type_min is to make sure the
23 * macros also produce sensible values for the exotic type _Bool. [The
24 * overflow checkers only almost work for _Bool, but that's
25 * a-feature-not-a-bug, since people shouldn't be doing arithmetic on
26 * _Bools. Besides, the gcc builtins don't allow _Bool* as third
27 * argument.]
28 *
29 * Idea stolen from
30 * https://mail-index.netbsd.org/tech-misc/2007/02/05/0000.html -
31 * credit to Christian Biere.
32 */
33#define __type_half_max(type) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type)))
34#define __type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T)))
35#define type_max(t) __type_max(typeof(t))
36#define __type_min(T) ((T)((T)-type_max(T)-(T)1))
37#define type_min(t) __type_min(typeof(t))
38
39
40/*
41 * Allows for effectively applying __must_check to a macro so we can have
42 * both the type-agnostic benefits of the macros while also being able to
43 * enforce that the return value is, in fact, checked.
44 */
45static __always_inline bool __must_check __must_check_overflow(bool overflow)
46{
47 return unlikely(overflow);
48}
49
61#define check_add_overflow(a, b, d) \
62 __must_check_overflow(__builtin_add_overflow(a, b, d))
63
73#define wrapping_add(type, a, b) \
74 ({ \
75 type __val; \
76 __builtin_add_overflow(a, b, &__val); \
77 __val; \
78 })
79
90#define wrapping_assign_add(var, offset) \
91 ({ \
92 typeof(var) *__ptr = &(var); \
93 *__ptr = wrapping_add(typeof(var), *__ptr, offset); \
94 })
95
107#define check_sub_overflow(a, b, d) \
108 __must_check_overflow(__builtin_sub_overflow(a, b, d))
109
119#define wrapping_sub(type, a, b) \
120 ({ \
121 type __val; \
122 __builtin_sub_overflow(a, b, &__val); \
123 __val; \
124 })
125
136#define wrapping_assign_sub(var, offset) \
137 ({ \
138 typeof(var) *__ptr = &(var); \
139 *__ptr = wrapping_sub(typeof(var), *__ptr, offset); \
140 })
141
153#define check_mul_overflow(a, b, d) \
154 __must_check_overflow(__builtin_mul_overflow(a, b, d))
155
165#define wrapping_mul(type, a, b) \
166 ({ \
167 type __val; \
168 __builtin_mul_overflow(a, b, &__val); \
169 __val; \
170 })
171
192#define check_shl_overflow(a, s, d) __must_check_overflow(({ \
193 typeof(a) _a = a; \
194 typeof(s) _s = s; \
195 typeof(d) _d = d; \
196 unsigned long long _a_full = _a; \
197 unsigned int _to_shift = \
198 _s >= 0 && _s < 8 * sizeof(*d) ? _s : 0; \
199 *_d = (_a_full << _to_shift); \
200 (_to_shift != _s || *_d < 0 || _a < 0 || \
201 (*_d >> _to_shift) != _a); \
202}))
203
204#define __overflows_type_constexpr(x, T) ( \
205 is_unsigned_type(typeof(x)) ? \
206 (x) > type_max(T) : \
207 is_unsigned_type(typeof(T)) ? \
208 (x) < 0 || (x) > type_max(T) : \
209 (x) < type_min(T) || (x) > type_max(T))
210
211#define __overflows_type(x, T) ({ \
212 typeof(T) v = 0; \
213 check_add_overflow((x), v, &v); \
214})
215
230#define overflows_type(n, T) \
231 __builtin_choose_expr(__is_constexpr(n), \
232 __overflows_type_constexpr(n, T), \
233 __overflows_type(n, T))
234
247#define range_overflows(start, size, max) ({ \
248 typeof(start) start__ = (start); \
249 typeof(size) size__ = (size); \
250 typeof(max) max__ = (max); \
251 (void)(&start__ == &size__); \
252 (void)(&start__ == &max__); \
253 start__ >= max__ || size__ > max__ - start__; \
254})
255
267#define range_overflows_t(type, start, size, max) \
268 range_overflows((type)(start), (type)(size), (type)(max))
269
282#define range_end_overflows(start, size, max) ({ \
283 typeof(start) start__ = (start); \
284 typeof(size) size__ = (size); \
285 typeof(max) max__ = (max); \
286 (void)(&start__ == &size__); \
287 (void)(&start__ == &max__); \
288 start__ > max__ || size__ > max__ - start__; \
289})
290
302#define range_end_overflows_t(type, start, size, max) \
303 range_end_overflows((type)(start), (type)(size), (type)(max))
304
316#define castable_to_type(n, T) \
317 __builtin_choose_expr(__is_constexpr(n), \
318 !__overflows_type_constexpr(n, T), \
319 __same_type(n, T))
320
330static __always_inline size_t __must_check size_mul(size_t factor1, size_t factor2)
331{
332 size_t bytes;
333
334 if (check_mul_overflow(factor1, factor2, &bytes))
335 return SIZE_MAX;
336
337 return bytes;
338}
339
349static __always_inline size_t __must_check size_add(size_t addend1, size_t addend2)
350{
351 size_t bytes;
352
353 if (check_add_overflow(addend1, addend2, &bytes))
354 return SIZE_MAX;
355
356 return bytes;
357}
358
370static __always_inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
371{
372 size_t bytes;
373
374 if (minuend == SIZE_MAX || subtrahend == SIZE_MAX ||
375 check_sub_overflow(minuend, subtrahend, &bytes))
376 return SIZE_MAX;
377
378 return bytes;
379}
380
391#define array_size(a, b) size_mul(a, b)
392
404#define array3_size(a, b, c) size_mul(size_mul(a, b), c)
405
418#define flex_array_size(p, member, count) \
419 __builtin_choose_expr(__is_constexpr(count), \
420 (count) * sizeof(*(p)->member) + __must_be_array((p)->member), \
421 size_mul(count, sizeof(*(p)->member) + __must_be_array((p)->member)))
422
434#define struct_size(p, member, count) \
435 __builtin_choose_expr(__is_constexpr(count), \
436 sizeof(*(p)) + flex_array_size(p, member, count), \
437 size_add(sizeof(*(p)), flex_array_size(p, member, count)))
438
452#define struct_size_t(type, member, count) \
453 struct_size((type *)NULL, member, count)
454
465#define struct_offset(p, member) (offsetof(typeof(*(p)), member))
466
477#define __DEFINE_FLEX(type, name, member, count, trailer...) \
478 _Static_assert(__builtin_constant_p(count), \
479 "onstack flex array members require compile-time const count"); \
480 union { \
481 u8 bytes[struct_size_t(type, member, count)]; \
482 type obj; \
483 } name##_u trailer; \
484 type *name = (type *)&name##_u
485
496#define _DEFINE_FLEX(type, name, member, count, initializer...) \
497 __DEFINE_FLEX(type, name, member, count, = { .obj initializer })
498
515#define DEFINE_RAW_FLEX(type, name, member, count) \
516 __DEFINE_FLEX(type, name, member, count, = { })
517
535#define DEFINE_FLEX(TYPE, NAME, MEMBER, COUNTER, COUNT) \
536 _DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .COUNTER = COUNT, })
537
545#define STACK_FLEX_ARRAY_SIZE(name, array) \
546 (__member_size((name)->array) / sizeof(*(name)->array) + \
547 __must_be_array((name)->array))
548
556#define typeof_flex_counter(FAM) \
557 typeof(_Generic(__flex_counter(FAM), \
558 void *: (size_t)0, \
559 default: *__flex_counter(FAM)))
560
572#define overflows_flex_counter_type(TYPE, FAM, COUNT) \
573 (overflows_type(COUNT, typeof_flex_counter(((TYPE *)NULL)->FAM)))
574
584#define __set_flex_counter(FAM, COUNT) \
585({ \
586 *_Generic(__flex_counter(FAM), \
587 void *: &(size_t){ 0 }, \
588 default: __flex_counter(FAM)) = (COUNT); \
589})
590
591#endif /* __LINUX_OVERFLOW_H */
#define SIZE_MAX
Definition limits.h:9
#define unlikely(x)
Definition compiler.h:30
#define __must_check
Definition compiler.h:114
#define __always_inline
Definition compiler_attributes.h:55
#define check_sub_overflow(a, b, d)
Definition overflow.h:107
#define check_mul_overflow(a, b, d)
Definition overflow.h:153
#define check_add_overflow(a, b, d)
Definition overflow.h:61