Assertion propagation creates an unreachable block and incorrectly propagates
There's an issue dotnet/runtime#6321 on the test JIT/Regression/CLR-x86-JIT/V1.2-M01/b07369
To reproduce the crash in a debug build we need to apply dfe547282 (original version for repro on ARM softfp e9042b105 ) and run the b07369.exe test (which is copied here and renamed as txt https://github.com/dotnet/coreclr/files/371033/b07369.txt) Note that there's a PR dotnet/coreclr#6353 which zeroes in the constructor which hides the issue.
The crash happens in RangeCheck::MergeEdgeAssertions:
assert(m_pCompiler->vnStore->IsVNArrLenArithBound(curAssertion->op1.vn) ||
m_pCompiler->vnStore->IsVNArrLenBound(curAssertion->op1.vn) ||
m_pCompiler->vnStore->IsVNConstantBound(curAssertion->op1.vn));
And the cause for the issue is that curAssertion is not in fact initialized. We just overrun the array optAssertionTabPrivate in the m_pCompiler->optGetAssertion line:
// Walk through the "assertions" to check if the apply.
BitVecOps::Iter iter(m_pCompiler->apTraits, assertions);
unsigned index = 0;
while (iter.NextElem(m_pCompiler->apTraits, &index))
{
index++;
Compiler::AssertionDsc* curAssertion = m_pCompiler->optGetAssertion((Compiler::AssertionIndex)index);
So the root of the issue is in the assertions bitmap.
The assertions bitmap is initialized in Compiler::optInitAssertionDataflowFlags()
BitVecOps::Assign(apTraits, jumpDestOut[block->bbNum], apFull);
Then in RangeCheck::MergeAssertion it is picked in the branch:
else if ((pred->bbJumpKind == BBJ_COND || pred->bbJumpKind == BBJ_ALWAYS) && pred->bbJumpDest == block)
{
if (m_pCompiler->bbJtrueAssertionOut != NULL)
{
assertions = m_pCompiler->bbJtrueAssertionOut[pred->bbNum];
JITDUMP("Merge assertions from pred BB%02d JTrue edge: %s\n", pred->bbNum, BitVecOps::ToString(m_pCompiler->apTraits, assertions));
}
}
And eventually passed here:
if (!BitVecOps::MayBeUninit(assertions))
{
// Perform the merge step to fine tune the range value.
MergeEdgeAssertions(op, assertions, pRange);
}
So it actually doesn't get updated after the initialization and the crash happens after all.
It's not updated because in the beginning of the function Compiler::optAssertionPropMain() we remove a jump making a basic block unreachable:
Here's a basic block without any predecessors and it's not m_pCompiler->fgFirstBB:
-------------------------------------------------------------------------------------------------------------------------------------
BBnum descAddr ref try hnd preds weight [IL range] [jump] [EH region] [flags]
-------------------------------------------------------------------------------------------------------------------------------------
BB01 [000827DC] 1 1 [000..032)-> BB03 (always) i label target new[]
BB02 [0008289C] 0 0.5 [032..039)-> BB04 (always) i
BB03 [0008295C] 1 BB01 0.5 [039..03F) i label target
BB04 [00082A1C] 2 BB02,BB03 1 [03F..059) (return) i label target idx
-------------------------------------------------------------------------------------------------------------------------------------
Then the value for the mask doesn't get properly updated for the basic block and the incorrect value causes the crash.
The problem with such a control flow for assertion propagation is highlighted in the following "workaround" for this issue: b427a3a45
@papaslavik - Thanks for all your analysis on this! I have not yet been able to run tests on the arm emulator, but I presumed that the control flow and assertions would be similar for x64/ux. However, it appears that the two methods in the test (Main and Method_0xcf54fbcb) are both being jitted with opts disabled. It doesn't seem to be set that way in the assembly, so I'm trying to track down why that's happening. In the meantime, I will also look into what's happening on the desktop version, which is indeed compiling the methods with opts enabled (and it does exhibit the behavior where the block becomes unreachable, though it apparently doesn't lead to an assert or AV).
@CarolEidt have you picked the commit I mentioned, which is needed to reproduce the issue?
@CarolEidt, in fact on a x64 machine the structure packing might is different, so my reproducing commit might not work. I think we need to just initialize the field with the incorrect values which then cause the crash.
So I rewrote the repro step according to my previous note, so now it should be cross platform: dfe547282
Just to show the crash message on arm softfp:
./corerun b07369.exe
Assert failure(PID 13242 [0x000033ba], Thread: 13242 [0x33ba]): Assertion failed 'm_pCompiler->vnStore->IsVNArrLenArithBound(curAssertion->op1.vn) || m_pCompiler->vnStore->IsVNArrLenBound(curAssertion->op1.vn) || m_pCompiler->vnStore->IsVNConstantBound(curAssertion->op1.vn)' in 'ILGEN_0x1c9344b9:Method_0xcf54fbcb():int' (IL size 89)
File: /home/v.nikolaev/CoreClr/coreclr/src/jit/rangecheck.cpp Line: 528
Image: /home/krivtsov/v.nikolaev/coreoverlay/corerun
**** MessageBox invoked, title 'corerun - Assert Failure (PID 13242, Thread 13242/0x33ba)' **** Assertion failed 'm_pCompiler->vnStore->IsVNArrLenArithBound(curAssertion->op1.vn) || m_pCompiler->vnStore->IsVNArrLenBound(curAssertion->op1.vn) || m_pCompiler->vnStore->IsVNConstantBound(curAssertion->op1.vn)' in 'ILGEN_0x1c9344b9:Method_0xcf54fbcb():int' (IL size 89)
/home/v.nikolaev/CoreClr/coreclr/src/jit/rangecheck.cpp, Line: 528
Abort - Kill program Retry - Debug Ignore - Keep running
Image: /home/krivtsov/v.nikolaev/coreoverlay/corerun
Aborted
@erozenfeld FYI
Just rechecked that on arm hard it also fails with the reproducing commit dfe5472
I've created a pull request with a suggested fix dotnet/coreclr#6478
But some checks fail in that PR
@papaslavik - My PR dotnet/coreclr#6484 corrects the asserts that I encountered when I added more aggressive checking of the AssertionDscs. However, I don't want to close this issue until I know that it also addresses the issue you are encountering on ARM. Please let me know.
Thanks @CarolEidt
@papaslavik I appreciate all of your effort in narrowing this down!