Skip to content

fix(runtime): harden PresentComposer Vulkan checks, partial-init cleanup, and target guards - #4822

Open
Persudes wants to merge 1 commit into
ItsNotRexed:mainfrom
Persudes:runtime/present-compose-hardening
Open

Persudes wants to merge 1 commit into
ItsNotRexed:mainfrom
Persudes:runtime/present-compose-hardening

Conversation

@Persudes

Copy link
Copy Markdown
Contributor

Claim: #4683 (comment)

Summary

Hardens PresentComposer in runtime/src/vulkan_present_compose.h and adds deterministic MockVk regression coverage in runtime/test/vk_presenter_sync_test.cpp (Section 4):

  • Device & Vulkan pointer completeness + safe teardown: ready() now checks device != VK_NULL_HANDLE and all 11 previously unchecked device function pointers (vkDestroyShaderModule, vkDestroyDescriptorSetLayout, vkDestroyDescriptorPool, vkDestroyPipelineLayout, vkDestroyPipeline, vkGetImageMemoryRequirements, vkBindImageMemory, vkDestroyImageView, vkDestroyImage, vkFreeMemory, vkCmdPipelineBarrier); ~PresentComposer() and release_target() guard destroy calls on non-null function pointers.
  • Local partial-init cleanup in ready(): Pipeline/descriptor objects are created into locals and destroyed immediately if any step fails, requiring vkCreateComputePipelines(...) == VK_SUCCESS && pipeline != VK_NULL_HANDLE and never passing failed/garbage output handles to vkDestroyPipeline.
  • ensure_target() validation & clean failure recovery: Rejects non-ready composers and zero extents (width == 0 || height == 0), clamps memory_.memoryTypeCount to VK_MAX_MEMORY_TYPES, creates image/memory/view into locals, and destroys only created objects on failure without invoking cleanup on garbage output handles.
  • record() guards & overflow-safe dispatch: Guards non-ready state, null command buffer, or missing target/zero extent, and dispatches ((width - 1u) / 8u + 1u, (height - 1u) / 8u + 1u, 1).

Red Control (pre-fix on d0835dd9)

Running Section 4 against unmodified runtime/src/vulkan_present_compose.h failed immediately as expected:

  • vk_presenter_sync_test.exe -> exit 1 at line 605: !c.ready() (missing vkGetImageMemoryRequirements allowed ready() to report success).

Verification (post-fix at 9e58c0f9 on Windows 11 x64, AMD Radeon RX 6900 XT, Vulkan SDK 1.4.363.1)

  • Build (cmd /c "call vcvars64.bat >nul && cmake --build runtime/build --config RelWithDebInfo --target vk_presenter_sync_test d3d12_present_test xbox_present_test gta6rt"): [22/22] exit 0, zero warnings in touched files.
  • runtime\build\vk_presenter_sync_test.exe (standard): exit 0 (vk_presenter_sync_test: ok, including presenter sync: PresentComposer hardening ok).
  • LEONIDA_VK_VALIDATION=1 runtime\build\vk_presenter_sync_test.exe: exit 0 (device: AMD Radeon RX 6900 XT (validation on), 0 validation errors).
  • ctest --test-dir runtime/build --output-on-failure -R "^(vk_presenter_sync|d3d12_present|xbox_present)$":
    • Standard: 3/3 passed (3.06 sec).
    • LEONIDA_VK_VALIDATION=1: 3/3 passed (3.08 sec).
  • Hygiene: git merge-tree --write-tree upstream/main HEAD exit 0; python -I tests/test_ctest_registration.py (4 tests OK); python -I scripts/check_no_game_data.py exit 0; git diff --check exit 0; CRLF=0 on both touched files.

AI-assisted (Gemini, Antigravity); no copyrighted game files or dumps used.

@shdann

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 9e58c0f9 on Windows 11 with the NVIDIA GeForce RTX 4090 (32.0.16.2101) and AMD Radeon(TM) Graphics (32.0.21043.5001): pass.

I built and ran vk_presenter_sync_test with MSVC on each adapter. Both runs passed the submit rollback, capture readback, concurrent presentation, and composer checks. Both capture tests reported four barriers for four writes and RGB (51, 153, 229).

