1/5
--- ## Memory and mstore ### Initial Memory State: - **Starting Point**: Before executing any memory operations, the memory is blank. - **Memory Representation**: Each block represents a byte. The memory layout is as follows: - `0x20`: Marks the 32nd byte in memory. - `0x40`: Marks the 64th byte in memory. - `0x60`: Marks the 96th byte in memory. - **Visualization**: Initially, all memory slots are zero, indicating no data has been stored yet. ### Understanding mstore Operation: - **Direct Memory Manipulation**: The `mstore` function is crucial for directly placing data in memory. - **Example Case**: Upon invoking `mstore` at `0x40`, the expected behavior is to place a value exactly at this position. - **Actual Behavior**: Contrary to expectations, `mstore` pads the value, filling from `0x40` to `0x60` with the value, using zero-padding for the bytes leading up to the last four bytes. ### Practical Implications of mstore Padding: - **Test Function**: A function `testMoleRevert` is used to ensure the code reverts under certain conditions by testing overflows with `type uint256 max`. - **Debugging**: Debugging tools such as Forge are employed to step through the execution and examine the memory and opcodes directly. - **Memory Overwriting Concern**: It’s revealed that `mstore` might be overwriting the free memory pointer at `0x40`, which is a critical error in memory management within Solidity contracts. ### The Forge Debugger Experience: - **Usage**: By holding down the key 'J', you can progress through the memory operations until the exact opcode is reached. - **Observations**: - The memory is modified by `mstore` at `0x40`, but because of zero-padding, the actual value is pushed to the end of the 32-byte block. - To access the meaningful data (last four bytes), you need to skip the preceding 28 bytes of zeros. ### Revert Function Analysis: - **Memory Reference for Revert**: The revert function aims to grab the error message starting from a certain memory position, but due to incorrect placement by `mstore`, it might end up with incorrect or no data. - **Correctness and Misplacement**: While the placement of data by `mstore` at `0x40` seems to comply with the intended memory operations, the analysis shows that the error handling might not work as expected due to this misplacement. #### Auditing Insights: - **Audit Finding**: The misuse of the free memory pointer (`0x40`) by overriding it during an `mstore` operation is highlighted as a significant issue in the contract, likely to be flagged as a critical error in an audit. - **Recommendation**: Corrective actions and code review are recommended to avoid such critical mistakes, ensuring that memory operations do not interfere with system-reserved pointers and that error messages are correctly handled and retrievable during a revert.
A detailed look at a Solidity memory demonstration - This lesson covers the differences between Solidity's mstore and revert opcodes and how they interact with memory. We also discuss how they work with the Free Memory Pointer.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Assembly
Writing smart contracts using Huff and Yul
Ethereum Virtual Machine OPCodes
Formal verification testing
Smart contract invariant testing
Halmos, Certora, Kontrol
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Guest lecturers:
Josselin Feist
Head of Blockchain at Trail of Bits
Last updated on January 17, 2025
Solidity Developer
Assembly and Formal VerificationDuration: 30min
Duration: 4h 38min
Duration: 3h 57min
Duration: 1h 56min
Course Overview
About the course
Assembly
Writing smart contracts using Huff and Yul
Ethereum Virtual Machine OPCodes
Formal verification testing
Smart contract invariant testing
Halmos, Certora, Kontrol
Security researcher
$49,999 - $120,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Guest lecturers:
Josselin Feist
Head of Blockchain at Trail of Bits
Last updated on January 17, 2025
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar