Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.standalone.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4769,6 +4769,7 @@ dependencies = [
"offset-allocator",
"serde",
"serde_json",
"smallvec",
"tar",
"tokio",
"ureq",
Expand Down
1 change: 1 addition & 0 deletions Cargo.standalone.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
29 changes: 17 additions & 12 deletions src/guest/napi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
};
Expand All @@ -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;

Expand All @@ -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,
)
};
Expand All @@ -3334,16 +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;
}
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
Expand Down
13 changes: 6 additions & 7 deletions src/guest/napi_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/guest/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NapiEnv>, guest_ptr: u32) -> Option<u32> {
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.
Expand Down
Loading
Loading