Skip to content

Fix ILVerify validation of interface MethodImpl bodies - #133670

Merged
jkotas merged 5 commits into
dotnet:mainfrom
pkuyo:fix-ilverify-133500-interface-verify
Oct 13, 2026
Merged

jkotas merged 5 commits into
dotnet:mainfrom
pkuyo:fix-ilverify-133500-interface-verify

Conversation

@pkuyo

@pkuyo pkuyo commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Fixes #133500

Reject non-virtual instance MethodImpl bodies before interface resolution, which can return a virtual base method and hide the invalid mapping.
Use the static resolver for static interface methods to allow valid non-virtual implementations.

And add regression tests.

@github-actions github-actions Bot added the area-Tools-ILVerification Issues related to ilverify tool and IL verification in general label Sep 11, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

MethodImpl validation remains skipped for abstract types, default interface methods, and types with only inherited interfaces.

Review effort: Balanced
Findings: 1 High severity

Open (1)
What changed in this PR

Adds ILVerify validation for invalid interface MethodImpl bodies and supports static interface implementations.

Changes:

  • Adds an InvalidMethodImpl diagnostic.
  • Validates instance/static compatibility and virtual instance bodies.
  • Adds regression IL cases.
File Description
VerifierError.cs Defines the new error code.
TypeVerifier.cs Adds MethodImpl validation and static resolution.
Strings.resx Adds diagnostic text.
InterfaceImplementation.il Adds valid and invalid regression cases.

@pcshrosbree

Copy link
Copy Markdown
Contributor

Thanks for this. I maintain an IL rewriter whose method bodies are gated on ILVerification (type-level checks aren't in that gate yet), so I ran this PR against a corpus.

At f854d22, ilverify output is identical to the PR's parent across 645 assemblies (~173k types): Microsoft.NETCore.App 10.0.12 and 11.0.0-preview.7, Microsoft.AspNetCore.App 10.0.12, and 151 NuGet packages (a 152nd crashes ilverify, unrelated: #135444). A control build that always reports emits ~196k diagnostics there, so the check is exercised, including CoreLib's static-abstract generic-math implementations. The osx-arm64 and win-x64 framework builds agree.

Beyond the PR's own tests, 30 hand-built cases against the CoreCLR 10.0.12 loader: nothing it accepts is flagged; 10 it rejects are newly caught. The misses we found also occur at the parent.

The five cases below exercise the post-resolution check and the Decl == method filter: removing either leaves the existing 391 tests passing but fails the added cases.

Note

This comment was prepared with AI assistance (Anthropic Claude and OpenAI Codex), under my direction. I reviewed it before posting.

Five extra cases for InterfaceImplementation.il (apply on f854d22; 396/396 in Release and Debug)
diff --git a/src/coreclr/tools/ILVerification.Tests/ILTests/InterfaceImplementation.il b/src/coreclr/tools/ILVerification.Tests/ILTests/InterfaceImplementation.il
index 020149f2e0e..147c74df419 100644
--- a/src/coreclr/tools/ILVerification.Tests/ILTests/InterfaceImplementation.il
+++ b/src/coreclr/tools/ILVerification.Tests/ILTests/InterfaceImplementation.il
@@ -538,3 +538,125 @@
         ret
     }
 }
