Repository navigation
Description
The IL below is invalid: the method's last instruction is a conditional branch, so control can fall off the end of the body. I'm not claiming the runtime has to handle invalid IL. I'm raising this because the JIT already has a check for exactly this shape, and since #95773 that check can no longer be reached.
The check
At main 56ba1c0, the BBJ_COND case in fgLinkBasicBlocks reads (fgbasic.cpp#L2736-L2776, abridged and annotated):
case BBJ_COND:
{
BasicBlock* const trueTarget = fgLookupBB(curBBdesc->GetTargetOffs());
BasicBlock* const falseTarget = curBBdesc->Next(); // nullptr for the last block
FlowEdge* const trueEdge = fgAddRefPred<initializingPreds>(trueTarget, curBBdesc);
FlowEdge* const falseEdge = fgAddRefPred<initializingPreds>(falseTarget, curBBdesc);
...
if (curBBdesc->IsLast())
{
BADCODE("Fall thru the end of a method");
}When the conditional branch is the last block, Next() is null, and fgAddRefPred asserts block != nullptr and then increments block->bbRefs (fgflow.cpp#L99-L105) before the IsLast() check runs.
In release/8.0, BBJ_COND, BBJ_ALWAYS and BBJ_LEAVE share one case, and if (!curBBdesc->bbNext) BADCODE("Fall thru the end of a method"); runs before the edge to bbNext is added (release/8.0 fgbasic.cpp#L2776-L2806). #95773 ("JIT: Add explicit successor for BBJ_COND false branch", 6c7e6e2, first in v9.0.0-preview.1.24080.9) gave BBJ_COND its own case, with the false-target edge added before the check (fgbasic.cpp#L2957-L2973 at 6c7e6e2e). The order is the same in release/9.0, release/10.0 and main.
What happens
| JIT | conditional branch as the last instruction |
|---|---|
| 8.0.26 | InvalidProgramException |
| 9.0.15; 10.0.6 and 10.0.9 to 10.0.13; 11.0.0-preview.7 and rc.1 | the process crashes (SIGSEGV on Linux and macOS, 0xC0000005 on Windows); with symbols, on 10.0.13 and 11.0.0-preview.7 (osx-arm64), the fault is a read through the null block in fgAddRefPred, called from fgLinkBasicBlocks |
| main 56ba1c0, Release | the process crashes (SIGSEGV) |
| main 56ba1c0, Checked | Assertion failed 'block != nullptr' ... during 'Pre-import', fgflow.cpp:101 |
Each method was run three times per runtime and configuration, each run in its own process. 8.0, 9.0 and main were run on linux-x64 with brtrue.s, brfalse.s, long-form brtrue, beq.s and a two-block brtrue.s method, and all five behave the same. 10.0 and 11.0 were run on linux-x64, osx-arm64 and windows-x64, though not every version on every host, with both brtrue.s methods, brfalse.s and long-form brfalse. The same loop followed by ret compiles on every version.
Repro
JitFallthrough.il (assemble with ilasm -dll):
.assembly extern System.Runtime { .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A) .ver 8:0:0:0 }
.assembly JitFallthrough { }
.class public auto ansi abstract sealed beforefieldinit P extends [System.Runtime]System.Object
{
// IL_0000 ldc.i4.0 ; IL_0001 brtrue.s IL_0000 ; end of IL
.method public hidebysig static void BrtrueSBackToStart() cil managed
{
.maxstack 1
L0: ldc.i4.0
brtrue.s L0
}
// Control: the same loop followed by ret. Compiles.
.method public hidebysig static void BrtrueSBackToStartThenRet() cil managed
{
.maxstack 1
L0: ldc.i4.0
brtrue.s L0
ret
}
}
Then run RuntimeHelpers.PrepareMethod(typeof(P).GetMethod("BrtrueSBackToStart").MethodHandle).
Question
Would you like the check order restored? The change moves the existing IsLast() check 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. I'm opening a PR with the change and a JIT IL test, which will link here. If you would rather close this as by design, that is fine too, and the PR can be closed with it.
Note
This issue was prepared with AI assistance (Anthropic Claude and OpenAI Codex), under my direction. I reviewed it before posting.
Activity
dotnet-policy-service commentedon Oct 14, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
JIT: check for a conditional branch that falls off the end of the met…