Skip to content

JIT: (bug) Loop cloning removes bounds checks from an unsigned countdown loop whose induction variable underflows #133824

Closed

Description

@EgorBo

Loop cloning treats an unsigned decrementing loop as monotone, so the IV can wrap below the limit (0 - k → huge uint) and the bounds-check-free fast clone reads far out of bounds.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

public static class P
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    public static int Test(int[] a, uint n, uint lo)
    {
        int sum = 0;
        for (uint i = n; i > lo; i -= 3)
        {
            sum += a[i];
        }
        return sum;
    }

    public static void Main()
    {
        int[] a = new int[8];
        try
        {
            Console.WriteLine(Test(a, 7, 0));
        }
        catch (IndexOutOfRangeException)
        {
            Console.WriteLine("IndexOutOfRangeException");
        }
    }
}

i walks 7, 4, 1, then 1 - 3 wraps to 0xFFFFFFFE, which is still > lo under the unsigned comparison, so a[i] must throw.

Expected

IndexOutOfRangeException

Actual

Fatal error.

Exit code 0xC0000005. The fast clone indexes a with 0xFFFFFFFE (~16 GB past the array base) — an out-of-bounds read from plain safe C#.

Notes

  • NaturalLoopIterInfo::IsDecreasingLoop() (src/coreclr/jit/flowgraph.cpp) does not consider TestTree->IsUnsigned(), so an unsigned GT_GT is accepted as a plain decreasing test even though it is not monotone under wraparound.
  • The derived cloning conditions a != null && n >= 0 && lo >= 0 && n < a.Length only constrain the initial IV; nothing proves the IV cannot step past the limit.
  • Any n/lo with (n - lo) % step != 0 hits it. Fix direction: bail out (or add conditions, e.g. step divides init - limit) when the test is unsigned.
  • Not a regression: also reproduces on released .NET 10.0.12.

Activity

  1. added this to the 12.0.0 milestone on Sep 13, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 13, 2026
  3. dotnet-policy-service commented on Sep 13, 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. self-assigned this
    on Sep 14, 2026
  5. changed the title [-]JIT (bug): Loop cloning removes bounds checks from an unsigned countdown loop whose induction variable underflows[/-] [+]JIT: (bug) Loop cloning removes bounds checks from an unsigned countdown loop whose induction variable underflows[/+] on Sep 16, 2026
  6. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 8.0.425
    • 9.0.318
    • 10.0.401
    • 11.0.100-rc.2.26467.112
  7. added a commit that references this issue on Nov 16, 2026
    05eecba
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions