You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ExecutionEngineException in assembly.GetTypes() when the assembly has a class with covariant return types #45037
public abstract class ABase
{
public abstract object this[int index] { get; }
}
public sealed class Concrete<T> : ABase
where T : class
{
public override T this[int index] // <- covariant indexer
{
get
{
throw null;
}
}
}
I think the problem is the covariant indexer. Whenever I replace the covariant indexer with a regular one that just returns object the problem goes away. I've also tried to reproduce the problem with methods and properties with covariant return types, but that did not trigger the bug.
Update: after more experimentation I did find it possible to trigger this bug with covariant return types for methods and properties. See my comment down below.
Configuration
I'm running .NET 5.0 (v5.0.100) on Window 10 x64. I have not tried it on another machine yet.
Regression?
Not really, since .NET 5.0 is the first version that supports covariant return types. However in previous .net versions you were able to call GetTypes() on an assembly without a crash :).
Other information
Unfortunately there's no stack trace. The only information I get from Visual Studio is:
An unhandled exception of type 'System.ExecutionEngineException' occurred in System.Private.CoreLib.dll
The program '[17496] FooCrash.exe' has exited with code -1 (0xffffffff).
By the way, I hope this is the right GitHub project for these types of exceptions. I saw that System.Private.Corelib lives in this repository so it was my best guess :)
Its worse than I thought, making it really hard to work around.
This does not trigger the bug (hence I thought regular covariant return types do work):
public abstract class ABase { public abstract IReadOnlyList<object> Get(int index); }
public sealed class Concrete<T> : ABase
where T : class
{
public override IReadOnlyList<T> Get(int index) => throw null;
}
However this does trigger the bug
public abstract class ABase { public abstract object Get(int index); }
public sealed class Concrete<T> : ABase
where T : class
{
public override T Get(int index) => throw null;
}
So basically any assembly with a class that has a plain covariant return type is unable to be loaded using assembly.GetTypes(). This is especially problematic when using dependency injection, because a single class taking use of the covariant return types feature makes the entire assembly unusable for DI.
I've updated the title of this bug report. I've also updated my reproduction repository to include all three examples.
changed the title [-]ExecutionEngineException in assembly.GetTypes() when the assembly has a class with a covariant indexer[/-][+]ExecutionEngineException in assembly.GetTypes() when the assembly has a class with covariant return types[/+]on Nov 22, 2020
I think I've found another problem with covariant return types which was triggered by similar code. But since it leads to a very different exception I believe it to be a different problem. So I've submitted it as issue #45082. If anybody believes its the same problem I'll close the other issue and add the problem to the reproduction repository.
All and all it seems there are still some kinks in the covariant return types feature that need to be worked out. I already see a lot of people tagged (sorry for the spam over the weekend, its when I have time to play with C#). Hope these issues don't ruin your Monday tomorrow :).
I am looking into it. The problem is that we don't handle uninstantiated generic types in the code that detects covariant return type compatibility at all. So it works for runtime cases when an instantiation of a generic type is used, but it doesn't work for the Assembly.GetTypes() case which returns the uninstantiated types too.
If you don't mind getting your hands really dirty, you could try using TryGetRawMetadata to get the raw metadata of the assembly, pass those values to construct a MetadataReader, use the metadata APIs to find the type you are actually looking for (depending on what you're searching for this may or may not be easy), and then Assembly.GetType(string) that specific type. This should avoid loading the problematic type - MetadataReader doesn't load types into the runtime, it inspects them at a lower level.
After further investigation of my case I noticed that the problem occurs not only when calling Assembly.GetTypes(), but also when creating an instance of a class with a covariant property.
Activity
Dotnet-GitSync-Bot commentedon Nov 20, 2020
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.
jkotas commentedon Nov 21, 2020
jkotas commentedon Nov 21, 2020
cc @janvorli @davidwrighton
roy-t commentedon Nov 22, 2020
Its worse than I thought, making it really hard to work around.
This does not trigger the bug (hence I thought regular covariant return types do work):
However this does trigger the bug
So basically any assembly with a class that has a plain covariant return type is unable to be loaded using
assembly.GetTypes(). This is especially problematic when using dependency injection, because a single class taking use of the covariant return types feature makes the entire assembly unusable for DI.I've updated the title of this bug report. I've also updated my reproduction repository to include all three examples.
[-]ExecutionEngineException in assembly.GetTypes() when the assembly has a class with a covariant indexer[/-][+]ExecutionEngineException in assembly.GetTypes() when the assembly has a class with covariant return types[/+]marek-safar commentedon Nov 22, 2020
/cc @lambdageek
roy-t commentedon Nov 22, 2020
I've found yet another way to trigger an execution engine exception using covariant types.
typeof(Concrete<>), (see class above). I've updated my repo to add this case as well (its a comment in theMainmethod)Hope that helps with tracking down the problem. :)
roy-t commentedon Nov 22, 2020
I think I've found another problem with covariant return types which was triggered by similar code. But since it leads to a very different exception I believe it to be a different problem. So I've submitted it as issue #45082. If anybody believes its the same problem I'll close the other issue and add the problem to the reproduction repository.
All and all it seems there are still some kinks in the covariant return types feature that need to be worked out. I already see a lot of people tagged (sorry for the spam over the weekend, its when I have time to play with C#). Hope these issues don't ruin your Monday tomorrow :).
lambdageek commentedon Nov 23, 2020
@marek-safar Works correctly in Mono.
janvorli commentedon Nov 24, 2020
I am looking into it. The problem is that we don't handle uninstantiated generic types in the code that detects covariant return type compatibility at all. So it works for runtime cases when an instantiation of a generic type is used, but it doesn't work for the Assembly.GetTypes() case which returns the uninstantiated types too.
lawrence-laz commentedon Nov 26, 2020
Any workaround until this gets resolved?
janvorli commentedon Nov 26, 2020
Unfortunately, there is no workaround (other than not using the method on assemblies with covariant returns methods with generic return types).
MichalStrehovsky commentedon Nov 26, 2020
If you don't mind getting your hands really dirty, you could try using TryGetRawMetadata to get the raw metadata of the assembly, pass those values to construct a MetadataReader, use the metadata APIs to find the type you are actually looking for (depending on what you're searching for this may or may not be easy), and then
Assembly.GetType(string)that specific type. This should avoid loading the problematic type -MetadataReaderdoesn't load types into the runtime, it inspects them at a lower level.Here's an example of how to do something similar:
https://stackoverflow.com/questions/50308931/can-i-access-method-bodies-e-g-il-byte-array-using-assembly-trygetrawmetadata
lawrence-laz commentedon Nov 26, 2020
After further investigation of my case I noticed that the problem occurs not only when calling
Assembly.GetTypes(), but also when creating an instance of a class with a covariant property.Here is a minimal repro:
In this case program stops with
Fatal error. Internal CLR error. (0x80131506).Does this seem like the same cause, or should this be registered as a separate issue?
janvorli commentedon Nov 26, 2020
I've checked and it is the same underlying issue.