Skip to content

Commit

Permalink
chore(shared_memory): tests, completed replaced old memory, some bug …
Browse files Browse the repository at this point in the history
…fixes
  • Loading branch information
Lorenzo Feroleto committed Aug 26, 2023
1 parent 98b9332 commit d76a2a2
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 63 deletions.
1 change: 0 additions & 1 deletion crates/interpreter/src/instructions/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,6 @@ pub fn call_inner<SPEC: Spec>(
let (reason, gas, return_data) = host.call(&mut call_input, &interpreter.shared_memory);

interpreter.return_data_buffer = return_data;

let target_len = min(out_len, interpreter.return_data_buffer.len());

match reason {
Expand Down
4 changes: 2 additions & 2 deletions crates/interpreter/src/instructions/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub fn mstore8(interpreter: &mut Interpreter, _host: &mut dyn Host) {
gas!(interpreter, gas::VERYLOW);
pop!(interpreter, index, value);
let index = as_usize_or_fail!(interpreter, index, InstructionResult::InvalidOperandOOG);
shared_memory_resize!(interpreter, index, 32);
shared_memory_resize!(interpreter, index, 1);
let value = value.as_le_bytes()[0];
// Safety: we resized our memory two lines above.
unsafe {
Expand All @@ -57,7 +57,7 @@ pub fn msize(interpreter: &mut Interpreter, _host: &mut dyn Host) {
gas!(interpreter, gas::BASE);
push!(
interpreter,
U256::from(interpreter.shared_memory.borrow().effective_len())
U256::from(interpreter.shared_memory.borrow().len())
);
}

Expand Down
18 changes: 10 additions & 8 deletions crates/interpreter/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct Interpreter {
/// left gas. Memory gas can be found in Memory field.
pub gas: Gas,
/// Memory.
pub memory: Memory,
// pub memory: Memory,
/// Stack.
pub stack: Stack,
/// After call returns, its return data is saved here.
Expand Down Expand Up @@ -73,7 +73,7 @@ impl Interpreter {
Self {
instruction_pointer: contract.bytecode.as_ptr(),
return_range: Range::default(),
memory: Memory::new(),
// memory: Memory::new(),
stack: Stack::new(),
return_data_buffer: Bytes::new(),
contract,
Expand Down Expand Up @@ -101,7 +101,7 @@ impl Interpreter {
Self {
instruction_pointer: contract.bytecode.as_ptr(),
return_range: Range::default(),
memory: Memory::new(),
// memory: Memory::new(),
stack: Stack::new(),
return_data_buffer: Bytes::new(),
contract,
Expand All @@ -122,9 +122,9 @@ impl Interpreter {
}

/// Reference of interpreter memory.
pub fn memory(&self) -> &Memory {
&self.memory
}
// pub fn memory(&self) -> &Memory {
// &self.memory
// }

/// Reference of interpreter stack.
pub fn stack(&self) -> &Stack {
Expand Down Expand Up @@ -182,13 +182,15 @@ impl Interpreter {
/// Copy and get the return value of the interpreter, if any.
pub fn return_value(&self) -> Bytes {
// if start is usize max it means that our return len is zero and we need to return empty
if self.return_range.start == usize::MAX {
let bytes = if self.return_range.start == usize::MAX {
Bytes::new()
} else {
Bytes::copy_from_slice(self.shared_memory.borrow().get_slice(
self.return_range.start,
self.return_range.end - self.return_range.start,
))
}
};
self.shared_memory.borrow_mut().free_memory();
bytes
}
}
Loading

0 comments on commit d76a2a2

Please sign in to comment.