Skip to content

Commit

Permalink
vm: add CODECOPY, increase CALLDATACOPY limit to 512
Browse files Browse the repository at this point in the history
  • Loading branch information
cdump committed Jun 7, 2024
1 parent d6fe511 commit 862272d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
11 changes: 10 additions & 1 deletion evmole/evm/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,21 @@ def _exec_opcode(self, op: OpCode) -> tuple[int, *tuple[Any, ...]]:
mem_off = self.stack.pop_uint()
src_off = self.stack.pop_uint()
size = self.stack.pop_uint()
if size > 256:
if size > 512:
raise UnsupportedOpError(op)
value = self.calldata.load(src_off, size)
self.memory.store(mem_off, value)
return (4,)

case Op.CODECOPY:
mem_off = self.stack.pop_uint()
src_off = self.stack.pop_uint()
size = self.stack.pop_uint()
if src_off + size > len(self.code):
raise UnsupportedOpError(op)
self.memory.store(mem_off, Element(data=self.code[src_off : src_off + size]))
return (3,)

case Op.SLOAD:
slot = self.stack.pop()
self.stack.push_uint(0)
Expand Down
14 changes: 13 additions & 1 deletion js/src/evm/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,26 @@ export class Vm {
const mem_off = Number(this.stack.pop_uint())
const src_off = Number(this.stack.pop_uint())
const size = Number(this.stack.pop_uint())
if (size > 256) {
if (size > 512) {
throw new UnsupportedOpError(op)
}
const value = this.calldata.load(src_off, size)
this.memory.store(mem_off, value)
return [4]
}

case Op.CODECOPY: {
const mem_off = Number(this.stack.pop_uint())
const src_off = Number(this.stack.pop_uint())
const size = Number(this.stack.pop_uint())
if (src_off + size > this.code.length) {
throw new UnsupportedOpError(op)
}
const value = this.code.subarray(src_off, src_off + size)
this.memory.store(mem_off, new Element(value))
return [3]
}

case Op.SLOAD: {
const slot = this.stack.pop()
this.stack.push_uint(0n)
Expand Down
33 changes: 23 additions & 10 deletions rust/src/evm/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,27 +357,40 @@ where
}

op::CALLDATACOPY => {
let mem_off = self.stack.pop_uint()?;
let mem_off: u32 = self.stack.pop_uint()?.try_into()?;
let src_off = self.stack.pop_uint()?;
let size = self.stack.pop_uint()?;
let size: usize = self.stack.pop_uint()?.try_into()?;

let size32: usize = size.try_into()?;
if size32 > 256 {
if size > 512 {
Err(UnsupportedOpError { op }.into())
} else {
let mem_off32: u32 = mem_off.try_into()?; // TODO: custom error?
let value = self.calldata.load(src_off, size);
let mut data: Vec<u8> = vec![0; size];

let value = self.calldata.load(src_off, size32);
let mut data: Vec<u8> = vec![0; size32];

let l = std::cmp::min(size32, 32);
let l = std::cmp::min(size, 32);
data[0..l].copy_from_slice(&value.data[0..l]);

self.memory.store(mem_off32, data, value.label);
self.memory.store(mem_off, data, value.label);
Ok(StepResult::new(op, 4))
}
}

op::CODECOPY => {
let mem_off: u32 = self.stack.pop_uint()?.try_into()?;
let src_off: usize = self.stack.pop_uint()?.try_into()?;
let size: usize = self.stack.pop_uint()?.try_into()?;

if src_off + size > self.code.len() {
Err(UnsupportedOpError { op }.into())
} else {
let mut data: Vec<u8> = vec![0; size];
data[0..size].copy_from_slice(&self.code[src_off..src_off+size]);

self.memory.store(mem_off, data, None);
Ok(StepResult::new(op, 3))
}
},

op::SLOAD => {
let slot = self.stack.pop()?;
let mut ret = StepResult::new(op, 100);
Expand Down

0 comments on commit 862272d

Please sign in to comment.