console_heap_gpu: fix erase_overlapping overflow and keep newer slot flags on rollback (CONSOLE-HEAP-GPU-OVERFLOW-AND-ROLLBACK) #2454

Open
github-bot wants to merge 2 commits from github-bot/gh-2454 into main AGit
Owner

@⁠Tyrannys on GitHub · 2026-11-27 10:56 UTC · original
Tyrannys:console-heap-gpu-overflow-and-rollback → main · +80 −5 · 2 files · 2 commits · open

Claimed in the coordination issue as: CONSOLE-HEAP-GPU-OVERFLOW-AND-ROLLBACK (#1608)

What and why

The fix is @⁠KushGene's (commit 694ba09ff, unchanged, with them as its author). @⁠enlight3d carried it after GitHub would not let KushGene's account open the pull request, and tested it on an RTX 3090. I'm opening it, with upstream/main merged in (no conflicts).

It fixes two bugs in runtime/src/console_heap_gpu.h, which runs only with LEONIDA_CONSOLE_HEAP_GPU=1:

  • ConsoleHeapKeys::erase_overlapping overflow. e.to + span wrapped past UINT64_MAX, so an entry at the top of the address space never counted as overlapping. The check is now e.to >= start || span > start - e.to, which cannot wrap.
  • ConsoleHeapGpu::commit(false, 0) rollback.
    • A rolled-back batch re-queued its slots even where a newer queue already held them, which overwrote the newer flags (kZeroFirst). It now re-queues only slots that are not already queued.
    • Rolled-back copies and keeps went to the back of the pending lists, behind newer ones. They now go to the front, and the copies are sorted with std::stable_sort, so the newer data is written last.

runtime/test/vk_console_heap_gpu_test.cpp covers both bugs.

Tested

enlight3d, on main 2859d0c9b (RTX 3090, NVIDIA 621.40, Windows 11, MSVC RelWithDebInfo): ctest -R "^(vk_console_heap_gpu|vk_command_replay|vk_predicated_copy|vk_predicate_capture|vk_queue)$" passed 5/5. The new test fails with main's header at line 63 and passes with the fix.

Here, on the merge with main 63185f4a3 (RTX 4070 SUPER, NVIDIA 621.01, Windows 11, MSVC RelWithDebInfo, Ninja):

  • The same ctest -R passes 5/5.
  • With main's console_heap_gpu.h, vk_console_heap_gpu fails at line 63: keys.erase_overlapping(UINT64_MAX - 8, UINT64_MAX) == 1.
  • With main's header plus only the erase_overlapping fix, it fails at line 560: !std::memcmp(out + 8 * kSlotKeep, keep_words.data(), 32). So the rollback checks catch the second bug too.
  • With the full fix it passes.

GPUs tested: NVIDIA GeForce RTX 3090, driver 621.40, Windows 11 (enlight3d); NVIDIA GeForce RTX 4070 SUPER 12 GB desktop, driver 621.01, Windows 11, profile M (ours)

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a)

🤖 Generated with Claude Code

**@⁠Tyrannys** on GitHub · 2026-11-27 10:56 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2454) `Tyrannys:console-heap-gpu-overflow-and-rollback` → `main` · +80 −5 · 2 files · 2 commits · open Claimed in the coordination issue as: CONSOLE-HEAP-GPU-OVERFLOW-AND-ROLLBACK (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) ### What and why The fix is @⁠KushGene's (commit 694ba09ff, unchanged, with them as its author). @⁠enlight3d carried it after GitHub would not let KushGene's account open the pull request, and tested it on an RTX 3090. I'm opening it, with `upstream/main` merged in (no conflicts). It fixes two bugs in `runtime/src/console_heap_gpu.h`, which runs only with `LEONIDA_CONSOLE_HEAP_GPU=1`: - **`ConsoleHeapKeys::erase_overlapping` overflow.** `e.to + span` wrapped past `UINT64_MAX`, so an entry at the top of the address space never counted as overlapping. The check is now `e.to >= start || span > start - e.to`, which cannot wrap. - **`ConsoleHeapGpu::commit(false, 0)` rollback.** - A rolled-back batch re-queued its slots even where a newer queue already held them, which overwrote the newer flags (`kZeroFirst`). It now re-queues only slots that are not already queued. - Rolled-back copies and keeps went to the back of the pending lists, behind newer ones. They now go to the front, and the copies are sorted with `std::stable_sort`, so the newer data is written last. `runtime/test/vk_console_heap_gpu_test.cpp` covers both bugs. ### Tested enlight3d, on `main` 2859d0c9b (RTX 3090, NVIDIA 621.40, Windows 11, MSVC RelWithDebInfo): `ctest -R "^(vk_console_heap_gpu|vk_command_replay|vk_predicated_copy|vk_predicate_capture|vk_queue)$"` passed 5/5. The new test fails with `main`'s header at line 63 and passes with the fix. Here, on the merge with `main` 63185f4a3 (RTX 4070 SUPER, NVIDIA 621.01, Windows 11, MSVC RelWithDebInfo, Ninja): - The same `ctest -R` passes 5/5. - With `main`'s `console_heap_gpu.h`, `vk_console_heap_gpu` fails at `line 63: keys.erase_overlapping(UINT64_MAX - 8, UINT64_MAX) == 1`. - With `main`'s header plus only the `erase_overlapping` fix, it fails at `line 560: !std::memcmp(out + 8 * kSlotKeep, keep_words.data(), 32)`. So the rollback checks catch the second bug too. - With the full fix it passes. GPUs tested: NVIDIA GeForce RTX 3090, driver 621.40, Windows 11 (enlight3d); NVIDIA GeForce RTX 4070 SUPER 12 GB desktop, driver 621.01, Windows 11, profile M (ours) AI-assisted: yes (Claude Code) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 10:58:32 +00:00
Author
Owner

