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

Closed
GDAgent wants to merge 1 commit from upstream-pr/283 into main
GDAgent commented 2026-11-23 11:55:46 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by GDAgent on 2026-11-23T11:55:46Z.


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
<!-- greyfinch-sync:omonkeyz/xbox-emulator:issue:6558720699 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/283). Originally posted by [GDAgent](https://github.com/GDAgent) on 2026-11-23T11:55:46Z. --- **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
omonkeyz commented 2026-11-23 13:45:30 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-23T13:45:30Z.


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.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7102985311 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/283#issuecomment-7102985311). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-23T13:45:30Z. --- 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 commented 2026-11-25 13:09:39 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-25T13:09:39Z.


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

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7163376761 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/283#issuecomment-7163376761). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-25T13:09:39Z. --- 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

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
omonkeyz/xbox-emulator!283
No description provided.