Skip to content

[wasm] Add browser-wasm64 support for CoreCLR (interpreter and ReadyToRun) - #138027

Draft
lewing wants to merge 79 commits into
dotnet:mainfrom
lewing:lewing-wasm64-interpreter-support
Draft

lewing wants to merge 79 commits into
dotnet:mainfrom
lewing:lewing-wasm64-interpreter-support

Conversation

@lewing

@lewing lewing commented Nov 20, 2026

Copy link
Copy Markdown
Member

Adds single-threaded CoreCLR support for browser-wasm64 (memory64 and table64), on both the interpreter and ReadyToRun.

  • Build: ./build.sh -os browser -arch wasm64 builds like browser-wasm. It produces the wasm64 cross-JIT and crossgen2, an R2R System.Private.CoreLib.wasm, the framework R2R images, ICU from the transport package's browser-wasm64 assets, and the runtime pack, SDK and workload pieces. NativeAOT is skipped for wasm64.
  • Runtime: the VM, GC (segments, 8-byte alignment), interpreter, call helpers and thunks are ported to 64-bit pointers. The browser host, JS interop and Webcil loader handle 64-bit memory and tables.
  • JS: the bundles choose the pointer width at build time, so wasm32 output is unchanged.
  • Tests: src/tests Wasm conditions now cover wasm64.
  • Also included: a JIT VN null-safety fix, and wasm string-constructor thunk keys plus an unboxing-stub test. These apply to wasm32 too.

Validation (local, macOS arm64):

  • Full Release builds for browser-wasm and browser-wasm64 pass.
  • wasm64 library tests on V8, all with 0 failures: System.Runtime.Numerics (8633 run), System.Collections (33873 run) and JS interop (476 run, composite R2R). System.Globalization matches wasm32.
  • CoreRun runtime tests pass on Node. Test-project property evaluation for browser-wasm, wasi-wasm and linux-x64 is unchanged.

Notes:

Note

This PR description was generated with GitHub Copilot.

Edenlia and others added 30 commits November 20, 2026 12:41
Keep the wasm64 default and clr alias limited to clr.corelib. Extend the existing subset name and help targets with an optional supported expanded-subset policy instead of adding a wasm64-only validation target.

Select 64-bit CoreLib constants, intrinsic sources, saved-context offsets and isolated app-host paths while preserving wasm32 behavior. Follow the existing common target and Platform inference rather than introducing global wasm64 RID, Platform or feature guards.

Native runtime, code generation and toolchain support remain outside this change.

(cherry picked from commit 1ea9679)
Keep the existing MSBuild property tied to wasm32. Explicitly include wasm64 in the shared CoreCLR feature conditions and SDK tools exclusion.

(cherry picked from commit 0f4ff15)
Let callers select clr.corelib explicitly and preserve the existing default subset and alias expansion and validation rules.

(cherry picked from commit 1ea0587)
Conditions that check for Wasm in src/tests now cover wasm64 as well,
keeping the style of each line:

- Architecture limitations (Wasm linker/relink, R2R/crossgen2 of the Wasm
  target, Wasm intrinsics, no JIT, GC configuration) compare
  TargetArchitecture against both wasm and wasm64.
- Host/OS limitations (process launch, host policy, native path loading,
  JS engine runners, wrapper scripts, mobile-style merged runners) check
  TargetsBrowser/TargetsWasi; browser-only uses check TargetsBrowser.

browser-wasm, wasi-wasm and linux-x64 evaluate as before.

Co-authored-by: Copilot App <[email protected]>
fgMorphSmpOpOptional rewrites '(val + iadd) * imul' during global morph,
before value numbering exists. It already uses fgUpdateConstTreeValueNumber
for the folded constant but called fgValueNumberTreeConst directly for the
reused add constant, which dereferences the null vnStore.

Co-authored-by: Copilot App <[email protected]>
String constructors run as static factories that return the new string, so
their interpreter transition thunks use that lowered signature rather than
the instance constructor's. The runtime found them with hand-maintained
key tables for each argument count in both directions
(GetCookieForManagedMethod and GetPortableEntryPointToInterpreterThunk).
Those keys hard-code 32-bit pointers and miss any shape the tables do not
list.

Compute the key from the signature instead: return the string pointer and
drop 'this', as crossgen2 does. On wasm32 the computed keys are identical to
every table entry (Iiip, Iiiip, Iiiiip, Iiiiiip, IiS8p and their M
counterparts), so the lookups and the string constructor optimization are
unchanged.

Add string constructor coverage to WasmInterpreterTransitions for every
constructor shape, called from both R2R and interpreted code.

Co-authored-by: Copilot App <[email protected]>
Interface calls on a boxed value type enter through an unboxing stub that
skips the MethodTable pointer and, for shared code, passes the generic
context. Cover exact code (U), a MethodTable context with and without a
return buffer (UG), and a MethodDesc context (UM), from both R2R and
interpreted callers. Mutating through the box checks that the stub passes
the payload address.

Co-authored-by: Copilot App <[email protected]>
Select the wasm64 host and target in the native build scripts and CMake:
define HOST_WASM with HOST_64BIT and TARGET_WASM64/TARGET_64BIT instead of
the 32-bit wasm defines, compile and link with -m64 (exported for builds
outside CMake, such as the CoreRun test kit), and keep wasm32's explicit
-nostdlib library list for wasm32 only. Add the universal wasm64 cross-JIT
and gcinfo targets for crossgen2.

Co-authored-by: Copilot App <[email protected]>
Add CORINFO_ARCH_WASM64 and the wasm64 instruction sets (WasmBase and
PackedSimd, matching wasm32), and teach the Wasm JIT to emit pointer-width
code: i64 address, funclet and data-offset constants, pointer-typed frame
identities, i64 range checks and compares, br_table index saturation,
pointer-sized filter results, and the zero-extension a long-typed compare
needs after lowering. Update the JIT-EE version GUID.

Co-authored-by: Copilot App <[email protected]>
Make the Wasm-specific runtime code pointer-width aware for wasm64:

- Interpreter and unwind context fields, RtlVirtualUnwind and the stack
  walker use pointer-sized values.
- Write barriers, the generic dictionary lookup helpers (dynamichelpers.S),
  funclet calls, GC polls and the async continuation global use i64
  addresses and an LLVM-emitted table64 indirect call where the assembler
  cannot type-check one.
- Interpreter transition keys lower pointers to i64 (ToPointer) and drop
  the 32-bit size asserts.
- Code heaps skip the 64-bit personality routine and jump stubs, which
  Wasm does not use.
- Checked 64-bit multiply uses the managed helpers on wasm64, since Wasm
  has no overflow flag.
- 64-bit alignment padding (FEATURE_64BIT_ALIGNMENT) stays wasm32-only.

Co-authored-by: Copilot App <[email protected]>
Regions reserve a large address range up front. WebAssembly has no
reserve-only memory: the reservation grows linear memory, which is never
returned. Disable regions for HOST_WASM and give wasm64 the wasm32
workstation segment sizes.

DATA_ALIGNMENT was 4, so any GC on wasm64 crashed in plan_phase on the
64-bit object layout. Make it 8 (and CODE_SIZE_ALIGN 8) on wasm64, matching
crossgen2's object and Vector<T> alignment for the target.

Co-authored-by: Copilot App <[email protected]>
… end

When emscripten_stack_get_end() returns 0, the PAL moves the stack limit
off address zero with emscripten_stack_set_limits. It moved it by
sizeof(size_t). Emscripten writes its stack cookie 4 bytes in when the
stack ends at 0, but checks it at the current stack end. On wasm64 the end
became 8, so the check (run by handleException for any escaped exception)
read the wrong words and reported "Stack cookie overwritten at 0x8",
hiding the real error. Move the limit by 4, the value wasm32 already used.

Co-authored-by: Copilot App <[email protected]>
Let crossgen2's portable call-helper generator target browser-wasm64 and
check in its output under vm/wasm/browser-wasm64, beside the wasm32 tables.
The interp-to-native thunks pass structs and byrefs as 64-bit addresses,
string constructors are keyed on their static factory signature (the
wasm32 tables already contain those keys), and System.Globalization.Native
entries are guarded so the runtime links without ICU.
generate-coreclr-helpers.proj selects the architecture with
CallHelperTargetArchitecture.

crossgen2 accepts wasm64 only for --generate-portable-callhelpers here;
ReadyToRun compilation for wasm64 is still rejected.

Co-authored-by: Copilot App <[email protected]>
…ation otherwise

