xref: /aosp_15_r20/external/libbpf/include/linux/overflow.h (revision f7c14bbac8cf49633f2740db462ea43457973ec4)
1*f7c14bbaSAndroid Build Coastguard Worker /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
2*f7c14bbaSAndroid Build Coastguard Worker 
3*f7c14bbaSAndroid Build Coastguard Worker #ifndef __LINUX_OVERFLOW_H
4*f7c14bbaSAndroid Build Coastguard Worker #define __LINUX_OVERFLOW_H
5*f7c14bbaSAndroid Build Coastguard Worker 
6*f7c14bbaSAndroid Build Coastguard Worker #define is_signed_type(type)	(((type)(-1)) < (type)1)
7*f7c14bbaSAndroid Build Coastguard Worker #define __type_half_max(type) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type)))
8*f7c14bbaSAndroid Build Coastguard Worker #define type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T)))
9*f7c14bbaSAndroid Build Coastguard Worker #define type_min(T) ((T)((T)-type_max(T)-(T)1))
10*f7c14bbaSAndroid Build Coastguard Worker 
11*f7c14bbaSAndroid Build Coastguard Worker #ifndef unlikely
12*f7c14bbaSAndroid Build Coastguard Worker #define unlikely(x)	__builtin_expect(!!(x), 0)
13*f7c14bbaSAndroid Build Coastguard Worker #endif
14*f7c14bbaSAndroid Build Coastguard Worker 
15*f7c14bbaSAndroid Build Coastguard Worker #ifdef __GNUC__
16*f7c14bbaSAndroid Build Coastguard Worker #define GCC_VERSION (__GNUC__ * 10000           \
17*f7c14bbaSAndroid Build Coastguard Worker                      + __GNUC_MINOR__ * 100     \
18*f7c14bbaSAndroid Build Coastguard Worker                      + __GNUC_PATCHLEVEL__)
19*f7c14bbaSAndroid Build Coastguard Worker #if GCC_VERSION >= 50100
20*f7c14bbaSAndroid Build Coastguard Worker #define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1
21*f7c14bbaSAndroid Build Coastguard Worker #endif
22*f7c14bbaSAndroid Build Coastguard Worker #endif
23*f7c14bbaSAndroid Build Coastguard Worker 
24*f7c14bbaSAndroid Build Coastguard Worker #ifdef COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW
25*f7c14bbaSAndroid Build Coastguard Worker 
26*f7c14bbaSAndroid Build Coastguard Worker #define check_mul_overflow(a, b, d) ({		\
27*f7c14bbaSAndroid Build Coastguard Worker 	typeof(a) __a = (a);			\
28*f7c14bbaSAndroid Build Coastguard Worker 	typeof(b) __b = (b);			\
29*f7c14bbaSAndroid Build Coastguard Worker 	typeof(d) __d = (d);			\
30*f7c14bbaSAndroid Build Coastguard Worker 	(void) (&__a == &__b);			\
31*f7c14bbaSAndroid Build Coastguard Worker 	(void) (&__a == __d);			\
32*f7c14bbaSAndroid Build Coastguard Worker 	__builtin_mul_overflow(__a, __b, __d);	\
33*f7c14bbaSAndroid Build Coastguard Worker })
34*f7c14bbaSAndroid Build Coastguard Worker 
35*f7c14bbaSAndroid Build Coastguard Worker #else
36*f7c14bbaSAndroid Build Coastguard Worker 
37*f7c14bbaSAndroid Build Coastguard Worker /*
38*f7c14bbaSAndroid Build Coastguard Worker  * If one of a or b is a compile-time constant, this avoids a division.
39*f7c14bbaSAndroid Build Coastguard Worker  */
40*f7c14bbaSAndroid Build Coastguard Worker #define __unsigned_mul_overflow(a, b, d) ({		\
41*f7c14bbaSAndroid Build Coastguard Worker 	typeof(a) __a = (a);				\
42*f7c14bbaSAndroid Build Coastguard Worker 	typeof(b) __b = (b);				\
43*f7c14bbaSAndroid Build Coastguard Worker 	typeof(d) __d = (d);				\
44*f7c14bbaSAndroid Build Coastguard Worker 	(void) (&__a == &__b);				\
45*f7c14bbaSAndroid Build Coastguard Worker 	(void) (&__a == __d);				\
46*f7c14bbaSAndroid Build Coastguard Worker 	*__d = __a * __b;				\
47*f7c14bbaSAndroid Build Coastguard Worker 	__builtin_constant_p(__b) ?			\
48*f7c14bbaSAndroid Build Coastguard Worker 	  __b > 0 && __a > type_max(typeof(__a)) / __b : \
49*f7c14bbaSAndroid Build Coastguard Worker 	  __a > 0 && __b > type_max(typeof(__b)) / __a;	 \
50*f7c14bbaSAndroid Build Coastguard Worker })
51*f7c14bbaSAndroid Build Coastguard Worker 
52*f7c14bbaSAndroid Build Coastguard Worker /*
53*f7c14bbaSAndroid Build Coastguard Worker  * Signed multiplication is rather hard. gcc always follows C99, so
54*f7c14bbaSAndroid Build Coastguard Worker  * division is truncated towards 0. This means that we can write the
55*f7c14bbaSAndroid Build Coastguard Worker  * overflow check like this:
56*f7c14bbaSAndroid Build Coastguard Worker  *
57*f7c14bbaSAndroid Build Coastguard Worker  * (a > 0 && (b > MAX/a || b < MIN/a)) ||
58*f7c14bbaSAndroid Build Coastguard Worker  * (a < -1 && (b > MIN/a || b < MAX/a) ||
59*f7c14bbaSAndroid Build Coastguard Worker  * (a == -1 && b == MIN)
60*f7c14bbaSAndroid Build Coastguard Worker  *
61*f7c14bbaSAndroid Build Coastguard Worker  * The redundant casts of -1 are to silence an annoying -Wtype-limits
62*f7c14bbaSAndroid Build Coastguard Worker  * (included in -Wextra) warning: When the type is u8 or u16, the
63*f7c14bbaSAndroid Build Coastguard Worker  * __b_c_e in check_mul_overflow obviously selects
64*f7c14bbaSAndroid Build Coastguard Worker  * __unsigned_mul_overflow, but unfortunately gcc still parses this
65*f7c14bbaSAndroid Build Coastguard Worker  * code and warns about the limited range of __b.
66*f7c14bbaSAndroid Build Coastguard Worker  */
67*f7c14bbaSAndroid Build Coastguard Worker 
68*f7c14bbaSAndroid Build Coastguard Worker #define __signed_mul_overflow(a, b, d) ({				\
69*f7c14bbaSAndroid Build Coastguard Worker 	typeof(a) __a = (a);						\
70*f7c14bbaSAndroid Build Coastguard Worker 	typeof(b) __b = (b);						\
71*f7c14bbaSAndroid Build Coastguard Worker 	typeof(d) __d = (d);						\
72*f7c14bbaSAndroid Build Coastguard Worker 	typeof(a) __tmax = type_max(typeof(a));				\
73*f7c14bbaSAndroid Build Coastguard Worker 	typeof(a) __tmin = type_min(typeof(a));				\
74*f7c14bbaSAndroid Build Coastguard Worker 	(void) (&__a == &__b);						\
75*f7c14bbaSAndroid Build Coastguard Worker 	(void) (&__a == __d);						\
76*f7c14bbaSAndroid Build Coastguard Worker 	*__d = (__u64)__a * (__u64)__b;					\
77*f7c14bbaSAndroid Build Coastguard Worker 	(__b > 0   && (__a > __tmax/__b || __a < __tmin/__b)) ||	\
78*f7c14bbaSAndroid Build Coastguard Worker 	(__b < (typeof(__b))-1  && (__a > __tmin/__b || __a < __tmax/__b)) || \
79*f7c14bbaSAndroid Build Coastguard Worker 	(__b == (typeof(__b))-1 && __a == __tmin);			\
80*f7c14bbaSAndroid Build Coastguard Worker })
81*f7c14bbaSAndroid Build Coastguard Worker 
82*f7c14bbaSAndroid Build Coastguard Worker #define check_mul_overflow(a, b, d)					\
83*f7c14bbaSAndroid Build Coastguard Worker 	__builtin_choose_expr(is_signed_type(typeof(a)),		\
84*f7c14bbaSAndroid Build Coastguard Worker 			__signed_mul_overflow(a, b, d),			\
85*f7c14bbaSAndroid Build Coastguard Worker 			__unsigned_mul_overflow(a, b, d))
86*f7c14bbaSAndroid Build Coastguard Worker 
87*f7c14bbaSAndroid Build Coastguard Worker 
88*f7c14bbaSAndroid Build Coastguard Worker #endif /* COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW */
89*f7c14bbaSAndroid Build Coastguard Worker 
90*f7c14bbaSAndroid Build Coastguard Worker #endif
91