Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Enable borrowing components and stores mutable at the same time (#11987)

Alex Crichtonec9b62abff64
diff --git a/crates/environ/src/component/info.rs b/crates/environ/src/component/info.rs
index 5e7e474381..51d14f1e63 100644
--- a/crates/environ/src/component/info.rs
+++ b/crates/environ/src/component/info.rs
@@ -540,6 +540,16 @@ pub struct CanonicalOptions {
pub data_model: CanonicalOptionsDataModel,
}
+impl CanonicalOptions {
+ /// Returns the memory referred to by these options, if any.
+ pub fn memory(&self) -> Option<RuntimeMemoryIndex> {
+ match self.data_model {
+ CanonicalOptionsDataModel::Gc {} => None,
+ CanonicalOptionsDataModel::LinearMemory(opts) => opts.memory,
+ }
+ }
+}
+
/// Possible encodings of strings within the component model.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[expect(missing_docs, reason = "self-describing variants")]
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rs
index 49e9b08ba1..e19258f1de 100644
--- a/crates/wasmtime/src/runtime/component/concurrent.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent.rs
@@ -50,7 +50,7 @@
//! store. This is equivalent to `StoreContextMut::spawn` but more convenient to use
//! in host functions.
-use crate::component::func::{self, Func, Options};
+use crate::component::func::{self, Func};
use crate::component::{HasData, HasSelf, Instance, Resource, ResourceTable, ResourceTableError};
use crate::fiber::{self, StoreFiber, StoreFiberYield};
use crate::store::{Store, StoreId, StoreInner, StoreOpaque, StoreToken};
@@ -2603,7 +2603,7 @@ impl Instance {
Some(memory) => {
let expected = lift.memory.map(|v| v.as_ptr()).unwrap_or(ptr::null_mut());
let actual = self.id().get(store).runtime_memory(memory);
- expected != actual
+ expected != actual.as_ptr()
}
// Memory not specified, meaning it didn't need to be
// specified per validation, so not invalid.
@@ -3159,13 +3159,11 @@ impl Instance {
(ordinal, 0, result)
}
};
- let options = Options::new_index(store, self, params.options);
- let ptr = func::validate_inbounds::<(u32, u32)>(
- options.memory_mut(store),
- &ValRaw::u32(params.payload),
- )?;
- options.memory_mut(store)[ptr + 0..][..4].copy_from_slice(&handle.to_le_bytes());
- options.memory_mut(store)[ptr + 4..][..4].copy_from_slice(&result.to_le_bytes());
+ let memory = self.options_memory_mut(store, params.options);
+ let ptr =
+ func::validate_inbounds::<(u32, u32)>(memory, &ValRaw::u32(params.payload))?;
+ memory[ptr + 0..][..4].copy_from_slice(&handle.to_le_bytes());
+ memory[ptr + 4..][..4].copy_from_slice(&result.to_le_bytes());
Ok(ordinal)
}
};
@@ -4902,11 +4900,15 @@ pub(crate) fn prepare_call<T, R>(
let (options, _flags, ty, raw_options) = handle.abi_info(store.0);
let instance = handle.instance().id().get(store.0);
+ let options = &instance.component().env_component().options[options];
let task_return_type = instance.component().types()[ty].results;
let component_instance = raw_options.instance;
- let callback = options.callback();
- let memory = options.memory_raw().map(SendSyncPtr::new);
- let string_encoding = options.string_encoding();
+ let callback = options.callback.map(|i| instance.runtime_callback(i));
+ let memory = options
+ .memory()
+ .map(|i| instance.runtime_memory(i))
+ .map(SendSyncPtr::new);
+ let string_encoding = options.string_encoding;
let token = StoreToken::new(store.as_context_mut());
let state = store.0.concurrent_state_mut();
@@ -5012,12 +5014,16 @@ fn queue_call0<T: 'static>(
guest_thread: QualifiedThreadId,
param_count: usize,
) -> Result<()> {
- let (options, flags, _ty, raw_options) = handle.abi_info(store.0);
+ let (_options, flags, _ty, raw_options) = handle.abi_info(store.0);
let is_concurrent = raw_options.async_;
+ let callback = raw_options.callback;
let instance = handle.instance();
let callee = handle.lifted_core_func(store.0);
- let callback = options.callback();
let post_return = handle.post_return_core_func(store.0);
+ let callback = callback.map(|i| {
+ let instance = instance.id().get(store.0);
+ SendSyncPtr::new(instance.runtime_callback(i))
+ });
log::trace!("queueing call {guest_thread:?}");
@@ -5039,7 +5045,7 @@ fn queue_call0<T: 'static>(
1,
instance_flags,
is_concurrent,
- callback.map(SendSyncPtr::new),
+ callback,
post_return.map(SendSyncPtr::new),
)
}
diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
index 89f2d6abf6..ad14ec34aa 100644
--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs
@@ -1,10 +1,10 @@
use super::table::{TableDebug, TableId};
use super::{Event, GlobalErrorContextRefCount, Waitable, WaitableCommon};
use crate::component::concurrent::{ConcurrentState, WorkItem, tls};
-use crate::component::func::{self, LiftContext, LowerContext, Options};
+use crate::component::func::{self, LiftContext, LowerContext};
use crate::component::matching::InstanceType;
use crate::component::values::{ErrorContextAny, FutureAny, StreamAny};
-use crate::component::{AsAccessor, Instance, Lower, Val, WasmList, WasmStr};
+use crate::component::{AsAccessor, Instance, Lift, Lower, Val, WasmList};
use crate::store::{StoreOpaque, StoreToken};
use crate::vm::component::{ComponentInstance, HandleTable, TransmitLocalState};
use crate::vm::{AlwaysMut, VMStore};
@@ -23,7 +23,7 @@ use futures::{FutureExt as _, stream};
use std::any::{Any, TypeId};
use std::boxed::Box;
use std::io::Cursor;
-use std::string::{String, ToString};
+use std::string::String;
use std::sync::{Arc, Mutex};
use std::vec::Vec;
use wasmtime_environ::component::{
@@ -112,7 +112,7 @@ impl TransmitIndex {
/// Retrieve the payload type of the specified stream or future, or `None` if it
/// has no payload type.
-fn payload(ty: TransmitIndex, types: &Arc<ComponentTypes>) -> Option<InterfaceType> {
+fn payload(ty: TransmitIndex, types: &ComponentTypes) -> Option<InterfaceType> {
match ty {
TransmitIndex::Future(ty) => types[types[ty].ty].payload,
TransmitIndex::Stream(ty) => types[types[ty].ty].payload,
@@ -135,20 +135,19 @@ fn get_mut_by_index_from(
fn lower<T: func::Lower + Send + 'static, B: WriteBuffer<T>, U: 'static>(
mut store: StoreContextMut<U>,
instance: Instance,
- options: &Options,
+ options: OptionsIndex,
ty: TransmitIndex,
address: usize,
count: usize,
buffer: &mut B,
) -> Result<()> {
- let types = instance.id().get(store.0).component().types().clone();
let count = buffer.remaining().len().min(count);
let lower = &mut if T::MAY_REQUIRE_REALLOC {
LowerContext::new
} else {
LowerContext::new_without_realloc
- }(store.as_context_mut(), options, &types, instance);
+ }(store.as_context_mut(), options, instance);
if address % usize::try_from(T::ALIGN32)? != 0 {
bail!("read pointer not aligned");
@@ -159,7 +158,7 @@ fn lower<T: func::Lower + Send + 'static, B: WriteBuffer<T>, U: 'static>(
.and_then(|b| b.get_mut(..T::SIZE32 * count))
.ok_or_else(|| anyhow::anyhow!("read pointer out of bounds of memory"))?;
- if let Some(ty) = payload(ty, &types) {
+ if let Some(ty) = payload(ty, lower.types) {
T::linear_store_list_to_memory(lower, ty, address, &buffer.remaining()[..count])?;
}
@@ -347,6 +346,7 @@ impl<D: 'static> DirectDestination<'_, D> {
address,
count,
options,
+ instance,
..
} = &transmit.read
else {
@@ -357,8 +357,8 @@ impl<D: 'static> DirectDestination<'_, D> {
unreachable!()
};
- options
- .memory_mut(self.store.0)
+ instance
+ .options_memory_mut(self.store.0, options)
.get_mut((address + guest_offset)..)
.and_then(|b| b.get_mut(..(count - guest_offset)))
.unwrap()
@@ -696,7 +696,7 @@ impl<'a, T> Source<'a, T> {
};
let cx =
- &mut LiftContext::new(store.0.store_opaque_mut(), &options, self.instance.unwrap());
+ &mut LiftContext::new(store.0.store_opaque_mut(), options, self.instance.unwrap());
let ty = payload(ty, cx.types);
let old_remaining = buffer.remaining_capacity();
lift::<T, B>(
@@ -783,6 +783,7 @@ impl<D: 'static> DirectSource<'_, D> {
address,
count,
options,
+ instance,
..
} = &transmit.write
else {
@@ -793,8 +794,8 @@ impl<D: 'static> DirectSource<'_, D> {
unreachable!()
};
- options
- .memory(self.store.0)
+ instance
+ .options_memory(self.store.0, options)
.get((address + guest_offset)..)
.and_then(|b| b.get(..(count - guest_offset)))
.unwrap()
@@ -1852,7 +1853,7 @@ enum WriteState {
instance: Instance,
ty: TransmitIndex,
flat_abi: Option<FlatAbi>,
- options: Options,
+ options: OptionsIndex,
address: usize,
count: usize,
handle: u32,
@@ -1888,7 +1889,8 @@ enum ReadState {
GuestReady {
ty: TransmitIndex,
flat_abi: Option<FlatAbi>,
- options: Options,
+ instance: Instance,
+ options: OptionsIndex,
address: usize,
count: usize,
handle: u32,
@@ -2215,7 +2217,7 @@ impl<T> StoreContextMut<'_, T> {
let mut dropped = false;
let produce = Box::new({
let producer = producer.clone();
- move |instance| {
+ move |_instance| {
let producer = producer.clone();
async move {
let (mut mine, mut buffer) = producer.lock().unwrap().take().unwrap();
@@ -2343,7 +2345,7 @@ impl<T> StoreContextMut<'_, T> {
*producer.lock().unwrap() = Some((mine, buffer));
if write_buffer {
- write(instance, token, id, producer.clone(), kind).await?;
+ write( token, id, producer.clone(), kind).await?;
}
Ok(if dropped {
@@ -2577,7 +2579,6 @@ impl<T> StoreContextMut<'_, T> {
}
async fn write<D: 'static, P: Send + 'static, T: func::Lower + 'static, B: WriteBuffer<T>>(
- instance: Option<Instance>,
token: StoreToken<D>,
id: TableId<TransmitState>,
pair: Arc<Mutex<Option<(P, B)>>>,
@@ -2606,6 +2607,7 @@ async fn write<D: 'static, P: Send + 'static, T: func::Lower + 'static, B: Write
address,
count,
handle,
+ instance,
} => {
let guest_offset = guest_offset.unwrap();
@@ -2622,8 +2624,8 @@ async fn write<D: 'static, P: Send + 'static, T: func::Lower + 'static, B: Write
move |mut store: StoreContextMut<D>| {
lower::<T, B, D>(
store.as_context_mut(),
- instance.unwrap(),
- &options,
+ instance,
+ options,
ty,
address + (T::SIZE32 * guest_offset),
count - guest_offset,
@@ -2679,6 +2681,7 @@ async fn write<D: 'static, P: Send + 'static, T: func::Lower + 'static, B: Write
address,
count,
handle,
+ instance,
};
anyhow::Ok(())
@@ -2850,15 +2853,15 @@ impl Instance {
mut store: StoreContextMut<T>,
flat_abi: Option<FlatAbi>,
write_ty: TransmitIndex,
- write_options: &Options,
+ write_options: OptionsIndex,
write_address: usize,
read_ty: TransmitIndex,
- read_options: &Options,
+ read_options: OptionsIndex,
read_address: usize,
count: usize,
rep: u32,
) -> Result<()> {
- let types = self.id().get(store.0).component().types().clone();
+ let types = self.id().get(store.0).component().types();
match (write_ty, read_ty) {
(TransmitIndex::Future(write_ty), TransmitIndex::Future(read_ty)) => {
assert_eq!(count, 1);
@@ -2866,14 +2869,15 @@ impl Instance {
let val = types[types[write_ty].ty]
.payload
.map(|ty| {
- let abi = types.canonical_abi(&ty);
+ let lift =
+ &mut LiftContext::new(store.0.store_opaque_mut(), write_options, self);
+
+ let abi = lift.types.canonical_abi(&ty);
// FIXME: needs to read an i64 for memory64
if write_address % usize::try_from(abi.align32)? != 0 {
bail!("write pointer not aligned");
}
- let lift =
- &mut LiftContext::new(store.0.store_opaque_mut(), write_options, self);
let bytes = lift
.memory()
.get(write_address..)
@@ -2887,8 +2891,8 @@ impl Instance {
.transpose()?;
if let Some(val) = val {
- let lower =
- &mut LowerContext::new(store.as_context_mut(), read_options, &types, self);
+ let lower = &mut LowerContext::new(store.as_context_mut(), read_options, self);
+ let types = lower.types;
let ty = types[types[read_ty].ty].payload.unwrap();
let ptr = func::validate_inbounds_dynamic(
types.canonical_abi(&ty),
@@ -2913,16 +2917,16 @@ impl Instance {
let store_opaque = store.0.store_opaque_mut();
{
- let src = write_options
- .memory(store_opaque)
+ let src = self
+ .options_memory(store_opaque, write_options)
.get(write_address..)
.and_then(|b| b.get(..length_in_bytes))
.ok_or_else(|| {
anyhow::anyhow!("write pointer out of bounds of memory")
})?
.as_ptr();
- let dst = read_options
- .memory_mut(store_opaque)
+ let dst = self
+ .options_memory_mut(store_opaque, read_options)
.get_mut(read_address..)
.and_then(|b| b.get_mut(..length_in_bytes))
.ok_or_else(|| {
@@ -2937,7 +2941,7 @@ impl Instance {
} else {
let store_opaque = store.0.store_opaque_mut();
let lift = &mut LiftContext::new(store_opaque, write_options, self);
- let ty = types[types[write_ty].ty].payload.unwrap();
+ let ty = lift.types[lift.types[write_ty].ty].payload.unwrap();
let abi = lift.types.canonical_abi(&ty);
let size = usize::try_from(abi.size32).unwrap();
if write_address % usize::try_from(abi.align32)? != 0 {
@@ -2956,9 +2960,8 @@ impl Instance {
let id = TableId::<TransmitHandle>::new(rep);
log::trace!("copy values {values:?} for {id:?}");
- let lower =
- &mut LowerContext::new(store.as_context_mut(), read_options, &types, self);
- let ty = types[types[read_ty].ty].payload.unwrap();
+ let lower = &mut LowerContext::new(store.as_context_mut(), read_options, self);
+ let ty = lower.types[lower.types[read_ty].ty].payload.unwrap();
let abi = lower.types.canonical_abi(&ty);
if read_address % usize::try_from(abi.align32)? != 0 {
bail!("read pointer not aligned");
@@ -2985,12 +2988,12 @@ impl Instance {
fn check_bounds(
self,
store: &StoreOpaque,
- options: &Options,
+ options: OptionsIndex,
ty: TransmitIndex,
address: usize,
count: usize,
) -> Result<()> {
- let types = self.id().get(store).component().types().clone();
+ let types = self.id().get(store).component().types();
let size = usize::try_from(
match ty {
TransmitIndex::Future(ty) => types[types[ty].ty]
@@ -3005,8 +3008,7 @@ impl Instance {
.unwrap();
if count > 0 && size > 0 {
- options
- .memory(store)
+ self.options_memory(store, options)
.get(address..)
.and_then(|b| b.get(..(size * count)))
.map(drop)
@@ -3029,8 +3031,7 @@ impl Instance {
) -> Result<ReturnCode> {
let address = usize::try_from(address).unwrap();
let count = usize::try_from(count).unwrap();
- let options = Options::new_index(store.0, self, options);
- self.check_bounds(store.0, &options, ty, address, count)?;
+ self.check_bounds(store.0, options, ty, address, count)?;
let (rep, state) = self.id().get_mut(store.0).get_mut_by_index(ty, handle)?;
let TransmitLocalState::Write { done } = *state else {
bail!(
@@ -3090,6 +3091,7 @@ impl Instance {
address: read_address,
count: read_count,
handle: read_handle,
+ instance: read_instance,
} => {
assert_eq!(flat_abi, read_flat_abi);
@@ -3130,10 +3132,10 @@ impl Instance {
store.as_context_mut(),
flat_abi,
ty,
- &options,
+ options,
address,
read_ty,
- &read_options,
+ read_options,
read_address,
count,
rep,
@@ -3171,6 +3173,7 @@ impl Instance {
address: read_address + (count * item_size),
count: read_count - count,
handle: read_handle,
+ instance: read_instance,
};
}
@@ -3216,7 +3219,7 @@ impl Instance {
}
};
- if result == ReturnCode::Blocked && !options.async_() {
+ if result == ReturnCode::Blocked && !self.options(store.0, options).async_ {
result = self.wait_for_write(store.0, transmit_handle)?;
}
@@ -3250,8 +3253,7 @@ impl Instance {
) -> Result<ReturnCode> {
let address = usize::try_from(address).unwrap();
let count = usize::try_from(count).unwrap();
- let options = Options::new_index(store.0, self, options);
- self.check_bounds(store.0, &options, ty, address, count)?;
+ self.check_bounds(store.0, options, ty, address, count)?;
let (rep, state) = self.id().get_mut(store.0).get_mut_by_index(ty, handle)?;
let TransmitLocalState::Read { done } = *state else {
bail!("invalid handle {handle}; expected `Read`; got {:?}", *state);
@@ -3295,6 +3297,7 @@ impl Instance {
address,
count,
handle,
+ instance: self,
};
Ok::<_, crate::Error>(())
};
@@ -3331,10 +3334,10 @@ impl Instance {
store.as_context_mut(),
flat_abi,
write_ty,
- &write_options,
+ write_options,
write_address,
ty,
- &options,
+ options,
address,
count,
rep,
@@ -3418,7 +3421,7 @@ impl Instance {
WriteState::Dropped => ReturnCode::Dropped(0),
};
- if result == ReturnCode::Blocked && !options.async_() {
+ if result == ReturnCode::Blocked && !self.options(store.0, options).async_ {
result = self.wait_for_read(store.0, transmit_handle)?;
}
@@ -3711,24 +3714,15 @@ impl Instance {
debug_msg_len: u32,
) -> Result<u32> {
self.id().get(store).check_may_leave(caller)?;
- let options = Options::new_index(store, self, options);
- let lift_ctx = &mut LiftContext::new(store, &options, self);
- // Read string from guest memory
- let address = usize::try_from(debug_msg_address)?;
- let len = usize::try_from(debug_msg_len)?;
- lift_ctx
- .memory()
- .get(address..)
- .and_then(|b| b.get(..len))
- .ok_or_else(|| anyhow::anyhow!("invalid debug message pointer: out of bounds"))?;
- let message = WasmStr::new(address, len, lift_ctx)?;
+ let lift_ctx = &mut LiftContext::new(store, options, self);
+ let debug_msg = String::linear_lift_from_flat(
+ lift_ctx,
+ InterfaceType::String,
+ &[ValRaw::u32(debug_msg_address), ValRaw::u32(debug_msg_len)],
+ )?;
// Create a new ErrorContext that is tracked along with other concurrent state
- let err_ctx = ErrorContextState {
- debug_msg: message
- .to_str_from_memory(options.memory(store))?
- .to_string(),
- };
+ let err_ctx = ErrorContextState { debug_msg };
let state = store.concurrent_state_mut();
let table_id = state.push(err_ctx)?;
let global_ref_count_idx =
@@ -3776,9 +3770,7 @@ impl Instance {
state.get_mut(TableId::<ErrorContextState>::new(handle_table_id_rep))?;
let debug_msg = debug_msg.clone();
- let options = Options::new_index(store.0, self, options);
- let types = self.id().get(store.0).component().types().clone();
- let lower_cx = &mut LowerContext::new(store, &options, &types, self);
+ let lower_cx = &mut LowerContext::new(store, options, self);
let debug_msg_address = usize::try_from(debug_msg_address)?;
// Lower the string into the component's memory
let offset = lower_cx
diff --git a/crates/wasmtime/src/runtime/component/func.rs b/crates/wasmtime/src/runtime/component/func.rs
index 9e46181ee7..366ffd7165 100644
--- a/crates/wasmtime/src/runtime/component/func.rs
+++ b/crates/wasmtime/src/runtime/component/func.rs
@@ -12,8 +12,8 @@ use anyhow::Context as _;
use core::mem::{self, MaybeUninit};
use core::ptr::NonNull;
use wasmtime_environ::component::{
- CanonicalOptions, ExportIndex, InterfaceType, MAX_FLAT_PARAMS, MAX_FLAT_RESULTS, TypeFuncIndex,
- TypeTuple,
+ CanonicalOptions, ExportIndex, InterfaceType, MAX_FLAT_PARAMS, MAX_FLAT_RESULTS, OptionsIndex,
+ TypeFuncIndex, TypeTuple,
};
#[cfg(feature = "component-model-async")]
@@ -533,14 +533,18 @@ impl Func {
pub(crate) fn abi_info<'a>(
&self,
store: &'a StoreOpaque,
- ) -> (Options, InstanceFlags, TypeFuncIndex, &'a CanonicalOptions) {
+ ) -> (
+ OptionsIndex,
+ InstanceFlags,
+ TypeFuncIndex,
+ &'a CanonicalOptions,
+ ) {
let vminstance = self.instance.id().get(store);
let component = vminstance.component();
let (ty, _def, options_index) = component.export_lifted_function(self.index);
let raw_options = &component.env_component().options[options_index];
- let options = Options::new_index(store, self.instance, options_index);
(
- options,
+ options_index,
vminstance.instance_flags(raw_options.instance),
ty,
raw_options,
@@ -909,8 +913,8 @@ impl Func {
may_enter: bool,
lower: impl FnOnce(&mut LowerContext<T>, InterfaceType) -> Result<()>,
) -> Result<()> {
- let types = self.instance.id().get(store.0).component().types().clone();
- let (options, mut flags, ty, _) = self.abi_info(store.0);
+ let (options_idx, mut flags, ty, options) = self.abi_info(store.0);
+ let async_ = options.async_;
// Test the "may enter" flag which is a "lock" on this instance.
// This is immediately set to `false` afterwards and note that
@@ -932,8 +936,9 @@ impl Func {
debug_assert!(flags.may_leave());
flags.set_may_leave(false);
}
- let mut cx = LowerContext::new(store.as_context_mut(), &options, &types, self.instance);
- let result = lower(&mut cx, InterfaceType::Tuple(types[ty].params));
+ let mut cx = LowerContext::new(store.as_context_mut(), options_idx, self.instance);
+ let param_ty = InterfaceType::Tuple(cx.types[ty].params);
+ let result = lower(&mut cx, param_ty);
unsafe { flags.set_may_leave(true) };
result?;
@@ -942,7 +947,7 @@ impl Func {
// post-return call being required as we're about to enter wasm and
// afterwards need a post-return.
unsafe {
- if may_enter && options.async_() {
+ if may_enter && async_ {
flags.set_may_enter(true);
} else {
flags.set_needs_post_return(true);
@@ -963,7 +968,7 @@ impl Func {
lift: impl FnOnce(&mut LiftContext, InterfaceType) -> Result<R>,
) -> Result<R> {
let (options, _flags, ty, _) = self.abi_info(store);
- let mut cx = LiftContext::new(store, &options, self.instance);
+ let mut cx = LiftContext::new(store, options, self.instance);
let ty = InterfaceType::Tuple(cx.types[ty].results);
lift(&mut cx, ty)
}
diff --git a/crates/wasmtime/src/runtime/component/func/host.rs b/crates/wasmtime/src/runtime/component/func/host.rs
index e8de25c601..a573342303 100644
--- a/crates/wasmtime/src/runtime/component/func/host.rs
+++ b/crates/wasmtime/src/runtime/component/func/host.rs
@@ -1,7 +1,7 @@
use crate::component::concurrent;
#[cfg(feature = "component-model-async")]
use crate::component::concurrent::{Accessor, Status};
-use crate::component::func::{LiftContext, LowerContext, Options};
+use crate::component::func::{LiftContext, LowerContext};
use crate::component::matching::InstanceType;
use crate::component::storage::slice_to_storage_mut;
use crate::component::types::ComponentFunc;
@@ -233,10 +233,10 @@ where
/// must be upheld. Generally that's done by ensuring this is only called from
/// the select few places it's intended to be called from.
unsafe fn call_host<T, Params, Return, F>(
- mut store: StoreContextMut<'_, T>,
+ store: StoreContextMut<'_, T>,
instance: Instance,
ty: TypeFuncIndex,
- options_idx: OptionsIndex,
+ options: OptionsIndex,
storage: &mut [MaybeUninit<ValRaw>],
closure: F,
) -> Result<()>
@@ -245,9 +245,10 @@ where
Params: Lift,
Return: Lower + 'static,
{
- let options = Options::new_index(store.0, instance, options_idx);
+ let (component, store) = instance.component_and_store_mut(store.0);
+ let mut store = StoreContextMut(store);
let vminstance = instance.id().get(store.0);
- let opts = &vminstance.component().env_component().options[options_idx];
+ let opts = &vminstance.component().env_component().options[options];
let async_ = opts.async_;
let caller_instance = opts.instance;
let mut flags = vminstance.instance_flags(caller_instance);
@@ -259,7 +260,7 @@ where
return Err(anyhow!(crate::Trap::CannotLeaveComponent));
}
- let types = vminstance.component().types().clone();
+ let types = component.types();
let ty = &types[ty];
let param_tys = InterfaceType::Tuple(ty.params);
let result_tys = InterfaceType::Tuple(ty.results);
@@ -271,15 +272,14 @@ where
// Lift the parameters, either from flat storage or from linear
// memory.
- let lift = &mut LiftContext::new(store.0.store_opaque_mut(), &options, instance);
+ let lift = &mut LiftContext::new(store.0.store_opaque_mut(), options, instance);
lift.enter_call();
let params = storage.lift_params(lift, param_tys)?;
// Load the return pointer, if present.
let retptr = match storage.async_retptr() {
Some(ptr) => {
- let mut lower =
- LowerContext::new(store.as_context_mut(), &options, &types, instance);
+ let mut lower = LowerContext::new(store.as_context_mut(), options, instance);
validate_inbounds::<Return>(lower.as_slice_mut(), ptr)?
}
// If there's no return pointer then `Return` should have an
@@ -295,12 +295,11 @@ where
let host_result = closure(store.as_context_mut(), params);
let mut lower_result = {
- let types = types.clone();
move |store: StoreContextMut<T>, ret: Return| {
unsafe {
flags.set_may_leave(false);
}
- let mut lower = LowerContext::new(store, &options, &types, instance);
+ let mut lower = LowerContext::new(store, options, instance);
ret.linear_lower_to_memory(&mut lower, result_tys, retptr)?;
unsafe {
flags.set_may_leave(true);
@@ -329,7 +328,7 @@ where
Status::Returned.pack(None)
};
- let mut lower = LowerContext::new(store, &options, &types, instance);
+ let mut lower = LowerContext::new(store, options, instance);
storage.lower_results(&mut lower, InterfaceType::U32, status)?;
}
#[cfg(not(feature = "component-model-async"))]
@@ -342,7 +341,7 @@ where
}
} else {
let mut storage = unsafe { Storage::<'_, Params, Return>::new_sync(storage) };
- let mut lift = LiftContext::new(store.0.store_opaque_mut(), &options, instance);
+ let mut lift = LiftContext::new(store.0.store_opaque_mut(), options, instance);
lift.enter_call();
let params = storage.lift_params(&mut lift, param_tys)?;
@@ -357,7 +356,7 @@ where
unsafe {
flags.set_may_leave(false);
}
- let mut lower = LowerContext::new(store, &options, &types, instance);
+ let mut lower = LowerContext::new(store, options, instance);
storage.lower_results(&mut lower, result_tys, ret)?;
unsafe {
flags.set_may_leave(true);
@@ -700,10 +699,10 @@ where
}
unsafe fn call_host_dynamic<T, F>(
- mut store: StoreContextMut<'_, T>,
+ store: StoreContextMut<'_, T>,
instance: Instance,
ty: TypeFuncIndex,
- options_idx: OptionsIndex,
+ options: OptionsIndex,
storage: &mut [MaybeUninit<ValRaw>],
closure: F,
) -> Result<()>
@@ -719,9 +718,10 @@ where
+ 'static,
T: 'static,
{
- let options = Options::new_index(store.0, instance, options_idx);
+ let (component, store) = instance.component_and_store_mut(store.0);
+ let mut store = StoreContextMut(store);
let vminstance = instance.id().get(store.0);
- let opts = &vminstance.component().env_component().options[options_idx];
+ let opts = &component.env_component().options[options];
let async_ = opts.async_;
let caller_instance = opts.instance;
let mut flags = vminstance.instance_flags(caller_instance);
@@ -733,13 +733,13 @@ where
return Err(anyhow!(crate::Trap::CannotLeaveComponent));
}
- let types = instance.id().get(store.0).component().types().clone();
+ let types = component.types();
let func_ty = &types[ty];
let param_tys = &types[func_ty.params];
let result_tys = &types[func_ty.results];
let mut params_and_results = Vec::new();
- let mut lift = &mut LiftContext::new(store.0.store_opaque_mut(), &options, instance);
+ let mut lift = &mut LiftContext::new(store.0.store_opaque_mut(), options, instance);
lift.enter_call();
let max_flat = if async_ {
MAX_FLAT_ASYNC_PARAMS
@@ -751,7 +751,7 @@ where
let ret_index = unsafe {
dynamic_params_load(
&mut lift,
- &types,
+ types,
storage,
param_tys,
&mut params_and_results,
@@ -770,29 +770,26 @@ where
0
} else {
let retptr = unsafe { storage[ret_index].assume_init() };
- let mut lower =
- LowerContext::new(store.as_context_mut(), &options, &types, instance);
+ let mut lower = LowerContext::new(store.as_context_mut(), options, instance);
validate_inbounds_dynamic(&result_tys.abi, lower.as_slice_mut(), &retptr)?
};
let future = closure(store.as_context_mut(), ty, params_and_results, result_start);
let task = instance.first_poll(store, future, caller_instance, {
- let types = types.clone();
let result_tys = func_ty.results;
move |store: StoreContextMut<T>, result_vals: Vec<Val>| {
- let result_tys = &types[result_tys];
- let result_vals = &result_vals[result_start..];
- assert_eq!(result_vals.len(), result_tys.types.len());
-
unsafe {
flags.set_may_leave(false);
}
- let mut lower = LowerContext::new(store, &options, &types, instance);
+ let mut lower = LowerContext::new(store, options, instance);
+ let result_tys = &lower.types[result_tys];
+ let result_vals = &result_vals[result_start..];
+ assert_eq!(result_vals.len(), result_tys.types.len());
let mut ptr = retptr;
for (val, ty) in result_vals.iter().zip(result_tys.types.iter()) {
- let offset = types.canonical_abi(ty).next_field32_size(&mut ptr);
+ let offset = lower.types.canonical_abi(ty).next_field32_size(&mut ptr);
val.store(&mut lower, *ty, offset)?;
}
@@ -830,7 +827,7 @@ where
flags.set_may_leave(false);
}
- let mut cx = LowerContext::new(store, &options, &types, instance);
+ let mut cx = LowerContext::new(store, options, instance);
if let Some(cnt) = result_tys.abi.flat_count(MAX_FLAT_RESULTS) {
let mut dst = storage[..cnt].iter_mut();
for (val, ty) in result_vals.iter().zip(result_tys.types.iter()) {
diff --git a/crates/wasmtime/src/runtime/component/func/options.rs b/crates/wasmtime/src/runtime/component/func/options.rs
index 1385439413..4c4ec2588c 100644
--- a/crates/wasmtime/src/runtime/component/func/options.rs
+++ b/crates/wasmtime/src/runtime/component/func/options.rs
@@ -1,207 +1,22 @@
+use crate::StoreContextMut;
use crate::component::concurrent::ConcurrentState;
use crate::component::matching::InstanceType;
use crate::component::resources::{HostResourceData, HostResourceIndex, HostResourceTables};
use crate::component::{Instance, ResourceType};
use crate::prelude::*;
+use crate::runtime::vm::VMFuncRef;
use crate::runtime::vm::component::{
CallContexts, ComponentInstance, HandleTable, InstanceFlags, ResourceTables,
};
-use crate::runtime::vm::{VMFuncRef, VMMemoryDefinition};
use crate::store::{StoreId, StoreOpaque};
-use crate::{FuncType, StoreContextMut};
use alloc::sync::Arc;
use core::pin::Pin;
use core::ptr::NonNull;
use wasmtime_environ::component::{
- CanonicalOptions, CanonicalOptionsDataModel, ComponentTypes, OptionsIndex, StringEncoding,
+ CanonicalOptions, CanonicalOptionsDataModel, ComponentTypes, OptionsIndex,
TypeResourceTableIndex,
};
-/// Runtime representation of canonical ABI options in the component model.
-///
-/// This structure packages up the runtime representation of each option from
-/// memories to reallocs to string encodings. Note that this is a "standalone"
-/// structure which has raw pointers internally. This allows it to be created
-/// out of thin air for a host function import, for example. The `store_id`
-/// field, however, is what is used to pair this set of options with a store
-/// reference to actually use the pointers.
-#[derive(Copy, Clone)]
-pub struct Options {
- /// The store from which this options originated.
- store_id: StoreId,
-
- /// An optional pointer for the memory that this set of options is referring
- /// to. This option is not required to be specified in the canonical ABI
- /// hence the `Option`.
- ///
- /// Note that this pointer cannot be safely dereferenced unless a store,
- /// verified with `self.store_id`, has the appropriate borrow available.
- memory: Option<NonNull<VMMemoryDefinition>>,
-
- /// Similar to `memory` but corresponds to the `canonical_abi_realloc`
- /// function.
- ///
- /// Safely using this pointer has the same restrictions as `memory` above.
- realloc: Option<NonNull<VMFuncRef>>,
-
- /// The encoding used for strings, if found.
- ///
- /// This defaults to utf-8 but can be changed if necessary.
- string_encoding: StringEncoding,
-
- /// Whether or not this the async option was set when lowering.
- async_: bool,
-
- #[cfg(feature = "component-model-async")]
- callback: Option<NonNull<VMFuncRef>>,
-}
-
-// The `Options` structure stores raw pointers but they're never used unless a
-// `Store` is available so this should be threadsafe and largely inherit the
-// thread-safety story of `Store<T>` itself.
-unsafe impl Send for Options {}
-unsafe impl Sync for Options {}
-
-impl Options {
- // FIXME(#4311): prevent a ctor where the memory is memory64
-
- /// Creates a new [`Options`] from the given [`OptionsIndex`] belonging to
- /// the specified [`Instance`]
- ///
- /// # Panics
- ///
- /// Panics if `instance` is not owned by `store` or if `index` is not valid
- /// for `instance`'s component.
- pub fn new_index(store: &StoreOpaque, instance: Instance, index: OptionsIndex) -> Options {
- let instance = instance.id().get(store);
- let CanonicalOptions {
- string_encoding,
- async_,
- callback,
- ref data_model,
- ..
- } = instance.component().env_component().options[index];
- let (memory, realloc) = match data_model {
- CanonicalOptionsDataModel::Gc { .. } => (None, None),
- CanonicalOptionsDataModel::LinearMemory(o) => (o.memory, o.realloc),
- };
- let memory = memory.map(|i| NonNull::new(instance.runtime_memory(i)).unwrap());
- let realloc = realloc.map(|i| instance.runtime_realloc(i));
- let callback = callback.map(|i| instance.runtime_callback(i));
- let _ = callback;
-
- Options {
- store_id: store.id(),
- memory,
- realloc,
- string_encoding,
- async_,
- #[cfg(feature = "component-model-async")]
- callback,
- }
- }
-
- fn realloc<'a, T>(
- &self,
- store: &'a mut StoreContextMut<'_, T>,
- realloc_ty: &FuncType,
- old: usize,
- old_size: usize,
- old_align: u32,
- new_size: usize,
- ) -> Result<(&'a mut [u8], usize)> {
- self.store_id.assert_belongs_to(store.0.id());
-
- let realloc = self.realloc.unwrap();
-
- let params = (
- u32::try_from(old)?,
- u32::try_from(old_size)?,
- old_align,
- u32::try_from(new_size)?,
- );
-
- type ReallocFunc = crate::TypedFunc<(u32, u32, u32, u32), u32>;
-
- // Invoke the wasm malloc function using its raw and statically known
- // signature.
- let result = unsafe { ReallocFunc::call_raw(store, realloc_ty, realloc, params)? };
-
- if result % old_align != 0 {
- bail!("realloc return: result not aligned");
- }
- let result = usize::try_from(result)?;
-
- let memory = self.memory_mut(store.0);
-
- let result_slice = match memory.get_mut(result..).and_then(|s| s.get_mut(..new_size)) {
- Some(end) => end,
- None => bail!("realloc return: beyond end of memory"),
- };
-
- Ok((result_slice, result))
- }
-
- /// Asserts that this function has an associated memory attached to it and
- /// then returns the slice of memory tied to the lifetime of the provided
- /// store.
- pub fn memory<'a>(&self, store: &'a StoreOpaque) -> &'a [u8] {
- self.store_id.assert_belongs_to(store.id());
-
- // The unsafety here is intended to be encapsulated by the two
- // preceding assertions. Namely we assert that the `store` is the same
- // as the original store of this `Options`, meaning that we safely have
- // either a shared reference or a mutable reference (as below) which
- // means it's safe to view the memory (aka it's not a different store
- // where our original store is on some other thread or something like
- // that).
- //
- // Additionally the memory itself is asserted to be present as memory
- // is an optional configuration in canonical ABI options.
- unsafe {
- let memory = self.memory.unwrap().as_ref();
- core::slice::from_raw_parts(memory.base.as_ptr(), memory.current_length())
- }
- }
-
- /// Same as above, just `_mut`
- pub fn memory_mut<'a>(&self, store: &'a mut StoreOpaque) -> &'a mut [u8] {
- self.store_id.assert_belongs_to(store.id());
-
- // See comments in `memory` about the unsafety
- unsafe {
- let memory = self.memory.unwrap().as_ref();
- core::slice::from_raw_parts_mut(memory.base.as_ptr(), memory.current_length())
- }
- }
-
- /// Returns the underlying encoding used for strings in this
- /// lifting/lowering.
- pub fn string_encoding(&self) -> StringEncoding {
- self.string_encoding
- }
-
- /// Returns the id of the store that this `Options` is connected to.
- pub fn store_id(&self) -> StoreId {
- self.store_id
- }
-
- /// Returns whether this lifting or lowering uses the async ABI.
- pub fn async_(&self) -> bool {
- self.async_
- }
-
- #[cfg(feature = "component-model-async")]
- pub(crate) fn callback(&self) -> Option<NonNull<VMFuncRef>> {
- self.callback
- }
-
- #[cfg(feature = "component-model-async")]
- pub(crate) fn memory_raw(&self) -> Option<NonNull<VMMemoryDefinition>> {
- self.memory
- }
-}
-
/// A helper structure which is a "package" of the context used during lowering
/// values into a component (or storing them into memory).
///
@@ -221,7 +36,7 @@ pub struct LowerContext<'a, T: 'static> {
/// canonical ABI. For example details like string encoding are contained
/// here along with which memory pointers are relative to or what the memory
/// allocation function is.
- pub options: &'a Options,
+ options: OptionsIndex,
/// Lowering happens within the context of a component instance and this
/// field stores the type information of that component instance. This is
@@ -241,8 +56,7 @@ impl<'a, T: 'static> LowerContext<'a, T> {
/// Creates a new lowering context from the specified parameters.
pub fn new(
store: StoreContextMut<'a, T>,
- options: &'a Options,
- types: &'a ComponentTypes,
+ options: OptionsIndex,
instance: Instance,
) -> LowerContext<'a, T> {
#[cfg(all(debug_assertions, feature = "component-model-async"))]
@@ -251,10 +65,11 @@ impl<'a, T: 'static> LowerContext<'a, T> {
// case we call the guest's realloc function.
store.0.with_blocking(|_, _| {});
}
+ let (component, store) = instance.component_and_store_mut(store.0);
LowerContext {
- store,
+ store: StoreContextMut(store),
options,
- types,
+ types: component.types(),
instance,
allow_realloc: true,
}
@@ -271,14 +86,14 @@ impl<'a, T: 'static> LowerContext<'a, T> {
#[cfg(feature = "component-model-async")]
pub(crate) fn new_without_realloc(
store: StoreContextMut<'a, T>,
- options: &'a Options,
- types: &'a ComponentTypes,
+ options: OptionsIndex,
instance: Instance,
) -> LowerContext<'a, T> {
+ let (component, store) = instance.component_and_store_mut(store.0);
LowerContext {
- store,
+ store: StoreContextMut(store),
options,
- types,
+ types: component.types(),
instance,
allow_realloc: false,
}
@@ -294,6 +109,11 @@ impl<'a, T: 'static> LowerContext<'a, T> {
self.instance.id().get_mut(self.store.0)
}
+ /// Returns the canonical options that are being used during lifting.
+ pub fn options(&self) -> &CanonicalOptions {
+ &self.instance().component().env_component().options[self.options]
+ }
+
/// Returns a view into memory as a mutable slice of bytes.
///
/// # Panics
@@ -301,7 +121,7 @@ impl<'a, T: 'static> LowerContext<'a, T> {
/// This will panic if memory has not been configured for this lowering
/// (e.g. it wasn't present during the specification of canonical options).
pub fn as_slice_mut(&mut self) -> &mut [u8] {
- self.options.memory_mut(self.store.0)
+ self.instance.options_memory_mut(self.store.0, self.options)
}
/// Invokes the memory allocation function (which is style after `realloc`)
@@ -320,17 +140,46 @@ impl<'a, T: 'static> LowerContext<'a, T> {
) -> Result<usize> {
assert!(self.allow_realloc);
- let realloc_func_ty = Arc::clone(self.instance().component().realloc_func_ty());
- self.options
- .realloc(
- &mut self.store,
- &realloc_func_ty,
- old,
- old_size,
- old_align,
- new_size,
- )
- .map(|(_, ptr)| ptr)
+ let (component, store) = self.instance.component_and_store_mut(self.store.0);
+ let instance = self.instance.id().get(store);
+ let options = &component.env_component().options[self.options];
+ let realloc_ty = component.realloc_func_ty();
+ let realloc = match options.data_model {
+ CanonicalOptionsDataModel::Gc {} => unreachable!(),
+ CanonicalOptionsDataModel::LinearMemory(m) => m.realloc.unwrap(),
+ };
+ let realloc = instance.runtime_realloc(realloc);
+
+ let params = (
+ u32::try_from(old)?,
+ u32::try_from(old_size)?,
+ old_align,
+ u32::try_from(new_size)?,
+ );
+
+ type ReallocFunc = crate::TypedFunc<(u32, u32, u32, u32), u32>;
+
+ // Invoke the wasm malloc function using its raw and statically known
+ // signature.
+ let result = unsafe {
+ ReallocFunc::call_raw(&mut StoreContextMut(store), &realloc_ty, realloc, params)?
+ };
+
+ if result % old_align != 0 {
+ bail!("realloc return: result not aligned");
+ }
+ let result = usize::try_from(result)?;
+
+ if self
+ .as_slice_mut()
+ .get_mut(result..)
+ .and_then(|s| s.get_mut(..new_size))
+ .is_none()
+ {
+ bail!("realloc return: beyond end of memory")
+ }
+
+ Ok(result)
}
/// Returns a fixed mutable slice of memory `N` bytes large starting at
@@ -463,13 +312,14 @@ impl<'a, T: 'static> LowerContext<'a, T> {
/// operations (or loading from memory).
#[doc(hidden)]
pub struct LiftContext<'a> {
+ store_id: StoreId,
/// Like lowering, lifting always has options configured.
- pub options: &'a Options,
+ options: OptionsIndex,
/// Instance type information, like with lowering.
pub types: &'a Arc<ComponentTypes>,
- memory: Option<&'a [u8]>,
+ memory: &'a [u8],
instance: Pin<&'a mut ComponentInstance>,
instance_handle: Instance,
@@ -492,23 +342,24 @@ impl<'a> LiftContext<'a> {
#[inline]
pub fn new(
store: &'a mut StoreOpaque,
- options: &'a Options,
+ options: OptionsIndex,
instance_handle: Instance,
) -> LiftContext<'a> {
+ let store_id = store.id();
// From `&mut StoreOpaque` provided the goal here is to project out
// three different disjoint fields owned by the store: memory,
// `CallContexts`, and `HandleTable`. There's no native API for that
// so it's hacked around a bit. This unsafe pointer cast could be fixed
// with more methods in more places, but it doesn't seem worth doing it
// at this time.
- let memory = options
- .memory
- .map(|_| options.memory(unsafe { &*(store as *const StoreOpaque) }));
+ let memory =
+ instance_handle.options_memory(unsafe { &*(store as *const StoreOpaque) }, options);
let (calls, host_table, host_resource_data, instance, concurrent_state) =
store.component_resource_state_with_instance_and_concurrent_state(instance_handle);
let (component, instance) = instance.component_and_self();
LiftContext {
+ store_id,
memory,
options,
types: component.types(),
@@ -521,6 +372,16 @@ impl<'a> LiftContext<'a> {
}
}
+ /// Returns the canonical options that are being used during lifting.
+ pub fn options(&self) -> &CanonicalOptions {
+ &self.instance.component().env_component().options[self.options]
+ }
+
+ /// Returns the `OptionsIndex` being used during lifting.
+ pub fn options_index(&self) -> OptionsIndex {
+ self.options
+ }
+
/// Returns the entire contents of linear memory for this set of lifting
/// options.
///
@@ -529,13 +390,13 @@ impl<'a> LiftContext<'a> {
/// This will panic if memory has not been configured for this lifting
/// operation.
pub fn memory(&self) -> &'a [u8] {
- self.memory.unwrap()
+ self.memory
}
/// Returns an identifier for the store from which this `LiftContext` was
/// created.
pub fn store_id(&self) -> StoreId {
- self.options.store_id
+ self.store_id
}
/// Returns the component instance that is being lifted from.
diff --git a/crates/wasmtime/src/runtime/component/func/typed.rs b/crates/wasmtime/src/runtime/component/func/typed.rs
index 8970c0c3d4..dceddd3285 100644
--- a/crates/wasmtime/src/runtime/component/func/typed.rs
+++ b/crates/wasmtime/src/runtime/component/func/typed.rs
@@ -1,5 +1,5 @@
use crate::component::Instance;
-use crate::component::func::{Func, LiftContext, LowerContext, Options};
+use crate::component::func::{Func, LiftContext, LowerContext};
use crate::component::matching::InstanceType;
use crate::component::storage::{storage_as_slice, storage_as_slice_mut};
use crate::prelude::*;
@@ -11,7 +11,8 @@ use core::marker;
use core::mem::{self, MaybeUninit};
use core::str;
use wasmtime_environ::component::{
- CanonicalAbiInfo, InterfaceType, MAX_FLAT_PARAMS, MAX_FLAT_RESULTS, StringEncoding, VariantInfo,
+ CanonicalAbiInfo, InterfaceType, MAX_FLAT_PARAMS, MAX_FLAT_RESULTS, OptionsIndex,
+ StringEncoding, VariantInfo,
};
#[cfg(feature = "component-model-async")]
@@ -995,12 +996,16 @@ macro_rules! forward_string_lifts {
unsafe impl Lift for $a {
#[inline]
fn linear_lift_from_flat(cx: &mut LiftContext<'_>, ty: InterfaceType, src: &Self::Lower) -> Result<Self> {
- Ok(<WasmStr as Lift>::linear_lift_from_flat(cx, ty, src)?.to_str_from_memory(cx.memory())?.into())
+ let s = <WasmStr as Lift>::linear_lift_from_flat(cx, ty, src)?;
+ let encoding = cx.options().string_encoding;
+ Ok(s.to_str_from_memory(encoding, cx.memory())?.into())
}
#[inline]
fn linear_lift_from_memory(cx: &mut LiftContext<'_>, ty: InterfaceType, bytes: &[u8]) -> Result<Self> {
- Ok(<WasmStr as Lift>::linear_lift_from_memory(cx, ty, bytes)?.to_str_from_memory(cx.memory())?.into())
+ let s = <WasmStr as Lift>::linear_lift_from_memory(cx, ty, bytes)?;
+ let encoding = cx.options().string_encoding;
+ Ok(s.to_str_from_memory(encoding, cx.memory())?.into())
}
}
)*)
@@ -1481,7 +1486,7 @@ fn lower_string<T>(cx: &mut LowerContext<'_, T>, string: &str) -> Result<(usize,
// to simd-accelerated helpers in the `encoding_rs` crate. This is ok though
// because we can fake that the host string was already stored in latin1
// format and follow that copy pattern instead.
- match cx.options.string_encoding() {
+ match cx.options().string_encoding {
// This corresponds to `store_string_copy` in the canonical ABI where
// the host's representation is utf-8 and the wasm module wants utf-8 so
// a copy is all that's needed (and the `realloc` can be precise for the
@@ -1614,12 +1619,13 @@ fn lower_string<T>(cx: &mut LowerContext<'_, T>, string: &str) -> Result<(usize,
pub struct WasmStr {
ptr: usize,
len: usize,
- options: Options,
+ options: OptionsIndex,
+ instance: Instance,
}
impl WasmStr {
pub(crate) fn new(ptr: usize, len: usize, cx: &mut LiftContext<'_>) -> Result<WasmStr> {
- let byte_len = match cx.options.string_encoding() {
+ let byte_len = match cx.options().string_encoding {
StringEncoding::Utf8 => Some(len),
StringEncoding::Utf16 => len.checked_mul(2),
StringEncoding::CompactUtf16 => {
@@ -1637,7 +1643,8 @@ impl WasmStr {
Ok(WasmStr {
ptr,
len,
- options: *cx.options,
+ options: cx.options_index(),
+ instance: cx.instance_handle(),
})
}
@@ -1667,12 +1674,17 @@ impl WasmStr {
store: impl Into<StoreContext<'a, T>>,
) -> Result<Cow<'a, str>> {
let store = store.into().0;
- let memory = self.options.memory(store);
- self.to_str_from_memory(memory)
+ let memory = self.instance.options_memory(store, self.options);
+ let encoding = self.instance.options(store, self.options).string_encoding;
+ self.to_str_from_memory(encoding, memory)
}
- pub(crate) fn to_str_from_memory<'a>(&self, memory: &'a [u8]) -> Result<Cow<'a, str>> {
- match self.options.string_encoding() {
+ pub(crate) fn to_str_from_memory<'a>(
+ &self,
+ encoding: StringEncoding,
+ memory: &'a [u8],
+ ) -> Result<Cow<'a, str>> {
+ match encoding {
StringEncoding::Utf8 => self.decode_utf8(memory),
StringEncoding::Utf16 => self.decode_utf16(memory, self.len),
StringEncoding::CompactUtf16 => {
@@ -1866,7 +1878,7 @@ where
pub struct WasmList<T> {
ptr: usize,
len: usize,
- options: Options,
+ options: OptionsIndex,
elem: InterfaceType,
instance: Instance,
_marker: marker::PhantomData<T>,
@@ -1892,7 +1904,7 @@ impl<T: Lift> WasmList<T> {
Ok(WasmList {
ptr,
len,
- options: *cx.options,
+ options: cx.options_index(),
elem,
instance: cx.instance_handle(),
_marker: marker::PhantomData,
@@ -1921,8 +1933,7 @@ impl<T: Lift> WasmList<T> {
// consumers should be validating through the iterator.
pub fn get(&self, mut store: impl AsContextMut, index: usize) -> Option<Result<T>> {
let store = store.as_context_mut().0;
- self.options.store_id().assert_belongs_to(store.id());
- let mut cx = LiftContext::new(store, &self.options, self.instance);
+ let mut cx = LiftContext::new(store, self.options, self.instance);
self.get_from_store(&mut cx, index)
}
@@ -1949,8 +1960,7 @@ impl<T: Lift> WasmList<T> {
store: impl Into<StoreContextMut<'a, U>>,
) -> impl ExactSizeIterator<Item = Result<T>> + 'a {
let store = store.into().0;
- self.options.store_id().assert_belongs_to(store.id());
- let mut cx = LiftContext::new(store, &self.options, self.instance);
+ let mut cx = LiftContext::new(store, self.options, self.instance);
(0..self.len).map(move |i| self.get_from_store(&mut cx, i).unwrap())
}
}
@@ -1976,7 +1986,7 @@ macro_rules! raw_wasm_list_accessors {
/// Panics if the `store` provided is not the one from which this
/// slice originated.
pub fn as_le_slice<'a, T: 'static>(&self, store: impl Into<StoreContext<'a, T>>) -> &'a [$i] {
- let memory = self.options.memory(store.into().0);
+ let memory = self.instance.options_memory(store.into().0, self.options);
self._as_le_slice(memory)
}
diff --git a/crates/wasmtime/src/runtime/component/instance.rs b/crates/wasmtime/src/runtime/component/instance.rs
index 4f67d39f65..737b40a55c 100644
--- a/crates/wasmtime/src/runtime/component/instance.rs
+++ b/crates/wasmtime/src/runtime/component/instance.rs
@@ -12,7 +12,7 @@ use crate::runtime::vm::component::{
CallContexts, ComponentInstance, ResourceTables, TypedResource, TypedResourceIndex,
};
use crate::runtime::vm::{self, VMFuncRef};
-use crate::store::StoreOpaque;
+use crate::store::{AsStoreOpaque, StoreOpaque};
use crate::{AsContext, AsContextMut, Engine, Module, StoreContextMut};
use alloc::sync::Arc;
use core::marker;
@@ -465,6 +465,122 @@ impl Instance {
pub(crate) fn lookup_vmdef(&self, store: &mut StoreOpaque, def: &CoreDef) -> vm::Export {
lookup_vmdef(store, self.id.instance(), def)
}
+
+ pub(crate) fn options<'a>(
+ &self,
+ store: &'a StoreOpaque,
+ options: OptionsIndex,
+ ) -> &'a CanonicalOptions {
+ &self.id.get(store).component().env_component().options[options]
+ }
+
+ fn options_memory_raw(
+ &self,
+ store: &StoreOpaque,
+ options: OptionsIndex,
+ ) -> Option<NonNull<vm::VMMemoryDefinition>> {
+ let instance = self.id.get(store);
+ let options = &instance.component().env_component().options[options];
+ let memory = match options.data_model {
+ CanonicalOptionsDataModel::Gc { .. } => return None,
+ CanonicalOptionsDataModel::LinearMemory(o) => match o.memory {
+ Some(m) => m,
+ None => return None,
+ },
+ };
+
+ Some(instance.runtime_memory(memory))
+ }
+
+ pub(crate) fn options_memory<'a>(
+ &self,
+ store: &'a StoreOpaque,
+ options: OptionsIndex,
+ ) -> &'a [u8] {
+ let memory = match self.options_memory_raw(store, options) {
+ Some(m) => m,
+ None => return &[],
+ };
+ // SAFETY: we're borrowing the entire `StoreOpaque` which owns the
+ // memory allocation to return the result of memory. That means that the
+ // lifetime connection here should be safe and the actual ptr/length are
+ // trusted parts of the runtime here.
+ unsafe {
+ let memory = memory.as_ref();
+ core::slice::from_raw_parts(memory.base.as_ptr(), memory.current_length())
+ }
+ }
+
+ pub(crate) fn options_memory_mut<'a>(
+ &self,
+ store: &'a mut StoreOpaque,
+ options: OptionsIndex,
+ ) -> &'a mut [u8] {
+ let memory = match self.options_memory_raw(store, options) {
+ Some(m) => m,
+ None => return &mut [],
+ };
+ // SAFETY: See `options_memory` comment above, and note that this is
+ // taking `&mut StoreOpaque` to thread the lifetime through instead.
+ unsafe {
+ let memory = memory.as_ref();
+ core::slice::from_raw_parts_mut(memory.base.as_ptr(), memory.current_length())
+ }
+ }
+
+ /// Helper function to simultaneously get a borrow to this instance's
+ /// component as well as the store that this component is contained within.
+ ///
+ /// Note that this function signature is not possible with safe Rust, so
+ /// this is using `unsafe` internally.
+ pub(crate) fn component_and_store_mut<'a, S>(
+ &self,
+ store: &'a mut S,
+ ) -> (&'a Component, &'a mut S)
+ where
+ S: AsStoreOpaque,
+ {
+ let store_opaque = store.as_store_opaque();
+ let instance = self.id.get_mut(store_opaque);
+ let component = instance.component();
+
+ // SAFETY: the goal of this function is to derive a pointer from
+ // `&mut S`, here `&Component`, and then return both so they can both be
+ // used at the same time. In general this is not safe operation since
+ // the original mutable pointer could be mutated or overwritten which
+ // would invalidate the derived pointer.
+ //
+ // In this case though we have a few guarantees which should make this
+ // safe:
+ //
+ // * Embedders never have the ability to overwrite a `StoreOpaque`. For
+ // example the closest thing of `StoreContextMut` wraps up the
+ // reference internally so it's inaccessible to the outside world.
+ // This means that while mutations can still happen it's not possible
+ // to overwrite a `StoreOpaque` directly.
+ //
+ // * Components are referred to by `vm::ComponentInstance` which holds a
+ // strong reference. All `ComponentInstance` structures are allocated
+ // within the store and unconditionally live as long as the entire
+ // store itself. This means that there's no worry of the rooting
+ // container going away or otherwise getting deallocated.
+ //
+ // * The `ComponentInstance` container has an invariant that after
+ // creation the component used to create it cannot be changed. This is
+ // enforced through `Pin<&mut ComponentInstance>` which disallows
+ // mutable access to the `component` field, instead only allowing
+ // read-only access.
+ //
+ // Putting all of this together it's not possible for a component,
+ // within a component instance, within a store, to be deallocated or mutated while
+ // a store is in use. Consequently it should be safe to simultaneously
+ // have a borrow to both at the same time, even if the store has a
+ // mutable borrow itself.
+ unsafe {
+ let component: *const Component = component;
+ (&*component, store)
+ }
+ }
}
/// Translates a `CoreDef`, a definition of a core wasm item, to an
diff --git a/crates/wasmtime/src/runtime/component/mod.rs b/crates/wasmtime/src/runtime/component/mod.rs
index 4f2bd48a61..f826468bfc 100644
--- a/crates/wasmtime/src/runtime/component/mod.rs
+++ b/crates/wasmtime/src/runtime/component/mod.rs
@@ -151,8 +151,8 @@ pub use wasm_wave;
#[doc(hidden)]
pub mod __internal {
pub use super::func::{
- ComponentVariant, LiftContext, LowerContext, Options, bad_type_info, format_flags,
- lower_payload, typecheck_enum, typecheck_flags, typecheck_record, typecheck_variant,
+ ComponentVariant, LiftContext, LowerContext, bad_type_info, format_flags, lower_payload,
+ typecheck_enum, typecheck_flags, typecheck_record, typecheck_variant,
};
pub use super::matching::InstanceType;
pub use crate::MaybeUninitExt;
diff --git a/crates/wasmtime/src/runtime/vm/component.rs b/crates/wasmtime/src/runtime/vm/component.rs
index 70a8eee279..94a0889c22 100644
--- a/crates/wasmtime/src/runtime/vm/component.rs
+++ b/crates/wasmtime/src/runtime/vm/component.rs
@@ -77,6 +77,12 @@ pub struct ComponentInstance {
// Otherwise the full guts of this component should only ever be used during
// the instantiation of this instance, meaning that after instantiation much
// of the component can be thrown away (theoretically).
+ //
+ // SAFETY: this field cannot be overwritten after an instance is created. It
+ // must contain this exact same value for the entire lifetime of this
+ // instance. This enables borrowing the component and this instance at the
+ // same time (instance mutably, component not). Additionally it enables
+ // borrowing a store mutably at the same time as a contained instance.
component: Component,
/// State of handles (e.g. resources, waitables, etc.) for this component.
@@ -337,11 +343,11 @@ impl ComponentInstance {
///
/// This can only be called after `idx` has been initialized at runtime
/// during the instantiation process of a component.
- pub fn runtime_memory(&self, idx: RuntimeMemoryIndex) -> *mut VMMemoryDefinition {
+ pub fn runtime_memory(&self, idx: RuntimeMemoryIndex) -> NonNull<VMMemoryDefinition> {
unsafe {
let ret = *self.vmctx_plus_offset::<VmPtr<_>>(self.offsets.runtime_memory(idx));
debug_assert!(ret.as_ptr() as usize != INVALID_PTR);
- ret.as_ptr()
+ ret.as_non_null()
}
}