+
+.class public auto ansi abstract beforefieldinit AbstractBaseNonVirtualMethodImpl
+    extends [System.Runtime]System.Object
+    implements IInterface
+{
+    .method public hidebysig instance void InterfaceMethod() cil managed
+    {
+        .override method instance void IInterface::InterfaceMethod()
+        .maxstack 0
+        ret
+    }
+}
+
+.class public auto ansi beforefieldinit InheritedNonVirtualMethodImpl_InvalidType_InvalidMethodImpl
+    extends AbstractBaseNonVirtualMethodImpl
+    implements IInterface
+{
+}
+
+.class interface public auto ansi abstract IMixedStaticInstance
+{
+    .method public hidebysig newslot abstract virtual instance void M() cil managed
+    {
+    }
+
+    .method public hidebysig newslot abstract virtual static void M(int32 x) cil managed
+    {
+    }
+}
+
+.class public auto ansi beforefieldinit SameNameStaticAndInstanceMethods_ValidType_Valid
+    extends [System.Runtime]System.Object
+    implements IMixedStaticInstance
+{
+    .method private final hidebysig newslot virtual instance void M() cil managed
+    {
+        .override method instance void IMixedStaticInstance::M()
+        .maxstack 0
+        ret
+    }
+
+    .method private hidebysig static void M(int32 x) cil managed
+    {
+        .override method void IMixedStaticInstance::M(int32)
+        .maxstack 0
+        ret
+    }
+}
+
+.class interface public auto ansi abstract IOverloadedInterface
+{
+    .method public hidebysig newslot abstract virtual instance void M() cil managed
+    {
+    }
+
+    .method public hidebysig newslot abstract virtual instance void M(int32 x) cil managed
+    {
+    }
+}
+
+.class public auto ansi beforefieldinit OneOverloadNonVirtual_InvalidType_InvalidMethodImpl
+    extends [System.Runtime]System.Object
+    implements IOverloadedInterface
+{
+    .method private final hidebysig newslot virtual instance void M() cil managed
+    {
+        .override method instance void IOverloadedInterface::M()
+        .maxstack 0
+        ret
+    }
+
+    .method private hidebysig instance void M(int32 x) cil managed
+    {
+        .override method instance void IOverloadedInterface::M(int32)
+        .maxstack 0
+        ret
+    }
+}
+
+.class interface public auto ansi abstract IGenericInterface`1<T>
+{
+    .method public hidebysig newslot abstract virtual instance void M(!T x) cil managed
+    {
+    }
+}
+
+.class public auto ansi beforefieldinit GenericBaseClass`1<T>
+    extends [System.Runtime]System.Object
+    implements class IGenericInterface`1<!T>
+{
+    .method public hidebysig newslot virtual instance void M(!T x) cil managed
+    {
+        .maxstack 0
+        ret
+    }
+}
+
+.class public auto ansi beforefieldinit GenericNonVirtualMethodHidesBaseVirtualMethod_InvalidType_InvalidMethodImpl<T>
+    extends class GenericBaseClass`1<!T>
+    implements class IGenericInterface`1<!T>
+{
+    .method public hidebysig instance void M(!T x) cil managed
+    {
+        .override method instance void class IGenericInterface`1<!T>::M(!0)
+        .maxstack 0
+        ret
+    }
+}
+
+.class public sequential ansi sealed beforefieldinit ValueTypeNonVirtualMethod_InvalidType_InvalidMethodImpl
+    extends [System.Runtime]System.ValueType
+    implements IInterface
+{
+    .field private int32 _value
+
+    .method public hidebysig instance void InterfaceMethod() cil managed
+    {
+        .override method instance void IInterface::InterfaceMethod()
+        .maxstack 0
+        ret
+    }
+}

@pkuyo

pkuyo commented Oct 11, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for this. I maintain an IL rewriter whose method bodies are gated on ILVerification (type-level checks aren't in that gate yet), so I ran this PR against a corpus.

At f854d22, ilverify output is identical to the PR's parent across 645 assemblies (~173k types): Microsoft.NETCore.App 10.0.12 and 11.0.0-preview.7, Microsoft.AspNetCore.App 10.0.12, and 151 NuGet packages (a 152nd crashes ilverify, unrelated: #135444). A control build that always reports emits ~196k diagnostics there, so the check is exercised, including CoreLib's static-abstract generic-math implementations. The osx-arm64 and win-x64 framework builds agree.

Beyond the PR's own tests, 30 hand-built cases against the CoreCLR 10.0.12 loader: nothing it accepts is flagged; 10 it rejects are newly caught. The misses we found also occur at the parent.

The five cases below exercise the post-resolution check and the Decl == method filter: removing either leaves the existing 391 tests passing but fails the added cases.

Note

This comment was prepared with AI assistance (Anthropic Claude and OpenAI Codex), under my direction. I reviewed it before posting.

Five extra cases for InterfaceImplementation.il (apply on f854d22; 396/396 in Release and Debug)

diff --git a/src/coreclr/tools/ILVerification.Tests/ILTests/InterfaceImplementation.il b/src/coreclr/tools/ILVerification.Tests/ILTests/InterfaceImplementation.il
index 020149f2e0e..147c74df419 100644
--- a/src/coreclr/tools/ILVerification.Tests/ILTests/InterfaceImplementation.il
+++ b/src/coreclr/tools/ILVerification.Tests/ILTests/InterfaceImplementation.il
@@ -538,3 +538,125 @@
         ret
     }
 }
