1/5
--- ### Understanding the Memory Structure When we begin examining the code, we first look at the state of memory before any significant operations—like `MSTORE`—have been executed. Our initial state shows that memory is empty, illustrated by the first 32 bytes all being set to zero. Each byte in memory is represented as a block, with `0x20` (32 in hexadecimal) marking the 32nd byte. ### Execution of MSTORE Executing `MSTORE` at a specific position in memory (`0x40`) doesn't directly place the value at that position. Instead, Solidity pads this placement to align with 32-byte segments due to its handling of memory. The value to be stored is padded with zeros up to 32 bytes, which means while the address `0x40` is targeted, the significant data ends up occupying the end of this 32-byte segment. Here's a step-by-step breakdown of what happens during an `MSTORE` operation: - The value is zero-padded to fill a block of 32 bytes. - This results in the significant data being pushed towards the end of the 32-byte block starting from `0x40`. ### Debugging and Analysis To delve deeper into how this behaves during runtime, we use debugging tools. By stepping through the code in a debugger, we can observe operations like `PUSH` and `MSTORE` in action. In this specific instance, we note that the `MSTORE` doesn't just overwrite `0x40` but affects the entire range from `0x40` to `0x60`, filling it primarily with zeros and ending with the actual data value. ### Issues and Concerns with Memory Handling A notable issue arises with the use of `0x40`—commonly used as the free memory pointer in Solidity—which gets overwritten during this `MSTORE`. This is typically considered bad practice as it can lead to unpredictable behavior or bugs, particularly when the memory area being overwritten includes essential control data like the free memory pointer. ### Reverting Operations and Error Handling If we attempt operations that exceed memory constraints or misuse it, Solidity might revert these operations. In our scenario, we hypothesized what might happen if we attempt to access or manipulate memory incorrectly, which would trigger a revert. Typically, such reverts are due to safety checks in the code ensuring that operations do not exceed predefined limits like `type(uint256).max`. In practice, developers can further secure their code by explicitly testing for expected reverts using test cases that simulate possible overflow scenarios or other edge cases in memory handling.
A deep dive into Incorrect Memory Access Bug Recap - This lesson revisits the memory access bug and identifies the error in the code, then utilizes a memory diagram to demonstrate the correct way to handle memory allocation and reversion.
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