foundry: bug(`forge coverage`): stack too deep when using `--ir-minimum` and coverage report is not accurate

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 (ddaf100 2022-09-24T00:05:44.382307Z)

What command(s) is the bug in?

forge coverage

Operating System

macOS (Apple Silicon)

Describe the bug

on executing forge coverage i am getting the below error

Error: 
Compiler run failed
CompilerError: Stack too deep when compiling inline assembly: Variable headStart is 1 slot(s) too deep inside the stack.

but forge test and forge build are working fine.

NOTE: I noticed also that forge build and forge test are considering the cache but forge coverage is trying to re-compile even though I have a cached successful build

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 22
  • Comments: 29 (9 by maintainers)

Commits related to this issue

Most upvoted comments

vouching for reopening this issue

AFAIK forge coverage currently compiles with the optimizer off, which is needed for better source maps. forge test (and build) work because you’re using the optimizer or via-IR which resolve the stack too deep error that’s occurring with the optimizer off (this is also why you see it recompile for coverage).

Unfortunately for now to use coverage you’ll need to refactor such that you can compile with the optimizer off / without via-IR

Hey! Same issue for me. I think the issue should be re-opened,

$ forge coverage --report lcov --ir-minimum
Warning! "--ir-minimum" flag enables viaIR with minimum optimization, which can result in inaccurate source mappings.
Only use this flag as a workaround if you are experiencing "stack too deep" errors.
Note that "viaIR" is only available in Solidity 0.8.13 and above.
See more:
https://github.com/foundry-rs/foundry/issues/3357

[⠊] Compiling...
[⠑] Compiling 265 files with 0.8.23
[⠔] Solc 0.8.23 finished in 437.62s
Error: 
Compiler run failed:
Error: Yul exception:Variable expr_13 is 1 too deep in the stack [ RET expr_13 expr expr_1 expr_3 expr_156548_address expr_156565_functionSelector expr_12 expr_156600_functionSelector expr_14 expr_156644_functionSelector expr_4 expr_156584_functionSelector expr_156624_functionSelector _70 expr_18 expr_156611_functionSelector _72 ]
memoryguard was present.
memoryguard was present.

For future readers:

This workaround is better than --ir-minimum:

https://github.com/taikoxyz/taiko-mono/pull/14489

hi, @mattsse @spockP --ir-minimum doesn’t work too

[⠒] Compiling...  
[⠆] Compiling 229 files with 0.8.22. 
[⠔] Solc 0.8.22 finished in 44.34s. 
Error: 
Compiler run failed:
Error: Yul exception:Cannot swap Variable var_fixturePoolEthereumUSDT_mpos with Variable var_fixturePoolBinanceUSDT_mpos: too deep in the stack by 3 slots in [ RET var_eidBinance var_eidBinance var_fixturePoolEthereumUSDT_mpos var_fixturePoolBinanceETH_mpos var_eidBinance var_fixturePoolBinanceETH_mpos var_fixturePoolEthereumETH_mpos var_eidEthereum var_fixturePoolEthereumUSDT_mpos var_eidEthereum var_eidEthereum var_fixturePoolEthereumETH_mpos var_fixturePoolEthereumETH_mpos var_eidEthereum var_assetETH var_assetUSDT var_eidEthereum var_eidBinance var_eidEthereum var_fixturePoolBinanceETH_mpos var_eidPolygon var_fixturePoolBinanceUSDT_mpos ]
No memoryguard was present. Consider using memory-safe assembly only and annotating it via 'assembly ("memory-safe") { ... }'.
No memoryguard was present. Consider using memory-safe assembly only and annotating it via 'assembly ("memory-safe") { ... }'.

@uranium93 structs are a neat way to solve Stack Too Deep.

I understand the reasoning for compiling without the optimizer – but I will note that this means currently any project that imports Seaport contracts directly cannot use forge coverage, as some assembly results in stack-too-deep errors.

There’s probably not much to do at the moment – but we’d love if we could encourage new projects to build on top of Seaport with Foundry (including coverage) in the future.

Any updates on this issue. When running forge coverage --via-ir and ir min I still get the stack too deep error but I dont get it with forge build or forge test

In my case --ir-minimum doesn’t work, only makes the error more sophisticated:

Compiler run failed:
Error: Yul exception:Cannot swap Variable value16 with Slot 0x14: too deep in the stack by 2 slots in [ RET value16 value15 value14 value13 value12 value11 value10 value9 value8 value7 value6 value5 value4 value3 value2 value1 value0 pos 0x14 ]
memoryguard was present.
memoryguard was present.

The solution that @aviggiano linked is for some specific blockchain as far as I can tell? Could be wrong.

Overall, I think we should reopen the issue as it’s not solved yet.

Is this issue thus nullified?

I think this is a question for @uranium93? But yes I’d say this is a known issue/limitation so we can close this

Would it be possible to have more information on where the Stack Too Deep error occurs in the source code pls?

Unfortunately not, as solc does not provide this information since it’s complex to retrieve. You can read more here: https://github.com/ethereum/solidity/issues/11638#issuecomment-1057485738

In our case the ir-minimum flag allows us to run coverage but we get a lot of false positives, coverage goes pretty down in %

Looks like there will be a remedy when solidity 0.8.21 comes out: https://github.com/ethereum/solidity/issues/13972

This is still the case, however this is not easy to resolve in forge due to the current coverage limitations (which themselves are due to solc limitations). The easiest solution is to update your code to avoid stack too deep issues

Hey @Rubilmax , for me the only fix that was possible and fast is to re-write the affected tests with hardhat