Skip to content

JIT: (bug) Loop cloning removes the Span bounds check although the IV wraps: no IV-overflow guard for a small stride with an arr.Length + largeOffset limit #137679

Closed

Description

@EgorBo

Loop cloning emits no IV-overflow guard for a loop whose limit is arr.Length + <large positive constant> when the stride is small and the loop indexes a Span<T>. The fast clone has no bounds check, i += 4 wraps to int.MinValue, i < limit still holds, and the loop reads far outside the span.

Minimal Repro

Needs ~2 GB for the array (the span length is int.MaxValue - 1).

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

public class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Test(short[] data, int[] limitArr, int start)
    {
        Span<byte> span = MemoryMarshal.AsBytes(data.AsSpan());
        int sum = 0;
        for (int i = start; i < limitArr.Length + (int.MaxValue - 2); i += 4)
        {
            sum += span[i];
        }
        return sum;
    }

    public static void Main()
    {
        try
        {
            Console.WriteLine(Test(new short[1073741823], new int[1], int.MaxValue - 3));
        }
        catch (IndexOutOfRangeException)
        {
            Console.WriteLine("IndexOutOfRangeException");
        }
    }
}

Expected

IndexOutOfRangeException: span[2147483644] is valid, then i wraps to int.MinValue, i < 2147483646 holds and span[i] must throw. This is also what a Checked JIT prints with DOTNET_JitCloneLoops=0.

Actual

Checked and Release JIT (win-x64, main a5d7014cefc): System.AccessViolationException in Program.Test. The fast loop has no bounds check and no overflow protection. JitDump of the Checked JIT shows the emitted conditions (all pass for this input, no overflow guard among them):

Conditions: (V05 GE 0) && (V01.Length+2147483645 GE 0) && (V01.Length+2147483645 LE V17[V05])

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 related to #129309 / #129349 (span + stride > 1 and LimitOffset cloning; not bisected). Same area as #133823 (fixed by #133834), but a different path: that fix doesn't cover this case.

Notes

  • In optDeriveLoopCloningConditions (loopcloning.cpp), the HasArrayLengthLimit overflow guard is only emitted when largeStride (stride >= 58), even if needsOverflowGuard is true because of hasSpans.
  • The comment's reasoning (arr.Length <= Array.MaxLength, so limit + s - 1 fits for s <= 57) ignores LimitOffset: here the limit can be up to int.MaxValue - 1, so limit + stride - 1 overflows.
  • limit <= span.Length doesn't help since span.Length can be int.MaxValue - 1 (a short[] reinterpreted as bytes).
  • Possible fix: also emit the guard when hasSpans || LimitOffset != 0, or compare maxLimitBase64 against Array.MaxLength regardless of stride.

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
    ce97120
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