Skip to content

Commit f97546b

Browse files
authored
Fixed ILVerify incorrectly flagging valid default interface implementations (#61185)
* Added logic for default interface method traversal to ILVerify method discovery * Added Tests for DefaultImplFix * Moved call to default interface impl resolution outside of ResolveInterfaceMethodTarget
1 parent a2b288d commit f97546b

File tree

4 files changed

+132
-52
lines changed

4 files changed

+132
-52
lines changed

src/coreclr/tools/ILVerification/TypeVerifier.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ public void VerifyInterfaces()
9999
continue;
100100
}
101101

102-
MethodDesc resolvedMethod = type.ResolveInterfaceMethodTarget(method);
102+
if (type.ResolveInterfaceMethodTarget(method) is not MethodDesc resolvedMethod)
103+
{
104+
type.ResolveInterfaceMethodToDefaultImplementationOnType(method, out resolvedMethod);
105+
}
106+
103107
if (resolvedMethod is null)
104108
{
105109
VerificationError(VerifierError.InterfaceMethodNotImplemented, Format(type), Format(implementedInterface.InterfaceType, _module, implementedInterface.InterfaceImplementation), Format(method));
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
.assembly DefaultInterfaceMethod
5+
{
6+
}
7+
8+
.assembly extern System.Runtime
9+
{
10+
}
11+
12+
.class interface private auto ansi abstract IInterface
13+
{
14+
.method public hidebysig newslot abstract virtual
15+
instance void Method () cil managed
16+
{
17+
}
18+
}
19+
20+
.class public auto ansi beforefieldinit ChildClassDoesNotImplementDefaultInterfaceMethod_ValidType_Valid
21+
extends [System.Runtime]System.Object
22+
implements IDefaultImplInterface
23+
{
24+
.method public hidebysig specialname rtspecialname
25+
instance void .ctor () cil managed
26+
{
27+
.maxstack 8
28+
29+
IL_0000: ldarg.0
30+
IL_0001: call instance void [System.Runtime]System.Object::.ctor()
31+
IL_0006: nop
32+
IL_0007: ret
33+
}
34+
}
35+
36+
.class public auto ansi beforefieldinit ChildClassDoesImplementDefaultInterfaceMethod_ValidType_Valid
37+
extends [System.Runtime]System.Object
38+
implements IDefaultImplInterface
39+
{
40+
.method public final hidebysig newslot virtual
41+
instance void DefaultImplementation () cil managed
42+
{
43+
.maxstack 8
44+
45+
IL_0000: ret
46+
}
47+
48+
.method public hidebysig specialname rtspecialname
49+
instance void .ctor () cil managed
50+
{
51+
.maxstack 8
52+
53+
IL_0000: ldarg.0
54+
IL_0001: call instance void [System.Runtime]System.Object::.ctor()
55+
IL_0006: nop
56+
IL_0007: ret
57+
}
58+
}
59+
60+
.class interface public auto ansi abstract IDefaultImplInterface
61+
{
62+
.method public hidebysig newslot virtual
63+
instance void DefaultImplementation () cil managed
64+
{
65+
.maxstack 8
66+
67+
IL_0000: ret
68+
}
69+
}
70+
71+
.class interface public auto ansi abstract IInheritedDefaultImplInterface
72+
implements IInterface
73+
{
74+
.method private final hidebysig virtual
75+
instance void IInterface.Method () cil managed
76+
{
77+
.override method instance void IInterface::Method()
78+
.maxstack 8
79+
80+
IL_0001: ret
81+
}
82+
}
83+
84+
.class public auto ansi beforefieldinit ClassInheritsFromInterfaceWhereContractIsImplementedByDerivedInterface_ValidType_Valid
85+
extends [System.Runtime]System.Object
86+
implements IInheritedDefaultImplInterface, IInterface
87+
{
88+
.method public hidebysig specialname rtspecialname
89+
instance void .ctor () cil managed
90+
{
91+
.maxstack 8
92+
93+
IL_0000: ldarg.0
94+
IL_0001: call instance void [System.Runtime]System.Object::.ctor()
95+
IL_0006: nop
96+
IL_0007: ret
97+
}
98+
}
99+
100+
.class interface public auto ansi abstract IReabstractDefaultImplementation
101+
implements IInheritedDefaultImplInterface, IInterface
102+
{
103+
.method private final hidebysig abstract virtual
104+
instance void IInterface.Method () cil managed
105+
{
106+
.override method instance void IInterface::Method()
107+
}
108+
}
109+
110+
.class public auto ansi beforefieldinit ChildClassInheritsFromInterfaceWithDefaultImplementationWhereChildInterfaceReabstractsInterfaceMethod_InvalidType_InterfaceMethodNotImplemented
111+
extends [System.Runtime]System.Object
112+
implements IReabstractDefaultImplementation, IInheritedDefaultImplInterface, IInterface
113+
{
114+
.method public hidebysig specialname rtspecialname
115+
instance void .ctor () cil managed
116+
{
117+
.maxstack 8
118+
119+
IL_0000: ldarg.0
120+
IL_0001: call instance void [System.Runtime]System.Object::.ctor()
121+
IL_0006: nop
122+
IL_0007: ret
123+
}
124+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<Import Project="ILTests.targets" />
3+
</Project>

src/tests/ilverify/ILTests/InterfaceImplementation.il

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -448,54 +448,3 @@
448448
{
449449
}
450450
}
451-
452-
.class public auto ansi beforefieldinit ChildClassDoesNotImplementDefaultInterfaceMethod_ValidType_Valid
453-
extends [System.Runtime]System.Object
454-
implements IDefaultImplInterface
455-
{
456-
.method public hidebysig specialname rtspecialname
457-
instance void .ctor () cil managed
458-
{
459-
.maxstack 8
460-
461-
IL_0000: ldarg.0
462-
IL_0001: call instance void [System.Runtime]System.Object::.ctor()
463-
IL_0006: nop
464-
IL_0007: ret
465-
}
466-
}
467-
468-
.class public auto ansi beforefieldinit ChildClassDoesImplementDefaultInterfaceMethod_ValidType_Valid
469-
extends [System.Runtime]System.Object
470-
implements IDefaultImplInterface
471-
{
472-
.method public final hidebysig newslot virtual
473-
instance void DefaultImplementation () cil managed
474-
{
475-
.maxstack 8
476-
477-
IL_0000: ret
478-
}
479-
480-
.method public hidebysig specialname rtspecialname
481-
instance void .ctor () cil managed
482-
{
483-
.maxstack 8
484-
485-
IL_0000: ldarg.0
486-
IL_0001: call instance void [System.Runtime]System.Object::.ctor()
487-
IL_0006: nop
488-
IL_0007: ret
489-
}
490-
}
491-
492-
.class interface public auto ansi abstract IDefaultImplInterface
493-
{
494-
.method public hidebysig newslot virtual
495-
instance void DefaultImplementation () cil managed
496-
{
497-
.maxstack 8
498-
499-
IL_0000: ret
500-
}
501-
}

0 commit comments

Comments
 (0)