Repository navigation
Fix ILVerify crash when a method ends with a conditional branch or switch - #135829
Merged
Merged
Conversation
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.
|
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. |
Contributor
|
Tagging subscribers to this area: @agocke |
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @dotnet/jit-contrib |
This was referenced Oct 15, 2026
Closed
Contributor
Author
|
Thank you for the review and merge! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem: when the last instruction of a method is a conditional branch or
switchwith its targets inside the method, ILVerify throwsIndexOutOfRangeExceptionfromCreateBasicBlock, called by the sharedFindJumpTargetsfor the fall-through block. The CLI stops at that method. This is the case tmat reported on #63227, which still throws onmainafter #122056. The repro is in the linked issue.Fix:
FindJumpTargets(Common/TypeSystem/IL/ILImporter.cs), create the fall-through block only when an instruction follows the branch. Otherwise callReportFallthroughAtEndOfMethod, the hook the import pass already uses when control falls through the end of the method.ReportFallthroughAtEndOfMethodnow also stops verifying the method, asReportInvalidExceptionRegiondoes. Without that, the import pass would look up the same block atILImporter.cs:474,:494and:506.This follows the review of #112082, an earlier attempt at the same fix that was closed for inactivity. There, jkotas suggested aborting at
ReportFallthroughAtEndOfMethodrather 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 andswitch.Effect on each importer:
MethodFallthroughfor these methods, at offset 0, sinceFindJumpTargetsruns before ILVerify tracks instruction offsets. It then continues with the next method.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.ReportFallthroughAtEndOfMethodalready throwsInvalidProgramException(ILImporter.Scanner.cs:1822-1825). For these methods the scanner now throws that instead ofIndexOutOfRangeException.ILScanner.CompileSingleMethodthen gives the method a throwing body, instead of failing withCodeGenerationFailedException. That is from reading the code. I builtILCompiler.Compilerwith 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 earlierret. The valid control adds aretafter the conditional branch. The branch operands are numeric offsets. The project setsOptimizetofalse, because the IL SDK passes-OPTIMIZEto ilasm in Release, which rewrites the long branches as short ones.BrTrueS.LastInstruction_Invalid_MethodFallthroughbrtrue.sBrFalseS.LastInstruction_Invalid_MethodFallthroughbrfalse.sBrTrue.LastInstruction_Invalid_MethodFallthroughbrtrueBlt.LastInstruction_Invalid_MethodFallthroughbltSwitch.LastInstruction_Invalid_MethodFallthroughswitch, one targetBrTrueS.BeforeReturn_Validretafter the samebrtrue.s(control)On
main, the five invalid cases throwIndexOutOfRangeExceptionfromCreateBasicBlock, and the control passes.ILVerification.Tests, on
mainat 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 teston the same project with--no-build:mainunchangedmainunchangedIndexOutOfRangeExceptionILVerifyCLI, built from this branch and from its parent, on the repro from the issue:IndexOutOfRangeException, exit code 1;MethodFallthroughinFallsOffEnd, then theReturnVoiderror inLater; exit code 2.Scope
This changes
FindJumpTargetsin the sharedILImporter.csandReportFallthroughAtEndOfMethodinILImporter.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.