Repository navigation
ariadne: wasm guests reach host objects by handle, and MemoryError (G1, third part) - #1099
Merged
Merged
Conversation
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.
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.
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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 becausescatter_free's draw differs under libc++.What it builds
value_cbor).data_objectcrosses as a handle: CBOR tag0x61726968("arih") over an unsigned integer. A handle the guest hands back (as a result, or as acallargument) is the same object again.handle_drop.ari;his 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.reply(), ascalldoes.kDataNodeBytes(256) moved, so bulk copies count against the action caps.Import::widemarks i64 parameters.imports:gainsdata.readanddata.write. Withoutdata.read, the data calls are aPermissionError.data.writeis reserved for the writers.MemoryError. Amemory.growpast 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 aMemoryErrorrather than aWasmError. The guest's ownfail()stays aWasmError.Docs
wasm_engine.h's ABI notes.MemoryErrorrow in STATE_EXEC_EXCEPTIONS.md.Tests
Locally on Windows, in a static Release tree:
ariadne_wasm_test: 18/18, three runs.HostObjectsCrossAsHandles.AVolumeCrossesAsAHandle(info, a box, every other voxel, a sample, a handle back as itself from an argument and from the guest's ownstate-data-get),AMeshAndAHeightField,ADroppedHandleNamesNothing(and a box outside the volume),ImportsNarrowTheDataCalls,AGuestOutOfMemoryIsAMemoryError.ariadne_script_engine_test: 20/20.state_exec_exceptions_test: 25/25.arix_wasm_blockandari_wasm_needs_arixctests pass.Not covered by a test: the byte charge (a step per 256 bytes). Only the code applies it.
Left for G1
volume_write,volume_new.state_exec.policy.