From 09b50bf39392cda308cde6d460a8bb339aa1e41a Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Tue, 27 Oct 2026 18:35:17 -0700 Subject: [PATCH 1/4] Benchmark guest imports and JavaScript calls into the guest tests/perf_calls.rs is an ignored test that times, through real Wasmer dispatch, the bridge and V8, with 1, 2 and 4 guests on their own threads: - a guest N-API import (napi_get_value_int32, napi_get_undefined); - JavaScript calling an empty guest function; - JavaScript calling a guest function that reads one argument and `this`, and returns a new int32, like a small DOM method. Run it with `./cargo-standalone.sh test --release --test perf_calls -- --ignored --nocapture`. Co-Authored-By: Claude Opus 5.5 --- tests/perf_calls.rs | 281 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 281 insertions(+) create mode 100644 tests/perf_calls.rs diff --git a/tests/perf_calls.rs b/tests/perf_calls.rs new file mode 100644 index 000000000..dd8d27a60 --- /dev/null +++ b/tests/perf_calls.rs @@ -0,0 +1,281 @@ +//! Not correctness tests: the cost of a guest N-API import and of a +//! JavaScript call into a guest function, through real Wasmer dispatch, the +//! bridge and V8, with 1, 2 and 4 guests on their own threads. Run with +//! `./cargo-standalone.sh test --release --test perf_calls -- --ignored --nocapture`. +//! +//! `PERF_ROUNDS` (default 5) sets the rounds per case, of which the median is +//! printed. `PERF_CASE` (0-3) runs one case, `PERF_THREADS` (e.g. `1,4`) +//! picks the thread counts and `PERF_SCALE` multiplies the calls per round, +//! for example to keep a profiler attached long enough. + +#![cfg(not(target_arch = "wasm32"))] + +use std::{ + sync::{Arc, Barrier}, + thread, + time::{Duration, Instant}, +}; + +use wasmer::{AsStoreMut, Engine, Imports, Instance, Memory, Module, Store, TypedFunction}; +use wasmer_napi::NapiCtx; + +const SOURCE_PTR: u32 = 4096; + +const WAT: &str = r#"(module + (import "napi_extension_wasmer_v0" "unofficial_napi_configure_runtime" + (func $configure (param i32) (result i32))) + (import "napi_extension_wasmer_v0" "unofficial_napi_create_env" + (func $create_env (param i32 i32 i32 i32) (result i32))) + (import "napi_extension_wasmer_v0" "unofficial_napi_release_env" + (func $release_env (param i32 i32) (result i32))) + (import "napi" "napi_create_string_utf8" + (func $create_string (param i32 i32 i32 i32) (result i32))) + (import "napi" "napi_run_script" (func $run_script (param i32 i32 i32) (result i32))) + (import "napi" "napi_get_value_int32" (func $get_int32 (param i32 i32 i32) (result i32))) + (import "napi" "napi_create_int32" (func $create_int32 (param i32 i32 i32) (result i32))) + (import "napi" "napi_get_undefined" (func $get_undefined (param i32 i32) (result i32))) + (import "napi" "napi_get_global" (func $get_global (param i32 i32) (result i32))) + (import "napi" "napi_set_named_property" + (func $set_named_property (param i32 i32 i32 i32) (result i32))) + (import "napi" "napi_create_function" + (func $create_function (param i32 i32 i32 i32 i32 i32) (result i32))) + (import "napi" "napi_get_cb_info" + (func $get_cb_info (param i32 i32 i32 i32 i32 i32) (result i32))) + + ;; 8 env, 12 scope, 28 string, 32 script result, 44 int handle, 48 global, + ;; 52 function, 56 callback result, 60 argc, 64 argv, 68 this, 72 int out. + (memory (export "memory") 2) + (table (export "__indirect_function_table") 4 funcref) + (global $env (mut i32) (i32.const 0)) + (global $scope (mut i32) (i32.const 0)) + (data (i32.const 100) "f\00") + (data (i32.const 104) "g\00") + + (func $define (param $name i32) (param $index i32) + (if (call $create_function (global.get $env) (local.get $name) (i32.const 1) + (local.get $index) (i32.const 0) (i32.const 52)) + (then unreachable)) + (if (call $get_global (global.get $env) (i32.const 48)) (then unreachable)) + (if (call $set_named_property (global.get $env) (i32.load (i32.const 48)) + (local.get $name) (i32.load (i32.const 52))) + (then unreachable))) + + (func (export "create") + (if (call $configure (i32.const 0)) (then unreachable)) + (if (call $create_env (i32.const 8) (i32.const 0) (i32.const 8) (i32.const 12)) + (then unreachable)) + (global.set $env (i32.load (i32.const 8))) + (global.set $scope (i32.load (i32.const 12))) + (if (call $create_int32 (global.get $env) (i32.const 42) (i32.const 44)) + (then unreachable)) + (call $define (i32.const 100) (i32.const 1)) + (call $define (i32.const 104) (i32.const 2))) + + (func (export "release") (result i32) + (call $release_env (global.get $scope) (i32.const 0))) + + (func (export "eval") (param $ptr i32) (param $len i32) (result i32) + (if (call $create_string (global.get $env) (local.get $ptr) (local.get $len) (i32.const 28)) + (then unreachable)) + (call $run_script (global.get $env) (i32.load (i32.const 28)) (i32.const 32))) + + ;; n x napi_get_value_int32 on one handle. + (func (export "loop_get_int32") (param $n i32) + (loop $again + (if (call $get_int32 (global.get $env) (i32.load (i32.const 44)) (i32.const 72)) + (then unreachable)) + (br_if $again (local.tee $n (i32.sub (local.get $n) (i32.const 1)))))) + + ;; n x napi_get_undefined. + (func (export "loop_get_undefined") (param $n i32) + (loop $again + (if (call $get_undefined (global.get $env) (i32.const 56)) (then unreachable)) + (br_if $again (local.tee $n (i32.sub (local.get $n) (i32.const 1)))))) + + ;; f(x): reads one argument and `this`, returns x as a new int32, like a + ;; small DOM method. + (func $f (param $env i32) (param $info i32) (result i32) + (i32.store (i32.const 60) (i32.const 1)) + (if (call $get_cb_info (local.get $env) (local.get $info) (i32.const 60) (i32.const 64) + (i32.const 68) (i32.const 0)) + (then unreachable)) + (if (call $get_int32 (local.get $env) (i32.load (i32.const 64)) (i32.const 72)) + (then unreachable)) + (if (call $create_int32 (local.get $env) (i32.load (i32.const 72)) (i32.const 56)) + (then unreachable)) + (i32.load (i32.const 56))) + ;; g(): does nothing, returns undefined. + (func $g (param $env i32) (param $info i32) (result i32) (i32.const 0)) + (elem (i32.const 1) $f $g) +)"#; + +struct Guest { + store: Store, + instance: Instance, + memory: Memory, +} + +impl Guest { + fn new(ctx: &NapiCtx, engine: &Engine, module: &Module) -> Self { + let mut store = Store::new(engine.clone()); + let hooks = ctx.runtime_hooks(); + let (imports, state): (Imports, _) = hooks + .additional_imports(module, &mut store.as_store_mut()) + .expect("failed to create N-API imports"); + let instance = Instance::new(&mut store, module, &imports).expect("instantiate"); + let memory = instance.exports.get_memory("memory").unwrap().clone(); + hooks + .configure_instance( + module, + &mut store.as_store_mut(), + &instance, + Some(&memory), + state, + ) + .expect("configure instance"); + let mut guest = Self { + store, + instance, + memory, + }; + let create: TypedFunction<(), ()> = guest.typed("create"); + create.call(&mut guest.store).expect("create trapped"); + guest + } + + fn typed( + &self, + name: &str, + ) -> TypedFunction { + self.instance + .exports + .get_typed_function(&self.store, name) + .unwrap() + } + + fn eval(&mut self, source: &str) -> i32 { + self.memory + .view(&self.store) + .write(u64::from(SOURCE_PTR), source.as_bytes()) + .unwrap(); + let eval: TypedFunction<(i32, i32), i32> = self.typed("eval"); + eval.call(&mut self.store, SOURCE_PTR as i32, source.len() as i32) + .expect("eval trapped") + } + + fn release(&mut self) -> i32 { + let release: TypedFunction<(), i32> = self.typed("release"); + release.call(&mut self.store).expect("release trapped") + } +} + +/// One benchmark case: performs `n` operations in a guest. +type Work = fn(&mut Guest, u32); + +/// Reads a numeric setting from the environment. +fn setting(name: &str) -> Option { + std::env::var(name) + .ok() + .and_then(|value| value.parse().ok()) +} + +/// Runs `work` on `threads` guests at once; returns the slowest thread's ns +/// per operation. +fn measure( + ctx: &NapiCtx, + engine: &Engine, + module: &Module, + threads: usize, + ops: u32, + work: Work, +) -> f64 { + let start = Arc::new(Barrier::new(threads + 1)); + let workers: Vec<_> = (0..threads) + .map(|_| { + let (ctx, engine, module, start) = ( + ctx.clone(), + engine.clone(), + module.clone(), + Arc::clone(&start), + ); + thread::spawn(move || { + let mut guest = Guest::new(&ctx, &engine, &module); + work(&mut guest, ops / 10); // warm up + start.wait(); + let began = Instant::now(); + work(&mut guest, ops); + let elapsed = began.elapsed(); + assert_eq!(guest.release(), 0); + elapsed + }) + }) + .collect(); + start.wait(); + let slowest: Duration = workers + .into_iter() + .map(|w| w.join().unwrap()) + .max() + .unwrap(); + slowest.as_nanos() as f64 / f64::from(ops) +} + +fn loop_get_int32(guest: &mut Guest, n: u32) { + let f: TypedFunction = guest.typed("loop_get_int32"); + f.call(&mut guest.store, n as i32).unwrap(); +} + +fn loop_get_undefined(guest: &mut Guest, n: u32) { + let f: TypedFunction = guest.typed("loop_get_undefined"); + f.call(&mut guest.store, n as i32).unwrap(); +} + +fn js_calls_f(guest: &mut Guest, n: u32) { + // In a function, so the warm-up and the measured run can both declare `s`. + let script = + format!("(() => {{ let s = 0; for (let i = 0; i < {n}; i++) s += f(i); return s; }})()"); + assert_eq!(guest.eval(&script), 0); +} + +fn js_calls_g(guest: &mut Guest, n: u32) { + let script = format!("for (let i = 0; i < {n}; i++) g();"); + assert_eq!(guest.eval(&script), 0); +} + +#[test] +#[ignore] +fn guest_call_costs() { + let store = Store::default(); + let module = Module::new(&store, wat::parse_str(WAT).unwrap()).unwrap(); + let engine = store.engine().clone(); + let ctx = NapiCtx::default(); + let rounds: usize = setting("PERF_ROUNDS").unwrap_or(5); + let cases: [(&str, u32, Work); 4] = [ + ("import napi_get_value_int32", 4_000_000, loop_get_int32), + ("import napi_get_undefined", 4_000_000, loop_get_undefined), + ("js->guest g() empty", 1_000_000, js_calls_g), + ("js->guest f(x) cb_info+get+create", 1_000_000, js_calls_f), + ]; + let only: Option = setting("PERF_CASE"); + let scale: u32 = setting("PERF_SCALE").unwrap_or(1); + let thread_counts: Vec = std::env::var("PERF_THREADS") + .ok() + .map(|v| v.split(',').filter_map(|t| t.parse().ok()).collect()) + .unwrap_or_else(|| vec![1, 2, 4]); + for (index, (name, ops, work)) in cases.into_iter().enumerate() { + if only.is_some_and(|only| only != index) { + continue; + } + let ops = ops * scale; + for &threads in &thread_counts { + let mut samples: Vec = (0..rounds) + .map(|_| measure(&ctx, &engine, &module, threads, ops, work)) + .collect(); + samples.sort_by(|a, b| a.partial_cmp(b).unwrap()); + println!( + "PERF case=\"{name}\" threads={threads} median_ns={:.1} min_ns={:.1}", + samples[samples.len() / 2], + samples[0] + ); + } + } +} From 39d60eb5bac57b2a15d57f6f54304747e011383b Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Tue, 27 Oct 2026 19:51:23 -0700 Subject: [PATCH 2/4] Stop allocating on every JavaScript call into the guest Profiling JavaScript calls into a guest function that reads one argument and returns a value showed about 27% of the time in malloc and free, all from bookkeeping the bridge redoes on every call: - Every scope frame allocated its list of value ids when its first value was stored and freed it when the frame was popped. Popped frames now give their emptied list to the next frame, through one PushScopeFrame helper that all five places opening a frame use. Each env keeps up to 8 lists of at most 256 entries. - Each callback inserted its napi_callback_info into an unordered_map and erased it on return. Callbacks nest, so a vector used as a stack does the same; lookups and removal search from the innermost entry. - napi_get_cb_info allocated for the argc read and the argument ids on the Rust side. The argc read no longer allocates, and the ids are a SmallVec of 16 (smallvec is already in the lock files through Wasmer). napi_get_cb_info also no longer mints ids for `this` or the arguments when the guest passes no pointer for them. And the owner-thread check remembers the env the thread resolved last, so a thread driving one env skips the hash lookup. tests/perf_calls.rs, median ns per call (release, macOS arm64): | Case | Threads | Before | After | |------------------------------------|---------|--------|-------| | Guest import | 1 | 24.0 | 23.0 | | | 4 | 26.0 | 25.7 | | JavaScript calls an empty function | 1 | 90.6 | 82.7 | | | 4 | 101.2 | 83.1 | | JavaScript calls f(x) | 1 | 311.5 | 207.3 | | | 4 | 358.4 | 237.8 | tests/guest_callbacks.rs checks the paths this changes: callbacks nested 40 deep through JavaScript that re-read their own callback info, scopes a callback leaves open, argument counts on both sides of 16, null out pointers, and envs created and released one after another on one thread. Co-Authored-By: Claude Opus 5.5 --- Cargo.lock | 1 + Cargo.standalone.lock | 1 + Cargo.standalone.toml | 1 + Cargo.toml | 1 + src/guest/napi.rs | 23 ++- src/guest/util.rs | 12 ++ src/napi_bridge_init.cc | 105 +++++++---- tests/guest_callbacks.rs | 367 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 469 insertions(+), 42 deletions(-) create mode 100644 tests/guest_callbacks.rs diff --git a/Cargo.lock b/Cargo.lock index d3e02b705..fb1d3e0e8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4799,6 +4799,7 @@ dependencies = [ "offset-allocator", "serde", "serde_json", + "smallvec", "tar", "tokio", "ureq", diff --git a/Cargo.standalone.lock b/Cargo.standalone.lock index ce6e41409..f9b82934a 100644 --- a/Cargo.standalone.lock +++ b/Cargo.standalone.lock @@ -4769,6 +4769,7 @@ dependencies = [ "offset-allocator", "serde", "serde_json", + "smallvec", "tar", "tokio", "ureq", diff --git a/Cargo.standalone.toml b/Cargo.standalone.toml index 755c08d84..50af8fd2f 100644 --- a/Cargo.standalone.toml +++ b/Cargo.standalone.toml @@ -46,6 +46,7 @@ virtual-fs = { git = "https://github.com/wasmerio/wasmer.git", rev = "c83ba6d315 tokio = { version = "1", features = ["rt-multi-thread"], optional = true } enum-iterator = "2.3.0" offset-allocator = "0.2.0" +smallvec = "1.15" [target.'cfg(target_arch = "wasm32")'.dependencies] js-sys = { version = "0.3", optional = true } diff --git a/Cargo.toml b/Cargo.toml index 89076d7f9..066dc2a50 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,6 +49,7 @@ virtual-fs = { version = "=0.705.0", path = "../virtual-fs", optional = true } tokio = { version = "1", features = ["rt-multi-thread"], optional = true } enum-iterator = "2.3.0" offset-allocator = "0.2.0" +smallvec = "1.15" [target.'cfg(target_arch = "wasm32")'.dependencies] js-sys = { version = "0.3", optional = true } diff --git a/src/guest/napi.rs b/src/guest/napi.rs index 490b6f793..c601021cd 100644 --- a/src/guest/napi.rs +++ b/src/guest/napi.rs @@ -6,6 +6,7 @@ use std::ffi::{CString, c_char}; +use smallvec::{SmallVec, smallvec}; use wasmer::{AsStoreMut, Function, FunctionEnv, FunctionEnvMut, Imports, namespace}; use wasmer_wasix::WasiError; @@ -3291,10 +3292,10 @@ fn guest_napi_get_cb_info( } // Read the caller's requested argc (size of their argv array) let wanted: u32 = if argc_ptr != 0 { - let Some(bytes) = read_guest_bytes(&mut env, argc_ptr, 4) else { + let Some(wanted) = read_guest_u32(&mut env, argc_ptr) else { return 1; }; - u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) + wanted } else { 0 }; @@ -3304,9 +3305,10 @@ fn guest_napi_get_cb_info( return 1; } - // Query the bridge for callback context + // Query the bridge for callback context. Most callbacks take a few + // arguments, so only larger requests allocate. + let mut argv_ids: SmallVec<[u32; 16]> = smallvec![0; wanted as usize]; let mut actual_argc: u32 = wanted; - let mut argv_ids = vec![0u32; wanted as usize]; let mut this_id: u32 = 0; let mut data_val: u64 = 0; @@ -3315,13 +3317,18 @@ fn guest_napi_get_cb_info( snapi_env(&env, e), _cbinfo, &mut actual_argc, - if wanted > 0 { + // Only mint ids for arguments and `this` if the guest asked. + if wanted > 0 && argv_ptr != 0 { argv_ids.as_mut_ptr() } else { std::ptr::null_mut() }, wanted, - &mut this_id, + if this_ptr != 0 { + &mut this_id + } else { + std::ptr::null_mut() + }, &mut data_val, ) }; @@ -3341,9 +3348,7 @@ fn guest_napi_get_cb_info( if u64::from(argv_ptr) + u64::from(to_write) * 4 > 1u64 << 32 { return 1; } - for i in 0..to_write { - write_guest_u32(&mut env, argv_ptr + i * 4, argv_ids[i as usize]); - } + write_guest_pod_slice(&mut env, argv_ptr, &argv_ids[..to_write as usize]); } // Write this_arg diff --git a/src/guest/util.rs b/src/guest/util.rs index 112bed046..28a325358 100644 --- a/src/guest/util.rs +++ b/src/guest/util.rs @@ -60,6 +60,18 @@ pub fn read_guest_bytes( Some(out) } +/// Reads a little-endian u32 from guest memory without allocating. +pub fn read_guest_u32(env: &mut FunctionEnvMut, guest_ptr: u32) -> Option { + let (state, store) = env.data_and_store_mut(); + let memory = state.memory.clone()?; + let mut bytes = [0u8; 4]; + memory + .view(&store) + .read(guest_ptr as u64, &mut bytes) + .ok()?; + Some(u32::from_le_bytes(bytes)) +} + /// Live size of the guest's linear memory in bytes, or 0 if it has none. Used /// to bound host allocations sized by a guest-supplied length: a guest can /// never reference more than its own memory holds. diff --git a/src/napi_bridge_init.cc b/src/napi_bridge_init.cc index 13806ad69..a42b0bd0e 100644 --- a/src/napi_bridge_init.cc +++ b/src/napi_bridge_init.cc @@ -42,6 +42,7 @@ struct CbRegistration { }; struct CallbackInvocation { + uint32_t id = 0; napi_callback_info info = nullptr; }; @@ -157,6 +158,9 @@ struct SnapiEnvState { // on first use; new values are minted into the innermost frame. std::vector scope_frames; uint32_t next_scope_id = 1; + // Emptied owned_slots buffers of popped frames. The next frame reuses one, + // so a guest callback does not allocate and free a buffer on every call. + std::vector> spare_owned_slots; // Handle table for napi_ref (references). std::unordered_map refs; @@ -184,7 +188,9 @@ struct SnapiEnvState { // Current guest invocation driving this env. This is only valid while the // driving host import remains on the stack. std::atomic active_callback_ctx{nullptr}; - std::unordered_map callback_invocations; + // Guest callbacks in progress, innermost last. Callbacks nest, so entries + // are added and removed in LIFO order and lookups hit the last few. + std::vector callback_invocations; uint32_t next_callback_invocation_id = 1; std::unordered_map cb_registry; uint32_t next_cb_reg_id = 1; @@ -211,10 +217,20 @@ std::unordered_set& g_envs = *new std::unordered_set* t_owned_envs = nullptr; +// The env this thread resolved last, always a member of t_owned_envs. A +// thread usually drives one env, so this answers most lookups without +// hashing. +thread_local SnapiEnvState* t_last_owned_env = nullptr; bool OwnedByThisThread(const SnapiEnvState* state) { - return state != nullptr && t_owned_envs != nullptr && - t_owned_envs->count(const_cast(state)) != 0; + if (state == nullptr) return false; + if (state == t_last_owned_env) return true; + if (t_owned_envs == nullptr || + t_owned_envs->count(const_cast(state)) == 0) { + return false; + } + t_last_owned_env = const_cast(state); + return true; } void AdoptOnThisThread(SnapiEnvState* state) { @@ -225,6 +241,7 @@ void AdoptOnThisThread(SnapiEnvState* state) { } void ForgetOnThisThread(SnapiEnvState* state) { + if (t_last_owned_env == state) t_last_owned_env = nullptr; if (t_owned_envs == nullptr) return; t_owned_envs->erase(state); if (t_owned_envs->empty()) { @@ -303,10 +320,18 @@ uint32_t RegisterCallbackInvocation(SnapiEnvState* state, napi_callback_info inf if (state == nullptr || info == nullptr) return 0; uint32_t id = state->next_callback_invocation_id++; if (id == 0) id = state->next_callback_invocation_id++; - state->callback_invocations[id] = CallbackInvocation{info}; + state->callback_invocations.push_back(CallbackInvocation{id, info}); return id; } +napi_callback_info FindCallbackInvocation(const SnapiEnvState& state, uint32_t id) { + const auto& invocations = state.callback_invocations; + for (auto it = invocations.rbegin(); it != invocations.rend(); ++it) { + if (it->id == id) return it->info; + } + return nullptr; +} + // Innermost open frame, creating the root frame on first use. ScopeFrame& CurrentFrame(SnapiEnvState& state) { if (state.scope_frames.empty()) { @@ -315,6 +340,21 @@ ScopeFrame& CurrentFrame(SnapiEnvState& state) { return state.scope_frames.back(); } +// Pushes the frame for a napi scope just opened, above the root frame. It +// reuses a popped frame's buffer, so a callback doesn't allocate one per call. +ScopeFrame& PushScopeFrame(SnapiEnvState& state, napi_handle_scope scope, + napi_escapable_handle_scope esc_scope) { + (void)CurrentFrame(state); + ScopeFrame& frame = state.scope_frames.emplace_back(); + frame.scope = scope; + frame.esc_scope = esc_scope; + if (!state.spare_owned_slots.empty()) { + frame.owned_slots = std::move(state.spare_owned_slots.back()); + state.spare_owned_slots.pop_back(); + } + return frame; +} + // Mints an id for `val` owned by `frame`. The common path stores into the // innermost frame; escape_handle stores into the parent of the escaped scope. uint32_t StoreValueInFrame(SnapiEnvState& state, napi_value val, ScopeFrame& frame) { @@ -384,6 +424,16 @@ void PopCurrentFrame(SnapiEnvState& state, bool close_napi_scope) { for (uint32_t index : frame.owned_slots) { FreeValueSlot(state, index); } + // Keep a few small buffers for the next frames; drop large ones so one + // big scope does not pin its memory for the env's lifetime. + constexpr size_t kMaxSpareFrames = 8; + constexpr size_t kMaxSpareCapacity = 256; + if (state.spare_owned_slots.size() < kMaxSpareFrames && + frame.owned_slots.capacity() != 0 && + frame.owned_slots.capacity() <= kMaxSpareCapacity) { + frame.owned_slots.clear(); + state.spare_owned_slots.push_back(std::move(frame.owned_slots)); + } if (close_napi_scope && state.env != nullptr) { if (frame.esc_scope != nullptr) { (void)napi_close_escapable_handle_scope(state.env, frame.esc_scope); @@ -1602,10 +1652,7 @@ void GuestFinalizerTrampoline(node_api_basic_env /*env*/, void* /*data*/, if (env != nullptr && napi_open_handle_scope(env, &fin_scope) == napi_ok && fin_scope != nullptr) { - (void)CurrentFrame(*bridge_state); - ScopeFrame frame; - frame.scope = fin_scope; - bridge_state->scope_frames.push_back(std::move(frame)); + PushScopeFrame(*bridge_state, fin_scope, nullptr); } snapi_host_invoke_wasm_finalizer(callback_ctx, record->guest_env, record->wasm_fn_ptr, record->data, @@ -2084,16 +2131,13 @@ extern "C" int snapi_bridge_open_handle_scope(SnapiEnvState* env_state, uint32_t auto* bridge_state = RequireEnvState(env_state); if (bridge_state == nullptr) return napi_invalid_arg; napi_env env = bridge_state->env; - (void)CurrentFrame(*bridge_state); // materialize the root frame first napi_handle_scope scope = nullptr; napi_status s = napi_open_handle_scope(env, &scope); if (s != napi_ok) return s; - ScopeFrame frame; - frame.scope = scope; + ScopeFrame& frame = PushScopeFrame(*bridge_state, scope, nullptr); frame.id = bridge_state->next_scope_id++; if (frame.id == 0) frame.id = bridge_state->next_scope_id++; - bridge_state->scope_frames.push_back(std::move(frame)); - *scope_out = bridge_state->scope_frames.back().id; + *scope_out = frame.id; return napi_ok; } @@ -2117,16 +2161,13 @@ extern "C" int snapi_bridge_open_escapable_handle_scope(SnapiEnvState* env_state auto* bridge_state = RequireEnvState(env_state); if (bridge_state == nullptr) return napi_invalid_arg; napi_env env = bridge_state->env; - (void)CurrentFrame(*bridge_state); // materialize the root frame first napi_escapable_handle_scope scope = nullptr; napi_status s = napi_open_escapable_handle_scope(env, &scope); if (s != napi_ok) return s; - ScopeFrame frame; - frame.esc_scope = scope; + ScopeFrame& frame = PushScopeFrame(*bridge_state, nullptr, scope); frame.id = bridge_state->next_scope_id++; if (frame.id == 0) frame.id = bridge_state->next_scope_id++; - bridge_state->scope_frames.push_back(std::move(frame)); - *scope_out = bridge_state->scope_frames.back().id; + *scope_out = frame.id; return napi_ok; } @@ -2774,8 +2815,8 @@ extern "C" int snapi_bridge_get_cb_info(SnapiEnvState* env_state, uint32_t cbinf auto* bridge_state = RequireEnvState(env_state); if (bridge_state == nullptr) return napi_invalid_arg; napi_env env = bridge_state->env; - auto it = bridge_state->callback_invocations.find(cbinfo_id); - if (it == bridge_state->callback_invocations.end()) return napi_generic_failure; + napi_callback_info info = FindCallbackInvocation(*bridge_state, cbinfo_id); + if (info == nullptr) return napi_generic_failure; size_t wanted = argc_ptr != nullptr ? static_cast(*argc_ptr) : 0; size_t actual = wanted; @@ -2783,7 +2824,7 @@ extern "C" int snapi_bridge_get_cb_info(SnapiEnvState* env_state, uint32_t cbinf napi_value this_arg = nullptr; void* raw_data = nullptr; napi_status s = - napi_get_cb_info(env, it->second.info, &actual, wanted > 0 ? argv.data() : nullptr, + napi_get_cb_info(env, info, &actual, wanted > 0 ? argv.data() : nullptr, &this_arg, &raw_data); if (s != napi_ok) return s; @@ -2815,10 +2856,10 @@ extern "C" int snapi_bridge_get_new_target(SnapiEnvState* env_state, uint32_t cb auto* bridge_state = RequireEnvState(env_state); if (bridge_state == nullptr) return napi_invalid_arg; napi_env env = bridge_state->env; - auto it = bridge_state->callback_invocations.find(cbinfo_id); - if (it == bridge_state->callback_invocations.end()) return napi_generic_failure; + napi_callback_info info = FindCallbackInvocation(*bridge_state, cbinfo_id); + if (info == nullptr) return napi_generic_failure; napi_value result; - napi_status s = napi_get_new_target(env, it->second.info, &result); + napi_status s = napi_get_new_target(env, info, &result); if (s != napi_ok) return s; *out_id = result ? StoreValue(*bridge_state, result) : 0; return napi_ok; @@ -2853,10 +2894,7 @@ void WasmInterruptCallback(napi_env env, void* raw) { if (env != nullptr && napi_open_handle_scope(env, &interrupt_scope) == napi_ok && interrupt_scope != nullptr) { - (void)CurrentFrame(*bridge_state); - ScopeFrame frame; - frame.scope = interrupt_scope; - bridge_state->scope_frames.push_back(std::move(frame)); + PushScopeFrame(*bridge_state, interrupt_scope, nullptr); } snapi_host_invoke_wasm_callback( callback_ctx, request->guest_env, request->wasm_fn_ptr, request->callback_arg); @@ -2923,12 +2961,9 @@ static napi_value generic_wasm_callback(napi_env env, napi_callback_info info) { // callback (arguments, intermediates, the return id) die when it returns, // matching N-API semantics. Escapable so the return value can be handed // back to V8's trampoline in the enclosing scope. - (void)CurrentFrame(*bridge_state); napi_escapable_handle_scope cb_scope = nullptr; if (napi_open_escapable_handle_scope(env, &cb_scope) == napi_ok && cb_scope != nullptr) { - ScopeFrame frame; - frame.esc_scope = cb_scope; - bridge_state->scope_frames.push_back(std::move(frame)); + PushScopeFrame(*bridge_state, nullptr, cb_scope); } // Call Rust trampoline → WASM callback @@ -2940,7 +2975,11 @@ static napi_value generic_wasm_callback(napi_env env, napi_callback_info info) { uint32_t result_id = snapi_host_invoke_wasm_callback( callback_ctx, it->second.guest_env, wasm_fn_ptr, cbinfo_id); - bridge_state->callback_invocations.erase(cbinfo_id); + // Callbacks nest, so ours is normally on top. + auto& invocations = bridge_state->callback_invocations; + auto invocation = std::find_if(invocations.rbegin(), invocations.rend(), + [&](const CallbackInvocation& i) { return i.id == cbinfo_id; }); + if (invocation != invocations.rend()) invocations.erase(std::next(invocation).base()); napi_value result = LoadValue(*bridge_state, result_id); if (cb_scope != nullptr) { diff --git a/tests/guest_callbacks.rs b/tests/guest_callbacks.rs new file mode 100644 index 000000000..18d5908fb --- /dev/null +++ b/tests/guest_callbacks.rs @@ -0,0 +1,367 @@ +//! JavaScript calling guest functions through real Wasmer dispatch, the +//! bridge and V8: nested callbacks, scopes a callback leaves open, argument +//! counts around the bridge's inline buffers, and envs created one after +//! another on one thread. + +#![cfg(not(target_arch = "wasm32"))] + +use wasmer::{AsStoreMut, Engine, Imports, Instance, Memory, Module, Store, TypedFunction}; +use wasmer_napi::NapiCtx; + +const SOURCE_PTR: u32 = 4096; + +const WAT: &str = r#"(module + (import "napi_extension_wasmer_v0" "unofficial_napi_configure_runtime" + (func $configure (param i32) (result i32))) + (import "napi_extension_wasmer_v0" "unofficial_napi_create_env" + (func $create_env (param i32 i32 i32 i32) (result i32))) + (import "napi_extension_wasmer_v0" "unofficial_napi_release_env" + (func $release_env (param i32 i32) (result i32))) + (import "napi" "napi_create_string_utf8" + (func $create_string (param i32 i32 i32 i32) (result i32))) + (import "napi" "napi_run_script" (func $run_script (param i32 i32 i32) (result i32))) + (import "napi" "napi_get_value_int32" (func $get_int32 (param i32 i32 i32) (result i32))) + (import "napi" "napi_create_int32" (func $create_int32 (param i32 i32 i32) (result i32))) + (import "napi" "napi_get_global" (func $get_global (param i32 i32) (result i32))) + (import "napi" "napi_set_named_property" + (func $set_named_property (param i32 i32 i32 i32) (result i32))) + (import "napi" "napi_create_function" + (func $create_function (param i32 i32 i32 i32 i32 i32) (result i32))) + (import "napi" "napi_get_cb_info" + (func $get_cb_info (param i32 i32 i32 i32 i32 i32) (result i32))) + (import "napi" "napi_call_function" + (func $call_function (param i32 i32 i32 i32 i32 i32) (result i32))) + (import "napi" "napi_open_handle_scope" (func $open_scope (param i32 i32) (result i32))) + (import "napi" "napi_close_handle_scope" (func $close_scope (param i32 i32) (result i32))) + (import "napi" "napi_open_escapable_handle_scope" + (func $open_esc (param i32 i32) (result i32))) + (import "napi" "napi_escape_handle" (func $escape (param i32 i32 i32 i32) (result i32))) + + ;; 8 env, 12 scope, 28 string, 32 script result, 48 global, 52 function, + ;; 72 int out, 200..228 scratch. Callbacks take 64- or 256-byte frames from + ;; a stack starting at 32768 so nested calls keep their own out-slots. + (memory (export "memory") 2) + (table (export "__indirect_function_table") 8 funcref) + (global $env (mut i32) (i32.const 0)) + (global $scope (mut i32) (i32.const 0)) + (global $sp (mut i32) (i32.const 32768)) + (global $err (mut i32) (i32.const 0)) + (global $stale_id (mut i32) (i32.const 0)) + (data (i32.const 100) "rec\00") + (data (i32.const 108) "leak\00") + (data (i32.const 116) "many20\00") + (data (i32.const 124) "stale\00") + (data (i32.const 132) "probe\00") + (data (i32.const 156) "many16\00") + + ;; Records the first failing step; the host reads it through `err`. + (func $fail (param $code i32) + (if (i32.eqz (global.get $err)) (then (global.set $err (local.get $code))))) + + (func $define (param $name i32) (param $index i32) + (if (call $create_function (global.get $env) (local.get $name) (i32.const -1) + (local.get $index) (i32.const 0) (i32.const 52)) + (then unreachable)) + (if (call $get_global (global.get $env) (i32.const 48)) (then unreachable)) + (if (call $set_named_property (global.get $env) (i32.load (i32.const 48)) + (local.get $name) (i32.load (i32.const 52))) + (then unreachable))) + + (func (export "create") + (if (call $configure (i32.const 0)) (then unreachable)) + (if (call $create_env (i32.const 8) (i32.const 0) (i32.const 8) (i32.const 12)) + (then unreachable)) + (global.set $env (i32.load (i32.const 8))) + (global.set $scope (i32.load (i32.const 12))) + (call $define (i32.const 100) (i32.const 1)) + (call $define (i32.const 108) (i32.const 2)) + (call $define (i32.const 116) (i32.const 3)) + (call $define (i32.const 124) (i32.const 4)) + (call $define (i32.const 132) (i32.const 5)) + (call $define (i32.const 156) (i32.const 6))) + + (func (export "release") (result i32) + (call $release_env (global.get $scope) (i32.const 0))) + + (func (export "err") (result i32) (global.get $err)) + + (func (export "eval") (param $ptr i32) (param $len i32) (result i32) + (if (call $create_string (global.get $env) (local.get $ptr) (local.get $len) (i32.const 28)) + (then unreachable)) + (call $run_script (global.get $env) (i32.load (i32.const 28)) (i32.const 32))) + + (func (export "result_int") (result i32) + (if (call $get_int32 (global.get $env) (i32.load (i32.const 32)) (i32.const 72)) + (then (return (i32.const -999999)))) + (i32.load (i32.const 72))) + + ;; rec(n, step): inside its own scope, calls step(n - 1), which calls rec + ;; again; then re-reads its own callback info after the inner callbacks + ;; ended and checks its first ids still resolve. Returns n + step(n - 1). + (func $rec (param $env i32) (param $info i32) (result i32) + (local $f i32) (local $n i32) + (local.set $f (global.get $sp)) + (global.set $sp (i32.add (global.get $sp) (i32.const 64))) + (i32.store (local.get $f) (i32.const 2)) + (if (call $get_cb_info (local.get $env) (local.get $info) (local.get $f) + (i32.add (local.get $f) (i32.const 4)) (i32.add (local.get $f) (i32.const 12)) (i32.const 0)) + (then (call $fail (i32.const 1)))) + (if (i32.ne (i32.load (local.get $f)) (i32.const 2)) (then (call $fail (i32.const 2)))) + (if (call $get_int32 (local.get $env) (i32.load offset=4 (local.get $f)) + (i32.add (local.get $f) (i32.const 16))) + (then (call $fail (i32.const 3)))) + (local.set $n (i32.load offset=16 (local.get $f))) + (if (i32.gt_s (local.get $n) (i32.const 0)) + (then + (if (call $open_scope (local.get $env) (i32.add (local.get $f) (i32.const 56))) + (then (call $fail (i32.const 4)))) + (if (call $create_int32 (local.get $env) (i32.sub (local.get $n) (i32.const 1)) + (i32.add (local.get $f) (i32.const 28))) + (then (call $fail (i32.const 5)))) + (if (call $call_function (local.get $env) (i32.load offset=12 (local.get $f)) + (i32.load offset=8 (local.get $f)) (i32.const 1) + (i32.add (local.get $f) (i32.const 28)) (i32.add (local.get $f) (i32.const 20))) + (then (call $fail (i32.const 6)))) + (if (call $get_int32 (local.get $env) (i32.load offset=20 (local.get $f)) + (i32.add (local.get $f) (i32.const 24))) + (then (call $fail (i32.const 7)))) + (i32.store offset=32 (local.get $f) (i32.const 2)) + (if (call $get_cb_info (local.get $env) (local.get $info) (i32.add (local.get $f) (i32.const 32)) + (i32.add (local.get $f) (i32.const 36)) (i32.const 0) (i32.const 0)) + (then (call $fail (i32.const 8)))) + (if (i32.ne (i32.load offset=32 (local.get $f)) (i32.const 2)) (then (call $fail (i32.const 9)))) + (if (call $get_int32 (local.get $env) (i32.load offset=36 (local.get $f)) + (i32.add (local.get $f) (i32.const 44))) + (then (call $fail (i32.const 10)))) + (if (i32.ne (i32.load offset=44 (local.get $f)) (local.get $n)) (then (call $fail (i32.const 11)))) + (if (call $get_int32 (local.get $env) (i32.load offset=4 (local.get $f)) + (i32.add (local.get $f) (i32.const 44))) + (then (call $fail (i32.const 12)))) + (if (i32.ne (i32.load offset=44 (local.get $f)) (local.get $n)) (then (call $fail (i32.const 13)))) + (if (call $close_scope (local.get $env) (i32.load offset=56 (local.get $f))) + (then (call $fail (i32.const 15)))) + (if (call $create_int32 (local.get $env) + (i32.add (local.get $n) (i32.load offset=24 (local.get $f))) + (i32.add (local.get $f) (i32.const 48))) + (then (call $fail (i32.const 14))))) + (else + (if (call $create_int32 (local.get $env) (i32.const 0) (i32.add (local.get $f) (i32.const 48))) + (then (call $fail (i32.const 16)))))) + (global.set $sp (local.get $f)) + (i32.load offset=48 (local.get $f))) + + ;; leak(x): returns 2x through an escapable scope it never closes, and + ;; leaves two more plain scopes open; the bridge closes all three. + (func $leak (param $env i32) (param $info i32) (result i32) + (local $f i32) + (local.set $f (global.get $sp)) + (global.set $sp (i32.add (global.get $sp) (i32.const 64))) + (i32.store (local.get $f) (i32.const 1)) + (if (call $get_cb_info (local.get $env) (local.get $info) (local.get $f) + (i32.add (local.get $f) (i32.const 4)) (i32.const 0) (i32.const 0)) + (then (call $fail (i32.const 21)))) + (if (call $get_int32 (local.get $env) (i32.load offset=4 (local.get $f)) + (i32.add (local.get $f) (i32.const 8))) + (then (call $fail (i32.const 22)))) + (if (call $open_esc (local.get $env) (i32.add (local.get $f) (i32.const 12))) + (then (call $fail (i32.const 23)))) + (if (call $create_int32 (local.get $env) (i32.mul (i32.load offset=8 (local.get $f)) (i32.const 2)) + (i32.add (local.get $f) (i32.const 16))) + (then (call $fail (i32.const 24)))) + (if (call $escape (local.get $env) (i32.load offset=12 (local.get $f)) + (i32.load offset=16 (local.get $f)) (i32.add (local.get $f) (i32.const 20))) + (then (call $fail (i32.const 25)))) + (if (call $open_scope (local.get $env) (i32.add (local.get $f) (i32.const 24))) + (then (call $fail (i32.const 26)))) + (if (call $create_int32 (local.get $env) (i32.const 99) (i32.add (local.get $f) (i32.const 28))) + (then (call $fail (i32.const 27)))) + (global.set $stale_id (i32.load offset=28 (local.get $f))) + (if (call $open_scope (local.get $env) (i32.add (local.get $f) (i32.const 32))) + (then (call $fail (i32.const 28)))) + (if (call $create_int32 (local.get $env) (i32.const 98) (i32.add (local.get $f) (i32.const 36))) + (then (call $fail (i32.const 29)))) + (global.set $sp (local.get $f)) + (i32.load offset=20 (local.get $f))) + + ;; stale(): 1 if the id leak() minted in a scope it left open no longer + ;; resolves. + (func $stale (param $env i32) (param $info i32) (result i32) + (local $s i32) + (local.set $s (call $get_int32 (local.get $env) (global.get $stale_id) (i32.const 200))) + (if (call $create_int32 (local.get $env) (i32.ne (local.get $s) (i32.const 0)) (i32.const 204)) + (then (call $fail (i32.const 41)))) + (i32.load (i32.const 204))) + + ;; Sums the first min(actual, want) int arguments, asking for `want`. + (func $sum_args (param $env i32) (param $info i32) (param $want i32) (result i32) + (local $f i32) (local $i i32) (local $sum i32) (local $n i32) + (local.set $f (global.get $sp)) + (global.set $sp (i32.add (global.get $sp) (i32.const 256))) + (i32.store (local.get $f) (local.get $want)) + (if (call $get_cb_info (local.get $env) (local.get $info) (local.get $f) + (i32.add (local.get $f) (i32.const 4)) (i32.add (local.get $f) (i32.const 200)) (i32.const 0)) + (then (call $fail (i32.const 31)))) + (local.set $n (i32.load (local.get $f))) + (if (i32.gt_u (local.get $n) (local.get $want)) (then (local.set $n (local.get $want)))) + (block $done + (loop $next + (br_if $done (i32.ge_u (local.get $i) (local.get $n))) + (if (call $get_int32 (local.get $env) + (i32.load offset=4 (i32.add (local.get $f) (i32.shl (local.get $i) (i32.const 2)))) + (i32.add (local.get $f) (i32.const 204))) + (then (call $fail (i32.const 33)))) + (local.set $sum (i32.add (local.get $sum) (i32.load offset=204 (local.get $f)))) + (local.set $i (i32.add (local.get $i) (i32.const 1))) + (br $next))) + (if (call $create_int32 (local.get $env) (local.get $sum) (i32.add (local.get $f) (i32.const 208))) + (then (call $fail (i32.const 34)))) + (global.set $sp (local.get $f)) + (i32.load offset=208 (local.get $f))) + (func $many20 (param $env i32) (param $info i32) (result i32) + (call $sum_args (local.get $env) (local.get $info) (i32.const 20))) + (func $many16 (param $env i32) (param $info i32) (result i32) + (call $sum_args (local.get $env) (local.get $info) (i32.const 16))) + + ;; probe(): napi_get_cb_info with every out pointer null, then all set; + ;; returns the argument count. + (func $probe (param $env i32) (param $info i32) (result i32) + (if (call $get_cb_info (local.get $env) (local.get $info) (i32.const 0) (i32.const 0) + (i32.const 0) (i32.const 0)) + (then (call $fail (i32.const 51)))) + (i32.store (i32.const 212) (i32.const 1)) + (if (call $get_cb_info (local.get $env) (local.get $info) (i32.const 212) (i32.const 216) + (i32.const 220) (i32.const 224)) + (then (call $fail (i32.const 52)))) + (if (call $create_int32 (local.get $env) (i32.load (i32.const 212)) (i32.const 228)) + (then (call $fail (i32.const 53)))) + (i32.load (i32.const 228))) + (elem (i32.const 1) $rec $leak $many20 $stale $probe $many16) +)"#; + +struct Guest { + store: Store, + instance: Instance, + memory: Memory, +} + +impl Guest { + fn new(ctx: &NapiCtx, engine: &Engine, module: &Module) -> Self { + let mut store = Store::new(engine.clone()); + let hooks = ctx.runtime_hooks(); + let (imports, state): (Imports, _) = hooks + .additional_imports(module, &mut store.as_store_mut()) + .expect("failed to create N-API imports"); + let instance = Instance::new(&mut store, module, &imports).expect("instantiate"); + let memory = instance.exports.get_memory("memory").unwrap().clone(); + hooks + .configure_instance( + module, + &mut store.as_store_mut(), + &instance, + Some(&memory), + state, + ) + .expect("configure instance"); + let mut guest = Self { + store, + instance, + memory, + }; + let create: TypedFunction<(), ()> = guest.typed("create"); + create.call(&mut guest.store).expect("create trapped"); + guest + } + + fn typed( + &self, + name: &str, + ) -> TypedFunction { + self.instance + .exports + .get_typed_function(&self.store, name) + .unwrap() + } + + /// Runs `source`; returns its status, its int32 result and the first + /// failing step a callback recorded (0 if none). + fn eval(&mut self, source: &str) -> (i32, i32, i32) { + self.memory + .view(&self.store) + .write(u64::from(SOURCE_PTR), source.as_bytes()) + .unwrap(); + let eval: TypedFunction<(i32, i32), i32> = self.typed("eval"); + let status = eval + .call(&mut self.store, SOURCE_PTR as i32, source.len() as i32) + .expect("eval trapped"); + let result: TypedFunction<(), i32> = self.typed("result_int"); + let value = if status == 0 { + result.call(&mut self.store).unwrap() + } else { + -777 + }; + let err: TypedFunction<(), i32> = self.typed("err"); + (status, value, err.call(&mut self.store).unwrap()) + } + + fn release(&mut self) -> i32 { + let release: TypedFunction<(), i32> = self.typed("release"); + release.call(&mut self.store).expect("release trapped") + } +} + +fn setup() -> (NapiCtx, Engine, Module) { + let store = Store::default(); + let module = Module::new(&store, wat::parse_str(WAT).unwrap()).unwrap(); + (NapiCtx::default(), store.engine().clone(), module) +} + +/// Returns 300 if every round passed, or a negative code for the first +/// failed check. +const ROUNDS: &str = r#"(() => { + function step(m) { return rec(m, step); } + function step2(m) { leak(m); stale(); return rec(m, step2); } + let ok = 0; + for (let i = 0; i < 300; i++) { + if (rec(40, step) !== 820) return -1; + if (rec(30, step2) !== 465) return -11; + if (leak(i) !== 2 * i) return -2; + if (stale() !== 1) return -3; + if (many20(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20) !== 210) return -4; + if (many20(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17) !== 153) return -5; + if (many20(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) !== 136) return -6; + if (many16(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16) !== 136) return -7; + if (many16(1,2,3) !== 6) return -8; + if (many16(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18) !== 136) return -9; + if (probe(5, 6) !== 2) return -10; + ok++; + } + return ok; +})()"#; + +/// Callbacks nested 40 deep through JavaScript keep their own callback info +/// and value ids; scopes a callback leaves open are closed and their ids stop +/// resolving; argument counts on both sides of the 16-entry inline buffers +/// arrive intact; null out pointers are accepted. +#[test] +fn nested_callbacks_scopes_and_arguments() { + let (ctx, engine, module) = setup(); + let mut guest = Guest::new(&ctx, &engine, &module); + assert_eq!(guest.eval(ROUNDS), (0, 300, 0)); + assert_eq!(guest.release(), 0); +} + +/// Envs created and released one after another on one thread: each new env +/// works, and a released one is refused. +#[test] +fn envs_created_one_after_another_on_one_thread() { + let (ctx, engine, module) = setup(); + for round in 0..6 { + let mut guest = Guest::new(&ctx, &engine, &module); + assert_eq!(guest.eval(ROUNDS), (0, 300, 0), "round {round}"); + assert_eq!(guest.release(), 0); + // `eval` traps when the bridge refuses the released env. + let eval: TypedFunction<(i32, i32), i32> = guest.typed("eval"); + let after = eval.call(&mut guest.store, SOURCE_PTR as i32, 1); + assert!(after.is_err(), "released env still ran: {after:?}"); + } +} From 5bfee5c6907eb93c6cd453f049bead84edc6e9ee Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Tue, 27 Oct 2026 19:51:45 -0700 Subject: [PATCH 3/4] Fill missing callback arguments with undefined When a guest asked napi_get_cb_info for more arguments than the call passed, the slots past the actual count were left unwritten in guest memory. N-API fills them with undefined, so an addon that checks an optional argument with napi_typeof(argv[i]) read whatever was there. - Native: the engine already fills those slots with undefined; the bridge now hands all of them to the guest, minting one id for undefined that every missing slot shares. - Host-JS: the bridge fills them the same way, and now reports the actual argument count in *argc, as N-API does, instead of the smaller of the actual and requested counts. Its guest layer also stops minting argument ids when the guest passes no argv buffer, and writes argv with one memory write. Tests: missing_arguments_are_undefined (tests/guest_callbacks.rs) fails on main, reading the unwritten slot; a wasm-bindgen test checks the host-JS argv and argc for calls with fewer and more arguments than requested. Co-Authored-By: Claude Opus 5.5 --- src/guest/napi.rs | 8 ++--- src/guest/napi_js.rs | 13 ++++---- src/napi_bridge_init.cc | 13 ++++++-- src/snapi_js.rs | 71 +++++++++++++++++++++++++++++++++++++--- tests/guest_callbacks.rs | 54 +++++++++++++++++++++++++++--- 5 files changed, 136 insertions(+), 23 deletions(-) diff --git a/src/guest/napi.rs b/src/guest/napi.rs index c601021cd..17f0206a0 100644 --- a/src/guest/napi.rs +++ b/src/guest/napi.rs @@ -3341,14 +3341,14 @@ fn guest_napi_get_cb_info( write_guest_u32(&mut env, argc_ptr, actual_argc); } - // Write argv (array of handle IDs) - only write up to min(wanted, actual) + // Write all `wanted` argv entries: those past the actual count are + // undefined. if argv_ptr != 0 { - let to_write = std::cmp::min(wanted, actual_argc); // The argv entries written must lie below 4 GiB. - if u64::from(argv_ptr) + u64::from(to_write) * 4 > 1u64 << 32 { + if u64::from(argv_ptr) + u64::from(wanted) * 4 > 1u64 << 32 { return 1; } - write_guest_pod_slice(&mut env, argv_ptr, &argv_ids[..to_write as usize]); + write_guest_pod_slice(&mut env, argv_ptr, &argv_ids); } // Write this_arg diff --git a/src/guest/napi_js.rs b/src/guest/napi_js.rs index 81141e369..66d93ace7 100644 --- a/src/guest/napi_js.rs +++ b/src/guest/napi_js.rs @@ -3960,7 +3960,8 @@ fn guest_napi_get_cb_info( snapi_env(&env, e), _cbinfo, &mut actual_argc, - if wanted > 0 { + // Only mint ids for arguments if the guest asked. + if wanted > 0 && argv_ptr != 0 { argv_ids.as_mut_ptr() } else { std::ptr::null_mut() @@ -3979,16 +3980,14 @@ fn guest_napi_get_cb_info( write_guest_u32(&mut env, argc_ptr, actual_argc); } - // Write argv (array of handle IDs) - only write up to min(wanted, actual) + // Write all `wanted` argv entries: those past the actual count are + // undefined. if argv_ptr != 0 { - let to_write = std::cmp::min(wanted, actual_argc); // The argv entries written must lie below 4 GiB. - if u64::from(argv_ptr) + u64::from(to_write) * 4 > 1u64 << 32 { + if u64::from(argv_ptr) + u64::from(wanted) * 4 > 1u64 << 32 { return 1; } - for i in 0..to_write { - write_guest_u32(&mut env, argv_ptr + i * 4, argv_ids[i as usize]); - } + write_guest_pod_slice(&mut env, argv_ptr, &argv_ids); } // Write this_arg diff --git a/src/napi_bridge_init.cc b/src/napi_bridge_init.cc index a42b0bd0e..4914f72de 100644 --- a/src/napi_bridge_init.cc +++ b/src/napi_bridge_init.cc @@ -2841,10 +2841,17 @@ extern "C" int snapi_bridge_get_cb_info(SnapiEnvState* env_state, uint32_t cbinf } if (data_out) *data_out = data_val; - const uint32_t to_copy = static_cast(std::min(wanted, actual)); if (argv_out != nullptr) { - for (uint32_t i = 0; i < to_copy; i++) { - argv_out[i] = StoreValue(*bridge_state, argv[i]); + // Slots past the actual arguments hold undefined (the engine filled + // them); one id serves them all. + uint32_t undefined_id = 0; + for (size_t i = 0; i < wanted; i++) { + if (i < actual) { + argv_out[i] = StoreValue(*bridge_state, argv[i]); + } else { + if (undefined_id == 0) undefined_id = StoreValue(*bridge_state, argv[i]); + argv_out[i] = undefined_id; + } } } return napi_ok; diff --git a/src/snapi_js.rs b/src/snapi_js.rs index 78742cc50..9b2197f51 100644 --- a/src/snapi_js.rs +++ b/src/snapi_js.rs @@ -3790,18 +3790,27 @@ pub unsafe extern "C" fn snapi_bridge_get_cb_info( } else { unsafe { argc.read() }.min(max) }; - let n = (info.args.len() as u32).min(requested); + let actual = info.args.len() as u32; + let n = actual.min(requested); + let (this_value, data_value) = (info.this_value, info.data); if !argv.is_null() { unsafe { ptr::copy_nonoverlapping(info.args.as_ptr(), argv, n as usize) } + // Slots past the actual arguments hold undefined; one id serves them all. + if n < requested { + let undefined = s.insert_primitive(JsValue::UNDEFINED); + for i in n..requested { + unsafe { argv.add(i as usize).write(undefined) } + } + } } if !argc.is_null() { - unsafe { argc.write(n) } + unsafe { argc.write(actual) } } if !this_out.is_null() { - unsafe { this_out.write(info.this_value) } + unsafe { this_out.write(this_value) } } if !data.is_null() { - unsafe { data.write(info.data) } + unsafe { data.write(data_value) } } NAPI_OK } @@ -7054,6 +7063,60 @@ mod tests { }); } + #[wasm_bindgen_test] + fn get_cb_info_reports_the_actual_count_and_undefined_for_missing_arguments() { + with_env(|env, state| { + let first = state.insert(Object::new().into()); + let second = state.insert(Object::new().into()); + for (id, args) in [(7, vec![first]), (8, vec![first, second])] { + state.callback_infos.insert( + id, + CallbackInfo { + args, + this_value: 0, + new_target: 0, + data: 0, + }, + ); + } + // More slots than arguments: the rest are undefined. + let (mut argc, mut argv) = (3u32, [u32::MAX; 3]); + let status = unsafe { + snapi_bridge_get_cb_info( + env, + 7, + &mut argc, + argv.as_mut_ptr(), + 3, + ptr::null_mut(), + ptr::null_mut(), + ) + }; + assert_eq!(status, NAPI_OK); + assert_eq!(argc, 1); + assert_eq!(argv[0], first); + for id in &argv[1..] { + assert!(state.get(*id).is_some_and(JsValue::is_undefined)); + } + // Fewer slots than arguments: argc is still the actual count. + let (mut argc, mut argv) = (1u32, [u32::MAX; 1]); + let status = unsafe { + snapi_bridge_get_cb_info( + env, + 8, + &mut argc, + argv.as_mut_ptr(), + 1, + ptr::null_mut(), + ptr::null_mut(), + ) + }; + assert_eq!(status, NAPI_OK); + assert_eq!(argc, 2); + assert_eq!(argv[0], first); + }); + } + #[wasm_bindgen_test] fn objects_keep_their_handle_while_it_is_live() { with_env(|_, state| objects_keep_their_handle(state)); diff --git a/tests/guest_callbacks.rs b/tests/guest_callbacks.rs index 18d5908fb..5e365c990 100644 --- a/tests/guest_callbacks.rs +++ b/tests/guest_callbacks.rs @@ -1,7 +1,7 @@ //! JavaScript calling guest functions through real Wasmer dispatch, the //! bridge and V8: nested callbacks, scopes a callback leaves open, argument -//! counts around the bridge's inline buffers, and envs created one after -//! another on one thread. +//! counts, missing arguments, and envs created one after another on one +//! thread. #![cfg(not(target_arch = "wasm32"))] @@ -36,12 +36,13 @@ const WAT: &str = r#"(module (import "napi" "napi_open_escapable_handle_scope" (func $open_esc (param i32 i32) (result i32))) (import "napi" "napi_escape_handle" (func $escape (param i32 i32 i32 i32) (result i32))) + (import "napi" "napi_typeof" (func $typeof (param i32 i32 i32) (result i32))) ;; 8 env, 12 scope, 28 string, 32 script result, 48 global, 52 function, ;; 72 int out, 200..228 scratch. Callbacks take 64- or 256-byte frames from ;; a stack starting at 32768 so nested calls keep their own out-slots. (memory (export "memory") 2) - (table (export "__indirect_function_table") 8 funcref) + (table (export "__indirect_function_table") 16 funcref) (global $env (mut i32) (i32.const 0)) (global $scope (mut i32) (i32.const 0)) (global $sp (mut i32) (i32.const 32768)) @@ -53,6 +54,7 @@ const WAT: &str = r#"(module (data (i32.const 124) "stale\00") (data (i32.const 132) "probe\00") (data (i32.const 156) "many16\00") + (data (i32.const 164) "undef\00") ;; Records the first failing step; the host reads it through `err`. (func $fail (param $code i32) @@ -78,7 +80,8 @@ const WAT: &str = r#"(module (call $define (i32.const 116) (i32.const 3)) (call $define (i32.const 124) (i32.const 4)) (call $define (i32.const 132) (i32.const 5)) - (call $define (i32.const 156) (i32.const 6))) + (call $define (i32.const 156) (i32.const 6)) + (call $define (i32.const 164) (i32.const 7))) (func (export "release") (result i32) (call $release_env (global.get $scope) (i32.const 0))) @@ -235,7 +238,37 @@ const WAT: &str = r#"(module (if (call $create_int32 (local.get $env) (i32.load (i32.const 212)) (i32.const 228)) (then (call $fail (i32.const 53)))) (i32.load (i32.const 228))) - (elem (i32.const 1) $rec $leak $many20 $stale $probe $many16) + + ;; undef(x): asks for 4 arguments into slots prefilled with -1. Returns + ;; argc * 100 + the napi_valuetype of argv[1] * 10 + that of argv[3], or + ;; -1 if either slot is not a valid value. + (func $undef (param $env i32) (param $info i32) (result i32) + (local $t1 i32) (local $t3 i32) + (i32.store (i32.const 400) (i32.const 4)) + (i32.store (i32.const 404) (i32.const -1)) + (i32.store (i32.const 408) (i32.const -1)) + (i32.store (i32.const 412) (i32.const -1)) + (i32.store (i32.const 416) (i32.const -1)) + (if (call $get_cb_info (local.get $env) (local.get $info) (i32.const 400) (i32.const 404) + (i32.const 0) (i32.const 0)) + (then (call $fail (i32.const 81)))) + (local.set $t1 (i32.const -1)) + (local.set $t3 (i32.const -1)) + (if (i32.eqz (call $typeof (local.get $env) (i32.load (i32.const 408)) (i32.const 420))) + (then (local.set $t1 (i32.load (i32.const 420))))) + (if (i32.eqz (call $typeof (local.get $env) (i32.load (i32.const 416)) (i32.const 420))) + (then (local.set $t3 (i32.load (i32.const 420))))) + (if (call $create_int32 (local.get $env) + (select + (i32.add (i32.mul (i32.load (i32.const 400)) (i32.const 100)) + (i32.add (i32.mul (local.get $t1) (i32.const 10)) (local.get $t3))) + (i32.const -1) + (i32.and (i32.ge_s (local.get $t1) (i32.const 0)) (i32.ge_s (local.get $t3) (i32.const 0)))) + (i32.const 424)) + (then (call $fail (i32.const 82)))) + (i32.load (i32.const 424))) + + (elem (i32.const 1) $rec $leak $many20 $stale $probe $many16 $undef) )"#; struct Guest { @@ -350,6 +383,17 @@ fn nested_callbacks_scopes_and_arguments() { assert_eq!(guest.release(), 0); } +/// Asking for more arguments than were passed reports the actual count and +/// fills the extra slots with undefined (`napi_undefined` is 0). +#[test] +fn missing_arguments_are_undefined() { + let (ctx, engine, module) = setup(); + let mut guest = Guest::new(&ctx, &engine, &module); + assert_eq!(guest.eval("undef(1)"), (0, 100, 0)); + assert_eq!(guest.eval("undef()"), (0, 0, 0)); + assert_eq!(guest.release(), 0); +} + /// Envs created and released one after another on one thread: each new env /// works, and a released one is refused. #[test] From 3282d921063c692c8789b8e1be4333a569d6e84c Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Tue, 27 Oct 2026 19:52:03 -0700 Subject: [PATCH 4/4] Escape a callback's return value before closing scopes it left open When a guest callback returned, the bridge first closed the scopes the guest had left open and then escaped the return value. If the value was created in one of those scopes, it was read from a closed scope, whose handle block V8 may already have freed. It worked only because the freed memory still held the value. Escape the value first: the escape slot belongs to the scope around the callback's, so closing the inner scopes and then the callback's own leaves it intact. This only runs while the callback's frame is open. a_return_value_from_a_scope_left_open_survives returns a value created after 3000 others in a scope the callback leaves open. On main it passes by luck, and crashes when freed memory is overwritten (MallocScribble=1 on macOS); with this change it passes either way. Co-Authored-By: Claude Opus 5.5 --- src/napi_bridge_init.cc | 34 +++++++++++++++++----------------- tests/guest_callbacks.rs | 37 ++++++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 22 deletions(-) diff --git a/src/napi_bridge_init.cc b/src/napi_bridge_init.cc index 4914f72de..32adf8ac7 100644 --- a/src/napi_bridge_init.cc +++ b/src/napi_bridge_init.cc @@ -2989,26 +2989,26 @@ static napi_value generic_wasm_callback(napi_env env, napi_callback_info info) { if (invocation != invocations.rend()) invocations.erase(std::next(invocation).base()); napi_value result = LoadValue(*bridge_state, result_id); - if (cb_scope != nullptr) { - // Force-close any scopes the guest left unbalanced so LIFO holds, then - // escape the return value into the enclosing scope and pop our frame. - while (bridge_state->scope_frames.size() > 1 && - bridge_state->scope_frames.back().esc_scope != cb_scope) { - PopCurrentFrame(*bridge_state, /*close_napi_scope=*/true); + auto& frames = bridge_state->scope_frames; + // Only while our frame is still open (it normally is the innermost one + // the guest did not close). + if (cb_scope != nullptr && + std::find_if(frames.rbegin(), frames.rend(), [&](const ScopeFrame& frame) { + return frame.esc_scope == cb_scope; + }) != frames.rend()) { + // Escape first: the result may live in a scope the guest left open, which + // closing would free. The escape slot belongs to the scope around ours. + if (result != nullptr) { + napi_value escaped = nullptr; + result = napi_escape_handle(env, cb_scope, result, &escaped) == napi_ok + ? escaped + : nullptr; } - if (!bridge_state->scope_frames.empty() && - bridge_state->scope_frames.back().esc_scope == cb_scope) { - if (result != nullptr) { - napi_value escaped = nullptr; - if (napi_escape_handle(env, cb_scope, result, &escaped) == napi_ok && - escaped != nullptr) { - result = escaped; - } else { - result = nullptr; - } - } + // Then close the scopes the guest left open, and ours. + while (frames.back().esc_scope != cb_scope) { PopCurrentFrame(*bridge_state, /*close_napi_scope=*/true); } + PopCurrentFrame(*bridge_state, /*close_napi_scope=*/true); } if (!result) { napi_get_undefined(env, &result); diff --git a/tests/guest_callbacks.rs b/tests/guest_callbacks.rs index 5e365c990..21e286360 100644 --- a/tests/guest_callbacks.rs +++ b/tests/guest_callbacks.rs @@ -1,7 +1,7 @@ //! JavaScript calling guest functions through real Wasmer dispatch, the -//! bridge and V8: nested callbacks, scopes a callback leaves open, argument -//! counts, missing arguments, and envs created one after another on one -//! thread. +//! bridge and V8: nested callbacks, scopes a callback leaves open, return +//! values created in them, argument counts, missing arguments, and envs +//! created one after another on one thread. #![cfg(not(target_arch = "wasm32"))] @@ -55,6 +55,7 @@ const WAT: &str = r#"(module (data (i32.const 132) "probe\00") (data (i32.const 156) "many16\00") (data (i32.const 164) "undef\00") + (data (i32.const 172) "badret\00") ;; Records the first failing step; the host reads it through `err`. (func $fail (param $code i32) @@ -81,7 +82,8 @@ const WAT: &str = r#"(module (call $define (i32.const 124) (i32.const 4)) (call $define (i32.const 132) (i32.const 5)) (call $define (i32.const 156) (i32.const 6)) - (call $define (i32.const 164) (i32.const 7))) + (call $define (i32.const 164) (i32.const 7)) + (call $define (i32.const 172) (i32.const 8))) (func (export "release") (result i32) (call $release_env (global.get $scope) (i32.const 0))) @@ -268,7 +270,21 @@ const WAT: &str = r#"(module (then (call $fail (i32.const 82)))) (i32.load (i32.const 424))) - (elem (i32.const 1) $rec $leak $many20 $stale $probe $many16 $undef) + ;; badret(): fills a scope it leaves open with enough handles to need new + ;; handle blocks, then returns a value created in it. + (func $badret (param $env i32) (param $info i32) (result i32) + (local $i i32) + (if (call $open_scope (local.get $env) (i32.const 300)) (then (call $fail (i32.const 71)))) + (block $done + (loop $next + (br_if $done (i32.ge_u (local.get $i) (i32.const 3000))) + (drop (call $create_int32 (local.get $env) (local.get $i) (i32.const 304))) + (local.set $i (i32.add (local.get $i) (i32.const 1))) + (br $next))) + (if (call $create_int32 (local.get $env) (i32.const 1234) (i32.const 304)) + (then (call $fail (i32.const 72)))) + (i32.load (i32.const 304))) + (elem (i32.const 1) $rec $leak $many20 $stale $probe $many16 $undef $badret) )"#; struct Guest { @@ -394,6 +410,17 @@ fn missing_arguments_are_undefined() { assert_eq!(guest.release(), 0); } +/// A callback returns a value created in a scope it left open, after enough +/// values to fill more than one handle block. +#[test] +fn a_return_value_from_a_scope_left_open_survives() { + let (ctx, engine, module) = setup(); + let mut guest = Guest::new(&ctx, &engine, &module); + let script = "(() => { let s = 0; for (let i = 0; i < 200; i++) s += badret(); return s; })()"; + assert_eq!(guest.eval(script), (0, 200 * 1234, 0)); + assert_eq!(guest.release(), 0); +} + /// Envs created and released one after another on one thread: each new env /// works, and a released one is refused. #[test]