This is an archive of the discontinued LLVM Phabricator instance.

CodeGen: Handle missed case of block removal during BlockPlacement.
ClosedPublic

Authored by iteratee on Oct 26 2016, 1:23 PM.

Details

Reviewers
djasper
davidxl
Summary

There is a use after free bug in the existing code. Loop layout selects
a preferred exit block, and then lays out the loop. If this block is
removed during layout, it needs to be invalidated to prevent a use after
free.

Diff Detail

Event Timeline

iteratee updated this revision to Diff 75937.Oct 26 2016, 1:23 PM
iteratee retitled this revision from to CodeGen: Handle missed case of block removal during BlockPlacement..
iteratee updated this object.
iteratee added reviewers: davidxl, djasper.
iteratee set the repository for this revision to rL LLVM.
iteratee added subscribers: echristo, llvm-commits.
davidxl edited edge metadata.Oct 27 2016, 1:08 PM

Can you explain the code path that leads to use after free?

I added comments to the 3 lines that show the possible use after free.

lib/CodeGen/MachineBlockPlacement.cpp
1477

ExitingBB is selected here. This is a reference to a block, and it only exists on the call stack.

1493

buildChain is called here. buildChain may tail-duplicate and remove the block referred to by ExitingBB.

1498

ExitingBB is used here, after it may have been freed.

davidxl added inline comments.Oct 27 2016, 1:47 PM
lib/CodeGen/MachineBlockPlacement.cpp
1503

Should the right fix be to move the 'findBestLoopExit' after buildChain call?

I found this by observing it in the debug logs while looking for something else, so I think the direct fix is worth committing, even if we do a better fix later.

lib/CodeGen/MachineBlockPlacement.cpp
1503

That would require re-writing findBestLoopExit to not assume that the blocks weren't all in a single chain already.

We should re-write it to do that, but I think that's out of scope for an existing use after free bug.

davidxl accepted this revision.Oct 27 2016, 2:15 PM
davidxl edited edge metadata.

lgtm.

As discussed, as this resolve the use after free error, it does point to possible missing loop rotation due to tail dup -- that needs to be investigated as follow up.

This revision is now accepted and ready to land.Oct 27 2016, 2:15 PM
iteratee closed this revision.Oct 27 2016, 2:52 PM