Skip to content

Fix ILVerify crash on delegate construction - #135445

Merged
jkotas merged 1 commit into
dotnet:mainfrom
pcshrosbree:fix-ilverify-delegate-ctor-nre
Oct 8, 2026
Merged

jkotas merged 1 commit into
dotnet:mainfrom
pcshrosbree:fix-ilverify-delegate-ctor-nre

Conversation

@pcshrosbree

Copy link
Copy Markdown
Contributor

Problem: when a delegate constructor's function pointer is a stack value that carries no method, such as a native int argument, local or constant rather than ldftn/ldvirtftn, ILVerify throws NullReferenceException in IsDelegateAssignable. That stops the CLI from completing the assembly: later methods and the type pass are not verified. The repro is in the linked issue.

Fix: skip the assignability check unless actualFtn.IsMethod, mirroring the Check(actualFtn.IsMethod, VerifierError.StackMethod) a few lines above. StackMethod has already been reported for such a value, and CheckDelegateCreation reports DelegatePattern. There is no target signature to compare, so the method fails with those two errors and verification continues. IsDelegateAssignable keeps its non-null precondition, and its only caller is this path.

Validation

New test in FtnTests.il, alongside the other NewObj.* cases: NewObj.DelegateFromNativeIntArgument_Invalid_StackMethod.DelegatePattern passes ldarg.0 (a native int) as the function pointer.

ILVerification.Tests, on main at dca76ff, with linux-x64 and SDK 11.0.100-rc.1.26420.103. Each row ran ./dotnet.sh build src/coreclr/tools/ILVerification.Tests/ILVerification.Tests.csproj -c <config>, then the same command with /t:Test:

Tree Configuration Result
main unchanged Release 384/384
this change Release 385/385
this change Debug 385/385
this change Checked 385/385
new test with the fix reverted Release 384/385; the new test fails with the NullReferenceException

The rows for this change (Release, Debug, Checked) and the reverted-fix row were repeated on win-x64 from a fresh clone, with the same results.

ILVerify CLI, built on linux-x64 from this branch and from its parent. The repro and the small assembly were run on linux-x64, osx-arm64 and win-x64; DotNext on linux-x64:

  • Repro: before, NullReferenceException. After, StackMethod and DelegatePattern at offset 2, and the run completes.
  • A small assembly holding argument, local, constant and dup-prefixed variants of the crashing shape; a ldftn carried across a branch; a plain ldftn delegate; and later methods with a ReturnVoid error and a DelegateCtor signature mismatch:
    • before: the run throws on the 2nd of its 10 methods;
    • after: all 10 methods and the type pass are processed;
    • each variant reports StackMethod + DelegatePattern, and the branch case DelegatePattern;
    • the plain delegate stays clean, and the later ReturnVoid and DelegateCtor errors are reported.
  • DotNext 5.26.3 (lib/net8.0/DotNext.dll): before, it throws while verifying the 94th method. After, all 479 types and 3,429 methods are processed, with 368 errors reported.

Corpus check (linux-x64). CLI output before and after this change, over 646 assemblies: Microsoft.NETCore.App 10.0.12 and 11.0.0-preview.7, Microsoft.AspNetCore.App 10.0.12, and 152 NuGet package assemblies. The only output that changed is DotNext.dll, from the crash to a completed run.

Scope

This changes only the newobj delegate path in ImportCall, and adds the test.

Resolves #135444

Note

This change and description were prepared with AI assistance (Anthropic Claude and OpenAI Codex), under my direction. I reviewed them before posting.

When a delegate constructor's function pointer is a stack value that
carries no method (for example a native int argument, local or constant
rather than the result of ldftn/ldvirtftn), IsDelegateAssignable
dereferenced the null method and threw NullReferenceException, which
stopped verification of the rest of the assembly. StackMethod and
DelegatePattern are already reported for that value, so skip the
assignability check unless the value carries a method, and add a
regression test.
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 8, 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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke
See info in area-owners.md if you want to be subscribed.

@jkotas jkotas added area-Tools-ILVerification Issues related to ilverify tool and IL verification in general and removed area-VM-coreclr labels Oct 8, 2026

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks

@jkotas

jkotas commented Oct 8, 2026

Copy link
Copy Markdown
Member

/ba-g infrastructure timeout

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@jkotas
jkotas merged commit 01b76b8 into dotnet:main Oct 8, 2026
150 of 152 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 13, 2026
@pcshrosbree

Copy link
Copy Markdown
Contributor Author

Thank you for the review and merge!

@github-actions github-actions Bot locked and limited conversation to collaborators Nov 12, 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 community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants