Skip to content

Commit 3c48653

Browse files
committed
Review feedback.
1 parent a49521c commit 3c48653

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

crates/wasmtime/src/runtime/store.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,26 @@ impl StoreOpaque {
13111311
self.instances[id].handle.get_mut()
13121312
}
13131313

1314+
/// Accessor from a raw `vmctx` to `&vm::Instance`.
1315+
///
1316+
/// # Safety
1317+
///
1318+
/// The `vmctx` pointer must be a valid vmctx from an active
1319+
/// instance that belongs to this store.
1320+
#[inline]
1321+
pub unsafe fn instance_from_vmctx(&self, vmctx: NonNull<VMContext>) -> &vm::Instance {
1322+
// SAFETY: The validity of this `byte_sub` relies on `vmctx`
1323+
// being a valid allocation which is itself a contract of this
1324+
// function. Likewise, the `.as_ref()` converts a valid `*mut
1325+
// Instance` to a `&Instance`.
1326+
unsafe {
1327+
vmctx
1328+
.byte_sub(mem::size_of::<vm::Instance>())
1329+
.cast::<vm::Instance>()
1330+
.as_ref()
1331+
}
1332+
}
1333+
13141334
/// Access multiple instances specified via `ids`.
13151335
///
13161336
/// # Panics

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::ptr::NonNull;
55
use wasmtime_environ::{TagIndex, Trap};
66
use wasmtime_unwinder::{Frame, ThrowAction};
77

8-
use super::{InstanceAndStore, VMContext, VMExnRef};
8+
use super::{VMContext, VMExnRef};
99
use crate::{
1010
store::AutoAssertNoGc,
1111
vm::{TrapReason, catch_unwind_and_record_trap, raise_preexisting_trap},
@@ -70,15 +70,21 @@ pub unsafe fn throw(nogc: &mut AutoAssertNoGc, exnref: VMExnRef) -> ! {
7070
let frame_vmctx =
7171
NonNull::new(frame_vmctx as *mut VMContext).expect("null vmctx in frame");
7272

73+
// SAFETY: we use `instance_from_vmctx` to get an
74+
// `&Instance` from a raw vmctx we read off the
75+
// stack frame. That method's safety requirements
76+
// are that the `vmctx` is a valid vmctx belonging
77+
// to an instance in the store (`nogc`). This is
78+
// satisfied because every Wasm frame in this
79+
// activation must belong to an instance in this
80+
// store: we do not permit cross-store calls
81+
// without exiting through host code.
7382
let (handler_tag_instance, handler_tag_index) = unsafe {
74-
InstanceAndStore::from_vmctx(frame_vmctx, |instance| {
75-
let (instance, store) = instance.unpack_mut();
76-
let tag = instance.get_exported_tag(
77-
store.id(),
78-
TagIndex::from_u32(module_local_tag_index),
79-
);
80-
tag.to_raw_indices()
81-
})
83+
let store_id = nogc.id();
84+
let instance = nogc.instance_from_vmctx(frame_vmctx);
85+
let tag = instance
86+
.get_exported_tag(store_id, TagIndex::from_u32(module_local_tag_index));
87+
tag.to_raw_indices()
8288
};
8389
log::trace!(
8490
"-> handler's tag {module_local_tag_index:?} resolves to instance {handler_tag_instance:?} defined-tag {handler_tag_index:?}"

0 commit comments

Comments
 (0)