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

Open
github-bot wants to merge 2 commits from github-bot/gh-112 into main AGit
Owner

@⁠adritoyalser on GitHub · 2026-11-25 14:24 UTC · original
adritoyalser:fix/watch-journal-remap-cleanup → main · +58 −0 · 2 files · 2 commits · open

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.
**@⁠adritoyalser** on GitHub · 2026-11-25 14:24 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/112) `adritoyalser:fix/watch-journal-remap-cleanup` → `main` · +58 −0 · 2 files · 2 commits · open ## 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`**.
`title_memory_remapped` cleared guarded-block state (`protected`,
`clean`, `holes`, `aliased`), bumped region epochs, and evicted
shadows/textures for `[at, at + len)`, but left stale page stamps behind
in the process-global `JOURNAL.written` map.

When a write-watched allocation was freed and Windows reused the same
virtual address for a fresh `MEM_WRITE_WATCH` allocation, `watch::writes`
returned the stale journal timestamp from the freed allocation even
though `GetWriteWatch` reported zero dirty pages. This deterministically
reproduced #102 whenever `texture::tests::warp_write_watched_texture_uploads_only_when_written`
ran before `watch::tests::written_pages_are_reported_to_every_asker` in
the same process.

- Remove whole pages covered by `[at, at + len)` (`at.div_ceil(PAGE)..end / PAGE`)
  from `JOURNAL.written` in `watch::title_memory_remapped`.
- Notify `watch::title_memory_remapped` before `VirtualFree` in
  `warp_write_watched_texture_uploads_only_when_written` and
  `written_pages_are_reported_to_every_asker` (matching the existing
  guarded-view tests in `watch.rs`).
- Add `watch::tests::remapped_memory_clears_stale_write_journal`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:35:59 +00:00
Author
Owner

@⁠adritoyalser on GitHub · 2026-11-25 15:27 UTC · original

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).

**@⁠adritoyalser** on GitHub · 2026-11-25 15:27 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/112#issuecomment-7166868984) 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`).
Author
Owner

@⁠Adika713 on GitHub · 2026-11-25 16:04 UTC · original

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.

**@⁠Adika713** on GitHub · 2026-11-25 16:04 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/112#issuecomment-7167566723) 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.
@ -718,2 +718,4 @@
}
}
{
let first = at.div_ceil(PAGE);
Author
Owner

@⁠gut0to on GitHub · 2026-11-25 14:53 UTC · original

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.

**@⁠gut0to** on GitHub · 2026-11-25 14:53 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/112#discussion_r4696694410) 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.
Author
Owner

@⁠gut0to on GitHub · 2026-11-25 14:53 UTC · original · reviewed

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

**@⁠gut0to** on GitHub · 2026-11-25 14:53 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/112#pullrequestreview-6059932730) · **reviewed** AI-assisted review: I checked the range bounds and the current regression-test coverage.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/112/head:github-bot/gh-112'
git switch 'github-bot/gh-112'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-112'
git switch 'github-bot/gh-112'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-112'
git switch 'github-bot/gh-112'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-112'
git switch 'main'
git merge --squash 'github-bot/gh-112'
git switch 'main'
git merge --ff-only 'github-bot/gh-112'
git switch 'main'
git merge 'github-bot/gh-112'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.