Skip to content

ILVerify throws IndexOutOfRangeException when a method ends with a conditional branch or switch #135828

Closed

Description

@pcshrosbree

Problem: when the last instruction of a method is a conditional branch or switch whose targets are all inside the method, control falls through the end of the method when the branch is not taken. ILVerify throws IndexOutOfRangeException for such a method instead of reporting MethodFallthrough, and the CLI stops at that method.

This is the case tmat reported on #63227: a method whose last instruction is brtrue.s to an earlier instruction, with no ret. #122056 closed #63227 with fixes for exception clause bounds and zero-length methods; this case still throws on main.

Repro

Repro.il:

.assembly extern System.Runtime { .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A ) .ver 10:0:0:0 }
.assembly Repro { .ver 1:0:0:0 }
.module Repro.dll

.class public abstract auto ansi sealed beforefieldinit C extends [System.Runtime]System.Object
{
    .method public hidebysig static void FallsOffEnd() cil managed
    {
        // The last instruction is a conditional branch back to the ret;
        // when the branch is not taken, control falls through the end of the method.
        br.s        Condition
    Return:
        ret
    Condition:
        ldc.i4.0
        brtrue.s    Return
    }

    .method public hidebysig static void Later() cil managed
    {
        // A real error in a later method: a value is left on the stack at a void return.
        ldc.i4.0
        ret
    }
}
$ ilasm -dll -output=Repro.dll Repro.il          # Microsoft.NETCore.ILAsm 12.0.0-alpha.1.26471.108
$ ilverify Repro.dll -r '<Microsoft.NETCore.App 10.0.12>/*.dll' -s System.Private.CoreLib
Error: Index was outside the bounds of the array.
System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at Internal.IL.ILImporter.CreateBasicBlock(Int32 offset) in src/coreclr/tools/Common/TypeSystem/IL/ILImporter.cs:line 100
   at Internal.IL.ILImporter.FindJumpTargets() in src/coreclr/tools/Common/TypeSystem/IL/ILImporter.cs:line 215
   at Internal.IL.ILImporter.FindBasicBlocks() in src/coreclr/tools/Common/TypeSystem/IL/ILImporter.cs:line 93
   at Internal.IL.ILImporter.Verify() in src/coreclr/tools/ILVerification/ILImporter.Verify.cs:line 226
   ...

The CLI exits with code 1, and the ReturnVoid error in Later is not reported.

Reproduced with ILVerify built from main at 56ba1c0 on linux-x64, and with the Microsoft.ILVerification 10.0.12 package, which throws the same exception from CreateBasicBlock.

Cause

FindJumpTargets (Common/TypeSystem/IL/ILImporter.cs at 56ba1c0) creates a basic block for the instruction after every conditional branch and switch (:215, :248, :264). When the branch is the last instruction, that offset is the length of the IL, and CreateBasicBlock indexes _basicBlocks past its end (:100). The importer's existing check for falling through the end of the method (ReportFallthroughAtEndOfMethod, :935-939) is in the import pass, which runs after FindJumpTargets.

The NativeAOT scanner (ILImporter.Scanner.cs) runs the same FindJumpTargets. Reading ILScanner.CompileSingleMethod, an IndexOutOfRangeException from the importer is not a TypeSystemException, so it is wrapped in CodeGenerationFailedException (ILScanner.cs:133-136) instead of the method getting a throwing body (:126-132). I did not run ilc to confirm this.

The JIT has a separate problem with the conditional-branch form: #135825.

Regression? No. The three CreateBasicBlock(_currentOffset) calls are unchanged since the code moved into this repo (9a22600, 2020), and release/10.0 and release/11.0 have the same code.

I have a fix with regression tests, and will open a PR.

Note

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

Activity

  1. added
    untriaged
    on Oct 14, 2026
  2. added
    area-CodeGen-coreclr
    on Oct 14, 2026
  3. dotnet-policy-service commented on Oct 14, 2026

    @dotnet-policy-service
    Contributor

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

  4. added a commit that references this issue on Oct 15, 2026
    40b4bcb
  5. removed
    untriaged
    on Oct 15, 2026
  6. locked and limited conversation to collaborators on Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclr

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions