Watch: a kernel write into title memory refused by a guard set again is opened and made #1009

Closed
github-bot wants to merge 1 commit from gh-pr/1009/head into gh-pr/1009/base
Owner

@⁠cmt8 on GitHub · 2026-11-25 15:57 UTC · original
up/kernel-write-retry → main · +133 −13 · 5 files · 1 commits · closed without merging

What breaks today

Since 8dd30ab4, a copy over one view of a physical page guards every view of it. Our own writes into title memory go through the kernel (the copy-back's write_mapped, copy_mapped into title memory, a title's synchronous ReadFile). A guard set again between writing and the kernel's write makes the kernel refuse it. Under Wine it does not fault into our handler: NtWriteVirtualMemory/NtReadVirtualMemory/NtReadFile check the buffer with virtual_check_buffer_for_write and copy under their own exception guard (dlls/ntdll/unix/virtual.c, file.c). So a copy-back is lost, and copy_mapped's piece-by-piece fallback writes zeros over the title's bytes.

The change

One commit, cherry-picked from our 738f7148. watch::kernel_write_refused(at, len) opens whatever is guarded there now and says whether anything was. write_mapped and copy_mapped (title destination, and its 64 KiB / 4 KiB fallback) retry a refused piece once before writing zeros. A synchronous ReadFile that fails with ERROR_NOACCESS/ERROR_INVALID_USER_BUFFER while a guard lay over its buffer is made again, up to three times. Overlapped reads are left alone, because a refused one still posts a completion. The refused writes are counted on a status line next to the page-writes one. VICTORY_READ_BESIDE=0 turns this off, together with 8dd30ab4's read-beside of fread and DirectStorage file reads, for A/B testing.

Evidence

  • In-game, one user's observation on one machine (cmt8: RX 7900 XTX, RADV Mesa 26.3, Proton Experimental): on a build with 8dd30ab4's view guard, the walls and floors of the first interior were missing. With this change and a second one they rendered: the second (DirectStorage page writes also making placed textures due under Wine) is held back, because main's new guards default covers part of it and we have not isolated what is left. So this observation does not show this commit's effect on its own.
  • New test watch::tests::a_kernel_write_refused_under_a_guard_set_again_is_made: a copy-back write into a view that was opened, then guarded again through the other view, lands. It passes under Wine. With VICTORY_READ_BESIDE=0 it fails ("the write landed"), which is the lost write.
  • Windows test binaries under Wine 11.22 with Proton Experimental's vkd3d-proton, using the wine-test xtask from Marvisak/VIctory #1218 (not part of this PR): the watch:: tests pass on this commit. The full run was made with the held-back commit on top: victory-gpu 164 passed, 8 failed, the same 8 that fail on 0b0074e0; victory-hle 97 passed, 1 failed, unchanged.

Not verified

  • Not run on Windows (we have none). The change is not gated on Wine, but it acts only after a kernel write has already failed. Where a write succeeds the path is unchanged. On Windows a kernel write into a read-only page also fails without a fault, so the retry is the same fix there if it ever happens.
  • The game has not been run with exactly this branch.
  • #571 (arminkhaleghirod; cause found by Yorivel) reports page-destination writes at every mapped address, on by default. It now conflicts with main, which has 8dd30ab4's form of the same report.
  • #426/#552 (YeokiNoewChi) read a plain DirectStorage read refused for its buffer through a bounce buffer. main now reads beside instead. This PR covers ReadFile, write_mapped and copy_mapped.
  • #154 (BitWuehler) and VICTORY_COPY_PLAIN copy in-process so writes fault into the guard. That is another way around the same refusal, off by default.

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

