Skip to content

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 into
dotnet:mainfrom
pcshrosbree:fix/jit-cond-branch-falls-off-end
Oct 15, 2026
Merged

jkotas merged 2 commits into
dotnet:mainfrom
pcshrosbree:fix/jit-cond-branch-falls-off-end

Conversation

@pcshrosbree

@pcshrosbree pcshrosbree commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

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 in fgLinkBasicBlocks reachable again.

Since #95773, the BBJ_COND case has added the false edge to curBBdesc->Next() before checking curBBdesc->IsLast(). For the last block, Next() is null, so fgAddRefPred asserted block != nullptr in a Checked JIT and dereferenced the null block in a Release JIT, before the check could run. Before #95773, BBJ_COND, BBJ_ALWAYS and BBJ_LEAVE shared one case, and the bbNext check ran before the fall-through edge was added; .NET 8 throws InvalidProgramException for this IL.

Change

The IsLast() check moves to the top of the BBJ_COND case, ahead of both fgAddRefPred calls. 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.s in a method with two blocks, brfalse.s, long-form brtrue and beq.s. Each method was compiled with RuntimeHelpers.PrepareMethod three times, each in its own process, under default settings, DOTNET_TieredCompilation=0 and DOTNET_JitMinOpts=1:

JIT before after
Checked Assertion failed 'block != nullptr' ... during 'Pre-import' (fgflow.cpp:101) InvalidProgramException
Release SIGSEGV InvalidProgramException

A switch in the same position throws InvalidProgramException before and after, and the same loop followed by ret compiles before and after. With the fix, the Checked JIT's dump for the two-block method ends with COMPILATION 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_33972 and Runtime_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.

…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.
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Oct 14, 2026
@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 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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@EgorBo

EgorBo commented Oct 15, 2026 •

Copy link
Copy Markdown
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

jkotas commented Oct 15, 2026

Copy link
Copy Markdown
Member

I see a series of open PRs to JIT for that

I think the only other PR in this category is #135602

@jkotas

jkotas commented Oct 15, 2026

Copy link
Copy Markdown
Member

I thought we didnt handle that in the JIT

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.

@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/jit-cond-branch-falls-off-end branch October 15, 2026 14:12
@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-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI 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.

JIT: the "Fall thru the end of a method" check in fgLinkBasicBlocks is unreachable for a final conditional branch since #95773

3 participants