This is an archive of the discontinued LLVM Phabricator instance.

[SimplifyCFG] Avoid shifting by a too large exponent.
ClosedPublic

Authored by fhahn on Apr 28 2022, 5:32 AM.

Details

Summary

TI->getBitWidth can be > 64 and in those cases the shift will be UB due
to the exponent being too large.

To fix this, cap the shift at 63. I think this should work out fine,
because TableSize is itself a 64 bit type and the maximum table size
must fit in the type. Also, if we would underestimate the size here, at
most we get an extra ZExt.

Diff Detail

Event Timeline

fhahn created this revision.Apr 28 2022, 5:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 28 2022, 5:32 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
fhahn requested review of this revision.Apr 28 2022, 5:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 28 2022, 5:32 AM

Are the tests changing with this patch? Use --check-globals with update_test_checks.py to verify that the table entries are as expected?

fhahn updated this revision to Diff 425989.Apr 29 2022, 1:49 AM

Are the tests changing with this patch? Use --check-globals with update_test_checks.py to verify that the table entries are as expected?

Thanks, I updated the test with --check-globals. It looks like the update script doesn't use the pattern for the global at its uses, I'll file a bug for that.

There are no test changes. I think for test changes we would need a table with a size larger than fits into a 64 bit integer.

spatel accepted this revision.Apr 29 2022, 6:08 AM

Are the tests changing with this patch? Use --check-globals with update_test_checks.py to verify that the table entries are as expected?

Thanks, I updated the test with --check-globals. It looks like the update script doesn't use the pattern for the global at its uses, I'll file a bug for that.

There are no test changes. I think for test changes we would need a table with a size larger than fits into a 64 bit integer.

I see. So was this bug found only by inspection?
It's hard to keep track of the various size constraints in this code, but this seems ok. LGTM.

This revision is now accepted and ready to land.Apr 29 2022, 6:08 AM
fhahn added a comment.Apr 29 2022, 6:10 AM

Are the tests changing with this patch? Use --check-globals with update_test_checks.py to verify that the table entries are as expected?

Thanks, I updated the test with --check-globals. It looks like the update script doesn't use the pattern for the global at its uses, I'll file a bug for that.

There are no test changes. I think for test changes we would need a table with a size larger than fits into a 64 bit integer.

I see. So was this bug found only by inspection?

It was found when using a Clang build with UBSan :)

This revision was landed with ongoing or failed builds.Apr 29 2022, 7:19 AM
This revision was automatically updated to reflect the committed changes.
arichardson added inline comments.
llvm/test/Transforms/SimplifyCFG/X86/switch-to-lookup-large-types.ll
24

@fhahn These two functions are identical, was this supposed to use an i64 in the switch?