argotorg / argotorg/solidity

forge coverage Fails with 'Stack Too Deep' Error When Using --via-ir, While forge test Works Fine

Open
#15,775 2 comments 0 reactions 0 assignees View on GitHub
bug :bug:
Dominant language
C++
Stars
25.7k
Forks
6.2k
Avg merge
1d 11h
Merged PRs (30d)
21

Description

## Description

When running `forge coverage` with the `--via-ir` flag, the compilation fails with a "stack too deep" error. Trying the `--ir-minimum` flag as a workaround, as suggested in the documentation, results in additional errors related to unsupported VM versions and instructions, even when using the latest Solidity compiler.

## Environment

- Compiler version: `0.8.28`
- Compilation pipeline: `IR`
- Target EVM version: `default`
- Framework/IDE: `Foundry`
- EVM execution environment / backend / blockchain client: `default`
- Operating system: `Fedora`

## Steps to Reproduce

1. Add the following configuration to `foundry.toml`:

```toml
[profile.default]
src = "src"
out = "out"
libs = ["lib"]

remappings = [
"@account-abstraction/=lib/account-abstraction/contracts",
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts",
"@foundry-era-contracts/=lib/foundry-era-contracts/src/system-contracts/contracts",
]

# i've tried comment and uncomment to rely solely on the --via-ir CLI command. The error persists in both scenarios.
# via-ir = true
# optimizer = true
# optimizer_runs = 2000
```

2. Run `forge coverage --via-ir`.
Observe the following error:

```bash
Error: Compiler run failed:
Error: Compiler error (/solidity/libyul/backends/evm/AsmCodeGen.cpp:63):
CompilerError: Stack too deep. Try compiling with `--via-ir` (cli) or the equivalent
`viaIR: true` (standard JSON) while enabling the optimizer. Otherwise, try removing local variables.
```

3. Run `forge coverage --ir-minimum` as a workaround.
Observe the following errors:

```bash

Warning (3420): Source file does not specify required compiler version! Consider adding "pragma solidity ^0.8.28;"
--> test/zkSync/ZkMinimalAccount.t.sol

Error (5921): "basefee" is not supported by the VM version.
--> lib/account-abstraction/contracts/core/UserOperationLib.sol:46:61:
|
46 | return min(maxFeePerGas, maxPriorityFeePerGas + block.basefee);
| ^^^^^^^^^^^^^

Error (5921): "basefee" is not supported by the VM version.
--> lib/account-abstraction/contracts/core/EntryPoint.sol:664:61:
|
664 | return min(maxFeePerGas, maxPriorityFeePerGas + block.basefee);
| ^^^^^^^^^^^^^

Error (7755): The "mcopy" instruction is only available for Cancun-compatible VMs (you are currently compiling for "istanbul").
--> lib/openzeppelin-contracts/contracts/utils/Bytes.sol:96:13:
|
96 | mcopy(add(result, 0x20), add(buffer, add(start, 0x20)), sub(end, start))
| ^^^^^

```
4. Test with `forge test --via-ir` to confirm that it works without issues.

```bash
forge test --via-ir
[⠊] Compiling...
No files changed, compilation skipped

Ran 13 tests for test/Paymaster.t.sol:PaymasterTest
[PASS] test_If_ValidatePayMasterUserOp_Returns_One_On_InvalidSignature_Then_Context_Is_Empty() (gas: 743328)
[PASS] test_If_ValidatePayMasterUserOp_Returns_Zero_On_ValidSignature_Then_Context_Is_Empty() (gas: 1676764)
[PASS] test_If_ValidatePayMasterUserOp_Returns_Zero_UserAddedToMapping_Then_Context_Is_Empty() (gas: 778492)
[PASS] test_onlyEntryPoint_CanCall_validatePaymasterUserOp() (gas: 726396)
[PASS] test_paymaster_addAddressToPaymaster_onlyOwnerCanCall() (gas: 16367)
[PASS] test_paymaster_addAddressToPaymaster_reverts_when_address_is_zero() (gas: 11147)
[PASS] test_paymaster_addAddress_emits_event() (gas: 38011)
[PASS] test_paymaster_entryPoint_CanCall_validatePaymasterUserOp() (gas: 742596)
[PASS] test_paymaster_getIfSignatureIsValid_returns_one_on_Invalidsignature() (gas: 742678)
[PASS] test_paymaster_getIfSignatureIsValid_returns_zero_on_validsignature() (gas: 1673057)
[PASS] test_paymaster_getUserOpHashWithoutPaymaster_returns_correct_hash() (gas: 732824)
[PASS] test_paymaster_onlyEntryPoint_CanCall_validatePaymasterUserOp() (gas: 726088)
[PASS] test_paymaster_updates_mapping_when_addAddressToPaymaster_is_called() (gas: 37568)
Suite result: ok. 13 passed; 0 failed; 0 skipped; finished in 22.43ms (30.18ms CPU time)
```

Contributor guide

Open the contributing guide

Research direction

Start by reproducing `forge coverage --via-ir` and compare it with the successful `forge test --via-ir` run using the configuration shown in `foundry.toml`. Then investigate the compiler's IR and coverage paths, including the `--ir-minimum` behavior and the reported VM-version errors. Done means coverage compiles successfully with the reported Solidity 0.8.28 setup without the stack-too-deep failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
solidity
Domain
blockchain, compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.