Skip to content

[RyuJIT Wasm] WasmObjectWriter Webcil Envelope Support#125468

Merged
adamperlin merged 39 commits intodotnet:mainfrom
adamperlin:adamperlin/wasm-object-writer-webcil
Mar 27, 2026
Merged

[RyuJIT Wasm] WasmObjectWriter Webcil Envelope Support#125468
adamperlin merged 39 commits intodotnet:mainfrom
adamperlin:adamperlin/wasm-object-writer-webcil

Conversation

@adamperlin
Copy link
Copy Markdown
Contributor

@adamperlin adamperlin commented Mar 12, 2026

This PR implements WebCIL envelope support for the WasmObjectWriter. It builds up the data segment and assigns RVAs/Raw data addresses to each Webcil section as a first pass. As a second pass, it emits the proper headers and sections, and and also implements relocation resolution for IMAGE_BASE* type relocs that may appear in Webcil data sections using the pre-assigned section addresses.

The WebcilDataSegment is emitted as two Wasm data segments according to the Webcil spec like so:

  (data "\0f\00\00\00") ;; data segment 0: payload size as a 4 byte LE uint32, 
                        ;; may have extra padding so that the next segment is properly aligned
  (data "payload\cc")  ;; data segment 1: webcil payload (start is 4 byte aligned)
      | webcil header
      __________________________
      | section header (1)
      | ...
      | section header (n)
      ___________________________
      | section 1 (align 16-byte)
      ___________________________
      | ...
      ___________________________
      | section n (align 16-byte)
      ___________________________

The PR also adds a simple validator program which uses Microsoft.NET.Webcil.Validator. I am fine if we don't want to include this or want this refactored, I just included it in case it would be useful to have as I used it for testing purposes.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds initial WebCIL “envelope” emission support to the CoreCLR AOT Wasm object writer so the runtime can locate PE/CLI header data and debug directory data inside a WebAssembly binary.

Changes:

  • Introduces WebcilHeader / WebcilSectionHeader encoders and wires Webcil.cs into ILCompiler projects.
  • Extends WasmObjectWriter to build a flat WebCIL segment, resolve PE-style relocations into it, and emit it as passive Wasm data segments (with DataCount + exported helpers/globals).
  • Adjusts dependency tracking so Wasm methods get explicit signature nodes, and updates some Wasm-specific section placement logic.

Reviewed changes

Copilot reviewed 10 out of 11 changed files in this pull request and generated 12 comments.

Show a summary per file
File Description
src/coreclr/tools/aot/ILCompiler.ReadyToRun/ILCompiler.ReadyToRun.csproj Adds Webcil.cs to the ReadyToRun tool build.
src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/MethodWithGCInfo.cs Adds a Wasm32-only dependency on WasmTypeNode for explicit signatures.
src/coreclr/tools/aot/ILCompiler.Compiler/ILCompiler.Compiler.csproj Adds Webcil.cs to the compiler tool build.
src/coreclr/tools/Common/Compiler/ObjectWriter/Webcil.cs New: encodes WebCIL header + section headers.
src/coreclr/tools/Common/Compiler/ObjectWriter/WasmObjectWriter.cs Major: builds/emits WebCIL payload as Wasm data segments; adds DataCount/Global handling and relocation resolution changes.
src/coreclr/tools/Common/Compiler/ObjectWriter/WasmNative.cs Adds WasmGlobal encoding support.
src/coreclr/tools/Common/Compiler/ObjectWriter/WasmInstructions.cs Adds instruction helpers needed for emitted Wasm stubs (locals + memory.init).
src/coreclr/tools/Common/Compiler/ObjectWriter/ObjectWriter.cs Adds Wasm-related adjustments and several debug Console.WriteLine statements during emission/relocation.
src/coreclr/tools/Common/Compiler/DependencyAnalysis/Target_Wasm/WasmTypes.cs Formatting tweak in WasmValueType.
src/coreclr/tools/Common/Compiler/DependencyAnalysis/Relocation.cs Exposes WASM_PADDED_RELOC_SIZE_32 publicly for reuse.
src/coreclr/tools/Common/Compiler/DependencyAnalysis/AssemblyStubNode.cs Places AssemblyStubNode into read-only data for Wasm targets.

You can also share your feedback on Copilot code review. Take the survey.

Copilot AI review requested due to automatic review settings March 13, 2026 18:46
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 13 out of 14 changed files in this pull request and generated 5 comments.


You can also share your feedback on Copilot code review. Take the survey.

Copilot AI review requested due to automatic review settings March 13, 2026 19:34
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 13 out of 14 changed files in this pull request and generated 7 comments.

Comments suppressed due to low confidence (1)

src/coreclr/tools/Common/Compiler/ObjectWriter/ObjectWriter.cs:389

  • This change makes all ISymbolRangeNode handling unconditional (previously gated by LayoutMode == CodeDataLayout.Unified). If the original condition was important for non-Wasm backends or for separate-layout scenarios, this risks changing behavior beyond Wasm. Consider restricting this to the Wasm path (or otherwise documenting why it is now correct for all targets/layout modes).
                // TODO-WASM: emit symbol ranges properly when code and data are separated
                // Right now we still need to determine placements for some traditionally text-placed nodes,
                // such as DebugDirectoryEntryNode and AssemblyStubNode
                if (depNode is ISymbolRangeNode symbolRange)
                {
                    symbolRangeNodes.Add(symbolRange);
                    continue;
                }

You can also share your feedback on Copilot code review. Take the survey.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Member

@pavelsavara pavelsavara left a comment

Choose a reason for hiding this comment

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

LGTM.

I wonder if we can add smoke test.
Produce trivial IL-only webcil with the object writer and test it with coreCLR loader.
Even manual/unmerged test doing this would be nice.

@adamperlin
Copy link
Copy Markdown
Contributor Author

LGTM.

I wonder if we can add smoke test. Produce trivial IL-only webcil with the object writer and test it with coreCLR loader. Even manual/unmerged test doing this would be nice.

I have validated that the basic structure is correct for a simple example using the existing Microsoft.NET.WebAssembly.WebcilReader (and a simple driver program that calls the reader), but I'd definitely like to have a smoke test! What would it take to use the CoreCLR loader at the moment?

Copy link
Copy Markdown
Member

@davidwrighton davidwrighton left a comment

Choose a reason for hiding this comment

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

LGTM modulo comments.

@pavelsavara
Copy link
Copy Markdown
Member

What would it take to use the CoreCLR loader at the moment?

Just produce valid WebCIL 1 IL-only assembly (no R2R). I think you are close to it, right ?

Copilot AI review requested due to automatic review settings March 24, 2026 22:17
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated 5 comments.

Copilot AI review requested due to automatic review settings March 27, 2026 00:45
@adamperlin adamperlin force-pushed the adamperlin/wasm-object-writer-webcil branch from 3aa17b2 to 73a90b9 Compare March 27, 2026 00:46
@adamperlin adamperlin force-pushed the adamperlin/wasm-object-writer-webcil branch from 73a90b9 to 21c9f99 Compare March 27, 2026 00:50
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

src/coreclr/tools/Common/Compiler/DependencyAnalysis/Relocation.cs:768

  • ReadValue was updated to treat WASM_TABLE_INDEX_U32/U64 like the other “target-only” Wasm relocs, but WriteValue still has no cases for these relocation types. Any attempt to resolve/write these relocs (e.g., in WasmObjectWriter.ResolveRelocations) will currently fall through to the default Debug.Fail path and leave the placeholder unchanged. Add WriteValue support for WASM_TABLE_INDEX_U32/U64 (write 32/64-bit indices as appropriate) to keep Read/Write symmetry and enable correct resolution.
                case RelocType.WASM_FUNCTION_INDEX_LEB:
                case RelocType.WASM_TABLE_INDEX_SLEB:
                case RelocType.WASM_TABLE_INDEX_U32:
                case RelocType.WASM_TABLE_INDEX_U64:
                case RelocType.WASM_TYPE_INDEX_LEB:
                case RelocType.WASM_GLOBAL_INDEX_LEB:
                    // These wasm relocs do not have offsets, just targets
                    return 0;

@adamperlin
Copy link
Copy Markdown
Contributor Author

/ba-g infra failures

@adamperlin adamperlin merged commit 60c0ab5 into dotnet:main Mar 27, 2026
116 of 123 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants