Skip to content

Diagnostic for NativePtr.stackalloc in positions where localloc can never be valid IL #20295

Closed

Description

@T-Gro

The following snippets are accepted by the F# compiler but throw System.InvalidProgramException at load, before any user code runs. Follow-up to #8083 (general case fixed by #20302).

1. localloc in an exception handler

open Microsoft.FSharp.NativeInterop
try () with _ -> NativePtr.stackalloc<int> 1 |> ignore

2. localloc while this is uninitialized (chained base constructor)

open Microsoft.FSharp.NativeInterop
type A(p: nativeptr<int>) = class end
type B() = inherit A(NativePtr.stackalloc<int> 1)
B() |> ignore

Suggest rejecting them in PostInferenceChecks (dsyme's suggestion on #8083) — a compile-time error instead of an InvalidProgramException at JIT time.

Details

Fixed for the general case by #20302: IlxGen spills the pending stack, emits localloc at an empty stack, then reloads the pending values under the result pointer.

Case 1 (handler). The CoreCLR importer rejects localloc when block->hasHndIndex() (dotnet/runtime, jit/importer.cpp) — that is, inside a catch, finally, or filter handler, not a try body. This is why the for .. in seq shape in fsharp/fslang-suggestions#1469 works. eenv.withinSEH does not yet separate a handler from a try body, so the diagnostic needs a new flag.

Case 2 (uninitialized this). ECMA forbids spilling an uninitialized this, so EmitLocallocCode falls back to a plain emit when uninitializedThisOnStackCount > 0. The code is a use-after-free regardless: the stack memory dies when the constructor returns. A real fix, instead of a diagnostic, would hoist the base-constructor arguments into locals before this is pushed.

Activity

  1. added
    Area-Diagnostics
    on Aug 19, 2026
  2. added this to the Backlog milestone on Aug 19, 2026
  3. jwosty commented on Aug 19, 2026

    @jwosty
    Contributor

    I'm a little confused. Are you saying that IlxGen (via some prior fix) already today does spilling for stackalloc for some cases and the filed issues are just situations where it doesn't do that yet? Or are you talking about an offline WIP branch of yours that implements such spilling? I'm just trying to follow along :)

    By the way for anyone else reading, here's another example:

    type Sink =
        [<MethodImpl(MethodImplOptions.NoInlining ||| MethodImplOptions.NoOptimization)>]
        static member StaticPut (n: byte, a: ReadOnlySpan<byte>) = a[0] + n
    
    let inline alloc<'T when 'T :unmanaged> n =
        let p = NativePtr.stackalloc<'T> n
        Span<'T>(NativePtr.toVoidPtr p, n)
    
    [<EntryPoint>]
    let main _ =
        Sink.StaticPut(42uy, alloc<byte> 16) |> ignore
        0
  4. T-Gro commented on Aug 20, 2026

    @T-Gro
    MemberAuthor

    @jwosty :

    This issue is (LLM stored) residual of what kind of program still crash even after #20302 fixing the common scenarios - to document what will continue to throw an InvalidProgramException.
    This new example of yours is covered in what's fixable.

    Nevertheless it was never supposed to be public in such chatty form, my apologies - let me restructure it.

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-DiagnosticsFeature ImprovementNeeds-Triage

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions