Blame SOURCES/0305-update-safemath-with-fallback-code-for-gcc-older-tha.patch

80913e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
80913e
From: Alexander Burmashev <alexander.burmashev@oracle.com>
80913e
Date: Wed, 22 Jul 2020 06:04:38 -0700
80913e
Subject: [PATCH] update safemath with fallback code for gcc older than 5.1
80913e
80913e
The code used in the header was taken from linux kernel commit
80913e
f0907827a8a9152aedac2833ed1b674a7b2a44f2.  Rasmus Villemoes
80913e
<linux@rasmusvillemoes.dk>, the original author of the patch, was
80913e
contacted directly, confirmed his authorship of the code, and gave his
80913e
permission on treating that dual license as MIT and including into GRUB2
80913e
sources
80913e
80913e
Signed-off-by: Alex Burmashev <alexander.burmashev@oracle.com>
80913e
---
80913e
 include/grub/safemath.h | 119 +++++++++++++++++++++++++++++++++++++++++++++++-
80913e
 1 file changed, 118 insertions(+), 1 deletion(-)
80913e
80913e
diff --git a/include/grub/safemath.h b/include/grub/safemath.h
b32e65
index c17b89bba..1ccac276b 100644
80913e
--- a/include/grub/safemath.h
80913e
+++ b/include/grub/safemath.h
80913e
@@ -31,7 +31,124 @@
80913e
 #define grub_mul(a, b, res)	__builtin_mul_overflow(a, b, res)
80913e
 
80913e
 #else
