Blame SOURCES/0140-util-size.h-fix-build-for-older-compilers.patch

26ccd9
From 10653a171bc0ca145236d2c75e5c5422caeb8b55 Mon Sep 17 00:00:00 2001
26ccd9
From: Vishal Verma <vishal.l.verma@intel.com>
26ccd9
Date: Wed, 23 Feb 2022 22:28:05 -0700
26ccd9
Subject: [PATCH 140/217] util/size.h: fix build for older compilers
26ccd9
26ccd9
Add a fallback for older compilers that lack __builtin_add_overflow()
26ccd9
and friends. Commit 7aa7c7be6e80 ("util: add the struct_size() helper from the
26ccd9
kernel") which added these helpers from the kernel neglected to copy
26ccd9
over the fallback code.
26ccd9
26ccd9
Link: https://lore.kernel.org/r/20220224052805.2462449-1-vishal.l.verma@intel.com
26ccd9
Fixes: 7aa7c7be6e80 ("util: add the struct_size() helper from the kernel")
26ccd9
Reported-by: Joao Martins <joao.m.martins@oracle.com>
26ccd9
Reviewed-by: Joao Martins <joao.m.martins@oracle.com>
26ccd9
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
26ccd9
---
26ccd9
 util/size.h | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++--
26ccd9
 1 file changed, 159 insertions(+), 4 deletions(-)
26ccd9
26ccd9
diff --git a/util/size.h b/util/size.h
26ccd9
index e72467f..1cb0669 100644
26ccd9
--- a/util/size.h
26ccd9
+++ b/util/size.h
26ccd9
@@ -6,6 +6,7 @@
26ccd9
 #include <stdbool.h>
26ccd9
 #include <stdint.h>
26ccd9
 #include <util/util.h>
26ccd9
+#include <ccan/short_types/short_types.h>
26ccd9
 
26ccd9
 #define SZ_1K     0x00000400
26ccd9
 #define SZ_4K     0x00001000
26ccd9
@@ -43,23 +44,177 @@ static inline bool is_power_of_2(unsigned long long v)
26ccd9
  * alias for __builtin_add_overflow, but add type checks similar to
26ccd9
  * below.
26ccd9
  */
26ccd9
-#define check_add_overflow(a, b, d) (({	\
26ccd9
+#define is_signed_type(type)       (((type)(-1)) < (type)1)
26ccd9
+#define __type_half_max(type) ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type)))
26ccd9
+#define type_max(T) ((T)((__type_half_max(T) - 1) + __type_half_max(T)))
26ccd9
+#define type_min(T) ((T)((T)-type_max(T)-(T)1))
26ccd9
+
26ccd9
+#if GCC_VERSION >= 50100
26ccd9
+#define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1
26ccd9
+#endif
26ccd9
+
26ccd9
+#if __clang__ && \
26ccd9
+    __has_builtin(__builtin_mul_overflow) && \
26ccd9
+    __has_builtin(__builtin_add_overflow)
26ccd9
+#define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1
26ccd9
+#endif
26ccd9
+
26ccd9
+#if COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW
26ccd9
+
26ccd9
+#define check_add_overflow(a, b, d) ({		\
26ccd9
 	typeof(a) __a = (a);			\
26ccd9
 	typeof(b) __b = (b);			\
26ccd9
 	typeof(d) __d = (d);			\
26ccd9
 	(void) (&__a == &__b);			\
26ccd9
 	(void) (&__a == __d);			\
26ccd9
 	__builtin_add_overflow(__a, __b, __d);	\
26ccd9
-}))
26ccd9
+})
26ccd9
+
26ccd9
+#define check_sub_overflow(a, b, d) ({		\
26ccd9
+	typeof(a) __a = (a);			\
26ccd9
+	typeof(b) __b = (b);			\
26ccd9
+	typeof(d) __d = (d);			\
26ccd9
+	(void) (&__a == &__b);			\
26ccd9
+	(void) (&__a == __d);			\
26ccd9
+	__builtin_sub_overflow(__a, __b, __d);	\
26ccd9
+})
26ccd9
 
26ccd9
-#define check_mul_overflow(a, b, d) (({	\
26ccd9
+#define check_mul_overflow(a, b, d) ({		\
26ccd9
 	typeof(a) __a = (a);			\
26ccd9
 	typeof(b) __b = (b);			\
26ccd9
 	typeof(d) __d = (d);			\
26ccd9
 	(void) (&__a == &__b);			\
26ccd9
 	(void) (&__a == __d);			\
26ccd9
 	__builtin_mul_overflow(__a, __b, __d);	\
26ccd9
-}))
26ccd9
+})
26ccd9
+
26ccd9
+
26ccd9
+#else /* !COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW */
26ccd9
+
26ccd9
+/* Checking for unsigned overflow is relatively easy without causing UB. */
26ccd9
+#define __unsigned_add_overflow(a, b, d) ({	\
26ccd9
+	typeof(a) __a = (a);			\
26ccd9
+	typeof(b) __b = (b);			\
26ccd9
+	typeof(d) __d = (d);			\
26ccd9
+	(void) (&__a == &__b);			\
26ccd9
+	(void) (&__a == __d);			\
26ccd9
+	*__d = __a + __b;			\
26ccd9
+	*__d < __a;				\
26ccd9
+})
26ccd9
+#define __unsigned_sub_overflow(a, b, d) ({	\
26ccd9
+	typeof(a) __a = (a);			\
26ccd9
+	typeof(b) __b = (b);			\
26ccd9
+	typeof(d) __d = (d);			\
26ccd9
+	(void) (&__a == &__b);			\
26ccd9
+	(void) (&__a == __d);			\
26ccd9
+	*__d = __a - __b;			\
26ccd9
+	__a < __b;				\
26ccd9
+})
26ccd9
+/*
26ccd9
+ * If one of a or b is a compile-time constant, this avoids a division.
26ccd9
+ */
26ccd9
+#define __unsigned_mul_overflow(a, b, d) ({		\
26ccd9
+	typeof(a) __a = (a);				\
26ccd9
+	typeof(b) __b = (b);				\
26ccd9
+	typeof(d) __d = (d);				\
26ccd9
+	(void) (&__a == &__b);				\
26ccd9
+	(void) (&__a == __d);				\
26ccd9
+	*__d = __a * __b;				\
26ccd9
+	__builtin_constant_p(__b) ?			\
26ccd9
+	  __b > 0 && __a > type_max(typeof(__a)) / __b : \
26ccd9
+	  __a > 0 && __b > type_max(typeof(__b)) / __a;	 \
26ccd9
+})
26ccd9
+
26ccd9
+/*
26ccd9
+ * For signed types, detecting overflow is much harder, especially if
26ccd9
+ * we want to avoid UB. But the interface of these macros is such that
26ccd9
+ * we must provide a result in *d, and in fact we must produce the
26ccd9
+ * result promised by gcc's builtins, which is simply the possibly
26ccd9
+ * wrapped-around value. Fortunately, we can just formally do the
26ccd9
+ * operations in the widest relevant unsigned type (u64) and then
26ccd9
+ * truncate the result - gcc is smart enough to generate the same code
26ccd9
+ * with and without the (u64) casts.
26ccd9
+ */
26ccd9
+
26ccd9
+/*
26ccd9
+ * Adding two signed integers can overflow only if they have the same
26ccd9
+ * sign, and overflow has happened iff the result has the opposite
26ccd9
+ * sign.
26ccd9
+ */
26ccd9
+#define __signed_add_overflow(a, b, d) ({	\
26ccd9
+	typeof(a) __a = (a);			\
26ccd9
+	typeof(b) __b = (b);			\
26ccd9
+	typeof(d) __d = (d);			\
26ccd9
+	(void) (&__a == &__b);			\
26ccd9
+	(void) (&__a == __d);			\
26ccd9
+	*__d = (u64)__a + (u64)__b;		\
26ccd9
+	(((~(__a ^ __b)) & (*__d ^ __a))	\
26ccd9
+		& type_min(typeof(__a))) != 0;	\
26ccd9
+})
26ccd9
+
26ccd9
+/*
26ccd9
+ * Subtraction is similar, except that overflow can now happen only
26ccd9
+ * when the signs are opposite. In this case, overflow has happened if
26ccd9
+ * the result has the opposite sign of a.
26ccd9
+ */
26ccd9
+#define __signed_sub_overflow(a, b, d) ({	\
26ccd9
+	typeof(a) __a = (a);			\
26ccd9
+	typeof(b) __b = (b);			\
26ccd9
+	typeof(d) __d = (d);			\
26ccd9
+	(void) (&__a == &__b);			\
26ccd9
+	(void) (&__a == __d);			\
26ccd9
+	*__d = (u64)__a - (u64)__b;		\
26ccd9
+	((((__a ^ __b)) & (*__d ^ __a))		\
26ccd9
+		& type_min(typeof(__a))) != 0;	\
26ccd9
+})
26ccd9
+
26ccd9
+/*
26ccd9
+ * Signed multiplication is rather hard. gcc always follows C99, so
26ccd9
+ * division is truncated towards 0. This means that we can write the
26ccd9
+ * overflow check like this:
26ccd9
+ *
26ccd9
+ * (a > 0 && (b > MAX/a || b < MIN/a)) ||
26ccd9
+ * (a < -1 && (b > MIN/a || b < MAX/a) ||
26ccd9
+ * (a == -1 && b == MIN)
26ccd9
+ *
26ccd9
+ * The redundant casts of -1 are to silence an annoying -Wtype-limits
26ccd9
+ * (included in -Wextra) warning: When the type is u8 or u16, the
26ccd9
+ * __b_c_e in check_mul_overflow obviously selects
26ccd9
+ * __unsigned_mul_overflow, but unfortunately gcc still parses this
26ccd9
+ * code and warns about the limited range of __b.
26ccd9
+ */
26ccd9
+
26ccd9
+#define __signed_mul_overflow(a, b, d) ({				\
26ccd9
+	typeof(a) __a = (a);						\
26ccd9
+	typeof(b) __b = (b);						\
26ccd9
+	typeof(d) __d = (d);						\
26ccd9
+	typeof(a) __tmax = type_max(typeof(a));				\
26ccd9
+	typeof(a) __tmin = type_min(typeof(a));				\
26ccd9
+	(void) (&__a == &__b);						\
26ccd9
+	(void) (&__a == __d);						\
26ccd9
+	*__d = (u64)__a * (u64)__b;					\
26ccd9
+	(__b > 0   && (__a > __tmax/__b || __a < __tmin/__b)) ||	\
26ccd9
+	(__b < (typeof(__b))-1  && (__a > __tmin/__b || __a < __tmax/__b)) || \
26ccd9
+	(__b == (typeof(__b))-1 && __a == __tmin);			\
26ccd9
+})
26ccd9
+
26ccd9
+
26ccd9
+#define check_add_overflow(a, b, d)					\
26ccd9
+	__builtin_choose_expr(is_signed_type(typeof(a)),		\
26ccd9
+			__signed_add_overflow(a, b, d),			\
26ccd9
+			__unsigned_add_overflow(a, b, d))
26ccd9
+
26ccd9
+#define check_sub_overflow(a, b, d)					\
26ccd9
+	__builtin_choose_expr(is_signed_type(typeof(a)),		\
26ccd9
+			__signed_sub_overflow(a, b, d),			\
26ccd9
+			__unsigned_sub_overflow(a, b, d))
26ccd9
+
26ccd9
+#define check_mul_overflow(a, b, d)					\
26ccd9
+	__builtin_choose_expr(is_signed_type(typeof(a)),		\
26ccd9
+			__signed_mul_overflow(a, b, d),			\
26ccd9
+			__unsigned_mul_overflow(a, b, d))
26ccd9
+
26ccd9
+#endif
26ccd9
 
26ccd9
 /*
26ccd9
  * Compute a*b+c, returning SIZE_MAX on overflow. Internal helper for
26ccd9
-- 
26ccd9
2.27.0
26ccd9