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

Capacity Overflow During Invariant Testing #6666

Closed
2 tasks done
DanielGelfand opened this issue Dec 25, 2023 · 4 comments · Fixed by #6819
Closed
2 tasks done

Capacity Overflow During Invariant Testing #6666

DanielGelfand opened this issue Dec 25, 2023 · 4 comments · Fixed by #6819
Labels
T-bug Type: bug

Comments

@DanielGelfand
Copy link

DanielGelfand commented Dec 25, 2023

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 (67ab870 2023-12-25T00:25:37.702950000Z)

What command(s) is the bug in?

forge test

Operating System

macOS (Apple Silicon)

Describe the bug

[⠆] Compiling...
No files changed, compilation skipped
The application panicked (crashed).
Message:  capacity overflow
Location: library/alloc/src/raw_vec.rs:534

This is a bug. Consider reporting it at https://github.com/foundry-rs/foundry

  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ BACKTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
   1: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   2: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   3: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   4: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   5: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   6: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   7: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   8: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   9: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  10: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  11: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  12: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  13: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  14: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  15: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  16: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  17: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  18: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  19: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  20: __pthread_deallocate<unknown>
      at <unknown source file>:<unknown line>

During invariant testing (with a fork), if I were to add a console log or an extra operation such as a simple balanceOf the error capacity overflow would show up. I would then have to play with removing/rearranging certain lines or modifying runs/depth to get the invariant test to run.

@DanielGelfand DanielGelfand added the T-bug Type: bug label Dec 25, 2023
@gakonst gakonst added this to Foundry Dec 25, 2023
@github-project-automation github-project-automation bot moved this to Todo in Foundry Dec 25, 2023
@aviggiano
Copy link

aviggiano commented Dec 28, 2023

I am also experiencing this issue with an invariant test.
My setup does not involve a fork

Update: FYI I changed assertTrue to assert and the issue went away

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 (nightly-67ab8704476d55e47545cf6217e236553c427a80)

What command(s) is the bug in?

forge test

Operating System

macOS (Apple Silicon)

Describe the bug

[I] ➜ RUST_BACKTRACE=full COLORBT_SHOW_HIDDEN=1 forge test --mc FoundryTester
[⠆] Compiling...
No files changed, compilation skipped
The application panicked (crashed).
Message:  capacity overflow
Location: library/alloc/src/raw_vec.rs:534

This is a bug. Consider reporting it at https://github.com/foundry-rs/foundry

  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ BACKTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
   1: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   2: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   3: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   4: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   5: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   6: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   7: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   8: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   9: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  10: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  11: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  12: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  13: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  14: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  15: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  16: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  17: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  18: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  19: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  20: __pthread_joiner_wake<unknown>
      at <unknown source file>:<unknown line>

Run with COLORBT_SHOW_HIDDEN=1 environment variable to disable frame filtering.
zsh: abort      RUST_BACKTRACE=full COLORBT_SHOW_HIDDEN=1 forge test --mc FoundryTester

@zaqk
Copy link

zaqk commented Jan 5, 2024

I'm on an M1 macOS (Apple Silicon).

If I change my assertion from assertGt to assertGe the error goes away.

Getting this error as well:

▶ RUST_BACKTRACE=full forge test --mp test/invariant/MyTest.invariant.t.sol
[⠒] Compiling...No files changed, compilation skipped
[⠢] Compiling...
The application panicked (crashed).
Message:  capacity overflow
Location: library/alloc/src/raw_vec.rs:545

This is a bug. Consider reporting it at https://github.com/foundry-rs/foundry

  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ BACKTRACE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
   1: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   2: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   3: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   4: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   5: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   6: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   7: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   8: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
   9: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  10: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  11: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  12: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  13: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  14: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  15: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  16: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  17: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  18: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  19: __mh_execute_header<unknown>
      at <unknown source file>:<unknown line>
  20: __pthread_joiner_wake<unknown>
      at <unknown source file>:<unknown line>

Run with COLORBT_SHOW_HIDDEN=1 environment variable to disable frame filtering.
[1]    87586 abort      RUST_BACKTRACE=full forge test --mp test/invariant/MyTest.invariant.t.sol

If I move a specific invariant test it goes away. This invariant test is being inherited from an abstract contract into two different test contracts. One of the test contracts works. The other does not.

@m-waqas88
Copy link

Versions:

