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

Strange block.timestamp / warp interactions with via-ir #3806

Closed
1 of 2 tasks
marktoda opened this issue Dec 1, 2022 · 7 comments
Closed
1 of 2 tasks

Strange block.timestamp / warp interactions with via-ir #3806

marktoda opened this issue Dec 1, 2022 · 7 comments
Labels
C-forge Command: forge Cmd-forge-test Command: forge test T-bug Type: bug

Comments

@marktoda
Copy link
Contributor

marktoda commented Dec 1, 2022

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 (f6361e6 2022-12-01T15:11:38.140433224Z)

What command(s) is the bug in?

forge test

Operating System

Linux

Describe the bug

Block.timestamp and warp seem to have very strange behavior under via-ir. Minimal repro:

foundry.toml:

  • via_ir = true
    function testWarp() public {
        console2.log("timestamp first", block.timestamp);
        vm.warp(block.timestamp + 100);
        console2.log("timestamp, timestamp + 100", block.timestamp, block.timestamp + 100);
    }

Expected output:

timestamp first: 1
timestamp, timestamp + 100: 101 201

Actual output:

timestamp first: 1
timestamp, timestamp + 100: 101 101

Super strange - I have no idea why the addition would not apply to block.timestamp

Another repro showing that stack variables get clobbered

    function testWarp() public {
        uint256 timestamp = block.timestamp;
        console2.log("timestamp before", timestamp);
        vm.warp(timestamp + 50);
        console2.log("timestamp after", timestamp);
    }

Expected output:

timestamp before 1
timestamp after 1

Actual output:

timestamp before 1
timestamp after 51
@marktoda marktoda added the T-bug Type: bug label Dec 1, 2022
@marktoda
Copy link
Contributor Author

marktoda commented Dec 1, 2022

oh also note that setting via_ir = false fixes the issue. Removing the warp also fixes the issue

@marktoda
Copy link
Contributor Author

marktoda commented Dec 1, 2022

#1373

seems compiler is seeing my block.timestamp + 100 pre-warp and autoresolving my block.timestamp + 100 post-warp to the same value

@marktoda
Copy link
Contributor Author

marktoda commented Dec 1, 2022

Can close i suppose but this is super strange behavior :(

@marktoda marktoda closed this as completed Dec 1, 2022
@marktoda
Copy link
Contributor Author

marktoda commented Dec 1, 2022

Any way we could remove whatever optimizerStep that does this clobbering for test/ contracts?

@PaulRBerg
Copy link
Contributor

PaulRBerg commented Dec 21, 2022

@marktoda did you find any workaround, after all?

I just enabled --via-ir and bumped into the same problem as you did here .. it looks like there is now an open issue in the Solidity repo (ethereum/solidity#12963) to track this strange behavior of Solidity, but I am not quite sure what I should do in the meantime? I would prefer not to disable --via-ir.

Did you, by chance, override the optimizerSteps in your Foundry config? I noticed that if I set it to either `` or dhfoDgvulfnTUtnIf, I get the strange behavior reported by you. But if I remove this configuration completely, the `warp` cheatcode works as expected.

@marktoda
Copy link
Contributor Author

Hey @PaulRBerg !

Honestly my solution was just to redesign the test suite to get around this issue. In our case, changing some constants around to re-evaluate block.timestamp in different expressions allowed the value to change properly, i.e.

uint256 startTimestamp = block.timestamp;
... startTimestamp + 10 // 11
uint256 newTimestamp = startTimestamp + 100); // 101
warp(newTimestamp)
... startTimestamp + 50  // 51

I did tinker around with optimizerSteps, but couldn't isolate the correct step easily and gave up to do the hacky way out :D

@PaulRBerg
Copy link
Contributor

Thanks for the tips, @marktoda. I will probably end up doing the same, i.e. introduce a few new constants to go around this whacky behavior of warp.

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-test Command: forge test T-bug Type: bug
Projects
None yet
Development

No branches or pull requests

3 participants