d3d12_xs: read placed-texture memory through fault-safe snapshots (fixes #110) #146

Closed
github-bot wants to merge 2 commits from gh-pr/146/head into gh-pr/146/base
Owner

@⁠MeNoSkills on GitHub · 2026-11-22 15:39 UTC · original
MeNoSkills:fix/upload-decommit-race → main · +99 −3 · 1 files · 2 commits · closed without merging

Fixes #110.

Problem

The texture uploader in crates/compat/d3d12_xs/src/upload.rs read the title's placed-resource memory directly (hashing, de-tiling, the sparse copy, and the zero watch). On console, CPU and GPU share memory with hardware-managed residency, so a title thread can never pull that memory out from under a reader. On PC the backing is VirtualAlloc'd, and a title that decommits it while the uploader is mid-read causes an access violation with no recovery path. The old module doc already listed this as a known limit ("a decommit racing the read would fault").

Change

All reads of title memory now go through read_title_memory / snapshot_title_memory, built on ReadProcessMemory on the current process. It reports a partial read as a failure instead of faulting.

  • check(): each changed subresource is snapshotted once, then hashed and de-tiled from the private copy (this also removes the window where the hash and the de-tile could see different data). If the snapshot fails, the subresource is counted as unreadable, left without a hash so it is retried on the next pump, and skipped. The existing sparse path keeps its behaviour: unreadable ranges stay zero.
  • first_nonzero (zero watch) reads in 64 KiB chunks; an unreadable chunk counts as "changed", so the subresource is re-examined by the normal path.
  • watch_pending hashes a snapshot and skips the subresource if it cannot be read.
  • I removed the doc-comment line that described the fault as a limit, since it no longer applies (the PR gate forbids added comment lines, so the explanation lives here).

No new dependencies. No behaviour change when memory stays readable, apart from one extra copy per checked subresource.

Tests

  • New decommitted_memory_fails_the_snapshot_instead_of_faulting: commits three pages, decommits the middle one, and checks that a snapshot spanning it fails, that snapshots of the intact pages still succeed, and that first_nonzero over the decommitted range returns true rather than crashing.
  • cargo test --locked -p d3d12_xs --lib upload: 12 passed, 0 failed, 1 ignored.
  • python .github/scripts/pr_gate.py --base upstream/main --head HEAD: 0 problems.

Not verified

I have no title dump, so I have not run this against Fortnite. The cost of the extra copy per subresource is unmeasured on a real texture-streaming run; the per-pump byte budgets are unchanged. A maintainer run with the lobby would confirm the race is gone and the pump time stays within budget.

**@⁠MeNoSkills** on GitHub · 2026-11-22 15:39 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/146) `MeNoSkills:fix/upload-decommit-race` → `main` · +99 −3 · 1 files · 2 commits · closed without merging Fixes #110. ## Problem The texture uploader in `crates/compat/d3d12_xs/src/upload.rs` read the title's placed-resource memory directly (hashing, de-tiling, the sparse copy, and the zero watch). On console, CPU and GPU share memory with hardware-managed residency, so a title thread can never pull that memory out from under a reader. On PC the backing is VirtualAlloc'd, and a title that decommits it while the uploader is mid-read causes an access violation with no recovery path. The old module doc already listed this as a known limit ("a decommit racing the read would fault"). ## Change All reads of title memory now go through `read_title_memory` / `snapshot_title_memory`, built on `ReadProcessMemory` on the current process. It reports a partial read as a failure instead of faulting. - `check()`: each changed subresource is snapshotted once, then hashed and de-tiled from the private copy (this also removes the window where the hash and the de-tile could see different data). If the snapshot fails, the subresource is counted as unreadable, left without a hash so it is retried on the next pump, and skipped. The existing sparse path keeps its behaviour: unreadable ranges stay zero. - `first_nonzero` (zero watch) reads in 64 KiB chunks; an unreadable chunk counts as "changed", so the subresource is re-examined by the normal path. - `watch_pending` hashes a snapshot and skips the subresource if it cannot be read. - I removed the doc-comment line that described the fault as a limit, since it no longer applies (the PR gate forbids added comment lines, so the explanation lives here). No new dependencies. No behaviour change when memory stays readable, apart from one extra copy per checked subresource. ## Tests - New `decommitted_memory_fails_the_snapshot_instead_of_faulting`: commits three pages, decommits the middle one, and checks that a snapshot spanning it fails, that snapshots of the intact pages still succeed, and that `first_nonzero` over the decommitted range returns true rather than crashing. - `cargo test --locked -p d3d12_xs --lib upload`: 12 passed, 0 failed, 1 ignored. - `python .github/scripts/pr_gate.py --base upstream/main --head HEAD`: 0 problems. ## Not verified I have no title dump, so I have not run this against Fortnite. The cost of the extra copy per subresource is unmeasured on a real texture-streaming run; the per-pump byte budgets are unchanged. A maintainer run with the lobby would confirm the race is gone and the pump time stays within budget.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:11:44 +00:00
github-bot closed this pull request 2026-11-26 06:11:44 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-22 15:44 UTC · original

Thanks for this, and for the thorough write-up and the decommit test! The race is real, but this is the per-frame texture path, and we can't take an extra copy plus ReadProcessMemory calls there without numbers. Two changes, please:

  1. Off by default. Keep the direct reads as the default path. Use the snapshot path only behind an env var, or only on the validation-failure path (when a range fails its check).
  2. Show the cost. Either post the per-call cost on a real draw-heavy workload, or add a micro-bench showing the default path is unchanged.

Longer term, the root fix for #110 is to validate the title pointer where it's used, not to copy memory on every call. If you'd like to take that on instead, it would be very welcome.

**@⁠omonkeyz** on GitHub · 2026-11-22 15:44 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/146#issuecomment-7081539441) Thanks for this, and for the thorough write-up and the decommit test! The race is real, but this is the per-frame texture path, and we can't take an extra copy plus `ReadProcessMemory` calls there without numbers. Two changes, please: 1. **Off by default.** Keep the direct reads as the default path. Use the snapshot path only behind an env var, or only on the validation-failure path (when a range fails its check). 2. **Show the cost.** Either post the per-call cost on a real draw-heavy workload, or add a micro-bench showing the default path is unchanged. Longer term, the root fix for #110 is to validate the title pointer where it's used, not to copy memory on every call. If you'd like to take that on instead, it would be very welcome.
Author
Owner

@⁠MeNoSkills on GitHub · 2026-11-22 15:52 UTC · original

Thanks for the review. Both changes are in 0744c01.

  1. Off by default. The default path is the original direct reads again (same code as main, plus one cached-switch check per call). The snapshot path is behind TOMBOX_UPLOAD_SAFE_READ=1 (live key upload_safe_read), so a title that hits #110 can opt in to skip the racing subresource instead of crashing.

  2. Cost. I added a micro-bench (default_reads_are_direct_and_the_safe_path_costs_one_copy) that hashes a buffer through both paths and checks that they produce identical results. Release build, one run on one machine:

hash of direct safe snapshot
16 KiB 2759 ns/call 4169 ns/call (+51%)
256 KiB 43996 ns/call 56605 ns/call (+29%)
4 MiB 704306 ns/call 1477694 ns/call (+110%)

The extra cost is the copy, which is exactly why it stays opt-in. These are micro-bench numbers, not a draw-heavy workload: I have no title dump, so I can't give per-frame numbers. The default path adds no copy.

I'd be glad to take the root fix (validate the title pointer where it is used, instead of copying). Could you point me to where you'd want that validation to live? My guess is a check against the same VirtualQuery scan already done in check(), re-run immediately before the read, but that still leaves a window, so I'd like your view before I build it.

**@⁠MeNoSkills** on GitHub · 2026-11-22 15:52 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/146#issuecomment-7081641974) Thanks for the review. Both changes are in 0744c01. 1. Off by default. The default path is the original direct reads again (same code as main, plus one cached-switch check per call). The snapshot path is behind TOMBOX_UPLOAD_SAFE_READ=1 (live key `upload_safe_read`), so a title that hits #110 can opt in to skip the racing subresource instead of crashing. 2. Cost. I added a micro-bench (`default_reads_are_direct_and_the_safe_path_costs_one_copy`) that hashes a buffer through both paths and checks that they produce identical results. Release build, one run on one machine: | hash of | direct | safe snapshot | |---|---|---| | 16 KiB | 2759 ns/call | 4169 ns/call (+51%) | | 256 KiB | 43996 ns/call | 56605 ns/call (+29%) | | 4 MiB | 704306 ns/call | 1477694 ns/call (+110%) | The extra cost is the copy, which is exactly why it stays opt-in. These are micro-bench numbers, not a draw-heavy workload: I have no title dump, so I can't give per-frame numbers. The default path adds no copy. I'd be glad to take the root fix (validate the title pointer where it is used, instead of copying). Could you point me to where you'd want that validation to live? My guess is a check against the same VirtualQuery scan already done in `check()`, re-run immediately before the read, but that still leaves a window, so I'd like your view before I build it.
Author
Owner

