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
.NET AOT caching IDynamicInterfaceCastable.IsInterfaceImplemented results based on type rather than instance #125577
From what I recall, IDynamicInterfaceCastable.IsInterfaceImplemented is meant to be a per instance decision rather than a per type thing. GetInterfaceImplementation was the one that was a per type decision. But it seems the results of IsInterfaceImplemented are being cached based on type. In the below repro, which is similar to our IInspectable in CsWinRT, we are seeing that if an is check passes for one instance of a type, it also passes for other instances even if the IDIC implementation is returning false.
This issue seems to only happen on AOT for generic interfaces. On JIT, the expected results are observed.
Reproduction Steps
internal class Program
{
static void Main(string[] args)
{
var shapeshifter = new Shapeshifter(true);
bool isEnumerableInt = shapeshifter is IEnumerable<int>;
Console.WriteLine("shapeshifter is IEnumerable<int>: " + isEnumerableInt);
var shapeshifter2 = new Shapeshifter(false);
bool isEnumerableInt2 = shapeshifter2 is IEnumerable<int>;
Console.WriteLine("shapeshifter2 is IEnumerable<int>: " + isEnumerableInt2);
}
}
class Shapeshifter : IDynamicInterfaceCastable
{
private bool _result;
public Shapeshifter(bool result)
{
_result = result;
}
public RuntimeTypeHandle GetInterfaceImplementation(RuntimeTypeHandle interfaceType) => throw new NotImplementedException();
public bool IsInterfaceImplemented(RuntimeTypeHandle interfaceType, bool throwIfNotImplemented) => _result;
}
Expected behavior
Same as .NET 8 AOT and same as .NET 10 JIT
net8.0\publish\win-x64\ConsoleApp35.exe
shapeshifter is IEnumerable: True
shapeshifter2 is IEnumerable: False
Actual behavior
net10.0\publish\win-x64\ConsoleApp35.exe
shapeshifter is IEnumerable: True
shapeshifter2 is IEnumerable: True
The root issue here is that COM is going to have cases where some types answer the question differently. Historically this was limited to __ComObject only, but now anything deriving from the public ComObject (source) can fall into the same bucket.
Understanding how to special case that across all runtimes is the next step.
Dumb question: isn't the issue here just that NAOT is caching the IsInterfaceImplemented result for multiple instances, which it shouldn't (because IsInterfaceImplemented is explicitly per-instance, unlike GetInterfaceImplementation)? As in, can you clarify why the behavior here from ComObject or any other type in particular is relevant, and why this is not just a caching issue on the NAOT side which should just be fixed by not caching that result for different instances?
As in, can you clarify why the behavior here from ComObject or any other type in particular is relevant, and why this is not just a caching issue on the NAOT side which should just be fixed by not caching that result for different instances?
Because the caching is a long standing expectation in the .NET type system. Casting results have been cached for many years and the type system and VM/JIT, in general, assumes once a type cast question is asked it is the same for the lifetime of the type. There are many optimization based around this and __ComObject had affordances for that. As you can see, native AOT leveraged these assumptions heavily.
The bug is that IDynamicInterfaceCastable is breaking a core invariant here that native AOT is relying upon. It would appear that CoreCLR has been more lax than it should be.
Bear with me, I'm not sure I'm following. I remember talking about this with @jkoritzinsky a bunch of times (as I wanted to make sure to implement things correctly in CsWinRT), and he specifically said that the contract for IDynamicInterfaceCastable is exactly:
GetInterfaceImplementationmust be invariant for the type
IsInterfaceImplementedmust be invariant for the instance
So like doesn't that mean this bug is just by definition an implementation bug in NAOT? (e.g. CoreCLR works fine)
As in, why are you saying that it's IDynamicInterfaceCastable that's breaking an invariant, and not NAOT?
.NET is a statically typed ecosystem. This means that what a type is and can be must be known statically and hence remain that way. There is latitude for __ComObject, the root of all COM objects, because that must have a bit of wiggle room or else COM interop would be very limited in the original built-in interop solution. The static nature of this means that a type's castable options, in the non-interop world, must be static and when asked "do you implement X" whatever the result, the type system can assume the result is static for the lifetime of the .NET process. Otherwise, what is the point of having a static type system if you can't optimize for that case?
The __ComObject must violate this for interop reasons, but that doesn't or rather shouldn't be expected to extend and pessimize non-interop scenarios where we don't need that freedom. ComObject needs to be treated the same as __ComObject at a minimum.
It would appear that we were a bit free in CoreCLR and it likely has resulted in us not following these rules as closely as we should. However, native AOT can be much clearer about this and it would seem they have. Meaning static type system allow optimizations based on that invariant. Since native AOT never supported __ComObject this invariant likely didn't matter much, but with ComObject it appears we need to update native AOT on that. The point though is casting is not and never has been predicated on normal .NET instances, it is based on .NET types.
I'll defer to @davidwrighton for the rest of this though because he is the type system expert so perhaps there is some subtle affordance for IDynamicInterfaceCastable, but that is not my understanding. I'm not saying it shouldn't be extended to that interface, but ComObject is an easy argument to make. Arbitrary uses of IDynamicInterfaceCastable are much hard to justify in my opinion.
@AaronRobinsonMSFT we already have a bunch of cases in ILC and NativeAOT to ensure that we don't make invalid optimizations (like devirtualization) in the presence of IDIC.
To that point, we even have a mechanism in ILC to determine if a type cast could have a valid result from IDIC.GetInterfaceImplementation.
The way I read this is definitely as a bug in NativeAOT's cast cache vs CoreCLR's per the design of IDIC.
This isn't just for ComObject, but for any type that implements IDIC.
we already have a bunch of cases in ILC and NativeAOT to ensure that we don't make invalid optimizations (like devirtualization) in the presence of IDIC.
Great. If native AOT already handle this logic, sounds good. Aligns precisely with what I said in #125577 (comment), we already have a pattern.
@AaronRobinsonMSFT we have an invariant on IDynamicInterfaceCastable that whenever type implements IDynamicInterfaceCastable that for a given interfaceTypeIDynamicInterfaceCastable.GetInterfaceImplementation must return a result which is semantically identical to any other calls. In addition, given an individual object IDynamicInterfaceCastable.IsInterfaceImplemented must return the same value. However, there is no requirement that IDynamicInterfaceCastable.IsInterfaceImplemented return the same value for different objects.
Activity
dotnet-policy-service commentedon Mar 15, 2026
Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.
AaronRobinsonMSFT commentedon Mar 15, 2026
We're going to need to work with @davidwrighton and @MichalStrehovsky on how best to handle this in native AOT. Adding @jkotas in case he has thoughts.
The root issue here is that COM is going to have cases where some types answer the question differently. Historically this was limited to
__ComObjectonly, but now anything deriving from the publicComObject(source) can fall into the same bucket.Understanding how to special case that across all runtimes is the next step.
Sergio0694 commentedon Mar 15, 2026
Dumb question: isn't the issue here just that NAOT is caching the
IsInterfaceImplementedresult for multiple instances, which it shouldn't (becauseIsInterfaceImplementedis explicitly per-instance, unlikeGetInterfaceImplementation)? As in, can you clarify why the behavior here fromComObjector any other type in particular is relevant, and why this is not just a caching issue on the NAOT side which should just be fixed by not caching that result for different instances?AaronRobinsonMSFT commentedon Mar 15, 2026
Because the caching is a long standing expectation in the .NET type system. Casting results have been cached for many years and the type system and VM/JIT, in general, assumes once a type cast question is asked it is the same for the lifetime of the type. There are many optimization based around this and
__ComObjecthad affordances for that. As you can see, native AOT leveraged these assumptions heavily.The bug is that
IDynamicInterfaceCastableis breaking a core invariant here that native AOT is relying upon. It would appear that CoreCLR has been more lax than it should be.Sergio0694 commentedon Mar 15, 2026
Bear with me, I'm not sure I'm following. I remember talking about this with @jkoritzinsky a bunch of times (as I wanted to make sure to implement things correctly in CsWinRT), and he specifically said that the contract for
IDynamicInterfaceCastableis exactly:GetInterfaceImplementationmust be invariant for the typeIsInterfaceImplementedmust be invariant for the instanceSo like doesn't that mean this bug is just by definition an implementation bug in NAOT? (e.g. CoreCLR works fine)
As in, why are you saying that it's
IDynamicInterfaceCastablethat's breaking an invariant, and not NAOT?AaronRobinsonMSFT commentedon Mar 15, 2026
.NET is a statically typed ecosystem. This means that what a type is and can be must be known statically and hence remain that way. There is latitude for
__ComObject, the root of all COM objects, because that must have a bit of wiggle room or else COM interop would be very limited in the original built-in interop solution. The static nature of this means that a type's castable options, in the non-interop world, must be static and when asked "do you implement X" whatever the result, the type system can assume the result is static for the lifetime of the .NET process. Otherwise, what is the point of having a static type system if you can't optimize for that case?The
__ComObjectmust violate this for interop reasons, but that doesn't or rather shouldn't be expected to extend and pessimize non-interop scenarios where we don't need that freedom.ComObjectneeds to be treated the same as__ComObjectat a minimum.It would appear that we were a bit free in CoreCLR and it likely has resulted in us not following these rules as closely as we should. However, native AOT can be much clearer about this and it would seem they have. Meaning static type system allow optimizations based on that invariant. Since native AOT never supported
__ComObjectthis invariant likely didn't matter much, but withComObjectit appears we need to update native AOT on that. The point though is casting is not and never has been predicated on normal .NET instances, it is based on .NET types.I'll defer to @davidwrighton for the rest of this though because he is the type system expert so perhaps there is some subtle affordance for
IDynamicInterfaceCastable, but that is not my understanding. I'm not saying it shouldn't be extended to that interface, butComObjectis an easy argument to make. Arbitrary uses ofIDynamicInterfaceCastableare much hard to justify in my opinion.jkoritzinsky commentedon Mar 15, 2026
@AaronRobinsonMSFT we already have a bunch of cases in ILC and NativeAOT to ensure that we don't make invalid optimizations (like devirtualization) in the presence of IDIC.
To that point, we even have a mechanism in ILC to determine if a type cast could have a valid result from IDIC.GetInterfaceImplementation.
The way I read this is definitely as a bug in NativeAOT's cast cache vs CoreCLR's per the design of IDIC.
This isn't just for ComObject, but for any type that implements IDIC.
AaronRobinsonMSFT commentedon Mar 15, 2026
Great. If native AOT already handle this logic, sounds good. Aligns precisely with what I said in #125577 (comment), we already have a pattern.
Do not store result of cast in IDynIntfCastable scenarios (#125605)
davidwrighton commentedon Mar 17, 2026
@AaronRobinsonMSFT we have an invariant on IDynamicInterfaceCastable that whenever type implements IDynamicInterfaceCastable that for a given
interfaceTypeIDynamicInterfaceCastable.GetInterfaceImplementationmust return a result which is semantically identical to any other calls. In addition, given an individual objectIDynamicInterfaceCastable.IsInterfaceImplementedmust return the same value. However, there is no requirement thatIDynamicInterfaceCastable.IsInterfaceImplementedreturn the same value for different objects.