Skip to content

Commit 6275df6

Browse files
committed
Refactor: make use of Store available during catch_traps and HostResult conversion.
1 parent 50bb7a5 commit 6275df6

File tree

7 files changed

+208
-162
lines changed

7 files changed

+208
-162
lines changed

crates/wasmtime/src/runtime/func.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@ use crate::runtime::vm::{
77
};
88
use crate::store::{AutoAssertNoGc, InstanceId, StoreId, StoreOpaque};
99
use crate::type_registry::RegisteredType;
10-
#[cfg(feature = "gc")]
11-
use crate::vm::{ExceptionTombstone, VMGcRef};
1210
use crate::{
1311
AsContext, AsContextMut, CallHook, Engine, Extern, FuncType, Instance, ModuleExport, Ref,
1412
StoreContext, StoreContextMut, Val, ValRaw, ValType,
1513
};
16-
#[cfg(feature = "gc")]
17-
use crate::{ExnRef, Rooted};
1814
use alloc::sync::Arc;
1915
use core::ffi::c_void;
2016
#[cfg(feature = "async")]
@@ -1270,21 +1266,7 @@ impl Func {
12701266

12711267
val_vec.extend((0..ty.results().len()).map(|_| Val::null_func_ref()));
12721268
let (params, results) = val_vec.split_at_mut(nparams);
1273-
match func(caller.sub_caller(), params, results) {
1274-
Ok(()) => {}
1275-
#[cfg(feature = "gc")]
1276-
Err(e) if e.is::<Rooted<ExnRef>>() => {
1277-
let exnref = e.downcast::<Rooted<ExnRef>>().unwrap();
1278-
let exnref = VMGcRef::from_raw_u32(exnref.to_raw(&mut caller.store.0).unwrap())
1279-
.unwrap()
1280-
.into_exnref_unchecked();
1281-
caller.store.0.set_pending_exception(exnref);
1282-
return Err(ExceptionTombstone.into());
1283-
}
1284-
Err(e) => {
1285-
return Err(e);
1286-
}
1287-
}
1269+
func(caller.sub_caller(), params, results)?;
12881270

12891271
// Unlike our arguments we need to dynamically check that the return
12901272
// values produced are correct. There could be a bug in `func` that

crates/wasmtime/src/runtime/store.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,6 +2247,7 @@ at https://bytecodealliance.org/security.
22472247
/// must be done separately.
22482248
#[cfg(feature = "gc")]
22492249
pub(crate) fn set_pending_exception(&mut self, exnref: VMExnRef) {
2250+
debug_assert!(self.pending_exception.is_none());
22502251
self.pending_exception = Some(exnref.into());
22512252
}
22522253

crates/wasmtime/src/runtime/trap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[cfg(feature = "coredump")]
22
use super::coredump::WasmCoreDump;
33
#[cfg(feature = "gc")]
4-
use super::{ExnRef, store::AutoAssertNoGc, vm::ExceptionTombstone};
4+
use super::{ExnRef, store::AutoAssertNoGc};
55
use crate::prelude::*;
66
use crate::store::StoreOpaque;
77
use crate::{AsContext, Module};
@@ -82,7 +82,7 @@ pub(crate) fn from_runtime_box(
8282
} = *runtime_trap;
8383
let (mut error, pc) = match reason {
8484
#[cfg(feature = "gc")]
85-
crate::runtime::vm::TrapReason::User(error) if error.is::<ExceptionTombstone>() => {
85+
crate::runtime::vm::TrapReason::Exception => {
8686
let mut nogc = AutoAssertNoGc::new(store);
8787
let exnref = nogc.take_pending_exception();
8888
let exnref = ExnRef::_from_raw(&mut nogc, exnref.as_gc_ref().as_raw_u32())

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ fn memory_grow(
168168
mut instance: Pin<&mut Instance>,
169169
delta: u64,
170170
memory_index: u32,
171-
) -> Result<Option<AllocationSize>, TrapReason> {
171+
) -> Result<Option<AllocationSize>> {
172172
let memory_index = DefinedMemoryIndex::from_u32(memory_index);
173173
let module = instance.env_module();
174174
let page_size_log2 = module.memories[module.memory_index(memory_index)].page_size_log2;
@@ -1540,30 +1540,30 @@ fn cont_new(
15401540
func: *mut u8,
15411541
param_count: u32,
15421542
result_count: u32,
1543-
) -> Result<Option<AllocationSize>, TrapReason> {
1543+
) -> Result<Option<AllocationSize>> {
15441544
let ans =
15451545
crate::vm::stack_switching::cont_new(store, instance, func, param_count, result_count)?;
15461546
Ok(Some(AllocationSize(ans.cast::<u8>() as usize)))
15471547
}
15481548

15491549
#[cfg(feature = "gc")]
1550-
unsafe fn get_instance_id(_store: &mut dyn VMStore, instance: Pin<&mut Instance>) -> u32 {
1550+
fn get_instance_id(_store: &mut dyn VMStore, instance: Pin<&mut Instance>) -> u32 {
15511551
instance.id().as_u32()
15521552
}
15531553

15541554
#[cfg(feature = "gc")]
1555-
unsafe fn throw_ref(
1555+
fn throw_ref(
15561556
mut store: &mut dyn VMStore,
15571557
_instance: Pin<&mut Instance>,
15581558
exnref: u32,
15591559
) -> Result<(), TrapReason> {
1560-
use crate::{AsStoreOpaqueMut, vm::ExceptionTombstone};
1560+
use crate::AsStoreOpaqueMut;
15611561

15621562
let exnref = VMGcRef::from_raw_u32(exnref).ok_or_else(|| Trap::NullReference)?;
15631563
let exnref = store.unwrap_gc_store_mut().clone_gc_ref(&exnref);
15641564
let exnref = exnref
15651565
.into_exnref(&*store.unwrap_gc_store().gc_heap)
15661566
.expect("gc ref should be an exception object");
15671567
store.as_store_opaque_mut().set_pending_exception(exnref);
1568-
Err(TrapReason::User(ExceptionTombstone.into()))
1568+
Err(TrapReason::Exception)
15691569
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ pub fn cont_new(
306306
func: *mut u8,
307307
param_count: u32,
308308
result_count: u32,
309-
) -> Result<*mut VMContRef, crate::vm::TrapReason> {
309+
) -> anyhow::Result<*mut VMContRef> {
310310
let caller_vmctx = instance.vmctx();
311311

312312
let stack_size = store.engine().config().async_stack_size;

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,23 @@ use core::ptr::NonNull;
55
use wasmtime_environ::TagIndex;
66
use wasmtime_unwinder::{Frame, ThrowAction};
77

8-
use super::{VMContext, VMExnRef};
8+
use super::{VMContext, VMStore};
99
use crate::store::AutoAssertNoGc;
1010

11-
/// Implementation of exception throw.
11+
/// Compute the target of the pending exception on the store.
1212
///
1313
/// # Safety
1414
///
1515
/// Must be invoked when Wasm is in the stack and control has
1616
/// re-entered the runtime.
17-
pub unsafe fn compute_throw(nogc: &mut AutoAssertNoGc, exnref: &VMExnRef) -> ThrowAction {
17+
pub unsafe fn compute_throw_action(store: &mut dyn VMStore) -> ThrowAction {
18+
let mut nogc = AutoAssertNoGc::new(store.store_opaque_mut());
19+
1820
// Get the tag identity relative to the store.
21+
let exnref = nogc.take_pending_exception(); // Temporarily take, to avoid borrowing issues.
1922
let (throwing_tag_instance_id, throwing_tag_defined_tag_index) =
20-
exnref.tag(nogc).expect("cannot read tag");
23+
exnref.tag(&mut nogc).expect("cannot read tag");
24+
nogc.set_pending_exception(exnref);
2125
log::trace!(
2226
"throwing: tag defined in instance {throwing_tag_instance_id:?} defined-tag {throwing_tag_defined_tag_index:?}"
2327
);

0 commit comments

Comments
 (0)