Skip to content

Commit

Permalink
address newly added overflow cases in test_wast_address
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun committed Nov 2, 2021
1 parent 07556f4 commit 37e1358
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions crates/vm/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub enum Trap {
NoMoreInstruction,
HostFunctionError(Box<dyn std::error::Error + Send + Sync>),
MemoryAddrOverflow {
base: i32,
base: u32,
offset: u64,
},
}
Expand Down Expand Up @@ -799,22 +799,14 @@ impl Executor {
Ok(store.memory(mem_addr))
}

fn mem_addr(base: i32, offset: u64, memory64: bool) -> ExecResult<u64> {
fn mem_addr(base: u32, offset: u64, memory64: bool) -> ExecResult<u64> {
let addr = if memory64 {
if base.is_negative() {
offset.checked_sub(base.wrapping_abs() as u64)
} else {
offset.checked_add(base as u64)
}
offset.checked_add(base as u64)
} else {
let offset: u32 = offset
.try_into()
.map_err(|_| Trap::MemoryAddrOverflow { base, offset })?;
let addr = if base.is_negative() {
offset.checked_sub(base.wrapping_abs() as u32)
} else {
offset.checked_add(base as u32)
};
let addr = offset.checked_add(base as u32);
addr.map(|v| v as u64)
};
if let Some(addr) = addr {
Expand All @@ -836,6 +828,7 @@ impl Executor {
) -> ExecResult<Signal> {
let val: T = self.pop_as()?;
let base_addr: i32 = self.pop_as()?;
let base_addr: u32 = u32::from_le_bytes(base_addr.to_le_bytes());
let addr = Self::mem_addr(base_addr, offset, config.features.memory64)? as usize;
let mut buf: Vec<u8> = std::iter::repeat(0)
.take(std::mem::size_of::<T>())
Expand All @@ -858,6 +851,7 @@ impl Executor {
) -> ExecResult<Signal> {
let val: T = self.pop_as()?;
let base_addr: i32 = self.pop_as()?;
let base_addr: u32 = u32::from_le_bytes(base_addr.to_le_bytes());
let addr = Self::mem_addr(base_addr, offset, config.features.memory64)? as usize;
let mut buf: Vec<u8> = std::iter::repeat(0)
.take(std::mem::size_of::<T>())
Expand All @@ -877,6 +871,7 @@ impl Executor {
T: Into<Value>,
{
let base_addr: i32 = self.pop_as()?;
let base_addr: u32 = u32::from_le_bytes(base_addr.to_le_bytes());
let addr = Self::mem_addr(base_addr, offset, config.features.memory64)? as usize;
let result: T = self
.memory(store)?
Expand All @@ -894,6 +889,7 @@ impl Executor {
config: &Config
) -> ExecResult<Signal> {
let base_addr: i32 = self.pop_as()?;
let base_addr: u32 = u32::from_le_bytes(base_addr.to_le_bytes());
let addr = Self::mem_addr(base_addr, offset, config.features.memory64)? as usize;

let result: T = self
Expand Down

0 comments on commit 37e1358

Please sign in to comment.