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

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

Open
Blockchain-Oracle opened this issue Jan 25, 2025 · 2 comments
Labels

Comments

@Blockchain-Oracle
Copy link

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:

    [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:

    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:

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))
   |             ^^^^^

  1. Test with forge test --via-ir to confirm that it works without issues.
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)
@cameel
Copy link
Member

cameel commented Jan 27, 2025

As for forge coverage --via-ir, see my response in the other issue: #13972 (comment). Tl;dr is that this is not a simple bug we can fix right now. Our current Yul->EVM transform is just still not good enough to overcome the inherent EVM limitation of having only 16 slots available at a time. Which is being worked on but may also eventually become irrelevant due to EOF.

As such, I'd be inclined to close this, because there's nothing we can do specifically for forge coverage and there are already many reports of such StackToDeep cases.

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.

This could be a problem with project configuration or a bug in forge. These errors are the expected result when you're compiling Cancun code and request code generation for a pre-Cancun EVM. The question is why an older EVM is being selected - but that does not look like a problem with solc itself.

@DaniPopes
Copy link
Contributor

--via-ir in forge coverage is a noop, and it now issues a warning. The errors you encounter with --ir-minimum are due to unspecified solc version in your config, fixed in foundry-rs/foundry#9768.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants