dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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