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

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