80913e
-#error gcc 5.1 or newer or clang 3.8 or newer is required
80913e
+/*
80913e
+ * Copyright 2020 Rasmus Villemoes
80913e
+ *
80913e
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
80913e
+ * of this software and associated documentation files (the "Software"), to
80913e
+ * deal in the Software without restriction, including without limitation the
80913e
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
80913e
+ * sell copies of the Software, and to permit persons to whom the Software is
80913e
+ * furnished to do so, subject to the following conditions:
80913e
+ *
80913e
+ * The above copyright notice and this permission notice shall be included in
80913e
+ * all copies or substantial portions of the Software.
80913e
+
80913e
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
80913e
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
80913e
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
80913e
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
80913e
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
80913e
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
80913e
+ * IN THE SOFTWARE.
80913e
+ */
80913e
+/*
80913e
+ * The code used in this header was taken from linux kernel commit
80913e
+ * f0907827a8a9152aedac2833ed1b674a7b2a44f2
80913e
+ * Rasmus Villemoes <linux@rasmusvillemoes.dk>, the original author of the
80913e
+ * patch, was contacted directly, confirmed his authorship of the code, and
80913e
+ * gave his permission on treating that dual license as MIT and including into
80913e
+ * GRUB2 sources
80913e
+ */
80913e
+
80913e
+#include <grub/types.h>
80913e
+#define is_signed_type(type)	(((type)(-1)) < (type)1)
80913e
+#define __type_half_max(type)	((type)1 << (8*sizeof(type) - 1 - is_signed_type(type)))
80913e
+#define type_max(T)		((T)((__type_half_max(T) - 1) + __type_half_max(T)))
80913e
+#define type_min(T)		((T)((T)-type_max(T)-(T)1))
80913e
+
80913e
+#define __unsigned_add_overflow(a, b, d) ({	\
80913e
+	typeof(+(a)) __a = (a);			\
80913e
+	typeof(+(b)) __b = (b);			\
80913e
+	typeof(d) __d = (d);			\
80913e
+	(void) (&__a == &__b);			\
80913e
+	(void) (&__a == __d);			\
80913e
+	*__d = __a + __b;			\
80913e
+	*__d < __a;				\
80913e
+})
80913e
+#define __unsigned_sub_overflow(a, b, d) ({     \
80913e
+	typeof(+(a)) __a = (a);			\
80913e
+	typeof(+(b)) __b = (b);			\
80913e
+	typeof(d) __d = (d);			\
80913e
+	(void) (&__a == &__b);			\
80913e
+	(void) (&__a == __d);			\
80913e
+	*__d = __a - __b;			\
80913e
+	__a < __b;				\
80913e
+})
80913e
+#define __unsigned_mul_overflow(a, b, d) ({		\
80913e
+	typeof(+(a)) __a = (a);				\
80913e
+	typeof(+(b)) __b = (b);				\
80913e
+	typeof(d) __d = (d);				\
80913e
+	(void) (&__a == &__b);				\
80913e
+	(void) (&__a == __d);				\
80913e
+	*__d = __a * __b;				\
80913e
+	__builtin_constant_p(__b) ?			\
80913e
+	  __b > 0 && __a > type_max(typeof(__a)) / __b :\
80913e
+	  __a > 0 && __b > type_max(typeof(__b)) / __a; \
80913e
+})
80913e
+
80913e
+#define __signed_add_overflow(a, b, d) ({		\
80913e
+	typeof(+(a)) __a = (a);				\
80913e
+	typeof(+(b)) __b = (b);				\
80913e
+	typeof(d) __d = (d);				\
80913e
+	(void) (&__a == &__b);				\
80913e
+	(void) (&__a == __d);				\
80913e
+	*__d = (grub_uint64_t)__a + (grub_uint64_t)__b;	\
80913e
+	(((~(__a ^ __b)) & (*__d ^ __a))		\
80913e
+		& type_min(typeof(__a))) != 0;		\
80913e
+})
80913e
+
80913e
+#define __signed_sub_overflow(a, b, d) ({		\
80913e
+	typeof(+(a)) __a = (a);				\
80913e
+	typeof(+(b)) __b = (b);				\
80913e
+	typeof(d) __d = (d);				\
80913e
+	(void) (&__a == &__b);				\
80913e
+	(void) (&__a == __d);				\
80913e
+	*__d = (grub_uint64_t)__a - (grub_uint64_t)__b;	\
80913e
+	((((__a ^ __b)) & (*__d ^ __a))			\
80913e
+		& type_min(typeof(__a))) != 0;		\
80913e
+})
80913e
+
80913e
+#define __signed_mul_overflow(a, b, d) ({			\
80913e
+	typeof(+(a)) __a = (a);					\
80913e
+	typeof(+(b)) __b = (b);					\
80913e
+	typeof(d) __d = (d);					\
80913e
+	typeof(+(a)) __tmax = type_max(typeof(+(a)));		\
80913e
+	typeof(+(a)) __tmin = type_min(typeof(+(a)));		\
80913e
+	(void) (&__a == &__b);					\
80913e
+	(void) (&__a == __d);					\
80913e
+	*__d = (grub_uint64_t)__a * (grub_uint64_t)__b;		\
80913e
+	(__b > 0   && (__a > __tmax/__b || __a < __tmin/__b)) ||\
80913e
+	(__b < (typeof(__b))-1  &&				\
80913e
+	 (__a > __tmin/__b || __a < __tmax/__b)) ||		\
80913e
+	(__b == (typeof(__b))-1 && __a == __tmin);		\
80913e
+})
80913e
+
80913e
+#define grub_add(a, b, d)					\
80913e
+	__builtin_choose_expr(is_signed_type(typeof(+(a))),	\
80913e
+			__signed_add_overflow(a, b, d),		\
80913e
+			__unsigned_add_overflow(a, b, d))
80913e
+
80913e
+#define grub_sub(a, b, d)					\
80913e
+	__builtin_choose_expr(is_signed_type(typeof(+(a))),	\
80913e
+			__signed_sub_overflow(a, b, d),		\
80913e
+			__unsigned_sub_overflow(a, b, d))
80913e
+
80913e
+#define grub_mul(a, b, d)					\
80913e
+	__builtin_choose_expr(is_signed_type(typeof(+(a))),	\
80913e
+			__signed_mul_overflow(a, b, d),		\
80913e
+			__unsigned_mul_overflow(a, b, d))
80913e
+
80913e
 #endif
80913e
 
80913e
 #endif /* GRUB_SAFEMATH_H */