Skip to content

Commit

Permalink
Tighten rules around empty memory ranges, and fix meq access checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Dentosal committed Jun 1, 2023
1 parent 0c39471 commit 6101ed0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions fuel-vm/src/interpreter/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ impl VmMemory {
pub fn try_copy_within(&mut self, dst_range: &MemoryRange, src_range: &MemoryRange) -> Result<(), RuntimeError> {
assert!(dst_range.len() == src_range.len());

if dst_range.len() == 0 {
return Ok(());
}

if dst_range.overlap_with(src_range).is_some() {
return Err(PanicReason::MemoryWriteOverlap.into());
}
Expand Down
2 changes: 1 addition & 1 deletion fuel-vm/src/interpreter/memory/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ where
return Err(PanicReason::MemoryAccessSize.into());
}

let eq = self.memory.read(&range0) == self.memory.read(&range1);
let eq = self.mem_read_range(&range0)? == self.mem_read_range(&range1)?;

let (_, mut w) = split_registers(&mut self.registers);
w[wrk] = eq as Word;
Expand Down
24 changes: 24 additions & 0 deletions fuel-vm/src/interpreter/memory/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ impl MemoryRange {

/// Checks if a range falls fully within another range
pub fn contains_range(&self, inner: &Self) -> bool {
if inner.len() == 0 && self.len() == 0{
return self.start == inner.start;
}

self.contains(&inner.start) && inner.end <= self.end
}

Expand Down Expand Up @@ -140,12 +144,26 @@ pub mod tests {

#[test]
fn test_contains_range() {
let empty0 = MemoryRange::try_new(0, 0).unwrap();
let empty1 = MemoryRange::try_new(1, 0).unwrap();

assert!(empty0.contains_range(&empty0));
assert!(empty1.contains_range(&empty1));
assert!(!empty1.contains_range(&empty0));
assert!(!empty0.contains_range(&empty1));

let a = MemoryRange::try_new(0, 10).unwrap();
let b = MemoryRange::try_new(2, 4).unwrap();
let c = MemoryRange::try_new(8, 4).unwrap();
let d = MemoryRange::try_new(10, 10).unwrap();
let e = MemoryRange::try_new(20, 10).unwrap();

assert!(a.contains_range(&a));
assert!(b.contains_range(&b));
assert!(c.contains_range(&c));
assert!(d.contains_range(&d));
assert!(e.contains_range(&e));

assert!(a.contains_range(&b));
assert!(!a.contains_range(&c));
assert!(!a.contains_range(&d));
Expand All @@ -154,13 +172,19 @@ pub mod tests {

#[test]
fn test_overlap() {
let empty0 = MemoryRange::try_new(0, 0).unwrap();
let empty1 = MemoryRange::try_new(1, 0).unwrap();

let a = MemoryRange::try_new(0, 10).unwrap();
let b = MemoryRange::try_new(5, 10).unwrap();
let c = MemoryRange::try_new(10, 10).unwrap();

assert_eq!(a.overlap_with(&b), Some(MemoryRange::try_new(5, 5).unwrap()));
assert_eq!(a.overlap_with(&c), None);
assert_eq!(b.overlap_with(&c), Some(MemoryRange::try_new(10, 5).unwrap()));

assert!(empty0.overlap_with(&empty0).is_none());
assert!(empty0.overlap_with(&empty0).is_none());
}

#[test]
Expand Down

0 comments on commit 6101ed0

Please sign in to comment.