foundry: Stack Overflow in forge test
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 (9b2d95d 2022-06-23T00:22:22.617534Z)
What command(s) is the bug in?
forge test
Operating System
macOS (Apple Silicon)
Describe the bug
It seems our test suite has just reach some threshold that causes a stack overflow.
Repro steps:
(I tested these on a fresh clone)
git clone https://github.com/ethereum-optimism/optimism
cd optimism/packages/contracts-bedrock
yarn # to install contract deps
forge test # this will work fine
git checkout 093244521b1f0f9489cd573ad66c3c317d7ecea4
forge test
The output of the final test on will terminate with:
thread '<unknown>' has overflowed its stack
fatal runtime error: stack overflow
[1] 62889 abort forge test
The same bug is visible in this CI output: https://app.circleci.com/pipelines/github/ethereum-optimism/optimism/3503/workflows/19bb9e25-731e-4dcf-9fc2-9edfb8b928a7/jobs/45876/parallel-runs/0/steps/0-105
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15 (9 by maintainers)
I don’t think this is a bug on our end, forgive me if I misunderstand:
In
L2OutputOracle_Initializer::setUpyou create a proxy that delegates toL2OutputOracle(the address of which is stored inoracleImpl. Let’s call this Proxy 1. The address of this proxy is then stored inoracle.In
L2OutputOracleUpgradeable_Test::setUpyou setoracleImpl = oracleand then you call some init function on a new Proxy (let’s call it Proxy 2). Proxy 2 delegates tooracleImpl, which is now the address of Proxy 1.So by my understanding, the call to Proxy 2 will delegatecall into the code of Proxy 1. Proxy 1’s code will just execute the fallback function which looks up it’s implementation using the storage of Proxy 2. The implementation noted in the storage of Proxy 2 is Proxy 1, so this delegatecall will delegatecall into Proxy 1. This behavior then loops, because it delegatecalls into itself, using Proxy 2’s storage every time
So I don’t think this is because of prank or Forge, I think it’s because of https://github.com/maurelian/optimism-1/blob/093244521b1f0f9489cd573ad66c3c317d7ecea4/packages/contracts-bedrock/contracts/test/L2OutputOracle.t.sol#L347?
I hope that made some sense.
Edit: The reason you get a stack overflow is then because it simply does this more times than our stack can handle, where in a “real” environment you would have run out of gas
@onbjerg I believe we end up in a loop in this setup function
https://github.com/maurelian/optimism-1/blob/093244521b1f0f9489cd573ad66c3c317d7ecea4/packages/contracts-bedrock/contracts/test/L2OutputOracle.t.sol#L342-L359
SO happens in this delegate call
https://github.com/maurelian/optimism-1/blob/093244521b1f0f9489cd573ad66c3c317d7ecea4/packages/contracts-bedrock/contracts/universal/Proxy.sol#L102
this is somehow triggered by the prank call in the setup function, I believe we fixed some prank/proxy things in the past but can’t remember
note there’s another prank in the super.setup() function
reproducible via
forge test -m test_cannotInitImpl(test doesn’t matter as long it’s one ofL2OutputOracleUpgradeable_Test) SO happens in setup