Restore the ICU transport and time zone packages for browser-wasm64 and use
only the package's runtimes/browser-wasm64 assets, never the wasm32
libraries. Build System.Globalization.Native and define FEATURE_WASM64_ICU
only when those assets exist; otherwise link CoreCLR without it. The
browser host defaults to invariant globalization on wasm64.

Co-authored-by: Copilot App <[email protected]>
Link the wasm64 CoreRun with WASM_BIGINT so 64-bit pointers cross the JS
boundary, and make libCorerun.js read and write pointer-sized out-params,
grow the table and import the table base with the module's address type.
Compile the CoreRun test kit with the same pointer-width options as the
runtime. With wasm64 ICU, load ICU data from DOTNET_ICU_DATA_PATH.

Co-authored-by: Copilot App <[email protected]>
The browser host, System.Native.Browser and JavaScript interop read and
write native memory through 32-bit views. On wasm64 pointers are BigInt at
the Emscripten boundary and occupy 8 bytes in native structures. Route
pointer conversions through shared helpers (pointers.ts) that convert to
Number only for checked heap indices, use 64-bit views for pointer-sized
fields, and give JSMarshalerArgument its wasm64 layout. wasm32 keeps its
existing layout and code paths.

Co-authored-by: Copilot App <[email protected]>
…owser-wasm64

With CROSSGEN2_EXPERIMENTAL_WASM64=1, --targetos browser and the wasm
container format, crossgen2 now compiles non-composite wasm64 images:

- The Wasm transition block, argument iterator, GC info and runtime
  function table treat wasm64 like wasm32, with 8-byte pointer slots.
- The webcil writer imports i64 memory, globals and table, and the
  R2R-to-interpreter, interpreter-to-R2R, import and virtual dispatch
  thunks use pointer-width loads, adds and constants.
- Fixups that store code addresses or take a reloc kind (resumption stubs,
  StoreMultiCallableAddrOfCode, string thunk injection, delay-load
  helpers, EH lookup) and the Vector<T> alignment use IsWasm.
- Explicit tail calls are left to the interpreter on both widths.
- Images root the R2R-to-interpreter thunk for the generic dictionary slow
  path, which the runtime calls directly.

The runtime keeps the interpreter as the default on wasm64 and loads R2R
code only when DOTNET_ReadyToRun is set explicitly.

Co-authored-by: Copilot App <[email protected]>
A closed static delegate whose target returns through a hidden buffer calls
a WasmClosedStaticRetBufThunk adapter. crossgen2 rooted the adapter only
when the 'this' and return-buffer parameters were i32, and its body used
i32 loads, so wasm64 images had none. The VM's adapter stayed without
native code and R2R calls through the delegate trapped on a null table
entry. Match the pointer type and emit pointer-width loads.

Co-authored-by: Copilot App <[email protected]>
On portable-entrypoint targets the runtime does not use crossgen2's IL
unboxing thunks: without a shared U/UG/UM stub it creates and interprets an
IL stub. The shared stubs were emitted only for wasm32, so on wasm64 every
call to a value-type method through a boxed receiver ran an interpreted
stub. Emit pointer-width stub bodies and use them on both widths. wasm32
images are unchanged.

Co-authored-by: Copilot App <[email protected]>
When CoreLib is part of the compilation, crossgen2 lets the JIT emit
speculative direct calls and finalizes them after code generation, keeping
them direct or rewriting them to indirect calls through the callee's
portable entrypoint. This ran only on wasm32.

