1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 #ifndef __LINUX_OVERFLOW_H
3 #define __LINUX_OVERFLOW_H
5 #include <linux/compiler.h>
6 #include <linux/limits.h>
9 * In the fallback code below, we need to compute the minimum and
10 * maximum values representable in a given type. These macros may also
11 * be useful elsewhere, so we provide them outside the
12 * COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW block.
14 * It would seem more obvious to do something like
16 * #define type_min(T) (T)(is_signed_type(T) ? (T)1 << (8*sizeof(T)-1) : 0)
17 * #define type_max(T) (T)(is_signed_type(T) ? ((T)1 << (8*sizeof(T)-1)) - 1 : ~(T)0)
19 * Unfortunately, the middle expressions, strictly speaking, have
20 * undefined behaviour, and at least some versions of gcc warn about
21 * the type_max expression (but not if -fsanitize=undefined is in
22 * effect; in that case, the warning is deferred to runtime...).
24 * The slightly excessive casting in type_min is to make sure the
25 * macros also produce sensible values for the exotic type _Bool. [The
26 * overflow checkers only almost work for _Bool, but that's
27 * a-feature-not-a-bug, since people shouldn't be doing arithmetic on
28 * _Bools. Besides, the gcc builtins don't allow _Bool* as third
32 * https://mail-index.netbsd.org/tech-misc/2007/02/05/0000.html -
33 * credit to Christian Biere.
35 #define is_signed_type(type) (((type)(-1)) < (type)1)
36 #define __type_half_max(type) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type)))
37 #define type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T)))
38 #define type_min(T) ((T)((T)-type_max(T)-(T)1))
41 * Avoids triggering -Wtype-limits compilation warning,
42 * while using unsigned data types to check a < 0.
44 #define is_non_negative(a) ((a) > 0 || (a) == 0)
45 #define is_negative(a) (!(is_non_negative(a)))
48 * Allows for effectively applying __must_check to a macro so we can have
49 * both the type-agnostic benefits of the macros while also being able to
50 * enforce that the return value is, in fact, checked.
52 static inline bool __must_check __must_check_overflow(bool overflow)
54 return unlikely(overflow);
57 #ifdef COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW
59 * For simplicity and code hygiene, the fallback code below insists on
60 * a, b and *d having the same type (similar to the min() and max()
61 * macros), whereas gcc's type-generic overflow checkers accept
62 * different types. Hence we don't just make check_add_overflow an
63 * alias for __builtin_add_overflow, but add type checks similar to
66 #define check_add_overflow(a, b, d) __must_check_overflow(({ \
67 typeof(a) __a = (a); \
68 typeof(b) __b = (b); \
69 typeof(d) __d = (d); \
70 (void) (&__a == &__b); \
71 (void) (&__a == __d); \
72 __builtin_add_overflow(__a, __b, __d); \
75 #define check_sub_overflow(a, b, d) __must_check_overflow(({ \
76 typeof(a) __a = (a); \
77 typeof(b) __b = (b); \
78 typeof(d) __d = (d); \
79 (void) (&__a == &__b); \
80 (void) (&__a == __d); \
81 __builtin_sub_overflow(__a, __b, __d); \
84 #define check_mul_overflow(a, b, d) __must_check_overflow(({ \
85 typeof(a) __a = (a); \
86 typeof(b) __b = (b); \
87 typeof(d) __d = (d); \
88 (void) (&__a == &__b); \
89 (void) (&__a == __d); \
90 __builtin_mul_overflow(__a, __b, __d); \
96 /* Checking for unsigned overflow is relatively easy without causing UB. */
97 #define __unsigned_add_overflow(a, b, d) ({ \
98 typeof(a) __a = (a); \
99 typeof(b) __b = (b); \
100 typeof(d) __d = (d); \
101 (void) (&__a == &__b); \
102 (void) (&__a == __d); \
106 #define __unsigned_sub_overflow(a, b, d) ({ \
107 typeof(a) __a = (a); \
108 typeof(b) __b = (b); \
109 typeof(d) __d = (d); \
110 (void) (&__a == &__b); \
111 (void) (&__a == __d); \
116 * If one of a or b is a compile-time constant, this avoids a division.
118 #define __unsigned_mul_overflow(a, b, d) ({ \
119 typeof(a) __a = (a); \
120 typeof(b) __b = (b); \
121 typeof(d) __d = (d); \
122 (void) (&__a == &__b); \
123 (void) (&__a == __d); \
125 __builtin_constant_p(__b) ? \
126 __b > 0 && __a > type_max(typeof(__a)) / __b : \
127 __a > 0 && __b > type_max(typeof(__b)) / __a; \
131 * For signed types, detecting overflow is much harder, especially if
132 * we want to avoid UB. But the interface of these macros is such that
133 * we must provide a result in *d, and in fact we must produce the
134 * result promised by gcc's builtins, which is simply the possibly
135 * wrapped-around value. Fortunately, we can just formally do the
136 * operations in the widest relevant unsigned type (u64) and then
137 * truncate the result - gcc is smart enough to generate the same code
138 * with and without the (u64) casts.
142 * Adding two signed integers can overflow only if they have the same
143 * sign, and overflow has happened iff the result has the opposite
146 #define __signed_add_overflow(a, b, d) ({ \
147 typeof(a) __a = (a); \
148 typeof(b) __b = (b); \
149 typeof(d) __d = (d); \
150 (void) (&__a == &__b); \
151 (void) (&__a == __d); \
152 *__d = (u64)__a + (u64)__b; \
153 (((~(__a ^ __b)) & (*__d ^ __a)) \
154 & type_min(typeof(__a))) != 0; \
158 * Subtraction is similar, except that overflow can now happen only
159 * when the signs are opposite. In this case, overflow has happened if
160 * the result has the opposite sign of a.
162 #define __signed_sub_overflow(a, b, d) ({ \
163 typeof(a) __a = (a); \
164 typeof(b) __b = (b); \
165 typeof(d) __d = (d); \
166 (void) (&__a == &__b); \
167 (void) (&__a == __d); \
168 *__d = (u64)__a - (u64)__b; \
169 ((((__a ^ __b)) & (*__d ^ __a)) \
170 & type_min(typeof(__a))) != 0; \
174 * Signed multiplication is rather hard. gcc always follows C99, so
175 * division is truncated towards 0. This means that we can write the
176 * overflow check like this:
178 * (a > 0 && (b > MAX/a || b < MIN/a)) ||
179 * (a < -1 && (b > MIN/a || b < MAX/a) ||
180 * (a == -1 && b == MIN)
182 * The redundant casts of -1 are to silence an annoying -Wtype-limits
183 * (included in -Wextra) warning: When the type is u8 or u16, the
184 * __b_c_e in check_mul_overflow obviously selects
185 * __unsigned_mul_overflow, but unfortunately gcc still parses this
186 * code and warns about the limited range of __b.
189 #define __signed_mul_overflow(a, b, d) ({ \
190 typeof(a) __a = (a); \
191 typeof(b) __b = (b); \
192 typeof(d) __d = (d); \
193 typeof(a) __tmax = type_max(typeof(a)); \
194 typeof(a) __tmin = type_min(typeof(a)); \
195 (void) (&__a == &__b); \
196 (void) (&__a == __d); \
197 *__d = (u64)__a * (u64)__b; \
198 (__b > 0 && (__a > __tmax/__b || __a < __tmin/__b)) || \
199 (__b < (typeof(__b))-1 && (__a > __tmin/__b || __a < __tmax/__b)) || \
200 (__b == (typeof(__b))-1 && __a == __tmin); \
204 #define check_add_overflow(a, b, d) __must_check_overflow( \
205 __builtin_choose_expr(is_signed_type(typeof(a)), \
206 __signed_add_overflow(a, b, d), \
207 __unsigned_add_overflow(a, b, d)))
209 #define check_sub_overflow(a, b, d) __must_check_overflow( \
210 __builtin_choose_expr(is_signed_type(typeof(a)), \
211 __signed_sub_overflow(a, b, d), \
212 __unsigned_sub_overflow(a, b, d)))
214 #define check_mul_overflow(a, b, d) __must_check_overflow( \
215 __builtin_choose_expr(is_signed_type(typeof(a)), \
216 __signed_mul_overflow(a, b, d), \
217 __unsigned_mul_overflow(a, b, d)))
219 #endif /* COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW */
221 /** check_shl_overflow() - Calculate a left-shifted value and check overflow
223 * @a: Value to be shifted
224 * @s: How many bits left to shift
225 * @d: Pointer to where to store the result
227 * Computes *@d = (@a << @s)
229 * Returns true if '*d' cannot hold the result or when 'a << s' doesn't
230 * make sense. Example conditions:
231 * - 'a << s' causes bits to be lost when stored in *d.
232 * - 's' is garbage (e.g. negative) or so large that the result of
233 * 'a << s' is guaranteed to be 0.
235 * - 'a << s' sets the sign bit, if any, in '*d'.
237 * '*d' will hold the results of the attempted shift, but is not
238 * considered "safe for use" if true is returned.
240 #define check_shl_overflow(a, s, d) __must_check_overflow(({ \
245 unsigned int _to_shift = \
246 is_non_negative(_s) && _s < 8 * sizeof(*d) ? _s : 0; \
247 *_d = (_a_full << _to_shift); \
248 (_to_shift != _s || is_negative(*_d) || is_negative(_a) || \
249 (*_d >> _to_shift) != _a); \
253 * array_size() - Calculate size of 2-dimensional array.
258 * Calculates size of 2-dimensional array: @a * @b.
260 * Returns: number of bytes needed to represent the array or SIZE_MAX on
263 static inline __must_check size_t array_size(size_t a, size_t b)
267 if (check_mul_overflow(a, b, &bytes))
274 * array3_size() - Calculate size of 3-dimensional array.
278 * @c: dimension three
280 * Calculates size of 3-dimensional array: @a * @b * @c.
282 * Returns: number of bytes needed to represent the array or SIZE_MAX on
285 static inline __must_check size_t array3_size(size_t a, size_t b, size_t c)
289 if (check_mul_overflow(a, b, &bytes))
291 if (check_mul_overflow(bytes, c, &bytes))
298 * Compute a*b+c, returning SIZE_MAX on overflow. Internal helper for
299 * struct_size() below.
301 static inline __must_check size_t __ab_c_size(size_t a, size_t b, size_t c)
305 if (check_mul_overflow(a, b, &bytes))
307 if (check_add_overflow(bytes, c, &bytes))
314 * struct_size() - Calculate size of structure with trailing array.
315 * @p: Pointer to the structure.
316 * @member: Name of the array member.
317 * @count: Number of elements in the array.
319 * Calculates size of memory needed for structure @p followed by an
320 * array of @count number of @member elements.
322 * Return: number of bytes needed or SIZE_MAX on overflow.
324 #define struct_size(p, member, count) \
326 sizeof(*(p)->member) + __must_be_array((p)->member),\
330 * flex_array_size() - Calculate size of a flexible array member
331 * within an enclosing structure.
333 * @p: Pointer to the structure.
334 * @member: Name of the flexible array member.
335 * @count: Number of elements in the array.
337 * Calculates size of a flexible array of @count number of @member
338 * elements, at the end of structure @p.
340 * Return: number of bytes needed or SIZE_MAX on overflow.
342 #define flex_array_size(p, member, count) \
344 sizeof(*(p)->member) + __must_be_array((p)->member))
346 #endif /* __LINUX_OVERFLOW_H */