[compiler-rt] 5ae3211 - [builtins] Fix signed shift overflows in ashlti3.c, ashrti3.c, ashldi3.c and ashrdi3.c

Karl-Johan Karlsson via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 27 21:47:05 PDT 2023


Author: Karl-Johan Karlsson
Date: 2023-08-28T06:39:48+02:00
New Revision: 5ae3211b105126d0db3bf07777edc8ecb1c8a3b0

URL: https://github.com/llvm/llvm-project/commit/5ae3211b105126d0db3bf07777edc8ecb1c8a3b0
DIFF: https://github.com/llvm/llvm-project/commit/5ae3211b105126d0db3bf07777edc8ecb1c8a3b0.diff

LOG: [builtins] Fix signed shift overflows in ashlti3.c, ashrti3.c, ashldi3.c and ashrdi3.c

When compiling the builtins with the undefined behavior sanitizer and running testcases you end up with the following warning:

UBSan: ashlti3.c:33:35: left shift of 1 by 63 places cannot be represented in type 'di_int' (aka 'long long')
UBSan: ashrti3.c:34:34: left shift of negative value -81985529216486891

This can be avoided by doing the shift in a matching unsigned variant of the type.

The same kind of patterns are found in ashldi3.c and ashrdi3.c

This was found in an out of tree target.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D158819

Added: 
    

Modified: 
    compiler-rt/lib/builtins/ashldi3.c
    compiler-rt/lib/builtins/ashlti3.c
    compiler-rt/lib/builtins/ashrdi3.c
    compiler-rt/lib/builtins/ashrti3.c

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/builtins/ashldi3.c b/compiler-rt/lib/builtins/ashldi3.c
index 04f22228f11db6..7b835da865d750 100644
--- a/compiler-rt/lib/builtins/ashldi3.c
+++ b/compiler-rt/lib/builtins/ashldi3.c
@@ -28,7 +28,8 @@ COMPILER_RT_ABI di_int __ashldi3(di_int a, int b) {
     if (b == 0)
       return a;
     result.s.low = input.s.low << b;
-    result.s.high = (input.s.high << b) | (input.s.low >> (bits_in_word - b));
+    result.s.high =
+        ((su_int)input.s.high << b) | (input.s.low >> (bits_in_word - b));
   }
   return result.all;
 }

diff  --git a/compiler-rt/lib/builtins/ashlti3.c b/compiler-rt/lib/builtins/ashlti3.c
index 99a133ffa22f8d..2bebf10401d34a 100644
--- a/compiler-rt/lib/builtins/ashlti3.c
+++ b/compiler-rt/lib/builtins/ashlti3.c
@@ -30,7 +30,8 @@ COMPILER_RT_ABI ti_int __ashlti3(ti_int a, int b) {
     if (b == 0)
       return a;
     result.s.low = input.s.low << b;
-    result.s.high = (input.s.high << b) | (input.s.low >> (bits_in_dword - b));
+    result.s.high =
+        ((du_int)input.s.high << b) | (input.s.low >> (bits_in_dword - b));
   }
   return result.all;
 }

diff  --git a/compiler-rt/lib/builtins/ashrdi3.c b/compiler-rt/lib/builtins/ashrdi3.c
index 934a5c47fd69b1..c0879b8b252dea 100644
--- a/compiler-rt/lib/builtins/ashrdi3.c
+++ b/compiler-rt/lib/builtins/ashrdi3.c
@@ -29,7 +29,8 @@ COMPILER_RT_ABI di_int __ashrdi3(di_int a, int b) {
     if (b == 0)
       return a;
     result.s.high = input.s.high >> b;
-    result.s.low = (input.s.high << (bits_in_word - b)) | (input.s.low >> b);
+    result.s.low =
+        ((su_int)input.s.high << (bits_in_word - b)) | (input.s.low >> b);
   }
   return result.all;
 }

diff  --git a/compiler-rt/lib/builtins/ashrti3.c b/compiler-rt/lib/builtins/ashrti3.c
index b306051df0287c..d6b1ad9192bf12 100644
--- a/compiler-rt/lib/builtins/ashrti3.c
+++ b/compiler-rt/lib/builtins/ashrti3.c
@@ -31,7 +31,8 @@ COMPILER_RT_ABI ti_int __ashrti3(ti_int a, int b) {
     if (b == 0)
       return a;
     result.s.high = input.s.high >> b;
-    result.s.low = (input.s.high << (bits_in_dword - b)) | (input.s.low >> b);
+    result.s.low =
+        ((du_int)input.s.high << (bits_in_dword - b)) | (input.s.low >> b);
   }
   return result.all;
 }


        


More information about the llvm-commits mailing list