Skip to content

Invariants: Allow targetContract(address(this)); for contracts that use targetFunctions #10253

@GalloDaSballo

Description

@GalloDaSballo

Component

Forge

Describe the feature you would like

Feature Request

Allow the usage of targetContract(address(this)); for Invariant Testing

Example

Allow:

https://github.com/Recon-Fuzz/create-chimera-app/blob/0294f60d1f31013923cbe2a0fd92c74e2b359a5e/test/recon/CryticToFoundry.sol#L13-L19

contract CryticToFoundry is Test, TargetFunctions, FoundryAsserts {
    function setUp() public {
        setup();

        targetContract(address(this));
    }

to work

Example Repro: https://github.com/GalloDaSballo/foundry-targetContract-repro

Context

An extremely common way of writing invariant tests uses Target Function, explicit functions that tell the fuzzer how to explore state

Source:
https://github.com/Recon-Fuzz/create-chimera-app (Recon)
https://github.com/perimetersec/arachne (PerimeterSec)
https://github.com/euler-xyz/euler-vault-kit/tree/master/src (Enigma Dark)
https://github.com/CodeHawks-Contests/2025-02-gamma/blob/main/test/fuzzing/echidna/CryticToFoundry.sol (Guardian Audits)

As it stands targetFunctions doesn't allow passing address(this)

This prevents these testing suites from being easily reusable with Foundry

Current logs:

Encountered 2 failing tests in test/recon/CryticToFoundry.sol:CryticToFoundry
[FAIL: failed to set up invariant testing environment: No contracts to fuzz.] invariant_count_never_gt_0() (runs: 0, calls: 0, reverts: 0)
[FAIL: failed to set up invariant testing environment: No contracts to fuzz.] invariant_number_never_zero() (runs: 0, calls: 0, reverts: 0)

Request

Allow us to pass targetContract(address(this)); which will allow us to use the same suite with no changes

Additional context

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Completed

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions