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

Memory Allocation overflow on abi.decode() #4409

Closed
2 tasks done
pscott opened this issue Feb 22, 2023 · 11 comments
Closed
2 tasks done

Memory Allocation overflow on abi.decode() #4409

pscott opened this issue Feb 22, 2023 · 11 comments
Labels
T-bug Type: bug

Comments

@pscott
Copy link

pscott commented Feb 22, 2023

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.2.0 (b44b045 2023-02-21T00:22:41.521901Z)

What command(s) is the bug in?

forge test --optimize --vv

Operating System

macOS (Apple Silicon)

Describe the bug

I’m getting the error [FAIL. Reason: Memory allocation overflow] on one of my tests.
it happens when I run abi.decode() like so:

// Structre of `Indexed Strategy`
struct IndexedStrategy {
  uint8 i;
  bytes b;
};

...

// Actual code that fails
(
    address v,
    uint256 id,
    Choice choice,
    IndexedStrategy[] memory uvs,
    string memory reason
) = abi.decode(data, (address, uint256, Choice, IndexedStrategy[], string));

This code works just fine if I remove reason. It also works just fine if I remove uvs and keep reason.
I get the same error if I interchange reason and put it between id and choice (I thought maybe it had to do with two dynamic args being next to each other).

@mattsse
Copy link
Member

mattsse commented Feb 22, 2023

this is an evm panic ref https://soliditydeveloper.com/solidity-0.8 0x41
can you try after increasing the memory_limit

forge config | grep memory                                                                                                                                                                                                                 
memory_limit = 33554432

not sure if that's exposed in cli, if not we should

@pscott
Copy link
Author

pscott commented Feb 22, 2023

this is an evm panic ref https://soliditydeveloper.com/solidity-0.8 0x41 can you try after increasing the memory_limit

forge config | grep memory                                                                                                                                                                                                                 
memory_limit = 33554432

not sure if that's exposed in cli, if not we should

Cheers. Indeed

➜  tmp git:(main) ✗ forge config | rg memory         
memory_limit = 33554432

Haven't found a way to increase the memory limit via CLI.
Also I'm not sure why this would trigger 0x41: my IndexedStrategy array holds 1 element and reason is set to "".. 🤔

@mattsse
Copy link
Member

mattsse commented Feb 22, 2023

unfamiliar with the internals but Memory allocation overflow is the mapped error for the evm panic with 0x41

@pscott
Copy link
Author

pscott commented Feb 24, 2023

Updated the foundry.toml profile.default config to and added memory_limit = 67108864 (x2 default value).
Got the same error. Any idea what I should look into?

@mattsse
Copy link
Member

mattsse commented Feb 24, 2023

can you try with something ridiculous like 1GB?

@rakita Under what circumstances is the memory limit in the evm actually violated?

@pscott
Copy link
Author

pscott commented Feb 24, 2023

memory_limit = 1000000000 still fails.
I wonder if memory_limit is actually taken into account because I have no way of knowing whether the memory limit actually is 1GB or the default one

@mattsse
Copy link
Member

mattsse commented Feb 24, 2023

hmm, perhaps this is actually unrelated to the revm env setting @rakita?

@rakita
Copy link
Contributor

rakita commented Feb 24, 2023

can you try with something ridiculous like 1GB?

@rakita Under what circumstances is the memory limit in the evm actually violated?

Do you mean this: https://github.com/bluealloy/revm/blob/main/crates/primitives/Cargo.toml#L60
It is memory inside Interpreter, when there is for example MSTORE that wants to write to the memory location with offset of 20gb, this would trigger.

@mattsse
Copy link
Member

mattsse commented Feb 24, 2023

this error is decoded from a revert/panic here

foundry/evm/src/decode.rs

Lines 117 to 118 in 13c1cf7

// allocating too much memory or creating too large array
Ok("Memory allocation overflow".to_string())

for

// allocating too much memory or creating too large array

is this unrelated to the memory_limit setting?

@pscott
Copy link
Author

pscott commented Feb 24, 2023

Nvm.. this error actually gets thrown when you try to decode something invalid (in my case, I forgot to add the last argument when ENCODING, so decoding was failing).

Would have expected another error to pop, not a weird memory overflow error but.. such is life.

sorry for wasting your time :(

@pscott pscott closed this as completed Feb 24, 2023
@github-project-automation github-project-automation bot moved this from Todo to Done in Foundry Feb 24, 2023
@mattsse
Copy link
Member

mattsse commented Feb 24, 2023

nice!

good to know, thanks for sharing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-bug Type: bug
Projects
Archived in project
Development

No branches or pull requests

3 participants