@⁠timbot103 on GitHub · 2026-11-27 12:09 UTC · original

Independently checked head c741bd6da on Windows 11 Pro 26200, MSVC 2022 (19.44.35230.0, RelWithDebInfo, Ninja), Vulkan SDK 1.4.363.0, no game files.

Local build & dual-GPU verification:

  • Clean MSVC build of vk_console_heap_gpu_test (zero warnings in PR files).
  • vk_console_heap_gpu_test with LEONIDA_VK_VALIDATION=1:
    • NVIDIA GeForce RTX 5080 (616.64): PASS (8 passes, 54004 slots, 6000 updates, 95537 misses, 1 overflows, 2 failed, 0 validation errors).
    • AMD Radeon(TM) Graphics (25.10.30.02): PASS (8 passes, 54004 slots, 6000 updates, 95537 misses, 1 overflows, 2 failed, 0 validation errors).

Static code review notes on c741bd6da:

  • What checks out: the subtraction-based range check in erase_overlapping (runtime/src/console_heap_gpu.h:171-181) is sound for representable half-open queries (empty queries erase nothing, adjacent endpoints do not intersect; note callers constructing s + bytes at d3d12_vulkan_device.cpp:6212-6217 can still wrap before calling the helper). Slot flags are properly serialized under the shared mutex for a live heap, and the test exercises both directions of kZeroFirst.
  • Copy ordering gap on overlapping rolled-back writes (runtime/src/console_heap_gpu.h:559-581, 748-749; vk_console_heap_gpu_test.cpp:536-564):
    • While the test writes two disjoint 4 KiB halves (so reversing them yields identical bytes), rolling back two writes that overlap the same destination bytes can still fail to ensure the newer copy wins:
      • From the same source/destination buffer pair, both regions are emitted in a single vkCmdCopyBuffer call, which violates VUID-vkCmdCopyBuffer-pRegions-12482 (overlapping destination regions within one copy command).
      • From different source buffers, grouping sorts by buffer handle (which can reorder newer before older) and the only transfer barrier is emitted after all copies.
    • Although the grouping predates this PR, it breaks the PR's stated "newer copy wins" rollback guarantee for overlapping writes. Preserving enqueue order for overlapping destination ranges — either trimming superseded destination bytes or inserting a transfer dependency between overlapping copies — and testing partial overlap (both same-source and distinct-source) would close this.
  • Minor inherited note: heap removal vacates an integer heap index before waiting for uncommitted passes (console_heap_gpu.h:390-415, 722-747), so a recreate reusing that index between record and rollback could enqueue old flags onto a fresh slot.

AI-assisted.