@⁠MiguelMatMar on GitHub · 2026-11-22 16:03 UTC · original

A second data point for the micro-bench, release build at PR head, 8-core Zen 2 desktop, three runs after one warm-up run:

hash of direct safe snapshot
16 KiB 2306-2439 ns/call 3794-4004 ns/call (+64%)
256 KiB 37023-39697 ns/call 51386-54379 ns/call (+37-39%)
4 MiB 593738-626825 ns/call 1370106-1466319 ns/call (+131-134%)

This matches your table. The 4 MiB overhead is a bit higher here (+131% against +110%). The first, cold run showed +181% at 256 KiB, so single runs are noisy at that size. The results of both paths were identical in every run.

I also checked that this PR and #121 (both touch upload.rs) merge cleanly with each other and with current main.

**@⁠MiguelMatMar** on GitHub · 2026-11-22 16:03 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/146#issuecomment-7081780020) A second data point for the micro-bench, release build at PR head, 8-core Zen 2 desktop, three runs after one warm-up run: | hash of | direct | safe snapshot | |---|---|---| | 16 KiB | 2306-2439 ns/call | 3794-4004 ns/call (+64%) | | 256 KiB | 37023-39697 ns/call | 51386-54379 ns/call (+37-39%) | | 4 MiB | 593738-626825 ns/call | 1370106-1466319 ns/call (+131-134%) | This matches your table. The 4 MiB overhead is a bit higher here (+131% against +110%). The first, cold run showed +181% at 256 KiB, so single runs are noisy at that size. The results of both paths were identical in every run. I also checked that this PR and #121 (both touch `upload.rs`) merge cleanly with each other and with current main.
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-22 16:11 UTC · original

Applied in 595eb3a and 360cd07 (merged in 1a1e283) with your authorship kept, thanks for the quick turnaround and the bench! One change: I stripped the two //! doc lines in upload.rs. The repo allows no comment lines at all, including doc comments. #110 stays open for the root fix: checking the title pointer where it's used.

**@⁠omonkeyz** on GitHub · 2026-11-22 16:11 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/146#issuecomment-7081877703) Applied in 595eb3a and 360cd07 (merged in 1a1e283) with your authorship kept, thanks for the quick turnaround and the bench! One change: I stripped the two //! doc lines in upload.rs. The repo allows no comment lines at all, including doc comments. #110 stays open for the root fix: checking the title pointer where it's used.
Author
Owner

@⁠MeNoSkills on GitHub · 2026-11-22 16:11 UTC · original

A second data point for the micro-bench, release build at PR head, 8-core Zen 2 desktop, three runs after one warm-up run:

hash of direct safe snapshot
16 KiB 2306-2439 ns/call 3794-4004 ns/call (+64%)
256 KiB 37023-39697 ns/call 51386-54379 ns/call (+37-39%)
4 MiB 593738-626825 ns/call 1370106-1466319 ns/call (+131-134%)
This matches your table. The 4 MiB overhead is a bit higher here (+131% against +110%). The first, cold run showed +181% at 256 KiB, so single runs are noisy at that size. The results of both paths were identical in every run.

I also checked that this PR and #121 (both touch upload.rs) merge cleanly with each other and with current main.

Thanks for running it on a second machine and for checking the merge with #121. Agreed that single runs at 256 KiB are noisy, which is why the numbers in the description came from steady-state loops. Nothing else changes on my side; happy to follow up on the validate-where-used root fix once you have a view on where it should live.

**@⁠MeNoSkills** on GitHub · 2026-11-22 16:11 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/146#issuecomment-7081885319) > A second data point for the micro-bench, release build at PR head, 8-core Zen 2 desktop, three runs after one warm-up run: > > hash of direct safe snapshot > 16 KiB 2306-2439 ns/call 3794-4004 ns/call (+64%) > 256 KiB 37023-39697 ns/call 51386-54379 ns/call (+37-39%) > 4 MiB 593738-626825 ns/call 1370106-1466319 ns/call (+131-134%) > This matches your table. The 4 MiB overhead is a bit higher here (+131% against +110%). The first, cold run showed +181% at 256 KiB, so single runs are noisy at that size. The results of both paths were identical in every run. > > I also checked that this PR and #121 (both touch `upload.rs`) merge cleanly with each other and with current main. Thanks for running it on a second machine and for checking the merge with #121. Agreed that single runs at 256 KiB are noisy, which is why the numbers in the description came from steady-state loops. Nothing else changes on my side; happy to follow up on the validate-where-used root fix once you have a view on where it should live.

Pull request closed

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