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 }}
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Nov 13, 2026. It is now read-only.
Constrained method calls use wrong generic context #3608
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.
usingSystem;usingSystem.Runtime.CompilerServices;internalclassProgram{classAtom{}interfaceIFoo<T>{objectFrob(objecto);}structFoo<T>:IFoo<T>whereT:class{publicintFrobbedValue;publicobjectFrob(objecto){FrobbedValue=12345;returnoasT;}}[MethodImpl(MethodImplOptions.NoInlining)]staticobjectDoFrob<T,U>(refTt,objecto)whereT:IFoo<U>{// Perform a constrained interface call from shared code.// This should have been resolved to a direct call at compile time.returnt.Frob(o);}privatestaticintMain(string[]args){varfoo=newFoo<Atom>();objectresult=DoFrob<Foo<Atom>,Atom>(reffoo,"Not atom");// If the FrobbedValue doesn't change when we frob, we must have done box+interface call.if(foo.FrobbedValue!=12345)thrownewException();// We passed a string, not an Atomif(result!=null)thrownewException();varatom=newAtom();result=DoFrob<Foo<Atom>,Atom>(reffoo,atom);if(result!=atom)thrownewException();return100;}}
classTestConstrainedMethodCalls{interfaceIFoo<T>{voidFrob(intx);}structFoo<T>:IFoo<T>{publicintFrobbedValue;publicvoidFrob(intx){FrobbedValue=12345;}}[MethodImpl(MethodImplOptions.NoInlining)]staticvoidDoFrob<T,U>(refTt)whereT: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);}publicstaticvoidMain(){varfoo=newFoo<object>();DoFrob<Foo<object>,object>(reffoo);// If the FrobbedValue doesn't change when we frob, we must have done box+interface call.if(foo.FrobbedValue!=12345)thrownewException();}}
CoreCLR seems to be generating a runtime lookup for an instantiating stub in DoFrob and calling that:
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.
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.
My RyuJIT fix didn't actually fix this for the case of generic virtual method:
usingSystem;usingSystem.Runtime.CompilerServices;classTestConstrainedMethodCalls{classAtom1{}classAtom2{}interfaceIFoo<T>{boolFrob(objecto);}structFoo<T>:IFoo<T>{publicintFrobbedValue;publicboolFrob(objecto){FrobbedValue=12345;returnoisT[,,];}}interfaceIBar<T>{boolFrob<U>(objecto);}structBar<T>:IBar<T>{publicintFrobbedValue;publicboolFrob<U>(objecto){FrobbedValue=5678;returnoisT[,,,];}}[MethodImpl(MethodImplOptions.NoInlining)]staticboolDoFrob1<T,U>(refTt,objecto)whereT:IFoo<U>{// Perform a constrained interface call from shared code.// This should have been resolved to a direct call at compile time.returnt.Frob(o);}[MethodImpl(MethodImplOptions.NoInlining)]staticboolDoFrob2<T,U>(refTt,objecto)whereT:IBar<U>{// Perform a constrained interface call from shared code.// This should have been resolved to a direct call at compile time.returnt.Frob<U>(o);}publicstaticvoidMain(){{varfoo1=newFoo<Atom1>();boolresult=DoFrob1<Foo<Atom1>,Atom1>(reffoo1,newAtom1[0,0,0]);// If the FrobbedValue doesn't change when we frob, we must have done box+interface call.if(foo1.FrobbedValue!=12345)thrownewException();// Also check we passed the right generic context to Foo.Frobif(!result)thrownewException();// Also check dependency analysis:// EEType for Atom2[,,] that we'll check for was never allocated.varfoo2=newFoo<Atom2>();if(DoFrob1<Foo<Atom2>,Atom2>(reffoo2,newobject()))thrownewException();}{varbar1=newBar<Atom1>();boolresult=DoFrob2<Bar<Atom1>,Atom1>(refbar1,newAtom1[0,0,0,0]);// If the FrobbedValue doesn't change when we frob, we must have done box+interface call.if(bar1.FrobbedValue!=5678)thrownewException();// Also check we passed the right generic context to Foo.Frobif(!result)thrownewException();// Also check dependency analysis:// EEType for Atom2[,,,] that we'll check for was never allocated.varbar2=newBar<Atom2>();if(DoFrob2<Bar<Atom2>,Atom2>(refbar2,newobject()))thrownewException();}}}
Activity
MichalStrehovsky commentedon Jun 13, 2017
So I'm playing with a slightly simplified repro:
CoreCLR seems to be generating a runtime lookup for an instantiating stub in
DoFroband calling that: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.
Fix up IL scanner
Fix up IL scanner (#3885)
MichalStrehovsky commentedon Jun 14, 2017
When resolving this bug, look for the issue number in the sources. We have some workarounds in place.
MichalStrehovsky commentedon Aug 25, 2017
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.
Update RyuJIT (#4407)
MichalStrehovsky commentedon Aug 29, 2017
Let's keep this open.
My RyuJIT fix didn't actually fix this for the case of generic virtual method:
We need a new JitInterface method after all.
MichalStrehovsky commentedon Nov 29, 2017
We also need to make sure dependencies are right. This needs to pass:
MichalStrehovsky commentedon Aug 31, 2019
Also look at the reference to this issue in the DynamicGenerics\GenericVirtualMethods test.
Fix handling of constrained calls on ValueTypes
Fix handling of constrained calls on ValueTypes (#1422)