Watch: save a page's old bytes before any guard is opened #2239

Manually merged
github merged 1 commit from gh-pr/2239/head into gh-pr/2239/base 2026-11-26 04:59:35 +00:00
Owner

@⁠denkencar-sketch on GitHub · 2026-11-25 08:31 UTC · original
denkencar-sketch:fix/watch-save-before-open → main · +19 −7 · 1 files · 1 commits · merged 2026-11-25 21:55 UTC

Summary

changed_here opens the guarded pages (VirtualProtect to read-write) and only then marks them and saves their bytes for armed trackers (Changed::save, "before the write lands"). changed also opens the faulting range before it handles the pages aliasing it. Once a page is open, a write by another thread lands without a fault, and a save after that keeps the new bytes as the page's old bytes. Shadow::copy_back_marked then sees no difference between the copy and before and overwrites the other thread's write in title memory.

Change

changed marks and saves the range and its aliases first (mark_changed), and opens them after (open_guarded). Nothing else changes: same marks, same saves, same pages opened, same return value.

Testing

No new test. It is a window between two calls on two threads, which I could not hit deterministically. The existing tests pass (143 in victory-gpu, three runs, and the full workspace), and cargo clippy --workspace --all-targets [--all-features] --locked -- -D warnings is clean. This is a reordering found by reading, so please read it with that in mind.

Not tried: a run in the game. modules::dstorage::tests::a_page_list_filled_after_its_request_starts fails now and then on main too (5 of 20 runs of cargo test -p victory-hle --lib); it is unrelated.

AI-assisted.

**@⁠denkencar-sketch** on GitHub · 2026-11-25 08:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/2239) `denkencar-sketch:fix/watch-save-before-open` → `main` · +19 −7 · 1 files · 1 commits · merged 2026-11-25 21:55 UTC ## Summary `changed_here` opens the guarded pages (`VirtualProtect` to read-write) and only then marks them and saves their bytes for armed trackers (`Changed::save`, "before the write lands"). `changed` also opens the faulting range before it handles the pages aliasing it. Once a page is open, a write by another thread lands without a fault, and a `save` after that keeps the new bytes as the page's old bytes. `Shadow::copy_back_marked` then sees no difference between the copy and `before` and overwrites the other thread's write in title memory. ## Change `changed` marks and saves the range and its aliases first (`mark_changed`), and opens them after (`open_guarded`). Nothing else changes: same marks, same saves, same pages opened, same return value. ## Testing **No new test.** It is a window between two calls on two threads, which I could not hit deterministically. The existing tests pass (143 in `victory-gpu`, three runs, and the full workspace), and `cargo clippy --workspace --all-targets [--all-features] --locked -- -D warnings` is clean. This is a reordering found by reading, so please read it with that in mind. Not tried: a run in the game. `modules::dstorage::tests::a_page_list_filled_after_its_request_starts` fails now and then on `main` too (5 of 20 runs of `cargo test -p victory-hle --lib`); it is unrelated. AI-assisted.
changed_here opened the guarded pages and then saved their bytes for armed trackers, and changed opened the faulting range before the pages that alias it. A write by another thread landing in between went unfaulted, and the save then kept the new bytes as the old ones, so copy_back_marked saw no difference and the write was overwritten. Marks and saves of the range and its aliases now come first, and the pages are opened after. Not covered by a test: it is a race between two threads.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:59:33 +00:00
github manually merged commit 583c14d517 into gh-pr/2239/base 2026-11-26 04:59:35 +00:00
Author
Owner

@⁠MamaLeVrai on GitHub · 2026-11-25 09:13 UTC · original

Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present, so the hardware-only shader checks run. I merged this PR's head 583c14d5 into main 940d32f in a throwaway tree (no conflict). cargo clippy --workspace --all-targets --locked -- -D warnings: clean; the same with --all-features: clean. cargo test --workspace --locked: 701 passed, 0 failed, 10 ignored, two runs (main alone on the same machine: 701 passed, 0 failed, 10 ignored). The title was not run, so this says nothing about in-game effect. No game data.

AI-assisted (Claude Code); no game data.

**@⁠MamaLeVrai** on GitHub · 2026-11-25 09:13 UTC · [original](https://github.com/Marvisak/VIctory/pull/2239#issuecomment-7158043282) Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present, so the hardware-only shader checks run. I merged this PR's head `583c14d5` into `main` `940d32f` in a throwaway tree (no conflict). `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean; the same with `--all-features`: clean. `cargo test --workspace --locked`: 701 passed, 0 failed, 10 ignored, two runs (`main` alone on the same machine: 701 passed, 0 failed, 10 ignored). The title was not run, so this says nothing about in-game effect. No game data. AI-assisted (Claude Code); no game data.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 12:44 UTC · original

Automated triage for the maintainers.

Merge candidate. In changed (crates/victory-gpu/src/watch.rs), on a write fault to a range and its aliases, main saved and opened one page at a time. Once one page was open, another thread's write could land on an alias that had not been saved yet, and that alias's "old bytes" would then already contain the write. The PR splits changed_here into mark_changed (save and mark) and open_guarded. It saves every page, the range and all its aliases, before opening any of them. The returned guarded value is computed the same way as before.

The reordering is right by my reading. There is no test, which is understandable for this race. CI check passes, an RX 6950 XT run shows the full workspace green, and the branch merges cleanly.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 12:44 UTC · [original](https://github.com/Marvisak/VIctory/pull/2239#issuecomment-7162795150) <!-- victory-triage v1 head=583c14d5170e1a9fb2d92a15f6fdcd675e2d9b1d verdict=merge-candidate at=2026-11-25T12:52:00Z --> Automated triage for the maintainers. **Merge candidate.** In `changed` (`crates/victory-gpu/src/watch.rs`), on a write fault to a range and its aliases, `main` saved and opened one page at a time. Once one page was open, another thread's write could land on an alias that had not been saved yet, and that alias's "old bytes" would then already contain the write. The PR splits `changed_here` into `mark_changed` (save and mark) and `open_guarded`. It saves every page, the range and all its aliases, before opening any of them. The returned `guarded` value is computed the same way as before. The reordering is right by my reading. There is no test, which is understandable for this race. CI `check` passes, an RX 6950 XT run shows the full workspace green, and the branch merges cleanly. --- _Generated by [Claude Code](https://claude.ai/code)_
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2239
No description provided.