forge 0.2.0 (24abca6 2024-01-16T00:28:45.101177000Z)
cast 0.2.0 (24abca6 2024-01-16T00:28:45.162187000Z)
anvil 0.2.0 (24abca6 2024-01-16T00:28:45.105452000Z)
chisel 0.2.0 (24abca6 2024-01-16T00:28:45.523635000Z)

I am also experiencing a similar error, when I am trying to play with the variables in assertGe statement.

The application panicked (crashed).
Message:  capacity overflow
Location: library/alloc/src/raw_vec.rs:545

This is a bug. Consider reporting it at https://github.com/foundry-rs/foundry

Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.
zsh: abort      forge test --match-path test/Invariant_2.t.sol

I am using below command
forge test --match-path test/Invariant_2.t.sol

Below are my code files

Invariant_2.t.sol

pragma solidity ^0.8.22;

import "forge-std/Test.sol";
import {Handler} from "../src/InvariantHandler.sol";
import {WETH} from "../src/WETH.sol";

contract InvariantHandlerTest is Test {
    WETH private weth;
    Handler private handler;

    function setUp() public {
        weth = new WETH();
        handler = new Handler(weth);
        deal(address(handler), 100 ether);
        targetContract(address(handler));
    }

    function invariant_WETH_ETH_balance_should_be_greater_than_or_equal_to_eth_deposited_by_Hanlder() public {
        assertGe(address(weth).balance, weth.balanceOf(address(handler)) + 5);
    }

}

When I add +5 in the assertGe statement then this error is thrown. Otherwise it works fine.

InvariantHandler.sol

pragma solidity ^0.8.22;

import {WETH} from "./WETH.sol";
import {CommonBase} from "forge-std/Base.sol";
import {StdCheats} from "forge-std/StdCheats.sol";
import {StdUtils} from "forge-std/StdUtils.sol";

contract Handler is CommonBase, StdCheats, StdUtils {
    WETH private weth;

    constructor(WETH _weth) {
        weth = _weth;
    }

    receive() external payable {}

    function deposit(uint256 _amount) public {
        _amount = bound(_amount, 0, address(this).balance);
        weth.deposit{value: _amount}();
    }

    function withdraw(uint256 _amount) public {
        _amount = bound(_amount, 0, weth.balanceOf(address(this)));
        weth.withdraw(_amount);
    }

    function sendToFallback(uint256 _amount) public {
        _amount = bound(_amount, 0, address(this).balance);
        (bool success,) = address(weth).call{value: _amount}("");
        require(success, "ETH send to fallback failed");
    }

}

WETH.sol

pragma solidity ^0.8.22;

contract WETH {
    string public name = "Wrapped Ether";
    string public symbol = "WETH";
    uint8 public decimals = 18;

    event Approval(address indexed src, address indexed guy, uint256 wad);
    event Transfer(address indexed src, address indexed dst, uint256 wad);
    event Deposit(address indexed dst, uint256 wad);
    event Withdrawal(address indexed src, uint256 wad);

    mapping(address => uint256) public balanceOf;
    mapping(address => mapping(address => uint256)) public allowance;

    receive() external payable {
        deposit();
    }

    function deposit() public payable {
        balanceOf[msg.sender] += msg.value;
        emit Deposit(msg.sender, msg.value);
    }

    function withdraw(uint256 wad) public {
        balanceOf[msg.sender] -= wad;
        payable(msg.sender).transfer(wad);
        emit Withdrawal(msg.sender, wad);
    }

    function totalSupply() public view returns (uint256) {
        return address(this).balance;
    }

    function approve(address guy, uint256 wad) public returns (bool) {
        allowance[msg.sender][guy] = wad;
        emit Approval(msg.sender, guy, wad);
        return true;
    }

    function transfer(address dst, uint256 wad) public returns (bool) {
        return transferFrom(msg.sender, dst, wad);
    }

    function transferFrom(address src, address dst, uint256 wad)
    public
    returns (bool)
    {
        if (
            src != msg.sender && allowance[src][msg.sender] != type(uint256).max
        ) {
            allowance[src][msg.sender] -= wad;
        }

        balanceOf[src] -= wad;
        balanceOf[dst] += wad;

        emit Transfer(src, dst, wad);

        return true;
    }
}

@mattsse
Copy link
Member

mattsse commented Jan 16, 2024

tysm for the repro @m-waqas88 !

with that I was able to track this down #6819

of course #6666 is a very cursed issue -.-

@github-project-automation github-project-automation bot moved this from Todo to Done in Foundry Jan 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-bug Type: bug
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

5 participants