Skip to content

Commit

Permalink
fix(levm): codecopy unnecessary memory allocation (#1311)
Browse files Browse the repository at this point in the history
Resolves #1293
  • Loading branch information
ilitteri authored Nov 27, 2024
1 parent 8728d41 commit 74351b1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crates/vm/levm/src/opcode_handlers/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ impl VM {

self.increase_consumed_gas(current_call_frame, gas_cost)?;

if size == 0 {
return Ok(OpcodeSuccess::Continue);
}

let bytecode_len = current_call_frame.bytecode.len();
let code = if offset < bytecode_len {
current_call_frame.bytecode.slice(
Expand Down
14 changes: 14 additions & 0 deletions crates/vm/levm/tests/edge_case_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,17 @@ fn test_non_compliance_addmod2() {
&U256::from("0xfc7490ee00fc74a0ee00fc7490ee00fc7490ee5")
);
}

#[test]
fn test_non_compliance_codecopy() {
let mut vm = new_vm_with_bytecode(Bytes::copy_from_slice(&[
0x5f, 0x60, 5, 0x60, 5, 0x39, 0x59,
]))
.unwrap();
let mut current_call_frame = vm.call_frames.pop().unwrap();
vm.execute(&mut current_call_frame);
assert_eq!(
current_call_frame.stack.stack.first().unwrap(),
&U256::zero()
);
}

0 comments on commit 74351b1

Please sign in to comment.