Skip to content

Commit

Permalink
fix: Fix arithmetic overflow in MemoryRegion
Browse files Browse the repository at this point in the history
fix #320
  • Loading branch information
jan-ferdinand committed Aug 19, 2024
1 parent ddabae0 commit f83f8aa
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions triton-vm/src/air/memory_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ impl IntegralMemoryLayout for DynamicTasmConstraintEvaluationMemoryLayout {

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct MemoryRegion {
start: u64,
start: BFieldElement,
size: u64,
}

impl MemoryRegion {
pub fn new<A: Into<u64>>(address: A, size: usize) -> Self {
let start = address.into();
let start = bfe!(address.into());
let size = u64::try_from(size).unwrap();
Self { start, size }
}
Expand All @@ -120,7 +120,11 @@ impl MemoryRegion {
}

pub fn contains_address<A: Into<u64>>(self, addr: A) -> bool {
(self.start..self.start + self.size).contains(&addr.into())
// move all arithmetic to u128 to avoid overflows
let addr = u128::from(addr.into());
let start = u128::from(self.start.value());
let end = start + u128::from(self.size);
(start..end).contains(&addr)
}
}

Expand Down Expand Up @@ -207,4 +211,13 @@ mod tests {
};
assert!(!layout.is_integral());
}

#[test]
fn memory_layout_integrity_check_does_not_panic_due_to_arithmetic_overflow() {
let mem_layout = DynamicTasmConstraintEvaluationMemoryLayout {
free_mem_page_ptr: bfe!(BFieldElement::MAX),
challenges_ptr: bfe!(1_u64 << 63),
};
assert!(mem_layout.is_integral());
}
}

0 comments on commit f83f8aa

Please sign in to comment.