A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Refine raw `VMContext` helpers (#11670)
Alex Crichton4e8ab840ca9c
diff --git a/crates/wasmtime/src/runtime/vm/component.rs b/crates/wasmtime/src/runtime/vm/component.rsindex 0e3168283b..541b8823ab 100644--- a/crates/wasmtime/src/runtime/vm/component.rs+++ b/crates/wasmtime/src/runtime/vm/component.rs@@ -223,11 +223,8 @@ impl ComponentInstance {{// SAFETY: it's a contract of this function that `vmctx` is a valid// allocation which can go backwards to a `ComponentInstance`.- let mut ptr = unsafe {- vmctx- .byte_sub(mem::size_of::<ComponentInstance>())- .cast::<ComponentInstance>()- };+ let mut ptr = unsafe { Self::from_vmctx(vmctx) };+// SAFETY: it's a contract of this function that it's safe to use `ptr`// as a mutable reference.let reference = unsafe { ptr.as_mut() };@@ -240,6 +237,23 @@ impl ComponentInstance {catch_unwind_and_record_trap(store, |store| f(store, instance))}+ /// Returns the `InstanceId` associated with the `vmctx` provided.+ ///+ /// # Safety+ ///+ /// The `vmctx` pointer must be a valid pointer and allocation within a+ /// `ComponentInstance`. See `Instance::from_vmctx` for some more+ /// information.+ unsafe fn from_vmctx(vmctx: NonNull<VMComponentContext>) -> NonNull<ComponentInstance> {+ // SAFETY: it's a contract of this function that `vmctx` is a valid+ // pointer to do this pointer arithmetic on.+ unsafe {+ vmctx+ .byte_sub(mem::size_of::<ComponentInstance>())+ .cast::<ComponentInstance>()+ }+ }+/// Returns the `InstanceId` associated with the `vmctx` provided.////// # Safety@@ -251,13 +265,7 @@ impl ComponentInstance {) -> ComponentInstanceId {// SAFETY: it's a contract of this function that `vmctx` is a valid// pointer with a `ComponentInstance` in front which can be read.- unsafe {- vmctx- .byte_sub(mem::size_of::<ComponentInstance>())- .cast::<ComponentInstance>()- .as_ref()- .id- }+ unsafe { Self::from_vmctx(vmctx).as_ref().id }}/// Returns the layout corresponding to what would be an allocation of adiff --git a/crates/wasmtime/src/runtime/vm/instance.rs b/crates/wasmtime/src/runtime/vm/instance.rsindex 62eb30304a..41965ac15c 100644--- a/crates/wasmtime/src/runtime/vm/instance.rs+++ b/crates/wasmtime/src/runtime/vm/instance.rs@@ -204,6 +204,48 @@ impl Instance {ret}+ /// Converts a raw `VMContext` pointer into a raw `Instance` pointer.+ ///+ /// # Safety+ ///+ /// Calling this function safely requires that `vmctx` is a valid allocation+ /// of a `VMContext` which is derived from `Instance::new`. To safely+ /// convert the returned raw pointer into a safe instance pointer callers+ /// will also want to uphold guarantees such as:+ ///+ /// * The instance should not be in use elsewhere. For example you can't+ /// call this function twice, turn both raw pointers into safe pointers,+ /// and then use both safe pointers.+ /// * There should be no other active mutable borrow to any other instance+ /// within the same store. Note that this is not restricted to just this+ /// instance pointer, but to all instances in a store. Instances can+ /// safely traverse to other instances "laterally" meaning that a mutable+ /// borrow on one is a mutable borrow on all.+ /// * There should be no active mutable borrow on the store accessible at+ /// the same time the instance is turned. Instances are owned by a store+ /// and a store can be used to acquire a safe instance borrow at any time.+ /// * The lifetime of the usage of the instance should not be unnecessarily+ /// long, for example it cannot be `'static`.+ ///+ /// Other entrypoints exist for converting from a raw `VMContext` to a safe+ /// pointer such as:+ ///+ /// * `Instance::enter_host_from_wasm`+ /// * `Instance::sibling_vmctx{,_mut}`+ ///+ /// These place further restrictions on the API signature to satisfy some of+ /// the above points.+ #[inline]+ pub(crate) unsafe fn from_vmctx(vmctx: NonNull<VMContext>) -> NonNull<Instance> {+ // SAFETY: The validity of `byte_sub` relies on `vmctx` being a valid+ // allocation.+ unsafe {+ vmctx+ .byte_sub(mem::size_of::<Instance>())+ .cast::<Instance>()+ }+ }+/// Encapsulated entrypoint to the host from WebAssembly, converting a raw/// `VMContext` pointer into a `VMStore` plus an `InstanceId`.///@@ -231,18 +273,13 @@ impl Instance {whereR: HostResult,{- // SAFETY: The validity of this `byte_sub` relies on `vmctx` being a- // valid allocation which is itself a contract of this function.- // Additionally `as_mut` requires that the pointer is valid, which is- // also a contract of this function. The lifetime of the reference will- // be constrained by the closure `f` provided to this function which- // inherently can't have the pointer escape, so the lifetime is scoped- // here.+ // SAFETY: It's a contract of this function that `vmctx` is a valid+ // pointer with neither the store nor other instances actively in use+ // when this is called, so it should be safe to acquire a mutable+ // pointer to the store and read the instance pointer.let (store, instance) = unsafe {- let instance = vmctx- .byte_sub(mem::size_of::<Instance>())- .cast::<Instance>()- .as_mut();+ let instance = Instance::from_vmctx(vmctx);+ let instance = instance.as_ref();let store = &mut *instance.store.unwrap().0.as_ptr();(store, instance.id)};@@ -265,12 +302,8 @@ impl Instance {#[inline]unsafe fn sibling_vmctx<'a>(&'a self, vmctx: NonNull<VMContext>) -> &'a Instance {// SAFETY: it's a contract of this function itself that `vmctx` is a- // valid pointer such that this pointer arithmetic is valid.- let ptr = unsafe {- vmctx- .byte_sub(mem::size_of::<Instance>())- .cast::<Instance>()- };+ // valid pointer. Additionally with `self` being a+ let ptr = unsafe { Instance::from_vmctx(vmctx) };// SAFETY: it's a contract of this function itself that `vmctx` is a// valid pointer to dereference. Additionally the lifetime of the return// value is constrained to be the same as `self` to avoid granting a@@ -299,11 +332,7 @@ impl Instance {) -> Pin<&'a mut Instance> {// SAFETY: it's a contract of this function itself that `vmctx` is a// valid pointer such that this pointer arithmetic is valid.- let mut ptr = unsafe {- vmctx- .byte_sub(mem::size_of::<Instance>())- .cast::<Instance>()- };+ let mut ptr = unsafe { Instance::from_vmctx(vmctx) };// SAFETY: it's a contract of this function itself that `vmctx` is a// valid pointer to dereference. Additionally the lifetime of the return@@ -313,33 +342,6 @@ impl Instance {unsafe { Pin::new_unchecked(ptr.as_mut()) }}- /// Accessor from a raw `vmctx` to `&vm::Instance`, given a store.- ///- /// This is like the above `sibling_vmctx{,_mut}` accessors, but- /// takes the store explicitly rather than inferring it from an- /// existing instance in the store.- ///- /// # Safety- ///- /// The `vmctx` pointer must be a valid vmctx from an active- /// instance that belongs to the given `store`.- #[inline]- pub unsafe fn from_vmctx<'a>(- _store: &'a StoreOpaque,- vmctx: NonNull<VMContext>,- ) -> &'a Instance {- // SAFETY: The validity of this `byte_sub` relies on `vmctx`- // being a valid allocation which is itself a contract of this- // function. Likewise, the `.as_ref()` converts a valid `*mut- // Instance` to a `&Instance`.- unsafe {- vmctx- .byte_sub(mem::size_of::<Instance>())- .cast::<Instance>()- .as_ref()- }- }-pub(crate) fn env_module(&self) -> &Arc<wasmtime_environ::Module> {self.runtime_info.env_module()}diff --git a/crates/wasmtime/src/runtime/vm/throw.rs b/crates/wasmtime/src/runtime/vm/throw.rsindex d99c296ca6..ba5105fe11 100644--- a/crates/wasmtime/src/runtime/vm/throw.rs+++ b/crates/wasmtime/src/runtime/vm/throw.rs@@ -80,19 +80,24 @@ pub unsafe fn compute_handler(store: &mut dyn VMStore) -> Option<Handler> {let frame_vmctx =NonNull::new(frame_vmctx as *mut VMContext).expect("null vmctx in frame");- // SAFETY: we use `Instance::from_vmctx` to get an- // `&Instance` from a raw vmctx we read off the- // stack frame. That method's safety requirements- // are that the `vmctx` is a valid vmctx belonging- // to an instance in the store (`nogc`). This is- // satisfied because every Wasm frame in this- // activation must belong to an instance in this- // store: we do not permit cross-store calls- // without exiting through host code.+ // SAFETY: we use `Instance::from_vmctx` to get a+ // `NonNull<Instance>` from a raw vmctx we read off the+ // stack frame. That method's safety requirements are that+ // the `vmctx` is a valid vmctx allocation which should be+ // true of all frames on the stack.+ //+ // Next the `.as_ref()` call enables reading this pointer,+ // and the validity of this relies on the fact that all wasm+ // frames for this activation belong to the same store and+ // there's no other active instance borrows at this time.+ // This function takes `&mut dyn VMStore` representing no+ // other active borrows, and internally the borrow is scoped+ // to this one block.let (handler_tag_instance, handler_tag_index) = unsafe {let store_id = nogc.id();- let instance = Instance::from_vmctx(&nogc, frame_vmctx);+ let instance = Instance::from_vmctx(frame_vmctx);let tag = instance+ .as_ref().get_exported_tag(store_id, TagIndex::from_u32(module_local_tag_index));tag.to_raw_indices()};