-
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 5 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,35 @@ 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); | ||
rakita marked this conversation as resolved.
Show resolved
Hide resolved
|
||
gas_or_fail!(interpreter, gas::verylowcopy_cost(len as u64)); | ||
|
||
// memory_resize!(interpreter, dest, len); | ||
memory_copy_resize(interpreter, host); | ||
let dest = as_usize_or_fail!(interpreter, dest, InstructionResult::InvalidOperandOOG); | ||
|
||
|
||
let src = as_usize_or_fail!(interpreter, src, InstructionResult::InvalidOperandOOG); | ||
// Read data with length len from src | ||
let binding = interpreter.memory.copy_to_vec(src, 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. It is not optimal to create temporary vec. There is a memory operation from 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 found and used 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 works perfectly |
||
let data = binding.as_slice() ; | ||
// Write data to dest | ||
interpreter.memory.set_data(src, dest, len, data); | ||
} | ||
|
||
pub fn memory_copy_resize(interpreter: &mut Interpreter, _host: &mut dyn Host) { | ||
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 is faulty, and you already have |
||
let mut mstart = as_usize_or_fail!(interpreter, interpreter.stack.peek(0).unwrap()); // stack[0]: dst | ||
|
||
if as_usize_or_fail!(interpreter, interpreter.stack.peek(1).unwrap()) > mstart { | ||
mstart = as_usize_or_fail!(interpreter, interpreter.stack.peek(1).unwrap()); // stack[1]: source | ||
} | ||
memory_resize!(interpreter, mstart, as_usize_or_fail!(interpreter, interpreter.stack.peek(2).unwrap())); | ||
} |
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.