dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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