+
+.class public auto ansi abstract beforefieldinit AbstractBaseNonVirtualMethodImpl
+    extends [System.Runtime]System.Object
+    implements IInterface
+{
+    .method public hidebysig instance void InterfaceMethod() cil managed
+    {
+        .override method instance void IInterface::InterfaceMethod()
+        .maxstack 0
+        ret
+    }
+}
+
+.class public auto ansi beforefieldinit InheritedNonVirtualMethodImpl_InvalidType_InvalidMethodImpl
+    extends AbstractBaseNonVirtualMethodImpl
+    implements IInterface
+{
+}
+
+.class interface public auto ansi abstract IMixedStaticInstance
+{
+    .method public hidebysig newslot abstract virtual instance void M() cil managed
+    {
+    }
+
+    .method public hidebysig newslot abstract virtual static void M(int32 x) cil managed
+    {
+    }
+}
+
+.class public auto ansi beforefieldinit SameNameStaticAndInstanceMethods_ValidType_Valid
+    extends [System.Runtime]System.Object
+    implements IMixedStaticInstance
+{
+    .method private final hidebysig newslot virtual instance void M() cil managed
+    {
+        .override method instance void IMixedStaticInstance::M()
+        .maxstack 0
+        ret
+    }
+
+    .method private hidebysig static void M(int32 x) cil managed
+    {
+        .override method void IMixedStaticInstance::M(int32)
+        .maxstack 0
+        ret
+    }
+}
+
+.class interface public auto ansi abstract IOverloadedInterface
+{
+    .method public hidebysig newslot abstract virtual instance void M() cil managed
+    {
+    }
+
+    .method public hidebysig newslot abstract virtual instance void M(int32 x) cil managed
+    {
+    }
+}
+
+.class public auto ansi beforefieldinit OneOverloadNonVirtual_InvalidType_InvalidMethodImpl
+    extends [System.Runtime]System.Object
+    implements IOverloadedInterface
+{
+    .method private final hidebysig newslot virtual instance void M() cil managed
+    {
+        .override method instance void IOverloadedInterface::M()
+        .maxstack 0
+        ret
+    }
+
+    .method private hidebysig instance void M(int32 x) cil managed
+    {
+        .override method instance void IOverloadedInterface::M(int32)
+        .maxstack 0
+        ret
+    }
+}
+
+.class interface public auto ansi abstract IGenericInterface`1<T>
+{
+    .method public hidebysig newslot abstract virtual instance void M(!T x) cil managed
+    {
+    }
+}
+
+.class public auto ansi beforefieldinit GenericBaseClass`1<T>
+    extends [System.Runtime]System.Object
+    implements class IGenericInterface`1<!T>
+{
+    .method public hidebysig newslot virtual instance void M(!T x) cil managed
+    {
+        .maxstack 0
+        ret
+    }
+}
+
+.class public auto ansi beforefieldinit GenericNonVirtualMethodHidesBaseVirtualMethod_InvalidType_InvalidMethodImpl<T>
+    extends class GenericBaseClass`1<!T>
+    implements class IGenericInterface`1<!T>
+{
+    .method public hidebysig instance void M(!T x) cil managed
+    {
+        .override method instance void class IGenericInterface`1<!T>::M(!0)
+        .maxstack 0
+        ret
+    }
+}
+
+.class public sequential ansi sealed beforefieldinit ValueTypeNonVirtualMethod_InvalidType_InvalidMethodImpl
+    extends [System.Runtime]System.ValueType
+    implements IInterface
+{
+    .field private int32 _value
+
+    .method public hidebysig instance void InterfaceMethod() cil managed
+    {
+        .override method instance void IInterface::InterfaceMethod()
+        .maxstack 0
+        ret
+    }
+}

Thanks! I've added all five test cases.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

User-facing diagnostic reason strings must be moved from inline code into resources.

1 open finding
1 resolved since last review

🧠 Review effort: Balanced

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Inherited non-virtual MethodImpl bodies can still be hidden by resolution to a virtual base slot.

0 open findings

1 resolved since last review

🧠 Review effort: Balanced

@jkotas

jkotas commented Oct 13, 2026

Copy link
Copy Markdown
Member

/ba-g known issue #132291

@jkotas
jkotas merged commit ab94f73 into dotnet:main Oct 13, 2026
119 of 122 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 14, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 14, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-Tools-ILVerification Issues related to ilverify tool and IL verification in general

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ILVerify does not flag a non-virtual method bound to an interface slot (TypeLoadException at class load)

4 participants