You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JIT: stack walk failure in arm64 function fragments #91139
export COMPlus_JitAggressiveInlining=1
export COMPlus_JitSplitFunctionSize=100
export COMPlus_GCStress=0x4
Assert failure(PID 94626 [0x000171a2], Thread: 23328476 [0x163f6dc]): !"About to FailFast. set DOTNET_AssertOnFailFast=0 if this is expected"
File: /Users/andya/repos/runtime0/src/coreclr/vm/jithelpers.cpp Line: 4616
Image: /Users/andya/repos/runtime0/artifacts/tests/coreclr/osx.arm64.Checked/Tests/Core_Root/corerun
(may also repro on other arm64 hosts, did not try)
By increasing the split function size you can make the test "pass":
export COMPlus_JitAggressiveInlining=1
export COMPlus_JitSplitFunctionSize=138
export COMPlus_GCStress=0x4
Unhandled exception. System.OverflowException: Value was either too large or too small for a Decimal.
at System.Decimal.op_Multiply(Decimal d1, Decimal d2)
at TestClass.LeafMethod0() in /Users/andya/bugs/antigen/156.cs:line 66
at TestClass.Method21(S2 p_s2_593, UInt16& p_ushort_594, Double& p_double_595, S2 p_s2_596, S1 p_s1_597, UInt32 p_uint_598, Decimal& p_decimal_599, S1& p_s1_600) in /Users/andya/bugs/antigen/156.cs:line 1309
at TestClass.Method0() in /Users/andya/bugs/antigen/156.cs:line 5701
at TestClass.Main(String[] args) in /Users/andya/bugs/antigen/156.cs:line 5798
I have debugged the issue and I believe the problem is that when function is split into multiple fragments, it should not have epilog in the first fragment. The doc mentioned in the comment above is not 100% clear about it, but it seems it can be interpreted this way (it says at a place where it shows an example: Prolog only (region 1: all epilogs are in separated regions))
When I have stepped through the unwinding code for the failing test, the problem occurs when we are unwinding from an address in the second fragment. We always use the fragment #1 unwind info for unwinding, so we have an offset that is behind the end of the first fragment. Since the first fragment contains an epilog, the unwinder thinks we are in epilog and based on the location in the epilog decides how many unwind codes to skip. Since the address is after the epilog, it skips all of them, so the only thing the unwinding ends up doing is to set PC to LR. So, the SP and other registers end up not being updated. That results in the stack cookie check failure, as the SP is incorrect, and the cookie location is relative to it.
The first fragment is the only one where a prolog can exist (naturally, at the beginning of the function) and the only place where a stack pointer adjustment can be made. Any fragment can have any number of epilogs (and not just at the end of the code range).
We always use the fragment 1 unwind info for unwinding
This sounds like the bug; we need to find the fragment in which the PC exists to find the right unwind info applicable to that fragment.
The other fragments don't have any information on the prolog. So, the unwinder would not be able to restore registers pushed in prolog from those. @AndyAyersMS has made a fix to use the first fragment info for unwinding a long time ago when we have hit that in our tests (dotnet/coreclr#22202).
The unwind info for the fragment 2 and the rest start with unwind code end_c. The unwinder exits on encountering this code, which happens always when it is not in one of the epilogs of the fragment.
I'd be curious to know what the current Windows implementation does.
I've checked and it actually has a fix - the handling of the end_c now just ignores the code and continues executing the codes following that. You are right that getting that fix should fix the problem.
it ends up executing all the prolog unwind codes
It actually ends up skipping all of them. Imagine if the location was in the middle of the epilog, it would want to skip part of them and if it was at the "ret", it would need to skip all of them. Since the unwinder doesn't expect getting offset that's past the end of the function, it ends up behaving as if it was at the "ret".
I'll update the unwinder to match the current state in Windows.
The CoreCLR built-in unwinder is only used on Linux, though. On Windows we use RtlUnwind, which I presume is already doing the right thing.
I think we still need to fix the bug introduced by dotnet/coreclr#22202 in LazyGetFunctionEntry that always finds the first fragment for a function/funclet and uses that in subsequent RtlVirtualUnwind calls. E.g., IsIPInEpilog, JIT_Patchpoint.
It's not clear to me which other code gets the RUNTIME_FUNCTION using GetFunctionEntry (which always returns the first fragment unwind info) and subsequently passes it to RtlVirtualUnwind. E.g., Thread::VirtualUnwindCallFrame on Windows uses RtlLookupFunctionEntry which should give the correct fragment. However, there are some cases where it uses GetFunctionEntry.
It's not clear to me which other code gets the RUNTIME_FUNCTION using GetFunctionEntry (which always returns the first fragment unwind info) and subsequently passes it to RtlVirtualUnwind.
By looking at the current state and the old change that introduced the problem, I believe that in all cases, reverting the old change would get a correct value at all the callers of the EECodeInfo::GetFunctionEntry. I don't see a place where the first fragment function entry would be correct.
GetFuncletStartAddress() calls FindRootEntry to find the first fragment. That actually does make sense. (It currently seems unnecessary because it first calls GetFunctionEntry() which also calls FindRootEntry).
Maybe the issue (#10800) that was attempted to be fixed by dotnet/coreclr#22202 is actually fixed by the fixed end_c unwinder handling? I can see in that issue that the end_c and "phantom prolog" are emitted the same as they are today.
GetFuncletStartAddress() calls FindRootEntry to find the first fragment.
Right, before old fix in dotnet/coreclr#22202, the GetFuncletStartAddress actually contained the code that was extracted into the FindRootEntry. So fully reverting that change will fix this issue.
Maybe the issue (#10800) that was attempted to be fixed by dotnet/coreclr#22202 is actually fixed by the fixed end_c unwinder handling?
changed the title [-]JIT: antigen test case fails when unwind fragment size is set very low[/-][+]JIT: stack walk failure in arm64 function fragments[/+]on Sep 21, 2023
ghost added
in-prThere is an active PR which will close this issue when it is merged
Activity
AndyAyersMS commentedon Aug 25, 2023
@BruceForstall FYI
BruceForstall commentedon Sep 13, 2023
Looks like we're failing fast due to a GS cookie check failure.
I've seen it also fail with more "normal" GC hole asserts (e.g., CREATE_CHECK_STRING).
BruceForstall commentedon Sep 14, 2023
I've been repro'ing on win-arm64 with:
If I use
COMPlus_GCStress=4the "FailFast" doesn't repro.Stack trace:
The failure is unreliable. Using a Debug build doesn't fail. If I turn on JitDump it doesn't fail.
BruceForstall commentedon Sep 14, 2023
@janvorli Here's a case where we seem to cause stack walking or GC issues when we use a JIT stress mode that causes us to create multiple unwind info "fragments" more frequently than required (using
DOTNET_JitSplitFunctionSize; see https://learn.microsoft.com/en-us/cpp/build/arm64-exception-handling?view=msvc-170#function-fragments). The fragment creating code has been there for a long time, but it might not get exercised much since the fragments are normally 512K (arm32) or 1MB (arm64) and presumably you'd need a function larger than that plus a stack walk (e.g., GC) in the non-zero fragment to hit any related code.If you have any suggestions for investigation, please let me know.
janvorli commentedon Sep 18, 2023
I'll take a look
BruceForstall commentedon Sep 18, 2023
@janvorli fyi, I created a test PR that enables fragment splitting aggressively (basically, create as many fragments as possible): #92203. I triggered the gcstress pipeline and there are lots of GCStress=3 failures: https://dev.azure.com/dnceng-public/public/_build/results?buildId=409281&view=results.
My current theory is that the VM doesn't know how to look up the GS cookie location correctly on the frame for the non-first fragment.
janvorli commentedon Sep 19, 2023
I have debugged the issue and I believe the problem is that when function is split into multiple fragments, it should not have epilog in the first fragment. The doc mentioned in the comment above is not 100% clear about it, but it seems it can be interpreted this way (it says at a place where it shows an example: Prolog only (region 1: all epilogs are in separated regions))
When I have stepped through the unwinding code for the failing test, the problem occurs when we are unwinding from an address in the second fragment. We always use the fragment #1 unwind info for unwinding, so we have an offset that is behind the end of the first fragment. Since the first fragment contains an epilog, the unwinder thinks we are in epilog and based on the location in the epilog decides how many unwind codes to skip. Since the address is after the epilog, it skips all of them, so the only thing the unwinding ends up doing is to set PC to LR. So, the SP and other registers end up not being updated. That results in the stack cookie check failure, as the SP is incorrect, and the cookie location is relative to it.
BruceForstall commentedon Sep 19, 2023
The first fragment is the only one where a prolog can exist (naturally, at the beginning of the function) and the only place where a stack pointer adjustment can be made. Any fragment can have any number of epilogs (and not just at the end of the code range).
This sounds like the bug; we need to find the fragment in which the PC exists to find the right unwind info applicable to that fragment.
janvorli commentedon Sep 19, 2023
The other fragments don't have any information on the prolog. So, the unwinder would not be able to restore registers pushed in prolog from those. @AndyAyersMS has made a fix to use the first fragment info for unwinding a long time ago when we have hit that in our tests (dotnet/coreclr#22202).
The unwind info for the fragment 2 and the rest start with unwind code
end_c. The unwinder exits on encountering this code, which happens always when it is not in one of the epilogs of the fragment.1 remaining item
janvorli commentedon Sep 20, 2023
I've checked and it actually has a fix - the handling of the
end_cnow just ignores the code and continues executing the codes following that. You are right that getting that fix should fix the problem.It actually ends up skipping all of them. Imagine if the location was in the middle of the epilog, it would want to skip part of them and if it was at the "ret", it would need to skip all of them. Since the unwinder doesn't expect getting offset that's past the end of the function, it ends up behaving as if it was at the "ret".
janvorli commentedon Sep 20, 2023
I'll update the unwinder to match the current state in Windows.
BruceForstall commentedon Sep 20, 2023
The CoreCLR built-in unwinder is only used on Linux, though. On Windows we use RtlUnwind, which I presume is already doing the right thing.
I think we still need to fix the bug introduced by dotnet/coreclr#22202 in
LazyGetFunctionEntrythat always finds the first fragment for a function/funclet and uses that in subsequentRtlVirtualUnwindcalls. E.g.,IsIPInEpilog,JIT_Patchpoint.It's not clear to me which other code gets the
RUNTIME_FUNCTIONusingGetFunctionEntry(which always returns the first fragment unwind info) and subsequently passes it toRtlVirtualUnwind. E.g.,Thread::VirtualUnwindCallFrameon Windows usesRtlLookupFunctionEntrywhich should give the correct fragment. However, there are some cases where it usesGetFunctionEntry.janvorli commentedon Sep 20, 2023
Right, that would be part of the fix.
By looking at the current state and the old change that introduced the problem, I believe that in all cases, reverting the old change would get a correct value at all the callers of the
EECodeInfo::GetFunctionEntry. I don't see a place where the first fragment function entry would be correct.BruceForstall commentedon Sep 20, 2023
GetFuncletStartAddress()callsFindRootEntryto find the first fragment. That actually does make sense. (It currently seems unnecessary because it first callsGetFunctionEntry()which also callsFindRootEntry).Maybe the issue (#10800) that was attempted to be fixed by dotnet/coreclr#22202 is actually fixed by the fixed
end_cunwinder handling? I can see in that issue that theend_cand "phantom prolog" are emitted the same as they are today.janvorli commentedon Sep 21, 2023
Right, before old fix in dotnet/coreclr#22202, the GetFuncletStartAddress actually contained the code that was extracted into the FindRootEntry. So fully reverting that change will fix this issue.
Yes, that's exactly it.
[-]JIT: antigen test case fails when unwind fragment size is set very low[/-][+]JIT: stack walk failure in arm64 function fragments[/+]