Repository navigation
JIT: check for a conditional branch that falls off the end of the method before linking its false edge - #135827
Merged
jkotas merged 2 commits intoOct 15, 2026
Conversation
…hod before linking its false edge
A method whose last IL instruction is a conditional branch is invalid
IL. fgLinkBasicBlocks rejects it with BADCODE("Fall thru the end of a
method"), but since dotnet#95773 the BBJ_COND case added the false edge to
curBBdesc->Next() before checking curBBdesc->IsLast(). For the last
block Next() is null, so fgAddRefPred asserted (Checked) or dereferenced
it (Release) and the check was never reached. Move the check ahead of
both fgAddRefPred calls, restoring the .NET 8 order of checking before
adding the fall-through edge.
Add an IL test with one method per shape: brtrue.s, brtrue.s in a
two-block method, brfalse.s, long-form brtrue and beq.s.
|
Azure Pipelines: Successfully started running 5 pipeline(s). 11 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: @JulieLeeMSFT, @jakobbotsch |
Member
|
@jkotas Is there any ongoing effort to improve the JITs resilience to invalid IL? (I see a series of open PRs to JIT for that) I thought we didnt handle that in the JIT - Id expect a huge gap between the JIT and ILVerify when it comes to detecting invalid IL. |
jkotas
reviewed
Oct 15, 2026
Member
I think the only other PR in this category is #135602 |
Member
Right. JIT is not hardened against invalid IL - by design. |
Drop Runtime_135825 as requested in review. The PR now contains only the reordered check in fgLinkBasicBlocks.
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.
The IL this affects is invalid: the method's last instruction is a conditional branch, so control can fall off the end of the body. This PR adds no new validation. It makes the existing
BADCODE("Fall thru the end of a method")check infgLinkBasicBlocksreachable again.Since #95773, the
BBJ_CONDcase has added the false edge tocurBBdesc->Next()before checkingcurBBdesc->IsLast(). For the last block,Next()is null, sofgAddRefPredassertedblock != nullptrin a Checked JIT and dereferenced the null block in a Release JIT, before the check could run. Before #95773,BBJ_COND,BBJ_ALWAYSandBBJ_LEAVEshared one case, and thebbNextcheck ran before the fall-through edge was added; .NET 8 throwsInvalidProgramExceptionfor this IL.Change
The
IsLast()check moves to the top of theBBJ_CONDcase, ahead of bothfgAddRefPredcalls. That restores .NET 8's order of checking before the fall-through edge is added. When the conditional branch is not the last block, the check is false and nothing else changes.There is no test: it was dropped in review, since invalid IL is undefined behavior.
Validation
These runs are on linux-x64 at main 56ba1c0, with a Checked runtime and Core_Root from the same commit. Each JIT was dropped into its own copy of Core_Root and run with
corerun.A standalone IL program has five methods that each end with a conditional branch back to an earlier instruction:
brtrue.s,brtrue.sin a method with two blocks,brfalse.s, long-formbrtrueandbeq.s. Each method was compiled withRuntimeHelpers.PrepareMethodthree times, each in its own process, under default settings,DOTNET_TieredCompilation=0andDOTNET_JitMinOpts=1:Assertion failed 'block != nullptr' ... during 'Pre-import'(fgflow.cpp:101)InvalidProgramExceptionInvalidProgramExceptionA
switchin the same position throwsInvalidProgramExceptionbefore and after, and the same loop followed byretcompiles before and after. With the fix, the Checked JIT's dump for the two-block method ends withCOMPILATION FAILED: Fall thru the end of a method (fgbasic.cpp:2742).Other tests, Checked JIT before and after the change:
JIT/IL_Conformance: 112 of 112 passed.JIT/Regression_2, excluding the removed test: 100 passed in both runs. The same two failed in both runs:Runtime_33972andRuntime_34937, whose FileCheck step cannot start on this machine because it needs a shared framework that is not installed.Resolves #135825
Note
This PR was prepared with AI assistance (Anthropic Claude and OpenAI Codex), under my direction. I reviewed it before posting.