copies of title memory: a guard page fails the copy and stays a guard page #14
No reviewers
Labels
No labels
accessibility
already-solved
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
merged
needs-decision
needs-game-run
needs-rebase
not-merged
nvidia
obsolete
question
security
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/avatarmanz--VIctory-7900XTX!14
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-14"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 UTCcopies 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, aSTATUS_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_mergeinsafecopy.rs) reads or writes a guard page, the OS clears the page's guard flag and raises0x80000001.on_writeinwatch.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: a0x80000001raised inside one of our copies now calls a new helper,rearm_guard, before failing the copy exactly as before.rearm_guardsetsPAGE_GUARDagain on the 4 KiB page at the faulting address (ExceptionInformation[1]), keeping the protection the page has now.EXCEPTION_CONTINUE_SEARCH, so whatever handles them (the OS, or Wine) still runs.watch.rs, andsafecopy.rsfor 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: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.... ok. The wholesafecopy::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-msvcpasses on this branch.Build on the upstream base (
v7900/main,up/guard-page=c277533on0a43566):cargo xwin build --profile diag -p victory --features diagnostics --target x86_64-pc-windows-msvcpasses, and thesafecopy::tests pass under Proton there too (11 passed, 1 ignored).Evidence from the rig
code=80000001(STATUS_GUARD_PAGE_VIOLATION) among about 3 million access violations from the write-catch handler.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, commit478c6ef, "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 thesafecopypath.Windows
This should matter on Windows too, wherever title guard pages meet our copies:
safecopyis on by default on every platform (VICTORY_SAFE_COPYunset or not0), 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 fromExceptionInformation[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:
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_guardedand 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.rearm_guardnow checks that the page is still committed (VirtualQuery) and thatVirtualProtectsucceeds. A failed re-arm, or a guard fault that gives no address, is counted inNOT_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).VICTORY_SAFE_COPYon, no caller ofcopy,put_changedormergefalls back toReadProcessMemory,NtWriteVirtualMemory,WriteProcessMemoryor a memcpy. Those calls exist only in theVICTORY_SAFE_COPY=0branches (host.rs:readandwrite_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.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, andsafecopy::gives 11 passed, 1 ignored, on both branches. Thediagbuild passes on both.@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):watch.rs("the copy's / put's / merge's fault cleared the guard flag"), so they now pin it.rearm_guardnow checks thatVirtualQueryreportsMEM_COMMITand thatVirtualProtectsucceeds; a failed re-arm, or a guard fault without an address (NumberParameters < 2), is counted (NOT_REARMED) and the first one logs a warning. Testa_page_that_cannot_be_guarded_again_is_counted(red against a stub that always succeeds).safecopypath. Every caller of copy/put/merge retries piecewise through the same routines (read,zero,write_mapped's 4 KiB pieces,put_changedper page, land'smerge(true)), each of which re-arms on its own fault.ReadProcessMemory/NtWriteVirtualMemory/WriteProcessMemoryare only reached withVICTORY_SAFE_COPY=0; the list with file:line is in the description.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_aliassetPAGE_READONLYover 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.@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_writere-arms for every fault inside one of our copies that is not a writeEXCEPTION_ACCESS_VIOLATION— which includes guard faults raised on the destination ofput_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_guardignores both return values:VirtualQuerycan report a page that is no longer committed (title unmapped or decommitted the range between the fault and the handler) andVirtualProtectcan fail. In both cases the guard flag stays consumed and nothing is printed — which is the bug this PR fixes, one level down. Onecopystatcounter (or a one-shottracing::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(...) == falseby falling back toReadProcessMemory/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 throughrearm_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]forSTATUS_GUARD_PAGE_VIOLATIONis the accessed address on Windows too, but the check is one test run on native Windows away from being certain, andsafecopyis 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_GUARDis preserved throughinfo.Protect | PAGE_GUARD, and re-arming beforefail(exit)is the right order.AI use
AI-assisted review (source reading). No build, no hardware run, no game files.
@avatarmanz on GitHub · 2026-11-26 07:55 UTC · original
Merged into main in the next push (tests pass); thanks.