3333 * credit to Christian Biere.
3434 */
3535#define __type_half_max (type ) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type)))
36- #define type_max (T ) ((T)((__type_half_max(T) - 1) + __type_half_max(T)))
37- #define type_min (T ) ((T)((T)-type_max(T)-(T)1))
36+ #define __type_max (T ) ((T)((__type_half_max(T) - 1) + __type_half_max(T)))
37+ #define type_max (t ) __type_max(typeof(t))
38+ #define __type_min (T ) ((T)((T)-type_max(T)-(T)1))
39+ #define type_min (t ) __type_min(typeof(t))
3840
3941/*
4042 * Avoids triggering -Wtype-limits compilation warning,
@@ -59,45 +61,122 @@ static inline bool __must_check __must_check_overflow(bool overflow)
5961 * @b: second addend
6062 * @d: pointer to store sum
6163 *
62- * Returns 0 on success .
64+ * Returns true on wrap-around, false otherwise .
6365 *
64- * *@d holds the results of the attempted addition, but is not considered
65- * "safe for use" on a non-zero return value, which indicates that the
66- * sum has overflowed or been truncated.
66+ * *@d holds the results of the attempted addition, regardless of whether
67+ * wrap-around occurred.
6768 */
6869#define check_add_overflow (a , b , d ) \
6970 __must_check_overflow(__builtin_add_overflow(a, b, d))
7071
72+ /**
73+ * wrapping_add() - Intentionally perform a wrapping addition
74+ * @type: type for result of calculation
75+ * @a: first addend
76+ * @b: second addend
77+ *
78+ * Return the potentially wrapped-around addition without
79+ * tripping any wrap-around sanitizers that may be enabled.
80+ */
81+ #define wrapping_add (type , a , b ) \
82+ ({ \
83+ type __val; \
84+ __builtin_add_overflow(a, b, &__val); \
85+ __val; \
86+ })
87+
88+ /**
89+ * wrapping_assign_add() - Intentionally perform a wrapping increment assignment
90+ * @var: variable to be incremented
91+ * @offset: amount to add
92+ *
93+ * Increments @var by @offset with wrap-around. Returns the resulting
94+ * value of @var. Will not trip any wrap-around sanitizers.
95+ *
96+ * Returns the new value of @var.
97+ */
98+ #define wrapping_assign_add (var , offset ) \
99+ ({ \
100+ typeof(var) *__ptr = &(var); \
101+ *__ptr = wrapping_add(typeof(var), *__ptr, offset); \
102+ })
103+
71104/**
72105 * check_sub_overflow() - Calculate subtraction with overflow checking
73106 * @a: minuend; value to subtract from
74107 * @b: subtrahend; value to subtract from @a
75108 * @d: pointer to store difference
76109 *
77- * Returns 0 on success .
110+ * Returns true on wrap-around, false otherwise .
78111 *
79- * *@d holds the results of the attempted subtraction, but is not considered
80- * "safe for use" on a non-zero return value, which indicates that the
81- * difference has underflowed or been truncated.
112+ * *@d holds the results of the attempted subtraction, regardless of whether
113+ * wrap-around occurred.
82114 */
83115#define check_sub_overflow (a , b , d ) \
84116 __must_check_overflow(__builtin_sub_overflow(a, b, d))
85117
118+ /**
119+ * wrapping_sub() - Intentionally perform a wrapping subtraction
120+ * @type: type for result of calculation
121+ * @a: minuend; value to subtract from
122+ * @b: subtrahend; value to subtract from @a
123+ *
124+ * Return the potentially wrapped-around subtraction without
125+ * tripping any wrap-around sanitizers that may be enabled.
126+ */
127+ #define wrapping_sub (type , a , b ) \
128+ ({ \
129+ type __val; \
130+ __builtin_sub_overflow(a, b, &__val); \
131+ __val; \
132+ })
133+
134+ /**
135+ * wrapping_assign_sub() - Intentionally perform a wrapping decrement assign
136+ * @var: variable to be decremented
137+ * @offset: amount to subtract
138+ *
139+ * Decrements @var by @offset with wrap-around. Returns the resulting
140+ * value of @var. Will not trip any wrap-around sanitizers.
141+ *
142+ * Returns the new value of @var.
143+ */
144+ #define wrapping_assign_sub (var , offset ) \
145+ ({ \
146+ typeof(var) *__ptr = &(var); \
147+ *__ptr = wrapping_sub(typeof(var), *__ptr, offset); \
148+ })
149+
86150/**
87151 * check_mul_overflow() - Calculate multiplication with overflow checking
88152 * @a: first factor
89153 * @b: second factor
90154 * @d: pointer to store product
91155 *
92- * Returns 0 on success .
156+ * Returns true on wrap-around, false otherwise .
93157 *
94- * *@d holds the results of the attempted multiplication, but is not
95- * considered "safe for use" on a non-zero return value, which indicates
96- * that the product has overflowed or been truncated.
158+ * *@d holds the results of the attempted multiplication, regardless of whether
159+ * wrap-around occurred.
97160 */
98161#define check_mul_overflow (a , b , d ) \
99162 __must_check_overflow(__builtin_mul_overflow(a, b, d))
100163
164+ /**
165+ * wrapping_mul() - Intentionally perform a wrapping multiplication
166+ * @type: type for result of calculation
167+ * @a: first factor
168+ * @b: second factor
169+ *
170+ * Return the potentially wrapped-around multiplication without
171+ * tripping any wrap-around sanitizers that may be enabled.
172+ */
173+ #define wrapping_mul (type , a , b ) \
174+ ({ \
175+ type __val; \
176+ __builtin_mul_overflow(a, b, &__val); \
177+ __val; \
178+ })
179+
101180/**
102181 * check_shl_overflow() - Calculate a left-shifted value and check overflow
103182 * @a: Value to be shifted
@@ -122,7 +201,7 @@ static inline bool __must_check __must_check_overflow(bool overflow)
122201 typeof(a) _a = a; \
123202 typeof(s) _s = s; \
124203 typeof(d) _d = d; \
125- u64 _a_full = _a; \
204+ unsigned long long _a_full = _a; \
126205 unsigned int _to_shift = \
127206 is_non_negative(_s) && _s < 8 * sizeof(*d) ? _s : 0; \
128207 *_d = (_a_full << _to_shift); \
@@ -132,10 +211,10 @@ static inline bool __must_check __must_check_overflow(bool overflow)
132211
133212#define __overflows_type_constexpr (x , T ) ( \
134213 is_unsigned_type(typeof(x)) ? \
135- (x) > type_max(typeof(T)) : \
214+ (x) > type_max(T) : \
136215 is_unsigned_type(typeof(T)) ? \
137- (x) < 0 || (x) > type_max(typeof(T)) : \
138- (x) < type_min(typeof(T)) || (x) > type_max(typeof(T) ))
216+ (x) < 0 || (x) > type_max(T) : \
217+ (x) < type_min(T) || (x) > type_max(T ))
139218
140219#define __overflows_type (x , T ) ({ \
141220 typeof(T) v = 0; \
@@ -312,27 +391,40 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
312391 struct_size((type *)NULL, member, count)
313392
314393/**
315- * _DEFINE_FLEX () - helper macro for DEFINE_FLEX() family.
316- * Enables caller macro to pass (different) initializer.
394+ * __DEFINE_FLEX () - helper macro for DEFINE_FLEX() family.
395+ * Enables caller macro to pass arbitrary trailing expressions
317396 *
318397 * @type: structure type name, including "struct" keyword.
319398 * @name: Name for a variable to define.
320399 * @member: Name of the array member.
321400 * @count: Number of elements in the array; must be compile-time const.
322- * @initializer: initializer expression (could be empty for no init) .
401+ * @trailer: Trailing expressions for attributes and/or initializers .
323402 */
324- #define _DEFINE_FLEX (type , name , member , count , initializer ) \
403+ #define __DEFINE_FLEX (type , name , member , count , trailer ... ) \
325404 _Static_assert(__builtin_constant_p(count), \
326405 "onstack flex array members require compile-time const count"); \
327406 union { \
328407 u8 bytes[struct_size_t(type, member, count)]; \
329408 type obj; \
330- } name##_u initializer ; \
409+ } name##_u trailer ; \
331410 type *name = (type *)&name##_u
332411
333412/**
334- * DEFINE_FLEX() - Define an on-stack instance of structure with a trailing
335- * flexible array member.
413+ * _DEFINE_FLEX() - helper macro for DEFINE_FLEX() family.
414+ * Enables caller macro to pass (different) initializer.
415+ *
416+ * @type: structure type name, including "struct" keyword.
417+ * @name: Name for a variable to define.
418+ * @member: Name of the array member.
419+ * @count: Number of elements in the array; must be compile-time const.
420+ * @initializer: Initializer expression (e.g., pass `= { }` at minimum).
421+ */
422+ #define _DEFINE_FLEX (type , name , member , count , initializer ...) \
423+ __DEFINE_FLEX(type, name, member, count, = { .obj initializer })
424+
425+ /**
426+ * DEFINE_RAW_FLEX() - Define an on-stack instance of structure with a trailing
427+ * flexible array member, when it does not have a __counted_by annotation.
336428 *
337429 * @type: structure type name, including "struct" keyword.
338430 * @name: Name for a variable to define.
@@ -342,8 +434,42 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
342434 * Define a zeroed, on-stack, instance of @type structure with a trailing
343435 * flexible array member.
344436 * Use __struct_size(@name) to get compile-time size of it afterwards.
437+ * Use __member_size(@name->member) to get compile-time size of @name members.
438+ * Use STACK_FLEX_ARRAY_SIZE(@name, @member) to get compile-time number of
439+ * elements in array @member.
440+ */
441+ #define DEFINE_RAW_FLEX (type , name , member , count ) \
442+ __DEFINE_FLEX(type, name, member, count, = { })
443+
444+ /**
445+ * DEFINE_FLEX() - Define an on-stack instance of structure with a trailing
446+ * flexible array member.
447+ *
448+ * @TYPE: structure type name, including "struct" keyword.
449+ * @NAME: Name for a variable to define.
450+ * @MEMBER: Name of the array member.
451+ * @COUNTER: Name of the __counted_by member.
452+ * @COUNT: Number of elements in the array; must be compile-time const.
453+ *
454+ * Define a zeroed, on-stack, instance of @TYPE structure with a trailing
455+ * flexible array member.
456+ * Use __struct_size(@NAME) to get compile-time size of it afterwards.
457+ * Use __member_size(@NAME->member) to get compile-time size of @NAME members.
458+ * Use STACK_FLEX_ARRAY_SIZE(@name, @member) to get compile-time number of
459+ * elements in array @member.
460+ */
461+ #define DEFINE_FLEX (TYPE , NAME , MEMBER , COUNTER , COUNT ) \
462+ _DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .COUNTER = COUNT, })
463+
464+ /**
465+ * STACK_FLEX_ARRAY_SIZE() - helper macro for DEFINE_FLEX() family.
466+ * Returns the number of elements in @array.
467+ *
468+ * @name: Name for a variable defined in DEFINE_RAW_FLEX()/DEFINE_FLEX().
469+ * @array: Name of the array member.
345470 */
346- #define DEFINE_FLEX (type , name , member , count ) \
347- _DEFINE_FLEX(type, name, member, count, = {})
471+ #define STACK_FLEX_ARRAY_SIZE (name , array ) \
472+ (__member_size((name)->array) / sizeof(*(name)->array) + \
473+ __must_be_array((name)->array))
348474
349475#endif /* _LINUXKPI_LINUX_OVERFLOW_H */
0 commit comments