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

bug(coverage): with --ir-minimum enabled is showing wrong readings on require statements #8695

Closed
2 tasks done
ChaituKReddy opened this issue Aug 19, 2024 · 12 comments
Closed
2 tasks done
Labels
C-forge Command: forge Cmd-forge-coverage Command: forge coverage T-bug Type: bug
Milestone

Comments

@ChaituKReddy
Copy link

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 (8549aad 2024-08-19T00:23:37.767039000Z)

What command(s) is the bug in?

forge coverage --ir-minimum

Operating System

macOS (Apple Silicon)

Describe the bug

From the counter contract example, the following code snippet and the test case for it yields 100% coverage with just

forge coverage

function addNum(uint256 _num) public {
     require(_num > 0, "Zero Number");
     number += _num;
}
function testAddNum() public {
        vm.expectRevert("Zero Number");
        counter.addNum(0);

        counter.addNum(1);
}
image

But the same with forge coverage --ir-minimum yields below report


image

The report is attached below

image

I'm trying to use the --ir-minimum in my project code base where I'm facing the same issue, this is a sample test to reproduce the issue.

@ChaituKReddy ChaituKReddy added T-bug Type: bug T-needs-triage Type: this issue needs to be labelled labels Aug 19, 2024
@zerosnacks
Copy link
Member

Hi @ChaituKReddy thanks for flagging this. Due to the AST transformation the via-ir pipeline performs this functionality is available as-is as stated. That said I think it is important that we keep track of issues, it is just that there likely is no structural solution yet in the short term.

Related: #3527

@zerosnacks zerosnacks added Cmd-forge-coverage Command: forge coverage C-forge Command: forge and removed T-needs-triage Type: this issue needs to be labelled labels Aug 20, 2024
@zerosnacks zerosnacks changed the title forge coverage with --ir-minimum enabled is showing wrong readings on require statements bug(forge coverage with --ir-minimum enabled is showing wrong readings on require` statements Aug 20, 2024
@zerosnacks zerosnacks changed the title bug(forge coverage with --ir-minimum enabled is showing wrong readings on require` statements bug(coverage): with --ir-minimum enabled is showing wrong readings on require statements Aug 20, 2024
@zerosnacks zerosnacks added this to the v1.0.0 milestone Aug 20, 2024
@VGabriel45
Copy link

@zerosnacks does this mean that using Solidity "0.8.26" with via-ir and "forge coverage" will output invalid coverage report ?
I am getting some unexpected behaviour when running "forge coverage --ir-minimum" in a sense that I don't get any error but instead some foundry tests are included in the coverage, un-covered lines output in the table are not correct and more.

@zerosnacks
Copy link
Member

@zerosnacks does this mean that using Solidity "0.8.26" with via-ir and "forge coverage" will output invalid coverage report ? I am getting some unexpected behaviour when running "forge coverage --ir-minimum" in a sense that I don't get any error but instead some foundry tests are included in the coverage, un-covered lines output in the table are not correct and more.

via-ir always applies some methods of optimization which invalidates the AST making it very unreliable. -ir-minimum attempts to apply the least amount of optimizations but it is still at a best attempt and issues are to be expected. It will likely require a significant rewrite of forge coverage post 1.0 with a different instrumentation insertions that can persist through the optimizations to address this.

@DaniPopes
Copy link
Member

cc @grandizzy

In general compiler optimizations should not be used at all for coverage for various reasons. There are no plans to support via-ir. We provide ir-minimum because of stack-too-deep errors, but it should be avoided when possible.

@VGabriel45
Copy link

cc @grandizzy

In general compiler optimizations should not be used at all for coverage for various reasons. There are no plans to support via-ir. We provide ir-minimum because of stack-too-deep errors, but it should be avoided when possible.

How can one run "forge coverage" with Solidity's version 0.8.26 and using the new require statement with custom errors sugar syntax in the contracts ?

@zerosnacks
Copy link
Member

zerosnacks commented Aug 28, 2024

cc @grandizzy
In general compiler optimizations should not be used at all for coverage for various reasons. There are no plans to support via-ir. We provide ir-minimum because of stack-too-deep errors, but it should be avoided when possible.

How can one run "forge coverage" with Solidity's version 0.8.26 and using the new require statement with custom errors sugar syntax in the contracts ?

That is unfortunately a via-ir only feature therefore not compatible with forge coverage, ref: https://soliditylang.org/blog/2024/05/21/solidity-0.8.26-release-announcement/

@tmigone
Copy link

tmigone commented Aug 30, 2024

@VGabriel45 did you find a workaround? I'm facing the exact same problem.

@ChaituKReddy
Copy link
Author

@tmigone

Based on my understanding, unless you're encountering a stack too deep issue, it's recommended not to use the --ir-minimum flag with forge coverage.

Instead, consider either:

  1. Refactoring your code by replacing require statements with custom errors and using revert to handle exceptions, or
  2. Waiting for future Solidity versions that will natively support require with custom errors, eliminating the need for the --via-ir flow.

This approach should help with maintaining coverage accuracy.

@VGabriel45
Copy link

@VGabriel45 did you find a workaround? I'm facing the exact same problem.

What we did is we created a side-branch with the same code but without the new require statement with custom errors which was forcing us to use via-ir in the main branch, now we are getting the coverage results for the code but this is a temporary work-around ... untill the Solidity devs do something about it.

@tmigone
Copy link

tmigone commented Aug 30, 2024

Waiting for future Solidity versions that will natively support require with custom errors, eliminating the need for the --via-ir flow.

@ChaituKReddy is there any indication for how long this could take? Do you know if it's been worked on?

@VGabriel45 thanks for your suggestion, seems like very cumbersome it's unfortunate that we have to resort to these techniques 😞

@zerosnacks
Copy link
Member

That is unfortunately a via-ir only feature therefore not compatible with forge coverage, ref: https://soliditylang.org/blog/2024/05/21/solidity-0.8.26-release-announcement/

Update: 0.8.27 adds support for custom errors in require to the legacy pipeline: https://soliditylang.org/blog/2024/09/04/solidity-0.8.27-release-announcement/

@jenpaff
Copy link
Collaborator

jenpaff commented Sep 24, 2024

duplicate of #8840

@jenpaff jenpaff closed this as completed Sep 24, 2024
@jenpaff jenpaff reopened this Sep 24, 2024
@jenpaff jenpaff closed this as not planned Won't fix, can't repro, duplicate, stale Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-forge Command: forge Cmd-forge-coverage Command: forge coverage T-bug Type: bug
Projects
None yet
Development

No branches or pull requests

6 participants