watch: clear stale JOURNAL.written entries on title_memory_remapped (#102) #112

Open
adritoyalser wants to merge 2 commits from upstream-pr/112 into main
adritoyalser commented 2026-11-25 14:24:41 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by adritoyalser on 2026-11-25T14:24:41Z.


Summary

Fixes #102.

Root Cause

watch::title_memory_remapped(at, len) cleared guarded-block state (protected, clean, holes, aliased), bumped REGION_EPOCHS, and invalidated shadows/placed textures/bindless views, but never removed pages in [at, at + len) from the process-global JOURNAL.written map.

When texture::tests::warp_write_watched_texture_uploads_only_when_written ran before watch::tests::written_pages_are_reported_to_every_asker in a serial run:

  1. warp_write_watched_texture_uploads_only_when_written allocated a MEM_WRITE_WATCH buffer via VirtualAlloc, wrote to it, recorded (raw / PAGE, stamp) in JOURNAL.written via watch::writes, and called VirtualFree without clearing JOURNAL.written.
  2. written_pages_are_reported_to_every_asker allocated a fresh MEM_WRITE_WATCH buffer via VirtualAlloc, Windows reused the just-freed 64 KiB-aligned virtual address, and watch::writes(start, end, false) returned the stale JOURNAL.written entry from the freed allocation — failing assert!(before.is_empty()) at watch.rs:998.

The exact failure reproduces 100% deterministically on 01aadd1 with:

cargo test -q -p victory-gpu --lib --locked -- --test-threads=1 warp_write_watched_texture written_pages_are_reported

Changes

  • crates/victory-gpu/src/watch.rs:
    • In title_memory_remapped(at, len), remove whole contained pages (at.div_ceil(PAGE)..end / PAGE) from JOURNAL.written (safe for writes_guarded because g.protected/g.clean are also cleared for the same range, and avoids clobbering any neighboring allocation on a partial 64 KiB page).
    • Call title_memory_remapped(p, 4 * PAGE) before VirtualFree in written_pages_are_reported_to_every_asker.
    • Add regression test watch::tests::remapped_memory_clears_stale_write_journal.
  • crates/victory-gpu/src/texture.rs:
    • Call crate::watch::title_memory_remapped(raw as usize, alloc_size) before VirtualFree in warp_write_watched_texture_uploads_only_when_written (consistent with the other watch::tests that notify title_memory_remapped on teardown).

Verification

  • Before fix: cargo test -q -p victory-gpu --lib --locked -- --test-threads=1 warp_write_watched_texture written_pages_are_reported -> 1 passed, 1 failed (watch.rs:998: assertion failed: before.is_empty()).
  • After fix:
    • cargo test -q -p victory-gpu --lib --locked -- --test-threads=1 warp_write_watched_texture written_pages_are_reported remapped_memory_clears_stale_write_journal -> 3 passed, 0 failed.
    • cargo test -q -p victory-gpu --lib --locked -- --test-threads=1 texture:: watch:: -> 12 passed, 0 failed.
    • cargo clippy -q -p victory-gpu --lib --tests --locked --no-deps -> 0 warnings in watch.rs and texture.rs.
<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:issue:6606701794 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/112). Originally posted by [adritoyalser](https://github.com/adritoyalser) on 2026-11-25T14:24:41Z. --- ## Summary Fixes #102. ### Root Cause `watch::title_memory_remapped(at, len)` cleared guarded-block state (`protected`, `clean`, `holes`, `aliased`), bumped `REGION_EPOCHS`, and invalidated shadows/placed textures/bindless views, but never removed pages in `[at, at + len)` from the process-global `JOURNAL.written` map. When `texture::tests::warp_write_watched_texture_uploads_only_when_written` ran before `watch::tests::written_pages_are_reported_to_every_asker` in a serial run: 1. `warp_write_watched_texture_uploads_only_when_written` allocated a `MEM_WRITE_WATCH` buffer via `VirtualAlloc`, wrote to it, recorded `(raw / PAGE, stamp)` in `JOURNAL.written` via `watch::writes`, and called `VirtualFree` without clearing `JOURNAL.written`. 2. `written_pages_are_reported_to_every_asker` allocated a fresh `MEM_WRITE_WATCH` buffer via `VirtualAlloc`, Windows reused the just-freed 64 KiB-aligned virtual address, and `watch::writes(start, end, false)` returned the stale `JOURNAL.written` entry from the freed allocation — failing `assert!(before.is_empty())` at `watch.rs:998`. The exact failure reproduces 100% deterministically on `01aadd1` with: ``` cargo test -q -p victory-gpu --lib --locked -- --test-threads=1 warp_write_watched_texture written_pages_are_reported ``` ### Changes - **`crates/victory-gpu/src/watch.rs`**: - In `title_memory_remapped(at, len)`, remove whole contained pages (`at.div_ceil(PAGE)..end / PAGE`) from `JOURNAL.written` (safe for `writes_guarded` because `g.protected`/`g.clean` are also cleared for the same range, and avoids clobbering any neighboring allocation on a partial 64 KiB page). - Call `title_memory_remapped(p, 4 * PAGE)` before `VirtualFree` in `written_pages_are_reported_to_every_asker`. - Add regression test `watch::tests::remapped_memory_clears_stale_write_journal`. - **`crates/victory-gpu/src/texture.rs`**: - Call `crate::watch::title_memory_remapped(raw as usize, alloc_size)` before `VirtualFree` in `warp_write_watched_texture_uploads_only_when_written` (consistent with the other `watch::tests` that notify `title_memory_remapped` on teardown). ## Verification - Before fix: `cargo test -q -p victory-gpu --lib --locked -- --test-threads=1 warp_write_watched_texture written_pages_are_reported` -> **1 passed, 1 failed** (`watch.rs:998: assertion failed: before.is_empty()`). - After fix: - `cargo test -q -p victory-gpu --lib --locked -- --test-threads=1 warp_write_watched_texture written_pages_are_reported remapped_memory_clears_stale_write_journal` -> **3 passed, 0 failed**. - `cargo test -q -p victory-gpu --lib --locked -- --test-threads=1 texture:: watch::` -> **12 passed, 0 failed**. - `cargo clippy -q -p victory-gpu --lib --tests --locked --no-deps` -> **0 warnings in `watch.rs` and `texture.rs`**.
gut0to commented 2026-11-25 14:53:42 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by gut0to on 2026-11-25T14:53:42Z.

Review: COMMENTED


AI-assisted review: I checked the range bounds and the current regression-test coverage.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:review:6059932730 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/112#pullrequestreview-6059932730). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-25T14:53:42Z. **Review:** COMMENTED --- AI-assisted review: I checked the range bounds and the current regression-test coverage.
gut0to commented 2026-11-25 14:53:42 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by gut0to on 2026-11-25T14:53:42Z.

Code review location: crates/victory-gpu/src/watch.rs; line 721

@@ -717,6 +717,17 @@ pub fn title_memory_remapped(at: usize, len: usize) {
             bump_region_epochs(at, end);
         }
     }
