This is an archive of the discontinued LLVM Phabricator instance.

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

Authored by Ka-Ka on Aug 25 2023, 12:05 AM.

Details

Summary

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.

Diff Detail

Event Timeline

Ka-Ka created this revision.Aug 25 2023, 12:05 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 25 2023, 12:05 AM
Herald added a subscriber: Enna1. · View Herald Transcript
Ka-Ka requested review of this revision.Aug 25 2023, 12:05 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 25 2023, 12:05 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
MaskRay accepted this revision.Aug 25 2023, 11:20 AM

Tested GCC __ashldi3 does silently discard high bits.

This revision is now accepted and ready to land.Aug 25 2023, 11:20 AM
Ka-Ka updated this revision to Diff 553751.Aug 26 2023, 11:42 AM

clang-format