**@⁠cmt8** on GitHub · 2026-11-25 15:57 UTC · [original](https://github.com/fxceless/victory-fork/pull/1009) `up/kernel-write-retry` → `main` · +133 −13 · 5 files · 1 commits · closed without merging ## What breaks today Since 8dd30ab4, a copy over one view of a physical page guards every view of it. Our own writes into title memory go through the kernel (the copy-back's `write_mapped`, `copy_mapped` into title memory, a title's synchronous `ReadFile`). A guard set again between `writing` and the kernel's write makes the kernel refuse it. Under Wine it does not fault into our handler: `NtWriteVirtualMemory`/`NtReadVirtualMemory`/`NtReadFile` check the buffer with `virtual_check_buffer_for_write` and copy under their own exception guard (dlls/ntdll/unix/virtual.c, file.c). So a copy-back is lost, and `copy_mapped`'s piece-by-piece fallback writes zeros over the title's bytes. ## The change One commit, cherry-picked from our 738f7148. `watch::kernel_write_refused(at, len)` opens whatever is guarded there now and says whether anything was. `write_mapped` and `copy_mapped` (title destination, and its 64 KiB / 4 KiB fallback) retry a refused piece once before writing zeros. A synchronous `ReadFile` that fails with `ERROR_NOACCESS`/`ERROR_INVALID_USER_BUFFER` while a guard lay over its buffer is made again, up to three times. Overlapped reads are left alone, because a refused one still posts a completion. The refused writes are counted on a status line next to the page-writes one. `VICTORY_READ_BESIDE=0` turns this off, together with 8dd30ab4's read-beside of `fread` and DirectStorage file reads, for A/B testing. ## Evidence - In-game, one user's observation on one machine (cmt8: RX 7900 XTX, RADV Mesa 26.3, Proton Experimental): on a build with 8dd30ab4's view guard, the walls and floors of the first interior were missing. With this change and a second one they rendered: the second (DirectStorage page writes also making placed textures due under Wine) is held back, because `main`'s new `guards` default covers part of it and we have not isolated what is left. So this observation does not show this commit's effect on its own. - New test `watch::tests::a_kernel_write_refused_under_a_guard_set_again_is_made`: a copy-back write into a view that was opened, then guarded again through the other view, lands. It passes under Wine. With `VICTORY_READ_BESIDE=0` it fails ("the write landed"), which is the lost write. - Windows test binaries under Wine 11.22 with Proton Experimental's vkd3d-proton, using the `wine-test` xtask from Marvisak/VIctory #1218 (not part of this PR): the `watch::` tests pass on this commit. The full run was made with the held-back commit on top: victory-gpu 164 passed, 8 failed, the same 8 that fail on `0b0074e0`; victory-hle 97 passed, 1 failed, unchanged. ## Not verified - Not run on Windows (we have none). The change is not gated on Wine, but it acts only after a kernel write has already failed. Where a write succeeds the path is unchanged. On Windows a kernel write into a read-only page also fails without a fault, so the retry is the same fix there if it ever happens. - The game has not been run with exactly this branch. ## Related - #571 (arminkhaleghirod; cause found by Yorivel) reports page-destination writes at every mapped address, on by default. It now conflicts with `main`, which has 8dd30ab4's form of the same report. - #426/#552 (YeokiNoewChi) read a plain DirectStorage read refused for its buffer through a bounce buffer. `main` now reads beside instead. This PR covers `ReadFile`, `write_mapped` and `copy_mapped`. - #154 (BitWuehler) and `VICTORY_COPY_PLAIN` copy in-process so writes fault into the guard. That is another way around the same refusal, off by default. AI-assisted (Claude Code); no game data.
With 8dd30ab4's guard over every view of a physical page, a copy over one
view makes the other views read-only from whatever thread it runs on. Our
own writes into title memory go through the kernel (write_mapped and
copy_mapped's title copies, both ReadProcessMemory/NtWriteVirtualMemory;
a title's synchronous ReadFile), after `writing` opened the destination;
a guard set again in between (now any copy over another view of the same
pages does it) makes the kernel refuse the write. Under Wine the kernel
side does not fault into our handler: NtReadVirtualMemory and NtReadFile
check the buffer with virtual_check_buffer_for_write and copy under their
own exception guard, virtual_locked_pread returns EFAULT
(dlls/ntdll/unix/virtual.c, file.c in Proton's Wine 11). So a copy-back's
write was lost, and copy_mapped's piece-by-piece fallback wrote zeros
over the title's bytes where it thought the source unreadable. 8dd30ab4
reads fread and DirectStorage file reads beside for this reason; these
paths were left.

- watch::kernel_write_refused(at, len): opens what is guarded there now
  and says whether anything was; counted (REFUSED, a new status line).
- write_mapped and copy_mapped (title destination, and its 64 KiB / 4 KiB
  fallback) try a refused piece again once it is opened, before writing
  zeros.
- ReadFile without an OVERLAPPED: a read that fails with ERROR_NOACCESS or
  ERROR_INVALID_USER_BUFFER while a guard lay over the buffer is made
  again (up to three times). It read nothing and moved no file pointer.
  Overlapped reads are left as they were: a refused pread there still
  posts a completion (add_completion at `done:`), and a second read would
  post a second one.
- VICTORY_READ_BESIDE=0 turns all of this and 8dd30ab4's read-beside of
  fread and DirectStorage file reads off, for A/B against the old paths.

Test: a copy-back write into a view opened and then guarded again through
the other view lands (and shows at the other view). With
VICTORY_READ_BESIDE=0 the same test fails under Wine ("the write
landed"), which is the loss this fixes.

Tested: cargo xwin build --release -p victory; cargo xtask policy (179
files ok); Windows tests under Wine 11.22 with Proton Experimental's
vkd3d-proton on an RX 7900 XTX: watch:: 13 passed, the new test
included; the new test with VICTORY_READ_BESIDE=0 fails ("the write
landed"). Not run on Windows.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 738f7148e542710daeb113dd8460e37d87638c1e of
cmt8/VIctory linux/fx-next6; the test calls this tree's guard(start, end))
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:55:48 +00:00
github-bot closed this pull request 2026-11-26 03:55:48 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.