Skip to content

ariadne_wasm: run guests in the browser under Asyncify (G2, first part) - #1197

Merged
transfix merged 6 commits into
masterfrom
feat/ox-browser-guests
Nov 14, 2026
Merged

transfix merged 6 commits into
masterfrom
feat/ox-browser-guests

Conversation

@transfix

Copy link
Copy Markdown
Owner

What it adds

G2's first part (ARIADNE_SCRIPTING_HOSTS.md §8.5, "Gas"; §10 row G2). WebAssembly guests now run in a single-threaded browser page with the same slices, limits, access and hooks they get natively. Nothing needs Rust yet, and the native path behaves as before.

  • Install-time transform. cvc_ariadne_wasm_transform (ariadne_wasm.cmake) runs emsdk's binaryen: wasm-opt --log-execution=ari --asyncify --pass-arg=asyncify-imports@ari.* -O1. The result:

    • Every function entry, loop header and return calls ari.log_execution, which is the gas check.
    • Every ari import is a point where the guest can pause.

    cvc_find_wasm_opt looks for wasm-opt beside emcc, under $EMSDK, on PATH, or at -DCVC_WASM_OPT.

  • Browser backend (browser_runtime.cpp, Emscripten only). A guest is a WebAssembly.Instance beside the host's module. Its JS state lives in Module.cvcWasmGuests; there are no C++ statics.

    • Fuel. A check costs kFuelPerCheck (16) units of fuel. The guest pauses at the check that spends its slice, or in an import that waits. Asyncify unwinds it into a room taken from its own ari_alloc, and the next poll rewinds it.
    • The room. Its size is ariadne.limits.wasm_unwind_bytes, 32 KiB by default. Asyncify writes a paused stack without checking it. A stack past the room therefore traps with the setting's name, and the guest refuses every later call.
    • Setup. run() and a start function can't pause, so a spent slice or a wait there traps.
    • Refused modules. A .wat module and an untransformed guest are refused, and the message names the transform.
    • Import types. JS checks no import's signature. The browser reads the guest's type and import sections (function_imports) and refuses any ari import whose signature differs from the host's, as wasmtime's linker does.
    • Compile cache. A module compiles once for each memory cap it runs under. A compile that fails above 8 MiB names Chrome's limit on the page's thread.
  • wasm_binary.{h,cpp}. cap_memory lowers a module's memory maximum to wasm_max_memory, or gives it one, so the page refuses a memory.grow past the cap as wasmtime's store limiter does. It refuses an imported, shared or 64-bit memory, and a minimum above the cap.

  • GuestMemory. It copies data in and out (size, read, write, plus map where the memory is the host's), because no host pointer reaches the page's guest memory. The engine's memory sites moved to it with no change in behaviour.

  • wasmtime defines ari.log_execution as a no-op, so transformed guests instantiate natively too. Its fuel still meters them.

  • Tests and CI.

    • In an Emscripten tree, CVC_BUILD_WASM_GUEST_TESTS builds ariadne_wasm_browser_test, with the transformed guests embedded at /guests.
    • Natively, the same file builds as ariadne_wasm_transform_test. Its transformed cases run where wasm-opt is found.
    • ci.yml gains a path-filtered wasm-guests (wasm, node) job. It builds a plain-wasm, single-threaded libcvc and runs the browser test under the emsdk's node.

How it was tested (Windows, sandipaws)

  • Native ctest (build-ox-browser-guests, CTEST_RE=Wasm|ariadne_wasm|ScriptEngine|wasm): 76/76 passed. These include ariadne_wasm_test, ariadne_wasm_transform_test, ariadne_script_engine_test, arix_wasm_block and ari_wasm_needs_arix. The transformed cases of ariadne_wasm_transform_test ran, because wasm-opt is passed in on this box.
  • ariadne_wasm_browser_test under emsdk node 22.16 (build-ox-browser-guests-web): 21/21 passed. That is WasmBinary 4, WasmTransformed 13 and WasmBrowser 4.
  • ari and arix rebuilt in the native tree: BUILD_EXIT=0.
  • clang-format 18.1.8 (git-clang-format --diff against the merge base with master): no changes left.

What is left out

  • F3 (making the CI claim true). package-linux still has no wasm-opt, so CI never runs the transformed cases of ariadne_wasm_transform_test natively. I couldn't fix that from here:

    • The emsdk is a multi-GB download.
    • The cvcpkg catalog has no binaryen package.
    • package-linux is a required check, and I can't test it from this box.

    Real coverage needs a binaryen package on cvcpkg, or a deliberate decision to install the emsdk in one package-linux lane.

  • F6 (asynchronous compile). Compiling with WebAssembly.compile, awaited by the document's load, waits for the ariglx-web wiring. For now there is only the compile cache and the clearer message for modules over 8 MiB.

  • F2 (retry with a bigger room). After an overflow the guest stays broken until its document is reloaded. It is not re-instantiated automatically.

  • Docs. ariadne.limits.wasm_unwind_bytes is documented only in the roadmap's §8.5 as-built note, not in docs/ARIADNE.md, because the browser backend isn't wired into any host yet.

  • Still left for G2: the weighted counters built through cvcpkg's Rust crates, wiring guests into ariglx-web with a headless-Chrome smoke test, and measuring JSPI against Asyncify.

Merging waits on Joe's review.

#1159 (O7c) kept the last source sea_surface found in a thread_local, so
the velocity a fluid field reads after a cell's height at the same point
and time did not invert the sideways move again. A thread_local that
keeps state between calls is a static cache, which libcvc's no-singletons
rule forbids (VOLROVER-LIBRARY-PORTS-ROADMAP.md 1.3,
tools/singleton_scan). #1159's release-grpc job, where the scan runs, was
cancelled, so the scan never judged it; the next PR that merges master
would fail that lane on it.

The memo now belongs to the caller:

- sea_surface::source_memo, and height() / velocity() overloads that take
  one. The plain reads share nothing.
- fluid_surface::height_with / velocity_with and a surface_memo the
  caller owns; the defaults ignore it. ocean_surface passes it to the sea.
- field_set::apply keeps one memo per cell, for the cell's height and its
  velocity at the same clipped centre.

The memo is keyed on the point, the time and the digest as before, so
every value is unchanged.
A browser backend for guest_runtime.h, and an install-time transform that needs no Rust, so a
document's WebAssembly guests get the same slices, limits, access and hooks in a single-threaded
page as natively (ARIADNE_SCRIPTING_HOSTS.md 8.5). Natively nothing changes, and wasmtime runs the
same transformed guests.

- cvc_ariadne_wasm_transform (ariadne_wasm.cmake) runs emsdk's binaryen: wasm-opt
  --log-execution=ari --asyncify --pass-arg=asyncify-imports@ari.* -O1. Every function entry,
  loop header and return calls ari.log_execution, the gas check, and every ari import is a point
  the guest may pause at. cvc_find_wasm_opt finds wasm-opt beside emcc, under $EMSDK, on PATH, or
  at -DCVC_WASM_OPT.
- browser_runtime.cpp (Emscripten): a guest is a WebAssembly.Instance beside the host's module,
  made in EM_JS with a trampoline per Import into cvc_ari_wasm_import. Its JS state lives in
  Module.cvcWasmGuests[<the instance>], with no C++ statics. A check costs kFuelPerCheck (16) units
  of fuel. The guest pauses at the check that spends its slice, or in an import that waits:
  Asyncify unwinds it into 16 KiB from its own ari_alloc, and the next poll rewinds it. Setup
  (run(), a start function) can't pause, so there a spent slice or a wait traps. A .wat module and
  an untransformed guest are refused, naming the transform.
- wasm_binary.{h,cpp}: cap_memory lowers a module's memory maximum to wasm_max_memory (or gives it
  one), so the page refuses a memory.grow past it, as wasmtime's store limiter does. It refuses an
  imported, shared or 64-bit memory and a minimum above the cap.
