Skip to content

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

Open
Tyrannys wants to merge 1 commit into
ProjectLeonida:mainfrom
Tyrannys:rt-present-probe-timeout
Open

Tyrannys wants to merge 1 commit into
ProjectLeonida:mainfrom
Tyrannys:rt-present-probe-timeout

Conversation

@Tyrannys

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-PRESENT-PROBE-TIMEOUT-UAF (#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 a73a57e.

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

…py is done (RT-PRESENT-PROBE-TIMEOUT-UAF)

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 ProjectLeonida#1608.

AI-assisted (Claude Code).
@ammonium231

Copy link
Copy Markdown
Contributor

RTX 3090 / 621.40, validation on, current main a73a57e: 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).

@qhukz13

qhukz13 commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

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).

@zmasi

zmasi commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

#2224 | head ea7b88b (merged cleanly onto origin/main 48bae6c) | Windows 11 (10.0.26200.9974), MSVC 19.44.35228.0, NVIDIA GeForce RTX 3090, Python 3.12.10 | AI-assisted: yes

Independent Verification (Windows 11 + MSVC + RTX 3090)

  • Diff & safety audit: Inspected 100% of the diff across runtime/CMakeLists.txt, runtime/src/vulkan_present.h, runtime/test/vk_presenter_probe_timeout.h, and runtime/test/vk_presenter_sync_test.cpp. python -I scripts/check_no_game_data.py passed (rc=0), and python -I tests/test_ctest_registration.py passed (7/7 OK).
  • Red -> Green on Windows 11 (MSVC 19.44.35228.0, CMake + Ninja RelWithDebInfo, NVIDIA GeForce RTX 3090):
    • Red (origin/main 48bae6cade vulkan_present.h + #2224 --probe-timeout harness): Built vk_presenter_sync_test.exe against unpatched runtime/src/vulkan_present.h and ran vk_presenter_sync_test.exe --probe-timeout on RTX 3090 -> exited 1: on the timed-out fence wait ([rt] present-probe: readback submit/wait failed (0)), cleanup() freed the staging buffer/memory while the copy was still held on the timeline semaphore (freed_in_flight = true) and the next present immediately reset/resubmitted the in-flight probe_commands_ and probe_fence_.
    • Green (#2224 + origin/main 48bae6cade): Rebuilt vk_presenter_sync_test.exe with patched runtime/src/vulkan_present.h (probe_reclaim + probe_parked_buffer_ / probe_parked_memory_) and ran all three CTest targets on RTX 3090:
      • vk_presenter_sync_test.exe -> vk_presenter_sync_test: ok (rc=0)
      • vk_presenter_sync_test.exe --sparse-capture -> sparse capture: frames 1/4, cap, last-fence drain, explicit readback, disk and splash-only handoffs verified (rc=0)
      • vk_presenter_sync_test.exe --probe-timeout -> vk_presenter_sync_test --probe-timeout: ok (rc=0; logged readback wait failed (2); its buffer is freed once the copy is done, skipped the busy probe on the in-flight frame, reclaimed and read back source frame 3 once signalled, and returned expected present 0..3 = {0, -1, 0, 0}).
      • Also verified LEONIDA_VK_VALIDATION=1 without VK_LAYER_KHRONOS_validation installed cleanly exits 77 (SKIP_RETURN_CODE 77).

@timbot103 timbot103 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed head ea7b88bc4 against its declared base and the supplied main snapshot. This was a static audit only. I ran no builds, tests, or GPU workloads.

The fence poll in runtime/src/vulkan_present.h:818 prevents command-buffer and fence reuse while the copy is pending and bounds deferred staging to one allocation per presenter. The timeout path and teardown still have two lifetime gaps.

  • Major — runtime/src/vulkan_present.h:231. Teardown forces reclamation even if vkQueueWaitIdle returns an error such as VK_ERROR_OUT_OF_HOST_MEMORY. A pending copy can lose its staging, fence, and command pool. Require completion or confirmed device loss before destroying those objects, and cover the failed idle wait in the regression test.
  • Major — runtime/src/vulkan_present.h:974. The source image can still retire while a timed-out probe reads it. runtime/src/vulkan_backend.h:1738 describes the probe's fence as outside the retirement timelines, and the image can be destroyed after the next retirement ticket completes on another queue. Register a lifetime dependency for the source and its backing through probe completion.
  • Minor — runtime/test/vk_presenter_probe_timeout.h:152. A scheduling pause can let the fixed delay expire before the third present checks the pending probe. That makes correct code fail the submission-count check. Signal at the queue-idle step that follows the probe attempt.
  • Minor — runtime/test/vk_presenter_probe_timeout.h:166. With validation disabled, the test can pass when the probe resumes and both parked objects leak. Assert that the hooks observed both the buffer destroy and the memory free.

AI-assisted.

@OreoFR

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

TAKEN by OreoFR (Windows, RTX 4070 8 GB)

@OreoFR

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

Verified by OreoFR — READY

  • Base: main c22a0a7 + this PR (head ea7b88b) merged with --no-ff
  • Red (main + only this PR's test and its CMake registration): the new ctest vk_presenter_probe_timeout fails on main at line 181 (kept): after the forced fence timeout, main frees the staging buffer while the held copy is still pending. vk_presenter_sync and vk_presenter_sparse_capture pass on main and on the merge.
  • Green (merge): vk_presenter_probe_timeout passes; ctest (Ninja RelWithDebInfo, all targets, gta6rt launchers excluded) in package p3 = main + d3d12: rewrite a missed journal file from an empty cache when the seeded one kept its data (RT-JOURNAL-SEEDED-REWRITE) #2179 + runtime: keep a timed-out present probe's staging buffer until its copy is done (RT-PRESENT-PROBE-TIMEOUT-UAF) #2224 (no shared files): 506 tests, new failures vs main: none. xmem_no_write_watch, d3d12_mesh and d3d12_mesh_heavy_cache failed in the parallel run and pass when re-run alone (main alone: 505 tests, 6 failures: big_compile_gate and api_ref_state and d3d12_queue_wait_timeout timeouts, xs_proxy_mock, vk_compile_gate_calibration, xbox_present; xbox_present fails in every package as on main)
  • gta6rt.exe links without the XMA decoder (LEONIDA_CARGO=OFF, LEONIDA_XMA2_DECODER_LIB empty) in package p3; imports unchanged vs main (25 DLLs, 755 functions, no WS2_32)
  • Diff read: no issue. A timed-out readback parks its buffer and memory; the next probe frees them only once probe_fence_ has signalled (it skips its own readback until then), and the destructor frees them after its queue wait, before the fence is destroyed.
  • Other runs: Consistent with ammonium231's and zmasi's runs and qhukz13's and timbot103's reviews.
  • Machine: Windows 11, Intel Core Ultra 7 155H, RTX 4070 Laptop 8 GB, NVIDIA 621.40, Python 3.12.10, MSVC 19.44

Ready to merge per the maintainers' plan.

AI-assisted (Claude Code, Claude Opus 5.5), run for OreoFR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants