Skip to content

JIT: (bug) [ARM64] non-zeroing stackalloc detects SP subtraction wrap with bvc (signed overflow), so a huge stackalloc silently succeeds instead of overflowing the stack #135524

Closed

Description

@EgorBo

On arm64, genLclHeap checks the wrap of sp - size for a non-zeroing stackalloc with the V flag (bvc) instead of the carry flag. For a huge size, SP is set to a wrapped, invalid address without any probing instead of raising a stack overflow.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

[InlineArray(1 << 20)]
public struct Big { public byte B; }

public class Program
{
    [SkipLocalsInit]
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Test(int n, int k)
    {
        Span<Big> s = stackalloc Big[n];
        if (k > 0)
            s[k].B = 1;
        return s.Length;
    }

    public static int Main()
    {
        Console.WriteLine(Test(int.MaxValue, 0));
        return 100;
    }
}

Expected

The process dies with Stack overflow. (a ~2^51-byte stackalloc can't succeed; Test(1 << 18, 0), ~2^38 bytes, does report Stack overflow.).

Actual

Prints 2147483647: SP is set to a wrapped, invalid address and no probing happens.

Regression?

No. Reproduces on .NET 10.0.12 and main Release.

Notes

  • genLclHeap (codegenarm64.cpp) emits subs regCnt, sp, regCnt; bvc Loop; mov regCnt, #0. For SP < N < 2^63 the unsigned subtraction borrows but V=0, so the clamp to 0 is skipped, the probe loop exits at once (cmp x2, x1; blo done) and mov sp, wrappedValue runs.
  • It should test the carry flag (bhs/bcs) instead. Touching s[k] then faults near the wrapped SP.

Activity

  1. added this to the 12.0.0 milestone on Oct 9, 2026
  2. added
    area-CodeGen-coreclr
    on Oct 9, 2026
  3. dotnet-policy-service commented on Oct 9, 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 Oct 12, 2026
  5. added a commit that references this issue on Oct 14, 2026
    6d39701
  6. locked and limited conversation to collaborators on Nov 13, 2026
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