Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
This repository was archived by the owner on Nov 13, 2026. It is now read-only.

Constrained method calls use wrong generic context #3608

Open

Description

@MichalStrehovsky

Below code will fail at runtime because RyuJIT generates code where in DoFrob we pass the interface type as generic context instead of Foo and things go bad in Frob.

Need to find the place where CoreCLR makes it do the right thing. Found this by accident because it showed up as a diff in my ILScanner work.

using System;
using System.Runtime.CompilerServices;

internal class Program
{
    class Atom { }

    interface IFoo<T>
    {
        object Frob(object o);
    }

    struct Foo<T> : IFoo<T> where T: class
    {
        public int FrobbedValue;

        public object Frob(object o)
        {
            FrobbedValue = 12345;
            return o as T;
        }
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static object DoFrob<T, U>(ref T t, object o) where T : IFoo<U>
    {
        // Perform a constrained interface call from shared code.
        // This should have been resolved to a direct call at compile time.
        return t.Frob(o);
    }

    private static int Main(string[] args)
    {
        var foo = new Foo<Atom>();
        object result = DoFrob<Foo<Atom>, Atom>(ref foo, "Not atom");

        // If the FrobbedValue doesn't change when we frob, we must have done box+interface call.
        if (foo.FrobbedValue != 12345)
            throw new Exception();

        // We passed a string, not an Atom
        if (result != null)
            throw new Exception();

        var atom = new Atom();
        result = DoFrob<Foo<Atom>, Atom>(ref foo, atom);
        if (result != atom)
            throw new Exception();

        return 100;
    }
}

Activity

  1. MichalStrehovsky commented on Jun 13, 2017

    @MichalStrehovsky
    MemberAuthor

    So I'm playing with a slightly simplified repro:

        class TestConstrainedMethodCalls
        {
            interface IFoo<T>
            {
                void Frob(int x);
            }
    
            struct Foo<T> : IFoo<T>
            {
                public int FrobbedValue;
    
                public void Frob(int x)
                {
                    FrobbedValue = 12345;
                }
            }
    
            [MethodImpl(MethodImplOptions.NoInlining)]
            static void DoFrob<T, U>(ref T t) where T : IFoo<U>
            {
                // Perform a constrained interface call from shared code.
                // This should have been resolved to a direct call at compile time.
                t.Frob(1234);
            }
    
            public static void Main()
            {
                var foo = new Foo<object>();
                DoFrob<Foo<object>, object>(ref foo);
    
                // If the FrobbedValue doesn't change when we frob, we must have done box+interface call.
                if (foo.FrobbedValue != 12345)
                    throw new Exception();
            }
        }

    CoreCLR seems to be generating a runtime lookup for an instantiating stub in DoFrob and calling that:

           55                   push     rbp
           4883EC40             sub      rsp, 64
           488D6C2440           lea      rbp, [rsp+40H]
           48894DF8             mov      qword ptr [rbp-08H], rcx
           48894D10             mov      qword ptr [rbp+10H], rcx
           48895518             mov      bword ptr [rbp+18H], rdx
    
    G_M52391_IG02:
           833D3347EFFF00       cmp      dword ptr [(reloc)], 0
           7405                 je       SHORT G_M52391_IG03
           E8ACEAFE5E           call     CORINFO_HELP_DBG_IS_JUST_MY_CODE
    
    G_M52391_IG03:
           90                   nop
           488B4D10             mov      rcx, qword ptr [rbp+10H]
           488B4938             mov      rcx, qword ptr [rcx+56]
           488B4910             mov      rcx, qword ptr [rcx+16]
           48894DF0             mov      qword ptr [rbp-10H], rcx
           488B4DF0             mov      rcx, qword ptr [rbp-10H]
           4885C9               test     rcx, rcx
           7517                 jne      SHORT G_M52391_IG04
           488B4D10             mov      rcx, qword ptr [rbp+10H]
           48BAE8CFC538FF7F0000 mov      rdx, 0x7FFF38C5CFE8
           E84FE8FF5E           call     CORINFO_HELP_RUNTIMEHANDLE_METHOD
           488945F0             mov      qword ptr [rbp-10H], rax
    
    G_M52391_IG04:
           488B4DF0             mov      rcx, qword ptr [rbp-10H]
           48894DE8             mov      qword ptr [rbp-18H], rcx
           488B4D18             mov      rcx, bword ptr [rbp+18H]
           BAD2040000           mov      edx, 0x4D2
           488B45E8             mov      rax, qword ptr [rbp-18H]
           FFD0                 call     rax
           90                   nop
           90                   nop
    
    G_M52391_IG05:
           488D6500             lea      rsp, [rbp]
           5D                   pop      rbp
           C3                   ret
    

    This is mildly annoying because it seems like we'll need yet another JitInterface change. What we need is a runtime lookup for the owning type after constraint resolution and pass that as the generic context to a canonical (constantly looked up) method.

  2. MichalStrehovsky commented on Jun 14, 2017

