Skip to content

d3d12: a guard page fails a copy and stays a guard page (D3D12-GPU-SAFECOPY-GUARD-PAGE-REARM) - #2709

Merged
OreoFR merged 10 commits into
ProjectLeonida:mainfrom
Kal0s-design:fix/safecopy-guard-rearm
Dec 1, 2026
Merged

OreoFR merged 10 commits into
ProjectLeonida:mainfrom
Kal0s-design:fix/safecopy-guard-rearm

Conversation

@Kal0s-design

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-GPU-SAFECOPY-GUARD-PAGE-REARM (#2570 (comment))

What and why

Title memory has guard pages: stack guards, and guards the title sets up itself. When a copy, put or merge of title memory (safecopy, running under the write-catch handler in watch.rs) touches one, it takes STATUS_GUARD_PAGE_VIOLATION, and Windows clears the page's guard flag as it raises that. The handler 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 own next touch of it no longer raised the guard fault the title set it up for: a stack that can't grow, or a guard the title waits on that never fires.

The change:

  • A guard fault inside a copy re-arms the guard flag on its page, with the protection the page has now, and fails the copy exactly as before.
  • rearm_guard checks that the page is still committed and that VirtualProtect succeeds. If it can't re-arm, or the fault carried no address, it counts the case in NOT_REARMED and logs the first one.
  • A guard fault outside our copies still goes to the next handler.

This is a port of zocram4cc's fix and tests from the sibling VIctory tree (TaynanGT/Vice-Tay fa11650 and c277533, GPL-3.0), credited as co-author. In their traced runs, a lost guard was linked to silent exits a few minutes into the world under Proton. That link is their reading of a trace, not something reproduced here.

Tested

  • Four tests ported to safecopy::tests: a guard page in the source of a copy, and in the destination of a copy, a put and a merge. Each checks that the call fails and the page is still a guard page. On main (454df8e) all four fail on native Windows ("the guard flag was consumed" / "...cleared the guard flag"); with the change they pass.
  • watch::rearm_tests::a_page_that_cannot_be_guarded_again_is_counted: a committed page is guarded again, a decommitted one is not, and both that case and a fault with no address are counted.
  • cargo test -p leonida-gpu --lib: 564 passed, 0 failed, 50 ignored.
  • cargo clippy -p leonida-gpu --all-targets (with and without --all-features): no warning in the changed lines. The warnings watch.rs already has are untouched.
  • Windows only. Linux and macOS are not covered; Wine/Proton isn't covered here either.

GPUs tested: n/a (CPU unit tests)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code

MoonEyes and others added 7 commits November 30, 2026 10:29
…_* variables

d3d12/docs/findings.md was reachable from d3d12/README.md alone, so a night of
runs repeated measurements it already holds. Index it, point AGENTS.md at it,
say how to measure (a switch by turns inside one run), and list the variables
the runtime names from its own sources: 415 of 669 were in no Markdown file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The launcher column saw one way of assigning of four, three kinds were wrong,
names read by turns through a constant, a function or asked_for! were missing,
and test modules with a brace in a raw string were cut short. measuring_fps.md
now says how to give LEONIDA_AB to the launcher and what GPU_TIMING and
PROFILE need.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tests/test_tool_stdout_encoding.py asks it of every tool. d3d12_run_fps.py came
in with ProjectLeonida#2594 without it, which left that test red on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Title memory has guard pages: stack guards, and guards the title sets up
itself. A copy, put or merge of title memory (safecopy, under the
write-catch handler in watch.rs) that touched one took
STATUS_GUARD_PAGE_VIOLATION, which clears the page's guard flag. The
handler 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.

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. The
re-arm checks that the page is still committed and that VirtualProtect
succeeds; a re-arm that fails, or a guard fault with no address, is
counted (NOT_REARMED) and the first one is logged. A guard fault outside
a copy still goes on to the next handler.

Tests: the source and destination cases for copy, put and merge (all four
fail on main with "...cleared the guard flag" / "the guard flag was
consumed"), and a_page_that_cannot_be_guarded_again_is_counted.

Port of zocram4cc's fix and tests in the sibling VIctory tree
(TaynanGT/Vice-Tay fa11650 and c277533, GPL-3.0).

Co-Authored-By: zocram4cc <79211006+zocram4cc@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@merzleckeier

Copy link
Copy Markdown
Contributor

Independent Wine verification. I merged 42b1f4ae4 onto 012b79a67 (tree fa6cddb25) and cross-built the stock leonida-gpu library tests for x86_64-pc-windows-msvc with Rust 1.98.1 and xwin. Tested on CachyOS with Wine 11.22, both with and without the diagnostics feature.

  • safecopy::tests:: passed 11 tests in each build, including all four new guard-page cases. The existing copy_costs benchmark was ignored.
  • watch::rearm_tests:: passed in each build. The committed page was re-armed, and the two failure cases were counted.
  • In the wider watch::tests:: family, the diagnostics build had 33 passes and one failure on both main and the PR. The PR build without diagnostics had 30 passes and the same failure. what_is_left_alone_is_no_known_hole fails at (0..3).all(known) == holes. I reproduced it in isolation on both trees, and the isolated case passed on both with LEONIDA_KNOWN_HOLES=0 as a control.

CPU tests using synthetic allocations. AI-assisted with OpenAI Codex.

@Kal0s-design

Copy link
Copy Markdown
Contributor Author

Merged main (d367675c7, all CI green there) into the branch, as MoonEyes asked on #2710 for the same two red checks: new head 6a206d82c (merge commit, no rebase or force-push). cargo test -p leonida-gpu --lib on the new head: 568 passed, 52 ignored, 0 failed. merzleckeier's Wine results are on the previous head 42b1f4ae4; the PR's own change is unchanged.

AI-assisted: yes

@KelboLive

Copy link
Copy Markdown

Independent check on Windows (not the author), at 6a206d82c.

  • cargo test --locked --jobs 8 -p leonida-gpu --lib -- --test-threads=8: 568 passed, 0 failed, 52 ignored in 15.6 s (build included separately).
  • Integration build: cargo build --locked --profile diag -p leonida --features diagnostics,mimalloc — succeeded in 2m 09s.

The PR's diff adds no unit test (source-only in safecopy.rs/watch.rs), so this is compile + regression coverage; the guard-page rearm behaviour itself needs a game run, which I did not do.

Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, NVIDIA RTX 4070 Ti SUPER (driver 621.01), Rust 1.98.1 pinned.

AI-assisted: yes (opencode).

@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Verified on Windows, for a merge (not the author). AI-assisted (Claude Code agent oreofr-agent). RTX 4070 Laptop 8 GB, NVIDIA 621.40, Core Ultra 7 155H, Windows 11 Pro, Rust 1.98.1 pinned, cargo test --locked.

Head 6a206d82c merged into current main 90c514437 (clean), tested in one batch with #2589, #2766, #2709 and #2802 (main + all four, merged in that order, clean).

  • Red by mutation: with the guard re-arm in the handler turned off (if false in place of the STATUS_GUARD_PAGE_VIOLATION test), the four safecopy guard-page tests fail. Read in code: not_rearmed logs from the vectored handler only on the first failure, and the faulting thread is inside a safe copy, not inside a log call, so it cannot hold the logger's lock there. The doc line "Whether at is committed read-write memory now." now sits above rearm_guard instead of writable (watch.rs ~4569), cosmetic. The mutation was reverted after the run.
  • Green / neighbours: leonida-gpu main 589 passed / 0 failed, main + the four 598 / 0; leonida-hle 242 / 0 on both.
  • In-game run of the batch (diagnostics,mimalloc, no DLSS), fresh copy of our gate data, -SeriesS -NoDlss -NoFrameGen, BAR 0, texture budget 1024, walking (keys file, 115 key changes): 330 s in the world, 0 device loss, 0 ERROR lines, no crash, clean close. Early stalls while ~169 pipelines were first made (as on main), then steady 21-23 fps (median of the last 30 fps lines 21.6), 24 GB private at the end.

One run of several pull requests together covers each at its head (docs/maintaining.md, #2797); if a merge of one of them breaks main, the others are split out and re-run.

@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Merging (after #2589, #2766). Head 6a206d82c merges cleanly with main 10bed9ff2 (merge commit 1fc92830b).
Claim: in the description, a comment in #2570 by the author (checked).
Red/green: by oreofr-agent (not the author), RTX 4070 Laptop, Windows 11: red by mutation on this head (comment above). I ran on main 86c30fcea + #2589, #2766, #2709, #2802 (this order): cargo test --locked -p leonida-gpu 599 passed, 0 failed (main alone 590/0); -p leonida-hle 242/0. An independent second check (separate agent) found no blocker.
In-game run: RTX 4070 Laptop 8 GB, NVIDIA 621.40, the four together at their heads on 90c514437: 330 s in the world walking, 0 device loss, 0 ERROR lines; same-scene picture against main: no visible change. Since then main only gained the two CI fixes (#2825, #2827: a test-module declaration and a tool's __main__), nothing that runs in the game. Default change with no switch: a guard page that stops a safe copy is guarded again (a revert undoes it). Note: GitHub's file list shows 13 files because of a stale base; the real diff is safecopy.rs and watch.rs.
CI: green on the head (5/5) and on main (CI, No Game Data on 86c30fcea; Runtime last green on cd9fe51a9).
AI-assisted: yes, Claude Code agent oreofr-agent, merged on OreoFR's (maintainer) written instruction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants