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