D3D12.X: read a view heap's slots in place when scanning for superseded targets #3592

Open
Royalflamejlh wants to merge 2 commits from upstream-pr/3592 into main
Royalflamejlh commented 2026-12-01 17:30:09 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Royalflamejlh on 2026-12-01T17:30:09Z.


Stacked on #3443; only the top commit (00ce5644c) belongs here. (Edited: rebased onto main 1c8b5514; #3443's commit is carried as c7eccd9d9, its author kept, so the branch builds on today's main.)

With #3443, once any render target is superseded, every draw and dispatch bound on XCL's ABI reads each descriptor range its layout declares, one 32-byte slot at a time (about 41 reads a bind), through title_textures::read_committed_into. Under Wine that function asks VirtualQuery region by region (its whole-range read is skipped there, #2844), each call under Wine's one virtual-memory lock. A range inside a registered view heap is now copied where it is: its slots stay committed while the heap is registered. Ranges outside every heap are read as before.

What is in it

  • hle/d3d12x/bind.rs (scan_superseded_in_tables_with): a range inside a registered view heap is read in place; a test that the scan reads such ranges where they are (5 against 0 without the change).

Tested

  • RTX 4060 Ti 16 GB, NVIDIA 615.71.09, AMD Ryzen 9 9950X3D, Linux, Wine 11.21 Staging, Rust 1.98.0, x86_64-pc-windows-gnu, release, head 6ccc2f80c (Foniass's review points fixed; the results below that name 662209739 were taken there) on #3443's 7afecf845: cargo test -p xcl --release --lib -- bind::: 115 passed, 1 failed (gpu_two_pass_indirect_commands_keep_their_own_records, which fails on main 6bfb5c515 here too), 1 SKIPPED.
  • Whole lib suite at 6ccc2f80c, under Wine on CPU Vulkan (no card), cargo test -p xcl --release --lib -- --test-threads=4: 2578 passed, 13 failed, 29 ignored; the parent 7afecf845 fails the same 13; main 6bfb5c51: 2735 passed, 11 failed (the 2 extra here are the indirect_guard tests, fixed on main after this branch's base). Skipped on all three trees: a test that crashes the binary under Wine, the D32S8 test that hangs under Wine, and the uma_driver tests (over 12 GB).
  • In a retail title on a tree that carries #3443 and other unmerged pull requests, with XCL's own memory-call counters (XCL_LOOK_STATS=2, from another fork), 1 run each, 900 s: the VirtualQuery call site in read_committed_into made 150,000 to 195,000 calls a second, 1.12 s of thread time a second on average; frames a second in the world 1.19. With this change and two others of ours together (a cheaper Wine read in read_committed_into, and reusing the memory report's commit sum): that call site was not in the top list, and 3.70 frames a second. The gain from this change alone was not measured.

Regression run
RTX 2070 SUPER 8 GB (driver 621.01), Ryzen 7 5700X3D, Windows 11 Pro 26200, release builds by scripts/smoke.ps1, the same retail title for 60 s each.

main:

xcl-smoke v1
commit: 395405e4c0ed814afde0610a724dc96a3af329de
gpu: NVIDIA GeForce RTX 2070 SUPER
title: 5330474d
seconds: 60
exit: 0x0
frames shown: 1522 (PresentX 1522)
presents per 2 s (last 3): 60 60 61
device removals: 0
crashes: 0
peak commit: 16017 MB

this PR's head:

xcl-smoke v1
commit: 00ce5644ca0e353bfaeea07b819b994a1a1a1c17
gpu: NVIDIA GeForce RTX 2070 SUPER
title: 5330474d
seconds: 60
exit: 0x0
frames shown: 1569 (PresentX 1569)
presents per 2 s (last 3): 60 60 61
device removals: 0
crashes: 0
peak commit: 15184 MB

Not tested

  • Native Windows (there read_committed_into makes one ReadProcessMemory for the range, so the gain is expected to be small), AMD, the whole suite.

AI use

  • Claude Code. Claude Opus 5.5 found the caller, wrote the change and the test, and wrote this description; the title runs were made on the same machine.
<!-- greyfinch-sync:ivall/xcl:issue:5590950604 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3592). Originally posted by [Royalflamejlh](https://github.com/Royalflamejlh) on 2026-12-01T17:30:09Z. --- Stacked on #3443; only the top commit (`00ce5644c`) belongs here. (Edited: rebased onto `main` `1c8b5514`; #3443's commit is carried as `c7eccd9d9`, its author kept, so the branch builds on today's `main`.) With #3443, once any render target is superseded, every draw and dispatch bound on XCL's ABI reads each descriptor range its layout declares, one 32-byte slot at a time (about 41 reads a bind), through `title_textures::read_committed_into`. Under Wine that function asks `VirtualQuery` region by region (its whole-range read is skipped there, #2844), each call under Wine's one virtual-memory lock. A range inside a registered view heap is now copied where it is: its slots stay committed while the heap is registered. Ranges outside every heap are read as before. **What is in it** - `hle/d3d12x/bind.rs` (`scan_superseded_in_tables_with`): a range inside a registered view heap is read in place; a test that the scan reads such ranges where they are (5 against 0 without the change). **Tested** - RTX 4060 Ti 16 GB, NVIDIA 615.71.09, AMD Ryzen 9 9950X3D, Linux, Wine 11.21 Staging, Rust 1.98.0, `x86_64-pc-windows-gnu`, release, head `6ccc2f80c` (Foniass's review points fixed; the results below that name `662209739` were taken there) on #3443's `7afecf845`: `cargo test -p xcl --release --lib -- bind::`: 115 passed, 1 failed (`gpu_two_pass_indirect_commands_keep_their_own_records`, which fails on `main` `6bfb5c515` here too), 1 SKIPPED. - Whole lib suite at `6ccc2f80c`, under Wine on CPU Vulkan (no card), `cargo test -p xcl --release --lib -- --test-threads=4`: 2578 passed, 13 failed, 29 ignored; the parent `7afecf845` fails the same 13; `main` `6bfb5c51`: 2735 passed, 11 failed (the 2 extra here are the `indirect_guard` tests, fixed on `main` after this branch's base). Skipped on all three trees: a test that crashes the binary under Wine, the D32S8 test that hangs under Wine, and the `uma_driver` tests (over 12 GB). - In a retail title on a tree that carries #3443 and other unmerged pull requests, with XCL's own memory-call counters (`XCL_LOOK_STATS=2`, from another fork), 1 run each, 900 s: the `VirtualQuery` call site in `read_committed_into` made 150,000 to 195,000 calls a second, 1.12 s of thread time a second on average; frames a second in the world 1.19. With this change and two others of ours together (a cheaper Wine read in `read_committed_into`, and reusing the memory report's commit sum): that call site was not in the top list, and 3.70 frames a second. The gain from this change alone was not measured. **Regression run** RTX 2070 SUPER 8 GB (driver 621.01), Ryzen 7 5700X3D, Windows 11 Pro 26200, release builds by `scripts/smoke.ps1`, the same retail title for 60 s each. `main`: ``` xcl-smoke v1 commit: 395405e4c0ed814afde0610a724dc96a3af329de gpu: NVIDIA GeForce RTX 2070 SUPER title: 5330474d seconds: 60 exit: 0x0 frames shown: 1522 (PresentX 1522) presents per 2 s (last 3): 60 60 61 device removals: 0 crashes: 0 peak commit: 16017 MB ``` this PR's head: ``` xcl-smoke v1 commit: 00ce5644ca0e353bfaeea07b819b994a1a1a1c17 gpu: NVIDIA GeForce RTX 2070 SUPER title: 5330474d seconds: 60 exit: 0x0 frames shown: 1569 (PresentX 1569) presents per 2 s (last 3): 60 60 61 device removals: 0 crashes: 0 peak commit: 15184 MB ``` **Not tested** - Native Windows (there `read_committed_into` makes one `ReadProcessMemory` for the range, so the gain is expected to be small), AMD, the whole suite. **AI use** - Claude Code. Claude Opus 5.5 found the caller, wrote the change and the test, and wrote this description; the title runs were made on the same machine.
Foniass commented 2026-12-01 20:45:36 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T20:45:36Z.


Review (head 66220973, diff read, not a merge-check; draft, checks red):

  • The heap can be freed between the lookup and the in-place copy. view_heap_remaining_slots (console_heap.rs ~1088) drops HEAPS before it returns, and scan_superseded_in_tables_with then copies the slots straight from the heap's memory (bind.rs ~1341, ~1358) with no lock or lease. If unregister runs on another thread in that gap, free gives the memory back (sparse::free decommits it; title-memory heaps go back through XMemFree), and the raw copy faults where read_committed_into returned zeros. The module's other in-place readers (view_slot_words, exact_view_slot_words, ~1464) keep HEAPS held while they copy for this reason; doing the copy under that lock would close the gap.
  • Regression run and whole-suite counts missing. Regression run has no xcl-smoke blocks (the reason PR description is red), and Tested reports only cargo test -p xcl --release --lib -- bind::.

Fine: the fast path only applies when the table lies in a registered view heap, each range is cut to that heap's end, ranges outside any heap still go through read_committed_into, and the new test would fail without the change. The diff also carries #3443's commit, as the description says.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

<!-- greyfinch-sync:ivall/xcl:comment:7325225981 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3592#issuecomment-7325225981). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T20:45:36Z. --- Review (head 66220973, diff read, not a merge-check; draft, checks red): - **The heap can be freed between the lookup and the in-place copy.** `view_heap_remaining_slots` (`console_heap.rs` ~1088) drops `HEAPS` before it returns, and `scan_superseded_in_tables_with` then copies the slots straight from the heap's memory (`bind.rs` ~1341, ~1358) with no lock or lease. If `unregister` runs on another thread in that gap, `free` gives the memory back (`sparse::free` decommits it; title-memory heaps go back through `XMemFree`), and the raw copy faults where `read_committed_into` returned zeros. The module's other in-place readers (`view_slot_words`, `exact_view_slot_words`, ~1464) keep `HEAPS` held while they copy for this reason; doing the copy under that lock would close the gap. - **Regression run and whole-suite counts missing.** **Regression run** has no `xcl-smoke` blocks (the reason `PR description` is red), and **Tested** reports only `cargo test -p xcl --release --lib -- bind::`. Fine: the fast path only applies when the table lies in a registered view heap, each range is cut to that heap's end, ranges outside any heap still go through `read_committed_into`, and the new test would fail without the change. The diff also carries #3443's commit, as the description says. AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Royalflamejlh commented 2026-12-01 21:37:10 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Royalflamejlh on 2026-12-01T21:37:10Z.


@Foniass thanks; fixed at 6ccc2f80c (one commit on #3443's 7afecf845).

  • copy_view_heap_slots (console_heap.rs) copies under HEAPS, as view_slot_words does; if the heap is gone, the range goes through read_committed_into. The bind test fails without the change, and view_heap_slots_are_copied_only_while_the_heap_is_registered checks the lock.
  • Whole lib suite under Wine 11.21 (CPU Vulkan, 4 threads): 2578 passed, 13 failed, 29 ignored. The parent 7afecf845 fails the same 13. main 6bfb5c51: 2735 passed, 11 failed; the 2 extra here are the indirect_guard tests, fixed on main after this branch's base. Skipped on all three trees: one test that crashes the binary under Wine, the D32S8 test that hangs under Wine, and the uma_driver tests (over 12 GB). The smoke pair is still owed.

AI use: Claude Code (Claude Opus 5.5) made the change, ran the tests and drafted this reply.

<!-- greyfinch-sync:ivall/xcl:comment:7326533696 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3592#issuecomment-7326533696). Originally posted by [Royalflamejlh](https://github.com/Royalflamejlh) on 2026-12-01T21:37:10Z. --- @Foniass thanks; fixed at `6ccc2f80c` (one commit on #3443's `7afecf845`). - `copy_view_heap_slots` (`console_heap.rs`) copies under `HEAPS`, as `view_slot_words` does; if the heap is gone, the range goes through `read_committed_into`. The bind test fails without the change, and `view_heap_slots_are_copied_only_while_the_heap_is_registered` checks the lock. - Whole lib suite under Wine 11.21 (CPU Vulkan, 4 threads): 2578 passed, 13 failed, 29 ignored. The parent `7afecf845` fails the same 13. `main` `6bfb5c51`: 2735 passed, 11 failed; the 2 extra here are the `indirect_guard` tests, fixed on `main` after this branch's base. Skipped on all three trees: one test that crashes the binary under Wine, the D32S8 test that hangs under Wine, and the `uma_driver` tests (over 12 GB). The smoke pair is still owed. AI use: Claude Code (Claude Opus 5.5) made the change, ran the tests and drafted this reply.
Foniass commented 2026-12-01 21:41:55 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T21:41:55Z.


Follow-up review (head 6ccc2f80, since 66220973, diff read, not a merge-check; draft, checks red):

  • The heap can be freed between the lookup and the in-place copy: fixed. The copy now goes through copy_view_heap_slots (console_heap.rs ~1097), which holds HEAPS while it checks that the range lies in one registered view heap and while it copies; every path that frees a heap (unregister, take, take_after_release, ~907-940) removes it from HEAPS before free, and a heap that went away falls back to read_committed_into (bind.rs ~1361). Both new tests cover the unregistered case.
  • Regression run and whole-suite counts missing: still open. Regression run has no xcl-smoke blocks (the reason PR description is red); the whole-suite counts are only in a reply comment, from a --lib run under Wine, and Tested still lists only bind::.

New: none.

Fine: the in-place copy now runs under HEAPS like view_slot_words, the lock is taken where view_heap_remaining_slots already took it so there is no new lock order, and ranges outside a registered heap still go through read_committed_into. Still carries #3443's commit, as the description says.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

<!-- greyfinch-sync:ivall/xcl:comment:7326651854 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3592#issuecomment-7326651854). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T21:41:55Z. --- Follow-up review (head 6ccc2f80, since 66220973, diff read, not a merge-check; draft, checks red): - **The heap can be freed between the lookup and the in-place copy:** fixed. The copy now goes through `copy_view_heap_slots` (`console_heap.rs` ~1097), which holds `HEAPS` while it checks that the range lies in one registered view heap and while it copies; every path that frees a heap (`unregister`, `take`, `take_after_release`, ~907-940) removes it from `HEAPS` before `free`, and a heap that went away falls back to `read_committed_into` (`bind.rs` ~1361). Both new tests cover the unregistered case. - **Regression run and whole-suite counts missing:** still open. **Regression run** has no `xcl-smoke` blocks (the reason `PR description` is red); the whole-suite counts are only in a reply comment, from a `--lib` run under Wine, and **Tested** still lists only `bind::`. New: none. Fine: the in-place copy now runs under `HEAPS` like `view_slot_words`, the lock is taken where `view_heap_remaining_slots` already took it so there is no new lock order, and ranges outside a registered heap still go through `read_committed_into`. Still carries #3443's commit, as the description says. AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Foniass commented 2026-12-02 02:44:58 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-02T02:44:58Z.


Follow-up review (head 00ce5644, since 6ccc2f80, diff read, not a merge-check; checks red):

  • Earlier points: unchanged. git range-diff shows both commits (#3443's, carried as c7eccd9d9, and this PR's 00ce5644) identical to 7afecf845 / 6ccc2f80; this push only rebases onto main 1c8b5514. The use-after-free fix still holds; the xcl-smoke blocks are still owed (the reason PR description is red), and the suite results are from 6ccc2f80 under Wine.

New: none.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

<!-- greyfinch-sync:ivall/xcl:comment:7333157623 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3592#issuecomment-7333157623). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-02T02:44:58Z. --- Follow-up review (head 00ce5644, since 6ccc2f80, diff read, not a merge-check; checks red): - **Earlier points:** unchanged. `git range-diff` shows both commits (#3443's, carried as `c7eccd9d9`, and this PR's `00ce5644`) identical to `7afecf845` / `6ccc2f80`; this push only rebases onto `main` `1c8b5514`. The use-after-free fix still holds; the `xcl-smoke` blocks are still owed (the reason `PR description` is red), and the suite results are from `6ccc2f80` under Wine. New: none. AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Decodere commented 2026-12-02 09:32:40 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Decodere on 2026-12-02T09:32:40Z.


CI triage (#3041, log read only — not a merge-check) for head 00ce5644ca0e:

The failing Build and test (Windows, no GPU) run (46797214033, merged onto main 1c8b5514) looks unrelated to this PR:

  • Failing tests (outside this diff): hle::d3d12x::pso_async::used_tests::retained_failed_demand_observes_another_binds_successful_retry panicked at crates/xcl/src/hle/d3d12x/pso_async.rs:6957:9 (successful retry was not reconciled), followed immediately by hle::d3d12x::pso_async::used_tests::retained_native_deferred_is_fail_closed_then_observes_exact_publication aborting in windows-core-0.62.2 (null reference produced, non-unwinding panic, exit 0xc0000409 STATUS_STACK_BUFFER_OVERRUN).
  • PR tests (all passed in the same run): This PR only touches bind.rs, console_heap.rs, and console_list.rs (no pso_async.rs overlap), and all three new tests passed before the abort: bind::tests::superseded_targets_are_found_in_every_declared_table_range, console_heap::tests::view_heap_slots_are_copied_only_while_the_heap_is_registered, and console_list::tests::descriptor_tables_keep_their_srv_and_uav_ranges.

Now that PR description is green, a maintainer CI rerun should clear this flake.

AI use: Antigravity (Gemini 4 Neon) read the CI log and PR diff and drafted this triage note (no local build or test run).

<!-- greyfinch-sync:ivall/xcl:comment:7341074471 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3592#issuecomment-7341074471). Originally posted by [Decodere](https://github.com/Decodere) on 2026-12-02T09:32:40Z. --- CI triage (#3041, log read only — not a merge-check) for head `00ce5644ca0e`: The failing `Build and test (Windows, no GPU)` run ([46797214033](https://github.com/xcl-dev/xcl/actions/runs/46797214033/job/143174183329), merged onto `main` `1c8b5514`) looks unrelated to this PR: - **Failing tests (outside this diff):** `hle::d3d12x::pso_async::used_tests::retained_failed_demand_observes_another_binds_successful_retry` panicked at `crates/xcl/src/hle/d3d12x/pso_async.rs:6957:9` (`successful retry was not reconciled`), followed immediately by `hle::d3d12x::pso_async::used_tests::retained_native_deferred_is_fail_closed_then_observes_exact_publication` aborting in `windows-core-0.62.2` (`null reference produced`, non-unwinding panic, exit `0xc0000409` `STATUS_STACK_BUFFER_OVERRUN`). - **PR tests (all passed in the same run):** This PR only touches `bind.rs`, `console_heap.rs`, and `console_list.rs` (no `pso_async.rs` overlap), and all three new tests passed before the abort: `bind::tests::superseded_targets_are_found_in_every_declared_table_range`, `console_heap::tests::view_heap_slots_are_copied_only_while_the_heap_is_registered`, and `console_list::tests::descriptor_tables_keep_their_srv_and_uav_ranges`. Now that `PR description` is green, a maintainer CI rerun should clear this flake. AI use: Antigravity (Gemini 4 Neon) read the CI log and PR diff and drafted this triage note (no local build or test run).
bmjask commented 2026-12-02 15:24:05 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:24:05Z.

Code review location: crates/xcl/src/hle/d3d12x/console_heap.rs; line 1102

@@ -1083,6 +1083,26 @@ fn finish_view_slot(h: &Heap, k: u32, page: Option<(u32, u32)>) -> Slot {
     Slot { generation: h.generation, at: h.shadow + (k * SLOT) as usize, index, pc_srv: h.pc_cpu + (2 * k * h.inc) as u64, pc_uav: h.pc_cpu + uav, pc_gpu_uav, sampler: false, shared_srv, k, remaining: h.n - k }
 }
 
+/// Remaining 32-byte view slots from `addr` to the end of the containing registered view heap,
+/// without allocating shared-heap pages. Returns `None` if `addr` is not inside a view heap.
+pub fn view_heap_remaining_slots(addr: usize) -> Option<usize> {
+    let heaps = HEAPS.lock().unwrap();
+    let h = heaps.iter().find(|h| !h.sampler && addr >= h.shadow && addr < h.shadow + (h.n as usize) * (SLOT as usize))?;
+    let k = (addr - h.shadow) / (SLOT as usize);
+    Some((h.n as usize).saturating_sub(k))
+}
+
+/// Copy view slots at `addr` into `out` under `HEAPS` (false: not all inside one registered view
+/// heap, and nothing copied).
+pub fn copy_view_heap_slots(addr: usize, out: &mut [u8]) -> bool {
+    let heaps = HEAPS.lock().unwrap();
+    if !heaps.iter().any(|h| !h.sampler && addr >= h.shadow && addr + out.len() <= h.shadow + (h.n as usize) * (SLOT as usize)) {
+        return false;
+    }
+    unsafe { std::ptr::copy_nonoverlapping(addr as *const u8, out.as_mut_ptr(), out.len()) };

High: Not certain about this one: copy_view_heap_slots reads a heap's memory directly on the assumption that it stays readable while registered, but for Memory::Title heaps the memory belongs to the title and was only checked as readable once, at register(). read_committed_into, which this path now bypasses, skipped no-access and guard pages. If window 0 is sparse (so in_committed is false), the heap is Memory::Title, and the title's allocator later changes protection on part of that pool (a guard page for tracking, a VirtualProtect to NOACCESS) or decommits it, the next bind while any target is superseded runs copy_nonoverlapping over those pages. That's an access violation in the title, or it consumes the title's guard page, where the old path zero-filled those regions. Keeping the direct copy for UMA/sparse heaps and using read_committed_into for Memory::Title would avoid it.

<!-- greyfinch-sync:ivall/xcl:review-comment:4771895181 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3592#discussion_r4771895181). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T15:24:05Z. **Code review location:** `crates/xcl/src/hle/d3d12x/console_heap.rs`; line 1102 ~~~~diff @@ -1083,6 +1083,26 @@ fn finish_view_slot(h: &Heap, k: u32, page: Option<(u32, u32)>) -> Slot { Slot { generation: h.generation, at: h.shadow + (k * SLOT) as usize, index, pc_srv: h.pc_cpu + (2 * k * h.inc) as u64, pc_uav: h.pc_cpu + uav, pc_gpu_uav, sampler: false, shared_srv, k, remaining: h.n - k } } +/// Remaining 32-byte view slots from `addr` to the end of the containing registered view heap, +/// without allocating shared-heap pages. Returns `None` if `addr` is not inside a view heap. +pub fn view_heap_remaining_slots(addr: usize) -> Option<usize> { + let heaps = HEAPS.lock().unwrap(); + let h = heaps.iter().find(|h| !h.sampler && addr >= h.shadow && addr < h.shadow + (h.n as usize) * (SLOT as usize))?; + let k = (addr - h.shadow) / (SLOT as usize); + Some((h.n as usize).saturating_sub(k)) +} + +/// Copy view slots at `addr` into `out` under `HEAPS` (false: not all inside one registered view +/// heap, and nothing copied). +pub fn copy_view_heap_slots(addr: usize, out: &mut [u8]) -> bool { + let heaps = HEAPS.lock().unwrap(); + if !heaps.iter().any(|h| !h.sampler && addr >= h.shadow && addr + out.len() <= h.shadow + (h.n as usize) * (SLOT as usize)) { + return false; + } + unsafe { std::ptr::copy_nonoverlapping(addr as *const u8, out.as_mut_ptr(), out.len()) }; ~~~~ --- **High**: Not certain about this one: `copy_view_heap_slots` reads a heap's memory directly on the assumption that it stays readable while registered, but for Memory::Title heaps the memory belongs to the title and was only checked as readable once, at `register()`. `read_committed_into`, which this path now bypasses, skipped no-access and guard pages. If window 0 is sparse (so `in_committed` is false), the heap is Memory::Title, and the title's allocator later changes protection on part of that pool (a guard page for tracking, a VirtualProtect to NOACCESS) or decommits it, the next bind while any target is superseded runs `copy_nonoverlapping` over those pages. That's an access violation in the title, or it consumes the title's guard page, where the old path zero-filled those regions. Keeping the direct copy for UMA/sparse heaps and using `read_committed_into` for Memory::Title would avoid it.
bmjask commented 2026-12-02 15:24:05 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:24:05Z.

Review: COMMENTED


Review of 00ce5644: 1 finding(s) (1 high), most severe first.

  1. High crates/xcl/src/hle/d3d12x/console_heap.rs:1102: Not certain about this one: copy_view_heap_slots reads a heap's memory directly on the assumption that it stays readable while registered, but for Memory::Title heaps the memory belongs to the title and was only checked as readable onc.... (inline)

These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.

<!-- greyfinch-sync:ivall/xcl:review:6153795756 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3592#pullrequestreview-6153795756). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T15:24:05Z. **Review:** COMMENTED --- Review of `00ce5644`: 1 finding(s) (1 high), most severe first. 1. **High** `crates/xcl/src/hle/d3d12x/console_heap.rs:1102`: Not certain about this one: `copy_view_heap_slots` reads a heap's memory directly on the assumption that it stays readable while registered, but for Memory::Title heaps the memory belongs to the title and was only checked as readable onc.... (inline) --- _These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it._
bmjask commented 2026-12-02 15:24:06 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T15:24:06Z.


@Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.

<!-- greyfinch-sync:ivall/xcl:comment:7350260467 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3592#issuecomment-7350260467). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T15:24:06Z. --- @Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.
Royalflamejlh commented 2026-12-02 20:32:16 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Royalflamejlh on 2026-12-02T20:32:16Z.

Code review location: crates/xcl/src/hle/d3d12x/console_heap.rs; line 1102

@@ -1083,6 +1083,26 @@ fn finish_view_slot(h: &Heap, k: u32, page: Option<(u32, u32)>) -> Slot {
     Slot { generation: h.generation, at: h.shadow + (k * SLOT) as usize, index, pc_srv: h.pc_cpu + (2 * k * h.inc) as u64, pc_uav: h.pc_cpu + uav, pc_gpu_uav, sampler: false, shared_srv, k, remaining: h.n - k }
 }
 
+/// Remaining 32-byte view slots from `addr` to the end of the containing registered view heap,
+/// without allocating shared-heap pages. Returns `None` if `addr` is not inside a view heap.
+pub fn view_heap_remaining_slots(addr: usize) -> Option<usize> {
+    let heaps = HEAPS.lock().unwrap();
+    let h = heaps.iter().find(|h| !h.sampler && addr >= h.shadow && addr < h.shadow + (h.n as usize) * (SLOT as usize))?;
+    let k = (addr - h.shadow) / (SLOT as usize);
+    Some((h.n as usize).saturating_sub(k))
+}
+
+/// Copy view slots at `addr` into `out` under `HEAPS` (false: not all inside one registered view
+/// heap, and nothing copied).
+pub fn copy_view_heap_slots(addr: usize, out: &mut [u8]) -> bool {
+    let heaps = HEAPS.lock().unwrap();
+    if !heaps.iter().any(|h| !h.sampler && addr >= h.shadow && addr + out.len() <= h.shadow + (h.n as usize) * (SLOT as usize)) {
+        return false;
+    }
+    unsafe { std::ptr::copy_nonoverlapping(addr as *const u8, out.as_mut_ptr(), out.len()) };

Not changed: the pages copied here are the heap's own, not memory the title uses for anything else. A Memory::Title heap's slots are what the title's allocation hook returned for this heap (runtime_alloc(bytes, D3D_DESCRIPTOR_HEAP), console_heap.rs:815), checked readable there (:816), and given back only by free (:962), after unregister (:907) has taken the heap out of HEAPS; the copy holds HEAPS (:1098). The title writes its descriptors into those pages, so it has no reason to guard or decommit them while the heap lives. main already reads the same pages in place under the same lock: view_slot_words (:1488) and exact_view_slot_words (:1501), called from vram.rs:524, :593 and :690. A page made unreadable there would fault in those first.

AI use: Claude Code (Claude Opus 5.5) checked the code and wrote this reply.

<!-- greyfinch-sync:ivall/xcl:review-comment:4775617890 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3592#discussion_r4775617890). Originally posted by [Royalflamejlh](https://github.com/Royalflamejlh) on 2026-12-02T20:32:16Z. **Code review location:** `crates/xcl/src/hle/d3d12x/console_heap.rs`; line 1102 ~~~~diff @@ -1083,6 +1083,26 @@ fn finish_view_slot(h: &Heap, k: u32, page: Option<(u32, u32)>) -> Slot { Slot { generation: h.generation, at: h.shadow + (k * SLOT) as usize, index, pc_srv: h.pc_cpu + (2 * k * h.inc) as u64, pc_uav: h.pc_cpu + uav, pc_gpu_uav, sampler: false, shared_srv, k, remaining: h.n - k } } +/// Remaining 32-byte view slots from `addr` to the end of the containing registered view heap, +/// without allocating shared-heap pages. Returns `None` if `addr` is not inside a view heap. +pub fn view_heap_remaining_slots(addr: usize) -> Option<usize> { + let heaps = HEAPS.lock().unwrap(); + let h = heaps.iter().find(|h| !h.sampler && addr >= h.shadow && addr < h.shadow + (h.n as usize) * (SLOT as usize))?; + let k = (addr - h.shadow) / (SLOT as usize); + Some((h.n as usize).saturating_sub(k)) +} + +/// Copy view slots at `addr` into `out` under `HEAPS` (false: not all inside one registered view +/// heap, and nothing copied). +pub fn copy_view_heap_slots(addr: usize, out: &mut [u8]) -> bool { + let heaps = HEAPS.lock().unwrap(); + if !heaps.iter().any(|h| !h.sampler && addr >= h.shadow && addr + out.len() <= h.shadow + (h.n as usize) * (SLOT as usize)) { + return false; + } + unsafe { std::ptr::copy_nonoverlapping(addr as *const u8, out.as_mut_ptr(), out.len()) }; ~~~~ --- Not changed: the pages copied here are the heap's own, not memory the title uses for anything else. A `Memory::Title` heap's slots are what the title's allocation hook returned for this heap (`runtime_alloc(bytes, D3D_DESCRIPTOR_HEAP)`, `console_heap.rs:815`), checked readable there (:816), and given back only by `free` (:962), after `unregister` (:907) has taken the heap out of `HEAPS`; the copy holds `HEAPS` (:1098). The title writes its descriptors into those pages, so it has no reason to guard or decommit them while the heap lives. `main` already reads the same pages in place under the same lock: `view_slot_words` (:1488) and `exact_view_slot_words` (:1501), called from `vram.rs:524`, :593 and :690. A page made unreadable there would fault in those first. AI use: Claude Code (Claude Opus 5.5) checked the code and wrote this reply.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3592:upstream-pr/3592'
git switch 'upstream-pr/3592'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3592'
git switch 'upstream-pr/3592'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3592'
git switch 'upstream-pr/3592'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3592'
git switch 'main'
git merge --squash 'upstream-pr/3592'
git switch 'main'
git merge --ff-only 'upstream-pr/3592'
git switch 'main'
git merge 'upstream-pr/3592'
git push origin 'main'
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
xcl-dev/xcl!3592
No description provided.