    @MichalStrehovsky
    MemberAuthor

    When resolving this bug, look for the issue number in the sources. We have some workarounds in place.

  3. MichalStrehovsky commented on Aug 25, 2017

    @MichalStrehovsky
    MemberAuthor

    This is mildly annoying because it seems like we'll need yet another JitInterface change. What we need is a runtime lookup for the owning type after constraint resolution and pass that as the generic context to a canonical (constantly looked up) method.

    Maybe we can get away with just embedding a generic handle of the constraint itself. I can't find a scenario where that wouldn't work.

  4. added a commit that references this issue on Aug 28, 2017
    94bc691
  5. MichalStrehovsky commented on Aug 29, 2017

    @MichalStrehovsky
    MemberAuthor

    Let's keep this open.

    My RyuJIT fix didn't actually fix this for the case of generic virtual method:

    using System;
    using System.Runtime.CompilerServices;
    
    class TestConstrainedMethodCalls
    {
        class Atom1 { }
        class Atom2 { }
    
        interface IFoo<T>
        {
            bool Frob(object o);
        }
    
        struct Foo<T> : IFoo<T>
        {
            public int FrobbedValue;
    
            public bool Frob(object o)
            {
                FrobbedValue = 12345;
                return o is T[,,];
            }
        }
    
        interface IBar<T>
        {
            bool Frob<U>(object o);
        }
    
        struct Bar<T> : IBar<T>
        {
            public int FrobbedValue;
    
            public bool Frob<U>(object o)
            {
                FrobbedValue = 5678;
                return o is T[,,,];
            }
        }
    
        [MethodImpl(MethodImplOptions.NoInlining)]
        static bool DoFrob1<T, U>(ref T t, object o) where T : IFoo<U>
        {
            // Perform a constrained interface call from shared code.
            // This should have been resolved to a direct call at compile time.
            return t.Frob(o);
        }
    
        [MethodImpl(MethodImplOptions.NoInlining)]
        static bool DoFrob2<T, U>(ref T t, object o) where T : IBar<U>
        {
            // Perform a constrained interface call from shared code.
            // This should have been resolved to a direct call at compile time.
            return t.Frob<U>(o);
        }
    
        public static void Main()
        {
            {
                var foo1 = new Foo<Atom1>();
                bool result = DoFrob1<Foo<Atom1>, Atom1>(ref foo1, new Atom1[0, 0, 0]);
    
                // If the FrobbedValue doesn't change when we frob, we must have done box+interface call.
                if (foo1.FrobbedValue != 12345)
                    throw new Exception();
    
                // Also check we passed the right generic context to Foo.Frob
                if (!result)
                    throw new Exception();
    
                // Also check dependency analysis:
                // EEType for Atom2[,,] that we'll check for was never allocated.
                var foo2 = new Foo<Atom2>();
                if (DoFrob1<Foo<Atom2>, Atom2>(ref foo2, new object()))
                    throw new Exception();
            }
    
            {
                var bar1 = new Bar<Atom1>();
                bool result = DoFrob2<Bar<Atom1>, Atom1>(ref bar1, new Atom1[0, 0, 0, 0]);
    
                // If the FrobbedValue doesn't change when we frob, we must have done box+interface call.
                if (bar1.FrobbedValue != 5678)
                    throw new Exception();
    
                // Also check we passed the right generic context to Foo.Frob
                if (!result)
                    throw new Exception();
    
                // Also check dependency analysis:
                // EEType for Atom2[,,,] that we'll check for was never allocated.
                var bar2 = new Bar<Atom2>();
                if (DoFrob2<Bar<Atom2>, Atom2>(ref bar2, new object()))
                    throw new Exception();
            }
        }
    }

    We need a new JitInterface method after all.

  6. MichalStrehovsky commented on Nov 29, 2017

    @MichalStrehovsky
    MemberAuthor

    We also need to make sure dependencies are right. This needs to pass:

    using System;
    using System.Runtime.CompilerServices;
    
    internal class Program
    {
        class Atom { }
    
        interface IFrobber
        {
            bool DoFrob<T>(object o);
        }
    
        struct Frobber : IFrobber
        {
            public bool HasFrobbedWithT;
    
            public bool DoFrob<T>(object o)
            {
                HasFrobbedWithT = o is T;
                return o is T[,,];
            }
        }
    
        [MethodImpl(MethodImplOptions.NoInlining)]
        static bool CallDoFrob<T, U>(ref T x, object o) where T : IFrobber
        {
            return x.DoFrob<U>(o);
        }
    
        private static void Main(string[] args)
        {
            Frobber f = new Frobber();
            bool result = CallDoFrob<Frobber, Atom>(ref f, new Atom());
            if (result)
                throw new Exception();
            if (!f.HasFrobbedWithT)
                throw new Exception();
        }
    }
  7. MichalStrehovsky commented on Aug 31, 2019

    @MichalStrehovsky
    MemberAuthor

    Also look at the reference to this issue in the DynamicGenerics\GenericVirtualMethods test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions