This is an archive of the discontinued LLVM Phabricator instance.

[MemoryBuiltins] Allow truncation in visitAllocaInst()
ClosedPublic

Authored by uabelho on Jul 5 2017, 3:57 AM.

Details

Summary

Solves PR33689.

If the pointer size is less than the size of the type used for the array
size in an alloca (the <ty> type below) then we could trigger the assert in
the PR. In that example we have pointer size i16 and <ty> is i32.

<result> = alloca [inalloca] <type> [, <ty> <NumElements>] [, align <alignment>]

Handle the situation by allowing truncation as well as zero extension in
ObjectSizeOffsetVisitor::visitAllocaInst().

Also, we now detect overflow in visitAllocaInst(), similar to how it was
already done in visitCallSite().

Diff Detail

Repository
rL LLVM

Event Timeline

uabelho created this revision.Jul 5 2017, 3:57 AM

If the truncation actually truncates something (and not just changes e.g. an i32 1 to an i16 1)
then we are not in a good spot, but then I suppose we have already messed up since then we
would have something like

%foo = alloca i16 , i32 1000000

with pointers being 16b wide so the allocated temporary wouldn't fit in memory
anyway.

I don't know this code very well though so if you think there is a more reasonable fix for this
please let me know.

davide added a subscriber: davide.Jul 5 2017, 8:41 AM

Thanks for this! One nit, and I think it looks good.

lib/Analysis/MemoryBuiltins.cpp
518 ↗(On Diff #105241)

Addressing your comment: I realize that this is an existing inconsistency, but we try to return unknown() if an overflow happens (e.g. on line 597). While we're in the area, can we make this test for overflow too, please?

Thanks for the comments!

lib/Analysis/MemoryBuiltins.cpp
518 ↗(On Diff #105241)

So you want me to detect if overflow actually happens and if so return unknown(), and add a testcase for the overflow case as well?

If so, yes I'll try.

lib/Analysis/MemoryBuiltins.cpp
518 ↗(On Diff #105241)

Yes, please.

I'm not 100% sure if testing this will be straightforward, though. If it seems nontrivial, just let me know. Since it's my nit, I'm happy to try to make a test for overflow and commit it after this lands. :)

uabelho added inline comments.Jul 7 2017, 6:19 AM
lib/Analysis/MemoryBuiltins.cpp
518 ↗(On Diff #105241)

Ok I will!

Yes the testing I don't know since I think the test case I have already is pretty non-straightforward.. The i64 is being converted to a [2 x i32] and I think it's that "2" that I will truncate.

So for overflow to happen with just small modifications of my existing test I'll need my %i64_t to be something huge. I'll give it a try though.

uabelho updated this revision to Diff 105810.Jul 10 2017, 12:02 AM

Added overflow checks to visitAllocaInst.

To avoid some code duplication I pulled out the CheckedZextOrTrunc lambda from visitCallSite
and made a member function of it.

uabelho added inline comments.Jul 10 2017, 12:06 AM
lib/Analysis/MemoryBuiltins.cpp
518 ↗(On Diff #105241)

Added overflow checks. Something like this is what you had in mind?

For the testcase, if I change
%i64_t = type i64
to
%i64_t = type i6488888
then the overflow checks trigger, but the end result from instcombine is still the same
as without overflow checks so I don't have any nice output change to check for.

If you know this code then I suppose you'll be able to make a testcase for it.

LGTM after a few stylistic nits are addressed. Thanks again!

lib/Analysis/MemoryBuiltins.cpp
511 ↗(On Diff #105810)

I think it's \p I, though I'm admittedly not great with doxygen.

522 ↗(On Diff #105810)

Please remove the trailing ;

535 ↗(On Diff #105810)

Please remove the brackets, since this is a single-statement if

518 ↗(On Diff #105241)

Something like this is what you had in mind?

Yup!

If you know this code then I suppose you'll be able to make a testcase for it

Will do. :)

uabelho updated this revision to Diff 105962.Jul 10 2017, 10:13 PM
uabelho edited the summary of this revision. (Show Details)

Addressed the few remaining issues.

(Totally idiotic to not see them myself to begin with. Sorry and thanks!)

Updated the summary so it now has a few words about the new overflow
checks in visitAllocaInst().

uabelho marked 3 inline comments as done.Jul 10 2017, 10:14 PM

Thanks!

LGTM!

Totally idiotic to not see them myself to begin with. Sorry and thanks!

Not a problem :)

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

Apparently I accidentally dropped the test case between two revisions of this patch so I submitted it
now in https://reviews.llvm.org/rL307887