Skip to content

Commit 10b262b

Browse files
authored
Fix issue with covariant return method validation (#41703)
The ClassLoader::ValidateMethodsWithCovariantReturnTypes was being called from a place where the relevant types were sometimes not fully loaded yet. That resulted in an attempt to load the type being loaded recursively, which was detected and the TypeLoadException was thrown. This change fixes it by moving the call to the ClassLoader::ValidateMethodsWithCovariantReturnTypes to the PushFinalLevels when the level is greater than CLASS_LOAD_EXACTPARENTS, which is the last stage of normal loading.
1 parent 3a94b2c commit 10b262b

File tree

5 files changed

+158
-5
lines changed

5 files changed

+158
-5
lines changed

src/coreclr/src/vm/class.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -963,8 +963,6 @@ void ClassLoader::LoadExactParents(MethodTable *pMT)
963963

964964
MethodTableBuilder::CopyExactParentSlots(pMT, pApproxParentMT);
965965

966-
ValidateMethodsWithCovariantReturnTypes(pMT);
967-
968966
// We can now mark this type as having exact parents
969967
pMT->SetHasExactParent();
970968

src/coreclr/src/vm/clsload.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3519,11 +3519,15 @@ static void PushFinalLevels(TypeHandle typeHnd, ClassLoadLevel targetLevel, cons
35193519
}
35203520
CONTRACTL_END
35213521

3522-
35233522
// This phase brings the type and all its transitive dependencies to their
35243523
// final state, sans the IsFullyLoaded bit.
35253524
if (targetLevel >= CLASS_DEPENDENCIES_LOADED)
35263525
{
3526+
if (!typeHnd.IsTypeDesc())
3527+
{
3528+
ClassLoader::ValidateMethodsWithCovariantReturnTypes(typeHnd.AsMethodTable());
3529+
}
3530+
35273531
BOOL fBailed = FALSE;
35283532
typeHnd.DoFullyLoad(NULL, CLASS_DEPENDENCIES_LOADED, NULL, &fBailed, pInstContext);
35293533
}

src/coreclr/src/vm/clsload.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,8 @@ class ClassLoader
943943
TypeHandle LoadTypeHandleThrowing(NameHandle* pName, ClassLoadLevel level = CLASS_LOADED,
944944
Module* pLookInThisModuleOnly=NULL);
945945

946+
static void ValidateMethodsWithCovariantReturnTypes(MethodTable* pMT);
947+
946948
private:
947949

948950
#ifndef DACCESS_COMPILE
@@ -969,8 +971,6 @@ class ClassLoader
969971

970972
static void LoadExactParentAndInterfacesTransitively(MethodTable *pMT);
971973

972-
static void ValidateMethodsWithCovariantReturnTypes(MethodTable* pMT);
973-
974974
static bool IsCompatibleWith(TypeHandle hType1, TypeHandle hType2);
975975
static CorElementType GetReducedTypeElementType(TypeHandle hType);
976976
static CorElementType GetVerificationTypeElementType(TypeHandle hType);
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
2+
// Licensed to the .NET Foundation under one or more agreements.
3+
// The .NET Foundation licenses this file to you under the MIT license.
4+
5+
// Metadata version: v4.0.30319
6+
.assembly extern System.Runtime
7+
{
8+
.publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A ) // .?_....:
9+
.ver 5:0:0:0
10+
}
11+
.assembly '41571'
12+
{
13+
.custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
14+
.custom instance void [System.Runtime]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78 // ....T..WrapNonEx
15+
63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 ) // ceptionThrows.
16+
17+
// --- The following custom attribute is added automatically, do not uncomment -------
18+
// .custom instance void [System.Runtime]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [System.Runtime]System.Diagnostics.DebuggableAttribute/DebuggingModes) = ( 01 00 07 01 00 00 00 00 )
19+
20+
.custom instance void [System.Runtime]System.Runtime.Versioning.TargetFrameworkAttribute::.ctor(string) = ( 01 00 18 2E 4E 45 54 43 6F 72 65 41 70 70 2C 56 // ....NETCoreApp,V
21+
65 72 73 69 6F 6E 3D 76 35 2E 30 01 00 54 0E 14 // ersion=v5.0..T..
22+
46 72 61 6D 65 77 6F 72 6B 44 69 73 70 6C 61 79 // FrameworkDisplay
23+
4E 61 6D 65 00 ) // Name.
24+
.custom instance void [System.Runtime]System.Reflection.AssemblyCompanyAttribute::.ctor(string) = ( 01 00 05 34 31 35 37 31 00 00 ) // ...41571..
25+
.custom instance void [System.Runtime]System.Reflection.AssemblyConfigurationAttribute::.ctor(string) = ( 01 00 05 44 65 62 75 67 00 00 ) // ...Debug..
26+
.custom instance void [System.Runtime]System.Reflection.AssemblyFileVersionAttribute::.ctor(string) = ( 01 00 07 31 2E 30 2E 30 2E 30 00 00 ) // ...1.0.0.0..
27+
.custom instance void [System.Runtime]System.Reflection.AssemblyInformationalVersionAttribute::.ctor(string) = ( 01 00 05 31 2E 30 2E 30 00 00 ) // ...1.0.0..
28+
.custom instance void [System.Runtime]System.Reflection.AssemblyProductAttribute::.ctor(string) = ( 01 00 05 34 31 35 37 31 00 00 ) // ...41571..
29+
.custom instance void [System.Runtime]System.Reflection.AssemblyTitleAttribute::.ctor(string) = ( 01 00 05 34 31 35 37 31 00 00 ) // ...41571..
30+
.hash algorithm 0x00008004
31+
.ver 1:0:0:0
32+
}
33+
.module '41571.dll'
34+
// MVID: {7F828A34-F3E8-43DB-A60B-950719B2314B}
35+
.imagebase 0x00400000
36+
.file alignment 0x00000200
37+
.stackreserve 0x00100000
38+
.subsystem 0x0003 // WINDOWS_CUI
39+
.corflags 0x00000001 // ILONLY
40+
// Image base: 0x000001926B4D0000
41+
42+
43+
// =============== CLASS MEMBERS DECLARATION ===================
44+
45+
.class private auto ansi beforefieldinit GitHub_41571.Program
46+
extends [System.Runtime]System.Object
47+
{
48+
.method private hidebysig static int32
49+
Main() cil managed
50+
{
51+
.entrypoint
52+
// Code size 19 (0x13)
53+
.maxstack 1
54+
.locals init (int32 V_0)
55+
IL_0000: nop
56+
IL_0001: newobj instance void GitHub_41571.Base::.ctor()
57+
IL_0006: call instance class GitHub_41571.Derived GitHub_41571.Base::CrashInCallingScope()
58+
IL_000b: pop
59+
IL_000c: ldc.i4.s 100
60+
IL_000e: stloc.0
61+
IL_000f: br.s IL_0011
62+
63+
IL_0011: ldloc.0
64+
IL_0012: ret
65+
} // end of method Program::Main
66+
67+
.method public hidebysig specialname rtspecialname
68+
instance void .ctor() cil managed
69+
{
70+
// Code size 8 (0x8)
71+
.maxstack 8
72+
IL_0000: ldarg.0
73+
IL_0001: call instance void [System.Runtime]System.Object::.ctor()
74+
IL_0006: nop
75+
IL_0007: ret
76+
} // end of method Program::.ctor
77+
78+
} // end of class GitHub_41571.Program
79+
80+
.class private auto ansi beforefieldinit GitHub_41571.Base
81+
extends [System.Runtime]System.Object
82+
{
83+
.method public hidebysig instance class GitHub_41571.Derived
84+
CrashInCallingScope() cil managed
85+
{
86+
// Code size 2 (0x2)
87+
.maxstack 8
88+
IL_0000: ldnull
89+
IL_0001: ret
90+
} // end of method Base::CrashInCallingScope
91+
92+
.method public hidebysig newslot virtual
93+
instance class GitHub_41571.Base
94+
NeededToCrash() cil managed
95+
{
96+
// Code size 2 (0x2)
97+
.maxstack 8
98+
IL_0000: ldnull
99+
IL_0001: ret
100+
} // end of method Base::NeededToCrash
101+
102+
.method public hidebysig specialname rtspecialname
103+
instance void .ctor() cil managed
104+
{
105+
// Code size 8 (0x8)
106+
.maxstack 8
107+
IL_0000: ldarg.0
108+
IL_0001: call instance void [System.Runtime]System.Object::.ctor()
109+
IL_0006: nop
110+
IL_0007: ret
111+
} // end of method Base::.ctor
112+
113+
} // end of class GitHub_41571.Base
114+
115+
.class private auto ansi beforefieldinit GitHub_41571.Derived
116+
extends GitHub_41571.Base
117+
{
118+
.method public hidebysig newslot virtual
119+
instance class GitHub_41571.Derived
120+
NeededToCrash() cil managed
121+
{
122+
.custom instance void [System.Runtime]System.Runtime.CompilerServices.PreserveBaseOverridesAttribute::.ctor() = ( 01 00 00 00 )
123+
.override method instance class GitHub_41571.Base GitHub_41571.Base::NeededToCrash()
124+
// Code size 2 (0x2)
125+
.maxstack 8
126+
IL_0000: ldnull
127+
IL_0001: ret
128+
} // end of method Derived::NeededToCrash
129+
130+
.method public hidebysig specialname rtspecialname
131+
instance void .ctor() cil managed
132+
{
133+
// Code size 8 (0x8)
134+
.maxstack 8
135+
IL_0000: ldarg.0
136+
IL_0001: call instance void GitHub_41571.Base::.ctor()
137+
IL_0006: nop
138+
IL_0007: ret
139+
} // end of method Derived::.ctor
140+
141+
} // end of class GitHub_41571.Derived
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk.IL">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<CLRTestKind>BuildAndRun</CLRTestKind>
5+
<CLRTestPriority>1</CLRTestPriority>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<Compile Include="GitHub_41571.il" />
9+
</ItemGroup>
10+
</Project>

0 commit comments

Comments
 (0)