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

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