]> git.proxmox.com Git - ovs.git/blob - datapath/linux/compat/include/linux/overflow.h
datapath: meter: Use struct_size() in kzalloc()
[ovs.git] / datapath / linux / compat / include / linux / overflow.h
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 #ifdef HAVE_OVERFLOW_H
3 #include_next <linux/overflow.h>
4 #else
5 #ifndef __LINUX_OVERFLOW_H
6 #define __LINUX_OVERFLOW_H
7
8 #include <linux/compiler.h>
9
10 /*
11 * In the fallback code below, we need to compute the minimum and
12 * maximum values representable in a given type. These macros may also
13 * be useful elsewhere, so we provide them outside the
14 * COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW block.
15 *
16 * It would seem more obvious to do something like
17 *
18 * #define type_min(T) (T)(is_signed_type(T) ? (T)1 << (8*sizeof(T)-1) : 0)
19 * #define type_max(T) (T)(is_signed_type(T) ? ((T)1 << (8*sizeof(T)-1)) - 1 : ~(T)0)
20 *
21 * Unfortunately, the middle expressions, strictly speaking, have
22 * undefined behaviour, and at least some versions of gcc warn about
23 * the type_max expression (but not if -fsanitize=undefined is in
24 * effect; in that case, the warning is deferred to runtime...).
25 *
26 * The slightly excessive casting in type_min is to make sure the
27 * macros also produce sensible values for the exotic type _Bool. [The
28 * overflow checkers only almost work for _Bool, but that's
29 * a-feature-not-a-bug, since people shouldn't be doing arithmetic on
30 * _Bools. Besides, the gcc builtins don't allow _Bool* as third
31 * argument.]
32 *
33 * Idea stolen from
34 * https://mail-index.netbsd.org/tech-misc/2007/02/05/0000.html -
35 * credit to Christian Biere.
36 */
37 #define is_signed_type(type) (((type)(-1)) < (type)1)
38 #define __type_half_max(type) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type)))
39 #define type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T)))
40 #define type_min(T) ((T)((T)-type_max(T)-(T)1))
41
42
43 #ifdef COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW
44 /*
45 * For simplicity and code hygiene, the fallback code below insists on
46 * a, b and *d having the same type (similar to the min() and max()
47 * macros), whereas gcc's type-generic overflow checkers accept
48 * different types. Hence we don't just make check_add_overflow an
49 * alias for __builtin_add_overflow, but add type checks similar to
50 * below.
51 */
52 #define check_add_overflow(a, b, d) ({ \
53 typeof(a) __a = (a); \
54 typeof(b) __b = (b); \
55 typeof(d) __d = (d); \
56 (void) (&__a == &__b); \
57 (void) (&__a == __d); \
58 __builtin_add_overflow(__a, __b, __d); \
59 })
60
61 #define check_sub_overflow(a, b, d) ({ \
62 typeof(a) __a = (a); \
63 typeof(b) __b = (b); \
64 typeof(d) __d = (d); \
65 (void) (&__a == &__b); \
66 (void) (&__a == __d); \
67 __builtin_sub_overflow(__a, __b, __d); \
68 })
69
70 #define check_mul_overflow(a, b, d) ({ \
71 typeof(a) __a = (a); \
72 typeof(b) __b = (b); \
73 typeof(d) __d = (d); \
74 (void) (&__a == &__b); \
75 (void) (&__a == __d); \
76 __builtin_mul_overflow(__a, __b, __d); \
77 })
78
79 #else
80
81
82 /* Checking for unsigned overflow is relatively easy without causing UB. */
83 #define __unsigned_add_overflow(a, b, d) ({ \
84 typeof(a) __a = (a); \
85 typeof(b) __b = (b); \
86 typeof(d) __d = (d); \
87 (void) (&__a == &__b); \
88 (void) (&__a == __d); \
89 *__d = __a + __b; \
90 *__d < __a; \
91 })
92 #define __unsigned_sub_overflow(a, b, d) ({ \
93 typeof(a) __a = (a); \
94 typeof(b) __b = (b); \
95 typeof(d) __d = (d); \
96 (void) (&__a == &__b); \
97 (void) (&__a == __d); \
98 *__d = __a - __b; \
99 __a < __b; \
100 })
101 /*
102 * If one of a or b is a compile-time constant, this avoids a division.
103 */
104 #define __unsigned_mul_overflow(a, b, d) ({ \
105 typeof(a) __a = (a); \
106 typeof(b) __b = (b); \
107 typeof(d) __d = (d); \
108 (void) (&__a == &__b); \
109 (void) (&__a == __d); \
110 *__d = __a * __b; \
111 __builtin_constant_p(__b) ? \
112 __b > 0 && __a > type_max(typeof(__a)) / __b : \
113 __a > 0 && __b > type_max(typeof(__b)) / __a; \
114 })
115
116 /*
117 * For signed types, detecting overflow is much harder, especially if
118 * we want to avoid UB. But the interface of these macros is such that
119 * we must provide a result in *d, and in fact we must produce the
120 * result promised by gcc's builtins, which is simply the possibly
121 * wrapped-around value. Fortunately, we can just formally do the
122 * operations in the widest relevant unsigned type (u64) and then
123 * truncate the result - gcc is smart enough to generate the same code
124 * with and without the (u64) casts.
125 */
126
127 /*
128 * Adding two signed integers can overflow only if they have the same
129 * sign, and overflow has happened iff the result has the opposite
130 * sign.
131 */
132 #define __signed_add_overflow(a, b, d) ({ \
133 typeof(a) __a = (a); \
134 typeof(b) __b = (b); \
135 typeof(d) __d = (d); \
136 (void) (&__a == &__b); \
137 (void) (&__a == __d); \
138 *__d = (u64)__a + (u64)__b; \
139 (((~(__a ^ __b)) & (*__d ^ __a)) \
140 & type_min(typeof(__a))) != 0; \
141 })
142
143 /*
144 * Subtraction is similar, except that overflow can now happen only
145 * when the signs are opposite. In this case, overflow has happened if
146 * the result has the opposite sign of a.
147 */
148 #define __signed_sub_overflow(a, b, d) ({ \
149 typeof(a) __a = (a); \
150 typeof(b) __b = (b); \
151 typeof(d) __d = (d); \
152 (void) (&__a == &__b); \
153 (void) (&__a == __d); \
154 *__d = (u64)__a - (u64)__b; \
155 ((((__a ^ __b)) & (*__d ^ __a)) \
156 & type_min(typeof(__a))) != 0; \
157 })
158
159 /*
160 * Signed multiplication is rather hard. gcc always follows C99, so
161 * division is truncated towards 0. This means that we can write the
162 * overflow check like this:
163 *
164 * (a > 0 && (b > MAX/a || b < MIN/a)) ||
165 * (a < -1 && (b > MIN/a || b < MAX/a) ||
166 * (a == -1 && b == MIN)
167 *
168 * The redundant casts of -1 are to silence an annoying -Wtype-limits
169 * (included in -Wextra) warning: When the type is u8 or u16, the
170 * __b_c_e in check_mul_overflow obviously selects
171 * __unsigned_mul_overflow, but unfortunately gcc still parses this
172 * code and warns about the limited range of __b.
173 */
174
175 #define __signed_mul_overflow(a, b, d) ({ \
176 typeof(a) __a = (a); \
177 typeof(b) __b = (b); \
178 typeof(d) __d = (d); \
179 typeof(a) __tmax = type_max(typeof(a)); \
180 typeof(a) __tmin = type_min(typeof(a)); \
181 (void) (&__a == &__b); \
182 (void) (&__a == __d); \
183 *__d = (u64)__a * (u64)__b; \
184 (__b > 0 && (__a > __tmax/__b || __a < __tmin/__b)) || \
185 (__b < (typeof(__b))-1 && (__a > __tmin/__b || __a < __tmax/__b)) || \
186 (__b == (typeof(__b))-1 && __a == __tmin); \
187 })
188
189
190 #define check_add_overflow(a, b, d) \
191 __builtin_choose_expr(is_signed_type(typeof(a)), \
192 __signed_add_overflow(a, b, d), \
193 __unsigned_add_overflow(a, b, d))
194
195 #define check_sub_overflow(a, b, d) \
196 __builtin_choose_expr(is_signed_type(typeof(a)), \
197 __signed_sub_overflow(a, b, d), \
198 __unsigned_sub_overflow(a, b, d))
199
200 #define check_mul_overflow(a, b, d) \
201 __builtin_choose_expr(is_signed_type(typeof(a)), \
202 __signed_mul_overflow(a, b, d), \
203 __unsigned_mul_overflow(a, b, d))
204
205
206 #endif /* COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW */
207
208 /** check_shl_overflow() - Calculate a left-shifted value and check overflow
209 *
210 * @a: Value to be shifted
211 * @s: How many bits left to shift
212 * @d: Pointer to where to store the result
213 *
214 * Computes *@d = (@a << @s)
215 *
216 * Returns true if '*d' cannot hold the result or when 'a << s' doesn't
217 * make sense. Example conditions:
218 * - 'a << s' causes bits to be lost when stored in *d.
219 * - 's' is garbage (e.g. negative) or so large that the result of
220 * 'a << s' is guaranteed to be 0.
221 * - 'a' is negative.
222 * - 'a << s' sets the sign bit, if any, in '*d'.
223 *
224 * '*d' will hold the results of the attempted shift, but is not
225 * considered "safe for use" if false is returned.
226 */
227 #define check_shl_overflow(a, s, d) ({ \
228 typeof(a) _a = a; \
229 typeof(s) _s = s; \
230 typeof(d) _d = d; \
231 u64 _a_full = _a; \
232 unsigned int _to_shift = \
233 _s >= 0 && _s < 8 * sizeof(*d) ? _s : 0; \
234 *_d = (_a_full << _to_shift); \
235 (_to_shift != _s || *_d < 0 || _a < 0 || \
236 (*_d >> _to_shift) != _a); \
237 })
238
239 /**
240 * array_size() - Calculate size of 2-dimensional array.
241 *
242 * @a: dimension one
243 * @b: dimension two
244 *
245 * Calculates size of 2-dimensional array: @a * @b.
246 *
247 * Returns: number of bytes needed to represent the array or SIZE_MAX on
248 * overflow.
249 */
250 static inline __must_check size_t array_size(size_t a, size_t b)
251 {
252 size_t bytes;
253
254 if (check_mul_overflow(a, b, &bytes))
255 return SIZE_MAX;
256
257 return bytes;
258 }
259
260 /**
261 * array3_size() - Calculate size of 3-dimensional array.
262 *
263 * @a: dimension one
264 * @b: dimension two
265 * @c: dimension three
266 *
267 * Calculates size of 3-dimensional array: @a * @b * @c.
268 *
269 * Returns: number of bytes needed to represent the array or SIZE_MAX on
270 * overflow.
271 */
272 static inline __must_check size_t array3_size(size_t a, size_t b, size_t c)
273 {
274 size_t bytes;
275
276 if (check_mul_overflow(a, b, &bytes))
277 return SIZE_MAX;
278 if (check_mul_overflow(bytes, c, &bytes))
279 return SIZE_MAX;
280
281 return bytes;
282 }
283
284 static inline __must_check size_t __ab_c_size(size_t n, size_t size, size_t c)
285 {
286 size_t bytes;
287
288 if (check_mul_overflow(n, size, &bytes))
289 return SIZE_MAX;
290 if (check_add_overflow(bytes, c, &bytes))
291 return SIZE_MAX;
292
293 return bytes;
294 }
295
296 /**
297 * struct_size() - Calculate size of structure with trailing array.
298 * @p: Pointer to the structure.
299 * @member: Name of the array member.
300 * @n: Number of elements in the array.
301 *
302 * Calculates size of memory needed for structure @p followed by an
303 * array of @n @member elements.
304 *
305 * Return: number of bytes needed or SIZE_MAX on overflow.
306 */
307 #define struct_size(p, member, n) \
308 __ab_c_size(n, \
309 sizeof(*(p)->member) + __must_be_array((p)->member),\
310 sizeof(*(p)))
311
312 #endif /* __LINUX_OVERFLOW_H */
313 #endif /* HAVE_OVERFLOW_H */