The JIT's deferred-call bundle passes the callee's frame identity in the
entrypoint parameter; on wasm64 that parameter is i64, so emit i64.const
(the padded SLEB is sign-extended, as for the method's own frame identity)
and type the signature slot as i64. The rewriter's indirect fallback loads
the entrypoint with 64-bit loads into an i64 scratch local.

For a wasm64 ReadyToRun CoreLib this doubles direct calls (35.5k to 71.7k),
cuts call_indirect from 101k to 64k and shrinks the image by about 600 KB.
wasm32 output is unchanged.

Co-authored-by: Copilot App <[email protected]>
Add the browser-wasm64 RID, build the CoreCLR runtime pack for it (with an
IL CoreLib), and teach the WebAssembly SDK, in-tree app targets and workload
manifest to build and relink browser-wasm64 apps: -m64 native relink,
pointer-width call-helper generation, signature conversion for the
Emscripten JS boundary, and the wasm64 runtime pack paths.

Apps publish IL by default. With PublishReadyToRun they use non-composite
ReadyToRun: webcil staging then wraps only assemblies with an R2R image and
passes the rest through as IL. The nested publish used for native relink
no longer generates or registers a publish boot JSON.

Co-authored-by: Copilot App <[email protected]>
Let src/tests build, lay out and run tests for -arch wasm64 -browser
through the Node/CoreRun wrappers:

- build.sh, run.sh and run.cs accept wasm64.
- The wasm test conditions use TestTargetsWasm.
- The per-test CoreRun relink and its generated call helpers use the
  target architecture.
- Core_Root gets the IL CoreLib and the portable RID graph.

ReadyToRun runs set CROSSGEN2_EXPERIMENTAL_WASM64 and DOTNET_ReadyToRun
and are non-composite only. The README documents the local workflow,
including the memory64 test-native build.

Co-authored-by: Copilot App <[email protected]>
crossgen2 rejected --composite for wasm64, and the runtime test wrappers
refused CompositeBuildMode. Composite images, of the test assemblies or
whole-program with CoreLib, already load and run on wasm64, and match
wasm32 on the bring-up, GC API, P/Invoke and transition test slices. Lift
both restrictions, so AlwaysUseCrossGen2 tests are composite on wasm64 as
they are on wasm32.

Co-authored-by: Copilot App <[email protected]>
Describe how to build the wasm64 runtime, cross-JIT and crossgen2, generate
the wasm64 call helpers, run CoreRun with and without ReadyToRun, use wasm64
ICU, and publish the in-tree browser sample. Record the decisions that
crossgen2 and the runtime must agree on (GC configuration and object
alignment, string constructor keys, closed static return-buffer thunks,
unboxing stubs, deferred direct calls) and what is not yet validated.

Co-authored-by: Copilot App <[email protected]>
…oundary

The managed side passes GCHandles and JS handles to the JS-implemented
interop imports as nint. On wasm64 an i64 parameter arrives in JS as a
BigInt unless the import declares an Emscripten pointer signature, so
CancelPromise's handle missed the Number-keyed JS-owned object table and
aborted the JS interop tests. Declare 'p' for the handle parameters of
ReleaseCSOwnedObject, CancelPromise and InvokeJSFunction, and declare the
native prototypes with intptr_t to match.

Array marshaling sized its buffer from value.length before checking the
value's type, so a non-array value allocated NaN bytes. wasm32 coerces that
to 0, but wasm64 converts the size to a BigInt and threw a RangeError
instead of the expected type-check error. Treat a missing length as 0.

Co-authored-by: Copilot App <[email protected]>
An IntPtr crosses to JavaScript as a number. The wasm64 marshaler treated
every IntPtr as a heap address and rejected values outside linear memory.
Marshal the full 64-bit value instead, and throw a RangeError when it cannot
be represented exactly (beyond 2^53). wasm32 keeps its 32-bit path.

The shared IntPtr test cases use values above 4 GiB that a number
represents exactly on 64-bit targets, a new test checks the RangeError, and
StructSize expects the 56-byte wasm64 JSMarshalerArgument layout.
System.Runtime.InteropServices.JavaScript.Tests pass on browser-wasm64 and
browser-wasm in interpreter mode.

Co-authored-by: Copilot App <[email protected]>
Treat wasm64 like wasm in the in-tree build:

- Build the clrjit_universal_wasm64 cross-JIT as the cross component and
  copy it next to crossgen2.
- crossgen-corelib.proj compiles System.Private.CoreLib.wasm.
- The runtime pack includes framework ReadyToRun images under native/r2r.
- Skip NativeAOT, which does not support wasm64.

The src/tests layout no longer needs the wasm64-only crossgen2 directory and
IL CoreLib copies, since the build now produces both.

Co-authored-by: Copilot App <[email protected]>
- The runtime uses ReadyToRun code unless DOTNET_ReadyToRun=0.
- crossgen2 no longer requires CROSSGEN2_EXPERIMENTAL_WASM64 for browser
  Wasm output.
- The SDK no longer special-cases browser-wasm64 for Webcil, composite
  ReadyToRun or DOTNET_ReadyToRun. It keeps the invariant globalization
  default.

Co-authored-by: Copilot App <[email protected]>
The self-installing IL Webcil wrapper imported an i32 memory and
__memory_base, so V8 rejected it on a memory64 host. Add a memory64 variant
(i64 memory, __memory_base and getWebcilSize destination) and select it
for browser-wasm64 through ConvertDllsToWebcil.Memory64.

Co-authored-by: Copilot App <[email protected]>
- Trimming tests run browser apps through run-v8.sh on browser-wasm64 too.
- Native relinking and native-process test exclusions cover wasm64.
- Wasm crossgen2 response files skip --embed-pgo-data by output format.

browser-wasm, wasi-wasm and linux-x64 evaluate as before.

Co-authored-by: Copilot App <[email protected]>
@lewing

lewing commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

/azp run runtime-libraries-coreclr outerloop-linux

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command.

@lewing

lewing commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

/azp run runtime-libraries-coreclr outerloop-linux

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

The native EventPipe test runtime and test callbacks still declared the
callback's control code as unsigned long, which no longer matches the
EventPipeCallback typedef.

Co-authored-by: Copilot App <[email protected]>
@lewing

lewing commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

/azp run runtime-libraries-coreclr outerloop-linux

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

lewing and others added 4 commits November 24, 2026 11:33
StackHelper.CallOnEmptyStack uses Task.Run and Wait, which single-threaded
browser runtimes don't support. The test is 64-bit-only, so it first ran on
browser with wasm64.

Co-authored-by: Copilot App <[email protected]>
WritingTooLargeBase64Bytes and WriteTooLargeArguments take hours on the
browser interpreter, timing out the wasm64 outerloop suite.

Co-authored-by: Copilot App <[email protected]>
System.Memory.Tests takes ~2h54m on wasm32 and exceeds the 4-hour suite
timeout on wasm64, which is slower and also runs the 64-bit-only tests.
Raise the wasm64 suite timeout and Helix batch bounds; the AzDO job
timeout (600 minutes) still covers them.

Co-authored-by: Copilot App <[email protected]>
@lewing

lewing commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

/azp run runtime-libraries-coreclr outerloop-linux

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@lewing

lewing commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Still using this to test outerloop fixes and changes because it has a shorter cycle time than the review stack.

lewing and others added 2 commits November 24, 2026 22:23
CanResizeToMaxArraySize and Double.ToString_ValidLargeFormat run on
browser-wasm64 because it is a 64-bit process. They grow wasm linear
memory to 8-10 GB, which never shrinks, so Chrome stalls on Helix machines
and the Common.Tests and System.Runtime.Tests suites hit the work item
timeout.

Co-authored-by: Copilot App <[email protected]>
The load/store and logical-shift tests assumed four 32-bit lanes per
Vector128<nint>, which fails on browser-wasm64 where nint is 64 bits.

Co-authored-by: Copilot App <[email protected]>
@lewing

lewing commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

/azp run runtime-libraries-coreclr outerloop-linux

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@lewing

lewing commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

/azp run runtime-libraries-coreclr outerloop-linux

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

Browser test apps don't inherit the host environment, so the wasm runner
scripts now forward DOTNET_CI=true to the app on Helix. That lets
[SkipOnCI] work in the browser and backs the new
PlatformDetection.IsNotBrowserOnCI condition.

The 64-bit-only multi-gigabyte tests that get OOM-killed in Helix
containers on browser-wasm64 (ArrayBuffer.CanResizeToMaxArraySize,
Double.ToString_ValidLargeFormat, the Utf8JsonWriter too-large tests,
Base64/Base64Url EncodeTooLargeSpan and
CreateReadOnlySpanFromNullTerminated_*_ExceedsMaximum) now skip only on
browser CI instead of on every browser run.

Co-authored-by: Copilot App <[email protected]>
@lewing

lewing commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

/azp run runtime-libraries-coreclr outerloop-linux

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

This branch was previously deployed

1 inactive (outdated) deployment
copilot-pat-pool — 081fd5c3 Deployed Nov 20, 2026 by lewing via conclusion #12957
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch-wasm WebAssembly architecture area-ReadyToRun NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) NO-REVIEW Experimental/testing PR, do NOT review it os-browser Browser variant of arch-wasm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants