From 10c7883e52b5d16acaafae1b17341018305b2730 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 14 Nov 2022 07:22:24 -0800 Subject: [PATCH] c-api: Fix `wasmtime_func_call_unchecked` to communicate all errors Change the return value of this function to a `wasmtime_error_t*` instead of the prior `wasm_trap_t*`. This is a leftover from #5149. Closes #5257 --- crates/c-api/include/wasmtime/func.h | 5 +++-- crates/c-api/src/func.rs | 25 ++++++++++++++----------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/crates/c-api/include/wasmtime/func.h b/crates/c-api/include/wasmtime/func.h index 2683eaabdff..e65d002c663 100644 --- a/crates/c-api/include/wasmtime/func.h +++ b/crates/c-api/include/wasmtime/func.h @@ -241,10 +241,11 @@ WASM_API_EXTERN wasmtime_error_t *wasmtime_func_call( * faster than that function, but the tradeoff is that embeddings must uphold * more invariants rather than relying on Wasmtime to check them for you. */ -WASM_API_EXTERN wasm_trap_t *wasmtime_func_call_unchecked( +WASM_API_EXTERN wasmtime_error_t *wasmtime_func_call_unchecked( wasmtime_context_t *store, const wasmtime_func_t *func, - wasmtime_val_raw_t *args_and_results + wasmtime_val_raw_t *args_and_results, + wasm_trap_t **trap ); /** diff --git a/crates/c-api/src/func.rs b/crates/c-api/src/func.rs index 68cebd0c427..fe2c27ed0ac 100644 --- a/crates/c-api/src/func.rs +++ b/crates/c-api/src/func.rs @@ -351,14 +351,7 @@ pub unsafe extern "C" fn wasmtime_func_call( store.data_mut().wasm_val_storage = params; None } - Ok(Err(trap)) => { - if trap.is::() { - *trap_ret = Box::into_raw(Box::new(wasm_trap_t::new(trap))); - None - } else { - Some(Box::new(wasmtime_error_t::from(trap))) - } - } + Ok(Err(trap)) => store_err(trap, trap_ret), Err(panic) => { let err = error_from_panic(panic); *trap_ret = Box::into_raw(Box::new(wasm_trap_t::new(err))); @@ -372,10 +365,20 @@ pub unsafe extern "C" fn wasmtime_func_call_unchecked( store: CStoreContextMut<'_>, func: &Func, args_and_results: *mut ValRaw, -) -> *mut wasm_trap_t { + trap_ret: &mut *mut wasm_trap_t, +) -> Option> { match func.call_unchecked(store, args_and_results) { - Ok(()) => ptr::null_mut(), - Err(trap) => Box::into_raw(Box::new(wasm_trap_t::new(trap))), + Ok(()) => None, + Err(trap) => store_err(trap, trap_ret), + } +} + +fn store_err(err: Error, trap_ret: &mut *mut wasm_trap_t) -> Option> { + if err.is::() { + *trap_ret = Box::into_raw(Box::new(wasm_trap_t::new(err))); + None + } else { + Some(Box::new(wasmtime_error_t::from(err))) } }