+    {
+        let first = at.div_ceil(PAGE);

Could the regression test also cover a partial-page remap boundary, verifying that fully covered pages are cleared while the adjacent partially covered page is preserved? The ceil(at / PAGE)..end / PAGE bounds prevent deleting neighboring-page state, and the current test uses only a page-aligned range. AI-assisted review.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:review-comment:4696694410 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/112#discussion_r4696694410). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-25T14:53:42Z. **Code review location:** `crates/victory-gpu/src/watch.rs`; line 721 ~~~~diff @@ -717,6 +717,17 @@ pub fn title_memory_remapped(at: usize, len: usize) { bump_region_epochs(at, end); } } + { + let first = at.div_ceil(PAGE); ~~~~ --- Could the regression test also cover a partial-page remap boundary, verifying that fully covered pages are cleared while the adjacent partially covered page is preserved? The ceil(at / PAGE)..end / PAGE bounds prevent deleting neighboring-page state, and the current test uses only a page-aligned range. AI-assisted review.
adritoyalser commented 2026-11-25 15:27:20 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by adritoyalser on 2026-11-25T15:27:20Z.


Good suggestion @gut0to! Expanded remapped_memory_clears_stale_write_journal to allocate 3 pages and call title_memory_remapped(p + 1, 2 * PAGE), verifying that only the fully covered middle page is cleared while both partially covered adjacent pages (base and base + 2) preserve their write-journal stamps. texture:: + watch:: tests pass (12 passed, 0 failed).

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:comment:7166868984 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/112#issuecomment-7166868984). Originally posted by [adritoyalser](https://github.com/adritoyalser) on 2026-11-25T15:27:20Z. --- Good suggestion @gut0to! Expanded `remapped_memory_clears_stale_write_journal` to allocate 3 pages and call `title_memory_remapped(p + 1, 2 * PAGE)`, verifying that only the fully covered middle page is cleared while both partially covered adjacent pages (`base` and `base + 2`) preserve their write-journal stamps. `texture::` + `watch::` tests pass (`12 passed, 0 failed`).
Adika713 commented 2026-11-25 16:04:17 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by Adika713 on 2026-11-25T16:04:17Z.


Verified on AMD Radeon RX 6950 XT, Ryzen 5 3600, 32 GB RAM, Windows 11,
Rust 1.98.1 / Cargo 1.98.1. No game data; the title was not run.

Each tree used an isolated CARGO_TARGET_DIR.

Before, at upstream/main 01aadd193d57e71fb4d57810eb2fdd1bc95c950b, from
F:\VIctory-Belobug-AMD with
CARGO_TARGET_DIR=F:\VIctory-Belobug-AMD\_agent_tmp\target-gpu-main-verify:

cargo test -q -p victory-gpu --lib --locked -- --test-threads=1 warp_write_watched_texture written_pages_are_reported

20 runs: 18 failed, 2 passed. Every failure was
watch::tests::written_pages_are_reported_to_every_asker panicking at
watch.rs:998:9 with assertion failed: before.is_empty(). A separate sample
of 20 serial full-lib runs at main failed 15 times on the same test.

After, at PR head a1e531b, from a detached worktree at
F:\VIctory-Belobug-AMD\_agent_tmp\watch-112 with
CARGO_TARGET_DIR=F:\VIctory-Belobug-AMD\_agent_tmp\target-watch-112:

cargo test -q --manifest-path F:\VIctory-Belobug-AMD\_agent_tmp\watch-112\Cargo.toml -p victory-gpu --lib --locked -- --test-threads=1 warp_write_watched_texture written_pages_are_reported
cargo test -q --manifest-path F:\VIctory-Belobug-AMD\_agent_tmp\watch-112\Cargo.toml -p victory-gpu --lib --locked -- --test-threads=1
cargo test -q --manifest-path F:\VIctory-Belobug-AMD\_agent_tmp\watch-112\Cargo.toml -p victory-gpu --lib --locked -- --test-threads=1 remapped_memory_clears_stale_write_journal
  • Deterministic repro: 20/20 runs passed; each
    test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 114 filtered out.
  • Full serial lib: 20/20 runs passed; each
    test result: ok. 112 passed; 0 failed; 4 ignored; 0 measured; 0 filtered out.
  • New regression test: test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 115 filtered out.

Overlap: PR #115 head c866e7a also makes the flake disappear (20/20
deterministic and 20/20 full serial runs passed), but it only changes the test.
PR #127 head c9e45b4 touches watch.rs and texture.rs but does not compile
here: error[E0425]: cannot find type 'F' in this scope at host.rs:3134 and
host.rs:3138; no tests ran. I wrote no competing fix.

Not tested: the game, or the full workspace suite.

AI-assisted: OpenCode, model Big Pickle. No game data.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:comment:7167566723 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/112#issuecomment-7167566723). Originally posted by [Adika713](https://github.com/Adika713) on 2026-11-25T16:04:17Z. --- Verified on AMD Radeon RX 6950 XT, Ryzen 5 3600, 32 GB RAM, Windows 11, Rust 1.98.1 / Cargo 1.98.1. No game data; the title was not run. Each tree used an isolated `CARGO_TARGET_DIR`. Before, at `upstream/main` `01aadd193d57e71fb4d57810eb2fdd1bc95c950b`, from `F:\VIctory-Belobug-AMD` with `CARGO_TARGET_DIR=F:\VIctory-Belobug-AMD\_agent_tmp\target-gpu-main-verify`: ```powershell cargo test -q -p victory-gpu --lib --locked -- --test-threads=1 warp_write_watched_texture written_pages_are_reported ``` 20 runs: **18 failed, 2 passed**. Every failure was `watch::tests::written_pages_are_reported_to_every_asker` panicking at `watch.rs:998:9` with `assertion failed: before.is_empty()`. A separate sample of 20 serial full-lib runs at main failed 15 times on the same test. After, at PR head `a1e531b`, from a detached worktree at `F:\VIctory-Belobug-AMD\_agent_tmp\watch-112` with `CARGO_TARGET_DIR=F:\VIctory-Belobug-AMD\_agent_tmp\target-watch-112`: ```powershell cargo test -q --manifest-path F:\VIctory-Belobug-AMD\_agent_tmp\watch-112\Cargo.toml -p victory-gpu --lib --locked -- --test-threads=1 warp_write_watched_texture written_pages_are_reported cargo test -q --manifest-path F:\VIctory-Belobug-AMD\_agent_tmp\watch-112\Cargo.toml -p victory-gpu --lib --locked -- --test-threads=1 cargo test -q --manifest-path F:\VIctory-Belobug-AMD\_agent_tmp\watch-112\Cargo.toml -p victory-gpu --lib --locked -- --test-threads=1 remapped_memory_clears_stale_write_journal ``` - Deterministic repro: 20/20 runs passed; each `test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 114 filtered out`. - Full serial lib: 20/20 runs passed; each `test result: ok. 112 passed; 0 failed; 4 ignored; 0 measured; 0 filtered out`. - New regression test: `test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 115 filtered out`. Overlap: PR #115 head `c866e7a` also makes the flake disappear (20/20 deterministic and 20/20 full serial runs passed), but it only changes the test. PR #127 head `c9e45b4` touches `watch.rs` and `texture.rs` but does not compile here: `error[E0425]: cannot find type 'F' in this scope` at `host.rs:3134` and `host.rs:3138`; no tests ran. I wrote no competing fix. Not tested: the game, or the full workspace suite. AI-assisted: OpenCode, model Big Pickle. No game data.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/112:upstream-pr/112'
git switch 'upstream-pr/112'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/112'
git switch 'upstream-pr/112'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/112'
git switch 'upstream-pr/112'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/112'
git switch 'main'
git merge --squash 'upstream-pr/112'
git switch 'main'
git merge --ff-only 'upstream-pr/112'
git switch 'main'
git merge 'upstream-pr/112'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Belobug/VIctory-Belobug-AMD!112
No description provided.