Skip to content

WASI hello world with native AOT - #135414

Merged
MichalStrehovsky merged 3 commits into
dotnet:mainfrom
MichalStrehovsky:nativeaot-wasi-relocatable-objects
Oct 13, 2026
Merged

MichalStrehovsky merged 3 commits into
dotnet:mainfrom
MichalStrehovsky:nativeaot-wasi-relocatable-objects

Conversation

@MichalStrehovsky

Copy link
Copy Markdown
Member

Well, it p/invokes to puts and prints Hi, but it's a hello world anyway.

Slop follows:

Emit linking symbol-table and segment-info metadata, together with CODE and merged DATA relocation sections, so wasm-ld can link managed method bodies and compiler data with WASI libc.

Keep segment indices and encoded content offsets in WasmDataSection. Emit ordinary sections, combined DATA, linking metadata and relocation metadata through one ordered IWasmSection list using the shared custom-section encoding. Annotate the serialized fields using Linking.md terminology.

Place Wasm method bodies in WasmCodeSection to avoid native alignment bytes between bodies. Return an empty funclet list for methods without EH, and use linker-assigned absolute function-table slots for NativeAOT frames.

This validates a runtime-less, allocation-free direct-call program. The transition and GC helpers are no-ops; this does not establish real runtime initialization, GC, exceptions/funclets, debug-info or full CoreLib support. No tests were added.

The current working zerosharp.cs changes, included here only as a repro:

--- a/src/coreclr/tools/aot/ILCompiler/reproZero/zerosharp.cs
+++ b/src/coreclr/tools/aot/ILCompiler/reproZero/zerosharp.cs
@@ -3,6 +3,7 @@
 
 using System;
 using System.Runtime;
+using System.Runtime.InteropServices;
 
 #region A couple very basic things
 namespace System
@@ -77,6 +78,13 @@ public sealed class DllImportAttribute : Attribute
     {
         public DllImportAttribute(string dllName) { }
     }
+
+    sealed class SuppressGCTransitionAttribute : Attribute { }
+
+    public sealed class UnmanagedCallersOnlyAttribute : Attribute
+    {
+        public string EntryPoint;
+    }
 }
 #endregion
 
@@ -113,6 +121,9 @@ static void RhpPInvoke(IntPtr frame) { }
         [RuntimeExport("RhpPInvokeReturn")]
         static void RhpPInvokeReturn(IntPtr frame) { }
 
+        [RuntimeExport("RhpGcPoll")]
+        static void RhpGcPoll() { }
+
         [RuntimeExport("RhpFallbackFailFast")]
         static void RhpFallbackFailFast() { while (true) ; }
     }
@@ -121,8 +132,19 @@ static void RhpPInvokeReturn(IntPtr frame) { }
 
 unsafe class Program
 {
-    static int Main()
+    [DllImport("*"), SuppressGCTransition]
+    static extern unsafe int puts(byte* str);
+
+    [UnmanagedCallersOnly(EntryPoint = "__main_argc_argv")]
+    static int NativeMain(int argc, byte** argv)
+    {
+        Main();
+        return 0;
+    }
+
+    static unsafe void Main()
     {
-        return 42;
+        int abc = 'H' | 'i' << 8;
+        puts((byte*)&abc);
     }
 }

Build validation from D:\git\runtime2 (all passed, exit 0):

.\build.cmd clr.wasmjit -rc Checked
.\dotnet.cmd build src\coreclr\tools\aot\ILCompiler\ILCompiler.csproj -c Checked
.\dotnet.cmd build src\coreclr\tools\aot\crossgen2\crossgen2.csproj -c Checked
.\dotnet.cmd build src\coreclr\tools\aot\ILCompiler\reproZero\reproZero.csproj -c Debug -p:CoreCLRConfiguration=Checked

Compile with the locally built managed ILC, not ilc-published:

& .\.dotnet\dotnet.exe `
    .\artifacts\bin\coreclr\windows.x64.Checked\ilc\ilc.dll `
    --targetarch:wasm --targetos:wasi `
    .\artifacts\bin\reproZero\x64\Debug\reproZero.dll `
    -o:.\artifacts\bin\repro\x64\Debug\repro.obj `
    --systemmodule:reproZero `
    -O --noscan --codegenopt:JitMinOpts=1

-O avoids the out-of-line P/Invoke stub's unsupported finally handling; --noscan bypasses the scanner's missing System.Buffer dependency; JitMinOpts=1 uses GC-helper calls instead of polls referencing RhpTrapThreads. ILC completed with exit 0.

Link the generated object with WASI libc (passed, exit 0):

& C:\Programs\clang\bin\clang.exe `
    --target=wasm32-wasip1 `
    --sysroot=D:\git\runtime2\artifacts\tmp\nativeaot-wasi\wasi-sysroot-34.0 `
    -nodefaultlibs .\artifacts\bin\repro\x64\Debug\repro.obj `
    -lc -o .\artifacts\bin\repro\x64\Debug\reproZero.wasm

Execute (printed Hi followed by a newline; exit 0):

wasmtime run .\artifacts\bin\repro\x64\Debug\reproZero.wasm

Emit linking symbol-table and segment-info metadata, together with CODE and
merged DATA relocation sections, so wasm-ld can link managed method bodies
and compiler data with WASI libc.

Keep segment indices and encoded content offsets in WasmDataSection. Emit
ordinary sections, combined DATA, linking metadata and relocation metadata
through one ordered IWasmSection list using the shared custom-section
encoding. Annotate the serialized fields using Linking.md terminology.

Place Wasm method bodies in WasmCodeSection to avoid native alignment bytes
between bodies. Return an empty funclet list for methods without EH, and
use linker-assigned absolute function-table slots for NativeAOT frames.

This validates a runtime-less, allocation-free direct-call program. The
transition and GC helpers are no-ops; this does not establish real runtime
initialization, GC, exceptions/funclets, debug-info or full CoreLib support.
No tests were added.

The local repro/build-wiring changes are deliberately excluded from this
commit: zerosharp.cs, reproZero.csproj, ilc.slnx and eng/Subsets.props.
The current working zerosharp.cs changes, included here only as a repro:

```diff
--- a/src/coreclr/tools/aot/ILCompiler/reproZero/zerosharp.cs
+++ b/src/coreclr/tools/aot/ILCompiler/reproZero/zerosharp.cs
@@ -3,6 +3,7 @@
 
 using System;
 using System.Runtime;
+using System.Runtime.InteropServices;
 
 #region A couple very basic things
 namespace System
@@ -77,6 +78,13 @@ public sealed class DllImportAttribute : Attribute
     {
         public DllImportAttribute(string dllName) { }
     }
+
+    sealed class SuppressGCTransitionAttribute : Attribute { }
+
+    public sealed class UnmanagedCallersOnlyAttribute : Attribute
+    {
+        public string EntryPoint;
+    }
 }
 #endregion
 
@@ -113,6 +121,9 @@ static void RhpPInvoke(IntPtr frame) { }
         [RuntimeExport("RhpPInvokeReturn")]
         static void RhpPInvokeReturn(IntPtr frame) { }
 
+        [RuntimeExport("RhpGcPoll")]
+        static void RhpGcPoll() { }
+
         [RuntimeExport("RhpFallbackFailFast")]
         static void RhpFallbackFailFast() { while (true) ; }
     }
@@ -121,8 +132,19 @@ static void RhpPInvokeReturn(IntPtr frame) { }
 
 unsafe class Program
 {
-    static int Main()
+    [DllImport("*"), SuppressGCTransition]
+    static extern unsafe int puts(byte* str);
+
+    [UnmanagedCallersOnly(EntryPoint = "__main_argc_argv")]
+    static int NativeMain(int argc, byte** argv)
+    {
+        Main();
+        return 0;
+    }
+
+    static unsafe void Main()
     {
-        return 42;
+        int abc = 'H' | 'i' << 8;
+        puts((byte*)&abc);
     }
 }
```

Build validation from D:\git\runtime2 (all passed, exit 0):

```powershell
.\build.cmd clr.wasmjit -rc Checked
.\dotnet.cmd build src\coreclr\tools\aot\ILCompiler\ILCompiler.csproj -c Checked
.\dotnet.cmd build src\coreclr\tools\aot\crossgen2\crossgen2.csproj -c Checked
.\dotnet.cmd build src\coreclr\tools\aot\ILCompiler\reproZero\reproZero.csproj -c Debug -p:CoreCLRConfiguration=Checked
```

Compile with the locally built managed ILC, not ilc-published:

```powershell
& .\.dotnet\dotnet.exe `
    .\artifacts\bin\coreclr\windows.x64.Checked\ilc\ilc.dll `
    --targetarch:wasm --targetos:wasi `
    .\artifacts\bin\reproZero\x64\Debug\reproZero.dll `
    -o:.\artifacts\bin\repro\x64\Debug\repro.obj `
    --systemmodule:reproZero `
    -O --noscan --codegenopt:JitMinOpts=1
```

-O avoids the out-of-line P/Invoke stub's unsupported finally handling;
--noscan bypasses the scanner's missing System.Buffer dependency;
JitMinOpts=1 uses GC-helper calls instead of polls referencing RhpTrapThreads.
ILC completed with exit 0.

Link the generated object with WASI libc (passed, exit 0):

```powershell
& C:\Programs\clang\bin\clang.exe `
    --target=wasm32-wasip1 `
    --sysroot=D:\git\runtime2\artifacts\tmp\nativeaot-wasi\wasi-sysroot-34.0 `
    -nodefaultlibs .\artifacts\bin\repro\x64\Debug\repro.obj `
    -lc -o .\artifacts\bin\repro\x64\Debug\reproZero.wasm
```

Execute (printed Hi followed by a newline; exit 0):

```powershell
wasmtime run .\artifacts\bin\repro\x64\Debug\reproZero.wasm
```

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 16791b4f-32ea-4467-8d9a-e9586267cd98
@MichalStrehovsky
MichalStrehovsky requested review from adamperlin and jtschuster and a balanced review from Copilot October 8, 2026 09:35
@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 6 pipeline(s).
10 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

Comment thread src/coreclr/jit/emitwasm.cpp Resolved
MichalStrehovsky added a commit that referenced this pull request Oct 12, 2026
Only RyuJIT change is needed because the compiler/CoreLib still have
this from the WASM prototype in the CoreRT repo (that turned into
feature/NativeAOT-LLVM).

I wanted to do this in a way that it doesn't conflict with
feature/NativeAOT-LLVM, but that one uses `TARGET_LLVM_WASM32` and
`TARGET_LLVM_WASM64` so there will be a conflict.

Verified we can make a fat call when one is added to reprozero.cs from
#135414.
@MichalStrehovsky
MichalStrehovsky merged commit f9d6cc0 into dotnet:main Oct 13, 2026
128 of 129 checks passed
@MichalStrehovsky
MichalStrehovsky deleted the nativeaot-wasi-relocatable-objects branch October 13, 2026 02:35
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 14, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 14, 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.

4 participants