foundry icon indicating copy to clipboard operation
foundry copied to clipboard

vm.pauseGasMetering() bricks construction of contracts with external calls

Open 3docSec opened this issue 1 year ago • 1 comments

Component

Forge

Have you ensured that all of these are up to date?

  • [X] Foundry
  • [X] Foundryup

What version of Foundry are you on?

forge 0.2.0 (b0b8cfb 2023-10-18T00:17:29.353012000Z)

What command(s) is the bug in?

forge test

Operating System

macOS (Apple Silicon)

Describe the bug

The below test contract shows how the creation of SomeContract succeeds with gas metering enabled, but fails when gas metering is disabled.

This applies to both CREATE and CREATE2 opcodes.

pragma solidity ^0.8.20;

import {Test} from "forge-std/Test.sol";

contract BugTest is Test {

    address public something;

    function testNoBug() public {
        new SomeContract();
    }

    function testFailBug() public {
        vm.pauseGasMetering();
        new SomeContract();
    }
}

contract SomeContract {
    constructor() {
        BugTest(msg.sender).something();
    }
}

Extra-verbose output is:

$ forge test -vvvv
[⠢] Compiling...
No files changed, compilation skipped

Running 2 tests for test/Counter.t.sol:BugTest
[PASS] testFailBug() (gas: 9223372036854754743)
Traces:
  [0] BugTest::testFailBug()
    ├─ [0] VM::pauseGasMetering()
    │   └─ ← ()
    ├─ [0] → new <Unknown>@0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f
    │   ├─ [0] BugTest::something() [staticcall]
    │   │   └─ ← 0x0000000000000000000000000000000000000000
    │   └─ ← 0 bytes of code
    └─ ← "EvmError: Revert"

[PASS] testNoBug() (gas: 47801)
Traces:
  [47801] BugTest::testNoBug()
    ├─ [15457] → new SomeContract@0x5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72f
    │   ├─ [2382] BugTest::something() [staticcall]
    │   │   └─ ← 0x0000000000000000000000000000000000000000
    │   └─ ← 63 bytes of code
    └─ ← ()

Test result: ok. 2 passed; 0 failed; 0 skipped; finished in 344.46µs

Ran 1 test suites: 2 tests passed, 0 failed, 0 skipped (2 total tests)

The full project can be found in this public repo. LMK if I can provide more info 🤗

3docSec avatar Oct 18 '23 13:10 3docSec