runtime: Crash on reentry of tiered compilation

Description

When our product (Autodesk Civil 3D) turns on tiered compilation and open some specific file, it will crash when the JIT is triggered in a reentry way. Let me try to describe it clearly.

  • There are two managed DLLs involved, AeccDbMgd.dll and AecBaseMgd.dll. AeccDbMgd.dll uses AecBaseMgd.dll.
  • The process will not load AecBaseMgd on start up, but on demand.
  • During start up, some native ‘observer’ code will try to construct a .NET object wrapper in AeccDbMgd.dll, which triggers the first JIT.
  • Due to the dependency between the two dlls, AecBaseMgd.dll will be loaded.
  • AutoCAD has some code that fires some events during loading of AecBaseMgd.dll, eventually triggers similar ‘observer’ code as above, and then the second time JIT.

Callstack is something like below:

Exception thrown at 0x00007FF88B0BB699 in acad.exe: Microsoft C++ exception: EETypeLoadException at memory location 0x00000070FDFEA860.
Some lines are omitted/obfuscated for confidentiality.

 	KernelBase.dll!00007ff88b0bb699()	Unknown
 	clrjit.dll!Compiler::impImportBlockCode(BasicBlock * block=0x000001d48ac788a0) Line 8397	C++
 	clrjit.dll!Compiler::impImportBlock(BasicBlock * block=0x000001d48ac788a0) Line 11237	C++
 	clrjit.dll!Compiler::impImport() Line 12187	C++
 	clrjit.dll!Compiler::fgImport() Line 580	C++
 	[Inline Frame] clrjit.dll!Phase::Run() Line 61	C++
 	[Inline Frame] clrjit.dll!DoPhase(Compiler *) Line 136	C++
 	clrjit.dll!Compiler::compCompile(void * * methodCodePtr=0x00000070fdfeeda0, unsigned int * methodCodeSize=0x00000070fdfeee58, JitFlags * compileFlags=0x00000070fdfeedc0) Line 4542	C++
 	clrjit.dll!Compiler::compCompileHelper(CORINFO_MODULE_STRUCT_ * classPtr, ICorJitInfo * compHnd=0x00000070fdfef1d0, CORINFO_METHOD_INFO * methodInfo=0x00000070fdfef0c0, void * * methodCodePtr=0x00000070fdfeeda0, unsigned int * methodCodeSize=0x00000070fdfeee58, JitFlags * compileFlags=0x00000070fdfeedc0) Line 7150	C++
 	clrjit.dll!Compiler::compCompile(CORINFO_MODULE_STRUCT_ * classPtr=0x00007fff45054c98, void * * methodCodePtr=0x00000070fdfeeda0, unsigned int * methodCodeSize=0x00000070fdfeee58, JitFlags * compileFlags=0x00000070fdfeedc0) Line 6292	C++
 	clrjit.dll!jitNativeCode(CORINFO_METHOD_STRUCT_ * methodHnd=0x00007fff45131a08, CORINFO_MODULE_STRUCT_ * classPtr=0x00007fff45054c98, ICorJitInfo * compHnd=0x00000070fdfef1d0, CORINFO_METHOD_INFO * methodInfo=0x00000070fdfef0c0, void * * methodCodePtr=0x00000070fdfeeda0, unsigned int * methodCodeSize=0x00000070fdfeee58, JitFlags * compileFlags=0x00000070fdfeedc0, void * inlineInfoPtr) Line 7783	C++
 	clrjit.dll!CILJit::compileMethod(ICorJitInfo * compHnd=0x00000070fdfef1d0, CORINFO_METHOD_INFO * methodInfo=0x00000070fdfef0c0, unsigned int flags=2147483652, unsigned char * * entryAddress=0x00000070fdfeee68, unsigned int * nativeSizeOfCode=0x00000070fdfeee58) Line 299	C++
 	[Managed to Native Transition]	
 	[Native to Managed Transition]	
 	KernelBase.dll!00007ff88b0bb699()	Unknown
 	clrjit.dll!Compiler::impImportBlockCode(BasicBlock * block=0x000001d48ac788a0) Line 8397	C++
 	clrjit.dll!Compiler::impImportBlock(BasicBlock * block=0x000001d48ac788a0) Line 11237	C++
 	clrjit.dll!Compiler::impImport() Line 12187	C++
 	clrjit.dll!Compiler::fgImport() Line 580	C++
 	[Inline Frame] clrjit.dll!Phase::Run() Line 61	C++
 	[Inline Frame] clrjit.dll!DoPhase(Compiler *) Line 136	C++
 	clrjit.dll!Compiler::compCompile(void * * methodCodePtr=0x00000070fdfeeda0, unsigned int * methodCodeSize=0x00000070fdfeee58, JitFlags * compileFlags=0x00000070fdfeedc0) Line 4542	C++
 	clrjit.dll!Compiler::compCompileHelper(CORINFO_MODULE_STRUCT_ * classPtr, ICorJitInfo * compHnd=0x00000070fdfef1d0, CORINFO_METHOD_INFO * methodInfo=0x00000070fdfef0c0, void * * methodCodePtr=0x00000070fdfeeda0, unsigned int * methodCodeSize=0x00000070fdfeee58, JitFlags * compileFlags=0x00000070fdfeedc0) Line 7150	C++
 	clrjit.dll!Compiler::compCompile(CORINFO_MODULE_STRUCT_ * classPtr=0x00007fff45054c98, void * * methodCodePtr=0x00000070fdfeeda0, unsigned int * methodCodeSize=0x00000070fdfeee58, JitFlags * compileFlags=0x00000070fdfeedc0) Line 6292	C++
 	clrjit.dll!jitNativeCode(CORINFO_METHOD_STRUCT_ * methodHnd=0x00007fff45131a08, CORINFO_MODULE_STRUCT_ * classPtr=0x00007fff45054c98, ICorJitInfo * compHnd=0x00000070fdfef1d0, CORINFO_METHOD_INFO * methodInfo=0x00000070fdfef0c0, void * * methodCodePtr=0x00000070fdfeeda0, unsigned int * methodCodeSize=0x00000070fdfeee58, JitFlags * compileFlags=0x00000070fdfeedc0, void * inlineInfoPtr) Line 7783	C++
 	clrjit.dll!CILJit::compileMethod(ICorJitInfo * compHnd=0x00000070fdfef1d0, CORINFO_METHOD_INFO * methodInfo=0x00000070fdfef0c0, unsigned int flags=2147483652, unsigned char * * entryAddress=0x00000070fdfeee68, unsigned int * nativeSizeOfCode=0x00000070fdfeee58) Line 299	C++
 	[Managed to Native Transition]	
 	acdbmgd.dll!<<<<<< Create .NET wrapper object 2 via C++/CLI >>>>>>
 	[Native to Managed Transition]	
 	acdb25.dll!<<<<<< Reactors fired on object 2 modification >>>>>>
        acdb25.dll!<<<<<< Object 2 is modified >>>>>>
 	acdb25.dll!<<<<<< Load AecBaseMgd.dll during JIT 1st time >>>>>>
 	[Managed to Native Transition]	
 	System.Private.CoreLib.dll!System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(string assemblyPath) Line 347	C#
 	System.Private.CoreLib.dll!System.Runtime.Loader.AssemblyLoadContext.GetFirstResolvedAssemblyFromResolvingEvent(System.Reflection.AssemblyName assemblyName = {System.Reflection.AssemblyName}) Line 632	C#
 	System.Private.CoreLib.dll!System.Runtime.Loader.AssemblyLoadContext.ResolveUsingEvent(System.Reflection.AssemblyName assemblyName) Line 698	C#
 	System.Private.CoreLib.dll!System.Runtime.Loader.AssemblyLoadContext.ResolveUsingResolvingEvent(nint gchManagedAssemblyLoadContext, System.Reflection.AssemblyName assemblyName) Line 134	C#
 	[Native to Managed Transition]	
 	clrjit.dll!Compiler::impImportBlockCode(BasicBlock * block=0x000001d495cf2a90) Line 8397	C++
 	clrjit.dll!Compiler::impImportBlock(BasicBlock * block=0x000001d495cf2a90) Line 11237	C++
 	clrjit.dll!Compiler::impImport() Line 12187	C++
 	clrjit.dll!Compiler::fgImport() Line 580	C++
 	[Inline Frame] clrjit.dll!Phase::Run() Line 61	C++
 	[Inline Frame] clrjit.dll!DoPhase(Compiler *) Line 136	C++
 	clrjit.dll!Compiler::compCompile(void * * methodCodePtr=0x00000070fdffc3b0, unsigned int * methodCodeSize=0x00000070fdffc468, JitFlags * compileFlags=0x00000070fdffc3d0) Line 4542	C++
 	clrjit.dll!Compiler::compCompileHelper(CORINFO_MODULE_STRUCT_ * classPtr, ICorJitInfo * compHnd=0x00000070fdffc7e0, CORINFO_METHOD_INFO * methodInfo=0x00000070fdffc6d0, void * * methodCodePtr=0x00000070fdffc3b0, unsigned int * methodCodeSize=0x00000070fdffc468, JitFlags * compileFlags=0x00000070fdffc3d0) Line 7150	C++
 	clrjit.dll!Compiler::compCompile(CORINFO_MODULE_STRUCT_ * classPtr=0x00007fff45054c98, void * * methodCodePtr=0x00000070fdffc3b0, unsigned int * methodCodeSize=0x00000070fdffc468, JitFlags * compileFlags=0x00000070fdffc3d0) Line 6292	C++
 	clrjit.dll!jitNativeCode(CORINFO_METHOD_STRUCT_ * methodHnd=0x00007fff451354a8, CORINFO_MODULE_STRUCT_ * classPtr=0x00007fff45054c98, ICorJitInfo * compHnd=0x00000070fdffc7e0, CORINFO_METHOD_INFO * methodInfo=0x00000070fdffc6d0, void * * methodCodePtr=0x00000070fdffc3b0, unsigned int * methodCodeSize=0x00000070fdffc468, JitFlags * compileFlags=0x00000070fdffc3d0, void * inlineInfoPtr) Line 7783	C++
 	clrjit.dll!CILJit::compileMethod(ICorJitInfo * compHnd=0x00000070fdffc7e0, CORINFO_METHOD_INFO * methodInfo=0x00000070fdffc6d0, unsigned int flags=2147483652, unsigned char * * entryAddress=0x00000070fdffc478, unsigned int * nativeSizeOfCode=0x00000070fdffc468) Line 299	C++
 	[Managed to Native Transition]	
 	[Native to Managed Transition]	
 	clrjit.dll!Compiler::impImportBlockCode(BasicBlock * block=0x000001d495cf2a90) Line 8397	C++
 	clrjit.dll!Compiler::impImportBlock(BasicBlock * block=0x000001d495cf2a90) Line 11237	C++
 	clrjit.dll!Compiler::impImport() Line 12187	C++
 	clrjit.dll!Compiler::fgImport() Line 580	C++
 	[Inline Frame] clrjit.dll!Phase::Run() Line 61	C++
 	[Inline Frame] clrjit.dll!DoPhase(Compiler *) Line 136	C++
 	clrjit.dll!Compiler::compCompile(void * * methodCodePtr=0x00000070fdffc3b0, unsigned int * methodCodeSize=0x00000070fdffc468, JitFlags * compileFlags=0x00000070fdffc3d0) Line 4542	C++
 	clrjit.dll!Compiler::compCompileHelper(CORINFO_MODULE_STRUCT_ * classPtr, ICorJitInfo * compHnd=0x00000070fdffc7e0, CORINFO_METHOD_INFO * methodInfo=0x00000070fdffc6d0, void * * methodCodePtr=0x00000070fdffc3b0, unsigned int * methodCodeSize=0x00000070fdffc468, JitFlags * compileFlags=0x00000070fdffc3d0) Line 7150	C++
 	clrjit.dll!Compiler::compCompile(CORINFO_MODULE_STRUCT_ * classPtr=0x00007fff45054c98, void * * methodCodePtr=0x00000070fdffc3b0, unsigned int * methodCodeSize=0x00000070fdffc468, JitFlags * compileFlags=0x00000070fdffc3d0) Line 6292	C++
 	clrjit.dll!jitNativeCode(CORINFO_METHOD_STRUCT_ * methodHnd=0x00007fff451354a8, CORINFO_MODULE_STRUCT_ * classPtr=0x00007fff45054c98, ICorJitInfo * compHnd=0x00000070fdffc7e0, CORINFO_METHOD_INFO * methodInfo=0x00000070fdffc6d0, void * * methodCodePtr=0x00000070fdffc3b0, unsigned int * methodCodeSize=0x00000070fdffc468, JitFlags * compileFlags=0x00000070fdffc3d0, void * inlineInfoPtr) Line 7783	C++
 	clrjit.dll!CILJit::compileMethod(ICorJitInfo * compHnd=0x00000070fdffc7e0, CORINFO_METHOD_INFO * methodInfo=0x00000070fdffc6d0, unsigned int flags=2147483652, unsigned char * * entryAddress=0x00000070fdffc478, unsigned int * nativeSizeOfCode=0x00000070fdffc468) Line 299	C++
 	[Managed to Native Transition]	
 	acdbmgd.dll!<<<<<< Create .NET wrapper object 1 via C++/CLI >>>>>>
 	[Native to Managed Transition]	
 	acdb25.dll!<<<<<< Reactors fired on object 1 modification >>>>>>
        acdb25.dll!<<<<<< Object 1 is modified during file open >>>>>>
 	acad.exe!<<<<<<< Open drawing >>>>>>

Reproduction Steps

It’s quite hard for me to have such a sample.

Expected behavior

No crash.

Actual behavior

Crash.

Regression?

No idea.

Known Workarounds

Pre-load the mgd dll.

Configuration

.NET 8.0.100 Windows x64

Other information

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 19 (15 by maintainers)

Most upvoted comments

This specific repro works fine in .NET Framework, However, this general problem existed in .NET Framework since forever. Different versions of .NET Framework have similar behavior changes. I am sure that I can build similar repro cases that work on one version of .NET Framework and do not work on a different version of .NET Framework (and vice versa), or that work on 32-bit .NET Framework x86 and do not work on 64-bit .NET Framework (and vice versa).

This behavior is by design. It is consequence of lazy assembly loading strategy used by .NET runtimes with JIT. It is not possible to “fix it”.

I agree with you that writing 100% robust assembly load event handlers is not easy. You have to be very careful about what these event handlers depend on.

This looks like a bug in the custom assembly resolver or assembly load event handler,

Implementations of custom assembly load resolvers and event handlers must be able to handle different timing and ordering of the load events. The timing and ordering of the load events can change depending on the optimizations performed by the JIT.

Here is a small example that demonstrates the problem. It fails with tiered compilation enabled (default) and succeeds with tiered compilation disable:

using System.Xml;

// This is bad implementation of AssemblyLoad event
AppDomain.CurrentDomain.AssemblyLoad += (o, a) =>
{
    Console.WriteLine(a.LoadedAssembly.FullName);

    try
    {
        M();
    }
    catch (Exception e)
    {
        Environment.FailFast("FAILED: ", e);
    }
};

M();

Console.WriteLine("SUCCESS");

Type M()
{
    return typeof(Test);
}

abstract class Test : XmlReader
{
}
>dotnet run -c release
System.Runtime.InteropServices, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Process terminated. FAILED
   at System.Environment.FailFast(System.String)
...
>set DOTNET_TieredCompilation=0
>dotnet run -c release
System.Runtime.InteropServices, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Text.Encoding.Extensions, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Threading, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
SUCCEEDED