A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Add memory range APIs, and borrow guest memory by range when possible. (#15183)
Chris Fallin0f36878cdf5a
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rsindex c4020b9a58..e3e50faab2 100644--- a/crates/wasmtime/src/runtime/component/concurrent.rs+++ b/crates/wasmtime/src/runtime/component/concurrent.rs@@ -4575,14 +4575,16 @@ impl Instance {}}};- let memory = self.options_memory_mut(store, params.options);let ptr = crate::component::func::validate_inbounds_dynamic(&CanonicalAbiInfo::POINTER_PAIR,- memory,+ self.options_memory(store, params.options),&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());+ let memory = self+ .options_memory_range_mut(store, params.options, ptr, 8)+ .unwrap();+ memory[0..4].copy_from_slice(&handle.to_le_bytes());+ memory[4..8].copy_from_slice(&result.to_le_bytes());Ok(ordinal)}diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rsindex d325defad7..c1d5f78460 100644--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs@@ -262,9 +262,9 @@ fn lower<T: func::Lower + Send + 'static, B: WriteBuffer<T>, U: 'static>(bail!("read pointer not aligned");}lower- .as_slice_mut()- .get_mut(address..)- .and_then(|b| b.get_mut(..T::SIZE32 * count))+ .as_slice()+ .get(address..)+ .and_then(|b| b.get(..T::SIZE32 * count)).ok_or_else(|| crate::format_err!("read pointer out of bounds of memory"))?;T::linear_store_list_to_memory(lower, ty, address, &buffer.remaining()[..count])?;@@ -497,10 +497,12 @@ impl<D: 'static> DirectDestination<'_, D> {bail_bug!("expected WriteState::HostReady")};- let memory = instance- .options_memory_mut(self.store.0, options)- .get_mut(address + guest_offset.as_usize()..)- .and_then(|b| b.get_mut(..(count.as_usize() - guest_offset.as_usize())));+ let memory = instance.options_memory_range_mut(+ self.store.0,+ options,+ address + guest_offset.as_usize(),+ count.as_usize() - guest_offset.as_usize(),+ );match memory {Some(memory) => Ok(memory),None => bail_bug!("guest buffer unexpectedly out of bounds"),@@ -3464,7 +3466,7 @@ impl Instance {&mut LowerContext::new(store.as_context_mut(), read_options, read_instance);let ptr = func::validate_inbounds_dynamic(read_abi,- lower.as_slice_mut(),+ lower.as_slice(),&ValRaw::u32(read_address.try_into()?),)?;let ty = match read_payload_ty {@@ -3514,8 +3516,14 @@ impl Instance {let src = write_instance.options_memory(store_opaque, write_options)[write_address..][..write_length_in_bytes].as_ptr();- let dst = read_instance.options_memory_mut(store_opaque, read_options)- [read_address..][..read_length_in_bytes]+ let dst = read_instance+ .options_memory_range_mut(+ store_opaque,+ read_options,+ read_address,+ read_length_in_bytes,+ )+ .unwrap().as_mut_ptr();// SAFETY: Both `src` and `dst` have been validated@@ -4461,7 +4469,7 @@ impl Instance {// memory, the ptr+length. This'll need to be updated when `memory64`// comes along. (FIXME(#4311))let offset = lower_cx- .as_slice_mut()+ .as_slice().get(debug_msg_address..).and_then(|b| b.get(..8)).map(|_| debug_msg_address)diff --git a/crates/wasmtime/src/runtime/component/func/host.rs b/crates/wasmtime/src/runtime/component/func/host.rsindex 7856067c90..184f5a70b6 100644--- a/crates/wasmtime/src/runtime/component/func/host.rs+++ b/crates/wasmtime/src/runtime/component/func/host.rs@@ -472,7 +472,7 @@ wherelet ptr = unsafe { rest[0].assume_init_ref() };Destination::Memory(validate_inbounds_dynamic(&result_tys.abi,- lower.as_slice_mut(),+ lower.as_slice(),ptr,)?)};@@ -515,12 +515,12 @@ where// Load/validate the return pointer, if present.let retptr = if !lift.types[fty.results].types.is_empty() {- let mut lower = LowerContext::new(store.as_context_mut(), options, instance);+ let lower = LowerContext::new(store.as_context_mut(), options, instance);// SAFETY: see `load_params` below about how the return pointer// should be safe to use.let ptr = unsafe { rest[0].assume_init_ref() };let result_tys = &lower.types[fty.results];- validate_inbounds_dynamic(&result_tys.abi, lower.as_slice_mut(), ptr)?+ validate_inbounds_dynamic(&result_tys.abi, lower.as_slice(), ptr)?} else {// If there's no return pointer then `R` should have an// empty flat representation. In this situation pretend the returndiff --git a/crates/wasmtime/src/runtime/component/func/options.rs b/crates/wasmtime/src/runtime/component/func/options.rsindex 3b6c8b1f14..6822df3026 100644--- a/crates/wasmtime/src/runtime/component/func/options.rs+++ b/crates/wasmtime/src/runtime/component/func/options.rs@@ -121,6 +121,16 @@ impl<'a, T: 'static> LowerContext<'a, T> {&self.instance().component().env_component().options[self.options]}+ /// Returns a view into memory as a slice of bytes.+ ///+ /// # Panics+ ///+ /// 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(crate) fn as_slice(&self) -> &[u8] {+ self.instance.options_memory(self.store.0, self.options)+ }+/// Returns a view into memory as a mutable slice of bytes.////// # Panics@@ -131,6 +141,22 @@ impl<'a, T: 'static> LowerContext<'a, T> {self.instance.options_memory_mut(self.store.0, self.options)}+ /// Returns the `len` bytes of memory at `offset`.+ ///+ /// Like `as_slice_mut`, but conveys the additional information+ /// about the range of offsets that will actually be+ /// mutated. Useful to the runtime if memory dirtiness is being+ /// tracked; prefer this API whenever possible.+ ///+ /// # Panics+ ///+ /// Panics if the range is out of bounds.+ pub(crate) fn slice_range_mut(&mut self, offset: usize, len: usize) -> &mut [u8] {+ self.instance+ .options_memory_range_mut(self.store.0, self.options, offset, len)+ .expect("out-of-bounds lowering")+ }+/// Invokes the memory allocation function (which is style after `realloc`)/// with the specified parameters.///@@ -203,9 +229,9 @@ impl<'a, T: 'static> LowerContext<'a, T> {let result = usize::try_from(result)?;if self- .as_slice_mut()- .get_mut(result..)- .and_then(|s| s.get_mut(..new_size))+ .as_slice()+ .get(result..)+ .and_then(|s| s.get(..new_size)).is_none(){bail!("realloc return: beyond end of memory")@@ -243,7 +269,7 @@ impl<'a, T: 'static> LowerContext<'a, T> {// For now I figure we can leave in this bounds check and if it becomes// an issue we can optimize further later, probably with judicious use// of `unsafe`.- self.as_slice_mut()[offset..].first_chunk_mut().unwrap()+ self.slice_range_mut(offset, N).try_into().unwrap()}/// Lowers an `own` resource into the guest, converting the `rep` specifieddiff --git a/crates/wasmtime/src/runtime/component/func/typed.rs b/crates/wasmtime/src/runtime/component/func/typed.rsindex 7957e1fbbc..f6766dde57 100644--- a/crates/wasmtime/src/runtime/component/func/typed.rs+++ b/crates/wasmtime/src/runtime/component/func/typed.rs@@ -963,7 +963,7 @@ macro_rules! integers {// `align_to_mut` which is not safe in general but is safe in// our specific case as all `u8` patterns are valid `Self`// patterns since `Self` is an integral type.- let dst = &mut cx.as_slice_mut()[offset..][..items.len() * Self::SIZE32];+ let dst = cx.slice_range_mut(offset, items.len() * Self::SIZE32);let (before, middle, end) = unsafe { dst.align_to_mut::<Self>() };assert!(before.is_empty() && end.is_empty());assert_eq!(middle.len(), items.len());@@ -1086,7 +1086,7 @@ macro_rules! floats {// This should all have already been verified in terms of// alignment and sizing meaning that these assertions here are// not truly necessary but are instead double-checks.- let dst = &mut cx.as_slice_mut()[offset..][..items.len() * Self::SIZE32];+ let dst = cx.slice_range_mut(offset, items.len() * Self::SIZE32);assert!(dst.as_ptr().cast::<Self>().is_aligned());// And with all that out of the way perform the copying loop.@@ -1395,7 +1395,8 @@ fn lower_string<T>(cx: &mut LowerContext<'_, T>, string: &str) -> Result<(usize,);}let ptr = cx.realloc(0, 0, 1, string.len())?;- cx.as_slice_mut()[ptr..][..string.len()].copy_from_slice(string.as_bytes());+ cx.slice_range_mut(ptr, string.len())+ .copy_from_slice(string.as_bytes());Ok((ptr, string.len()))}@@ -1412,7 +1413,7 @@ fn lower_string<T>(cx: &mut LowerContext<'_, T>, string: &str) -> Result<(usize,}let mut ptr = cx.realloc(0, 0, 2, size)?;let mut copied = 0;- let bytes = &mut cx.as_slice_mut()[ptr..][..size];+ let bytes = cx.slice_range_mut(ptr, size);for (u, bytes) in string.encode_utf16().zip(bytes.chunks_mut(2)) {let u_bytes = u.to_le_bytes();bytes[0] = u_bytes[0];@@ -1430,7 +1431,7 @@ fn lower_string<T>(cx: &mut LowerContext<'_, T>, string: &str) -> Result<(usize,let bytes = string.as_bytes();let mut iter = string.char_indices();let mut ptr = cx.realloc(0, 0, 2, bytes.len())?;- let mut dst = &mut cx.as_slice_mut()[ptr..][..bytes.len()];+ let mut dst = cx.slice_range_mut(ptr, bytes.len());let mut result = 0;while let Some((i, ch)) = iter.next() {// Test if this `char` fits into the latin1 encoding.@@ -1450,7 +1451,7 @@ fn lower_string<T>(cx: &mut LowerContext<'_, T>, string: &str) -> Result<(usize,bail!("byte length too large");}ptr = cx.realloc(ptr, bytes.len(), 2, worst_case)?;- dst = &mut cx.as_slice_mut()[ptr..][..worst_case];+ dst = cx.slice_range_mut(ptr, worst_case);// Previously encoded latin1 bytes are inflated to their 16-bit// size for utf16diff --git a/crates/wasmtime/src/runtime/component/instance.rs b/crates/wasmtime/src/runtime/component/instance.rsindex 9d7bdebfb3..18cd382d38 100644--- a/crates/wasmtime/src/runtime/component/instance.rs+++ b/crates/wasmtime/src/runtime/component/instance.rs@@ -520,6 +520,18 @@ impl Instance {}}+ /// The `len` bytes at `offset` of the options' memory, if in bounds.+ pub(crate) fn options_memory_range_mut<'a>(+ &self,+ store: &'a mut StoreOpaque,+ options: OptionsIndex,+ offset: usize,+ len: usize,+ ) -> Option<&'a mut [u8]> {+ let end = offset.checked_add(len)?;+ self.options_memory_mut(store, options).get_mut(offset..end)+ }+/// Helper function to simultaneously get a borrow to this instance's/// component as well as the store that this component is contained within.///diff --git a/crates/wasmtime/src/runtime/memory.rs b/crates/wasmtime/src/runtime/memory.rsindex 360fcb0c43..02ec8b1523 100644--- a/crates/wasmtime/src/runtime/memory.rs+++ b/crates/wasmtime/src/runtime/memory.rs@@ -10,6 +10,7 @@ use alloc::sync::Arc;use core::any::Any;use core::cell::UnsafeCell;use core::fmt;+use core::ops::Range;use core::slice;use core::time::Duration;use wasmtime_environ::{DefinedMemoryIndex, MemoryTunables};@@ -497,6 +498,85 @@ impl Memory {}}+ /// Returns `range` of this memory as a native Rust slice, or an error if+ /// it is out of bounds.+ ///+ /// Note that this method will consider the entire store context provided as+ /// borrowed for the duration of the lifetime of the returned slice.+ ///+ /// # Panics+ ///+ /// Panics if this memory doesn't belong to `store`.+ pub fn data_range<'a, T: 'static>(+ &self,+ store: impl Into<StoreContext<'a, T>>,+ range: Range<usize>,+ ) -> Result<&'a [u8], MemoryAccessError> {+ self.data(store)+ .get(range)+ .ok_or(MemoryAccessError { _private: () })+ }++ /// Returns `range` of this memory as a native Rust mutable slice, or an+ /// error if it is out of bounds.+ ///+ /// Unlike [`Memory::data_mut`], which hands out all of memory,+ /// only this range is borrowed for modification, so that Wasmtime+ /// knows precisely what the host may modify. Prefer this method+ /// over `data_mut` plus a sub-slice operation: the additional+ /// semantic information about written memory may be useful to+ /// Wasmtime for efficiency reasons in some cases.+ ///+ /// Note that this method will consider the entire store context provided as+ /// borrowed for the duration of the lifetime of the returned slice. See+ /// [`Memory::data_ranges_and_store_data_mut`] to access multiple ranges+ /// while also borrowing the store's data.+ ///+ /// # Panics+ ///+ /// Panics if this memory doesn't belong to `store`.+ pub fn data_range_mut<'a, T: 'static>(+ &self,+ store: impl Into<StoreContextMut<'a, T>>,+ range: Range<usize>,+ ) -> Result<&'a mut [u8], MemoryAccessError> {+ let store = store.into();+ MemoryRanges {+ memory: *self,+ store: store.0,+ }+ .into_range_mut(range)+ }++ /// Like [`Memory::data_and_store_mut`], but returns a [`MemoryRanges`]+ /// through which ranges of memory are read and modified, rather than one+ /// slice of all of memory, alongside the store's data (the `T` in+ /// [`Store<T>`](crate::Store)).+ ///+ /// This is useful for host code that works with the store's `T` while+ /// accessing several parts of memory, such as the bindings of a WASI+ /// implementation: as with [`Memory::data_range_mut`], only the ranges+ /// borrowed for modification are considered modified.+ ///+ /// # Panics+ ///+ /// Panics if this memory doesn't belong to `store`.+ pub fn data_ranges_and_store_data_mut<'a, T: 'static>(+ &self,+ store: impl Into<StoreContextMut<'a, T>>,+ ) -> (MemoryRanges<'a>, &'a mut T) {+ let store = store.into();+ assert!(self.comes_from_same_store(store.0));+ let (data, opaque) = store.0.data_and_opaque_mut();+ (+ MemoryRanges {+ memory: *self,+ store: opaque,+ },+ data,+ )+ }+/// Returns the base pointer, in the host's address space, that the memory/// is located at.///@@ -1638,3 +1718,69 @@ mod tests {Ok(())}}++/// Access to ranges of a [`Memory`], from+/// [`Memory::data_ranges_and_store_data_mut`].+///+/// Ranges are read with [`MemoryRanges::data_range`] (or all of memory with+/// [`MemoryRanges::data`]) and modified with+/// [`MemoryRanges::data_range_mut`], which considers only the range it+/// returns modified.+pub struct MemoryRanges<'a> {+ memory: Memory,+ store: &'a mut StoreOpaque,+}++impl<'a> MemoryRanges<'a> {+ /// The length of the memory, in bytes.+ pub fn len(&self) -> usize {+ self.memory.internal_data_size(self.store)+ }++ /// Whether the memory is empty.+ pub fn is_empty(&self) -> bool {+ self.len() == 0+ }++ /// All of the memory, for reading.+ pub fn data(&self) -> &[u8] {+ let definition = self.store[self.memory.instance].memory(self.memory.index);+ // SAFETY: the store owns this non-shared memory, and this borrow of+ // `self` excludes modification through it.+ unsafe { slice::from_raw_parts(definition.base.as_ptr(), definition.current_length()) }+ }++ /// `range` of the memory, for reading, or an error if it is out of+ /// bounds.+ pub fn data_range(&self, range: Range<usize>) -> Result<&[u8], MemoryAccessError> {+ self.data()+ .get(range)+ .ok_or(MemoryAccessError { _private: () })+ }++ /// `range` of the memory, for modification, or an error if it is out of+ /// bounds. Only this range is considered modified.+ pub fn data_range_mut(&mut self, range: Range<usize>) -> Result<&mut [u8], MemoryAccessError> {+ MemoryRanges {+ memory: self.memory,+ store: &mut *self.store,+ }+ .into_range_mut(range)+ }++ fn into_range_mut(self, range: Range<usize>) -> Result<&'a mut [u8], MemoryAccessError> {+ let len = self.len();+ if range.start > range.end || range.end > len {+ return Err(MemoryAccessError { _private: () });+ }+ let definition = self.store[self.memory.instance].memory(self.memory.index);+ // SAFETY: the range is in bounds, and the store, which owns this+ // non-shared memory, is borrowed for the slice's lifetime.+ unsafe {+ Ok(slice::from_raw_parts_mut(+ definition.base.as_ptr().add(range.start),+ range.len(),+ ))+ }+ }+}diff --git a/crates/wasmtime/src/runtime/store.rs b/crates/wasmtime/src/runtime/store.rsindex 592eb0e2d6..785c354bfe 100644--- a/crates/wasmtime/src/runtime/store.rs+++ b/crates/wasmtime/src/runtime/store.rs@@ -1415,6 +1415,14 @@ impl<T> StoreInner<T> {(data, limiter, &mut self.inner)}+ /// Borrows the store's `T` and the rest of the store, which are+ /// disjoint, mutably at the same time.+ #[inline]+ pub(crate) fn data_and_opaque_mut(&mut self) -> (&mut T, &mut StoreOpaque) {+ let (data, _limiter, opaque) = self.data_limiter_and_opaque();+ (data, opaque)+ }+#[inline]fn data_mut(&mut self) -> &mut T {self.data_limiter_and_opaque().0diff --git a/crates/wiggle/generate/src/wasmtime.rs b/crates/wiggle/generate/src/wasmtime.rsindex bea34fb3aa..c14da9c605 100644--- a/crates/wiggle/generate/src/wasmtime.rs+++ b/crates/wiggle/generate/src/wasmtime.rs@@ -119,10 +119,12 @@ fn generate_func(let fuel = wiggle::wasmtime_crate::AsContextMut::as_context_mut(&mut caller).hostcall_fuel();let (mut mem, ctx) = match &export {Some(wiggle::wasmtime_crate::Extern::Memory(m)) => {- let (mem, ctx) = m.data_and_store_mut(&mut caller);+ // Access memory by range, so that only what the host+ // writes is considered modified.+ let (mem, ctx) = m.data_ranges_and_store_data_mut(&mut caller);let ctx = get_cx(ctx);ctx.set_hostcall_fuel(fuel);- (wiggle::GuestMemory::Unshared(mem), ctx)+ (wiggle::GuestMemory::Ranges(mem), ctx)}Some(wiggle::wasmtime_crate::Extern::SharedMemory(m)) => {let ctx = get_cx(caller.data_mut());diff --git a/crates/wiggle/src/guest_type.rs b/crates/wiggle/src/guest_type.rsindex 19d76a042b..d149ad41e4 100644--- a/crates/wiggle/src/guest_type.rs+++ b/crates/wiggle/src/guest_type.rs@@ -97,7 +97,7 @@ macro_rules! integer_primitives {// See `read` above for various checks here.let val = val.to_le();let offset = ptr.offset();- let host_ptr = mem.validate_size_align::<Self>(offset, 1)?;+ let host_ptr = mem.validate_size_align_mut::<Self>(offset, 1)?;let host_ptr = &host_ptr[0];let atomic_value_ref: &$ty_atomic =unsafe { &*(host_ptr.get().cast::<$ty_atomic>()) };diff --git a/crates/wiggle/src/lib.rs b/crates/wiggle/src/lib.rsindex 1888a1ff21..5e4772de13 100644--- a/crates/wiggle/src/lib.rs+++ b/crates/wiggle/src/lib.rs@@ -43,6 +43,11 @@ pub mod wasmtime_crate {pub enum GuestMemory<'a> {Unshared(&'a mut [u8]),Shared(&'a [UnsafeCell<u8>]),+ /// An unshared Wasmtime memory accessed by range, so that only the ranges+ /// written are considered modified (see+ /// `wasmtime::Memory::data_ranges_and_store_data_mut`).+ #[cfg(feature = "wasmtime")]+ Ranges(wasmtime::MemoryRanges<'a>),}// manual impls are needed because of the `UnsafeCell` in the `Shared` branch@@ -95,11 +100,11 @@ impl<'a> GuestMemory<'a> {/// not valid to read from.pub fn as_cow(&self, ptr: GuestPtr<[u8]>) -> Result<Cow<'_, [u8]>, GuestError> {match self {- GuestMemory::Unshared(_) => match self.as_slice(ptr)? {+ GuestMemory::Shared(_) => Ok(Cow::Owned(self.to_vec(ptr)?)),+ _ => match self.as_slice(ptr)? {Some(slice) => Ok(Cow::Borrowed(slice)),None => unreachable!(),},- GuestMemory::Shared(_) => Ok(Cow::Owned(self.to_vec(ptr)?)),}}@@ -133,6 +138,8 @@ impl<'a> GuestMemory<'a> {match self {GuestMemory::Unshared(slice) => Ok(Some(&slice[range])),GuestMemory::Shared(_) => Ok(None),+ #[cfg(feature = "wasmtime")]+ GuestMemory::Ranges(memory) => Ok(Some(&memory.data()[range])),}}@@ -154,6 +161,10 @@ impl<'a> GuestMemory<'a> {match self {GuestMemory::Unshared(slice) => Ok(Some(&mut slice[range])),GuestMemory::Shared(_) => Ok(None),+ #[cfg(feature = "wasmtime")]+ GuestMemory::Ranges(memory) => Ok(Some(+ memory.data_range_mut(range).expect("range was validated"),+ )),}}@@ -208,7 +219,7 @@ impl<'a> GuestMemory<'a> {return Ok(());}- let guest = self.validate_size_align::<T>(ptr.pointer.0, ptr.pointer.1)?;+ let guest = self.validate_size_align_mut::<T>(ptr.pointer.0, ptr.pointer.1)?;// SAFETY: in the shared memory case, we copy and accept that// the guest data may be concurrently modified. TODO: audit that@@ -251,9 +262,62 @@ impl<'a> GuestMemory<'a> {unsafe { &*(s as *const [u8] as *const [UnsafeCell<u8>]) }}GuestMemory::Shared(s) => s,+ #[cfg(feature = "wasmtime")]+ GuestMemory::Ranges(memory) => {+ let s = memory.data();+ unsafe { &*(s as *const [u8] as *const [UnsafeCell<u8>]) }+ }};- let memory = &cells[range.clone()];+ Self::align_cells(&cells[range.clone()], range)+ }++ /// Like `validate_size_align`, for writing: with+ /// `GuestMemory::Ranges`, only the validated range is borrowed for+ /// modification.+ pub(crate) fn validate_size_align_mut<T>(+ &mut self,+ offset: u32,+ len: u32,+ ) -> Result<&[UnsafeCell<T>], GuestError>+ where+ T: GuestTypeTransparent,+ {+ match self {+ #[cfg(feature = "wasmtime")]+ GuestMemory::Ranges(memory) => {+ let range = {+ let len = len+ .checked_mul(T::guest_size())+ .ok_or(GuestError::PtrOverflow)?;+ let region = Region { start: offset, len };+ let start = usize::try_from(offset)?;+ let end = start+ .checked_add(usize::try_from(len)?)+ .ok_or(GuestError::PtrOverflow)?;+ if end > memory.len() {+ return Err(GuestError::PtrOutOfBounds(region));+ }+ start..end+ };+ let s: &mut [u8] = memory+ .data_range_mut(range.clone())+ .expect("range was validated");+ let cells = unsafe { &*(s as *const [u8] as *const [UnsafeCell<u8>]) };+ Self::align_cells(cells, range)+ }+ _ => self.validate_size_align(offset, len),+ }+ }+ /// Reinterprets `memory`, which holds `range` of guest memory, as `T`s,+ /// failing if it is misaligned.+ fn align_cells<T>(+ memory: &[UnsafeCell<u8>],+ range: Range<usize>,+ ) -> Result<&[UnsafeCell<T>], GuestError>+ where+ T: GuestTypeTransparent,+ {// ... and then align it to `T`, failing if either the head or tail slices// are nonzero in length. This `unsafe` here is from the standard library// and should be ok since the input slice is `UnsafeCell<u8>` and the output@@ -289,6 +353,8 @@ impl<'a> GuestMemory<'a> {let oob = match self {GuestMemory::Unshared(b) => b.get(range.clone()).is_none(),GuestMemory::Shared(b) => b.get(range.clone()).is_none(),+ #[cfg(feature = "wasmtime")]+ GuestMemory::Ranges(memory) => range.end > memory.len(),};if oob {Err(GuestError::PtrOutOfBounds(region))@@ -299,10 +365,7 @@ impl<'a> GuestMemory<'a> {/// Returns whether this is a shared memory or not.pub fn is_shared_memory(&self) -> bool {- match self {- GuestMemory::Shared(_) => true,- GuestMemory::Unshared(_) => false,- }+ matches!(self, GuestMemory::Shared(_))}}diff --git a/tests/all/memory.rs b/tests/all/memory.rsindex 2ab911a9f1..54a1348a43 100644--- a/tests/all/memory.rs+++ b/tests/all/memory.rs@@ -986,3 +986,65 @@ fn atomic_wait_massive_timeout() -> Result<()> {Ok(())}++#[test]+fn data_ranges() -> Result<()> {+ let engine = Engine::default();+ let mut store = Store::new(&engine, 5_u32);+ let memory = Memory::new(&mut store, MemoryType::new(1, None)?)?;++ memory+ .data_range_mut(&mut store, 10..13)?+ .copy_from_slice(b"abc");+ assert_eq!(memory.data_range(&store, 10..13)?, b"abc");+ assert_eq!(memory.data_range(&store, 0..0)?, b"");+ assert_eq!(memory.data_range(&store, 65536..65536)?, b"");+ assert!(memory.data_range(&store, 65535..65537).is_err());+ assert!(memory.data_range_mut(&mut store, 70000..70001).is_err());+ #[allow(clippy::reversed_empty_ranges, reason = "testing invalid ranges")]+ {+ assert!(memory.data_range(&store, 5..4).is_err());+ assert!(memory.data_range_mut(&mut store, 5..4).is_err());+ }++ // Ranges can be accessed while also borrowing the store's data.+ let (mut ranges, data) = memory.data_ranges_and_store_data_mut(&mut store);+ *data += 1;+ assert_eq!(ranges.len(), 65536);+ assert_eq!(&ranges.data()[10..13], b"abc");+ ranges.data_range_mut(11..12)?[0] = b'X';+ assert_eq!(ranges.data_range(10..13)?, b"aXc");+ assert!(ranges.data_range_mut(65536..65537).is_err());+ assert_eq!(*store.data(), 6);+ assert_eq!(&memory.data(&store)[10..13], b"aXc");+ Ok(())+}++/// A memory and a store that it does not belong to.+fn memory_and_other_store() -> (Memory, Store<()>) {+ let engine = Engine::default();+ let mut store = Store::new(&engine, ());+ let memory = Memory::new(&mut store, MemoryType::new(1, None).unwrap()).unwrap();+ (memory, Store::new(&engine, ()))+}++#[test]+#[should_panic = "wrong store"]+fn data_range_wrong_store() {+ let (memory, other) = memory_and_other_store();+ let _ = memory.data_range(&other, 0..1);+}++#[test]+#[should_panic = "wrong store"]+fn data_range_mut_wrong_store() {+ let (memory, mut other) = memory_and_other_store();+ let _ = memory.data_range_mut(&mut other, 0..1);+}++#[test]+#[should_panic = "comes_from_same_store"]+fn data_ranges_and_store_data_mut_wrong_store() {+ let (memory, mut other) = memory_and_other_store();+ let _ = memory.data_ranges_and_store_data_mut(&mut other);+}