runtime: keep a timed-out present probe's staging buffer until its copy is done (RT-PRESENT-PROBE-TIMEOUT-UAF) #2224

Open
github-bot wants to merge 1 commit from github-bot/gh-2224 into main AGit
Owner

@⁠Tyrannys on GitHub · 2026-11-26 07:27 UTC · original
Tyrannys:rt-present-probe-timeout → main · +322 −5 · 4 files · 1 commits · open

Claimed in the coordination issue as: RT-PRESENT-PROBE-TIMEOUT-UAF (ubiquitarianism/leonida#1608)

What and why

djrolfes found in #1608 (from reading the code) that the optional present-source readback probe (LEONIDA_CAPTURE_SOURCE=1, probe_read_source in runtime/src/vulkan_present.h) frees its staging resources after a 5-second fence timeout, even though the GPU copy may still be running. It is real, and a bit wider than the buffer:

  • After a successful vkQueueSubmit, a failed vkWaitForFences ran cleanup(). That freed the staging memory and destroyed the buffer the pending copy writes into.
  • The next probe then called vkResetCommandBuffer, vkBeginCommandBuffer and vkResetFences on the probe's command buffer and fence while they were still pending, and submitted them again.

Change (the probe only; nothing else in the presenter is affected):

  • On a failed wait after a successful submit, the buffer and its memory are parked instead of freed. The probe still gives up on that readback and returns, as before.
  • probe_reclaim() runs at the start of each probe. It checks the fence without waiting (timeout 0). Once the fence has signalled, it frees the parked buffer and memory. Until then, that probe is skipped and logged, so the busy command buffer and fence are never reset.
  • The destructor frees whatever is still parked after its vkQueueWaitIdle, so nothing is left at vkDestroyDevice.
  • If the submit itself fails, the buffer and memory are still freed at once, since the GPU never got the copy.

Tested

New: vk_presenter_probe_timeout runs vk_presenter_sync_test --probe-timeout. It needs no game files.

  • It turns the probe on with a fresh, owner-only capture folder.
  • The test's driver hooks make the probe's copy wait on a timeline semaphore that the test has not signalled yet, and make its fence wait return a real VK_TIMEOUT at once.
  • The frame submit of that present is made to fail, so nothing is presented behind the held copy.
  • It then checks that:
    • the buffer and memory of the held copy are not destroyed while it is pending;
    • a present made while the copy is still held does not resubmit the probe;
    • after the test signals, the next probe frees the parked buffer and reads back again;
    • the validation count, read after vkDestroyDevice, is 0.

Results:

  • With this change: passes plain and with LEONIDA_VK_VALIDATION=1 (0 errors).
  • With main's vulkan_present.h, the test fails: the buffer is freed while the copy is pending. Validation also reports VUID-vkResetCommandBuffer-commandBuffer-00045, VUID-vkBeginCommandBuffer-commandBuffer-00049, the in-use fence reset, and objects still alive at vkDestroyDevice.
  • ctest -R "vk_presenter|vk_present_dump" (7 tests): all pass, plain and with LEONIDA_VK_VALIDATION=1.
  • ctest -R "present|presenter|swapchain|capture" with validation: 14 of 15 pass. xbox_present fails at present_planes(&one, 1) (VkResult -11) on this machine. It fails the same way with main's vulkan_present.h, plain or under validation, so it is not caused by this change.
  • tests/test_ctest_registration.py passes. scripts/check_no_game_data.py on the changed files is clean. The game was not run.

Built with Ninja and MSVC on main a73a57e13.

GPUs tested: NVIDIA GeForce RTX 4070 SUPER, driver 621.01, Windows 11

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: no translator change)

🤖 Generated with Claude Code

**@⁠Tyrannys** on GitHub · 2026-11-26 07:27 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2224) `Tyrannys:rt-present-probe-timeout` → `main` · +322 −5 · 4 files · 1 commits · open Claimed in the coordination issue as: RT-PRESENT-PROBE-TIMEOUT-UAF (ubiquitarianism/leonida#1608) ### What and why djrolfes found in #1608 (from reading the code) that the optional present-source readback probe (`LEONIDA_CAPTURE_SOURCE=1`, `probe_read_source` in `runtime/src/vulkan_present.h`) frees its staging resources after a 5-second fence timeout, even though the GPU copy may still be running. It is real, and a bit wider than the buffer: - After a successful `vkQueueSubmit`, a failed `vkWaitForFences` ran `cleanup()`. That freed the staging memory and destroyed the buffer the pending copy writes into. - The next probe then called `vkResetCommandBuffer`, `vkBeginCommandBuffer` and `vkResetFences` on the probe's command buffer and fence while they were still pending, and submitted them again. Change (the probe only; nothing else in the presenter is affected): - On a failed wait after a successful submit, the buffer and its memory are parked instead of freed. The probe still gives up on that readback and returns, as before. - `probe_reclaim()` runs at the start of each probe. It checks the fence without waiting (timeout 0). Once the fence has signalled, it frees the parked buffer and memory. Until then, that probe is skipped and logged, so the busy command buffer and fence are never reset. - The destructor frees whatever is still parked after its `vkQueueWaitIdle`, so nothing is left at `vkDestroyDevice`. - If the submit itself fails, the buffer and memory are still freed at once, since the GPU never got the copy. ### Tested New: `vk_presenter_probe_timeout` runs `vk_presenter_sync_test --probe-timeout`. It needs no game files. - It turns the probe on with a fresh, owner-only capture folder. - The test's driver hooks make the probe's copy wait on a timeline semaphore that the test has not signalled yet, and make its fence wait return a real `VK_TIMEOUT` at once. - The frame submit of that present is made to fail, so nothing is presented behind the held copy. - It then checks that: - the buffer and memory of the held copy are not destroyed while it is pending; - a present made while the copy is still held does not resubmit the probe; - after the test signals, the next probe frees the parked buffer and reads back again; - the validation count, read after `vkDestroyDevice`, is 0. Results: - With this change: passes plain and with `LEONIDA_VK_VALIDATION=1` (0 errors). - With `main`'s `vulkan_present.h`, the test fails: the buffer is freed while the copy is pending. Validation also reports VUID-vkResetCommandBuffer-commandBuffer-00045, VUID-vkBeginCommandBuffer-commandBuffer-00049, the in-use fence reset, and objects still alive at `vkDestroyDevice`. - `ctest -R "vk_presenter|vk_present_dump"` (7 tests): all pass, plain and with `LEONIDA_VK_VALIDATION=1`. - `ctest -R "present|presenter|swapchain|capture"` with validation: 14 of 15 pass. `xbox_present` fails at `present_planes(&one, 1)` (VkResult -11) on this machine. It fails the same way with `main`'s `vulkan_present.h`, plain or under validation, so it is not caused by this change. - `tests/test_ctest_registration.py` passes. `scripts/check_no_game_data.py` on the changed files is clean. The game was not run. Built with Ninja and MSVC on `main` a73a57e13. GPUs tested: NVIDIA GeForce RTX 4070 SUPER, driver 621.01, Windows 11 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: no translator change) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The optional present-source readback probe (LEONIDA_CAPTURE_SOURCE) freed its
staging buffer and memory when its 5-second fence wait failed, although the
copy it had submitted could still be running, and the next probe reset and
resubmitted the same command buffer and fence while they were pending.

On a failed wait after a successful submit the buffer and memory are now
parked. A later probe frees them once the fence has signalled and skips its
readback until then; the destructor frees them after its queue wait. A failed
submit still frees them at once.

vk_presenter_sync_test --probe-timeout holds the probe's copy on a timeline
semaphore and makes its fence wait time out, then checks that nothing in use
is freed, the next probe does not resubmit, readbacks resume once the copy is
done, and validation reports nothing up to and including vkDestroyDevice.

Found by djrolfes in #1608.

AI-assisted (Claude Code).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:22:49 +00:00
Author
Owner

@⁠ammonium231 on GitHub · 2026-11-26 09:23 UTC · original

RTX 3090 / 621.40, validation on, current main a73a57e13: this PR's vk_presenter_probe_timeout plus main's vulkan_present.h hits VUID-vkQueueSubmit-pCommandBuffers-00071, a WRITE_AFTER_WRITE on the probe buffer, and 2 leaked objects (VkBuffer + VkDeviceMemory, VUID-vkDestroyDevice-device-05137) on 5/5 runs. With this PR the same test is clean. Present ctest is 13/14 on main vs 14/15 with the PR; the only failure on both is xbox_present (VkResult -11).

**@⁠ammonium231** on GitHub · 2026-11-26 09:23 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2224#issuecomment-7187895310) RTX 3090 / 621.40, validation on, current main a73a57e13: this PR's vk_presenter_probe_timeout plus main's vulkan_present.h hits VUID-vkQueueSubmit-pCommandBuffers-00071, a WRITE_AFTER_WRITE on the probe buffer, and 2 leaked objects (VkBuffer + VkDeviceMemory, VUID-vkDestroyDevice-device-05137) on 5/5 runs. With this PR the same test is clean. Present ctest is 13/14 on main vs 14/15 with the PR; the only failure on both is xbox_present (VkResult -11).
Author
Owner

@⁠qhukz13 on GitHub · 2026-11-26 11:52 UTC · original

I reviewed ea7b88bc by reading the code against main a73a57e13. My Windows PC is down today, so there is no build or run in this comment. ammonium231's RTX 3090 run above already covers the failing and fixed sides.

The fix reads right to me. These are the paths I checked:

  • The destructor always waits before it frees the parked buffer. ~VulkanPresenter does vkQueueWaitIdle(queue_) unconditionally under the submit turn, not only when there is a swapchain. Only after that does probe_reclaim(true) free the parked buffer and memory. The probe submits on that same queue_, so its copy is done by then.
  • One parked slot is enough. probe_reclaim(false) runs before any new record or submit, so a second timed-out readback cannot overwrite a buffer that is still parked. It also covers the second probe of the same present (the "target" call at line 811): it is skipped as well, instead of resetting the busy command buffer and fence.
  • Device loss. If the wait failed with VK_ERROR_DEVICE_LOST, the fence never signals. probe_reclaim(false) then returns false for good, and every later probe is skipped and logged. For a diagnostic behind LEONIDA_CAPTURE_SOURCE=1 that is the right outcome. The destructor still frees the parked objects after its idle wait, and freeing them is allowed after a loss.
  • A failed submit frees at once, as before. That is correct because the GPU never got the copy.

One question, probably outside this PR. After a timeout the held copy still reads the source image, not only the staging buffer. resource_lock is released before the submit, and vulkan_backend.h:1738 says present and probe submissions have their own fences, outside the queue retirement timelines. So if the title destroys that resource while the copy is held, is its destruction ordered after the probe's fence? I think it is ordered when queue_ is also one of the context's replay queues: a later retire-ticket signal on the same queue covers the earlier probe submission. I don't think it is ordered when the presenter has its own queue. After a 5 s timeout the device is most likely gone anyway, so I would not hold the PR for this. A sentence in the description saying the fix covers the staging buffer and the probe's own command buffer and fence would make the scope clear.

AI-assisted (Claude Code).

**@⁠qhukz13** on GitHub · 2026-11-26 11:52 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2224#issuecomment-7191274452) I reviewed `ea7b88bc` by reading the code against main `a73a57e13`. My Windows PC is down today, so there is no build or run in this comment. ammonium231's RTX 3090 run above already covers the failing and fixed sides. The fix reads right to me. These are the paths I checked: - **The destructor always waits before it frees the parked buffer.** `~VulkanPresenter` does `vkQueueWaitIdle(queue_)` unconditionally under the submit turn, not only when there is a swapchain. Only after that does `probe_reclaim(true)` free the parked buffer and memory. The probe submits on that same `queue_`, so its copy is done by then. - **One parked slot is enough.** `probe_reclaim(false)` runs before any new record or submit, so a second timed-out readback cannot overwrite a buffer that is still parked. It also covers the second probe of the same present (the `"target"` call at line 811): it is skipped as well, instead of resetting the busy command buffer and fence. - **Device loss.** If the wait failed with `VK_ERROR_DEVICE_LOST`, the fence never signals. `probe_reclaim(false)` then returns false for good, and every later probe is skipped and logged. For a diagnostic behind `LEONIDA_CAPTURE_SOURCE=1` that is the right outcome. The destructor still frees the parked objects after its idle wait, and freeing them is allowed after a loss. - **A failed submit frees at once**, as before. That is correct because the GPU never got the copy. One question, probably outside this PR. After a timeout the held copy still reads the **source image**, not only the staging buffer. `resource_lock` is released before the submit, and `vulkan_backend.h:1738` says present and probe submissions have their own fences, outside the queue retirement timelines. So if the title destroys that resource while the copy is held, is its destruction ordered after the probe's fence? I think it is ordered when `queue_` is also one of the context's replay queues: a later retire-ticket signal on the same queue covers the earlier probe submission. I don't think it is ordered when the presenter has its own queue. After a 5 s timeout the device is most likely gone anyway, so I would not hold the PR for this. A sentence in the description saying the fix covers the staging buffer and the probe's own command buffer and fence would make the scope clear. AI-assisted (Claude Code).
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/2224/head:github-bot/gh-2224'
git switch 'github-bot/gh-2224'

Merge

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