Skip to content

Fix stack unwinding for NativeAOT on Unix - #74673

Merged
AntonLapounov merged 1 commit into
dotnet:mainfrom
AntonLapounov:FixNativeAotUnwinding
Aug 27, 2022
Merged

Fix stack unwinding for NativeAOT on Unix#74673
AntonLapounov merged 1 commit into
dotnet:mainfrom
AntonLapounov:FixNativeAotUnwinding

Conversation

@AntonLapounov

@AntonLapounovAntonLapounov commented Aug 26, 2022

Copy link
Copy Markdown
Contributor

Fix intermittent failures for running ./build.sh on Unix:

 crossgen2 -> /runtime/artifacts/bin/coreclr/Linux.x64.Debug/crossgen2/linux-x64/publish/
crossgen2: /runtime/src/coreclr/nativeaot/Runtime/unix/UnixNativeCodeManager.cpp:296: virtual bool UnixNativeCodeManager::UnwindStackFrame(MethodInfo *, REGDISPLAY *, PInvokeTransitionFrame **): Assertion `pNativeMethodInfo->pMainLSDA == pNativeMethodInfo->pLSDA' failed.
Aborted
/runtime/src/installer/pkg/sfx/Microsoft.NETCore.App/Microsoft.NETCore.App.Crossgen2.sfxproj(70,5): error MSB3073: The command "/runtime/artifacts/bin/coreclr/Linux.x64.Debug/crossgen2/linux-x64/publish/crossgen2 /runtime/artifacts/bin/coreclr/Linux.x64.Debug/IL/System.Private.CoreLib.dll --out /runtime/artifacts/obj/Microsoft.NETCore.App.Crossgen2/Debug/net7.0/linux-x64/S.P.C.tmp" exited with code 134.

Below is an example of the call stack that fails the assertion when being unwound:

#38 S_P_CoreLib_System_Exception__ToString ()
at /runtime/src/libraries/System.Private.CoreLib/src/System/Exception.cs:124
#39 ILCompiler_ReadyToRun_Internal_JitInterface_CorInfoImpl__AllocException_0 ()
at /runtime/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs:160
#40 ILCompiler_ReadyToRun_Internal_JitInterface_CorInfoImpl___canGetCookieForPInvokeCalliSig ()
at /runtime/src/coreclr/tools/Common/JitInterface/CorInfoBase.cs:2063
#41 RhpCallCatchFunclet ()
at /runtime/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.S:335
#42 S_P_CoreLib_System_Runtime_EH__DispatchEx ()
at /runtime/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ExceptionHandling.cs:700
#43 S_P_CoreLib_System_Runtime_EH__RhThrowEx ()
at /runtime/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/ExceptionHandling.cs:570
#44 RhpThrowEx () at /runtime/src/coreclr/nativeaot/Runtime/amd64/ExceptionHandling.S:152
#45 ILCompiler_ReadyToRun_Internal_JitInterface_CorInfoImpl__canGetCookieForPInvokeCalliSig ()
at /runtime/src/coreclr/tools/aot/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs:2811
#46 ILCompiler_ReadyToRun_Internal_JitInterface_CorInfoImpl___canGetCookieForPInvokeCalliSig ()
at /runtime/src/coreclr/tools/Common/JitInterface/CorInfoBase.cs:2059
#47 ?? ()
from /runtime/artifacts/bin/coreclr/Linux.x64.Debug/crossgen2/linux-x64/publish/libjitinterface_x64.so

Note that frame 40 above corresponds to the catch funclet of this reverse P/Invoke method:

[UnmanagedCallersOnly]
staticbyte_canGetCookieForPInvokeCalliSig(IntPtrthisHandle,IntPtr*ppException,CORINFO_SIG_INFO*szMetaSig)
{
var_this=GetThis(thisHandle);
try
{
return_this.canGetCookieForPInvokeCalliSig(szMetaSig)?(byte)1:(byte)0;
}
catch(Exceptionex)
{
*ppException=_this.AllocException(ex);
returndefault;
}
}

The fix is to use the funclet's LSDA instead of the main LSDA — similar to what we do on Windows.

Related changes: dotnet/corert#2157, dotnet/corert#4487.
Fixes#70543.

@jkotasjkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for fixing this! We should backport to the fix to 7.0.

@AntonLapounov

Copy link
Copy Markdown
ContributorAuthor

The failure in WasmBuildTests is #74328.

@AntonLapounov
AntonLapounov merged commit 9a1debd into dotnet:mainAug 27, 2022
@AntonLapounov
AntonLapounov deleted the FixNativeAotUnwinding branch August 27, 2022 00:06
@AntonLapounov

Copy link
Copy Markdown
ContributorAuthor

/backport to release/7.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/7.0: https://github.com/dotnet/runtime/actions/runs/2937328274

@AntonLapounovAntonLapounov changed the title Fix stack unwinding for NativeAOT on Unix x64Fix stack unwinding for NativeAOT on UnixAug 27, 2022
@ghostghost locked as resolved and limited conversation to collaborators Sep 26, 2022
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-NativeAOT-coreclros-linuxLinux OS (any supported distro)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crossgen2 crashes on linux-x64

2 participants

@AntonLapounov@jkotas