Skip to content

Commit

Permalink
make writing to heap directly private
Browse files Browse the repository at this point in the history
  • Loading branch information
joonazan committed Jul 24, 2024
1 parent 88bc3b2 commit 26d3f79
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
23 changes: 13 additions & 10 deletions src/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ impl HeapId {
#[derive(Debug, Clone, PartialEq)]
pub struct Heap(Vec<u8>);

impl Heap {
fn write_u256(&mut self, start_address: u32, value: U256) {
let end = (start_address + 32) as usize;
if end > self.0.len() {
self.0.resize(end, 0);
}

let mut bytes = [0; 32];
value.to_big_endian(&mut bytes);
self.0[start_address as usize..end].copy_from_slice(&bytes);
}
}

impl HeapInterface for Heap {
fn read_u256(&self, start_address: u32) -> U256 {
self.read_u256_partially(start_address..start_address + 32)
Expand All @@ -33,16 +46,6 @@ impl HeapInterface for Heap {
}
U256::from_big_endian(&bytes)
}
fn write_u256(&mut self, start_address: u32, value: U256) {
let end = (start_address + 32) as usize;
if end > self.0.len() {
self.0.resize(end, 0);
}

let mut bytes = [0; 32];
value.to_big_endian(&mut bytes);
self.0[start_address as usize..end].copy_from_slice(&bytes);
}
fn read_range_big_endian(&self, range: Range<u32>) -> Vec<u8> {
let end = (range.end as usize).min(self.0.len());
let mut result = vec![0; range.len()];
Expand Down
1 change: 0 additions & 1 deletion src/instruction_handlers/heap_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use u256::U256;
pub trait HeapInterface {
fn read_u256(&self, start_address: u32) -> U256;
fn read_u256_partially(&self, range: Range<u32>) -> U256;
fn write_u256(&mut self, start_address: u32, value: U256);
fn read_range_big_endian(&self, range: Range<u32>) -> Vec<u8>;
}

Expand Down
12 changes: 7 additions & 5 deletions src/single_instruction_test/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ pub struct Heap {
pub(crate) write: Option<(u32, U256)>,
}

impl Heap {
fn write_u256(&mut self, start_address: u32, value: U256) {
assert!(self.write.is_none());
self.write = Some((start_address, value));
}
}

impl HeapInterface for Heap {
fn read_u256(&self, start_address: u32) -> U256 {
assert!(self.write.is_none());
Expand All @@ -25,11 +32,6 @@ impl HeapInterface for Heap {
U256::from_little_endian(&result)
}

fn write_u256(&mut self, start_address: u32, value: U256) {
assert!(self.write.is_none());
self.write = Some((start_address, value));
}

fn read_range_big_endian(&self, _: std::ops::Range<u32>) -> Vec<u8> {
// This is wrong, but this method is only used to get the final return value.
vec![]
Expand Down

0 comments on commit 26d3f79

Please sign in to comment.