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> |
f0907827 RV |
7 | |
8 | /* | |
4eb6bd55 ND |
9 | * We need to compute the minimum and maximum values representable in a given |
10 | * type. These macros may also be useful elsewhere. It would seem more obvious | |
11 | * to do something like: | |
f0907827 RV |
12 | * |
13 | * #define type_min(T) (T)(is_signed_type(T) ? (T)1 << (8*sizeof(T)-1) : 0) | |
14 | * #define type_max(T) (T)(is_signed_type(T) ? ((T)1 << (8*sizeof(T)-1)) - 1 : ~(T)0) | |
15 | * | |
16 | * Unfortunately, the middle expressions, strictly speaking, have | |
17 | * undefined behaviour, and at least some versions of gcc warn about | |
18 | * the type_max expression (but not if -fsanitize=undefined is in | |
19 | * effect; in that case, the warning is deferred to runtime...). | |
20 | * | |
21 | * The slightly excessive casting in type_min is to make sure the | |
22 | * macros also produce sensible values for the exotic type _Bool. [The | |
23 | * overflow checkers only almost work for _Bool, but that's | |
24 | * a-feature-not-a-bug, since people shouldn't be doing arithmetic on | |
25 | * _Bools. Besides, the gcc builtins don't allow _Bool* as third | |
26 | * argument.] | |
27 | * | |
28 | * Idea stolen from | |
29 | * https://mail-index.netbsd.org/tech-misc/2007/02/05/0000.html - | |
30 | * credit to Christian Biere. | |
31 | */ | |
32 | #define is_signed_type(type) (((type)(-1)) < (type)1) | |
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_min(T) ((T)((T)-type_max(T)-(T)1)) | |
36 | ||
dc7fe518 LR |
37 | /* |
38 | * Avoids triggering -Wtype-limits compilation warning, | |
39 | * while using unsigned data types to check a < 0. | |
40 | */ | |
41 | #define is_non_negative(a) ((a) > 0 || (a) == 0) | |
42 | #define is_negative(a) (!(is_non_negative(a))) | |
f0907827 | 43 | |
9b80e4c4 KC |
44 | /* |
45 | * Allows for effectively applying __must_check to a macro so we can have | |
46 | * both the type-agnostic benefits of the macros while also being able to | |
47 | * enforce that the return value is, in fact, checked. | |
48 | */ | |
49 | static inline bool __must_check __must_check_overflow(bool overflow) | |
50 | { | |
51 | return unlikely(overflow); | |
52 | } | |
53 | ||
f0907827 RV |
54 | /* |
55 | * For simplicity and code hygiene, the fallback code below insists on | |
56 | * a, b and *d having the same type (similar to the min() and max() | |
57 | * macros), whereas gcc's type-generic overflow checkers accept | |
58 | * different types. Hence we don't just make check_add_overflow an | |
59 | * alias for __builtin_add_overflow, but add type checks similar to | |
60 | * below. | |
61 | */ | |
9b80e4c4 | 62 | #define check_add_overflow(a, b, d) __must_check_overflow(({ \ |
f0907827 RV |
63 | typeof(a) __a = (a); \ |
64 | typeof(b) __b = (b); \ | |
65 | typeof(d) __d = (d); \ | |
66 | (void) (&__a == &__b); \ | |
67 | (void) (&__a == __d); \ | |
68 | __builtin_add_overflow(__a, __b, __d); \ | |
9b80e4c4 | 69 | })) |
f0907827 | 70 | |
9b80e4c4 | 71 | #define check_sub_overflow(a, b, d) __must_check_overflow(({ \ |
f0907827 RV |
72 | typeof(a) __a = (a); \ |
73 | typeof(b) __b = (b); \ | |
74 | typeof(d) __d = (d); \ | |
75 | (void) (&__a == &__b); \ | |
76 | (void) (&__a == __d); \ | |
77 | __builtin_sub_overflow(__a, __b, __d); \ | |
9b80e4c4 | 78 | })) |
f0907827 | 79 | |
9b80e4c4 | 80 | #define check_mul_overflow(a, b, d) __must_check_overflow(({ \ |
f0907827 RV |
81 | typeof(a) __a = (a); \ |
82 | typeof(b) __b = (b); \ | |
83 | typeof(d) __d = (d); \ | |
84 | (void) (&__a == &__b); \ | |
85 | (void) (&__a == __d); \ | |
86 | __builtin_mul_overflow(__a, __b, __d); \ | |
9b80e4c4 | 87 | })) |
f0907827 | 88 | |
0c668477 JG |
89 | /** check_shl_overflow() - Calculate a left-shifted value and check overflow |
90 | * | |
91 | * @a: Value to be shifted | |
92 | * @s: How many bits left to shift | |
93 | * @d: Pointer to where to store the result | |
94 | * | |
95 | * Computes *@d = (@a << @s) | |
96 | * | |
97 | * Returns true if '*d' cannot hold the result or when 'a << s' doesn't | |
98 | * make sense. Example conditions: | |
99 | * - 'a << s' causes bits to be lost when stored in *d. | |
100 | * - 's' is garbage (e.g. negative) or so large that the result of | |
101 | * 'a << s' is guaranteed to be 0. | |
102 | * - 'a' is negative. | |
103 | * - 'a << s' sets the sign bit, if any, in '*d'. | |
104 | * | |
105 | * '*d' will hold the results of the attempted shift, but is not | |
4578be13 | 106 | * considered "safe for use" if true is returned. |
0c668477 | 107 | */ |
9b80e4c4 | 108 | #define check_shl_overflow(a, s, d) __must_check_overflow(({ \ |
0c668477 JG |
109 | typeof(a) _a = a; \ |
110 | typeof(s) _s = s; \ | |
111 | typeof(d) _d = d; \ | |
112 | u64 _a_full = _a; \ | |
113 | unsigned int _to_shift = \ | |
dc7fe518 | 114 | is_non_negative(_s) && _s < 8 * sizeof(*d) ? _s : 0; \ |
0c668477 | 115 | *_d = (_a_full << _to_shift); \ |
dc7fe518 LR |
116 | (_to_shift != _s || is_negative(*_d) || is_negative(_a) || \ |
117 | (*_d >> _to_shift) != _a); \ | |
9b80e4c4 | 118 | })) |
0c668477 | 119 | |
610b15c5 | 120 | /** |
e1be43d9 | 121 | * size_mul() - Calculate size_t multiplication with saturation at SIZE_MAX |
610b15c5 | 122 | * |
e1be43d9 KC |
123 | * @factor1: first factor |
124 | * @factor2: second factor | |
610b15c5 | 125 | * |
e1be43d9 KC |
126 | * Returns: calculate @factor1 * @factor2, both promoted to size_t, |
127 | * with any overflow causing the return value to be SIZE_MAX. The | |
128 | * lvalue must be size_t to avoid implicit type conversion. | |
610b15c5 | 129 | */ |
e1be43d9 | 130 | static inline size_t __must_check size_mul(size_t factor1, size_t factor2) |
610b15c5 KC |
131 | { |
132 | size_t bytes; | |
133 | ||
e1be43d9 | 134 | if (check_mul_overflow(factor1, factor2, &bytes)) |
610b15c5 KC |
135 | return SIZE_MAX; |
136 | ||
137 | return bytes; | |
138 | } | |
139 | ||
140 | /** | |
e1be43d9 | 141 | * size_add() - Calculate size_t addition with saturation at SIZE_MAX |
610b15c5 | 142 | * |
e1be43d9 KC |
143 | * @addend1: first addend |
144 | * @addend2: second addend | |
610b15c5 | 145 | * |
e1be43d9 KC |
146 | * Returns: calculate @addend1 + @addend2, both promoted to size_t, |
147 | * with any overflow causing the return value to be SIZE_MAX. The | |
148 | * lvalue must be size_t to avoid implicit type conversion. | |
610b15c5 | 149 | */ |
e1be43d9 | 150 | static inline size_t __must_check size_add(size_t addend1, size_t addend2) |
610b15c5 KC |
151 | { |
152 | size_t bytes; | |
153 | ||
e1be43d9 | 154 | if (check_add_overflow(addend1, addend2, &bytes)) |
610b15c5 KC |
155 | return SIZE_MAX; |
156 | ||
157 | return bytes; | |
158 | } | |
159 | ||
e1be43d9 KC |
160 | /** |
161 | * size_sub() - Calculate size_t subtraction with saturation at SIZE_MAX | |
162 | * | |
163 | * @minuend: value to subtract from | |
164 | * @subtrahend: value to subtract from @minuend | |
165 | * | |
166 | * Returns: calculate @minuend - @subtrahend, both promoted to size_t, | |
167 | * with any overflow causing the return value to be SIZE_MAX. For | |
168 | * composition with the size_add() and size_mul() helpers, neither | |
169 | * argument may be SIZE_MAX (or the result with be forced to SIZE_MAX). | |
170 | * The lvalue must be size_t to avoid implicit type conversion. | |
e0478542 | 171 | */ |
e1be43d9 | 172 | static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend) |
610b15c5 KC |
173 | { |
174 | size_t bytes; | |
175 | ||
e1be43d9 KC |
176 | if (minuend == SIZE_MAX || subtrahend == SIZE_MAX || |
177 | check_sub_overflow(minuend, subtrahend, &bytes)) | |
610b15c5 KC |
178 | return SIZE_MAX; |
179 | ||
180 | return bytes; | |
181 | } | |
182 | ||
183 | /** | |
e1be43d9 | 184 | * array_size() - Calculate size of 2-dimensional array. |
610b15c5 | 185 | * |
e1be43d9 KC |
186 | * @a: dimension one |
187 | * @b: dimension two | |
610b15c5 | 188 | * |
e1be43d9 KC |
189 | * Calculates size of 2-dimensional array: @a * @b. |
190 | * | |
191 | * Returns: number of bytes needed to represent the array or SIZE_MAX on | |
192 | * overflow. | |
610b15c5 | 193 | */ |
e1be43d9 KC |
194 | #define array_size(a, b) size_mul(a, b) |
195 | ||
196 | /** | |
197 | * array3_size() - Calculate size of 3-dimensional array. | |
198 | * | |
199 | * @a: dimension one | |
200 | * @b: dimension two | |
201 | * @c: dimension three | |
202 | * | |
203 | * Calculates size of 3-dimensional array: @a * @b * @c. | |
204 | * | |
205 | * Returns: number of bytes needed to represent the array or SIZE_MAX on | |
206 | * overflow. | |
207 | */ | |
208 | #define array3_size(a, b, c) size_mul(size_mul(a, b), c) | |
610b15c5 | 209 | |
b19d57d0 GS |
210 | /** |
211 | * flex_array_size() - Calculate size of a flexible array member | |
212 | * within an enclosing structure. | |
213 | * | |
214 | * @p: Pointer to the structure. | |
215 | * @member: Name of the flexible array member. | |
216 | * @count: Number of elements in the array. | |
217 | * | |
218 | * Calculates size of a flexible array of @count number of @member | |
219 | * elements, at the end of structure @p. | |
220 | * | |
221 | * Return: number of bytes needed or SIZE_MAX on overflow. | |
222 | */ | |
223 | #define flex_array_size(p, member, count) \ | |
e1be43d9 KC |
224 | size_mul(count, \ |
225 | sizeof(*(p)->member) + __must_be_array((p)->member)) | |
226 | ||
227 | /** | |
228 | * struct_size() - Calculate size of structure with trailing flexible array. | |
229 | * | |
230 | * @p: Pointer to the structure. | |
231 | * @member: Name of the array member. | |
232 | * @count: Number of elements in the array. | |
233 | * | |
234 | * Calculates size of memory needed for structure @p followed by an | |
235 | * array of @count number of @member elements. | |
236 | * | |
237 | * Return: number of bytes needed or SIZE_MAX on overflow. | |
238 | */ | |
239 | #define struct_size(p, member, count) \ | |
240 | size_add(sizeof(*(p)), flex_array_size(p, member, count)) | |
b19d57d0 | 241 | |
f0907827 | 242 | #endif /* __LINUX_OVERFLOW_H */ |