Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cancun): EIP-5656: MCOPY - Memory copying instruction #528

Merged
merged 19 commits into from
Jul 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/interpreter/src/instructions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ pub fn eval<H: Host, S: Spec>(opcode: u8, interp: &mut Interpreter, host: &mut H
opcode::DELEGATECALL => host::delegate_call::<S>(interp, host), //check
opcode::STATICCALL => host::static_call::<S>(interp, host), //check
opcode::CHAINID => host_env::chainid::<S>(interp, host),
opcode::MCOPY => memory::mcopy::<S>(interp, host),
_ => return_not_found(interp, host),
}
}
28 changes: 27 additions & 1 deletion crates/interpreter/src/instructions/memory.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
use crate::{gas, interpreter::Interpreter, primitives::U256, Host, InstructionResult};
use revm_primitives::SpecId::CANCUN;

use crate::{
gas,
interpreter::Interpreter,
primitives::{Spec, U256},
Host, InstructionResult,
};

pub fn mload(interpreter: &mut Interpreter, _host: &mut dyn Host) {
gas!(interpreter, gas::VERYLOW);
Expand Down Expand Up @@ -35,3 +42,22 @@ pub fn msize(interpreter: &mut Interpreter, _host: &mut dyn Host) {
gas!(interpreter, gas::BASE);
push!(interpreter, U256::from(interpreter.memory.effective_len()));
}
// From EIP-5656 MCOPY
pub fn mcopy<SPEC: Spec>(interpreter: &mut Interpreter, _host: &mut dyn Host) {
check!(interpreter, SPEC::enabled(CANCUN));
pop!(interpreter, dest, src, len);
if len == U256::ZERO {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not correct, even if len is zero, some gas needs to be deduced.
maybe it is best to check if len == 0 after [gas_or_fail](gas::verylowcopy_cost) is added.

return;
}
let len = as_usize_or_fail!(interpreter, len, InstructionResult::InvalidOperandOOG);
gas_or_fail!(interpreter, gas::verylowcopy_cost(len as u64));

let dest = as_usize_or_fail!(interpreter, dest, InstructionResult::InvalidOperandOOG);
memory_resize!(interpreter, dest, len);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would assume that both dest and src can increment memory. So you need to find what is bigger and increment memory by a bigger value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a function to check and handle this memory_copy_resize that was inspired by the geth pr's implementation in the memory_table.go


let src = as_usize_or_fail!(interpreter, src, InstructionResult::InvalidOperandOOG);
// Read data with length len from src
let data = interpreter.memory.copy(src, len);
// Write data to dest
interpreter.memory.set_data(src, dest, len, &data);
}
5 changes: 3 additions & 2 deletions crates/interpreter/src/instructions/opcode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub const JUMPI: u8 = 0x57;
pub const PC: u8 = 0x58;
pub const MSIZE: u8 = 0x59;
pub const JUMPDEST: u8 = 0x5b;
pub const MCOPY: u8 = 0x5e;
pub const PUSH0: u8 = 0x5f;
pub const PUSH1: u8 = 0x60;
pub const PUSH2: u8 = 0x61;
Expand Down Expand Up @@ -276,7 +277,7 @@ pub const OPCODE_JUMPMAP: [Option<&'static str>; 256] = [
/* 0x5b */ Some("JUMPDEST"),
/* 0x5c */ None,
/* 0x5d */ None,
/* 0x5e */ None,
/* 0x5e */ Some("MCOPY"),
/* 0x5f */ Some("PUSH0"),
/* 0x60 */ Some("PUSH1"),
/* 0x61 */ Some("PUSH2"),
Expand Down Expand Up @@ -658,7 +659,7 @@ macro_rules! gas_opcodee {
OpInfo::jumpdest(),
/* 0x5c */ OpInfo::none(),
/* 0x5d */ OpInfo::none(),
/* 0x5e */ OpInfo::none(),
/* 0x5e MCOPY */ OpInfo::dynamic_gas(),
/* 0x5f PUSH0 */
OpInfo::gas(if SpecId::enabled($spec_id, SpecId::SHANGHAI) {
gas::BASE
Expand Down
8 changes: 8 additions & 0 deletions crates/interpreter/src/interpreter/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ impl Memory {
&self.data[offset..offset + size]
}

/// Memory copy
/// Copies memory inplace to a returns a [u8]
#[inline(always)]
pub fn copy(&self, offset: usize, size: usize) -> [u8; 32] {
let mut copy = [0u8; 32];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like copy_word because copy is only 32, it should be size

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed this with returning a vec of bytes and then passing as a slice to the data

copy.copy_from_slice(&self.data[offset..size]);
copy
}
/// Set memory region at given offset
///
/// # Safety
Expand Down