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

Karl-Johan Karlsson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 25 00:05:10 PDT 2023


Ka-Ka created this revision.
Ka-Ka added reviewers: MaskRay, phosek.
Herald added a subscriber: Enna1.
Herald added a project: All.
Ka-Ka requested review of this revision.
Herald added a project: Sanitizers.
Herald added a subscriber: Sanitizers.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158819

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


Index: compiler-rt/lib/builtins/ashrti3.c
===================================================================
--- compiler-rt/lib/builtins/ashrti3.c
+++ compiler-rt/lib/builtins/ashrti3.c
@@ -31,7 +31,7 @@
     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;
 }
Index: compiler-rt/lib/builtins/ashrdi3.c
===================================================================
--- compiler-rt/lib/builtins/ashrdi3.c
+++ compiler-rt/lib/builtins/ashrdi3.c
@@ -29,7 +29,7 @@
     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;
 }
Index: compiler-rt/lib/builtins/ashlti3.c
===================================================================
--- compiler-rt/lib/builtins/ashlti3.c
+++ compiler-rt/lib/builtins/ashlti3.c
@@ -30,7 +30,7 @@
     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;
 }
Index: compiler-rt/lib/builtins/ashldi3.c
===================================================================
--- compiler-rt/lib/builtins/ashldi3.c
+++ compiler-rt/lib/builtins/ashldi3.c
@@ -28,7 +28,7 @@
     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;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158819.553388.patch
Type: text/x-patch
Size: 1908 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230825/55459429/attachment.bin>


More information about the llvm-commits mailing list