Skip to content

Fix up FCalls on native AOT/WASM - #135764

Merged
MichalStrehovsky merged 5 commits into
dotnet:mainfrom
MichalStrehovsky:nativeaot-wasm-fcalls
Oct 15, 2026
Merged

MichalStrehovsky merged 5 commits into
dotnet:mainfrom
MichalStrehovsky:nativeaot-wasm-fcalls

Conversation

@MichalStrehovsky

Copy link
Copy Markdown
Member

Part of a series of pull requests that enable compiling, linking, and running a Console.WriteLine hello world on WASI/native AOT.

Summary

Match the Wasm managed/native ABI and publish the managed stack pointer before clang-generated native code can allocate a linear-memory stack frame.

Port CoreCLR's naked FCall save/publish/call/restore wrappers, including their comments, with NativeAOT declarations/linkage and no portable-entrypoint context argument. Cover the floating-point/int64 variants and preserve the native ABI of write barriers.

Add portable pointer-array allocation and forward allocator-to-allocator calls directly to _IMPL, avoiding wrapper reentry above a live native frame. Fix the portable P/Invoke helper names and mirror CoreCLR's special begin/return boundaries: begin leaves SP published for the native target; debug return checks synchronization. Reverse-P/Invoke helpers inherit the FCall wrappers.

This fixes startup corruption caused by native allocation overlapping live managed frames. It does not implement Wasm GC stack scanning.

Validation

  • .\build.cmd clr.nativeaotruntime -rc Checked -os wasi -arch wasm - passed.
  • Focused FCall stack regression - failed before the publication fix and passed afterward; covered nine signatures and a simulated 4096-byte managed frame.
  • Local verify-stack-boundaries.ps1 and its -RuntimeConfiguration Release variant - passed, covering ordinary/suppressed P/Invokes, nested unmanaged-callable callbacks, and dynamic localloc.
  • Wasm ABI/TLS probe and Windows x64 macro-ABI control - passed.

These results are from the combined local bringup tree, including prerequisite NativeAOT/Wasm enablement, not a fresh build of this isolated main-based branch. The probe scripts are local fixtures, not tests added by this PR. No GC-pressure/root-survival claim is made.

Note

This PR description was generated by GitHub Copilot.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: c78266f3-08e3-4918-b2e5-9fe0ddf2ca17
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Use the RAW FCall naming, including CoreCLR-style non-Wasm fallbacks, and compile the existing naked Wasm barriers for NativeAOT instead of allowing C++ barriers to access an unpublished native stack.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: c78266f3-08e3-4918-b2e5-9fe0ddf2ca17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Native-ABI exports receive an incompatible managed stack-pointer argument through the new FCall wrappers.

2 open findings
What changed in this PR

Updates NativeAOT/Wasm call boundaries to prevent native stack allocations from overlapping live managed frames, supporting WASI startup.

Changes:

  • Adds FCall stack-pointer publication and restoration wrappers.
  • Updates P/Invoke boundaries and allocator forwarding.
  • Integrates Wasm write barriers while preserving their native ABI.

Validation was not rerun during this review.

File Description
src/​coreclr/​runtime/​wasm/​writebarriers.cpp Adds NativeAOT-compatible includes and declarations.
src/​coreclr/​nativeaot/​Runtime/​thread.cpp Updates portable P/Invoke names and Wasm stack boundaries.
src/​coreclr/​nativeaot/​Runtime/​portable.cpp Adds pointer-array allocation and direct implementation forwarding.
src/​coreclr/​nativeaot/​Runtime/​CommonMacros.h Introduces Wasm FCall wrappers and raw native declarations.
src/​coreclr/​nativeaot/​Runtime/​CMakeLists.txt Includes Wasm write barriers in NativeAOT builds.

🧠 Review effort: Balanced

MichalStrehovsky and others added 3 commits October 15, 2026 09:53
Remove the obsolete ARM64 exclusion from portable write barriers.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: c78266f3-08e3-4918-b2e5-9fe0ddf2ca17
Co-authored-by: Copilot <[email protected]>
Copilot-Session: c78266f3-08e3-4918-b2e5-9fe0ddf2ca17
Co-authored-by: Copilot <[email protected]>
Copilot-Session: c78266f3-08e3-4918-b2e5-9fe0ddf2ca17
@MichalStrehovsky
MichalStrehovsky merged commit 405dfb6 into dotnet:main Oct 15, 2026
101 of 103 checks passed
@MichalStrehovsky
MichalStrehovsky deleted the nativeaot-wasm-fcalls branch October 15, 2026 05:34
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 19, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 19, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

arch-wasm WebAssembly architecture area-NativeAOT-coreclr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants