Skip to content

[browser][coreCLR] Export __stack_pointer global#128567

Merged
pavelsavara merged 4 commits into
dotnet:mainfrom
pavelsavara:browser__stack_pointer
May 28, 2026
Merged

[browser][coreCLR] Export __stack_pointer global#128567
pavelsavara merged 4 commits into
dotnet:mainfrom
pavelsavara:browser__stack_pointer

Conversation

@pavelsavara

Copy link
Copy Markdown
Member

Problem

The __stack_pointer WebAssembly global export was being removed from the final .wasm binary despite being explicitly requested via -sEXPORTED_FUNCTIONS=___stack_pointer and -Wl,--export=__stack_pointer.

This global is needed by libCorerun.js to share the stack pointer with dynamically loaded Webcil sub-modules (wasmExports.__stack_pointer is passed as an import to child wasm instances).

Root Cause

Emscripten's wasm-metadce pass (dead code elimination) unconditionally excludes all wasm global exports from its reachability graph:

# building.py line 766
exports = sorted(set(settings.WASM_EXPORTS) - set(settings.WASM_GLOBAL_EXPORTS))

Since __stack_pointer is a wasm global (not a function), it's never included in the DCE graph regardless of EXPORTED_FUNCTIONS. The wasm-metadce tool then considers it unreachable and removes it.

Metadce runs when OPT_LEVEL >= 3 or SHRINK_LEVEL >= 1 (i.e., -O3, -Os, or -Oz).

Fix

  1. Set -O2 for Browser Release builds in configureoptimization.cmake — this prevents metadce from running while still enabling wasm-opt optimizations.
  2. Add -Wl,--export=__stack_pointer to all Browser link targets (corerun, browserhost, BrowserWasmApp) to ensure wasm-ld emits the export.
  3. Add ___stack_pointer to EmccExportedFunction in eng/native.wasm.targets

Follow-up

We should re-visit this after we upgrade emscripten, we could have more efficient way how to do it

@pavelsavara pavelsavara added this to the 11.0.0 milestone May 25, 2026
@pavelsavara pavelsavara requested a review from kg May 25, 2026 18:51
@pavelsavara pavelsavara self-assigned this May 25, 2026
@pavelsavara pavelsavara requested a review from maraf as a code owner May 25, 2026 18:51
@pavelsavara pavelsavara added the arch-wasm WebAssembly architecture label May 25, 2026
Copilot AI review requested due to automatic review settings May 25, 2026 18:51
@pavelsavara pavelsavara requested a review from akoeplinger as a code owner May 25, 2026 18:51
@pavelsavara pavelsavara added area-Host os-browser Browser variant of arch-wasm labels May 25, 2026
@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.

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.

Pull request overview

This PR updates the browser-wasm (CoreCLR) build/link configuration to ensure the __stack_pointer WebAssembly global remains exported in optimized builds, so JS code can access wasmExports.__stack_pointer when instantiating related modules.

Changes:

  • Adds explicit linker export of the __stack_pointer global (-Wl,--export=__stack_pointer) and ensures exports.js is linked where needed.
  • Adjusts CoreCLR browser-wasm relink arguments to include the new export flag and reposition -lexports.js.
  • Updates shared wasm export lists and attempts to change browser Release optimization behavior to avoid toolchain DCE removing the global.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/native/corehost/browserhost/CMakeLists.txt Adds -lexports.js and linker export for __stack_pointer to the browserhost link.
src/mono/browser/build/BrowserWasmApp.CoreCLR.targets Updates CoreCLR relink args to include -lexports.js and -Wl,--export=__stack_pointer.
src/coreclr/hosts/corerun/CMakeLists.txt Adds -lexports.js and linker export for __stack_pointer; removes explicit ___stack_pointer from EXPORTED_FUNCTIONS list.
eng/native/configureoptimization.cmake Changes browser Release optimization intent to -O2 (to avoid metadce).
eng/native.wasm.targets Adds ___stack_pointer to the exported function list for browser targets.

Comment thread eng/native/configureoptimization.cmake
@kg

kg commented May 25, 2026

Copy link
Copy Markdown
Contributor

Thank you for getting to the bottom of this. I'll test it out soon.

@kg

kg commented May 26, 2026

Copy link
Copy Markdown
Contributor

I did gh pr checkout 128567, then rmdir artifacts, then did a release mode test run with dotnet run wasm-ryujit-runner.cs -- --auto-build --config Release --checkout Z:\runtime --assembly Z:\wasm-test\bin\Debug\net10.0\wasm-test.dll --corerun.

It failed:

/// Run '"node"  ./corerun.js -c /Users/kg/AppData/Local/Temp/wasm-ryujit-runner/sandbox/IL /Users/kg/AppData/Local/Temp/wasm-ryujit-runner/sandbox/IL/test-module.wasm' in cwd 'C:\Users\kg\AppData\Local\Temp\wasm-ryujit-runner\sandbox'...
Failed to construct WebAssembly instance for Webcil image: {
  wasmPath: '/Users/kg/AppData/Local/Temp/wasm-ryujit-runner/sandbox/IL/test-module.wasm',
  errorMessage: '__stack_pointer was not preserved by the linker or optimizer'
}
Unhandled exception. System.BadImageFormatException: Bad IL format. The format of the file '/Users/kg/AppData/Local/Temp/wasm-ryujit-runner/sandbox/IL/test-module.wasm' is invalid.
Aborted()

@pavelsavara

Copy link
Copy Markdown
Member Author

Thanks @kg !

I made another change here and then used your tool

image

Copilot AI review requested due to automatic review settings May 27, 2026 09:17

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.

Pull request overview

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

Comment thread src/mono/browser/build/BrowserWasmApp.CoreCLR.targets Outdated
Comment thread eng/native/configureoptimization.cmake
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 27, 2026 09:27

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.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Comment thread eng/native/configureoptimization.cmake
@kg

kg commented May 28, 2026

Copy link
Copy Markdown
Contributor

Helpers appear to be broken in Release builds atm but this indeed fixes running Hello World by preserving the export, thank you!

EDIT: This is just because your branch is based on a point in time before i fixed write barriers, so everything looks good.

@pavelsavara

Copy link
Copy Markdown
Member Author

/ba-g unrelated CI failures

@pavelsavara pavelsavara merged commit 87494fa into dotnet:main May 28, 2026
174 of 182 checks passed
@pavelsavara pavelsavara deleted the browser__stack_pointer branch May 28, 2026 07:26
@dotnet-milestone-bot dotnet-milestone-bot Bot modified the milestones: 11.0.0, 11.0-preview6 Jun 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-Host os-browser Browser variant of arch-wasm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants