copies of title memory: a guard page fails the copy and stays a guard page #14

Manually merged
github merged 2 commits from github-bot/gh-14 into main 2026-11-26 08:21:46 +00:00 AGit
Owner

@⁠zocram4cc on GitHub · 2026-11-25 23:29 UTC · original
zocram4cc:up/guard-page → main · +165 −4 · 2 files · 2 commits · merged 2026-11-26 08:17 UTC

copies of title memory: a guard page fails the copy and stays a guard page

AI-assisted (Claude Code). The change, the test and the runs were done with an AI agent on the author's machine.

Problem

With the default VICTORY_SAFE_COPY=1, a STATUS_GUARD_PAGE_VIOLATION (0x80000001) raised inside one of our copies of title memory is treated like any other fault in a copy: the copy fails, and the page's guard flag, which the fault consumed, is never set again. In one traced run under Proton (Wine) the write-catch handler caught about 3 million access violations and two such guard faults inside our copies.

Cause

Title memory has guard pages: stack guards, and guards the title sets up itself. When one of our copies (victory_safe_copy, victory_safe_put, victory_safe_merge in safecopy.rs) reads or writes a guard page, the OS clears the page's guard flag and raises 0x80000001.

on_write in watch.rs, the first vectored exception handler in the process, sent every fault raised inside a copy to that copy's failing exit, whatever the exception code. Nothing set the guard flag again, so the page stayed an ordinary page: the title's next touch of it no longer raised the guard fault it was set up for.

The title's own guard pages therefore stop working after one of our copies touches them. The test below checks exactly that state.

Fix

  • watch.rs, on_write: a 0x80000001 raised inside one of our copies now calls a new helper, rearm_guard, before failing the copy exactly as before. rearm_guard sets PAGE_GUARD again on the 4 KiB page at the faulting address (ExceptionInformation[1]), keeping the protection the page has now.
  • Guard faults outside our copies are unchanged: they still return EXCEPTION_CONTINUE_SEARCH, so whatever handles them (the OS, or Wine) still runs.
  • The fix commit: 58 lines added and 4 removed, in two files (watch.rs, and safecopy.rs for the test). The review follow-ups below add to it.

Test

New test safecopy::tests::a_guard_page_fails_the_copy_and_stays_a_guard_page. It makes the second page of an 8 KiB allocation a guard page, copies the 8 KiB from it, and checks that the copy fails and that the page is still a guard page (VirtualQuery).

Run under Proton Experimental (Wine), cargo xwin test -p victory-gpu, --test-threads 1:

  • Without the fix: test safecopy::tests::a_guard_page_fails_the_copy_and_stays_a_guard_page ... FAILED, panicked at crates/victory-gpu/src/safecopy.rs:746:9: the guard flag was consumed.
  • With the fix: ... ok. The whole safecopy:: set passes: 11 passed, 1 ignored (copy_costs, a measurement that is not run by default; the three destination tests below are among the 11).

Not included: a test that a thread's stack still grows after a copy touched its guard. Under this Proton a new thread's stack is committed almost to its guard, with one reserved page below it, so growth cannot be told apart with or without the fix.

Build: cargo xwin build --profile diag -p victory --features diagnostics --target x86_64-pc-windows-msvc passes on this branch.

Build on the upstream base (v7900/main, up/guard-page = c277533 on 0a43566): cargo xwin build --profile diag -p victory --features diagnostics --target x86_64-pc-windows-msvc passes, and the safecopy:: tests pass under Proton there too (11 passed, 1 ignored).

Evidence from the rig

  • Before the fix, one traced run logged two code=80000001 (STATUS_GUARD_PAGE_VIOLATION) among about 3 million access violations from the write-catch handler.
  • Game runs with this change under Proton still end now and then: that is a separate native crash (a Linux SIGSEGV in a worker thread, under investigation), not this guard path. This PR fixes the lost guard flag, shown by the test above; it does not claim more.

