Blame SOURCES/0282-safemath-Add-some-arithmetic-primitives-that-check-f.patch

80913e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
80913e
From: Peter Jones <pjones@redhat.com>
80913e
Date: Mon, 15 Jun 2020 10:58:42 -0400
80913e
Subject: [PATCH] safemath: Add some arithmetic primitives that check for
80913e
 overflow
80913e
80913e
This adds a new header, include/grub/safemath.h, that includes easy to
80913e
use wrappers for __builtin_{add,sub,mul}_overflow() declared like:
80913e
80913e
  bool OP(a, b, res)
80913e
80913e
where OP is grub_add, grub_sub or grub_mul. OP() returns true in the
80913e
case where the operation would overflow and res is not modified.
80913e
Otherwise, false is returned and the operation is executed.
80913e
80913e
These arithmetic primitives require newer compiler versions. So, bump
80913e
these requirements in the INSTALL file too.
80913e
80913e
Signed-off-by: Peter Jones <pjones@redhat.com>
80913e
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
80913e
Upstream-commit-id: de1c315841a
80913e
---
80913e
 include/grub/compiler.h |  8 ++++++++
80913e
 include/grub/safemath.h | 37 +++++++++++++++++++++++++++++++++++++
80913e
 INSTALL                 | 22 ++--------------------
80913e
 3 files changed, 47 insertions(+), 20 deletions(-)
80913e
 create mode 100644 include/grub/safemath.h
80913e
80913e
diff --git a/include/grub/compiler.h b/include/grub/compiler.h
80913e
index 9859ff4cc79..ebafec68957 100644
80913e
--- a/include/grub/compiler.h
80913e
+++ b/include/grub/compiler.h
80913e
@@ -48,6 +48,14 @@
80913e
 #  define WARN_UNUSED_RESULT
80913e
 #endif
80913e
 
80913e
+#if defined(__clang__) && defined(__clang_major__) && defined(__clang_minor__)
80913e
+#  define CLANG_PREREQ(maj,min) \
80913e
+          ((__clang_major__ > (maj)) || \
80913e
+	   (__clang_major__ == (maj) && __clang_minor__ >= (min)))
80913e
+#else
80913e
+#  define CLANG_PREREQ(maj,min) 0
80913e
+#endif
80913e
+
80913e
 #define UNUSED __attribute__((__unused__))
80913e
 
80913e
 #endif /* ! GRUB_COMPILER_HEADER */
80913e
diff --git a/include/grub/safemath.h b/include/grub/safemath.h
80913e
new file mode 100644
80913e
index 00000000000..c17b89bba17
80913e
--- /dev/null
80913e
+++ b/include/grub/safemath.h
80913e
@@ -0,0 +1,37 @@
80913e
+/*
80913e
+ *  GRUB  --  GRand Unified Bootloader
80913e
+ *  Copyright (C) 2020  Free Software Foundation, Inc.
80913e
+ *
80913e
+ *  GRUB is free software: you can redistribute it and/or modify
80913e
+ *  it under the terms of the GNU General Public License as published by
80913e
+ *  the Free Software Foundation, either version 3 of the License, or
80913e
+ *  (at your option) any later version.
80913e
+ *
80913e
+ *  GRUB is distributed in the hope that it will be useful,
80913e
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
80913e
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
80913e
+ *  GNU General Public License for more details.
80913e
+ *
80913e
+ *  You should have received a copy of the GNU General Public License
80913e
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
80913e
+ *
80913e
+ *  Arithmetic operations that protect against overflow.
80913e
+ */
80913e
+
80913e
+#ifndef GRUB_SAFEMATH_H
80913e
+#define GRUB_SAFEMATH_H 1
80913e
+
80913e
+#include <grub/compiler.h>
80913e
+
80913e
+/* These appear in gcc 5.1 and clang 3.8. */
80913e
+#if GNUC_PREREQ(5, 1) || CLANG_PREREQ(3, 8)
80913e
+
80913e
+#define grub_add(a, b, res)	__builtin_add_overflow(a, b, res)
80913e
+#define grub_sub(a, b, res)	__builtin_sub_overflow(a, b, res)
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
+#endif
80913e
+
80913e
+#endif /* GRUB_SAFEMATH_H */
80913e
diff --git a/INSTALL b/INSTALL
80913e
index f3c20edc844..f8bd9116480 100644
80913e
--- a/INSTALL
80913e
+++ b/INSTALL
80913e
@@ -11,27 +11,9 @@ GRUB depends on some software packages installed into your system. If
80913e
 you don't have any of them, please obtain and install them before
80913e
 configuring the GRUB.
80913e
 
80913e
-* GCC 4.1.3 or later
80913e
-  Note: older versions may work but support is limited
80913e
-
80913e
-  Experimental support for clang 3.3 or later (results in much bigger binaries)
80913e
+* GCC 5.1.0 or later
80913e
+  Experimental support for clang 3.8.0 or later (results in much bigger binaries)
80913e
   for i386, x86_64, arm (including thumb), arm64, mips(el), powerpc, sparc64
80913e
-  Note: clang 3.2 or later works for i386 and x86_64 targets but results in
80913e
-        much bigger binaries.
80913e
-	earlier versions not tested
80913e
-  Note: clang 3.2 or later works for arm
80913e
-	earlier versions not tested
80913e
-  Note: clang on arm64 is not supported due to
80913e
-	https://llvm.org/bugs/show_bug.cgi?id=26030
80913e
-  Note: clang 3.3 or later works for mips(el)
80913e
-	earlier versions fail to generate .reginfo and hence gprel relocations
80913e
-	fail.
80913e
-  Note: clang 3.2 or later works for powerpc
80913e
-	earlier versions not tested
80913e
-  Note: clang 3.5 or later works for sparc64
80913e
-        earlier versions return "error: unable to interface with target machine"
80913e
-  Note: clang has no support for ia64 and hence you can't compile GRUB
80913e
-	for ia64 with clang
80913e
 * GNU Make
80913e
 * GNU Bison 2.3 or later
80913e
 * GNU gettext 0.17 or later