3

Solidity seems to allocate memory under pointers being multiples of 32. This is understandable since most allocation sizes are multiples of 32. But is it a requirement or is it fine if a Yul inline violates this convention? E.g. in Yul you can easily allocate 33 bytes: mstore(0x40, add(33, mload(0x40)), but will it cause problems for Solidity? You can also place items under non-aligned locations, e.g. my_array := add(mload(0x40), 1) (remember to initialize my_array and update 0x40), will Solidity work fine with my_array?

Edit: It seems that the legacy pipeline breaks the 32-byte alignment when calling external functions, it allocates a multiple of 32 plus 4 bytes for the selector. Via-IR on the other hand rounds up all the allocations to multiples of 32 even if a call is made. So the questions is probably more about via-IR?

1 Answer 1

1

There are no assumptions that the free memory pointer is 32-byte aligned. Dynamic variables (bytes and string) do not have to occupy multiples of 32 bytes.

Related documentation:

Forge test showing bytes encoding that causes a misaligned fmp:

 function test_memory_allocation(bytes1 bs) external { bytes memory a = abi.encodePacked(bs); uint256 fmp; assembly { fmp := mload(0x40) } assertEq(fmp % 32, 0); // Fails } 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.