**@⁠timbot103** on GitHub · 2026-11-27 12:09 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2454#issuecomment-7217908394) Independently checked head `c741bd6da` on Windows 11 Pro 26200, MSVC 2022 (`19.44.35230.0`, RelWithDebInfo, Ninja), Vulkan SDK `1.4.363.0`, no game files. **Local build & dual-GPU verification:** - Clean MSVC build of `vk_console_heap_gpu_test` (zero warnings in PR files). - `vk_console_heap_gpu_test` with `LEONIDA_VK_VALIDATION=1`: - **NVIDIA GeForce RTX 5080** (`616.64`): **PASS** (`8 passes, 54004 slots, 6000 updates, 95537 misses, 1 overflows, 2 failed`, 0 validation errors). - **AMD Radeon(TM) Graphics** (`25.10.30.02`): **PASS** (`8 passes, 54004 slots, 6000 updates, 95537 misses, 1 overflows, 2 failed`, 0 validation errors). **Static code review notes on `c741bd6da`:** - **What checks out:** the subtraction-based range check in `erase_overlapping` (`runtime/src/console_heap_gpu.h:171-181`) is sound for representable half-open queries (empty queries erase nothing, adjacent endpoints do not intersect; note callers constructing `s + bytes` at `d3d12_vulkan_device.cpp:6212-6217` can still wrap before calling the helper). Slot flags are properly serialized under the shared mutex for a live heap, and the test exercises both directions of `kZeroFirst`. - **Copy ordering gap on overlapping rolled-back writes (`runtime/src/console_heap_gpu.h:559-581, 748-749`; `vk_console_heap_gpu_test.cpp:536-564`):** - While the test writes two disjoint 4 KiB halves (so reversing them yields identical bytes), rolling back two writes that overlap the **same** destination bytes can still fail to ensure the newer copy wins: - From the same source/destination buffer pair, both regions are emitted in a single `vkCmdCopyBuffer` call, which violates `VUID-vkCmdCopyBuffer-pRegions-12482` (overlapping destination regions within one copy command). - From different source buffers, grouping sorts by buffer handle (which can reorder newer before older) and the only transfer barrier is emitted after all copies. - Although the grouping predates this PR, it breaks the PR's stated "newer copy wins" rollback guarantee for overlapping writes. Preserving enqueue order for overlapping destination ranges — either trimming superseded destination bytes or inserting a transfer dependency between overlapping copies — and testing partial overlap (both same-source and distinct-source) would close this. - **Minor inherited note:** heap removal vacates an integer heap index before waiting for uncommitted passes (`console_heap_gpu.h:390-415, 722-747`), so a recreate reusing that index between record and rollback could enqueue old flags onto a fresh slot. AI-assisted.
Author
Owner

@⁠lixonik on GitHub · 2026-11-27 12:13 UTC · original · reviewed

Tested c741bd6da locally merged onto main a3bafd05d02b on Windows 11 with MSVC 19.44 (Release), NVIDIA GeForce RTX 5070 (610.88; Vulkan validation unavailable locally), plus WSL Ubuntu GCC 11.4 with -fsanitize=undefined,address for the CPU ConsoleHeapKeys paths.

  • ConsoleHeapKeys::erase_overlapping overflow fix and commit(false, 0) slot-flag preservation are verified red/green: main failed at line 63 (erase_overlapping) and, with only that fix applied, at line 560 (kSlotKeep). All 5 CTests (vk_console_heap_gpu, vk_command_replay, vk_predicated_copy, vk_predicate_capture, vk_queue) and the GCC UBSan/ASan key checks passed on the PR.
  • A scratch GPU check of overlapping rolled-back copies found a remaining ordering bug when two copies target the same dst from different source buffers, and the new test does not currently exercise copy ordering. Details inline.

AI-assisted.

**@⁠lixonik** on GitHub · 2026-11-27 12:13 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2454#pullrequestreview-6087943989) · **reviewed** Tested `c741bd6da` locally merged onto `main` `a3bafd05d02b` on Windows 11 with MSVC 19.44 (Release), NVIDIA GeForce RTX 5070 (610.88; Vulkan validation unavailable locally), plus WSL Ubuntu GCC 11.4 with `-fsanitize=undefined,address` for the CPU `ConsoleHeapKeys` paths. - `ConsoleHeapKeys::erase_overlapping` overflow fix and `commit(false, 0)` slot-flag preservation are verified red/green: `main` failed at line 63 (`erase_overlapping`) and, with only that fix applied, at line 560 (`kSlotKeep`). All 5 CTests (`vk_console_heap_gpu`, `vk_command_replay`, `vk_predicated_copy`, `vk_predicate_capture`, `vk_queue`) and the GCC UBSan/ASan key checks passed on the PR. - A scratch GPU check of overlapping rolled-back copies found a remaining ordering bug when two copies target the same `dst` from different source buffers, and the new test does not currently exercise copy ordering. Details inline. AI-assisted.
@ -557,3 +558,3 @@
// The copies first (another kind of work in the same prelude), then what reads their destinations may run.
if (!copies_.empty()) {
std::sort(copies_.begin(), copies_.end(), [](const Copy& a, const Copy& b) {
std::stable_sort(copies_.begin(), copies_.end(), [](const Copy& a, const Copy& b) {
Author
Owner

@⁠lixonik on GitHub · 2026-11-27 12:13 UTC · original

[P2] std::stable_sort by (src, dst) still lets an older rolled-back copy overwrite a newer copy to the same destination.

  • Condition: Pass 1 queues queue_copy(older_src, 0, dst, 0, 4096, keep1) (filled with 0x11) and calls record(). Before commit(false, 0), a newer update queues queue_copy(newer_src, 0, dst, 0, 4096, keep2) (filled with 0x22) from a distinct VkBuffer whose handle compares smaller (newer_src < older_src, which can happen in DeviceState::gpu_copy_shadow when a remapped ShadowImport is recreated).
  • Actual result on RTX 5070: commit(false, 0) prepends older_src ahead of newer_src, then std::stable_sort sorts newer_src before older_src, so dst ends up with the stale 0x11 bytes instead of 0x22.
  • Same-(src, dst) overlap: even when older_src == newer_src with different srcOffset targeting the same dstOffset, the loop below batches both regions into a single vkCmdCopyBuffer, where overlapping destination regions violate VUID-vkCmdCopyBuffer-pRegions-00117 and have no guaranteed execution order; distinct vkCmdCopyBuffer calls targeting the same dst also have no TRANSFER_WRITE --> TRANSFER_WRITE barrier between them.
  • Expected / how to verify: preserve submission order across overlapping destination copies (e.g. batch only contiguous same-(src, dst) runs without sorting across different src, and insert a transfer barrier before any copy overlapping a prior destination range in the pass), and verify with a test covering both newer_src < older_src and same-src overlapping dst.
**@⁠lixonik** on GitHub · 2026-11-27 12:13 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2454#discussion_r4719224918) [P2] `std::stable_sort` by `(src, dst)` still lets an older rolled-back copy overwrite a newer copy to the same destination. - Condition: Pass 1 queues `queue_copy(older_src, 0, dst, 0, 4096, keep1)` (filled with `0x11`) and calls `record()`. Before `commit(false, 0)`, a newer update queues `queue_copy(newer_src, 0, dst, 0, 4096, keep2)` (filled with `0x22`) from a distinct `VkBuffer` whose handle compares smaller (`newer_src < older_src`, which can happen in `DeviceState::gpu_copy_shadow` when a remapped `ShadowImport` is recreated). - Actual result on RTX 5070: `commit(false, 0)` prepends `older_src` ahead of `newer_src`, then `std::stable_sort` sorts `newer_src` before `older_src`, so `dst` ends up with the stale `0x11` bytes instead of `0x22`. - Same-`(src, dst)` overlap: even when `older_src == newer_src` with different `srcOffset` targeting the same `dstOffset`, the loop below batches both regions into a single `vkCmdCopyBuffer`, where overlapping destination regions violate `VUID-vkCmdCopyBuffer-pRegions-00117` and have no guaranteed execution order; distinct `vkCmdCopyBuffer` calls targeting the same `dst` also have no `TRANSFER_WRITE --> TRANSFER_WRITE` barrier between them. - Expected / how to verify: preserve submission order across overlapping destination copies (e.g. batch only contiguous same-`(src, dst)` runs without sorting across different `src`, and insert a transfer barrier before any copy overlapping a prior destination range in the pass), and verify with a test covering both `newer_src < older_src` and same-`src` overlapping `dst`.
@ -497,0 +546,4 @@
// Newer updates arrive after record() and before Pass 1's rollback commit(false, 0).
gpu.queue(heap, kSlotKeep);
gpu.queue(heap, kSlotZero | leonida::ConsoleHeapGpu::kZeroFirst);
gpu.queue_copy(copy_src, 4096, copy_dst, 4096, 4096, keep2);
Author
Owner

@⁠lixonik on GitHub · 2026-11-27 12:13 UTC · original

[P3] The new rollback test uses disjoint destination offsets ([0, 4096) and [4096, 8192)), so it does not verify the copy-ordering part of the change.

With only the copies_ changes reverted (std::sort and copies_.insert(copies_.end(), ...)), vk_console_heap_gpu_test still passed on RTX 5070. Targeting overlapping dst bytes (including from two distinct source VkBuffers ordered newer_src < older_src) would make the test fail whenever rolled-back copies execute after newer ones.

**@⁠lixonik** on GitHub · 2026-11-27 12:13 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2454#discussion_r4719224936) [P3] The new rollback test uses disjoint destination offsets (`[0, 4096)` and `[4096, 8192)`), so it does not verify the copy-ordering part of the change. With only the `copies_` changes reverted (`std::sort` and `copies_.insert(copies_.end(), ...)`), `vk_console_heap_gpu_test` still passed on RTX 5070. Targeting overlapping `dst` bytes (including from two distinct source `VkBuffer`s ordered `newer_src < older_src`) would make the test fail whenever rolled-back copies execute after newer ones.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2454/head:github-bot/gh-2454'
git switch 'github-bot/gh-2454'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2454'
git switch 'github-bot/gh-2454'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2454'
git switch 'github-bot/gh-2454'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2454'
git switch 'main'
git merge --squash 'github-bot/gh-2454'
git switch 'main'
git merge --ff-only 'github-bot/gh-2454'
git switch 'main'
git merge 'github-bot/gh-2454'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.