From 3032189c0b73b340c10fa317467b0e540d08df09 Mon Sep 17 00:00:00 2001 From: Karl-Johan Karlsson Date: Thu, 9 Mar 2023 08:37:47 +0100 Subject: [PATCH] [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 --- compiler-rt/lib/builtins/int_mulo_impl.inc | 2 +- compiler-rt/lib/builtins/int_mulv_impl.inc | 2 +- compiler-rt/lib/builtins/mulodi4.c | 1 + compiler-rt/lib/builtins/mulosi4.c | 1 + compiler-rt/lib/builtins/muloti4.c | 1 + compiler-rt/lib/builtins/mulvdi3.c | 1 + compiler-rt/lib/builtins/mulvsi3.c | 1 + compiler-rt/lib/builtins/mulvti3.c | 1 + 8 files changed, 8 insertions(+), 2 deletions(-) diff --git a/compiler-rt/lib/builtins/int_mulo_impl.inc b/compiler-rt/lib/builtins/int_mulo_impl.inc index 567d8b9e6e60..592b7893edbd 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 1e920716ec49..06559cf302ea 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 7209676a327e..6ecf92664fb5 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 4e03c24455d6..3fd18a122a46 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 9a7aa85b022b..9aab6fc3efb3 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 1d672c6dc155..d787d297d564 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 00b2e50eeca9..2571881195fc 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 ba355149f9a7..fad9b2ae2765 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); }