Skip to content

Commit

Permalink
fix(levm): add memory alignment in extcodecopy (#1263)
Browse files Browse the repository at this point in the history
**Motivation**

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

**Description**

Previously, in EXTCODECOPY the allocated memory size was not aligned to
a multiple of 32 bytes, and was added in, for example, 12 bytes (should
increase in blocks of 32). Now the increases are done in groups of 32.

Closes #1251
  • Loading branch information
maximopalopoli authored Nov 25, 2024
1 parent 450476a commit ec09fed
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 6 additions & 2 deletions crates/vm/levm/src/opcode_handlers/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,13 @@ impl VM {

let bytecode = self.get_account(&address).info.bytecode;

let new_memory_size = dest_offset.checked_add(size).ok_or(VMError::Internal(
let new_memory_size = (((!size).checked_add(1).ok_or(VMError::Internal(
InternalError::ArithmeticOperationOverflow,
))?;
))?) & 31)
.checked_add(size)
.ok_or(VMError::Internal(
InternalError::ArithmeticOperationOverflow,
))?;
let current_memory_size = current_call_frame.memory.data.len();
if current_memory_size < new_memory_size {
current_call_frame.memory.data.resize(new_memory_size, 0);
Expand Down
11 changes: 11 additions & 0 deletions crates/vm/levm/tests/edge_case_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,14 @@ fn test_non_compliance_extcodecopy() {
vm.execute(&mut current_call_frame);
assert_eq!(current_call_frame.stack.stack.pop().unwrap(), U256::zero());
}

#[test]
fn test_non_compliance_extcodecopy_memory_resize() {
let mut vm = new_vm_with_bytecode(Bytes::copy_from_slice(&[
0x60, 12, 0x5f, 0x5f, 0x5f, 0x3c, 89,
]))
.unwrap();
let mut current_call_frame = vm.call_frames.pop().unwrap();
vm.execute(&mut current_call_frame);
assert_eq!(current_call_frame.stack.pop().unwrap(), U256::from(32));
}

0 comments on commit ec09fed

Please sign in to comment.