d3d12_xs: guard reads of placed-texture memory with a fault-recovering call (root fix for #110) #153
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
library
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/omonkeyz--xbox-emulator!153
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/153/head"
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?
@MeNoSkills on GitHub · 2026-11-22 16:19 UTC · original
MeNoSkills:fix/upload-guarded-reads→main· +487 −71 · 3 files · 2 commits · closed without mergingFollow-up to #146, which kept direct reads by default and offered a copying snapshot behind an env var. You asked for the root fix: handle a title pointer that goes bad where it is used, without a copy per call. Refs #110.
Change
guard.rs:guarded(f) -> Option<R>runsfthrough a small x86-64 trampoline (global_asm!) and a vectored exception handler. Ifftakes an access violation or in-page error while a guard is active on that thread, the handler restores the stack and registers saved by the trampoline and the call returnsNone. It is a__try/__exceptequivalent for a closure. Nonvolatile registers (including xmm6-xmm15) are saved and restored by the trampoline; the handler is installed once and only acts when the current thread has an active guard, otherwise it lets the exception continue.upload.rs: every direct read of title memory runs under the guard: the hash, the all-zero test, the sparse copy, the de-tile (also inside the parallel workers) and the zero-watch scan. When a read fails, the subresource is counted as unreadable, keeps no hash and is retried on a later pump, as the existing "unreadable placed texture" path does.ReadProcessMemorysnapshot andTOMBOX_UPLOAD_SAFE_READ, which the guard makes unnecessary.Cost
No copy and no extra pass on the default path. Micro-bench
guarded_reads_cost_one_call_per_range_not_per_byte(release, one run, one machine), hash of a buffer with and without the guard:The remaining cost is one trampoline call and one thread-local write per guarded range. These are micro-bench numbers; I have no title dump, so I cannot give per-frame numbers.
Tests
guard::tests: normal result, decommitted page returnsNone, nested guards, 1000 faults with a running accumulator to check the saved registers, a concurrent decommit/recommit thread against a reader (20000 reads: 19557 completed, 443 faulted and were skipped, no crash), and the guard is cleared afterwards.upload::zero_watch_tests::decommitted_memory_skips_the_read_instead_of_faulting: hash, zero test and de-tile all fail cleanly on a decommitted page and succeed on the intact ones.cargo test --locked -p d3d12_xs --lib: 459 passed, 27 failed. The same 27 fail on current main in my environment (Windows 10 build 19045, not Windows 11), main has 453 passing.python .github/scripts/pr_gate.py --base upstream/main --head HEAD: 0 problems.Caveats
@omonkeyz on GitHub · 2026-11-22 16:28 UTC · original
Thanks, this is the right direction for #110: no copy, and the bench shows no measurable cost. The asm looks right to me (alignment, xmm6-15 and the nonvolatile registers, shadow space, and the CONTEXT offsets), and the extern "C" trampoline makes a panic abort rather than unwind into code with no unwind info. It will be applied once these four conditions hold:
ExceptionInformation[1]) is outside our own modules' images and heaps, i.e. a title address. In every other case it returnsEXCEPTION_CONTINUE_SEARCH, so a bug in our own code still crashes visibly.#[track_caller]location passed intoguarded), so recovered faults never pass silently.fn(not a capturing closure), or add a test that pins this down.First = 0, or explain in the PR whyFirst = 1is needed and safe.Please also rebase onto current main (
604b5a1). Thanks again!@MeNoSkills on GitHub · 2026-11-22 16:35 UTC · original
Thanks for the careful review. Rebased onto
604b5a1and updated in112da49; all four conditions are in.Narrow catch.
guarded(lo, len, f, ctx)now takes the title range the closure is about to read. The handler acts only when a guard is active on the thread, the code is an access violation or in-page error, there are at least 2 exception parameters, and the fault address (ExceptionInformation[1]) is inside [lo, lo+len). Anything else returns EXCEPTION_CONTINUE_SEARCH, so a bug in our own code (a stray pointer, a write to the upload buffer) still crashes visibly. I used the declared range instead of "outside our images and heaps" because it is stricter and does not need to enumerate heaps. Tests cover the boundaries (first and last byte, one past either end, address 0, one parameter, other exception codes) and drive the handler with a fabricated EXCEPTION_RECORD and CONTEXT to check it leaves them untouched when it declines.Logging. Every recovered fault is recorded with the fault address, the faulting RIP and the call site (#[track_caller] through the helpers, so it points into upload.rs), rate limited to the first 32 and then every 1024th. The log call happens after recovery, outside the handler. There is also a
recovered()counter, which the tests use.Trivial by structure. There are no closures any more.
guardedtakes a plainunsafe extern "C" fn(*mut c_void)and a pointer to a job struct of plain data (hash, non-zero scan, sparse copy, de-tile). Each job has aconst _: () = assert!(!needs_drop::<Job>()), so adding a field with a destructor fails to compile. The job functions do not allocate or take locks. This does not enforce the lock/alloc rule by type, only by structure and review.Coexistence. Registered with First = 0, so the title's, DXC's and other VEH handlers run before ours. I do not have a case that needs First = 1.
cargo test --locked -p d3d12_xs --lib: 462 passed, 27 failed; the same 27 fail on main in my environment (Windows 10 build 19045). Micro-bench guarded vs unguarded hash: +0.5% (16 KiB), +0.1% (256 KiB), -0.1% (4 MiB), i.e. noise. I still have not run this on Windows 11 or with a title.@omonkeyz on GitHub · 2026-11-22 17:24 UTC · original
Applied in
5d5bc33and439826e(merged infe391fe) with your authorship kept, thanks. All four conditions hold:I made one change: in
a_read_of_decommitted_memory_inside_the_range_is_recovered_and_counted, therecovered()check is now>= before + 2instead of exact equality.RECOVEREDis a process-wide counter, and the concurrent-decommit test increments it at the same time, so the exact check failed under the default parallel test run.cargo test -p d3d12_xs --libnow passes 494/494.Pull request closed