This is an archive of the discontinued LLVM Phabricator instance.

Fix constant folding of fp2int to large integers
ClosedPublic

Authored by RKSimon on Mar 17 2017, 5:04 AM.

Details

Summary

We make the assumption in most of our constant folding code that a fp2int will target an integer of 128-bits or less, calling the APFloat::convertToInteger with only uint64_t[2] of raw bits for the result.

Fuzz testing (PR24662) showed that we don't handle other cases at all, resulting in stack overflows and all sorts of crashes.

This patch uses the APSInt version of APFloat::convertToInteger instead to better handle such cases.

What do people think of making the APFloat::convertToInteger(uint64_t*) version non-public to avoid this issue in future?

Diff Detail

Repository
rL LLVM

Event Timeline

RKSimon created this revision.Mar 17 2017, 5:04 AM
efriedma accepted this revision.Mar 17 2017, 11:04 AM

LGTM.

What do people think of making the APFloat::convertToInteger(uint64_t*) version non-public to avoid this issue in future?

If you want to make the API a bit safer, you can make it take a MutableArrayRef rather than a raw pointer, and assert if the array is too small. (Same applies to other APInt/APFloat APIs.)

This revision is now accepted and ready to land.Mar 17 2017, 11:04 AM
This revision was automatically updated to reflect the committed changes.