Skip to content

Fix ILVerify crash when a method ends with a conditional branch or switch - #135829

Merged
jkotas merged 1 commit into
dotnet:mainfrom
pcshrosbree:fix/ilverify-fallthrough-off-end
Oct 15, 2026
Merged

jkotas merged 1 commit into
dotnet:mainfrom
pcshrosbree:fix/ilverify-fallthrough-off-end

Conversation

@pcshrosbree

Copy link
Copy Markdown
Contributor

Problem: when the last instruction of a method is a conditional branch or switch with its targets inside the method, ILVerify throws IndexOutOfRangeException from CreateBasicBlock, called by the shared FindJumpTargets for the fall-through block. The CLI stops at that method. This is the case tmat reported on #63227, which still throws on main after #122056. The repro is in the linked issue.

Fix:

  • In the shared FindJumpTargets (Common/TypeSystem/IL/ILImporter.cs), create the fall-through block only when an instruction follows the branch. Otherwise call ReportFallthroughAtEndOfMethod, the hook the import pass already uses when control falls through the end of the method.
  • In ILVerify, ReportFallthroughAtEndOfMethod now also stops verifying the method, as ReportInvalidExceptionRegion does. Without that, the import pass would look up the same block at ILImporter.cs:474, :494 and :506.

This follows the review of #112082, an earlier attempt at the same fix that was closed for inactivity. There, jkotas suggested aborting at ReportFallthroughAtEndOfMethod rather than skipping the branch in the import pass, and MichalStrehovsky asked whether the long branch forms needed the same check. This change covers the short and long conditional forms and switch.

Effect on each importer:

  • ILVerify reports MethodFallthrough for these methods, at offset 0, since FindJumpTargets runs before ILVerify tracks instruction offsets. It then continues with the next method.
  • ILVerify, existing cases: when the import pass reports MethodFallthrough (control falls off the end after any other instruction), verification of that method now stops there, so errors in blocks not yet imported are no longer reported for that method. No existing test changes.
  • NativeAOT scanner: its ReportFallthroughAtEndOfMethod already throws InvalidProgramException (ILImporter.Scanner.cs:1822-1825). For these methods the scanner now throws that instead of IndexOutOfRangeException. ILScanner.CompileSingleMethod then gives the method a throwing body, instead of failing with CodeGenerationFailedException. That is from reading the code. I built ILCompiler.Compiler with the change but did not run ilc.

Validation

New tests in a new FallthroughTests.il. In each invalid test, the last instruction branches back to an earlier ret. The valid control adds a ret after the conditional branch. The branch operands are numeric offsets. The project sets Optimize to false, because the IL SDK passes -OPTIMIZE to ilasm in Release, which rewrites the long branches as short ones.

Test Last instruction
BrTrueS.LastInstruction_Invalid_MethodFallthrough brtrue.s
BrFalseS.LastInstruction_Invalid_MethodFallthrough brfalse.s
BrTrue.LastInstruction_Invalid_MethodFallthrough brtrue
Blt.LastInstruction_Invalid_MethodFallthrough blt
Switch.LastInstruction_Invalid_MethodFallthrough switch, one target
BrTrueS.BeforeReturn_Valid ret after the same brtrue.s (control)

On main, the five invalid cases throw IndexOutOfRangeException from CreateBasicBlock, and the control passes.

ILVerification.Tests, on main at 56ba1c0, 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 ./dotnet.sh test on the same project with --no-build:

Tree Configuration Result
main unchanged Release 468/468
main unchanged Debug 468/468
this change Release 474/474
this change Debug 474/474
new tests with the fix reverted Release 469/474; the five invalid cases fail with IndexOutOfRangeException
new tests with the fix reverted Debug 469/474; the same five fail

ILVerify CLI, built from this branch and from its parent, on the repro from the issue:

  • before: IndexOutOfRangeException, exit code 1;
  • after: MethodFallthrough in FallsOffEnd, then the ReturnVoid error in Later; exit code 2.

Scope

This changes FindJumpTargets in the shared ILImporter.cs and ReportFallthroughAtEndOfMethod in ILImporter.Verify.cs, and adds the tests.

Resolves #135828

Note

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

When the last instruction of a method is a conditional branch or
switch, FindJumpTargets created a basic block for the fall-through at
the end of the IL, so an IndexOutOfRangeException escaped. Report the
fall-through through ReportFallthroughAtEndOfMethod instead. The
NativeAOT scanner already throws InvalidProgramException from that
hook; ILVerify now reports MethodFallthrough and stops verifying the
method, as it does for EHClauseOutOfRange.

Add regression tests in a new FallthroughTests.il for brtrue.s,
brfalse.s, brtrue, blt and switch as the last instruction, and a valid
control. The test project disables ilasm optimization so that the long
branch forms stay long in Release.
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 14, 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 15, 2026
@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 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

@pcshrosbree

Copy link
Copy Markdown
Contributor Author

Thank you for the review and merge!

@pcshrosbree
pcshrosbree deleted the fix/ilverify-fallthrough-off-end branch October 15, 2026 03:50
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 19, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 19, 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 IndexOutOfRangeException when a method ends with a conditional branch or switch

2 participants