You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Diagnostic for NativePtr.stackalloc in positions where localloc can never be valid IL #20295
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).
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.
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:
typeSink=[<MethodImpl(MethodImplOptions.NoInlining ||| MethodImplOptions.NoOptimization)>]static memberStaticPut(n:byte,a:ReadOnlySpan<byte>)= a[0]+ n
let inlinealloc<'Twhen'T:unmanaged>n =letp= NativePtr.stackalloc<'T> n
Span<'T>(NativePtr.toVoidPtr p, n)[<EntryPoint>]letmain _ =
Sink.StaticPut(42uy, alloc<byte>16)|> ignore
0
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.
Activity
jwosty commentedon Aug 19, 2026
I'm a little confused. Are you saying that IlxGen (via some prior fix) already today does spilling for
stackallocfor 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:
Spill the evaluation stack around 'localloc'
T-Gro commentedon Aug 20, 2026
@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.
Spill the evaluation stack around 'localloc'
Spill the evaluation stack around 'localloc' (#20302)