bug in ARMFrameLowering.cpp:processFunctionBeforeCalleeSavedScan

Hello LLVMDev,

I’ve encountered what looks like a bug in LLVM 2.9, it doesn’t appear to be fixed in 3.0 either.

The problem occurs in function ARMFrameLowering.cpp:processFunctionBeforeCalleeSavedScan. There’s a circular dependency in setting the variable BigStack and AFI->setHashStackFrame(true). The expression which initializes BigStack calls estimateRSSStackSizeLimit which in turn checks AFI->hasStackFrame(). Unfortunately setHasStackFrame(true) only gets called after BigStack is initialized.

The specific issue this caused in my case was that BigStack got initialized as false because estimateRSSStackSizeLimit returned (1<<12)-1 instead of (1<<8)-1. The code thus never entered the if (BigStack && !ExtraCSSpill) block further down where setScavengingFrameIndex should have been set. As a result of it was not being set there, there was an assertion failure (ScavengingFrameIndex >= 0) and subsequent memory corruption in RegScavenger::scavengeRegister.

I fixed this by performing the CanEliminateFrame and RegInfo->cannotEliminateFrame checks before the call to estimateRSSStackSizeLimit, since these values are available before BigStack is initialized. Does that sound reasonable? I’ve attached a patch with my change.

Oh, I should mention that the previous version of LLVM we used, 2.6, didn’t include the check to hasStackFrame() in estimateRSSStackSizeLimit and worked on the same input.

Thanks,

-alok.

ARMFrameLowering.patch (613 Bytes)

Hello Alok,

I fixed this by performing the CanEliminateFrame and
RegInfo->cannotEliminateFrame checks before the call to
estimateRSSStackSizeLimit, since these values are available before BigStack
is initialized. Does that sound reasonable? I’ve attached a patch with my
change.

Will you please provide a testcase which reproduces the problem?

Thanks!

Hi Alok,

Echoing Anton's statements, please provide a testcase! :slight_smile:

Apart from that, looking at your patch and description I actually think that it is estimateRSStackSizeLimit that is wrong.

It is performing a check for a Thumb2 addressing mode whether the target has a FP register. That's the main check - for some reason it is appending another check AFI->hasStackFrame() which seems wrong. The function should assume the function has a stack frame, because its entire function is to estimate its size!

It looks like that check has been copy-pasted from elsewhere in the file. Anton, would you agree with this? I haven't looked at ARMFrameLowering before.

Cheers,

James

Thanks James, Anton. I'll try and get a working test case, but it might take a bit of time as it's been a while since I encountered the error. Hopefully the input which caused it is not too hard to locate.