-
Notifications
You must be signed in to change notification settings - Fork 644
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
Changes from 4 commits
6537323
02d1480
a1df5a3
3615f07
01e2841
3a257ca
e490c74
be43cca
fc0cb3a
71d9eca
9bf0669
2fd74c6
97b4ed9
8fd3de5
576fa1c
2742804
a54a86e
53ca642
10a91d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
|
@@ -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 { | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made a function to check and handle this |
||
|
||
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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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.