Skip to content

[browser][coreCLR] Loading WebCIL - 2nd attempt#124904

Open
pavelsavara wants to merge 8 commits intodotnet:mainfrom
pavelsavara:loading_webcil2
Open

[browser][coreCLR] Loading WebCIL - 2nd attempt#124904
pavelsavara wants to merge 8 commits intodotnet:mainfrom
pavelsavara:loading_webcil2

Conversation

@pavelsavara
Copy link
Member

@pavelsavara pavelsavara commented Feb 26, 2026

Enable Webcil loading in CoreCLR for Browser/WASM

Summary

Adds native Webcil container format support to the CoreCLR runtime, enabling CoreCLR-on-WASM to load .wasm-wrapped assemblies directly — previously only supported by the Mono runtime. Removes the WasmEnableWebcil=false override that was forcing CoreCLR browser builds to use raw .dll files.

Changes

CoreCLR Webcil decoder (src/coreclr/)

  • New WebcilDecoder class (webcildecoder.h, webcildecoder.cpp) — standalone reader for the Webcil container format with full RVA/offset translation, COR header resolution, metadata access, section validation, IL method checking, debug directory support, and DAC enumeration.
  • FEATURE_WEBCIL cmake gate enabled for CLR_CMAKE_TARGET_BROWSER (clrfeatures.cmake, clrdefinitions.cmake).

PEImageLayout composition refactor (src/coreclr/vm/)

  • PEImageLayout no longer inherits from PEDecoder; instead holds m_peDecoder (and conditionally m_webcilDecoder) as members with an ImageFormat discriminator (FORMAT_PE/FORMAT_WEBCIL).
  • ~160 inline forwarding methods in peimagelayout.inl dispatch to the active decoder via DECODER_DISPATCH/DECODER_CHECK/PE_OR_WEBCIL macros.
  • PEDecoder public API split: methods needed by PEImageLayout forwarding stay public; PE-internal helpers (GetHeaders, GetDirectoryEntry, CheckDirectoryEntry, relocation/TLS/strong-name internals, etc.) moved to a private section accessible only via friend class PEImageLayout.
  • FlatImageLayout constructor detects Webcil magic and initializes both decoders (Webcil for format operations, PE decoder for base/size/flags used by cDAC).
  • New HasHeaders() abstraction on PEImageLayout and PEImage — returns HasWebcilHeaders() or HasNTHeaders() depending on format. Callers in peassembly.cpp, peimage.cpp, eventtrace.cpp updated from HasNTHeaders() to HasHeaders().
  • debugdebugger.cpp: switched from PEDecoder* to PEImageLayout* and uses format-agnostic GetDirectoryEntryData instead of raw GetDirectoryEntry+GetDirectoryData.
  • ceeload.cpp: same PEDecoder*PEImageLayout* fix for IJW host lookup.
  • peassembly.cpp: accept IMAGE_FILE_MACHINE_UNKNOWN (Webcil machine type) for IL-only validation; guard 32-bit NT header check with HasNTHeaders().
  • assemblybindercommon.cpp: treat IMAGE_FILE_MACHINE_UNKNOWN as processor-agnostic MSIL.

cDAC diagnostics (src/native/managed/cdac/)

  • PEImageLayout data descriptor gains Format field (datadescriptor.inc, PEImageLayout.cs).
  • Loader_1.cs: Webcil-aware RvaToOffset path that reads the Webcil section table directly from the target memory when IsWebcilFormat is set.

Build/infrastructure

  • Removed WasmEnableWebcil=false from CoreCLR browser build properties (WasmApp.InTree.props, tests.browser.targets).
  • MSBuild task renames: WebCilCandidatesWebcilCandidates, IsWebCilEnabledIsWebcilEnabled, _WasmBuildWebCilPath_WasmBuildWebcilPath and related properties.
  • Test infrastructure: BuildEnvironment.cs removes CoreCLR Webcil exclusion; env var WasmEnableWebCilWasmEnableWebcil.

@pavelsavara pavelsavara added this to the 11.0.0 milestone Feb 26, 2026
@pavelsavara pavelsavara self-assigned this Feb 26, 2026
Copilot AI review requested due to automatic review settings February 26, 2026 14:56
@pavelsavara pavelsavara added arch-wasm WebAssembly architecture area-AssemblyLoader-coreclr os-browser Browser variant of arch-wasm labels Feb 26, 2026
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @agocke, @elinor-fung
See info in area-owners.md if you want to be subscribed.

Copy link
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

Enables CoreCLR-on-Browser/WASM to load assemblies packaged in the Webcil container format (i.e., .wasm-wrapped assemblies), aligning CoreCLR behavior with existing Mono Webcil support and removing prior build/test overrides that forced raw .dll usage.

Changes:

  • Adds CoreCLR-side Webcil decoding and wires it into PEImageLayout via format-dispatched forwarding to either PEDecoder (PE) or WebcilDecoder (Webcil).
  • Updates the Webcil tooling pipeline (converter/reader/wrapper + MSBuild tasks/targets) including 16-byte section alignment and property renames (WebCil*Webcil*).
  • Extends cDAC contracts/readers and WASM test infrastructure to understand and exercise Webcil images.

Reviewed changes

Copilot reviewed 35 out of 35 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/tasks/Microsoft.NET.WebAssembly.Webcil/WebcilWasmWrapper.cs Improves wrapper error message for payload offset validation.
src/tasks/Microsoft.NET.WebAssembly.Webcil/WebcilReader.cs Updates to PascalCase header fields and adds bounds checking in RVA translation.
src/tasks/Microsoft.NET.WebAssembly.Webcil/WebcilHeader.cs Renames Webcil header fields to PascalCase for consistency.
src/tasks/Microsoft.NET.WebAssembly.Webcil/WebcilConverter.cs Implements 16-byte section alignment, debug directory translation, and related validations.
src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ConvertDllsToWebCil.cs Renames output item list to WebcilCandidates.
src/tasks/Microsoft.NET.Sdk.WebAssembly.Pack.Tasks/ComputeWasmPublishAssets.cs Renames task property to IsWebcilEnabled and updates usage.
src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Data/PEImageLayout.cs Adds Format field to contract + helper to detect Webcil format.
src/native/managed/cdac/Microsoft.Diagnostics.DataContractReader.Contracts/Contracts/Loader_1.cs Adds Webcil-aware RVA-to-offset logic by reading Webcil section table from target memory.
src/mono/wasm/features.md Documentation update: “WebCIL” → “Webcil”.
src/mono/wasm/Wasm.Build.Tests/Common/BuildEnvironment.cs Removes CoreCLR Webcil exclusion; renames env var WasmEnableWebcil.
src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets Renames MSBuild properties/items/parameters from WebCilWebcil.
src/mono/cmake/options.cmake Documentation string update for ENABLE_WEBCIL.
src/mono/cmake/config.h.in Comment update for ENABLE_WEBCIL.
src/mono/browser/debugger/BrowserDebugProxy/DebugStore.cs Documentation wording update for Webcil handling.
src/mono/browser/build/WasmApp.InTree.props Removes CoreCLR WasmEnableWebcil=false override.
src/coreclr/vm/peimagelayout.inl Adds decoder-dispatch forwarding methods; introduces format-agnostic header checks.
src/coreclr/vm/peimagelayout.h Refactors PEImageLayout to composition with PEDecoder/WebcilDecoder and ImageFormat discriminator.
src/coreclr/vm/peimagelayout.cpp Initializes Webcil decoder on detection; updates PEDecoder init/reset call sites; DAC enumeration dispatch.
src/coreclr/vm/peimage.inl Adds PEImage::HasHeaders() helper.
src/coreclr/vm/peimage.h Declares HasHeaders() in PEImage.
src/coreclr/vm/peimage.cpp Uses HasHeaders() instead of HasNTHeaders() in metadata/debug-directory paths.
src/coreclr/vm/peassembly.cpp Accepts IMAGE_FILE_MACHINE_UNKNOWN for IL-only validation; guards 32-bit header check.
src/coreclr/vm/eventtrace.cpp Switches checks from NT headers to format-agnostic headers.
src/coreclr/vm/debugdebugger.cpp Switches from PEDecoder* to PEImageLayout* and uses GetDirectoryEntryData.
src/coreclr/vm/datadescriptor/datadescriptor.inc Adds Format field to PEImageLayout’s cDAC data descriptor.
src/coreclr/vm/ceeload.cpp Updates IJW lookup to use PEImageLayout* (post-inheritance refactor).
src/coreclr/utilcode/webcildecoder.cpp Adds CoreCLR Webcil decoding/validation implementation and DAC enumeration support.
src/coreclr/utilcode/CMakeLists.txt Adds webcildecoder.cpp to utilcode build.
src/coreclr/inc/webcildecoder.h Adds Webcil structures and WebcilDecoder API.
src/coreclr/inc/pedecoder.h Makes PEImageLayout a friend and restricts PE-internal APIs to PEImageLayout usage.
src/coreclr/clrfeatures.cmake Enables FEATURE_WEBCIL for browser builds.
src/coreclr/clrdefinitions.cmake Adds FEATURE_WEBCIL compile definition when enabled.
src/coreclr/binder/assemblybindercommon.cpp Treats IMAGE_FILE_MACHINE_UNKNOWN as processor-agnostic MSIL in binder logic.
eng/testing/tests.browser.targets Removes CoreCLR WasmEnableWebcil=false override for browser tests.
docs/design/mono/webcil.md Updates spec text for alignment and PascalCase field names + naming consistency.

