d3d12_xs: drop gpu_va ranges when their resource is destroyed #283

Closed
github-bot wants to merge 1 commit from gh-pr/283/head into gh-pr/283/base
Owner

@⁠GDAgent on GitHub · 2026-11-23 11:55 UTC · original
GDAgent:fix/d3d12-gpu-va-forget-destroyed → main · +155 −5 · 1 files · 1 commits · closed without merging

Library / area: d3d12_xs (gpu_va.rs)

Exports changed (exports.txt rows): none

Source of the behaviour: a crash in The Matrix Awakens (Xbox Series X build), native Windows 11 on an RDNA2 GPU (gfx1032); not run under Proton.

gpu_va records the GPU (and mapped CPU) address range of every buffer and texture it is told about, but never removed a range when the title released the resource. The range stayed until a newer resource happened to overlap it, so lookup, lookup_buffer and similar calls could return a resource that no longer exists.

Several callers pass the looked-up resource straight to the driver (the copy paths in console_slots.rs and cl_console_fix.rs, fill.rs, gds.rs, bop64.rs, ...), so a stale entry there means a call on a destroyed resource.

Evidence: I hit it with a not-yet-submitted path of ours that writes buffer descriptors for every tracked buffer (it uses the same gpu_va tables). About 31 s into loading, the title crashed inside the runtime's CreateShaderResourceView while those descriptors were being written. The faulting instruction loads the resource's vtable (mov rax,[rdx]) and then reads slot 10 (mov rax,[rax+0x50]); the access violation was a read at address 0x50, so the vtable pointer of the passed resource was 0, a destroyed object.

Change:

  • note_resource attaches a small COM object to each resource with SetPrivateDataInterface (own GUID), once per resource. The runtime releases private data when the resource is destroyed; the object's last Release calls gpu_va::forget(res).
  • insert / insert_cpu record each range's start per resource. forget removes exactly those entries (only where the entry at that start still belongs to the resource, so another resource's range at the same start, e.g. a nested one on another level, stays), plus the resource's texture VA, width and state.
  • If SetPrivateDataInterface fails, nothing is forgotten and the next note_resource for that resource tries again.

Tests:

  • forget_drops_only_the_ranges_of_that_resource: GPU and CPU ranges of one resource are removed; a neighbour and a larger range sharing a start with a nested one of the forgotten resource stay; a second forget is a no-op.
  • a_destroyed_buffer_leaves_the_tables_and_a_second_note_keeps_the_watch: a committed buffer on the default adapter is noted twice, found by lookup, and is gone (range and state) after its last Release. Without the watcher this test fails: the destroyed buffer is still returned.
  • cargo test -p d3d12_xs --lib -- --test-threads=1 (with TOMBOX_REMOVED_EXIT_MS=off): 663 passed, 5 failed, 71 ignored. The same 4 fail on main here (alias::tests::msaa_depth_with_a_larger_pc_layout_never_overlaps_the_next_title_placement, placed_offset::tests::uneven_views_get_exact_helpers_and_msaa_shares_the_alias_heap, title_shadow::tests::buffer_then_texture_migrates_to_one_local_heap_and_keeps_bytes, xs_exports::console_fixed_tests::fixed_result_dxgix_exports_match_every_console_d3d12_xs); the fifth, dbcount::tests::translated_pixel_shaders_with_uavs_keep_their_source, fails now and then in a full single-threaded run (a pointer-keyed map sees an address reused from an earlier test) and passes on its own.

Verified in a title (partly): with this change on top of a local test branch, a 150 s run of The Matrix Awakens with the same settings that crashed at ~31 s loaded for the whole run without the crash (0 unhandled exceptions; the run ended at its time limit). The title was still loading at the end of the run, so later gameplay is not covered.

  • Tests pass
  • No game or console files and no binaries included
  • No fake success; anything not possible is left missing with a note
**@⁠GDAgent** on GitHub · 2026-11-23 11:55 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/283) `GDAgent:fix/d3d12-gpu-va-forget-destroyed` → `main` · +155 −5 · 1 files · 1 commits · closed without merging **Library / area:** `d3d12_xs` (`gpu_va.rs`) **Exports changed (exports.txt rows):** none **Source of the behaviour:** a crash in The Matrix Awakens (Xbox Series X build), native Windows 11 on an RDNA2 GPU (gfx1032); not run under Proton. `gpu_va` records the GPU (and mapped CPU) address range of every buffer and texture it is told about, but never removed a range when the title released the resource. The range stayed until a newer resource happened to overlap it, so `lookup`, `lookup_buffer` and similar calls could return a resource that no longer exists. Several callers pass the looked-up resource straight to the driver (the copy paths in `console_slots.rs` and `cl_console_fix.rs`, `fill.rs`, `gds.rs`, `bop64.rs`, ...), so a stale entry there means a call on a destroyed resource. Evidence: I hit it with a not-yet-submitted path of ours that writes buffer descriptors for every tracked buffer (it uses the same `gpu_va` tables). About 31 s into loading, the title crashed inside the runtime's `CreateShaderResourceView` while those descriptors were being written. The faulting instruction loads the resource's vtable (`mov rax,[rdx]`) and then reads slot 10 (`mov rax,[rax+0x50]`); the access violation was a read at address 0x50, so the vtable pointer of the passed resource was 0, a destroyed object. Change: - `note_resource` attaches a small COM object to each resource with `SetPrivateDataInterface` (own GUID), once per resource. The runtime releases private data when the resource is destroyed; the object's last `Release` calls `gpu_va::forget(res)`. - `insert` / `insert_cpu` record each range's start per resource. `forget` removes exactly those entries (only where the entry at that start still belongs to the resource, so another resource's range at the same start, e.g. a nested one on another level, stays), plus the resource's texture VA, width and state. - If `SetPrivateDataInterface` fails, nothing is forgotten and the next `note_resource` for that resource tries again. Tests: - `forget_drops_only_the_ranges_of_that_resource`: GPU and CPU ranges of one resource are removed; a neighbour and a larger range sharing a start with a nested one of the forgotten resource stay; a second `forget` is a no-op. - `a_destroyed_buffer_leaves_the_tables_and_a_second_note_keeps_the_watch`: a committed buffer on the default adapter is noted twice, found by `lookup`, and is gone (range and state) after its last `Release`. Without the watcher this test fails: the destroyed buffer is still returned. - `cargo test -p d3d12_xs --lib -- --test-threads=1` (with `TOMBOX_REMOVED_EXIT_MS=off`): 663 passed, 5 failed, 71 ignored. The same 4 fail on `main` here (`alias::tests::msaa_depth_with_a_larger_pc_layout_never_overlaps_the_next_title_placement`, `placed_offset::tests::uneven_views_get_exact_helpers_and_msaa_shares_the_alias_heap`, `title_shadow::tests::buffer_then_texture_migrates_to_one_local_heap_and_keeps_bytes`, `xs_exports::console_fixed_tests::fixed_result_dxgix_exports_match_every_console_d3d12_xs`); the fifth, `dbcount::tests::translated_pixel_shaders_with_uavs_keep_their_source`, fails now and then in a full single-threaded run (a pointer-keyed map sees an address reused from an earlier test) and passes on its own. **Verified in a title (partly):** with this change on top of a local test branch, a 150 s run of The Matrix Awakens with the same settings that crashed at ~31 s loaded for the whole run without the crash (0 unhandled exceptions; the run ended at its time limit). The title was still loading at the end of the run, so later gameplay is not covered. - [x] Tests pass - [x] No game or console files and no binaries included - [x] No fake success; anything not possible is left `missing` with a note
gpu_va keeps the GPU and CPU address ranges of every buffer and texture
it was told about, but nothing removed them when the title released the
resource. A range stayed until a newer resource overlapped it, so a
lookup could return a resource that no longer exists. Code that turns
such an entry into a descriptor then passes a destroyed resource to the
driver: in The Matrix Awakens (Xbox Series X build) this crashed inside
CreateShaderResourceView about 31 s into loading, reading the vtable of
the freed resource (AV READ at 0x50, the vtable pointer was 0).

note_resource now attaches a small private-data object to each resource
(SetPrivateDataInterface with its own GUID), once per resource. The
runtime releases it when the resource is destroyed, and its last Release
removes that resource's ranges (recorded per resource at insert time, so
an entry of another resource at the same start is left alone), its
texture VA, width and state. If SetPrivateDataInterface fails, nothing is
forgotten and the next note retries.

Tests: forget removes only the ranges of that resource, including a
smaller range nested at the same start as another resource's; a real
committed buffer noted twice is found by lookup and is gone after its
last Release (the test fails without the watcher: the destroyed buffer is
still returned).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:17:15 +00:00
github-bot closed this pull request 2026-11-26 06:17:15 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-23 13:45 UTC · original

Applied locally on main with your authorship kept (cherry-picked, 0cc4e3b); tests pass in a sandbox. It ships in the next release; this PR will be closed when that is pushed. Thanks.

**@⁠omonkeyz** on GitHub · 2026-11-23 13:45 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/283#issuecomment-7102985311) Applied locally on main with your authorship kept (cherry-picked, 0cc4e3b); tests pass in a sandbox. It ships in the next release; this PR will be closed when that is pushed. Thanks.
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-25 13:09 UTC · original

Applied on main in 0cc4e3b (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

**@⁠omonkeyz** on GitHub · 2026-11-25 13:09 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/283#issuecomment-7163376761) Applied on main in 0cc4e3b (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

Pull request closed

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