1

I was dabbling with inline assembly, and I wanted to discover the extent of addressable memory. After many trials, I found out that the only usable memory locations that could be accessed with OPCODEs like mload and mstore fall within the inclusive range: (0x0, 0x12B260), or (0, 1225312) in decimal.

Why is 1225312 the last addressable memory location? And where in the docs does it mention the limits of memory?

7
  • Solidity has documentation regarding this docs.soliditylang.org/en/v0.8.6/internals/… but not sure if helpful for this question Jun 26, 2021 at 18:45
  • Why do you say it is the last addressable memory? From the Yellow paper memory is 2**256 bytes, usually the cost of accessing a position p is expensive, the formula is quadratic on p.
    – Ismael
    Jun 27, 2021 at 5:49
  • @Ismael I just noticed that it's not persistent. See the following code: gist.github.com/scorpion9979/38c93c0123741cf15f41a5be92544ea2 Jun 28, 2021 at 7:34
  • When the location is a constant value, the maximum addressable location is 1225312. But when it's an input given by the user, the maximum addressable location is 1225216. Apparently adding more inputs to the memAtLoc method further decreases the possible maximum addressable location in the assembly lines. Jun 28, 2021 at 7:40
  • How are you testing? ganache, geth, besu, remix, ... In remix I set gas limit to 300M and I was able to call memAtLoc with 1_500_000, using 4_453_977 gas, and with 2_500_000 it uses 12_177_244 gas.
    – Ismael
    Jun 28, 2021 at 16:00

1 Answer 1

0

From Ethereum's Yellow pape section 9.4.1 Machine State the memory is defined as an array of 2^256 bytes, approx. 1.15 * 10^77 bytes. So there's a limit how much memory is available to contracts but it is really high.

There's a much smaller practical limit defined by the cost to access memory. From appendix H.1 Gas Cost the formular for memory cost is:

C_mem(a) ≡ G_memory · a + trunc(a^2 / 512)

Where a is the highest address rounded to 32, G_memory is 3 gas.

If the contract access memory at address p, then it will be charged for the whole interval [0, p). It doesn't mapper if it only read one byte.

Also in section 9.1 Fees Overview they state the practical limit for memory is 2^32.

9.1: Fees Overview

[..] Due to this fee it is highly unlikely addresses will ever go above 32-bit bounds. That said, implementations must be able to manage this eventuality.

Geth implementation in several places assume the maximum address is 64 bits.

Another limit that should be considered is block gas limit, since the cost to access memory has to fit in 64 bits, so maximum a is such that 3 a + trunc(a^2 / 512) < 2^64.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.