Skip to content

ariadne: wasm guests reach host objects by handle, and MemoryError (G1, third part) - #1099

Merged
transfix merged 13 commits into
masterfrom
feat/wasm-guests-3
Nov 9, 2026
Merged

transfix merged 13 commits into
masterfrom
feat/wasm-guests-3

Conversation

@transfix

@transfix transfix commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

G1, third part: WebAssembly guests reach host objects (volumes, meshes, height fields) by handle and copy only the regions they read. This is the reading half of the roadmap's §8.4.1 (the design in #1092). A guest that runs out of memory at its limit now raises a MemoryError.

On master since #1094 merged (ad8b075). Master is merged in at 11b5b05. The conflicts were #1094's own lines, squashed, against this branch's edits of them, so this branch's side is kept. #1075's roadmap changes come in untouched. The branch also carries #1094's copy of #1095's hunk for NavSimWorldAgents.EachPolicyDrivesItsOwnAgents, which fails on macOS because scatter_free's draw differs under libc++.

What it builds

  • Handles in place of values (value_cbor).
    • A value holding a data_object crosses as a handle: CBOR tag 0x61726968 ("arih") over an unsigned integer. A handle the guest hands back (as a result, or as a call argument) is the same object again.
    • Without a handle table, a host object still doesn't cross, as before.
  • The instance's handle table.
    • It pins the object, the version the guest was handed, until handle_drop.
    • Each slot has a generation, so a dropped handle names nothing even after its slot is reused.
    • At most 65,536 live handles per instance.
  • Data calls (imports in module ari; h is an i64 handle, the rest are i32s).
    • handle_drop(h)
    • data_info(h): the object's type and sizes, as CBOR; no data moves.
    • volume_read(h, x, y, z, nx, ny, nz, lod, dst, len): a box, every 2^lod-th voxel, in the volume's own voxel type.
    • volume_sample(h, points, n, dst, len): interpolated values at points.
    • mesh_read(h, attr_ptr, attr_len, first, count, dst, len), the attribute's name in guest memory: positions or normals (f64 ×3); indices, quads or lines (u32).
    • heightfield_read(h, row, col, rows, cols, dst, len): f32 heights.
    • Each copies straight into the guest's memory and returns the bytes written, or a negative length with the error's text for reply(), as call does.
    • Each is charged a step per kDataNodeBytes (256) moved, so bulk copies count against the action caps.
    • Import::wide marks i64 parameters.
  • imports: gains data.read and data.write. Without data.read, the data calls are a PermissionError. data.write is reserved for the writers.
  • MemoryError. A memory.grow past the limit still gives the guest -1. A trap while the memory is at its limit (Instance::memory_at_limit, which checks both the store's cap and the module's own maximum) is now a MemoryError rather than a WasmError. The guest's own fail() stays a WasmError.

Docs

  • wasm_engine.h's ABI notes.
  • Roadmap §8.4.1 (from docs(roadmap): guests reach host objects by handle, and copy only what they read #1092): "As G1's third part builds it", the reading half's calls as built, and what's left.
  • ARIADNE.md §9.6.
  • Roadmap §8.3: "G1's third part, built", plus the shorter "Left for G1" list and the G1 row.
  • The errors bullet in the roadmap's limits section.
  • The MemoryError row in STATE_EXEC_EXCEPTIONS.md.

Tests

Locally on Windows, in a static Release tree:

  • ariadne_wasm_test: 18/18, three runs.
    • New codec test HostObjectsCrossAsHandles.
    • New engine tests through a WAT data guest: AVolumeCrossesAsAHandle (info, a box, every other voxel, a sample, a handle back as itself from an argument and from the guest's own state-data-get), AMeshAndAHeightField, ADroppedHandleNamesNothing (and a box outside the volume), ImportsNarrowTheDataCalls, AGuestOutOfMemoryIsAMemoryError.
  • ariadne_script_engine_test: 20/20.
  • state_exec_exceptions_test: 25/25.
  • The arix_wasm_block and ari_wasm_needs_arix ctests pass.

Not covered by a test: the byte charge (a step per 256 bytes). Only the code applies it.

Left for G1

  • In-program sliced calls.
  • The pump budget counting slice steps.
  • The lock manager for the instance.
  • The key, pointer, signal and message hooks.
  • The dedicated state imports.
  • §8.4.1's writes: volume_write, volume_new.
  • A host option for state_exec.policy.
  • wasmtime in the recipes.

The seam G1 (WebAssembly guests on wasmtime) and L1 (Lua) plug into beside
Python (docs/roadmap/ARIADNE_SCRIPTING_HOSTS.md 3.1, 8.3-8.7).

- inc/cvc/ariadne/script_engine.h: ScriptEngine (language, has_module,
  load_block, unload), registered once per process by language
  (register_script_engine), which makes its block a key the loader keeps
  and its tokens hold; set_script_engine goes. ScriptBlock is what a
  block hands its engine: the block, the document's path, dir and prefix.
- ScriptLane, one per runtime and engine (AppRuntime::use_script_engine):
  the document's function registry, activations, and hooks (an on: event
  as an action, every frame and a changed value as a resident, one in
  flight).
- Activations run like sliced tasks: a process of the document's program
  lane (Runtime::start_process: its owner tag, chroot, channel policy,
  lock manager, functions: and ariadne.limits.* caps, which an activation
  may lower and never raise), a DSL loop around (script-step) that steps
  a ScriptActivation one slice at a time. A slice charges DSL steps, so
  the step cap is the gas cap; it may finish, yield the frame, await a
  future, or call any function the lane holds, which the loop applies in
  the process (narrowed by may_call). A language's activations share
  ariadne.limits.<language>_frame_budget (0.008 s) of each frame
  (exec_frame).
- Python's engine is a ScriptEngine: it answers the tokens and runs the
  python: block in load_block, and attach() uses it instead of a load
  hook. What a document sees is unchanged.
- Tests: ariadne_script_engine_test (a stub engine through all of it);
  the AriadneRequires token test through a stub Python engine.
A document's wasm: block runs a compiled plugin in arix and ariglx, as a
ScriptEngine on P1b's interface (docs/roadmap/ARIADNE_SCRIPTING_HOSTS.md
8.2-8.7; ARIADNE.md 9.6).

- src/ariadne_wasm: the runtime interface (guest_runtime.h) and its
  wasmtime backend, where each slice is one wasmtime_call_future_poll
  under a fuel yield interval and an import that waits is an async host
  function; a CBOR codec for DSL values; and the engine: the wasm:
  block (module, export, hooks, imports, limits, priority), an instance
  per document, _initialize, and the guest ABI (values as CBOR in the
  guest's memory, ari_alloc, the call, reply, yield and fail imports).
- ScriptLane::export_activation: a function whose calls are activations
  the caller parks on (an async function whose work starts itself, so no
  pool thread, and the wait counts against the caller's caps), with
  -async futures. AppRuntime::register_async_work_function under it.
  ActivationOutcome keeps a failed step's exception, so it re-raises
  with its own class.
- Fuel is charged as DSL steps (ariadne.limits.wasm_gas_per_step), under
  the action caps and the wasm frame budget; one activation per
  instance; a guest calling its own export is refused; imports:
  narrows its calls (state.read, state.write, msg, net, call).
- WasmError joins the built-in classes (18), the wasm token and the
  loader's warning for a wasm: block on a host without guests.
- arix and ariglx carry the engine where wasmtime's C API is found
  (-DARI_WITH_WASM / -DARIGL_WITH_WASM require it); CI's package-linux
  installs the cvcpkg wasmtime package.
- Tests: ariadne_wasm_test (a WAT guest), the export_activation stub
  case, and the arix_wasm_block / ari_wasm_needs_arix ctests.
…cond part)

ScriptLane::run runs an activation to its end now: it starts it and
pumps the app's scheduler a frame at a time (the new Runtime::pump)
until it ends, under its lane's caps, with a wall-clock backstop. It
refuses from inside a step of the app's scheduler, which it would
re-enter. ProgramLane::init gives a load's work run_init's caps (10
million steps, 5 s).

- The wasm engine runs a guest's ari_init that way at every load,
  after its exports and hooks are registered and before the document's
  init:. A failure fails the load.
- An exported activation called from init: (whose scheduler is its own,
  so its program can't park) now runs to its end in the call:
  AppRuntime::register_async_work_function takes a blocking form, which
  the provider binds where the lane's scheduler isn't the app's. A
  kernel can't do this (state_exec forbids a kernel to run processes),
  and a wrapper around the async native loses its place to park.
- The provider computes that only when its runtime has such functions:
  a gone runtime's provider stays registered, and its app may be gone.

Tests: RunFinishesAnActivationNow and the init: case of
AnExportedCallParksOnItsActivation (stub engine);
AriInitAndInitCallsRunAtLoad (wasm); wasm_block.ari's setup case.
master's tree is feat/wasm-guests' (ec7eef2), which this branch already holds.
A value holding a host object (a volume, a mesh, a height field) crosses to
a guest as a handle under CBOR tag "arih", and comes back as the same object.
The instance's handle table pins the version it was handed, with a
generation per slot so a dropped handle names nothing.

New imports in module "ari" (h is an i64 handle):
- handle_drop(h)
- data_info(h): the object's type and sizes, as CBOR, with no data moved
- volume_read(h, box, lod, dst, len): a box, every 2^lod-th voxel
- volume_sample(h, points, n, dst, len)
- mesh_read(h, attr, first, count, dst, len)
- heightfield_read(h, rect, dst, len)

Each copies only the region asked for, straight into the guest's memory,
and is charged a step per 256 bytes moved. imports: gains data.read and
data.write; without data.read the data calls are a PermissionError.

A trap with the guest's memory at its limit (a memory.grow was refused) is
now a MemoryError rather than a WasmError.
The handle's tag and generation, the 65,536-handle cap, the readers' plain
integer regions and element types, what data_info knows, and what is left:
the writers and volume_new, the state imports, the browser's path.
Master's test (#1077) compared one agent's position. scatter_free's draw
differs between standard libraries, and on macOS that agent stays where
it began under both policies, so the macOS recipe and package legs fail
here. This is the same hunk as open #1095, so merging master once #1095
lands is clean.
Base automatically changed from feat/wasm-guests-2 to master November 9, 2026 18:35
The conflicts were #1094's own lines, squashed on master, against this
branch's edits of them (the ABI notes in wasm_engine.h, and roadmap 8.3's
G1 lists and the G1 row); this branch's side is kept. #1075's roadmap
changes (5.1, 5.5, 9, 10) come in untouched. The 8.4.1 note now gives
mesh_read's attribute as a pointer and a length, as the import takes it.
@transfix
transfix merged commit 79eeab0 into master Nov 9, 2026
22 of 31 checks passed
@transfix
transfix deleted the feat/wasm-guests-3 branch November 9, 2026 20:21
transfix added a commit that referenced this pull request Nov 9, 2026
…andles (#1099)

One conflict, in ariadne.cpp's includes: both sides kept. Master's cache notes
state writes under a lock on the writer's thread, so writes from an isolated
document's group on a pool thread invalidate a kept result like any other.
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