Skip to content

[wasi] Strip native debug info from the CoreCLR WASI per-app host - #137700

Draft
lewing wants to merge 4 commits into
dotnet:mainfrom
lewing:lewing-strip-debug-coreclr-wasi-relink
Draft

lewing wants to merge 4 commits into
dotnet:mainfrom
lewing:lewing-strip-debug-coreclr-wasi-relink

Conversation

@lewing

@lewing lewing commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

CoreCLR WASI relinks the wasihost (corerun) for every app, but that link ignored WasmNativeStrip. Release hosts therefore shipped the runtime pack's DWARF and the wasm name section. For a trimmed hello world, 29.4 MB of the 33.9 MB host was DWARF; in a real app (NesmWasm, composite R2R) it was about 26 MB of a 47.6 MB host. Debug wasn't protected either: CoreCLR WASI forces WasmBuildNative=false, so the WasmApp.Common rule that keeps symbols in Debug never applied, and WasmNativeStrip resolved to true in every configuration.

Changes

  • Defaults (WasiApp.CoreCLR.targets): WasmNativeStrip is false in Debug and true otherwise. WasmEmitSymbolMap defaults to true; it's set before WasiApp.targets is imported, because that file defaults it to false.
  • Link:
    • -Wl,--strip-debug when stripping, or when WasmNativeDebugSymbols=false. This drops DWARF at link time but keeps function names, so the R2R composition can still merge the composite's names in.
    • The generated callhelper sources compile with -g when symbols are kept.
  • New _WasiCoreCLRFinalizeHost target and StripWasiComponentSymbols task:
    • Runs after the R2R composition, which renumbers functions, on both the R2R and non-R2R paths.
    • With WasmNativeStrip=true, removes the name and .debug_* custom sections from the component's first core module.
    • Writes the names to managed/corerun.symbols as index:name lines, the emscripten symbol-map format browser uses for dotnet.native.js.symbols. The indices are the ones wasmtime prints as <wasm function N>. WasmEmitSymbolMap=false skips the sidecar.
    • The component rewrite shares its parsing with the existing ReplaceFirstCoreModule in Microsoft.NET.WebAssembly.Webcil.
  • R2R --wasm-debug-info: name when the merged host keeps names or writes the sidecar, otherwise none. crossgen2's own symbol-map sidecar isn't used, because it numbers functions within the composite, not the merged host. (R2R debug and inlining info is already stripped for wasi-* by Microsoft.NET.CrossGen.targets since Disable unused ReadyToRun metadata on WebAssembly #134690.)
  • Library tests (tests.wasi.targets): CoreCLR test hosts keep names but drop DWARF (WasmNativeStrip=false and WasmNativeDebugSymbols=false), so wasmtime backtraces in Helix logs stay readable.

Sizes

Trimmed console app on the 12.0.0-alpha.1.26574.109 packs (their targets match main), wasi-sdk 34:

Mode main This PR Sidecar
Release 33.22 MB 3.72 MB 0.61 MB
Release + composite R2R 35.15 MB 7.57 MB 1.59 MB
Debug — 33.31 MB (DWARF and names kept) —

Keeping names without DWARF (WasmNativeStrip=false + WasmNativeDebugSymbols=false) adds roughly 0.6 MB (non-R2R) to 1.8 MB (R2R), measured on the earlier 26565 packs. The R2R payload (1,156,448 B) is unchanged here because the local 26565 SDK predates #134690; newer SDKs strip R2R debug info on their own.

Validation

Baseline: the stock 26574.109 Microsoft.NET.Runtime.WebAssembly.Wasi.Sdk pack, whose targets are byte-identical to main. This PR: the same pack with this branch's WasiApp.CoreCLR.targets, WasmApp.Common.targets, and locally built WasmAppBuilder/Webcil assemblies dropped in. SDK 26565, wasi-sdk 34, wasmtime 49.

  • Published Release, Release + R2R, and Debug. Every host passes wasm-tools validate --features all, and the stripped hosts have no name or .debug_* sections.
  • Under wasmtime 49, hello world returns its exit code for Release, Release + R2R, and Debug.
  • A forced Environment.FailFast trap, symbolized through corerun.symbols, gives the same 16 frames as the unstripped baseline. With R2R this includes managed frames such as app_Program__Fail and S_P_CoreLib_System_Environment__FailFast.
  • On the earlier 26565 packs: the opt-out combinations, and incremental republish toggling WasmEmitSymbolMap=false and WasmNativeStrip=false (removes or restores the sidecar and DWARF correctly).
  • WebcilInWasmSizesTests.WasmComponentSymbols_StripsFirstCoreModuleSymbols covers the component rewrite and symbol map with a synthetic component. I ran it outside Wasm.Build.Tests (this worktree has no libs build), compiled verbatim against the rebuilt Webcil assembly: 2/2 passed.

Not done:

  • No in-tree build or test run.
  • CI won't run the new task end to end. The CI library-test lanes keep names, so only user Release builds run StripWasiComponentSymbols. Wasm.Build.Tests doesn't run for CoreCLR WASI, and the WasmAppBuilder tasks have no unit-test project.

Notes

  • wasmtime only names frames from the module's own name section. It can't load a symbol-map sidecar, and it ignores .dwp DWARF packages for components. So stripped Release hosts show <wasm function N>, and corerun.symbols maps those back to names. This matches the default for NativeAOT on wasm and Mono WASI (-Wl,-s). To keep names in the host, set WasmNativeStrip=false and WasmNativeDebugSymbols=false.
  • The prebuilt corerun in the runtime pack also carries about 34 MB of DWARF. It's excluded from app bundles, so that's left for a separate change.

Note

This pull request description was generated with GitHub Copilot.

@azure-pipelines

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

@lewing

lewing commented Nov 17, 2026

Copy link
Copy Markdown
Member Author

I'm exploring options here, there will likely be other changes even if we take this form now.

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

Debug and debugger-enabled WASI builds still receive flags that strip R2R debug information, while the component rewrite and symbol-map generation lack automated regression coverage.

2 open issues
What changed in this PR

This PR reduces CoreCLR WASI app size by stripping native debug information from per-app hosts and optionally preserving function names in a symbol-map sidecar.

Changes:

  • Adds component symbol stripping and corerun.symbols generation after ReadyToRun composition.
  • Introduces configuration-sensitive stripping defaults and ReadyToRun debug-information options.
  • Keeps function names in library-test hosts while removing DWARF, and documents the WASI-specific settings.
File Description
src/​tasks/​WasmAppBuilder/​StripWasiComponentSymbols.cs Adds the MSBuild task for stripping host symbols and tracking outputs.
src/​tasks/​Microsoft.NET.WebAssembly.Webcil/​WasmModuleInfoReader.cs Shares component rewriting logic and adds symbol extraction and removal.
src/​mono/​wasm/​build/​WasmApp.Common.targets Documents CoreCLR WASI stripping and symbol-map defaults.
src/​mono/​wasi/​build/​WasiApp.CoreCLR.targets Integrates stripping defaults, linker options, ReadyToRun settings, and host finalization.
eng/​testing/​tests.wasi.targets Configures test hosts to retain names without DWARF.

Review effort: Balanced

@lewing lewing added the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label Nov 20, 2026
@lewing lewing closed this Nov 22, 2026
@lewing lewing reopened this Nov 25, 2026
lewing and others added 4 commits November 24, 2026 21:13
The CoreCLR WASI build always relinks the wasihost per app, but the link
ignored WasmNativeStrip, so Release hosts carried the runtime pack's DWARF
(~29 MB of a 34 MB host for a hello world) and the wasm name section.

- Default WasmNativeStrip to false in Debug and true otherwise; WasmApp.Common
  only applies its Debug default when WasmBuildNative is set, which CoreCLR
  WASI forces to false.
- Link with --strip-debug when stripping (or when WasmNativeDebugSymbols is
  false), and compile the generated callhelpers with -g when keeping symbols.
- Add _WasiCoreCLRFinalizeHost, which runs after the ReadyToRun composition
  and, with WasmNativeStrip, drops the name section from the host's core
  module and writes the names to managed/corerun.symbols ('index:name', the
  emscripten symbol map format) unless WasmEmitSymbolMap is false.
- Keep function names (without DWARF) in the library test hosts so wasmtime
  backtraces in test logs stay readable.

Co-authored-by: Copilot App <[email protected]>
- Strip the R2R debug and inlining info unless Configuration is Debug or
  DebuggerSupport is set, as browser does. This halves the composite payload
  of a hello world (1,188,208 to 598,848 bytes).
- Pass --wasm-debug-info explicitly: 'name' when the merged host keeps its
  names or writes corerun.symbols, so managed R2R frames are symbolized, and
  'none' otherwise. crossgen2's symbol-map sidecar numbers functions within
  the composite rather than the merged host, so it isn't used.

Co-authored-by: Copilot App <[email protected]>
Microsoft.NET.CrossGen.targets already adds --strip-inlining-info and
--strip-debug-info for wasi-* unless the properties are explicitly false
(dotnet#134690), and it is imported before WasiApp.CoreCLR.targets, so the
Debug/DebuggerSupport exemption here could never take effect. CoreCLR wasm
does not consume that metadata, so drop the duplicate block and keep only
--wasm-debug-info.

Add a synthetic-component test for WasmComponentSymbols.StripFirstCoreModule
covering name and DWARF section removal, preservation of other sections and
later core modules, multibyte function indices, and the optional symbol map.

Co-authored-by: Copilot App <[email protected]>
@lewing
lewing force-pushed the lewing-strip-debug-coreclr-wasi-relink branch from 64b798a to ce31cf2 Compare November 25, 2026 03:19
@lewing lewing removed the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label Nov 25, 2026

This branch has not been deployed

No deployments
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.

2 participants