Skip to content

Commit

Permalink
Make ByteBuffer::new infallible
Browse files Browse the repository at this point in the history
  • Loading branch information
ark0f committed Jul 11, 2023
1 parent b21d376 commit d19c946
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 34 deletions.
17 changes: 3 additions & 14 deletions crates/wasmi/src/memory/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
use alloc::{vec, vec::Vec};
use core::{fmt, fmt::Display};

/// Dummy error for fallible `Vec`-based virtual memory operations.
#[derive(Debug)]
pub struct VirtualMemoryError {}

impl Display for VirtualMemoryError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "encountered failure while operating with virtual memory")
}
}

/// A `Vec`-based byte buffer implementation.
///
Expand All @@ -30,10 +19,10 @@ impl ByteBuffer {
///
/// - If the initial length is 0.
/// - If the initial length exceeds the maximum supported limit.
pub fn new(initial_len: usize) -> Result<Self, VirtualMemoryError> {
Ok(Self {
pub fn new(initial_len: usize) -> Self {
Self {
bytes: vec![0x00_u8; initial_len],
})
}
}

/// Grows the byte buffer to the given `new_size`.
Expand Down
12 changes: 5 additions & 7 deletions crates/wasmi/src/memory/buffer_vmem.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use super::MemoryError;
use core::fmt::Debug;
use wasmi_core::VirtualMemory;

pub use wasmi_core::VirtualMemoryError;

/// A virtual memory based byte buffer implementation.
///
/// # Note
Expand Down Expand Up @@ -37,12 +34,13 @@ impl ByteBuffer {
///
/// - If the initial length is 0.
/// - If the initial length exceeds the maximum supported limit.
pub fn new(initial_len: usize) -> Result<Self, MemoryError> {
let bytes = VirtualMemory::new(Self::ALLOCATION_SIZE)?;
Ok(Self {
pub fn new(initial_len: usize) -> Self {
let bytes = VirtualMemory::new(Self::ALLOCATION_SIZE)
.unwrap_or_else(|err| unreachable!("Failed to allocate memory: {err}"));
Self {
bytes,
len: initial_len,
})
}
}

/// Grows the byte buffer by the given delta.
Expand Down
10 changes: 0 additions & 10 deletions crates/wasmi/src/memory/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::MemoryType;
use crate::memory::VirtualMemoryError;
use core::{fmt, fmt::Display};

/// An error that may occur upon operating with virtual or linear memory.
Expand All @@ -14,8 +13,6 @@ pub enum MemoryError {
OutOfBoundsAccess,
/// Tried to create an invalid linear memory type.
InvalidMemoryType,
/// A generic virtual memory error.
Vmem(VirtualMemoryError),
/// Occurs when `ty` is not a subtype of `other`.
InvalidSubtype {
/// The [`MemoryType`] which is not a subtype of `other`.
Expand All @@ -40,16 +37,9 @@ impl Display for MemoryError {
Self::InvalidMemoryType => {
write!(f, "tried to create an invalid virtual memory type")
}
Self::Vmem(error) => Display::fmt(error, f),
Self::InvalidSubtype { ty, other } => {
write!(f, "memory type {ty:?} is not a subtype of {other:?}",)
}
}
}
}

impl From<VirtualMemoryError> for MemoryError {
fn from(error: VirtualMemoryError) -> Self {
Self::Vmem(error)
}
}
4 changes: 1 addition & 3 deletions crates/wasmi/src/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ use super::{AsContext, AsContextMut, StoreContext, StoreContextMut, Stored};
use wasmi_arena::ArenaIndex;
use wasmi_core::Pages;

pub use byte_buffer::VirtualMemoryError;

/// A raw index to a linear memory entity.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct MemoryIdx(u32);
Expand Down Expand Up @@ -142,7 +140,7 @@ impl MemoryEntity {
.to_bytes()
.ok_or(MemoryError::OutOfBoundsAllocation)?;
let memory = Self {
bytes: ByteBuffer::new(initial_len)?,
bytes: ByteBuffer::new(initial_len),
memory_type,
current_pages: initial_pages,
};
Expand Down

0 comments on commit d19c946

Please sign in to comment.