Skip to content

ILVerify throws NullReferenceException when a delegate constructor's function pointer carries no method #135444

Closed

Description

@pcshrosbree

Problem: when the function pointer passed to a delegate constructor is a stack value that carries no method, ILVerify throws NullReferenceException. Examples are a native int argument, a local or a constant, rather than the result of ldftn/ldvirtftn. The CLI then stops at that method: the remaining methods and the type-verification pass are not processed, so their errors are never reported.

The expected result is a verification error for the method: StackMethod and DelegatePattern.

Repro

Repro.il:

.assembly extern System.Runtime { .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A ) .ver 10:0:0:0 }
.assembly Repro { .ver 1:0:0:0 }
.module Repro.dll

.class public abstract auto ansi sealed beforefieldinit C extends [System.Runtime]System.Object
{
    .method public hidebysig static class [System.Runtime]System.Action Make(object target, native int fn) cil managed
    {
        .maxstack 2
        ldarg.0
        ldarg.1
        newobj instance void [System.Runtime]System.Action::.ctor(object, native int)
        ret
    }
}
$ ilasm -dll -output=Repro.dll Repro.il          # Microsoft.NETCore.ILAsm 10.0.0
$ ilverify Repro.dll -r '<Microsoft.NETCore.App 10.0.12>/*.dll' -s System.Private.CoreLib
Error: Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Internal.IL.ILImporter.IsDelegateAssignable(MethodDesc targetMethod, TypeDesc delegateType, StackValue firstArg) in src/coreclr/tools/ILVerification/ILImporter.Verify.cs:line 1140
   at Internal.IL.ILImporter.ImportCall(ILOpcode opcode, Int32 token) in src/coreclr/tools/ILVerification/ILImporter.Verify.cs:line 1627
   at Internal.IL.ILImporter.ImportBasicBlock(BasicBlock basicBlock)
   ...

Reproduced with ILVerify built from main at dca76ff (on linux-x64, osx-arm64 and win-x64) and at 906f6c7 (on linux-x64). ILImporter.Verify.cs is unchanged between those two commits.

Cause

In ImportCall, the newobj delegate path runs Check(actualFtn.IsMethod, VerifierError.StackMethod), which records the error and continues, and CheckDelegateCreation reports DelegatePattern. The path then calls IsDelegateAssignable(actualFtn.Method, ...). When the stack value carries no method, actualFtn.Method is null, and IsDelegateAssignable dereferences it (targetMethod.Signature).

Impact

The exception escapes the per-method error handling.

A small assembly shows the loss. It has 10 methods: this shape is the 2nd, and later methods contain a real ReturnVoid error and a real DelegateCtor signature mismatch.

  • ilverify throws on the 2nd method, so neither later error is reported.
  • With the null case skipped, all 10 methods and the type pass are processed: the bad delegate constructions report StackMethod + DelegatePattern, and the later ReturnVoid and DelegateCtor errors are reported as well.

The runtime accepts this shape. The JIT compiles the repro method, and on .NET 10.0.12 (linux-x64) and 10.0.11 (win-x64) a delegate constructed this way, with an argument or a local holding the function pointer, can be invoked.

It also reproduces on a published library. DotNext 5.26.3 (lib/net8.0/DotNext.dll) constructs delegates in DotNext.DelegateHelpers.CreateDelegate from function-pointer parameters. ilverify throws while verifying the 94th method. With the null case skipped, it processes all 479 types and 3,429 methods and reports 368 errors.

Configuration

  • ILVerify: built from main at dca76ff with the repo SDK (11.0.100-rc.1.26420.103).
  • Hosts: linux-x64 (Ubuntu 26.04), osx-arm64 (macOS 27.0.1) and win-x64 (Windows build 10.0.26100).
  • References: Microsoft.NETCore.App 10.0.12.

Regression? No. The unguarded IsDelegateAssignable(actualFtn.Method, ...) call dates from #65425 (2022), and release/10.0 has the same call (ILImporter.Verify.cs line 1601).

I have a fix with a regression test, and will open a PR.

Note

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

Activity

  1. added
    untriaged
    on Oct 8, 2026
  2. added
    area-Tools-ILVerification
    on Oct 8, 2026
  3. dotnet-policy-service commented on Oct 8, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @dotnet/jit-contrib
    See info in area-owners.md if you want to be subscribed.

  4. added a commit that references this issue on Oct 8, 2026
    01b76b8
  5. removed
    untriaged
    on Oct 8, 2026
  6. locked and limited conversation to collaborators on Nov 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Tools-ILVerification

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions