[compiler-rt] 3032189 - [compiler-rt] Avoid signed shift overflow in __muloXi4 and __mulvXi3
Karl-Johan Karlsson via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 8 23:44:45 PST 2023
Author: Karl-Johan Karlsson
Date: 2023-03-09T08:37:47+01:00
New Revision: 3032189c0b73b340c10fa317467b0e540d08df09
URL: https://github.com/llvm/llvm-project/commit/3032189c0b73b340c10fa317467b0e540d08df09
DIFF: https://github.com/llvm/llvm-project/commit/3032189c0b73b340c10fa317467b0e540d08df09.diff
LOG: [compiler-rt] Avoid signed shift overflow in __muloXi4 and __mulvXi3
When compiling compiler-rt with -fsanitize=undefined and running testcases you
end up with the following warning:
UBSan: int_mulo_impl.inc:21:36: left shift of 1 by 63 places cannot be represented in type 'di_int' (aka 'long long')
This can be avoided by simply doing the shift in a matching unsigned variant of
the type.
The same kind of pattern seems to exist in int_mulv_impl.inc
This was found in an out of tree target.
Reviewed By: MaskRay
Differential Revision: https://reviews.llvm.org/D145556
Added:
Modified:
compiler-rt/lib/builtins/int_mulo_impl.inc
compiler-rt/lib/builtins/int_mulv_impl.inc
compiler-rt/lib/builtins/mulodi4.c
compiler-rt/lib/builtins/mulosi4.c
compiler-rt/lib/builtins/muloti4.c
compiler-rt/lib/builtins/mulvdi3.c
compiler-rt/lib/builtins/mulvsi3.c
compiler-rt/lib/builtins/mulvti3.c
Removed:
################################################################################
diff --git a/compiler-rt/lib/builtins/int_mulo_impl.inc b/compiler-rt/lib/builtins/int_mulo_impl.inc
index 567d8b9e6e603..592b7893edbdc 100644
--- a/compiler-rt/lib/builtins/int_mulo_impl.inc
+++ b/compiler-rt/lib/builtins/int_mulo_impl.inc
@@ -18,7 +18,7 @@
static __inline fixint_t __muloXi4(fixint_t a, fixint_t b, int *overflow) {
const int N = (int)(sizeof(fixint_t) * CHAR_BIT);
- const fixint_t MIN = (fixint_t)1 << (N - 1);
+ const fixint_t MIN = (fixint_t)((fixuint_t)1 << (N - 1));
const fixint_t MAX = ~MIN;
*overflow = 0;
fixint_t result = a * b;
diff --git a/compiler-rt/lib/builtins/int_mulv_impl.inc b/compiler-rt/lib/builtins/int_mulv_impl.inc
index 1e920716ec499..06559cf302ea6 100644
--- a/compiler-rt/lib/builtins/int_mulv_impl.inc
+++ b/compiler-rt/lib/builtins/int_mulv_impl.inc
@@ -18,7 +18,7 @@
static __inline fixint_t __mulvXi3(fixint_t a, fixint_t b) {
const int N = (int)(sizeof(fixint_t) * CHAR_BIT);
- const fixint_t MIN = (fixint_t)1 << (N - 1);
+ const fixint_t MIN = (fixint_t)((fixuint_t)1 << (N - 1));
const fixint_t MAX = ~MIN;
if (a == MIN) {
if (b == 0 || b == 1)
diff --git a/compiler-rt/lib/builtins/mulodi4.c b/compiler-rt/lib/builtins/mulodi4.c
index 7209676a327e4..6ecf92664fb5c 100644
--- a/compiler-rt/lib/builtins/mulodi4.c
+++ b/compiler-rt/lib/builtins/mulodi4.c
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#define fixint_t di_int
+#define fixuint_t du_int
#include "int_mulo_impl.inc"
// Returns: a * b
diff --git a/compiler-rt/lib/builtins/mulosi4.c b/compiler-rt/lib/builtins/mulosi4.c
index 4e03c24455d67..3fd18a122a463 100644
--- a/compiler-rt/lib/builtins/mulosi4.c
+++ b/compiler-rt/lib/builtins/mulosi4.c
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#define fixint_t si_int
+#define fixuint_t su_int
#include "int_mulo_impl.inc"
// Returns: a * b
diff --git a/compiler-rt/lib/builtins/muloti4.c b/compiler-rt/lib/builtins/muloti4.c
index 9a7aa85b022bf..9aab6fc3efb33 100644
--- a/compiler-rt/lib/builtins/muloti4.c
+++ b/compiler-rt/lib/builtins/muloti4.c
@@ -19,6 +19,7 @@
// Effects: sets *overflow to 1 if a * b overflows
#define fixint_t ti_int
+#define fixuint_t tu_int
#include "int_mulo_impl.inc"
COMPILER_RT_ABI ti_int __muloti4(ti_int a, ti_int b, int *overflow) {
diff --git a/compiler-rt/lib/builtins/mulvdi3.c b/compiler-rt/lib/builtins/mulvdi3.c
index 1d672c6dc155d..d787d297d564e 100644
--- a/compiler-rt/lib/builtins/mulvdi3.c
+++ b/compiler-rt/lib/builtins/mulvdi3.c
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#define fixint_t di_int
+#define fixuint_t du_int
#include "int_mulv_impl.inc"
// Returns: a * b
diff --git a/compiler-rt/lib/builtins/mulvsi3.c b/compiler-rt/lib/builtins/mulvsi3.c
index 00b2e50eeca91..2571881195fcc 100644
--- a/compiler-rt/lib/builtins/mulvsi3.c
+++ b/compiler-rt/lib/builtins/mulvsi3.c
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#define fixint_t si_int
+#define fixuint_t su_int
#include "int_mulv_impl.inc"
// Returns: a * b
diff --git a/compiler-rt/lib/builtins/mulvti3.c b/compiler-rt/lib/builtins/mulvti3.c
index ba355149f9a76..fad9b2ae2765b 100644
--- a/compiler-rt/lib/builtins/mulvti3.c
+++ b/compiler-rt/lib/builtins/mulvti3.c
@@ -19,6 +19,7 @@
// Effects: aborts if a * b overflows
#define fixint_t ti_int
+#define fixuint_t tu_int
#include "int_mulv_impl.inc"
COMPILER_RT_ABI ti_int __mulvti3(ti_int a, ti_int b) { return __mulvXi3(a, b); }
More information about the llvm-commits
mailing list