Skip to content

Commit

Permalink
fix: Addition overflows
Browse files Browse the repository at this point in the history
  • Loading branch information
xxuejie committed Dec 15, 2023
1 parent 768347f commit d09434a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
13 changes: 8 additions & 5 deletions src/machine/asm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fn check_memory_writable(
let page = page + 1;
if page as usize >= machine.memory_pages() {
return Err(Error::MemOutOfBound(
addr + size as u64,
addr.wrapping_add(size as u64),
OutOfBoundKind::Memory,
));
} else {
Expand Down Expand Up @@ -167,7 +167,7 @@ fn check_memory_executable(
let page = page + 1;
if page as usize >= machine.memory_pages() {
return Err(Error::MemOutOfBound(
addr + size as u64,
addr.wrapping_add(size as u64),
OutOfBoundKind::Memory,
));
} else {
Expand Down Expand Up @@ -197,7 +197,7 @@ fn check_memory_inited(
let page = page + 1;
if page as usize >= machine.memory_pages() {
return Err(Error::MemOutOfBound(
addr + size as u64,
addr.wrapping_add(size as u64),
OutOfBoundKind::Memory,
));
} else {
Expand Down Expand Up @@ -383,14 +383,17 @@ impl Memory for Box<AsmCoreMachine> {
return Err(Error::MemPageUnalignedAccess(addr));
}
if round_page_up(size) != size {
return Err(Error::MemPageUnalignedAccess(addr + size));
return Err(Error::MemPageUnalignedAccess(addr.wrapping_add(size)));
}

if addr > self.memory_size() as u64 {
return Err(Error::MemOutOfBound(addr, OutOfBoundKind::Memory));
}
if size > self.memory_size() as u64 || addr + size > self.memory_size() as u64 {
return Err(Error::MemOutOfBound(addr + size, OutOfBoundKind::Memory));
return Err(Error::MemOutOfBound(
addr.wrapping_add(size),
OutOfBoundKind::Memory,
));
}
if offset_from_addr > size {
return Err(Error::MemOutOfBound(
Expand Down
7 changes: 5 additions & 2 deletions src/memory/wxorx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ impl<M: Memory> Memory for WXorXMemory<M> {
return Err(Error::MemPageUnalignedAccess(addr));
}
if round_page_up(size) != size {
return Err(Error::MemPageUnalignedAccess(addr + size));
return Err(Error::MemPageUnalignedAccess(addr.wrapping_add(size)));
}

if addr > self.memory_size() as u64 {
return Err(Error::MemOutOfBound(addr, OutOfBoundKind::Memory));
}
if size > self.memory_size() as u64 || addr + size > self.memory_size() as u64 {
return Err(Error::MemOutOfBound(addr + size, OutOfBoundKind::Memory));
return Err(Error::MemOutOfBound(
addr.wrapping_add(size),
OutOfBoundKind::Memory,
));
}
if offset_from_addr > size {
return Err(Error::MemOutOfBound(
Expand Down
6 changes: 4 additions & 2 deletions src/snapshot2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ impl<I: Clone + PartialEq, D: DataSource<I>> Snapshot2Context<I, D> {
}
let data = self.data_source().load_data(id, *offset, *length)?;
if data.len() as u64 % PAGE_SIZE != 0 {
return Err(Error::MemPageUnalignedAccess(address * data.len() as u64));
return Err(Error::MemPageUnalignedAccess(
address.wrapping_add(data.len() as u64),
));
}
machine.memory_mut().store_bytes(*address, &data)?;
for i in 0..(data.len() as u64 / PAGE_SIZE) {
Expand All @@ -85,7 +87,7 @@ impl<I: Clone + PartialEq, D: DataSource<I>> Snapshot2Context<I, D> {
}
if content.len() as u64 % PAGE_SIZE != 0 {
return Err(Error::MemPageUnalignedAccess(
address + content.len() as u64,
address.wrapping_add(content.len() as u64),
));
}
machine.memory_mut().store_bytes(*address, content)?;
Expand Down

0 comments on commit d09434a

Please sign in to comment.