Merge pull request #5703 from wasmerio/simplify-unwind
Some checks failed
Builds / Set up (push) Has been cancelled
Builds / Build on macos-x64 (push) Has been cancelled
Builds / Build on macos-arm64 (push) Has been cancelled
Builds / Build on linux-arm64 (push) Has been cancelled
Builds / Build on linux-x64 (push) Has been cancelled
Builds / Build on windows-x64 (push) Has been cancelled
Builds / Windows GNU (push) Has been cancelled
Builds / macOS aarch64 (JSC) (push) Has been cancelled
Builds / macOS x86_64 (JSC) (push) Has been cancelled
Builds / Linux riscv64 (push) Has been cancelled
Builds / release (push) Has been cancelled
Documentation / Documentation (push) Has been cancelled
test-sys / Set up (push) Has been cancelled
test-sys / Code lint (push) Has been cancelled
test-sys / cargo-deny (push) Has been cancelled
test-sys / Test on NodeJS (push) Has been cancelled
test-sys / Test wasi-fyi (push) Has been cancelled
test-sys / Test WASIX (push) Has been cancelled
test-sys / Test wasm build (push) Has been cancelled
test-sys / Test JSC build (push) Has been cancelled
test-sys / Test API for v8 feature on linux-x64 (push) Has been cancelled
test-sys / Test API for v8 feature on macos-arm (push) Has been cancelled
test-sys / Test API for wamr feature on linux-x64 (push) Has been cancelled
test-sys / Test API for wamr feature on macos-arm (push) Has been cancelled
test-sys / Test API for wasmi feature on linux-x64 (push) Has been cancelled
test-sys / Test API for wasmi feature on macos-arm (push) Has been cancelled
test-sys / Test build docs rs (push) Has been cancelled
test-sys / Build C-API on linux-aarch64 (push) Has been cancelled
test-sys / Build wasmer-cli on linux-aarch64 (push) Has been cancelled
test-sys / Build C-API on linux-riscv64 (push) Has been cancelled
test-sys / Build wasmer-cli on linux-riscv64 (push) Has been cancelled
test-sys / Build and test C-API on linux-musl (push) Has been cancelled
test-sys / Build and test C-API on linux-x64 (push) Has been cancelled
test-sys / Build and test C-API on macos-arm (push) Has been cancelled
test-sys / Build and test C-API on macos-x64 (push) Has been cancelled
test-sys / Build and test C-API on windows-gnu (push) Has been cancelled
test-sys / Build and test C-API on windows-x64 (push) Has been cancelled
test-sys / Build wasmer-cli on linux-musl (push) Has been cancelled
test-sys / Build wasmer-cli on linux-x64 (push) Has been cancelled
test-sys / Build wasmer-cli on macos-arm (push) Has been cancelled
test-sys / Build wasmer-cli on macos-x64 (push) Has been cancelled
test-sys / Build wasmer-cli on windows-gnu (push) Has been cancelled
test-sys / Build wasmer-cli on windows-x64 (push) Has been cancelled
test-sys / Check wasmer API with all sys features enabled on linux-musl (push) Has been cancelled
test-sys / Check wasmer API with all sys features enabled on linux-x64 (push) Has been cancelled
test-sys / Check wasmer API with all sys features enabled on macos-arm (push) Has been cancelled
test-sys / Check wasmer API with all sys features enabled on macos-x64 (push) Has been cancelled
test-sys / Check wasmer API with all sys features enabled on windows-gnu (push) Has been cancelled
test-sys / Check wasmer API with all sys features enabled on windows-x64 (push) Has been cancelled
test-sys / Build and test C-API with v8 on linux-musl (push) Has been cancelled
test-sys / Build and test C-API with v8 on linux-x64 (push) Has been cancelled
test-sys / Build and test C-API with v8 on macos-arm (push) Has been cancelled
test-sys / Build and test C-API with v8 on macos-x64 (push) Has been cancelled
test-sys / Build and test C-API with v8 on windows-gnu (push) Has been cancelled
test-sys / Build and test C-API with v8 on windows-x64 (push) Has been cancelled
test-sys / Run wast test suite for all compilers on linux-musl (push) Has been cancelled
test-sys / Unit-test cranelift on no-std on linux-musl (push) Has been cancelled
test-sys / Unit-test examples on linux-musl (push) Has been cancelled
test-sys / Unit-test packages on std on linux-musl (push) Has been cancelled
test-sys / Unit-test singlepass on no-std on linux-musl (push) Has been cancelled
test-sys / Unit-test wasmer-cli on linux-musl (push) Has been cancelled
test-sys / Run wast test suite for all compilers on linux-x64 (push) Has been cancelled
test-sys / Unit-test cranelift on no-std on linux-x64 (push) Has been cancelled
test-sys / Unit-test examples on linux-x64 (push) Has been cancelled
test-sys / Unit-test packages on std on linux-x64 (push) Has been cancelled
test-sys / Unit-test singlepass on no-std on linux-x64 (push) Has been cancelled
test-sys / Unit-test wasmer-cli on linux-x64 (push) Has been cancelled
test-sys / Run wast test suite for all compilers on macos-arm (push) Has been cancelled
test-sys / Unit-test cranelift on no-std on macos-arm (push) Has been cancelled
test-sys / Unit-test examples on macos-arm (push) Has been cancelled
test-sys / Unit-test packages on std on macos-arm (push) Has been cancelled
test-sys / Unit-test singlepass on no-std on macos-arm (push) Has been cancelled
test-sys / Unit-test wasmer-cli on macos-arm (push) Has been cancelled
test-sys / Run wast test suite for all compilers on macos-x64 (push) Has been cancelled
test-sys / Unit-test cranelift on no-std on macos-x64 (push) Has been cancelled
test-sys / Unit-test examples on macos-x64 (push) Has been cancelled
test-sys / Unit-test packages on std on macos-x64 (push) Has been cancelled
test-sys / Unit-test singlepass on no-std on macos-x64 (push) Has been cancelled
test-sys / Unit-test wasmer-cli on macos-x64 (push) Has been cancelled
test-sys / Run wast test suite for all compilers on windows-x64 (push) Has been cancelled
test-sys / Unit-test cranelift on no-std on windows-x64 (push) Has been cancelled
test-sys / Unit-test examples on windows-x64 (push) Has been cancelled
test-sys / Unit-test packages on std on windows-x64 (push) Has been cancelled
test-sys / Unit-test singlepass on no-std on windows-x64 (push) Has been cancelled
test-sys / Unit-test wasmer-cli on windows-x64 (push) Has been cancelled
test-sys / CLI integration tests on linux-musl (push) Has been cancelled
test-sys / CLI integration tests on linux-x64 (push) Has been cancelled
test-sys / CLI integration tests on macos-arm (push) Has been cancelled
test-sys / CLI integration tests on macos-x64 (push) Has been cancelled
test-sys / CLI integration tests on windows-x64 (push) Has been cancelled
test-sys / test-wasmer-integration-tests (push) Has been cancelled
wasmer-config / Compile and Test (push) Has been cancelled
wasmer-config / Linting and Formatting (push) Has been cancelled

Simplify unwind
This commit is contained in:
Syrus Akbary
2025-08-26 00:14:44 +02:00
committed by GitHub
3 changed files with 144 additions and 135 deletions

View File

@@ -440,12 +440,18 @@ where
this: &mut VMDynamicFunctionContext<Self>,
values_vec: *mut RawValue,
) {
let result =
on_host_stack(|| panic::catch_unwind(AssertUnwindSafe(|| (this.ctx.func)(values_vec))));
let result = on_host_stack(|| {
panic::catch_unwind(AssertUnwindSafe(|| {
(this.ctx.func)(values_vec).map_err(Box::new)
}))
});
// IMPORTANT: DO NOT ALLOCATE ON THE STACK,
// AS WE ARE IN THE WASM STACK, NOT ON THE HOST ONE.
// See: https://github.com/wasmerio/wasmer/pull/5700
match result {
Ok(Ok(())) => {}
Ok(Err(trap)) => raise_user_trap(on_host_stack(|| Box::new(trap))),
Ok(Err(trap)) => raise_user_trap(trap),
Err(panic) => resume_panic(panic),
}
}
@@ -522,21 +528,22 @@ macro_rules! impl_host_function {
RetsAsResult: IntoResult<Rets>,
Func: Fn($( $x , )*) -> RetsAsResult + 'static,
{
// println!("func wrapper");
let mut store = StoreMut::from_raw(env.raw_store as *mut _);
let result = on_host_stack(|| {
// println!("func wrapper1");
panic::catch_unwind(AssertUnwindSafe(|| {
$(
let $x = FromToNativeWasmType::from_native(NativeWasmTypeInto::from_abi(&mut store, $x));
)*
(env.func)($($x),* ).into_result()
(env.func)($($x),* ).into_result().map_err(Box::new)
}))
});
// IMPORTANT: DO NOT ALLOCATE ON THE STACK,
// AS WE ARE IN THE WASM STACK, NOT ON THE HOST ONE.
// See: https://github.com/wasmerio/wasmer/pull/5700
match result {
Ok(Ok(result)) => return result.into_c_struct(&mut store),
Ok(Err(trap)) => raise_user_trap(on_host_stack(|| Box::new(trap))),
Ok(Err(trap)) => raise_user_trap(trap),
Err(panic) => resume_panic(panic) ,
}
}
@@ -551,20 +558,20 @@ macro_rules! impl_host_function {
() -> crate::backend::sys::vm::VMTrampoline {
unsafe extern "C" fn call_trampoline<$( $x: FromToNativeWasmType, )* Rets: WasmTypeList>
(
vmctx: *mut crate::backend::sys::vm::VMContext,
body: crate::backend::sys::vm::VMFunctionCallback,
args: *mut RawValue,
) {
let body: unsafe extern "C" fn(vmctx: *mut crate::backend::sys::vm::VMContext, $( $x: <$x::Native as NativeWasmType>::Abi, )*) -> Rets::CStruct = std::mem::transmute(body);
let mut _n = 0;
$(
let $x = *args.add(_n).cast();
_n += 1;
)*
(
vmctx: *mut crate::backend::sys::vm::VMContext,
body: crate::backend::sys::vm::VMFunctionCallback,
args: *mut RawValue,
) {
let body: unsafe extern "C" fn(vmctx: *mut crate::backend::sys::vm::VMContext, $( $x: <$x::Native as NativeWasmType>::Abi, )*) -> Rets::CStruct = std::mem::transmute(body);
let mut _n = 0;
$(
let $x = *args.add(_n).cast();
_n += 1;
)*
let results = body(vmctx, $( $x ),*);
Rets::write_c_struct_to_ptr(results, args);
let results = body(vmctx, $( $x ),*);
Rets::write_c_struct_to_ptr(results, args);
}
call_trampoline::<$( $x, )* Rets> as _
@@ -587,25 +594,28 @@ macro_rules! impl_host_function {
{
let mut store = StoreMut::from_raw(env.raw_store as *mut _);
let result = wasmer_vm::on_host_stack(|| {
panic::catch_unwind(AssertUnwindSafe(|| {
$(
let $x = FromToNativeWasmType::from_native(NativeWasmTypeInto::from_abi(&mut store, $x));
)*
let store_mut = StoreMut::from_raw(env.raw_store as *mut _);
let f_env = crate::backend::sys::function::env::FunctionEnvMut {
store_mut,
func_env: env.env.as_sys().clone(),
}.into();
(env.func)(f_env, $($x),* ).into_result()
}))
});
let result = wasmer_vm::on_host_stack(|| {
panic::catch_unwind(AssertUnwindSafe(|| {
$(
let $x = FromToNativeWasmType::from_native(NativeWasmTypeInto::from_abi(&mut store, $x));
)*
let store_mut = StoreMut::from_raw(env.raw_store as *mut _);
let f_env = crate::backend::sys::function::env::FunctionEnvMut {
store_mut,
func_env: env.env.as_sys().clone(),
}.into();
(env.func)(f_env, $($x),* ).into_result().map_err(Box::new)
}))
});
match result {
Ok(Ok(result)) => return result.into_c_struct(&mut store),
Ok(Err(trap)) => wasmer_vm::raise_user_trap(on_host_stack(|| Box::new(trap))),
Err(panic) => wasmer_vm::resume_panic(panic),
}
// IMPORTANT: DO NOT ALLOCATE ON THE STACK,
// AS WE ARE IN THE WASM STACK, NOT ON THE HOST ONE.
// See: https://github.com/wasmerio/wasmer/pull/5700
match result {
Ok(Ok(result)) => return result.into_c_struct(&mut store),
Ok(Err(trap)) => raise_user_trap(trap),
Err(panic) => resume_panic(panic),
}
}
func_wrapper::< T, $( $x, )* Rets, RetsAsResult, Func > as _
}
@@ -620,21 +630,20 @@ macro_rules! impl_host_function {
body: crate::backend::sys::vm::VMFunctionCallback,
args: *mut RawValue,
) {
let body: unsafe extern "C" fn(vmctx: *mut crate::backend::sys::vm::VMContext, $( $x: <$x::Native as NativeWasmType>::Abi, )*) -> Rets::CStruct = std::mem::transmute(body);
let mut _n = 0;
$(
let $x = *args.add(_n).cast();
_n += 1;
)*
let body: unsafe extern "C" fn(vmctx: *mut crate::backend::sys::vm::VMContext, $( $x: <$x::Native as NativeWasmType>::Abi, )*) -> Rets::CStruct = std::mem::transmute(body);
let mut _n = 0;
$(
let $x = *args.add(_n).cast();
_n += 1;
)*
let results = body(vmctx, $( $x ),*);
Rets::write_c_struct_to_ptr(results, args);
let results = body(vmctx, $( $x ),*);
Rets::write_c_struct_to_ptr(results, args);
}
call_trampoline::<$( $x, )* Rets> as _
call_trampoline::<$( $x, )* Rets> as _
}
}
};
}};
}
// Here we go! Let's generate all the C struct, `WasmTypeList`

View File

@@ -619,68 +619,68 @@ macro_rules! impl_host_function {
<$( $x: FromToNativeWasmType, )* Rets: WasmTypeList, RetsAsResult: IntoResult<Rets>, T: Send + 'static, Func: Fn(FunctionEnvMut<T>, $( $x , )*) -> RetsAsResult + 'static>
(this: &Func) -> crate::backend::v8::vm::VMFunctionCallback {
unsafe extern "C" fn func_wrapper<$( $x, )* Rets, RetsAsResult, Func, T>(env: *mut c_void, args: *const wasm_val_vec_t, results: *mut wasm_val_vec_t) -> *mut wasm_trap_t
where
$( $x: FromToNativeWasmType, )*
Rets: WasmTypeList,
RetsAsResult: IntoResult<Rets>,
T: Send + 'static,
Func: Fn(FunctionEnvMut<'_, T>, $( $x , )*) -> RetsAsResult + 'static,
where
$( $x: FromToNativeWasmType, )*
Rets: WasmTypeList,
RetsAsResult: IntoResult<Rets>,
T: Send + 'static,
Func: Fn(FunctionEnvMut<'_, T>, $( $x , )*) -> RetsAsResult + 'static,
{
let r: *mut (crate::backend::v8::function::FunctionCallbackEnv<'_, Func>) = env as _;
let store = &mut (*r).store.as_store_mut();
let r: *mut (crate::backend::v8::function::FunctionCallbackEnv<'_, Func>) = env as _;
let store = &mut (*r).store.as_store_mut();
let mut i = 0;
let mut i = 0;
$(
let c_arg = (*(*args).data.wrapping_add(i)).clone();
let wasmer_arg = c_arg.into_wv();
let raw_arg : RawValue = wasmer_arg.as_raw(store);
let $x : $x = FromToNativeWasmType::from_native($x::Native::from_raw(store, raw_arg));
$(
let c_arg = (*(*args).data.wrapping_add(i)).clone();
let wasmer_arg = c_arg.into_wv();
let raw_arg : RawValue = wasmer_arg.as_raw(store);
let $x : $x = FromToNativeWasmType::from_native($x::Native::from_raw(store, raw_arg));
i += 1;
i += 1;
)*
let env_handle = (*r).env_handle.as_ref().unwrap().clone();
let mut fn_env = crate::backend::v8::function::env::FunctionEnv::from_handle(env_handle).into_mut(store);
let func: &Func = &(*r).func;
let env_handle = (*r).env_handle.as_ref().unwrap().clone();
let mut fn_env = crate::backend::v8::function::env::FunctionEnv::from_handle(env_handle).into_mut(store);
let func: &Func = &(*r).func;
let result = panic::catch_unwind(AssertUnwindSafe(|| unsafe {
((*r).func)(BackendFunctionEnvMut::V8(fn_env).into(), $( $x, )* ).into_result()
}));
let result = panic::catch_unwind(AssertUnwindSafe(|| unsafe {
((*r).func)(BackendFunctionEnvMut::V8(fn_env).into(), $( $x, )* ).into_result()
}));
match result {
Ok(Ok(result)) => {
let types = Rets::wasm_types();
let mut native_results = result.into_array(store);
let native_results = native_results.as_mut();
match result {
Ok(Ok(result)) => {
let types = Rets::wasm_types();
let mut native_results = result.into_array(store);
let native_results = native_results.as_mut();
let native_results: Vec<Value> = native_results.into_iter().enumerate().map(|(i, r)| Value::from_raw(store, types[i], r.clone())).collect();
let native_results: Vec<Value> = native_results.into_iter().enumerate().map(|(i, r)| Value::from_raw(store, types[i], r.clone())).collect();
let mut c_results: Vec<wasm_val_t> = native_results.into_iter().map(IntoCApiValue::into_cv).collect();
let mut c_results: Vec<wasm_val_t> = native_results.into_iter().map(IntoCApiValue::into_cv).collect();
if c_results.len() != (*results).size {
panic!("when calling host function: number of observed results differ from wanted results")
}
if c_results.len() != (*results).size {
panic!("when calling host function: number of observed results differ from wanted results")
}
unsafe {
for i in 0..(*results).size {
*((*results).data.wrapping_add(i)) = c_results[i]
}
unsafe {
for i in 0..(*results).size {
*((*results).data.wrapping_add(i)) = c_results[i]
}
}
}
unsafe { std::ptr::null_mut() }
},
unsafe { std::ptr::null_mut() }
},
Ok(Err(e)) => { let trap = crate::backend::v8::error::Trap::user(Box::new(e)); unsafe { trap.into_wasm_trap(store) } },
Ok(Err(e)) => { let trap = crate::backend::v8::error::Trap::user(Box::new(e)); unsafe { trap.into_wasm_trap(store) } },
Err(e) => { unimplemented!("host function panicked"); }
}
}
Err(e) => { unimplemented!("host function panicked"); }
}
}
func_wrapper::< $( $x, )* Rets, RetsAsResult, Func, T> as _
func_wrapper::< $( $x, )* Rets, RetsAsResult, Func, T> as _
}
}
};

View File

@@ -621,68 +621,68 @@ macro_rules! impl_host_function {
<$( $x: FromToNativeWasmType, )* Rets: WasmTypeList, RetsAsResult: IntoResult<Rets>, T: Send + 'static, Func: Fn(FunctionEnvMut<T>, $( $x , )*) -> RetsAsResult + 'static>
(this: &Func) -> crate::backend::wasmi::vm::VMFunctionCallback {
unsafe extern "C" fn func_wrapper<$( $x, )* Rets, RetsAsResult, Func, T>(env: *mut c_void, args: *const wasm_val_vec_t, results: *mut wasm_val_vec_t) -> *mut wasm_trap_t
where
$( $x: FromToNativeWasmType, )*
Rets: WasmTypeList,
RetsAsResult: IntoResult<Rets>,
T: Send + 'static,
Func: Fn(FunctionEnvMut<'_, T>, $( $x , )*) -> RetsAsResult + 'static,
where
$( $x: FromToNativeWasmType, )*
Rets: WasmTypeList,
RetsAsResult: IntoResult<Rets>,
T: Send + 'static,
Func: Fn(FunctionEnvMut<'_, T>, $( $x , )*) -> RetsAsResult + 'static,
{
let r: *mut (crate::backend::wasmi::function::FunctionCallbackEnv<'_, Func>) = env as _;
let store = &mut (*r).store.as_store_mut();
let r: *mut (crate::backend::wasmi::function::FunctionCallbackEnv<'_, Func>) = env as _;
let store = &mut (*r).store.as_store_mut();
let mut i = 0;
let mut i = 0;
$(
let c_arg = (*(*args).data.wrapping_add(i)).clone();
let wasmer_arg = c_arg.into_wv();
let raw_arg : RawValue = wasmer_arg.as_raw(store);
let $x : $x = FromToNativeWasmType::from_native($x::Native::from_raw(store, raw_arg));
$(
let c_arg = (*(*args).data.wrapping_add(i)).clone();
let wasmer_arg = c_arg.into_wv();
let raw_arg : RawValue = wasmer_arg.as_raw(store);
let $x : $x = FromToNativeWasmType::from_native($x::Native::from_raw(store, raw_arg));
i += 1;
i += 1;
)*
let env_handle = (*r).env_handle.as_ref().unwrap().clone();
let mut fn_env = crate::backend::wasmi::function::env::FunctionEnv::from_handle(env_handle).into_mut(store);
let func: &Func = &(*r).func;
let env_handle = (*r).env_handle.as_ref().unwrap().clone();
let mut fn_env = crate::backend::wasmi::function::env::FunctionEnv::from_handle(env_handle).into_mut(store);
let func: &Func = &(*r).func;
let result = panic::catch_unwind(AssertUnwindSafe(|| unsafe {
((*r).func)(BackendFunctionEnvMut::Wasmi(fn_env).into(), $( $x, )* ).into_result()
}));
let result = panic::catch_unwind(AssertUnwindSafe(|| unsafe {
((*r).func)(BackendFunctionEnvMut::Wasmi(fn_env).into(), $( $x, )* ).into_result()
}));
match result {
Ok(Ok(result)) => {
let types = Rets::wasm_types();
let mut native_results = result.into_array(store);
let native_results = native_results.as_mut();
match result {
Ok(Ok(result)) => {
let types = Rets::wasm_types();
let mut native_results = result.into_array(store);
let native_results = native_results.as_mut();
let native_results: Vec<Value> = native_results.into_iter().enumerate().map(|(i, r)| Value::from_raw(store, types[i], r.clone())).collect();
let native_results: Vec<Value> = native_results.into_iter().enumerate().map(|(i, r)| Value::from_raw(store, types[i], r.clone())).collect();
let mut c_results: Vec<wasm_val_t> = native_results.into_iter().map(IntoCApiValue::into_cv).collect();
let mut c_results: Vec<wasm_val_t> = native_results.into_iter().map(IntoCApiValue::into_cv).collect();
if c_results.len() != (*results).size {
panic!("when calling host function: number of observed results differ from wanted results")
}
if c_results.len() != (*results).size {
panic!("when calling host function: number of observed results differ from wanted results")
}
unsafe {
for i in 0..(*results).size {
*((*results).data.wrapping_add(i)) = c_results[i]
}
unsafe {
for i in 0..(*results).size {
*((*results).data.wrapping_add(i)) = c_results[i]
}
}
}
unsafe { std::ptr::null_mut() }
},
unsafe { std::ptr::null_mut() }
},
Ok(Err(e)) => { let trap = crate::backend::wasmi::error::Trap::user(Box::new(e)); unsafe { trap.into_wasm_trap(store) } },
Ok(Err(e)) => { let trap = crate::backend::wasmi::error::Trap::user(Box::new(e)); unsafe { trap.into_wasm_trap(store) } },
Err(e) => { unimplemented!("host function panicked"); }
}
}
Err(e) => { unimplemented!("host function panicked"); }
}
}
func_wrapper::< $( $x, )* Rets, RetsAsResult, Func, T> as _
func_wrapper::< $( $x, )* Rets, RetsAsResult, Func, T> as _
}
}
};