Copilot AI review requested due to automatic review settings February 26, 2026 15:12
Copy link
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 35 out of 35 changed files in this pull request and generated 4 comments.

Comments suppressed due to low confidence (1)

src/tasks/Microsoft.NET.WebAssembly.Webcil/WebcilReader.cs:354

  • New exceptions in TranslateRVA use nameof(_stream) as the parameter name. Since _stream is a private field (not a parameter), this produces a less helpful ParamName than nameof(stream) (constructor parameter) or omitting the param name entirely. Consider aligning with the earlier constructor exceptions which use nameof(stream).

Copilot AI review requested due to automatic review settings February 26, 2026 15:24
Copy link
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 35 out of 35 changed files in this pull request and generated 2 comments.

Copilot AI review requested due to automatic review settings February 27, 2026 14:16
Copy link
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 23 out of 23 changed files in this pull request and generated 5 comments.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings February 27, 2026 15:07
pavelsavara and others added 2 commits February 27, 2026 16:09
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
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 23 out of 23 changed files in this pull request and generated 1 comment.

if (pdwPEKind != NULL)
*pdwPEKind = peILonly;
if (pdwMachine != NULL)
*pdwMachine = IMAGE_FILE_MACHINE_UNKNOWN;
Copy link
Member

Choose a reason for hiding this comment

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

IL-only files have machine type IMAGE_FILE_MACHINE_I386 for historic reasons. I think it would make more sense to return it here so that we do not need to fix all callers.

LIMITED_METHOD_CONTRACT;

// Webcil has no PE IMAGE_DATA_DIRECTORY array.
// Only the debug directory is stored in the WebcilHeader.
Copy link
Member

Choose a reason for hiding this comment

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

CorHeader is stored in webcil header as well, but this method does not return it.

It may be better to have concrete HasDebugDirectoryEntry / GetDebugDirectoryEntry methods and make the generic one unimplemented for webcil.

// IL method validation
// ------------------------------------------------------------

CHECK WebcilDecoder::CheckILMethod(RVA rva)
Copy link
Member

Choose a reason for hiding this comment

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

This is duplicating a lot of non-trivial logic. Any way we can avoid it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-AssemblyLoader-coreclr os-browser Browser variant of arch-wasm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants