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.
Details
Diff Detail
Event Timeline
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. |
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. |
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.
ExitingBB is selected here. This is a reference to a block, and it only exists on the call stack.