Skip to content

JIT: (bug) Loop cloning merges the unsigned zero-trip guard 0 <u n with the signed n >= 0 guard and drops the latter, removing bounds checks for n >= 0x80000000 #137676

Closed

Description

@EgorBo

For a do { a[i] ... } while (i < n) loop with uint i, n, loop cloning combines the unsigned zero-trip guard 0 <u n with the signed n >= 0 guard and keeps only the former. For n >= 0x80000000 the remaining signed guard n <= a.Length passes, the bounds-check-free fast loop is entered and runs off the end of the array.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Test(int[] a, uint n)
    {
        int sum = 0;
        uint i = 0;
        do
        {
            sum += a[i];
            i++;
        } while (i < n);
        return sum;
    }

    static void Main()
    {
        try
        {
            Console.WriteLine(Test(new int[4], 0x80000000));
        }
        catch (IndexOutOfRangeException)
        {
            Console.WriteLine("IndexOutOfRangeException");
        }
    }
}

Expected

IndexOutOfRangeException: a[4] is out of range on the 5th iteration. This is also what a Checked JIT prints with DOTNET_JitCloneLoops=0.

Actual

Checked and Release JIT (win-x64, main a5d7014cefc): System.AccessViolationException. The fast loop has no bounds checks. JitDump of the Checked JIT:

Conditions: (0 LTU V01) && (V01 GE 0) && (V01 LE V00.Length)
...
After optimizing cloning conditions
	(0 LTU V01) && (V01 LE V00.Length)

For n = 0x80000000 (negative as an int) the signed check n <= a.Length passes.

Regression?

Yes. Same repro, default settings:

Runtime Result
.NET 8 (8.0.34) OK (IndexOutOfRangeException)
.NET 9 (9.0.23) OK
.NET 10 (10.0.15) OK
.NET 11 (11.0.0) / main (a5d7014cefc) AccessViolationException

Likely introduced by #128532 (it added the NeedsZeroTripGuard cloning condition; not bisected).

Notes

  • LC_Condition::Combines (loopcloning.cpp) treats a condition and one with GenTree::ReverseRelop(oper) and swapped operands as redundant (0 < n vs n >= 0), and LoopCloneContext::OptimizeConditions keeps the first one and removes the other. Combines never looks at compareUnsigned.
  • Keeping 0 <u n and dropping n >= 0 is wrong: 0 <u n only means n != 0; it doesn't imply n >= 0 as a signed int, so the non-negativity guard that protects the signed n <= a.Length check is lost.
  • Possible fix: don't combine conditions whose compareUnsigned differs (and, more generally, only drop a condition that the kept one implies).
  • Different bug from JIT: (bug) Loop cloning removes bounds checks from an unsigned countdown loop whose induction variable underflows #133824 (unsigned decrementing loop whose IV underflows).

Activity

  1. added this to the 12.0.0 milestone on Nov 16, 2026
  2. added
    area-CodeGen-coreclr
    on Nov 16, 2026
  3. dotnet-policy-service commented on Nov 16, 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 Nov 17, 2026
  5. added a commit that references this issue on Nov 18, 2026
    2cfadf0
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