MethodIterator returns method of an open generic type def when iterating over methods of an instantiated generic type
Affected tests: Loader\classloader\TypeGeneratorTests\TypeGeneratorTest612 Loader\classloader\TypeGeneratorTests\TypeGeneratorTest613 Loader\classloader\TypeGeneratorTests\TypeGeneratorTest614 Loader\classloader\TypeGeneratorTests\TypeGeneratorTest681 Loader\classloader\TypeGeneratorTests\TypeGeneratorTest682 Loader\classloader\TypeGeneratorTests\TypeGeneratorTest683
The bug is in the encoding of a callsite like the following: callvirt instance string class G3_C1084`1<!!T0>::Method4()
In ReadyToRun, crossgen will encode such calls as ENCODE_VIRTUAL_ENTRY_REF_TOKEN entries. This means that at runtime, we'll attempt to load up a MethodDesc from the memberref token of the callsite. The problem is that we don't have a SigTypeContext when processing the memberref, so the type var portion of the signature (!!0) will cause a BadImageFormatException to be thrown.
Compare repro "Generated612" with a similar test in the tree "Generated656" (under Loader\classloader\TypeGeneratorTests\TypeGeneratorTest656). The latter has a similar callsite: callvirt instance string class G3_C1128`1<!!T0>::Method4()
However, the Generated656 test is passing. The similar callsite is encoded by crossgen as a ENCODE_VIRTUAL_ENTRY, with a zapsig encoding for G3_C1128`1<__Canon>::Method4(). This is the correct behavior.
The 6 tests affected by this bug are failing because the callsite is encoded as a ENCODE_VIRTUAL_ENTRY_REF_TOKEN, instead of being a ENCODE_VIRTUAL_ENTRY.
This difference in encoding occurs as a side effect of the result returned by MethodTable::MethodIterator, in MemberLoader::FindMethod(...). In the problematic case, it should return a MethodDesc pointer to the "Method4" method on the __Canon instantiated type, but instead, it returns the method on the open generic type.
The encoding happens in ZapImportTable::GetStubDispatchCell(...): GetCompileInfo()->TryEncodeMethodAsToken(handle, pResolvedToken, &referencingModule) will return a non-nil mdToken, which causes crossgen to encode it as a ENCODE_VIRTUAL_ENTRY_REF_TOKEN.
Tests are currently in a PR (#6709). I'll merge them into master soon
Please don't forget to update issues.targets to re-enable the test cases when fixing the bug
File tests\testsFailingOutsideWindows.txt will also need to be updated to re-enable the tests to run on non-Windows platforms
@rahku Can you PTAL?
@fadimounir Will you be able to help with a fix for this?
@fadimounir Can you help with this?
Unhandled Exception: System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B (COR_E_BADIMAGEFORMAT)) at Framework.M.G3_C1084.T[T0,W](W inst, String exp) at Framework.ConstrainedCallsTest() in D:\GitHub\coreclr\tests\src\Loader\classloader\TypeGeneratorTests\TypeGeneratorTest612\Generated612.il:line 1654 at Framework.Main() in D:\GitHub\coreclr\tests\src\Loader\classloader\TypeGeneratorTests\TypeGeneratorTest612\Generated612.il:line 2756 Expected: 100 Actual: -532462766 END EXECUTION - FAILED
The exception is here:
ldstr "G2_C21::ClassMethod1326.MI.4933()#G2_C21::ClassMethod1327.MI.4934<System.Object>()#G3_C1084::ClassMethod1356.MI.14216<System.Object>()#G3_C1084::ClassMethod3562.14214()#G3_C1084::ClassMethod3563.14215()#G2_C21::Method4.4929()#G2_C21::Method5.4930()#G2_C21::Method6.4931<System.Object>()#G3_C1084::Method7.14212<System.Object>()#"
call void Framework::M.G3_C1084.T<class BaseClass0,class G3_C1084`1<class BaseClass0>>(!!1,string)