Commit | Line | Data |
---|---|---|
f0907827 RV |
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> | |
a4947e84 | 6 | #include <linux/limits.h> |
230f6fa2 | 7 | #include <linux/const.h> |
f0907827 RV |
8 | |
9 | /* | |
4eb6bd55 ND |
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: | |
f0907827 RV |
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 | */ | |
f0907827 | 33 | #define __type_half_max(type) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type))) |
bd1ebf24 KC |
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)) | |
f0907827 | 38 | |
dc7fe518 LR |
39 | /* |
40 | * Avoids triggering -Wtype-limits compilation warning, | |
41 | * while using unsigned data types to check a < 0. | |
42 | */ | |
43 | #define is_non_negative(a) ((a) > 0 || (a) == 0) | |
44 | #define is_negative(a) (!(is_non_negative(a))) | |
f0907827 | 45 | |
9b80e4c4 KC |
46 | /* |
47 | * Allows for effectively applying __must_check to a macro so we can have | |
48 | * both the type-agnostic benefits of the macros while also being able to | |
49 | * enforce that the return value is, in fact, checked. | |
50 | */ | |
51 | static inline bool __must_check __must_check_overflow(bool overflow) | |
52 | { | |
53 | return unlikely(overflow); | |
54 | } | |
55 | ||
31970608 KC |
56 | /** |
57 | * check_add_overflow() - Calculate addition with overflow checking | |
d219d2a9 KC |
58 | * @a: first addend |
59 | * @b: second addend | |
60 | * @d: pointer to store sum | |
61 | * | |
3e19086f | 62 | * Returns true on wrap-around, false otherwise. |
d219d2a9 | 63 | * |
3e19086f KC |
64 | * *@d holds the results of the attempted addition, regardless of whether |
65 | * wrap-around occurred. | |
f0907827 | 66 | */ |
d219d2a9 KC |
67 | #define check_add_overflow(a, b, d) \ |
68 | __must_check_overflow(__builtin_add_overflow(a, b, d)) | |
f0907827 | 69 | |
d70de805 KC |
70 | /** |
71 | * wrapping_add() - Intentionally perform a wrapping addition | |
72 | * @type: type for result of calculation | |
73 | * @a: first addend | |
74 | * @b: second addend | |
75 | * | |
76 | * Return the potentially wrapped-around addition without | |
77 | * tripping any wrap-around sanitizers that may be enabled. | |
78 | */ | |
79 | #define wrapping_add(type, a, b) \ | |
80 | ({ \ | |
81 | type __val; \ | |
82 | __builtin_add_overflow(a, b, &__val); \ | |
83 | __val; \ | |
84 | }) | |
85 | ||
08d45ee8 KC |
86 | /** |
87 | * wrapping_assign_add() - Intentionally perform a wrapping increment assignment | |
88 | * @var: variable to be incremented | |
89 | * @offset: amount to add | |
90 | * | |
91 | * Increments @var by @offset with wrap-around. Returns the resulting | |
92 | * value of @var. Will not trip any wrap-around sanitizers. | |
93 | * | |
94 | * Returns the new value of @var. | |
95 | */ | |
96 | #define wrapping_assign_add(var, offset) \ | |
97 | ({ \ | |
98 | typeof(var) *__ptr = &(var); \ | |
99 | *__ptr = wrapping_add(typeof(var), *__ptr, offset); \ | |
100 | }) | |
101 | ||
31970608 KC |
102 | /** |
103 | * check_sub_overflow() - Calculate subtraction with overflow checking | |
d219d2a9 KC |
104 | * @a: minuend; value to subtract from |
105 | * @b: subtrahend; value to subtract from @a | |
106 | * @d: pointer to store difference | |
107 | * | |
3e19086f | 108 | * Returns true on wrap-around, false otherwise. |
d219d2a9 | 109 | * |
3e19086f KC |
110 | * *@d holds the results of the attempted subtraction, regardless of whether |
111 | * wrap-around occurred. | |
d219d2a9 KC |
112 | */ |
113 | #define check_sub_overflow(a, b, d) \ | |
114 | __must_check_overflow(__builtin_sub_overflow(a, b, d)) | |
f0907827 | 115 | |
d70de805 KC |
116 | /** |
117 | * wrapping_sub() - Intentionally perform a wrapping subtraction | |
118 | * @type: type for result of calculation | |
119 | * @a: minuend; value to subtract from | |
120 | * @b: subtrahend; value to subtract from @a | |
121 | * | |
122 | * Return the potentially wrapped-around subtraction without | |
123 | * tripping any wrap-around sanitizers that may be enabled. | |
124 | */ | |
125 | #define wrapping_sub(type, a, b) \ | |
126 | ({ \ | |
127 | type __val; \ | |
128 | __builtin_sub_overflow(a, b, &__val); \ | |
129 | __val; \ | |
130 | }) | |
131 | ||
08d45ee8 KC |
132 | /** |
133 | * wrapping_assign_sub() - Intentionally perform a wrapping decrement assign | |
134 | * @var: variable to be decremented | |
135 | * @offset: amount to subtract | |
136 | * | |
137 | * Decrements @var by @offset with wrap-around. Returns the resulting | |
138 | * value of @var. Will not trip any wrap-around sanitizers. | |
139 | * | |
140 | * Returns the new value of @var. | |
141 | */ | |
142 | #define wrapping_assign_sub(var, offset) \ | |
143 | ({ \ | |
144 | typeof(var) *__ptr = &(var); \ | |
145 | *__ptr = wrapping_sub(typeof(var), *__ptr, offset); \ | |
146 | }) | |
147 | ||
31970608 KC |
148 | /** |
149 | * check_mul_overflow() - Calculate multiplication with overflow checking | |
d219d2a9 KC |
150 | * @a: first factor |
151 | * @b: second factor | |
152 | * @d: pointer to store product | |
153 | * | |
3e19086f | 154 | * Returns true on wrap-around, false otherwise. |
d219d2a9 | 155 | * |
3e19086f KC |
156 | * *@d holds the results of the attempted multiplication, regardless of whether |
157 | * wrap-around occurred. | |
d219d2a9 KC |
158 | */ |
159 | #define check_mul_overflow(a, b, d) \ | |
160 | __must_check_overflow(__builtin_mul_overflow(a, b, d)) | |
f0907827 | 161 | |
d70de805 KC |
162 | /** |
163 | * wrapping_mul() - Intentionally perform a wrapping multiplication | |
164 | * @type: type for result of calculation | |
165 | * @a: first factor | |
166 | * @b: second factor | |
167 | * | |
168 | * Return the potentially wrapped-around multiplication without | |
169 | * tripping any wrap-around sanitizers that may be enabled. | |
170 | */ | |
171 | #define wrapping_mul(type, a, b) \ | |
172 | ({ \ | |
173 | type __val; \ | |
174 | __builtin_mul_overflow(a, b, &__val); \ | |
175 | __val; \ | |
176 | }) | |
177 | ||
31970608 KC |
178 | /** |
179 | * check_shl_overflow() - Calculate a left-shifted value and check overflow | |
0c668477 JG |
180 | * @a: Value to be shifted |
181 | * @s: How many bits left to shift | |
182 | * @d: Pointer to where to store the result | |
183 | * | |
184 | * Computes *@d = (@a << @s) | |
185 | * | |
31970608 | 186 | * Returns true if '*@d' cannot hold the result or when '@a << @s' doesn't |
0c668477 | 187 | * make sense. Example conditions: |
0c668477 | 188 | * |
31970608 KC |
189 | * - '@a << @s' causes bits to be lost when stored in *@d. |
190 | * - '@s' is garbage (e.g. negative) or so large that the result of | |
191 | * '@a << @s' is guaranteed to be 0. | |
192 | * - '@a' is negative. | |
193 | * - '@a << @s' sets the sign bit, if any, in '*@d'. | |
194 | * | |
195 | * '*@d' will hold the results of the attempted shift, but is not | |
4578be13 | 196 | * considered "safe for use" if true is returned. |
0c668477 | 197 | */ |
9b80e4c4 | 198 | #define check_shl_overflow(a, s, d) __must_check_overflow(({ \ |
0c668477 JG |
199 | typeof(a) _a = a; \ |
200 | typeof(s) _s = s; \ | |
201 | typeof(d) _d = d; \ | |
c5e6d3d8 | 202 | unsigned long long _a_full = _a; \ |
0c668477 | 203 | unsigned int _to_shift = \ |
dc7fe518 | 204 | is_non_negative(_s) && _s < 8 * sizeof(*d) ? _s : 0; \ |
0c668477 | 205 | *_d = (_a_full << _to_shift); \ |
dc7fe518 LR |
206 | (_to_shift != _s || is_negative(*_d) || is_negative(_a) || \ |
207 | (*_d >> _to_shift) != _a); \ | |
9b80e4c4 | 208 | })) |
0c668477 | 209 | |
4b21d25b KC |
210 | #define __overflows_type_constexpr(x, T) ( \ |
211 | is_unsigned_type(typeof(x)) ? \ | |
bd1ebf24 | 212 | (x) > type_max(T) : \ |
4b21d25b | 213 | is_unsigned_type(typeof(T)) ? \ |
bd1ebf24 KC |
214 | (x) < 0 || (x) > type_max(T) : \ |
215 | (x) < type_min(T) || (x) > type_max(T)) | |
4b21d25b KC |
216 | |
217 | #define __overflows_type(x, T) ({ \ | |
218 | typeof(T) v = 0; \ | |
219 | check_add_overflow((x), v, &v); \ | |
220 | }) | |
221 | ||
222 | /** | |
223 | * overflows_type - helper for checking the overflows between value, variables, | |
224 | * or data type | |
225 | * | |
226 | * @n: source constant value or variable to be checked | |
227 | * @T: destination variable or data type proposed to store @x | |
228 | * | |
229 | * Compares the @x expression for whether or not it can safely fit in | |
230 | * the storage of the type in @T. @x and @T can have different types. | |
231 | * If @x is a constant expression, this will also resolve to a constant | |
232 | * expression. | |
233 | * | |
234 | * Returns: true if overflow can occur, false otherwise. | |
235 | */ | |
236 | #define overflows_type(n, T) \ | |
237 | __builtin_choose_expr(__is_constexpr(n), \ | |
238 | __overflows_type_constexpr(n, T), \ | |
239 | __overflows_type(n, T)) | |
240 | ||
241 | /** | |
242 | * castable_to_type - like __same_type(), but also allows for casted literals | |
243 | * | |
244 | * @n: variable or constant value | |
245 | * @T: variable or data type | |
246 | * | |
247 | * Unlike the __same_type() macro, this allows a constant value as the | |
248 | * first argument. If this value would not overflow into an assignment | |
249 | * of the second argument's type, it returns true. Otherwise, this falls | |
250 | * back to __same_type(). | |
251 | */ | |
252 | #define castable_to_type(n, T) \ | |
253 | __builtin_choose_expr(__is_constexpr(n), \ | |
254 | !__overflows_type_constexpr(n, T), \ | |
255 | __same_type(n, T)) | |
256 | ||
610b15c5 | 257 | /** |
e1be43d9 | 258 | * size_mul() - Calculate size_t multiplication with saturation at SIZE_MAX |
e1be43d9 KC |
259 | * @factor1: first factor |
260 | * @factor2: second factor | |
610b15c5 | 261 | * |
e1be43d9 KC |
262 | * Returns: calculate @factor1 * @factor2, both promoted to size_t, |
263 | * with any overflow causing the return value to be SIZE_MAX. The | |
264 | * lvalue must be size_t to avoid implicit type conversion. | |
610b15c5 | 265 | */ |
e1be43d9 | 266 | static inline size_t __must_check size_mul(size_t factor1, size_t factor2) |
610b15c5 KC |
267 | { |
268 | size_t bytes; | |
269 | ||
e1be43d9 | 270 | if (check_mul_overflow(factor1, factor2, &bytes)) |
610b15c5 KC |
271 | return SIZE_MAX; |
272 | ||
273 | return bytes; | |
274 | } | |
275 | ||
276 | /** | |
e1be43d9 | 277 | * size_add() - Calculate size_t addition with saturation at SIZE_MAX |
e1be43d9 KC |
278 | * @addend1: first addend |
279 | * @addend2: second addend | |
610b15c5 | 280 | * |
e1be43d9 KC |
281 | * Returns: calculate @addend1 + @addend2, both promoted to size_t, |
282 | * with any overflow causing the return value to be SIZE_MAX. The | |
283 | * lvalue must be size_t to avoid implicit type conversion. | |
610b15c5 | 284 | */ |
e1be43d9 | 285 | static inline size_t __must_check size_add(size_t addend1, size_t addend2) |
610b15c5 KC |
286 | { |
287 | size_t bytes; | |
288 | ||
e1be43d9 | 289 | if (check_add_overflow(addend1, addend2, &bytes)) |
610b15c5 KC |
290 | return SIZE_MAX; |
291 | ||
292 | return bytes; | |
293 | } | |
294 | ||
e1be43d9 KC |
295 | /** |
296 | * size_sub() - Calculate size_t subtraction with saturation at SIZE_MAX | |
e1be43d9 KC |
297 | * @minuend: value to subtract from |
298 | * @subtrahend: value to subtract from @minuend | |
299 | * | |
300 | * Returns: calculate @minuend - @subtrahend, both promoted to size_t, | |
301 | * with any overflow causing the return value to be SIZE_MAX. For | |
302 | * composition with the size_add() and size_mul() helpers, neither | |
303 | * argument may be SIZE_MAX (or the result with be forced to SIZE_MAX). | |
304 | * The lvalue must be size_t to avoid implicit type conversion. | |
e0478542 | 305 | */ |
e1be43d9 | 306 | static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend) |
610b15c5 KC |
307 | { |
308 | size_t bytes; | |
309 | ||
e1be43d9 KC |
310 | if (minuend == SIZE_MAX || subtrahend == SIZE_MAX || |
311 | check_sub_overflow(minuend, subtrahend, &bytes)) | |
610b15c5 KC |
312 | return SIZE_MAX; |
313 | ||
314 | return bytes; | |
315 | } | |
316 | ||
317 | /** | |
e1be43d9 | 318 | * array_size() - Calculate size of 2-dimensional array. |
e1be43d9 KC |
319 | * @a: dimension one |
320 | * @b: dimension two | |
610b15c5 | 321 | * |
e1be43d9 KC |
322 | * Calculates size of 2-dimensional array: @a * @b. |
323 | * | |
324 | * Returns: number of bytes needed to represent the array or SIZE_MAX on | |
325 | * overflow. | |
610b15c5 | 326 | */ |
e1be43d9 KC |
327 | #define array_size(a, b) size_mul(a, b) |
328 | ||
329 | /** | |
330 | * array3_size() - Calculate size of 3-dimensional array. | |
e1be43d9 KC |
331 | * @a: dimension one |
332 | * @b: dimension two | |
333 | * @c: dimension three | |
334 | * | |
335 | * Calculates size of 3-dimensional array: @a * @b * @c. | |
336 | * | |
337 | * Returns: number of bytes needed to represent the array or SIZE_MAX on | |
338 | * overflow. | |
339 | */ | |
340 | #define array3_size(a, b, c) size_mul(size_mul(a, b), c) | |
610b15c5 | 341 | |
b19d57d0 GS |
342 | /** |
343 | * flex_array_size() - Calculate size of a flexible array member | |
344 | * within an enclosing structure. | |
b19d57d0 GS |
345 | * @p: Pointer to the structure. |
346 | * @member: Name of the flexible array member. | |
347 | * @count: Number of elements in the array. | |
348 | * | |
349 | * Calculates size of a flexible array of @count number of @member | |
350 | * elements, at the end of structure @p. | |
351 | * | |
352 | * Return: number of bytes needed or SIZE_MAX on overflow. | |
353 | */ | |
354 | #define flex_array_size(p, member, count) \ | |
230f6fa2 KC |
355 | __builtin_choose_expr(__is_constexpr(count), \ |
356 | (count) * sizeof(*(p)->member) + __must_be_array((p)->member), \ | |
357 | size_mul(count, sizeof(*(p)->member) + __must_be_array((p)->member))) | |
e1be43d9 KC |
358 | |
359 | /** | |
360 | * struct_size() - Calculate size of structure with trailing flexible array. | |
e1be43d9 KC |
361 | * @p: Pointer to the structure. |
362 | * @member: Name of the array member. | |
363 | * @count: Number of elements in the array. | |
364 | * | |
d67790dd | 365 | * Calculates size of memory needed for structure of @p followed by an |
e1be43d9 KC |
366 | * array of @count number of @member elements. |
367 | * | |
368 | * Return: number of bytes needed or SIZE_MAX on overflow. | |
369 | */ | |
370 | #define struct_size(p, member, count) \ | |
230f6fa2 KC |
371 | __builtin_choose_expr(__is_constexpr(count), \ |
372 | sizeof(*(p)) + flex_array_size(p, member, count), \ | |
373 | size_add(sizeof(*(p)), flex_array_size(p, member, count))) | |
b19d57d0 | 374 | |
d67790dd KC |
375 | /** |
376 | * struct_size_t() - Calculate size of structure with trailing flexible array | |
377 | * @type: structure type name. | |
378 | * @member: Name of the array member. | |
379 | * @count: Number of elements in the array. | |
380 | * | |
381 | * Calculates size of memory needed for structure @type followed by an | |
382 | * array of @count number of @member elements. Prefer using struct_size() | |
383 | * when possible instead, to keep calculations associated with a specific | |
384 | * instance variable of type @type. | |
385 | * | |
386 | * Return: number of bytes needed or SIZE_MAX on overflow. | |
387 | */ | |
388 | #define struct_size_t(type, member, count) \ | |
389 | struct_size((type *)NULL, member, count) | |
390 | ||
26dd68d2 PK |
391 | /** |
392 | * _DEFINE_FLEX() - helper macro for DEFINE_FLEX() family. | |
393 | * Enables caller macro to pass (different) initializer. | |
394 | * | |
395 | * @type: structure type name, including "struct" keyword. | |
396 | * @name: Name for a variable to define. | |
397 | * @member: Name of the array member. | |
398 | * @count: Number of elements in the array; must be compile-time const. | |
399 | * @initializer: initializer expression (could be empty for no init). | |
400 | */ | |
d8e45f29 | 401 | #define _DEFINE_FLEX(type, name, member, count, initializer...) \ |
26dd68d2 PK |
402 | _Static_assert(__builtin_constant_p(count), \ |
403 | "onstack flex array members require compile-time const count"); \ | |
404 | union { \ | |
405 | u8 bytes[struct_size_t(type, member, count)]; \ | |
406 | type obj; \ | |
407 | } name##_u initializer; \ | |
408 | type *name = (type *)&name##_u | |
409 | ||
410 | /** | |
d8e45f29 KC |
411 | * DEFINE_RAW_FLEX() - Define an on-stack instance of structure with a trailing |
412 | * flexible array member, when it does not have a __counted_by annotation. | |
26dd68d2 PK |
413 | * |
414 | * @type: structure type name, including "struct" keyword. | |
415 | * @name: Name for a variable to define. | |
416 | * @member: Name of the array member. | |
417 | * @count: Number of elements in the array; must be compile-time const. | |
418 | * | |
419 | * Define a zeroed, on-stack, instance of @type structure with a trailing | |
420 | * flexible array member. | |
421 | * Use __struct_size(@name) to get compile-time size of it afterwards. | |
422 | */ | |
d8e45f29 | 423 | #define DEFINE_RAW_FLEX(type, name, member, count) \ |
26dd68d2 PK |
424 | _DEFINE_FLEX(type, name, member, count, = {}) |
425 | ||
d8e45f29 KC |
426 | /** |
427 | * DEFINE_FLEX() - Define an on-stack instance of structure with a trailing | |
428 | * flexible array member. | |
429 | * | |
430 | * @TYPE: structure type name, including "struct" keyword. | |
431 | * @NAME: Name for a variable to define. | |
432 | * @MEMBER: Name of the array member. | |
433 | * @COUNTER: Name of the __counted_by member. | |
434 | * @COUNT: Number of elements in the array; must be compile-time const. | |
435 | * | |
436 | * Define a zeroed, on-stack, instance of @TYPE structure with a trailing | |
437 | * flexible array member. | |
438 | * Use __struct_size(@NAME) to get compile-time size of it afterwards. | |
439 | */ | |
440 | #define DEFINE_FLEX(TYPE, NAME, MEMBER, COUNTER, COUNT) \ | |
441 | _DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .obj.COUNTER = COUNT, }) | |
442 | ||
f0907827 | 443 | #endif /* __LINUX_OVERFLOW_H */ |