foundry: compiling tests `via-ir` causes issues with warp & `block.timestamp` stored in memory
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 (276a122 2022-04-20T18:04:32.540251+00:00)
What command(s) is the bug in?
forge test
Operating System
macOS (amd)
Describe the bug
Can explore with this repo on commit b2dd8cfafa79d02394826e8f01138e849d78f784
forge installmake test- see tests passing
- update
foundry.tomlto buildvia-ir = true forge test -vvv --match-test createERC20- see test failing
the error appears to be with vestingStart, which is block.timestamp saved to memory at the beginning & later tested against. It appears to jump forward with the warp. My guess (not confirmed browsing opcode output) is that via-ir is trying to save me gas by skipping the m{store,load} for the timestamp (bc in regular contracts block timestamps don’t change w/i execution of a fn) & just re-loading block.timestamp freshly on later reference
(to debug this I had to use emit log_uint – couldn’t seem to get console2 or console to output to trace… but that’s a separate topic)
fortunately this is easy enough to fix in my own case since vestingStart is only ever 0 how/where I was currently using it (and via-ir doesn’t appear to save any gas for these contracts)
wanted to flag for others in the meantime
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 3
- Comments: 15 (5 by maintainers)
Commits related to this issue
- Set via_ir to false https://github.com/foundry-rs/foundry/issues/1373 — committed to InverterNetwork/contracts by FHieser 3 years ago
- Set via_ir to false https://github.com/foundry-rs/foundry/issues/1373 — committed to InverterNetwork/contracts by FHieser 3 years ago
- Set via_ir to false https://github.com/foundry-rs/foundry/issues/1373 — committed to InverterNetwork/contracts by FHieser 3 years ago
- Setup Script: PR for Issue 183 (#242) * fix URLs * Add PaymentManager * format * Added deployment scripts for first 3 contracts * Having fun with the OneScriptToRuleThemAll, lol * Exac... — committed to InverterNetwork/contracts by saxenism 3 years ago
- fix: use `vm.getBlockTimestamp` for `skip` and `rewind` (#634) Given that during `via-ir` compilations, the `block.timestamp` variable might be distorted due to optimizations, the more correct way t... — committed to foundry-rs/forge-std by xenide 2 years ago
- fix: use `vm.getBlockTimestamp` for `skip` and `rewind` (#634) Given that during `via-ir` compilations, the `block.timestamp` variable might be distorted due to optimizations, the more correct way t... — committed to thomas-lamb-tech/std_forge by xenide 2 years ago
These cheatcodes could potentially be useful:
https://github.com/foundry-rs/forge-std/blob/master/src/Vm.sol#L577-L587
I’m not surethis is a Foundry issue. When you call
vm.warp, then the block’s timestamp is set to what you pass towarpfor the remainder of the test untilvm.warpis called with a new value. For that reason, if Solidity optimized things by callingblock.timestampagain, you would see whatever you passed towarp.It might be that Solidity instead optimizes by putting
block.timestampon the stack before it is actually called and then does a pop or other stack operation later. This is sound for normal Solidity sinceblock.timestampis immutable under normal circumstances, but obviously breaks things for us.For example this is what I imagine is happening (pseudo-code):
The only real fix we could do is track the location of stack items that have been produced by
block.timestamp(and other immutables too likeblock.number, further complicating things), however, this would break tests like:I’m not really sure what we should do here. Pinging @hrkrshnn and @gakonst
Hi guys, I’m facing the same on 0.8.27
@onbjerg Might be possible to config the compiler so that optimization is mostly off but “stack too deep” doesn’t happen.
This happens whenever you define a custom
optimizerSteps=setting I think, just observed the same results as detailed hereI’m facing this in 0.8.28.
In this case
lStartTimegets arbitrarily modified and therefore the two console logs output different values even though there is no modification tolStartTimeThe workaround I managed to find was to do:
Since timestamp is way smaller than 256 bits i guess this is safe enough in my case.
Right, I think I had it confused; what is actually happening is that you replace all occurrences of a variable that just contains
block.timestampwithblock.timestamp(which is a fair optimization but muddies things for us). See this example:Not sure if we should re-open @gakonst, I don’t know if there is anything we can do on our side. Any way to get around this @hrkrshnn?
Thanks for flagging @wminshew, closing given:
Good point. I think it’s easiest to fix this: made https://github.com/ethereum/solidity/issues/12963 to track. @onbjerg
ye so likely we will need to provide a helper function here that blackboxes it from the compiler. @hrkrshnn what do you think about going more general and native with a blackbox inside solidity?