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

Merged
Kal0s-design merged 10 commits from upstream-pr/2709 into main 2026-12-01 05:22:15 +00:00
Kal0s-design commented 2026-11-30 10:28:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-11-30T10:28:59Z.


Claimed in the coordination issue as: D3D12-GPU-SAFECOPY-GUARD-PAGE-REARM (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7275542541)

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 (454df8e95) 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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5563458387 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2709). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-11-30T10:28:59Z. --- Claimed in the coordination issue as: D3D12-GPU-SAFECOPY-GUARD-PAGE-REARM (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7275542541) ### 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` (454df8e95) 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 - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] 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](https://claude.com/claude-code)
merzleckeier commented 2026-11-30 10:35:42 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T10:35:42Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7275815155 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2709#issuecomment-7275815155). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-11-30T10:35:42Z. --- **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 commented 2026-11-30 12:14:04 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-11-30T12:14:04Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7278145377 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2709#issuecomment-7278145377). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-11-30T12:14:04Z. --- 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 commented 2026-11-30 15:24:47 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-11-30T15:24:47Z.


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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7282953380 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2709#issuecomment-7282953380). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-30T15:24:47Z. --- 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 commented 2026-12-01 03:16:28 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T03:16:28Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7299531021 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2709#issuecomment-7299531021). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-12-01T03:16:28Z. --- **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 commented 2026-12-01 05:22:22 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T05:22:22Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7301701254 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2709#issuecomment-7301701254). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-12-01T05:22:22Z. --- 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 in to join this conversation.
No reviewers
No labels
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.

Reference
ProjectLeonida/mandy!2709
No description provided.