Skip to content

.NET AOT caching IDynamicInterfaceCastable.IsInterfaceImplemented results based on type rather than instance #125577

Closed

Description

@manodasanW

Description

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

Regression?

.NET 8, seems related to #108328

Known Workarounds

No response

Configuration

.NET 10

Other information

No response

Activity

  1. added
    untriaged
    on Mar 15, 2026
  2. dotnet-policy-service commented on Mar 15, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/interop-contrib
    See info in area-owners.md if you want to be subscribed.

  3. added
    partner-impact
    on Mar 15, 2026
  4. AaronRobinsonMSFT commented on Mar 15, 2026

    @AaronRobinsonMSFT
    Member

    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 __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.

  5. Sergio0694 commented on Mar 15, 2026

    @Sergio0694
    Contributor

    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?

  6. AaronRobinsonMSFT commented on Mar 15, 2026

    @AaronRobinsonMSFT
    Member

    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.

  7. Sergio0694 commented on Mar 15, 2026

    @Sergio0694
    Contributor

    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:

    • GetInterfaceImplementation must be invariant for the type
    • IsInterfaceImplemented must 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?

  8. AaronRobinsonMSFT commented on Mar 15, 2026

    @AaronRobinsonMSFT
    Member

    .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.

  9. jkoritzinsky commented on Mar 15, 2026

    @jkoritzinsky
    Member

    @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.

  10. AaronRobinsonMSFT commented on Mar 15, 2026

    @AaronRobinsonMSFT
    Member

    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.

  11. added a commit that references this issue on Mar 16, 2026
    b954bd7
  12. removed
    untriaged
    on Mar 16, 2026
  13. davidwrighton commented on Mar 17, 2026

    @davidwrighton
    Member

    @AaronRobinsonMSFT we have an invariant on IDynamicInterfaceCastable that whenever type implements IDynamicInterfaceCastable that for a given interfaceType IDynamicInterfaceCastable.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.

  14. locked and limited conversation to collaborators on Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Runtime.InteropServicespartner-impact

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions