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

Merged
leonida/mandy
Tyrannys merged 2 commits from console-heap-gpu-overflow-and-rollback into main 2026-11-29 01:05:10 +00:00

Pull request successfully merged and closed

Tyrannys commented 2026-11-27 10:56:26 +00:00

Claimed in the coordination issue as: CONSOLE-HEAP-GPU-OVERFLOW-AND-ROLLBACK (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7216316918)

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

Claimed in the coordination issue as: CONSOLE-HEAP-GPU-OVERFLOW-AND-ROLLBACK (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7216316918) ### 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)
timbot103 commented 2026-11-27 12:09:34 +00:00

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.

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.
lixonik reviewed 2026-11-27 12:13:11 +00:00
lixonik left a comment
No description provided.
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.
zmasi commented 2026-11-28 23:23:25 +00:00

#2454 | head c741bd6dab24d54e8c462554fce493a328dfce09 (merged cleanly with main adfb3821a23612263c1c5b195453ed01c4c114db — no conflicts on current main) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(vk_console_heap_gpu|vk_command_replay|vk_predicated_copy|vk_predicate_capture|vk_queue)$" --output-on-failure (LEONIDA_VK_VALIDATION=1) | red on main: building vk_console_heap_gpu_test against main's runtime/src/console_heap_gpu.h fails vk_console_heap_gpu at line 63: keys.erase_overlapping(UINT64_MAX - 8, UINT64_MAX) == 1; applying only the erase_overlapping overflow fix on main then fails at line 560: !std::memcmp(out + 8 * kSlotKeep, keep_words.data(), 32) when rollback clobbers newer flags and queued copies | green on PR+main: 5/5 CTests (vk_console_heap_gpu — 8 passes, 54004 slots, 6000 updates, 95537 misses, 1 overflows, 2 failed, vk_command_replay, vk_predicated_copy, vk_predicate_capture, vk_queue) pass in 3.42 s on RTX 3090 with 0 Khronos validation errors; check_no_game_data.py 0 violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes

#2454 | head c741bd6dab24d54e8c462554fce493a328dfce09 (merged cleanly with main adfb3821a23612263c1c5b195453ed01c4c114db — no conflicts on current `main`) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^(vk_console_heap_gpu|vk_command_replay|vk_predicated_copy|vk_predicate_capture|vk_queue)$" --output-on-failure` (`LEONIDA_VK_VALIDATION=1`) | red on main: building `vk_console_heap_gpu_test` against `main`'s `runtime/src/console_heap_gpu.h` fails `vk_console_heap_gpu` at `line 63: keys.erase_overlapping(UINT64_MAX - 8, UINT64_MAX) == 1`; applying only the `erase_overlapping` overflow fix on `main` then fails at `line 560: !std::memcmp(out + 8 * kSlotKeep, keep_words.data(), 32)` when rollback clobbers newer flags and queued copies | green on PR+main: `5/5` CTests (`vk_console_heap_gpu` — `8 passes, 54004 slots, 6000 updates, 95537 misses, 1 overflows, 2 failed`, `vk_command_replay`, `vk_predicated_copy`, `vk_predicate_capture`, `vk_queue`) pass in `3.42 s` on RTX 3090 with `0` Khronos validation errors; `check_no_game_data.py` `0` violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes
enlight3d commented 2026-11-29 00:45:57 +00:00

Run of #2454 (ctest, red/green), by enlight3d. Nothing pushed.

Heads: PR c741bd6da (merge of main 63185f4a3 into the branch), main 1bc055e6c. git merge-tree --write-tree main <PR head> is clean (no conflicts).
The PR changes runtime/src/console_heap_gpu.h and adds checks to runtime/test/vk_console_heap_gpu_test.cpp. The header on main is the same as the PR's base, so the test is new and the code is changed.

Machine: NVIDIA GeForce RTX 3090 24 GB, driver 621.40, Windows 11 Pro (build 10.0.26300), MSVC 19.44 (VS 2022 Build Tools), CMake + Ninja, RelWithDebInfo, -DLEONIDA_REQUIRE_VULKAN=ON.

Commands (in the vcvars64 environment, only this target built):

cmake -S runtime -B bld -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLEONIDA_REQUIRE_VULKAN=ON
cmake --build bld --target vk_console_heap_gpu_test -j 8
ctest --test-dir bld -R "^vk_console_heap_gpu$" --output-on-failure
  • GREEN (PR head): vk_console_heap_gpu Passed (1/1, 0.30 s).
  • RED 1 (PR head, with console_heap_gpu.h taken from main, test kept): Failed at line 63: keys.erase_overlapping(UINT64_MAX - 8, UINT64_MAX) == 1. This is a behavioural red: the test builds against main's header, it fails because of the overflow.
  • RED 2 (main's header plus only the erase_overlapping fix): Failed at line 560: !std::memcmp(out + 8 * kSlotKeep, keep_words.data(), 32), so the rollback checks catch the second bug too.

I did not run the other four tests the PR description lists (vk_command_replay, vk_predicated_copy, vk_predicate_capture, vk_queue).
Link check: merged into main 1bc055e6c, gta6rt.exe builds and links (RelWithDebInfo, MSVC 19.44); no new imports vs main.

Run of #2454 (ctest, red/green), by enlight3d. Nothing pushed. Heads: PR `c741bd6da` (merge of main 63185f4a3 into the branch), main `1bc055e6c`. `git merge-tree --write-tree main <PR head>` is clean (no conflicts). The PR changes `runtime/src/console_heap_gpu.h` and adds checks to `runtime/test/vk_console_heap_gpu_test.cpp`. The header on main is the same as the PR's base, so the test is new and the code is changed. Machine: NVIDIA GeForce RTX 3090 24 GB, driver 621.40, Windows 11 Pro (build 10.0.26300), MSVC 19.44 (VS 2022 Build Tools), CMake + Ninja, RelWithDebInfo, `-DLEONIDA_REQUIRE_VULKAN=ON`. Commands (in the vcvars64 environment, only this target built): ``` cmake -S runtime -B bld -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLEONIDA_REQUIRE_VULKAN=ON cmake --build bld --target vk_console_heap_gpu_test -j 8 ctest --test-dir bld -R "^vk_console_heap_gpu$" --output-on-failure ``` - GREEN (PR head): `vk_console_heap_gpu` Passed (1/1, 0.30 s). - RED 1 (PR head, with `console_heap_gpu.h` taken from main, test kept): Failed at `line 63: keys.erase_overlapping(UINT64_MAX - 8, UINT64_MAX) == 1`. This is a behavioural red: the test builds against main's header, it fails because of the overflow. - RED 2 (main's header plus only the `erase_overlapping` fix): Failed at `line 560: !std::memcmp(out + 8 * kSlotKeep, keep_words.data(), 32)`, so the rollback checks catch the second bug too. I did not run the other four tests the PR description lists (vk_command_replay, vk_predicated_copy, vk_predicate_capture, vk_queue). Link check: merged into main 1bc055e6c, gta6rt.exe builds and links (RelWithDebInfo, MSVC 19.44); no new imports vs main.
MoonEyes commented 2026-11-29 01:05:12 +00:00

Merged after enlight3d (Thib)'s red/green run (RUN, RTX 3090, driver 621.40, Windows 11: green with this PR, red without it, and gta6rt.exe still links) and a re-check here: no conflict with main 21f5baff1, same head as run, no-game-data green, and the only ctest failures in the Windows/macOS CI jobs are tests this diff does not touch (known on main or Vulkan: vk_xdxr_postbuild_readback vk_bindless_dimensions d3d12_compile_pool xbox_present ).

AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes.

Merged after **enlight3d** (Thib)'s red/green run ([RUN](https://github.com/ProjectLeonida/mandy/pull/2454#issuecomment-7248335563), RTX 3090, driver 621.40, Windows 11: green with this PR, red without it, and `gta6rt.exe` still links) and a re-check here: no conflict with `main` 21f5baff1, same head as run, `no-game-data` green, and the only ctest failures in the Windows/macOS CI jobs are tests this diff does not touch (known on `main` or Vulkan: vk_xdxr_postbuild_readback vk_bindless_dimensions d3d12_compile_pool xbox_present ). _AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes._
Sign in to join this conversation.
No description provided.