-
Notifications
You must be signed in to change notification settings - Fork 147
feat(vm): Memory variable #1609
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
Open
marioevz
wants to merge
2
commits into
main
Choose a base branch
from
memory-variable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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 |
---|---|---|
|
@@ -5,8 +5,8 @@ | |
from enum import Enum | ||
|
||
from ethereum_test_forks import Fork, Frontier | ||
from ethereum_test_tools import Address, Alloc, Bytecode, Conditional | ||
from ethereum_test_tools.vm.opcode import Opcodes as Op | ||
from ethereum_test_tools import Address, Alloc, Bytecode, Conditional, MemoryVariable | ||
from ethereum_test_tools import Opcodes as Op | ||
|
||
|
||
class ScenarioExpectOpcode(Enum): | ||
|
@@ -190,20 +190,40 @@ def make_gas_hash_contract(pre: Alloc) -> Address: | |
So that if we can't check exact value in expect section, | ||
we at least could spend unique gas amount. | ||
""" | ||
# EVM memory variables | ||
variable_byte_offset = MemoryVariable(0) | ||
variable_current_byte = MemoryVariable(32) | ||
|
||
# Code for memory initialization | ||
initialize_code = variable_byte_offset.store(0) | ||
calldata_copy = Op.JUMPDEST + Op.CALLDATACOPY( | ||
dest_offset=variable_current_byte.offset + 32 - 1, | ||
offset=variable_byte_offset, | ||
size=1, | ||
) | ||
|
||
# Code offsets | ||
offset_calldata_copy = len(initialize_code) | ||
offset_conditional = offset_calldata_copy + len(calldata_copy) | ||
|
||
# Deploy contract | ||
gas_hash_address = pre.deploy_contract( | ||
code=Op.MSTORE(0, 0) | ||
+ Op.JUMPDEST | ||
+ Op.CALLDATACOPY(63, Op.MLOAD(0), 1) | ||
+ Op.JUMPDEST | ||
code=initialize_code | ||
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 prefer not to subfunc the code for readability |
||
+ calldata_copy # offset_calldata_copy | ||
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 makes it less readable |
||
+ Op.JUMPDEST # offset_conditional | ||
+ Conditional( | ||
condition=Op.ISZERO(Op.MLOAD(32)), | ||
if_true=Op.MSTORE(0, Op.ADD(1, Op.MLOAD(0))) | ||
+ Conditional( | ||
condition=Op.GT(Op.MLOAD(0), 32), | ||
if_true=Op.RETURN(0, 0), | ||
if_false=Op.JUMP(5), | ||
condition=Op.ISZERO(variable_current_byte), | ||
if_true=( | ||
# Increase the calldata byte offset, and if it's greater than the calldata size, | ||
# return, otherwise jump to the calldata copy code and read the next byte. | ||
variable_byte_offset.add(1) | ||
+ Conditional( | ||
condition=Op.GT(variable_byte_offset, Op.CALLDATASIZE()), | ||
if_true=Op.RETURN(offset=0, size=0), | ||
if_false=Op.JUMP(offset_calldata_copy), | ||
) | ||
), | ||
if_false=Op.MSTORE(32, Op.SUB(Op.MLOAD(32), 1)) + Op.JUMP(14), | ||
if_false=(variable_current_byte.sub(1) + Op.JUMP(offset_conditional)), | ||
) | ||
) | ||
return gas_hash_address | ||
|
@@ -234,19 +254,25 @@ def make_invalid_opcode_contract(pre: Alloc, fork: Fork) -> Address: | |
if op not in valid_opcode_values: | ||
invalid_opcodes.append(op) | ||
|
||
variable_results_sum = MemoryVariable(0) | ||
variable_opcode = MemoryVariable(32) | ||
|
||
code = Bytecode( | ||
sum( | ||
Op.MSTORE(64, opcode) | ||
+ Op.MSTORE( | ||
32, | ||
Op.CALL(gas=50000, address=invalid_opcode_caller, args_offset=64, args_size=32), | ||
variable_opcode.store(opcode) | ||
+ variable_results_sum.add( | ||
Op.CALL( | ||
gas=50000, | ||
address=invalid_opcode_caller, | ||
args_offset=variable_opcode.offset, | ||
args_size=32, | ||
), | ||
) | ||
+ Op.MSTORE(0, Op.ADD(Op.MLOAD(0), Op.MLOAD(32))) | ||
for opcode in invalid_opcodes | ||
) | ||
# If any of invalid instructions works, mstore[0] will be > 1 | ||
+ Op.MSTORE(0, Op.ADD(Op.MLOAD(0), 1)) | ||
+ Op.RETURN(0, 32) | ||
+ variable_results_sum.add(1) | ||
+ variable_results_sum.return_value() | ||
) | ||
|
||
return pre.deploy_contract(code=code) |
Oops, something went wrong.
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.
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.
32, -1 magic numbers?
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.
It was 63 before, we were putting it in the last byte of the variable.
So the offset, plus its size (32), minus the one byte.