Skip to content

Commit 8995bcc

Browse files
authored
Use a helper method to invoke VMFuncRef::array_call (#9630)
* Use a helper method to invoke `VMFuncRef::array_call` This is intended to encapsulate the usage of a native raw pointer and in the future act as a dispatch point for invoking Pulley instead of native code. * Fix some callers * More fixese
1 parent be12a66 commit 8995bcc

File tree

9 files changed

+58
-42
lines changed

9 files changed

+58
-42
lines changed

benches/call.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,7 @@ fn bench_host_to_wasm<Params, Results>(
189189
for (i, param) in params.iter().enumerate() {
190190
space[i] = param.to_raw(&mut *store).unwrap();
191191
}
192-
untyped
193-
.call_unchecked(&mut *store, space.as_mut_ptr(), space.len())
194-
.unwrap();
192+
untyped.call_unchecked(&mut *store, &mut space[..]).unwrap();
195193
for (i, expected) in results.iter().enumerate() {
196194
let ty = expected.ty(&store).unwrap();
197195
let actual = Val::from_raw(&mut *store, space[i], ty);

crates/c-api/src/func.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,8 @@ pub unsafe extern "C" fn wasmtime_func_call_unchecked(
391391
args_and_results_len: usize,
392392
trap_ret: &mut *mut wasm_trap_t,
393393
) -> Option<Box<wasmtime_error_t>> {
394-
match func.call_unchecked(store, args_and_results, args_and_results_len) {
394+
let slice = std::ptr::slice_from_raw_parts_mut(args_and_results, args_and_results_len);
395+
match func.call_unchecked(store, slice) {
395396
Ok(()) => None,
396397
Err(trap) => store_err(trap, trap_ret),
397398
}

crates/wasmtime/src/runtime/component/func.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,10 @@ impl Func {
470470
crate::Func::call_unchecked_raw(
471471
store,
472472
export.func_ref,
473-
space.as_mut_ptr().cast(),
474-
mem::size_of_val(space) / mem::size_of::<ValRaw>(),
473+
core::ptr::slice_from_raw_parts_mut(
474+
space.as_mut_ptr().cast(),
475+
mem::size_of_val(space) / mem::size_of::<ValRaw>(),
476+
),
475477
)?;
476478

477479
// Note that `.assume_init_ref()` here is unsafe but we're relying
@@ -620,8 +622,7 @@ impl Func {
620622
crate::Func::call_unchecked_raw(
621623
&mut store,
622624
func.func_ref,
623-
&post_return_arg as *const ValRaw as *mut ValRaw,
624-
1,
625+
core::ptr::slice_from_raw_parts(&post_return_arg, 1).cast_mut(),
625626
)?;
626627
}
627628

crates/wasmtime/src/runtime/component/resources.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ impl ResourceAny {
10341034
// destructors have al been previously type-checked and are guaranteed
10351035
// to take one i32 argument and return no results, so the parameters
10361036
// here should be configured correctly.
1037-
unsafe { crate::Func::call_unchecked_raw(store, dtor, args.as_mut_ptr(), args.len()) }
1037+
unsafe { crate::Func::call_unchecked_raw(store, dtor, &mut args) }
10381038
}
10391039

10401040
fn lower_to_index<U>(&self, cx: &mut LowerContext<'_, U>, ty: InterfaceType) -> Result<u32> {

crates/wasmtime/src/runtime/func.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,34 +1056,23 @@ impl Func {
10561056
pub unsafe fn call_unchecked(
10571057
&self,
10581058
mut store: impl AsContextMut,
1059-
params_and_returns: *mut ValRaw,
1060-
params_and_returns_capacity: usize,
1059+
params_and_returns: *mut [ValRaw],
10611060
) -> Result<()> {
10621061
let mut store = store.as_context_mut();
10631062
let data = &store.0.store_data()[self.0];
10641063
let func_ref = data.export().func_ref;
1065-
Self::call_unchecked_raw(
1066-
&mut store,
1067-
func_ref,
1068-
params_and_returns,
1069-
params_and_returns_capacity,
1070-
)
1064+
Self::call_unchecked_raw(&mut store, func_ref, params_and_returns)
10711065
}
10721066

10731067
pub(crate) unsafe fn call_unchecked_raw<T>(
10741068
store: &mut StoreContextMut<'_, T>,
10751069
func_ref: NonNull<VMFuncRef>,
1076-
params_and_returns: *mut ValRaw,
1077-
params_and_returns_capacity: usize,
1070+
params_and_returns: *mut [ValRaw],
10781071
) -> Result<()> {
10791072
invoke_wasm_and_catch_traps(store, |caller| {
1080-
let func_ref = func_ref.as_ref();
1081-
(func_ref.array_call)(
1082-
func_ref.vmctx,
1083-
caller.cast::<VMOpaqueContext>(),
1084-
params_and_returns,
1085-
params_and_returns_capacity,
1086-
)
1073+
func_ref
1074+
.as_ref()
1075+
.array_call(caller.cast::<VMOpaqueContext>(), params_and_returns)
10871076
})
10881077
}
10891078

@@ -1256,7 +1245,10 @@ impl Func {
12561245
}
12571246

12581247
unsafe {
1259-
self.call_unchecked(&mut *store, values_vec.as_mut_ptr(), values_vec_size)?;
1248+
self.call_unchecked(
1249+
&mut *store,
1250+
core::ptr::slice_from_raw_parts_mut(values_vec.as_mut_ptr(), values_vec_size),
1251+
)?;
12601252
}
12611253

12621254
for ((i, slot), val) in results.iter_mut().enumerate().zip(&values_vec) {

crates/wasmtime/src/runtime/func/typed.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +213,13 @@ where
213213

214214
let result = invoke_wasm_and_catch_traps(store, |caller| {
215215
let (func_ref, storage) = &mut captures;
216-
let func_ref = func_ref.as_ref();
217-
(func_ref.array_call)(
218-
func_ref.vmctx,
219-
VMOpaqueContext::from_vmcontext(caller),
220-
(storage as *mut Storage<_, _>) as *mut ValRaw,
221-
mem::size_of_val::<Storage<_, _>>(storage) / mem::size_of::<ValRaw>(),
222-
);
216+
let storage_len = mem::size_of_val::<Storage<_, _>>(storage) / mem::size_of::<ValRaw>();
217+
let storage: *mut Storage<_, _> = storage;
218+
let storage = storage.cast::<ValRaw>();
219+
let storage = core::ptr::slice_from_raw_parts_mut(storage, storage_len);
220+
func_ref
221+
.as_ref()
222+
.array_call(VMOpaqueContext::from_vmcontext(caller), storage);
223223
});
224224

225225
let (_, storage) = captures;

crates/wasmtime/src/runtime/instance.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,9 @@ impl Instance {
362362
let caller_vmctx = instance.vmctx();
363363
unsafe {
364364
super::func::invoke_wasm_and_catch_traps(store, |_default_caller| {
365-
let func = f.func_ref.as_ref().array_call;
366-
func(
367-
f.func_ref.as_ref().vmctx,
368-
VMOpaqueContext::from_vmcontext(caller_vmctx),
369-
[].as_mut_ptr(),
370-
0,
371-
)
365+
f.func_ref
366+
.as_ref()
367+
.array_call(VMOpaqueContext::from_vmcontext(caller_vmctx), &mut [])
372368
})?;
373369
}
374370
Ok(())

crates/wasmtime/src/runtime/vm/vmcontext.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,34 @@ pub struct VMFuncRef {
690690
unsafe impl Send for VMFuncRef {}
691691
unsafe impl Sync for VMFuncRef {}
692692

693+
impl VMFuncRef {
694+
/// Invokes the `array_call` field of this `VMFuncRef` with the supplied
695+
/// arguments.
696+
///
697+
/// This will invoke the function pointer in the `array_call` field with:
698+
///
699+
/// * the `callee` vmctx as `self.vmctx`
700+
/// * the `caller` as `caller` specified here
701+
/// * the args pointer as `args_and_results`
702+
/// * the args length as `args_and_results`
703+
///
704+
/// The `args_and_results` area must be large enough to both load all
705+
/// arguments from and store all results to.
706+
///
707+
/// # Unsafety
708+
///
709+
/// This method is unsafe because it can be called with any pointers. They
710+
/// must all be valid for this wasm function call to proceed.
711+
pub unsafe fn array_call(&self, caller: *mut VMOpaqueContext, args_and_results: *mut [ValRaw]) {
712+
(self.array_call)(
713+
self.vmctx,
714+
caller,
715+
args_and_results.cast(),
716+
args_and_results.len(),
717+
)
718+
}
719+
}
720+
693721
#[cfg(test)]
694722
mod test_vm_func_ref {
695723
use super::VMFuncRef;

tests/all/call_hook.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fn call_wrapped_func() -> Result<(), Error> {
100100
Val::F32(3.0f32.to_bits()).to_raw(&mut store)?,
101101
Val::F64(4.0f64.to_bits()).to_raw(&mut store)?,
102102
];
103-
f.call_unchecked(&mut store, args.as_mut_ptr(), args.len())?;
103+
f.call_unchecked(&mut store, &mut args)?;
104104
}
105105
n += 1;
106106

0 commit comments

Comments
 (0)