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

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