-
Notifications
You must be signed in to change notification settings - Fork 966
deduplication for COPY operations #1655
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
Closed
+46
−49
Closed
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9d196f3
add copy_cost for *COPY operations
harsh-ps-2003 6b15eee
seperate common logic of opcodes to a method
harsh-ps-2003 1b72873
fmt nit
harsh-ps-2003 0453a50
performance nit
harsh-ps-2003 9c685ad
fmt nit
harsh-ps-2003 dbc2d57
test nits
harsh-ps-2003 1474059
fmt nit
harsh-ps-2003 a1a488b
refactor
harsh-ps-2003 f92ff49
Merge branch 'bluealloy:main' into copycode
harsh-ps-2003 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,24 +38,12 @@ pub fn codesize<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) | |
|
|
||
| pub fn codecopy<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) { | ||
| pop!(interpreter, memory_offset, code_offset, len); | ||
| let len = as_usize_or_fail!(interpreter, len); | ||
| gas_or_fail!(interpreter, gas::verylowcopy_cost(len as u64)); | ||
| if len == 0 { | ||
| return; | ||
| } | ||
| let memory_offset = as_usize_or_fail!(interpreter, memory_offset); | ||
| let code_offset = as_usize_saturated!(code_offset); | ||
| resize_memory!(interpreter, memory_offset, len); | ||
|
|
||
| let len = as_usize_or_fail!(interpreter, len); | ||
| // Inform the optimizer that the bytecode cannot be EOF to remove a bounds check. | ||
| assume!(!interpreter.contract.bytecode.is_eof()); | ||
| // Note: this can't panic because we resized memory to fit. | ||
| interpreter.shared_memory.set_data( | ||
| memory_offset, | ||
| code_offset, | ||
| len, | ||
| interpreter.contract.bytecode.original_byte_slice(), | ||
| ); | ||
| let source = interpreter.contract.bytecode.original_byte_slice().as_ptr(); | ||
| copy_to_memory(interpreter, memory_offset, code_offset, len, source); | ||
| } | ||
|
|
||
| pub fn calldataload<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) { | ||
|
|
@@ -94,21 +82,9 @@ pub fn callvalue<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) | |
| pub fn calldatacopy<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) { | ||
| pop!(interpreter, memory_offset, data_offset, len); | ||
| let len = as_usize_or_fail!(interpreter, len); | ||
| gas_or_fail!(interpreter, gas::verylowcopy_cost(len as u64)); | ||
| if len == 0 { | ||
| return; | ||
| } | ||
| let memory_offset = as_usize_or_fail!(interpreter, memory_offset); | ||
| let data_offset = as_usize_saturated!(data_offset); | ||
| resize_memory!(interpreter, memory_offset, len); | ||
|
|
||
| // Note: this can't panic because we resized memory to fit. | ||
| interpreter.shared_memory.set_data( | ||
| memory_offset, | ||
| data_offset, | ||
| len, | ||
| &interpreter.contract.input, | ||
| ); | ||
| let source = interpreter.contract.input.as_ptr(); | ||
| copy_to_memory(interpreter, memory_offset, data_offset, len, source); | ||
| } | ||
|
|
||
| /// EIP-211: New opcodes: RETURNDATASIZE and RETURNDATACOPY | ||
|
|
@@ -127,8 +103,6 @@ pub fn returndatacopy<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interprete | |
| pop!(interpreter, memory_offset, offset, len); | ||
|
|
||
| let len = as_usize_or_fail!(interpreter, len); | ||
| gas_or_fail!(interpreter, gas::verylowcopy_cost(len as u64)); | ||
|
|
||
| let data_offset = as_usize_saturated!(offset); | ||
| let data_end = data_offset.saturating_add(len); | ||
|
|
||
|
|
@@ -139,22 +113,8 @@ pub fn returndatacopy<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interprete | |
| return; | ||
| } | ||
|
|
||
| // if len is zero memory is not resized. | ||
| if len == 0 { | ||
| return; | ||
| } | ||
|
|
||
| // resize memory | ||
| let memory_offset = as_usize_or_fail!(interpreter, memory_offset); | ||
| resize_memory!(interpreter, memory_offset, len); | ||
|
|
||
| // Note: this can't panic because we resized memory to fit. | ||
| interpreter.shared_memory.set_data( | ||
| memory_offset, | ||
| data_offset, | ||
| len, | ||
| &interpreter.return_data_buffer, | ||
| ); | ||
| let source = interpreter.return_data_buffer.as_ptr(); | ||
| copy_to_memory(interpreter, memory_offset, data_offset, len, source); | ||
| } | ||
|
|
||
| /// Part of EOF `<https://eips.ethereum.org/EIPS/eip-7069>`. | ||
|
|
@@ -179,6 +139,32 @@ pub fn returndataload<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &m | |
| *offset = B256::from(output).into(); | ||
| } | ||
|
|
||
| // common logic for copying data from a source buffer to the EVM's memory | ||
| fn copy_to_memory( | ||
| interpreter: &mut Interpreter, | ||
| memory_offset: U256, | ||
| data_offset: usize, | ||
| len: usize, | ||
| source: *const u8, | ||
| ) { | ||
|
||
| gas_or_fail!(interpreter, gas::verylowcopy_cost(len as u64)); | ||
| if len == 0 { | ||
| return; | ||
| } | ||
| let memory_offset = as_usize_or_fail!(interpreter, memory_offset); | ||
| resize_memory!(interpreter, memory_offset, len); | ||
|
|
||
| // Note: this can't panic because we resized memory to fit. | ||
| unsafe { | ||
| interpreter.shared_memory.set_data( | ||
| memory_offset, | ||
| data_offset, | ||
| len, | ||
| std::slice::from_raw_parts(source, len), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| pub fn gas<H: Host + ?Sized>(interpreter: &mut Interpreter, _host: &mut H) { | ||
| gas!(interpreter, gas::BASE); | ||
| push!(interpreter, U256::from(interpreter.gas.remaining())); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
source's length is notlen, this should be*const [u8]. This is why tests are failing