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

Open
opened 2026-11-26 21:20:29 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2224
Author: @Tyrannys
Created: 11/26/2026
Status: 🔄 Open

Base: main ← Head: rt-present-probe-timeout


📝 Commits (1)

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

📊 Changes

4 files changed (+322 additions, -5 deletions)

View changed files

📝 runtime/CMakeLists.txt (+2 -1)
📝 runtime/src/vulkan_present.h (+38 -4)
➕ runtime/test/vk_presenter_probe_timeout.h (+190 -0)
📝 runtime/test/vk_presenter_sync_test.cpp (+92 -0)

📄 Description

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2224 **Author:** [@Tyrannys](https://github.com/Tyrannys) **Created:** 11/26/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `rt-present-probe-timeout` --- ### 📝 Commits (1) - [`ea7b88b`](https://github.com/ubiquitarianism/leonida/commit/ea7b88bc48293a5bd1766fb810ba4816a2a28eed) runtime: keep a timed-out present probe's staging buffer until its copy is done (RT-PRESENT-PROBE-TIMEOUT-UAF) ### 📊 Changes **4 files changed** (+322 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+2 -1) 📝 `runtime/src/vulkan_present.h` (+38 -4) ➕ `runtime/test/vk_presenter_probe_timeout.h` (+190 -0) 📝 `runtime/test/vk_presenter_sync_test.cpp` (+92 -0) </details> ### 📄 Description 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#2228
No description provided.