Credit

The direct-copy path (VICTORY_DIRECT_COPY=1) had the same bug. It was fixed in klbf-lab PR #34 (https://github.com/PastiiJay/klbf-lab/pull/34, commit 478c6ef, "direct copy: a guard page fails the copy and stays a guard page"), and that test is the model for this one. This change is the same fix for the safecopy path.

Windows

This should matter on Windows too, wherever title guard pages meet our copies: safecopy is on by default on every platform (VICTORY_SAFE_COPY unset or not 0), and a guard page raised inside a copy takes the same path. It has not been tested on Windows. The re-arm reads the faulting address from ExceptionInformation[1]. Wine provides it for this exception (the test checks that under Proton); native Windows should be checked before this is relied on there.

Review follow-ups

Review of avatarmanz/VIctory-7900XTX#14 by itsonewi (approves in principle). The second commit answers it:

  1. Destination guard. A copy, a put (put_changed) and a merge into a guard page each fail, and the page stays a guard page. Tests: a_guard_page_in_the_destination_fails_a_copy_and_stays_guarded and the put and merge versions. They pass with the fix. Without it they fail ("the copy's fault cleared the guard flag", and the same for put and merge). The fix already covered this path; the tests pin it.
  2. Re-arm checks. rearm_guard now checks that the page is still committed (VirtualQuery) and that VirtualProtect succeeds. A failed re-arm, or a guard fault that gives no address, is counted in NOT_REARMED, and the first one is logged as a warning, "guard not re-armed". Test: a_page_that_cannot_be_guarded_again_is_counted (it fails against a stub that always says true, and passes with the checks).
  3. Kernel retries after a failed copy. None found. With VICTORY_SAFE_COPY on, no caller of copy, put_changed or merge falls back to ReadProcessMemory, NtWriteVirtualMemory, WriteProcessMemory or a memcpy. Those calls exist only in the VICTORY_SAFE_COPY=0 branches (host.rs: read and write_mapped), which have no fault handler and are unchanged. A retry on the safecopy path runs the same routine again, and each guard fault re-arms the page. No code change for this item.
  4. Windows is still untested. The Windows statement above stands. Whether Windows reports the faulting address in ExceptionInformation[1] is unverified; without it the re-arm is skipped and counted.

Under Proton Experimental, --test-threads 1: the three destination tests and the counting test pass, and safecopy:: gives 11 passed, 1 ignored, on both branches. The diag build passes on both.

**@⁠zocram4cc** on GitHub · 2026-11-25 23:29 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/14) `zocram4cc:up/guard-page` → `main` · +165 −4 · 2 files · 2 commits · merged 2026-11-26 08:17 UTC # copies of title memory: a guard page fails the copy and stays a guard page AI-assisted (Claude Code). The change, the test and the runs were done with an AI agent on the author's machine. ## Problem With the default `VICTORY_SAFE_COPY=1`, a `STATUS_GUARD_PAGE_VIOLATION` (`0x80000001`) raised inside one of our copies of title memory is treated like any other fault in a copy: the copy fails, and the page's guard flag, which the fault consumed, is never set again. In one traced run under Proton (Wine) the write-catch handler caught about 3 million access violations and two such guard faults inside our copies. ## Cause Title memory has guard pages: stack guards, and guards the title sets up itself. When one of our copies (`victory_safe_copy`, `victory_safe_put`, `victory_safe_merge` in `safecopy.rs`) reads or writes a guard page, the OS clears the page's guard flag and raises `0x80000001`. `on_write` in `watch.rs`, the first vectored exception handler in the process, sent every fault raised inside a copy to that copy's failing exit, whatever the exception code. Nothing set the guard flag again, so the page stayed an ordinary page: the title's next touch of it no longer raised the guard fault it was set up for. The title's own guard pages therefore stop working after one of our copies touches them. The test below checks exactly that state. ## Fix - `watch.rs`, `on_write`: a `0x80000001` raised inside one of our copies now calls a new helper, `rearm_guard`, before failing the copy exactly as before. `rearm_guard` sets `PAGE_GUARD` again on the 4 KiB page at the faulting address (`ExceptionInformation[1]`), keeping the protection the page has now. - Guard faults outside our copies are unchanged: they still return `EXCEPTION_CONTINUE_SEARCH`, so whatever handles them (the OS, or Wine) still runs. - The fix commit: 58 lines added and 4 removed, in two files (`watch.rs`, and `safecopy.rs` for the test). The review follow-ups below add to it. ## Test New test `safecopy::tests::a_guard_page_fails_the_copy_and_stays_a_guard_page`. It makes the second page of an 8 KiB allocation a guard page, copies the 8 KiB from it, and checks that the copy fails and that the page is still a guard page (`VirtualQuery`). Run under Proton Experimental (Wine), `cargo xwin test -p victory-gpu`, `--test-threads 1`: - Without the fix: `test safecopy::tests::a_guard_page_fails_the_copy_and_stays_a_guard_page ... FAILED`, `panicked at crates/victory-gpu/src/safecopy.rs:746:9: the guard flag was consumed`. - With the fix: `... ok`. The whole `safecopy::` set passes: 11 passed, 1 ignored (`copy_costs`, a measurement that is not run by default; the three destination tests below are among the 11). Not included: a test that a thread's stack still grows after a copy touched its guard. Under this Proton a new thread's stack is committed almost to its guard, with one reserved page below it, so growth cannot be told apart with or without the fix. Build: `cargo xwin build --profile diag -p victory --features diagnostics --target x86_64-pc-windows-msvc` passes on this branch. Build on the upstream base (`v7900/main`, `up/guard-page` = `c277533` on `0a43566`): `cargo xwin build --profile diag -p victory --features diagnostics --target x86_64-pc-windows-msvc` passes, and the `safecopy::` tests pass under Proton there too (11 passed, 1 ignored). ## Evidence from the rig - Before the fix, one traced run logged two `code=80000001` (`STATUS_GUARD_PAGE_VIOLATION`) among about 3 million access violations from the write-catch handler. - Game runs with this change under Proton still end now and then: that is a separate native crash (a Linux SIGSEGV in a worker thread, under investigation), not this guard path. This PR fixes the lost guard flag, shown by the test above; it does not claim more. ## Credit The direct-copy path (`VICTORY_DIRECT_COPY=1`) had the same bug. It was fixed in klbf-lab PR #34 (https://github.com/PastiiJay/klbf-lab/pull/34, commit `478c6ef`, "direct copy: a guard page fails the copy and stays a guard page"), and that test is the model for this one. This change is the same fix for the `safecopy` path. ## Windows This should matter on Windows too, wherever title guard pages meet our copies: `safecopy` is on by default on every platform (`VICTORY_SAFE_COPY` unset or not `0`), and a guard page raised inside a copy takes the same path. It has not been tested on Windows. The re-arm reads the faulting address from `ExceptionInformation[1]`. Wine provides it for this exception (the test checks that under Proton); native Windows should be checked before this is relied on there. ## Review follow-ups Review of avatarmanz/VIctory-7900XTX#14 by itsonewi (approves in principle). The second commit answers it: 1. **Destination guard.** A copy, a put (`put_changed`) and a merge into a guard page each fail, and the page stays a guard page. Tests: `a_guard_page_in_the_destination_fails_a_copy_and_stays_guarded` and the put and merge versions. They pass with the fix. Without it they fail ("the copy's fault cleared the guard flag", and the same for put and merge). The fix already covered this path; the tests pin it. 2. **Re-arm checks.** `rearm_guard` now checks that the page is still committed (`VirtualQuery`) and that `VirtualProtect` succeeds. A failed re-arm, or a guard fault that gives no address, is counted in `NOT_REARMED`, and the first one is logged as a warning, "guard not re-armed". Test: `a_page_that_cannot_be_guarded_again_is_counted` (it fails against a stub that always says true, and passes with the checks). 3. **Kernel retries after a failed copy.** None found. With `VICTORY_SAFE_COPY` on, no caller of `copy`, `put_changed` or `merge` falls back to `ReadProcessMemory`, `NtWriteVirtualMemory`, `WriteProcessMemory` or a memcpy. Those calls exist only in the `VICTORY_SAFE_COPY=0` branches (`host.rs`: `read` and `write_mapped`), which have no fault handler and are unchanged. A retry on the safecopy path runs the same routine again, and each guard fault re-arms the page. No code change for this item. 4. **Windows is still untested.** The Windows statement above stands. Whether Windows reports the faulting address in `ExceptionInformation[1]` is unverified; without it the re-arm is skipped and counted. Under Proton Experimental, `--test-threads 1`: the three destination tests and the counting test pass, and `safecopy::` gives 11 passed, 1 ignored, on both branches. The `diag` build passes on both.
Title memory has guard pages: stack guards, and guards the title sets up
itself. A copy of title memory (safecopy: victory_safe_copy, _put and
_merge, under the write-catch handler in watch.rs) that touched one got
STATUS_GUARD_PAGE_VIOLATION (0x80000001), and the OS cleared the page's
guard flag. on_write sent every fault inside a copy to the copy's failing
exit, whatever the code, and nothing set the flag again. The page stayed an
ordinary page, so the title's next touch of it no longer raised the guard
fault it was set up for. Under Proton the process then exits silently 1-3
minutes into a world; a traced run logged two 0x80000001 among three
million access violations. That the lost guard causes the exit is the
reading of that trace, not something reproduced here.

A guard fault inside a copy now sets the guard flag again on its page, with
the protection the page has, and fails the copy as before. A guard fault
outside a copy still goes on to the next handler (CONTINUE_SEARCH).

The direct copy (VICTORY_DIRECT_COPY) had the same bug; klbf-lab PR #34
(commit 478c6ef) fixed it there. This is the same fix for safecopy.

Test: safecopy::tests::a_guard_page_fails_the_copy_and_stays_a_guard_page.
Under Proton Experimental it fails without this change ("the guard flag was
consumed") and passes with it.
Review of avatarmanz/VIctory-7900XTX#14 (itsonewi) asked for three things on
the guard path.

Destination: a copy, a put and a merge into a guard page each fail and leave
the page a guard page. The fix in the previous commit already does this; the
tests pin it. Without the fix they fail ("the copy's fault cleared the guard
flag", the same for put and merge).

Re-arm: rearm_guard checks that the page is still committed (VirtualQuery)
and that VirtualProtect succeeds. A failed re-arm, or a guard fault that
gave no address (NumberParameters < 2), is counted in NOT_REARMED; the first
is logged once as a warning, "guard not re-armed". Test:
a_page_that_cannot_be_guarded_again_is_counted (red against a stub that
always answers true, green with the checks).

Kernel retries: none. With safecopy on, no caller of copy, put or merge falls
back to ReadProcessMemory, NtWriteVirtualMemory, WriteProcessMemory or a
memcpy after a failed copy. The kernel calls are only on the
VICTORY_SAFE_COPY=0 branches (host.rs read and write_mapped). The retries go
through the same safecopy routine again, and each guard fault re-arms the
page. No code change for this.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:31:18 +00:00
Author
Owner

@⁠zocram4cc on GitHub · 2026-11-26 00:17 UTC · original

Thanks @⁠itsonewi, all four addressed in c277533 (description updated with a "Review follow-ups" section):

  1. Destination side: three new tests guard the destination of copy, put and merge. They already pass with the fix (the non-access-violation branch re-arms whichever side faulted) and fail against the pre-fix watch.rs ("the copy's / put's / merge's fault cleared the guard flag"), so they now pin it.
  2. Silent re-arm failure: rearm_guard now checks that VirtualQuery reports MEM_COMMIT and that VirtualProtect succeeds; a failed re-arm, or a guard fault without an address (NumberParameters < 2), is counted (NOT_REARMED) and the first one logs a warning. Test a_page_that_cannot_be_guarded_again_is_counted (red against a stub that always succeeds).
  3. Retries through the kernel: none on the safecopy path. Every caller of copy/put/merge retries piecewise through the same routines (read, zero, write_mapped's 4 KiB pieces, put_changed per page, land's merge(true)), each of which re-arms on its own fault. ReadProcessMemory/NtWriteVirtualMemory/WriteProcessMemory are only reached with VICTORY_SAFE_COPY=0; the list with file:line is in the description.
  4. Windows: still not run there; said so in the description. If ExceptionInformation[1] were missing, the re-arm is skipped and counted, so it fails visibly rather than silently.

One related finding, not in this PR: protect_here/protect_alias set PAGE_READONLY over a whole 64 KiB block after checking only the first page's old protection, so a title guard page later in the block loses its guard without any fault (confirmed with a probe). I'll send that separately with its own test.

**@⁠zocram4cc** on GitHub · 2026-11-26 00:17 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/14#issuecomment-7178594952) Thanks @⁠itsonewi, all four addressed in c277533 (description updated with a "Review follow-ups" section): 1. **Destination side**: three new tests guard the destination of copy, put and merge. They already pass with the fix (the non-access-violation branch re-arms whichever side faulted) and fail against the pre-fix `watch.rs` ("the copy's / put's / merge's fault cleared the guard flag"), so they now pin it. 2. **Silent re-arm failure**: `rearm_guard` now checks that `VirtualQuery` reports `MEM_COMMIT` and that `VirtualProtect` succeeds; a failed re-arm, or a guard fault without an address (`NumberParameters < 2`), is counted (`NOT_REARMED`) and the first one logs a warning. Test `a_page_that_cannot_be_guarded_again_is_counted` (red against a stub that always succeeds). 3. **Retries through the kernel**: none on the `safecopy` path. Every caller of copy/put/merge retries piecewise through the same routines (`read`, `zero`, `write_mapped`'s 4 KiB pieces, `put_changed` per page, land's `merge(true)`), each of which re-arms on its own fault. `ReadProcessMemory`/`NtWriteVirtualMemory`/`WriteProcessMemory` are only reached with `VICTORY_SAFE_COPY=0`; the list with file:line is in the description. 4. **Windows**: still not run there; said so in the description. If `ExceptionInformation[1]` were missing, the re-arm is skipped and counted, so it fails visibly rather than silently. One related finding, not in this PR: `protect_here`/`protect_alias` set `PAGE_READONLY` over a whole 64 KiB block after checking only the first page's old protection, so a title guard page later in the block loses its guard without any fault (confirmed with a probe). I'll send that separately with its own test.
Author
Owner

@⁠itsonewi on GitHub · 2026-11-25 23:41 UTC · original · reviewed

The fix is right and the test proves exactly the symptom described. Four things I would change or would want answered before this is relied on.

1. Only the read side is covered

The test makes the source page a guard page and copies from it. The branch you added in on_write re-arms for every fault inside one of our copies that is not a write EXCEPTION_ACCESS_VIOLATION — which includes guard faults raised on the destination of put_changed/victory_safe_merge. Those are reached when a title guard page is written, which is at least as likely as read (a stack guard the title touches, a page the title armed before writing through it). A second case with the destination page guarded would cover it with the same three lines.

2. A failure to re-arm is silent

rearm_guard ignores both return values: VirtualQuery can report a page that is no longer committed (title unmapped or decommitted the range between the fault and the handler) and VirtualProtect can fail. In both cases the guard flag stays consumed and nothing is printed — which is the bug this PR fixes, one level down. One copystat counter (or a one-shot tracing::warn! in diagnostics) for "guard not re-armed" would make it visible instead of invisible.

3. Does any caller retry a failed copy through the kernel?

If a caller reacts to copy(...) == false by falling back to ReadProcessMemory/NtWriteVirtualMemory, that second attempt touches the same guard page again, the kernel clears the flag again, and this handler never sees it — so the guard is lost exactly as before. The fix is only complete if every retry path also goes through rearm_guard, or if no retry happens. Worth stating which it is in the description.

4. Windows

The description is honest that this was only run under Proton. ExceptionInformation[1] for STATUS_GUARD_PAGE_VIOLATION is the accessed address on Windows too, but the check is one test run on native Windows away from being certain, and safecopy is on by default there. If you cannot run it, say so in the PR and I would still take the change with that written down, since the current behaviour (silently disarming a title guard) is worse than the unverified one.

Not blocking any of the above: the code reads correctly for the fault path it was written for, PAGE_GUARD is preserved through info.Protect | PAGE_GUARD, and re-arming before fail(exit) is the right order.

AI use

AI-assisted review (source reading). No build, no hardware run, no game files.

**@⁠itsonewi** on GitHub · 2026-11-25 23:41 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/14#pullrequestreview-6066535560) · **reviewed** The fix is right and the test proves exactly the symptom described. Four things I would change or would want answered before this is relied on. ## 1. Only the read side is covered The test makes the **source** page a guard page and copies from it. The branch you added in `on_write` re-arms for every fault inside one of our copies that is not a write `EXCEPTION_ACCESS_VIOLATION` — which includes guard faults raised on the **destination** of `put_changed`/`victory_safe_merge`. Those are reached when a title guard page is written, which is at least as likely as read (a stack guard the title touches, a page the title armed before writing through it). A second case with the destination page guarded would cover it with the same three lines. ## 2. A failure to re-arm is silent `rearm_guard` ignores both return values: `VirtualQuery` can report a page that is no longer committed (title unmapped or decommitted the range between the fault and the handler) and `VirtualProtect` can fail. In both cases the guard flag stays consumed and nothing is printed — which is the bug this PR fixes, one level down. One `copystat` counter (or a one-shot `tracing::warn!` in diagnostics) for "guard not re-armed" would make it visible instead of invisible. ## 3. Does any caller retry a failed copy through the kernel? If a caller reacts to `copy(...) == false` by falling back to `ReadProcessMemory`/`NtWriteVirtualMemory`, that second attempt touches the same guard page again, the kernel clears the flag again, and this handler never sees it — so the guard is lost exactly as before. The fix is only complete if every retry path also goes through `rearm_guard`, or if no retry happens. Worth stating which it is in the description. ## 4. Windows The description is honest that this was only run under Proton. `ExceptionInformation[1]` for `STATUS_GUARD_PAGE_VIOLATION` is the accessed address on Windows too, but the check is one test run on native Windows away from being certain, and `safecopy` is on by default there. If you cannot run it, say so in the PR and I would still take the change with that written down, since the current behaviour (silently disarming a title guard) is worse than the unverified one. Not blocking any of the above: the code reads correctly for the fault path it was written for, `PAGE_GUARD` is preserved through `info.Protect | PAGE_GUARD`, and re-arming before `fail(exit)` is the right order. ## AI use AI-assisted review (source reading). No build, no hardware run, no game files.
Author
Owner

@⁠avatarmanz on GitHub · 2026-11-26 07:55 UTC · original

Merged into main in the next push (tests pass); thanks.

**@⁠avatarmanz** on GitHub · 2026-11-26 07:55 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/14#issuecomment-7185989799) Merged into main in the next push (tests pass); thanks.
github manually merged commit 150f287edb into main 2026-11-26 08:21:46 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.