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

♻️ More memory-safe annotations #1353

Merged
merged 2 commits into from
Feb 11, 2025
Merged

♻️ More memory-safe annotations #1353

merged 2 commits into from
Feb 11, 2025

Conversation

Vectorized
Copy link
Owner

Description

https://docs.soliditylang.org/en/latest/assembly.html#advanced-safe-use-of-memory

Beyond the strict definition of memory-safety given above, there are cases in which you may want to use more than 64 bytes of scratch space starting at memory offset 0. If you are careful, it can be admissible to use memory up to (and not including) offset 0x80 and still safely declare the assembly block as memory-safe. This is admissible under either of the following conditions:

  • By the end of the assembly block, the free memory pointer at offset 0x40 is restored to a sane value (i.e. it is either restored to its original value or an increment of it due to a manual memory allocation), and the memory word at offset 0x60 is restored to a value of zero.

  • The assembly block terminates, i.e. execution can never return to high-level Solidity code. This is the case, for example, if your assembly block unconditionally ends in calling the revert opcode.

Checklist

Ensure you completed all of the steps below before submitting your pull request:

  • Ran forge fmt?
  • Ran forge test?

Pull requests with an incomplete checklist will be thrown out.

@Vectorized Vectorized changed the title Memsafe more ♻️ More memory-safe annotations Feb 11, 2025
@Vectorized Vectorized merged commit 61bcff4 into main Feb 11, 2025
13 checks passed
@Vectorized Vectorized deleted the memsafe-more branch February 11, 2025 12:24
@@ -50,6 +51,7 @@ contract EIP7702Proxy {

fallback() external payable virtual {
uint256 s = __self;
/// @solidity memory-safe-assembly
assembly {
mstore(0x40, returndatasize()) // Optimization trick to change `6040608052` into `3d604052`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mean 6080604052 (mstore(0x40, 0x80))?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Oh yes. Nice find!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants