Skip to content

Commit

Permalink
fix(levm): prevent inefficient memory allocation in extcodecopy (#1258)
Browse files Browse the repository at this point in the history
**Motivation**

Fixes an memory inefficiency found by
[FuzzingLabs](https://github.com/FuzzingLabs) in extcodecopy opcode
implementation.

**Description**

Before, there was memory allocation even if the size to copy was zero.
Now, if that happens, returns.

Closes #1245
  • Loading branch information
maximopalopoli authored Nov 25, 2024
1 parent 08d15bb commit 450476a
Show file tree
Hide file tree
Showing 2 changed files with 12 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 @@ -314,6 +314,10 @@ impl VM {

self.increase_consumed_gas(current_call_frame, gas_cost)?;

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

if !is_cached {
self.cache_from_db(&address);
};
Expand Down
8 changes: 8 additions & 0 deletions crates/vm/levm/tests/edge_case_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,11 @@ fn test_non_compliance_returndatacopy() {
let txreport = vm.execute(&mut current_call_frame);
assert_eq!(txreport.result, TxResult::Revert(VMError::VeryLargeNumber));
}

#[test]
fn test_non_compliance_extcodecopy() {
let mut vm = new_vm_with_bytecode(Bytes::copy_from_slice(&[88, 88, 88, 89, 60, 89])).unwrap();
let mut current_call_frame = vm.call_frames.pop().unwrap();
vm.execute(&mut current_call_frame);
assert_eq!(current_call_frame.stack.stack.pop().unwrap(), U256::zero());
}

0 comments on commit 450476a

Please sign in to comment.