fix(evm): don't gate deleteStateSnapshot on backend init in CowBackend - #16640
Open
gomesalexandre wants to merge 2 commits into
Open
fix(evm): don't gate deleteStateSnapshot on backend init in CowBackend#16640gomesalexandre wants to merge 2 commits into
gomesalexandre wants to merge 2 commits into
Conversation
vm.deleteStateSnapshot / vm.deleteStateSnapshots silently returned false / no-op for a snapshot that genuinely exists, whenever it's the first cheatcode call of a fuzz run - e.g. a snapshot taken in setUp(). CowBackend::delete_state_snapshot gated access via initialized_backend_mut(), which returned None while pending_init was still Some (i.e. before any mutating cheatcode had run in the current fuzz iteration). But pending_init only tracks deferred Backend::initialize() setup, not whether a snapshot exists - a snapshot taken in a prior, separate execution is already present in the deep-cloned state_snapshots map regardless. Fix: check existence on the possibly-still-borrowed backend via the read-only state_snapshots() accessor before calling backend_mut(), so a miss never forces an unnecessary clone and a hit still gets a correct answer. Same pattern applied to the plural delete_state_snapshots(), using a new StateSnapshots::is_empty(). Removed the now-dead initialized_backend_mut() helper.
gomesalexandre
requested review from
0xrusowsky,
DaniPopes,
grandizzy and
mattsse
as code owners
September 4, 2026 20:28
gomesalexandre
requested review from
figtracer,
mablr and
stevencartavia
as code owners
September 4, 2026 20:28
Contributor
✅ Changelog foundThe deterministic check will validate the changed entry. |
figtracer
approved these changes
Sep 4, 2026
figtracer
enabled auto-merge (squash)
September 4, 2026 23:23
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
`vm.deleteStateSnapshot`/`vm.deleteStateSnapshots` silently return `false`/no-op for a snapshot that genuinely exists, whenever the delete is the FIRST cheatcode call of a test/fuzz run - e.g. a snapshot taken in `setUp()`:
Same operation, same snapshot id, and the answer flips depending on unrelated prior cheatcode history within the run. `vm.deleteStateSnapshots()` (delete-all) is `void`-returning, so its no-op failure mode was completely silent - no assertion could ever catch it.
Per the spec (`crates/cheatcodes/spec/src/vm.rs`), `deleteStateSnapshot` should return `false` only if the snapshot does not exist - it does exist here.
Root cause
`CowBackend::delete_state_snapshot` (`crates/evm/core/src/backend/cow.rs`) gated backend access via `initialized_backend_mut()`, which returned `None` whenever `pending_init` was still `Some` - i.e. before ANY mutating cheatcode had run in the current call. But `pending_init` only tracks whether the deferred `Backend::initialize()` setup call has happened yet; it has nothing to do with whether a snapshot exists. A snapshot taken in a prior, separate execution (like `setUp()`) is already present in the deep-cloned `state_snapshots` map regardless of `pending_init`. `revert_state`, a sibling method, proves this - it goes through `backend_mut()` unconditionally and correctly finds the same snapshot id.
Fix
Check existence on the possibly-still-`Cow::Borrowed` backend first, via the read-only `state_snapshots()` accessor - no clone triggered - and only call `backend_mut()` (which clones-on-write and may run the deferred `initialize()`) when there's actually something to remove. This preserves the original intent of avoiding an unnecessary clone on a fuzz iteration that never really mutates the backend, while fixing the false-negative on a genuine hit. Same pattern applied to `delete_state_snapshots()`, backed by a new `StateSnapshots::is_empty()` accessor. The now-unused `initialized_backend_mut()` helper (its only two callers were these two methods) was removed as dead code.
Testing
receipts
no runtime UI change - the Solidity fixture's real red-before-green run against the built `forge` binary is the receipt for this PR (cheatcode-level fix, not a runnable app).