0003-safemath-Add-some-arithmetic-primitives-that-check-f.patch 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. From 782a4580a5e347793443aa8e9152db1bf4a0fff8 Mon Sep 17 00:00:00 2001
  2. From: Peter Jones <pjones@redhat.com>
  3. Date: Mon, 15 Jun 2020 10:58:42 -0400
  4. Subject: [PATCH] safemath: Add some arithmetic primitives that check for
  5. overflow
  6. MIME-Version: 1.0
  7. Content-Type: text/plain; charset=UTF-8
  8. Content-Transfer-Encoding: 8bit
  9. This adds a new header, include/grub/safemath.h, that includes easy to
  10. use wrappers for __builtin_{add,sub,mul}_overflow() declared like:
  11. bool OP(a, b, res)
  12. where OP is grub_add, grub_sub or grub_mul. OP() returns true in the
  13. case where the operation would overflow and res is not modified.
  14. Otherwise, false is returned and the operation is executed.
  15. These arithmetic primitives require newer compiler versions. So, bump
  16. these requirements in the INSTALL file too.
  17. Signed-off-by: Peter Jones <pjones@redhat.com>
  18. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  19. Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
  20. ---
  21. INSTALL | 22 ++--------------------
  22. include/grub/compiler.h | 8 ++++++++
  23. include/grub/safemath.h | 37 +++++++++++++++++++++++++++++++++++++
  24. 3 files changed, 47 insertions(+), 20 deletions(-)
  25. create mode 100644 include/grub/safemath.h
  26. diff --git a/INSTALL b/INSTALL
  27. index 8acb40902..dcb9b7d7b 100644
  28. --- a/INSTALL
  29. +++ b/INSTALL
  30. @@ -11,27 +11,9 @@ GRUB depends on some software packages installed into your system. If
  31. you don't have any of them, please obtain and install them before
  32. configuring the GRUB.
  33. -* GCC 4.1.3 or later
  34. - Note: older versions may work but support is limited
  35. -
  36. - Experimental support for clang 3.3 or later (results in much bigger binaries)
  37. +* GCC 5.1.0 or later
  38. + Experimental support for clang 3.8.0 or later (results in much bigger binaries)
  39. for i386, x86_64, arm (including thumb), arm64, mips(el), powerpc, sparc64
  40. - Note: clang 3.2 or later works for i386 and x86_64 targets but results in
  41. - much bigger binaries.
  42. - earlier versions not tested
  43. - Note: clang 3.2 or later works for arm
  44. - earlier versions not tested
  45. - Note: clang on arm64 is not supported due to
  46. - https://llvm.org/bugs/show_bug.cgi?id=26030
  47. - Note: clang 3.3 or later works for mips(el)
  48. - earlier versions fail to generate .reginfo and hence gprel relocations
  49. - fail.
  50. - Note: clang 3.2 or later works for powerpc
  51. - earlier versions not tested
  52. - Note: clang 3.5 or later works for sparc64
  53. - earlier versions return "error: unable to interface with target machine"
  54. - Note: clang has no support for ia64 and hence you can't compile GRUB
  55. - for ia64 with clang
  56. * GNU Make
  57. * GNU Bison 2.3 or later
  58. * GNU gettext 0.17 or later
  59. diff --git a/include/grub/compiler.h b/include/grub/compiler.h
  60. index c9e1d7a73..8f3be3ae7 100644
  61. --- a/include/grub/compiler.h
  62. +++ b/include/grub/compiler.h
  63. @@ -48,4 +48,12 @@
  64. # define WARN_UNUSED_RESULT
  65. #endif
  66. +#if defined(__clang__) && defined(__clang_major__) && defined(__clang_minor__)
  67. +# define CLANG_PREREQ(maj,min) \
  68. + ((__clang_major__ > (maj)) || \
  69. + (__clang_major__ == (maj) && __clang_minor__ >= (min)))
  70. +#else
  71. +# define CLANG_PREREQ(maj,min) 0
  72. +#endif
  73. +
  74. #endif /* ! GRUB_COMPILER_HEADER */
  75. diff --git a/include/grub/safemath.h b/include/grub/safemath.h
  76. new file mode 100644
  77. index 000000000..c17b89bba
  78. --- /dev/null
  79. +++ b/include/grub/safemath.h
  80. @@ -0,0 +1,37 @@
  81. +/*
  82. + * GRUB -- GRand Unified Bootloader
  83. + * Copyright (C) 2020 Free Software Foundation, Inc.
  84. + *
  85. + * GRUB is free software: you can redistribute it and/or modify
  86. + * it under the terms of the GNU General Public License as published by
  87. + * the Free Software Foundation, either version 3 of the License, or
  88. + * (at your option) any later version.
  89. + *
  90. + * GRUB is distributed in the hope that it will be useful,
  91. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  92. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  93. + * GNU General Public License for more details.
  94. + *
  95. + * You should have received a copy of the GNU General Public License
  96. + * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  97. + *
  98. + * Arithmetic operations that protect against overflow.
  99. + */
  100. +
  101. +#ifndef GRUB_SAFEMATH_H
  102. +#define GRUB_SAFEMATH_H 1
  103. +
  104. +#include <grub/compiler.h>
  105. +
  106. +/* These appear in gcc 5.1 and clang 3.8. */
  107. +#if GNUC_PREREQ(5, 1) || CLANG_PREREQ(3, 8)
  108. +
  109. +#define grub_add(a, b, res) __builtin_add_overflow(a, b, res)
  110. +#define grub_sub(a, b, res) __builtin_sub_overflow(a, b, res)
  111. +#define grub_mul(a, b, res) __builtin_mul_overflow(a, b, res)
  112. +
  113. +#else
  114. +#error gcc 5.1 or newer or clang 3.8 or newer is required
  115. +#endif
  116. +
  117. +#endif /* GRUB_SAFEMATH_H */
  118. --
  119. 2.26.2