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

fix(cancun): revamp MCOPY with overlapping #254

Merged
merged 6 commits into from
May 29, 2024

Conversation

Nashtare
Copy link
Collaborator

cf comment in #252 (comment): this handles properly cases of overlapping with risk of overwrite.

Also adds some tests from the EIP

@Nashtare Nashtare added this to the Cancun - Q2 2024 milestone May 28, 2024
@Nashtare Nashtare requested a review from LindaGuiga May 28, 2024 15:33
@Nashtare Nashtare self-assigned this May 28, 2024
@github-actions github-actions bot added the crate: evm_arithmetization Anything related to the evm_arithmetization crate. label May 28, 2024
// stack: count, DST, SRC, count, retdest
%lt_const(0x21)
// stack: count <= 32, DST, SRC, count, retdest
%jumpi(memcpy_bytes_finish)
Copy link
Contributor

Choose a reason for hiding this comment

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

Won't memcpy_bytes_finish copy the first count bytes instead of the last count bytes here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

From my understanding it shouldn't. Small sequences are not using this %memcpy_bytes_backwards macro (see last comment in sys_mcopy code). For larger sequences (once the below DST / SRC update is fixed as per your comment), then we should enter this last iteration with the correct final offsets and the remaining count being size % 32.

Comment on lines 134 to 137
%sub_const(0x40)
// Decrement SRC by 32.
SWAP1
%sub_const(0x20)
Copy link
Contributor

@wborgeaud wborgeaud May 29, 2024

Choose a reason for hiding this comment

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

These subtraction can "underflow" when the offset of SRC or the original DST are <32.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah yeah good catch

let post_memory = hex!("0000010203040506070000000000000000000000000000000000000000000000");

assert!(test_mcopy(dest_offset, offset, size, &pre_memory, &post_memory).is_ok())
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The test

#[test]
fn test() {
    let dest_offset = 1;
    let offset = 0;
    let size = 33;
    let pre_memory = hex!("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2021222324252627");
    let post_memory = hex!("00000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20222324252627");

    assert!(test_mcopy(dest_offset, offset, size, &pre_memory, &post_memory).is_ok())
}

currently fails due to the issues mentioned above. Maybe we can add it here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah good point. The EIP specifies a wider set of tests for all edge cases, could be worth adding a few others as well.

@Nashtare
Copy link
Collaborator Author

@wborgeaud let me know if this looks good to you now!

Copy link
Contributor

@wborgeaud wborgeaud left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

@Nashtare Nashtare merged commit a9f095a into cancun/mcopy_check_offsets May 29, 2024
6 checks passed
@Nashtare Nashtare deleted the cancun/mcopy_backwards_copy branch May 29, 2024 15:54
Nashtare added a commit that referenced this pull request May 29, 2024
* fix: check invalid offsets

* Update per Will's comment

* Minor

* fix(cancun): revamp `MCOPY` with overlapping (#254)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crate: evm_arithmetization Anything related to the evm_arithmetization crate.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants