runtime icon indicating copy to clipboard operation
runtime copied to clipboard

References from one dynamic assembly to another dynamic assembly sometimes do not result in an AppDomain.TypeResolve event

Open spoiledsport opened this issue 5 years ago • 5 comments

When attempting to instantiate a dynamic type hosted in a dynamic assembly from another type residing in another dynamic assembly, do not fire a TypeResolve event. Trying to create the type fail with a TypeLoadException.

This issue is not present on Mono, and the type can be successfully created.

A minimal reproduction code has been included which targets both the full framework and .NET Core.

This seems to be a bug in every .NET runtime I tried up to now. I see similar closed issue #10237

  • Which version of .NET is the code running on?

Microsoft.AspNetCore.All 2.1.29 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.29 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.29 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

  • What OS and version, and what distro if applicable? Windows 10, Windows 11
  • What is the architecture (x64, x86, ARM, ARM64)? x64, x86

TypeResolveBug.cs.txt

spoiledsport avatar Aug 28 '21 12:08 spoiledsport

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ghost avatar Aug 28 '21 12:08 ghost

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

Issue Details

When attempting to instantiate a dynamic type hosted in a dynamic assembly from another type residing in another dynamic assembly, do not fire a TypeResolve event. Trying to create the type fail with a TypeLoadException.

This issue is not present on Mono, and the type can be successfully created.

A minimal reproduction code has been included which targets both the full framework and .NET Core.

This seems to be a bug in every .NET runtime I tried up to now. I see similar closed issue #10237

  • Which version of .NET is the code running on?

Microsoft.AspNetCore.All 2.1.29 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.29 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.29 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.18 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

  • What OS and version, and what distro if applicable? Windows 10, Windows 11
  • What is the architecture (x64, x86, ARM, ARM64)? x64, x86

TypeResolveBug.cs.txt

Author: spoiledsport
Assignees: -
Labels:

area-AssemblyLoader-coreclr, untriaged

Milestone: -

ghost avatar Aug 29 '21 18:08 ghost

I modified the repro to use AssemblyBuilder.DefineDynamicAssembly (as the AppDomain API is only available on .NET Framework).

Leaving the repro otherwise as-is I see consistent behavior between both .NET Framework and .NET Core:

  • It triggers the TypeResolve event handler for Bar
  • Fails to load Bar type

But looking at the repro there seems to be a problem - the Bar type was never actually created. There's no call TypeBuilder.CreateType for that type. So I added tb2.CreateType() after the last line of ilgen2.Emit... With this change the repro works without any exception (and no event handler is triggered either).

My understanding of the TypeBuilder API is that one must call CreateType before actually using the type at runtime. Mono's implementation is probably a bit more permissive. @lambdageek for comments on the Mono's behavior.

vitek-karas avatar Sep 13 '21 14:09 vitek-karas

Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

This process is part of our issue cleanup automation.

This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.