- GuestMemory is copies in and out (size, read, write; map where the memory is the host's), since
  no host pointer reaches the page's guest memory. Instance::memory() returns it. The engine's
  memory sites moved to it with no change in behaviour: the data calls stage their output in
  GuestOut and their input in GuestIn, which map the memory natively.
- wasmtime defines ari.log_execution as a no-op, so transformed guests instantiate there; its fuel
  still meters them.
- CVC_BUILD_WASM_GUEST_TESTS builds ariadne_wasm_browser_test in an Emscripten tree with the tests
  off (googletest from source, the transformed guests embedded at /guests). Natively the same file
  is ariadne_wasm_transform_test, whose transformed cases run where wasm-opt is found.
- ci.yml: a path-filtered wasm-guests (wasm, node) job builds a plain-wasm, single-threaded
  libcvc and runs ariadne_wasm_browser_test under the emsdk's node.

Left for G2: the weighted counters through cvcpkg's Rust crates, wiring guests into ariglx-web
with a headless-Chrome smoke, and JSPI measured against Asyncify.

Tests: WasmBinary.{CapsTheMemoryMaximum,LowersAnExistingMaximumOnly,RefusesWhatItCantCap},
WasmTransformed.* (exports, parked calls, a yield, a spin sliced at its gas checks and killed by a
step cap, a deep stack paused and resumed, traps and fail(), state imports resumed in the slice, a
grow past memory_mb, a volume read into the guest's memory, a frame hook) and
WasmBrowser.{AWatModuleIsRefused,AnUntransformedGuestIsRefused}: 16/16 under node 22.16
(ariadne_wasm_browser_test) and natively on wasmtime (ariadne_wasm_transform_test);
ariadne_wasm_test, ariadne_script_engine_test, arix_wasm_block and ari_wasm_needs_arix unchanged
and green.
… its unwind room breaks it

Review fixes to G2's first part (browser_runtime.cpp, ARIADNE_SCRIPTING_HOSTS.md 8.5).

- JS checks no import's signature, so a guest declaring `ari.call (param i32 i32)` ran, and the
  host's side read parameters past its span. wasm_binary's function_imports reads the guest's
  type and import sections, and the browser refuses an `ari` import whose parameters (an i64
  where Import::wide marks one) or result differ from the host's, or one the host doesn't
  define, before it instantiates the module, as wasmtime's linker does natively. dispatch()
  also traps when the arguments it gets aren't the import's count.
- Asyncify writes a paused stack unchecked and checks only in asyncify_stop_unwind, after it
  has written past the room. cvc_wasm_call reads the room's header first: a stack past it traps
  with what it took and the setting's name, and the guest refuses every later call, since its
  memory after the room is gone. The room is ariadne.limits.wasm_unwind_bytes
  (InstanceLimits::unwind_bytes), 32 KiB by default (it was a fixed 16 KiB).
- A module is compiled once for each memory cap it runs under and kept with its BrModule
  (Module.cvcWasmModules), not on every instantiate. A compile that fails past 8 MiB names
  Chrome's limit on the page's thread; the asynchronous compile is left for the ariglx-web
  wiring.
- tests/CMakeLists.txt builds ariadne_wasm_test and ariadne_wasm_transform_test natively only:
  under Emscripten the find macro finds the browser backend, which compiles no .wat.
  CVC_BUILD_WASM_GUEST_TESTS with CVC_BUILD_TESTS on now warns that it is ignored.
- ci.yml: the wasm-guests path filter also covers the CMake, the libcvc recipe and the
  AppRuntime and state code the test drives. Its comment and the roadmap's Tests bullet no
  longer claim the package lanes run the transformed cases natively: no package lane has a
  wasm-opt, so there only the cases that need no transformed guest run.

Tests: WasmBinary.ReadsTheImportsTypes, WasmTransformed.AnImportOfAnotherTypeIsRefused and
WasmTransformed.AModuleServesEveryDocument on both backends;
WasmBrowser.AStackPastTheUnwindRoomBreaksTheGuest and WasmBrowser.TheUnwindRoomIsASetting
under node. 21/21 under node 22.16 (ariadne_wasm_browser_test); natively ariadne_wasm_test,
ariadne_wasm_transform_test, ariadne_script_engine_test, arix_wasm_block and
ari_wasm_needs_arix green (76/76).
@transfix
transfix disabled auto-merge November 13, 2026 09:37
@transfix
transfix enabled auto-merge (squash) November 13, 2026 09:37
transfix added a commit that referenced this pull request Nov 13, 2026
# Conflicts:
#	docs/roadmap/ARIADNE_SCRIPTING_HOSTS.md
#	src/cvc/tests/ariadne_wasm_test.cpp
…I bound in the browser

#1199's WASI preview 1 stubs reached the guest's memory through
GuestMemory::bytes(), which this branch replaced with copies (size, read,
write, map), because no host pointer reaches the page's guest memory.

- wasi_preview1.cpp reads and writes by copies: inside() checks a range,
  store_u32/store_u64 write a value, and the iovecs and poll records are
  read in before they're walked. random_get fills in 4 KiB chunks, so a
  large request never takes a host buffer its size. The errnos are as
  before (EFAULT outside the memory).
- browser_runtime.cpp binds each import under its own module (the spec
  is "module:name:returns;"), so a WASI guest's wasi_snapshot_preview1
  imports are there in the page, and gives an i32 result (WASI's errno)
  as a Number, an i64 as a BigInt. imports_match checks every module's
  imports against the host's, the i32 results among them.
- ariadne_wasm.cmake: wasi_preview1.cpp joins the common sources, since
  wasm_engine.cpp calls wasi_imports() on both backends.
- cvc_ariadne_wasm_transform passes binaryen CVC_WASM_GUEST_FEATURES (bulk
  memory, sign extension, multi-value, reference types, SIMD, extended
  constants, tail calls): it reads only the MVP otherwise, and a guest from
  clang or rustc, or the WASI test guest, uses more.
- The test guests include wasi_guest.wat, transformed, and
  WasmTransformed.AWasiGuestRunsOnTheStubs runs its clocks, random
  numbers, args, stdout and a sleep on both backends.
@transfix
transfix disabled auto-merge November 14, 2026 01:43
@transfix
transfix enabled auto-merge (squash) November 14, 2026 01:43
@transfix
transfix disabled auto-merge November 14, 2026 01:43
@transfix
transfix enabled auto-merge (squash) November 14, 2026 01:43
transfix added a commit that referenced this pull request Nov 14, 2026
- The transform keeps its three steps, with #1197's CVC_WASM_GUEST_FEATURES
  on both wasm-opt runs (the WASI test guest's bulk memory).
- meter.cpp and wasi_preview1.cpp are both common sources.
- wasm_engine.cpp: #1199's _initialize message, on kSetupGas.
- wasmtime_runtime.cpp: #1199's set_result; the log_execution no-op stays gone.
- browser_runtime.cpp: the imports bound under their own modules, with ari.gas
  the gas pass's import, checked as (param i64).
- Tests: the spin's count and the WASI guest on both backends, side by side.
@transfix
transfix merged commit accadc3 into master Nov 14, 2026
28 checks passed
@transfix
transfix deleted the feat/ox-browser-guests branch November 14, 2026 08:51
transfix added a commit that referenced this pull request Nov 14, 2026
…ted-gas

#1197 is in master as its squash, so the conflicts were #1197's own lines
against this branch's changes to them: this branch's are kept.

Two compile breaks on master, fixed here as #1203 fixed its own callers:
- ariadne_wasm_browser_test.cpp (#1197) called register_script_engine(engine)
  and unregister_script_engine("wasm"), which #1203 removed: the fixture
  registers nothing, as each test's AppRuntime calls use_script_engine.
- ariadne_runtime_test.cpp (#1180) called load_string(yaml), which #1203
  made load_string(app, yaml): the three calls pass the test's app.
transfix added a commit that referenced this pull request Nov 14, 2026
…app (master builds again) (#1229)

#1197's ariadne_wasm_browser_test.cpp (built natively as ariadne_wasm_transform_test wherever
wasmtime is found, and in the wasm-guests node job) still called register_script_engine(engine)
and unregister_script_engine("wasm"), which #1203 replaced with the (app, ...) forms. #1197's CI ran
before #1203 landed, so master at accadc3 doesn't compile it.

As #1203 did for ariadne_wasm_test.cpp: SetUp no longer registers the engine, and the TearDown
that unregistered it goes. Each of the file's 18 AppRuntimes already calls
use_script_engine(engine), which registers it on that runtime's app.
transfix added a commit that referenced this pull request Nov 14, 2026
CI at 9d002d1 built it on master as of 09:28, after #1197 added
ariadne_wasm_browser_test.cpp with register_script_engine calls that
predate #1203 (all three package-linux lanes and wasm-guests failed to
compile it). Master fixed that in #1229, and the load_string calls
#1169 left in the loader and chart tests in #1190. This merge takes
master as it is now, so CI builds on those fixes. No conflicts.

Master's new natives (#1190's file-pick and http-upload, #1195's
py_console, #1169's chart) build against this branch's call_args
context as they are.

Checked on Windows (MSVC Release): full build; the Ariadne, net, HTTP,
state_exec, groups, slices, async-call, cycle-registry and multi-app
ctests, 1336/1336. ariadne_wasm_browser_test is not built here (it needs
wasmtime or Emscripten); master's CI builds it.
transfix added a commit that referenced this pull request Nov 14, 2026
…#1222-#1225, #1229)

Master's #1190 (file-pick and http-upload) was written for a single async host; this branch runs
an isolated document's programs on its own group's host, on a pool worker in an `any` group.

- net_intrinsics.cpp: StartedRequest takes master's deliver()/on_outcome, settling through the
  host's finisher. http-upload is its own provider, `any` as the requests are: it parks on the
  calling program's host (ictx.host), takes its progress path from ictx.root_path (no tree walk
  on a worker), and a started upload (a browser page) keeps the host's finisher, not the host.
- ariadne.cpp: file-pick, file-info and file-forget bind into the program's env as they are, in
  every group (install_backend_verbs), not through the backend verbs' host_proxied wrapper, which
  cannot park an async verb. file-pick parks on the program's own host, claims the Runtime's
  picker hub under its lock where it is called, and opens the picker on the host thread
  (exec_pool::run_on_host; inline when called there). Its answer goes through the host's
  finisher, as the host may be gone by then.
- ci.yml: the sanitizer list keeps this branch's tests and master's point_classes_test.
- STATE_EXEC_THREADED.md: rows for http-upload, the file verbs, and the contours and
  point-classes providers. The last two are registered without a kind, so they are host_only, and
  an isolated document in an app that installs them goes to a host-affinity group.
- New test: AriadneIsolatedDocs.FilePickOpensItsPickerOnTheFramesThread.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant