runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Crash on reentry of tiered compilation

Open angelowang opened this issue 2 years ago • 20 comments

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

angelowang avatar Aug 28 '24 08:08 angelowang

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch See info in area-owners.md if you want to be subscribed.

dotnet-policy-service avatar Aug 28 '24 08:08 dotnet-policy-service

Can you share the dump with us?

EgorBo avatar Aug 28 '24 12:08 EgorBo

errorreport_778684275.zip This is what our crash system captures. It contains a minidump file.

angelowang avatar Aug 29 '24 00:08 angelowang

A few other questions:

  • Is this a regression from an earlier .NET version?
  • Are you able to reproduce the problem?
  • Does the problem go away if tiered compilation is disabled?

AndyAyersMS avatar Aug 29 '24 17:08 AndyAyersMS

Is this a regression from an earlier .NET version?

We don't know. Civil 3D has just ported to .NET last year, and .NET 8 is the first version.

Are you able to reproduce the problem?

Yes, it constantly crashes.

Does the problem go away if tiered compilation is disabled?

Yes, it goes away.

angelowang avatar Aug 30 '24 01:08 angelowang

From the files you shared above it looks like there is an assembly load notification that goes into native code and then calls back into managed which triggers more jitting, perhaps in the assembly that was just loaded (I can't tell).

It is not clear how tiering plays a role here. The error is on a foreground thread and the TC thread is apparently idle. We will likely need a more detailed dump to figure this out. Can you enable a full (or at least heap) dump and share that?

Also can you capture what happens with DOTNET_JitDisasmSummary=1?

FYI @mangod9 possibly a loader issue?

AndyAyersMS avatar Aug 30 '24 21:08 AndyAyersMS

@AndyAyersMS Sent you a OneDrive link via your MS email.

perhaps in the assembly that was just loaded

Yes. There are only two DLLs. AeccDbMgd.dll contains Alignment and Corridor. They both inherit from Entity which is defined in AecBaseMgd.dll. The two JITs happen when constructing Alignment and Corridor respectively. Constructing Alignment triggers loading of AecBaseMgd.dll and this loading triggers construction of Corridor. 🤣

angelowang avatar Sep 02 '24 07:09 angelowang

Thanks -- I will try to look at this soon.

AndyAyersMS avatar Sep 03 '24 15:09 AndyAyersMS

Yeah I think somebody on the runtime or maybe interop team should take this one over. As you say, it looks like:

  1. There's a request construct an Alignment object
  2. Which triggers type load for Entity
  3. Which triggers a custom assembly resolver
  4. Which triggers an assembly load notification for AecBaseMgd.dll
  5. Which triggers your assembly load event handler
  6. Which triggers construction of Corridor (via COM\RCW)
  7. Which attempts a type load for Entity
  8. Which fails.

I suspect tiering's main role is to alter the order of class loading / initialization.

@mangod9 I am going to reassign this one to runtime.

AndyAyersMS avatar Sep 03 '24 21:09 AndyAyersMS

Tagging subscribers to this area: @mangod9 See info in area-owners.md if you want to be subscribed.

dotnet-policy-service avatar Sep 03 '24 21:09 dotnet-policy-service

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

jkotas avatar Sep 03 '24 23:09 jkotas

Tagging subscribers to this area: @vitek-karas, @agocke, @vsadov See info in area-owners.md if you want to be subscribed.

dotnet-policy-service avatar Sep 03 '24 23:09 dotnet-policy-service

We may want to mention best practices for implementing assembly load event handlers in the docs.

jkotas avatar Sep 03 '24 23:09 jkotas

Very nice example to help me understand the problem. Thanks!

I tried the example code with .NET framework, it can work. So it's a regression from .NET framework. It's also a regression from .NET core 2 since Tiered compilation is enabled by default from .NET core 3.

From a .NET developer's perspective, I'd like to suggest fix this issue in JIT side if it's possible, rather than requiring developers considering it. It's actually not easy for developers to completely avoid this issue, the M() method can be called indirectly in a complex system.

panxn avatar Sep 04 '24 06:09 panxn

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.

jkotas avatar Sep 04 '24 15:09 jkotas

We may want to mention best practices for implementing assembly load event handlers in the docs.

@jkotas What would these best practices be? It sounds like one should avoid triggering any nested JIT but I don't really see how to do this. We don't even know that AssemblyLoad is triggered by JIT.

szilvaa-adsk avatar Sep 05 '24 21:09 szilvaa-adsk

The problem is with triggering recursive type loading or JITing of the type or method that triggered the assembly load. Nested type loading or JITing is fine in general, but it cannot be for the same type or method.

jkotas avatar Sep 06 '24 01:09 jkotas

Thanks! This helps. Do you know of any APIs that would allow an implementer of AssemblyLoad to find out if the event is being triggered because of JIT and what type/method triggered it?

szilvaa-adsk avatar Sep 06 '24 01:09 szilvaa-adsk

This information is not available in the event.

jkotas avatar Sep 06 '24 01:09 jkotas

Closing as by-design.

agocke avatar Jul 21 '25 17:07 agocke