Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c-api: Fix wasmtime_func_call_unchecked to communicate all errors #5262

Merged
merged 1 commit into from Nov 14, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions crates/c-api/include/wasmtime/func.h
Expand Up @@ -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
);

/**
Expand Down
25 changes: 14 additions & 11 deletions crates/c-api/src/func.rs
Expand Up @@ -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>() {
*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)));
Expand All @@ -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<Box<wasmtime_error_t>> {
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<Box<wasmtime_error_t>> {
if err.is::<Trap>() {
*trap_ret = Box::into_raw(Box::new(wasm_trap_t::new(err)));
None
} else {
Some(Box::new(wasmtime_error_t::from(err)))
}
}

Expand Down