I also repeated a cleanup case 128 times with the PR's Vulkan mock. Each cycle creates a 64×64 target and forces view creation to fail during a resize to 32×32. After each failure, the image, memory allocation, and view creation counts match their corresponding release counts. The next cycle can create a target again.

The new test built against merge base d0835dd9 exits 1 on both adapters at the first !c.ready() check. The fault injection uses mock calls. The presentation checks use real Vulkan devices. This run covers the focused test.

AI-assisted.

@Atomic258

Copy link
Copy Markdown
Contributor

NVIDIA datapoint for 9e58c0f9f2f749df51feefc426efbadbf8e7eb28, in addition to the author's AMD RX 6900 XT coverage. The PR had no prior NVIDIA results when I checked.

Windows 11 x64, GeForce RTX 5090 (driver 617.42), Vulkan SDK 1.4.363.0, MSVC 14.44.35207, Ninja / RelWithDebInfo.

  • cmd /c C:\Users\Nick\build-4822.bat ran cmake --build runtime\build --target vk_presenter_sync_test: passed.
  • runtime/build/vk_presenter_sync_test.exe: exit 0; selected the RTX 5090 and printed presenter sync: PresentComposer hardening ok and vk_presenter_sync_test: ok.
  • LEONIDA_VK_VALIDATION=1 runtime/build/vk_presenter_sync_test.exe: exit 0; confirmed device: NVIDIA GeForce RTX 5090 (validation on) and no validation errors printed.
  • Red control: git checkout origin/main -- runtime/src/vulkan_present_compose.h (local origin/main at 98fa503f8), keeping the PR test intact. Rebuild succeeded; the test exited 1 at line 605: !c.ready(). Restored the header from HEAD in both index and worktree, rebuilt, and reran: exit 0. git status --porcelain was empty.

AI-assisted (Devin); no game files.

@memorypressure

Copy link
Copy Markdown

Reviewed at merge with current main 3faac55: the auto-merge is clean textually, but note main gained PR #3551 (3ff1566, verified on main) inside runtime/test/vk_presenter_sync_test.cpp after your base d0835dd — so the merged test file is new in combination and has never been compiled anywhere, and this branch reports no CI checks at all. Please run the windows job on the merge commit before landing. The header changes themselves review well: handles validated after every vkCreate, partial-init cleanup lambdas, a state guard before target creation, memoryTypeCount clamped to VK_MAX_MEMORY_TYPES. No game data; nothing else on main touched these files. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only review).

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 9e58c0f on NVIDIA GeForce RTX 4080 (driver 617.42), Windows 11, clang-cl cross-build: no regression, but the present test cannot run here.

  • vk_presenter_sync_test: runs and passes, each section ends "ok".
  • d3d12_present_test exits 77 with "SKIP: the process has no interactive window station", so it gives no evidence for the present path.

xbox_present_test and the PR's red control were not run, and the validation layer was not enabled, so the PR's validation claims are not reproduced here.

AI-assisted.

@Kam471 Kam471 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.

Reviewed head 9e58c0f9f2f749df51feefc426efbadbf8e7eb28. Windows 11 26200, MSVC 19.44.35207, Ninja, RelWithDebInfo, Vulkan 1.4.363, RTX 5070 Ti.

vk_presenter_sync 1/1: PresentComposer hardening ok; the held presents (229,26,51), capture WAW 4/4, concurrent present/execute, and device-idle teardown checks still pass. Red: reverting runtime/src/vulkan_present_compose.h only exits 1 at line 778: !c.ready() — a MockVk with missing function pointers was accepted as ready.

Verdict: ready to merge. Partial initialisation unwinds, a 0x0 extent is refused instead of creating a 0x0 swapchain/blit, and a composer that is not ready returns VK_ERROR_FORMAT_NOT_SUPPORTED from vulkan_present.h instead of dispatching null handles.

402 of the 491 lines are the new MockVk test; the header change is contained. Related: #4852 (presenter sync setup errors) and merged #5928 overlap in coverage, not code. First-frame: a failed first present now degrades to "compose unsupported" rather than a null-handle dispatch.

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