This is an archive of the discontinued LLVM Phabricator instance.

[builtins] Fix signed integer overflows in fp_fixint_impl.inc
ClosedPublic

Authored by Ka-Ka on Aug 29 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: fp_fixint_impl.inc:39:42: left shift of 8388608 by 40 places cannot be represented in type 'fixint_t' (aka 'long long')
UBSan: fp_fixint_impl.inc:39:17: signed integer overflow: -1 * -9223372036854775808 cannot be represented in type 'fixint_t' (aka 'long long')

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

This was found in an out of tree target.

Diff Detail

Event Timeline

Ka-Ka created this revision.Aug 29 2023, 12:05 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 29 2023, 12:05 AM
Herald added a subscriber: Enna1. · View Herald Transcript
Ka-Ka requested review of this revision.Aug 29 2023, 12:05 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 29 2023, 12:05 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
Ka-Ka added inline comments.Aug 29 2023, 12:10 AM
compiler-rt/test/builtins/Unit/fixsfdi_test.c
73–76

The added test pass when compiling with gcc -lgcc.

MaskRay accepted this revision.Aug 30 2023, 5:58 PM
MaskRay added inline comments.
compiler-rt/lib/builtins/fp_fixint_impl.inc
39

Looks ok.

compiler-rt/test/builtins/Unit/fixsfdi_test.c
73–76

This checks the __fixunssfdi code path, not fp_fixint_impl.inc.

This revision is now accepted and ready to land.Aug 30 2023, 5:58 PM
Ka-Ka added inline comments.Aug 31 2023, 12:17 AM
compiler-rt/test/builtins/Unit/fixsfdi_test.c
73–76

It turn out that the added test only trigger the intended case if compiler-rt is compiled with -D__SOFTFP__ (which we do in our out of tree target). Maybe it is impossible to trigger the signed overflow in fp_fixint_impl.inc in a in tree target (ARM seems to have a feature for softfp but probably not enabled when compiling compiler-rt?).
Should I simply remove the added test from the patch as it don't trigger the intended case?

MaskRay added inline comments.Aug 31 2023, 12:20 AM
compiler-rt/test/builtins/Unit/fixsfdi_test.c
73–76

should be fine to keep it...

Ka-Ka added inline comments.Aug 31 2023, 12:26 AM
compiler-rt/test/builtins/Unit/fixsfdi_test.c
73–76

Sure, then I keep it. I will write something in the commit message about it.

Thanks for reviewing.

This revision was landed with ongoing or failed builds.Aug 31 2023, 1:11 AM
This revision was automatically updated to reflect the committed changes.