From 48392b9553e93a5470c2c755c02825f30d99abec Mon Sep 17 00:00:00 2001 From: Mica White Date: Sat, 31 Oct 2026 11:31:09 -0400 Subject: Fix merge --- crates/tinywasm/src/store/memory/mod.rs | 331 +------------------------------- 1 file changed, 6 insertions(+), 325 deletions(-) (limited to 'crates') diff --git a/crates/tinywasm/src/store/memory/mod.rs b/crates/tinywasm/src/store/memory/mod.rs index 6219c60..f7dfcd9 100644 --- a/crates/tinywasm/src/store/memory/mod.rs +++ b/crates/tinywasm/src/store/memory/mod.rs @@ -1,338 +1,19 @@ use crate::interpreter::Value128; mod instance; +#[cfg(feature = "std")] +mod shared; mod vec; pub(crate) use instance::MemoryInstance; +#[cfg(feature = "std")] +pub use shared::{MemoryShared, MemorySharedGuard}; pub(crate) use vec::VecMemory; /// Internal storage for a linear memory. pub(crate) type MemoryStorage = VecMemory; -<<<<<<< HEAD - /// Returns true if the memory is empty. - fn is_empty(&self) -> bool { - self.len() == 0 - } - - /// Grows the memory to `new_len` bytes. - /// - /// The runtime only calls this with lengths that are exact multiples of the Wasm page size for - /// the owning memory. - fn grow_to(&mut self, new_len: usize) -> core::result::Result<(), crate::Trap>; - - /// Reads up to `dst.len()` bytes starting at `addr` and returns the number of bytes read. - /// - /// Backends may return fewer bytes than requested even when more data is available. This lets - /// non-contiguous backends stop at a natural boundary such as the end of a chunk. - fn read(&self, addr: usize, dst: &mut [u8]) -> usize; - - /// Writes up to `src.len()` bytes starting at `addr` and returns the number of bytes written. - /// - /// Backends may return fewer bytes than requested even when more space is available. This lets - /// non-contiguous backends stop at a natural boundary such as the end of a chunk. - fn write(&mut self, addr: usize, src: &[u8]) -> usize; - - /// Writes all bytes in `src` starting at `addr`, or returns `None` if any byte could not be written. - fn write_all(&mut self, addr: usize, src: &[u8]) -> Option<()> { - let Some(end) = addr.checked_add(src.len()) else { - cold_path(); - return None; - }; - - if end > self.len() { - cold_path(); - return None; - } - - let mut offset = 0; - while offset < src.len() { - let written = self.write(addr + offset, &src[offset..]); - if written == 0 { - cold_path(); - return None; - } - offset += written; - } - - Some(()) - } - - /// Fills the range `[addr, addr + len)` with `val`. - fn fill(&mut self, addr: usize, len: usize, val: u8) -> Option<()> { - let end = addr.checked_add(len)?; - if end > self.len() { - return None; - } - - let mut offset = 0; - while offset < len { - let chunk_len = min(len - offset, 1024); - let chunk = vec![val; chunk_len]; - self.write_all(addr + offset, &chunk)?; - offset += chunk_len; - } - - Some(()) - } - - /// Copies `len` bytes from `src` to `dst` within the same memory. - fn copy_within(&mut self, dst: usize, src: usize, len: usize) -> Option<()> { - let src_end = src.checked_add(len)?; - let dst_end = dst.checked_add(len)?; - if src_end > self.len() || dst_end > self.len() { - return None; - } - - if len == 0 || dst == src { - return Some(()); - } - - // If the source and destination ranges are disjoint, we can copy forward without a temporary buffer. - if dst < src || dst >= src_end { - let mut offset = 0; - while offset < len { - let chunk_len = min(len - offset, 1024); - let mut chunk = vec![0; chunk_len]; - self.read_exact(src + offset, &mut chunk)?; - self.write_all(dst + offset, &chunk)?; - offset += chunk_len; - } - } else { - // Otherwise, we need to copy backward to avoid overwriting the source data before it's read. - let mut offset = len; - while offset > 0 { - let chunk_len = min(offset, 1024); - offset -= chunk_len; - let mut chunk = vec![0; chunk_len]; - self.read_exact(src + offset, &mut chunk)?; - self.write_all(dst + offset, &chunk)?; - } - } - - Some(()) - } - - /// Reads exactly `dst.len()` bytes starting at `addr`. - fn read_exact(&self, addr: usize, dst: &mut [u8]) -> Option<()> { - let Some(end) = addr.checked_add(dst.len()) else { - cold_path(); - return None; - }; - - if end > self.len() { - cold_path(); - return None; - } - - let mut offset = 0; - while offset < dst.len() { - let read = self.read(addr + offset, &mut dst[offset..]); - if read == 0 { - cold_path(); - return None; - } - offset += read; - } - - Some(()) - } - - /// Reads `len` bytes starting at `addr` into a newly allocated buffer. - fn read_vec(&self, addr: usize, len: usize) -> Option> { - let end = addr.checked_add(len)?; - if end > self.len() { - return None; - } - - let mut data = vec![0; len]; - self.read_exact(addr, &mut data)?; - Some(data) - } - - /// Reads exactly 1 byte at `addr`. - fn read_8(&self, addr: usize) -> core::result::Result<[u8; 1], crate::Trap> { - let mut bytes = [0; 1]; - self.read_exact(addr, &mut bytes).ok_or_else(|| { - cold_path(); - memory_oob(addr, 1, self.len()) - })?; - Ok(bytes) - } - - /// Reads exactly 2 bytes at `addr`. - fn read_16(&self, addr: usize) -> core::result::Result<[u8; 2], crate::Trap> { - let mut bytes = [0; 2]; - self.read_exact(addr, &mut bytes).ok_or_else(|| { - cold_path(); - memory_oob(addr, 2, self.len()) - })?; - Ok(bytes) - } - - /// Reads exactly 4 bytes at `addr`. - fn read_32(&self, addr: usize) -> core::result::Result<[u8; 4], crate::Trap> { - let mut bytes = [0; 4]; - self.read_exact(addr, &mut bytes).ok_or_else(|| { - cold_path(); - memory_oob(addr, 4, self.len()) - })?; - Ok(bytes) - } - - /// Reads exactly 8 bytes at `addr`. - fn read_64(&self, addr: usize) -> core::result::Result<[u8; 8], crate::Trap> { - let mut bytes = [0; 8]; - self.read_exact(addr, &mut bytes).ok_or_else(|| { - cold_path(); - memory_oob(addr, 8, self.len()) - })?; - Ok(bytes) - } - - /// Reads exactly 16 bytes at `addr`. - fn read_128(&self, addr: usize) -> core::result::Result<[u8; 16], crate::Trap> { - let mut bytes = [0; 16]; - self.read_exact(addr, &mut bytes).ok_or_else(|| { - cold_path(); - memory_oob(addr, 16, self.len()) - })?; - Ok(bytes) - } - - /// Writes exactly 1 byte at `addr`. - fn write_8(&mut self, addr: usize, bytes: &[u8]) -> core::result::Result<(), crate::Trap> { - self.write_all(addr, bytes).ok_or_else(|| { - cold_path(); - memory_oob(addr, 1, self.len()) - }) - } - - /// Writes exactly 2 bytes at `addr`. - fn write_16(&mut self, addr: usize, bytes: &[u8]) -> core::result::Result<(), crate::Trap> { - self.write_all(addr, bytes).ok_or_else(|| { - cold_path(); - memory_oob(addr, 2, self.len()) - }) - } - - /// Writes exactly 4 bytes at `addr`. - fn write_32(&mut self, addr: usize, bytes: &[u8]) -> core::result::Result<(), crate::Trap> { - self.write_all(addr, bytes).ok_or_else(|| { - cold_path(); - memory_oob(addr, 4, self.len()) - }) - } - - /// Writes exactly 8 bytes at `addr`. - fn write_64(&mut self, addr: usize, bytes: &[u8]) -> core::result::Result<(), crate::Trap> { - self.write_all(addr, bytes).ok_or_else(|| { - cold_path(); - memory_oob(addr, 8, self.len()) - }) - } - - /// Writes exactly 16 bytes at `addr`. - fn write_128(&mut self, addr: usize, bytes: &[u8]) -> core::result::Result<(), crate::Trap> { - self.write_all(addr, bytes).ok_or_else(|| { - cold_path(); - memory_oob(addr, 16, self.len()) - }) - } -} - -type MemoryFactory = dyn Fn(MemoryType) -> Result> + Send + Sync; - -/// Configures how runtime memory instances are created. -#[derive(Clone, Default)] -pub struct MemoryBackend(MemoryBackendInner); - -#[derive(Clone, Default)] -enum MemoryBackendInner { - #[default] - Vec, - Paged { - chunk_size: usize, - }, - Custom(Arc), -} - -impl MemoryBackend { - /// Uses a contiguous [`VecMemory`] for each memory instance. - /// - /// This is usually the fastest option for reads and writes, but large grows can be expensive - /// because they may reallocate and copy the entire buffer. - pub const fn vec() -> Self { - Self(MemoryBackendInner::Vec) - } - - /// Uses sparse chunked storage for each memory instance. - /// - /// `chunk_size` is the backend chunk size in bytes. It must be a non-zero power - /// of two and is independent from the Wasm page size. - /// - /// This generally makes growth cheaper than [`Self::vec`], but read and write operations do a - /// little more work and may be slightly slower. - pub fn paged(chunk_size: usize) -> Self { - assert!(chunk_size.is_power_of_two(), "chunk_size must be a non-zero power of two"); - Self(MemoryBackendInner::Paged { chunk_size }) - } - - /// Uses a custom factory to create memory instances. - pub fn custom(factory: F) -> Self - where - F: Fn(MemoryType) -> Result + Send + Sync + 'static, - M: LinearMemory + Send + Sync + 'static, - { - Self(MemoryBackendInner::Custom(Arc::new(move |ty| { - let memory = factory(ty)?; - Ok(Box::new(memory) as Box) - }))) - } - - pub(crate) fn create(&self, ty: MemoryType, initial_len: usize) -> Result { - let storage = match &self.0 { - MemoryBackendInner::Vec => { - Box::new(VecMemory::try_new(initial_len).map_err(Error::Trap)?) as Box - } - MemoryBackendInner::Paged { chunk_size } => { - Box::new(PagedMemory::try_new(initial_len, *chunk_size).map_err(Error::Trap)?) - as Box - } - MemoryBackendInner::Custom(factory) => factory(ty)?, - }; - - if storage.len() < initial_len { - return Err(Error::Other(format!( - "memory backend returned {} bytes for a memory that requires at least {initial_len}", - storage.len() - ))); - } - - Ok(storage) - } - - pub(crate) fn create_lazy(&self, ty: MemoryType, initial_len: usize) -> Result { - Ok(Box::new(LazyLinearMemory::new_with_initial_len(ty, initial_len, self.clone()))) - } -} - -#[cfg(feature = "debug")] -impl core::fmt::Debug for MemoryBackend { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - match &self.0 { - MemoryBackendInner::Vec => f.debug_tuple("MemoryBackend::Vec").finish(), - MemoryBackendInner::Paged { chunk_size } => { - f.debug_struct("MemoryBackend::Paged").field("chunk_size", chunk_size).finish() - } - MemoryBackendInner::Custom(_) => f.debug_tuple("MemoryBackend::Custom").finish(), - } - } -} - -pub(crate) type MemoryStorage = Box; - -/// A trait for types that can be converted to and from static byte arrays +/// A trait for types that can be converted to and from static byte arrays. pub(crate) trait MemValue: Copy + Default { /// Store a value in memory. fn to_mem_bytes(self) -> [u8; N]; @@ -393,6 +74,6 @@ impl MemValue<16> for Value128 { } } -const fn memory_oob(offset: usize, len: usize, max: usize) -> crate::Trap { +pub(super) const fn memory_oob(offset: usize, len: usize, max: usize) -> crate::Trap { crate::Trap::MemoryOutOfBounds { offset, len, max } } -- cgit v1.3.1