dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.26k stars 4.73k forks source link

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

Open spoiledsport opened 3 years ago

spoiledsport commented 3 years ago

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

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]

TypeResolveBug.cs.txt

dotnet-issue-labeler[bot] commented 3 years ago

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 commented 3 years ago

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](https://github.com/dotnet/runtime/files/7070673/TypeResolveBug.cs.txt)
Author: spoiledsport
Assignees: -
Labels: `area-AssemblyLoader-coreclr`, `untriaged`
Milestone: -
vitek-karas commented 3 years ago

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:

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.