vulkan_device_markers: clamp memoryTypeCount, guard null proc/target and unmap #2308

Closed
narekshaghoyandev wants to merge 1 commit from upstream-pr/2308 into main
narekshaghoyandev commented 2026-11-26 13:39:09 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by narekshaghoyandev on 2026-11-26T13:39:09Z.


Fixes bounds, null-pointer, and teardown bugs in runtime/src/vulkan_device_markers.h:

  • Clamp memory.memoryTypeCount to VK_MAX_MEMORY_TYPES in DeviceMarkers::memory_type() so an oversized count cannot read past memory.memoryTypes[32] or shift 1u << i by >= 32.
  • Guard DeviceMarkers::create() against a null PFN_vkGetDeviceProcAddr loader pointer.
  • Guard MarkerTarget::started() / finished() against an empty target or null marker function pointers.
  • Only call vkUnmapMemory in ~DeviceMarkers() when words_ != nullptr (i.e. vkMapMemory succeeded), avoiding unmapping unmapped memory after a failed vkMapMemory.
  • Added regression tests in runtime/test/vulkan_device_markers_test.cpp.

Verification

  • g++ -std=c++20 -D_GLIBCXX_DEBUG -Wall -Iruntime/src runtime/test/vulkan_device_markers_test.cpp passed
  • g++ -std=c++20 -fsanitize=address,undefined -Wall -Iruntime/src runtime/test/vulkan_device_markers_test.cpp passed
  • Negative control: new regression tests failed with UBSan OOB + shift-exponent-32 + ASan SEGV on unmodified ubiq/main
  • python3 -I scripts/check_no_game_data.py runtime/src/vulkan_device_markers.h runtime/test/vulkan_device_markers_test.cpp clean

Claimed in the coordination issue as: RT-VULKAN-DEVICE-MARKERS-BOUNDS-AND-UNMAP (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7193601247)
AI-assisted: yes (Gemini)

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5511065928 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2308). Originally posted by [narekshaghoyandev](https://github.com/narekshaghoyandev) on 2026-11-26T13:39:09Z. --- Fixes bounds, null-pointer, and teardown bugs in `runtime/src/vulkan_device_markers.h`: - Clamp `memory.memoryTypeCount` to `VK_MAX_MEMORY_TYPES` in `DeviceMarkers::memory_type()` so an oversized count cannot read past `memory.memoryTypes[32]` or shift `1u << i` by `>= 32`. - Guard `DeviceMarkers::create()` against a null `PFN_vkGetDeviceProcAddr` loader pointer. - Guard `MarkerTarget::started()` / `finished()` against an empty target or null marker function pointers. - Only call `vkUnmapMemory` in `~DeviceMarkers()` when `words_ != nullptr` (i.e. `vkMapMemory` succeeded), avoiding unmapping unmapped memory after a failed `vkMapMemory`. - Added regression tests in `runtime/test/vulkan_device_markers_test.cpp`. ### Verification - `g++ -std=c++20 -D_GLIBCXX_DEBUG -Wall -Iruntime/src runtime/test/vulkan_device_markers_test.cpp` passed - `g++ -std=c++20 -fsanitize=address,undefined -Wall -Iruntime/src runtime/test/vulkan_device_markers_test.cpp` passed - Negative control: new regression tests failed with UBSan OOB + shift-exponent-32 + ASan SEGV on unmodified `ubiq/main` - `python3 -I scripts/check_no_game_data.py runtime/src/vulkan_device_markers.h runtime/test/vulkan_device_markers_test.cpp` clean Claimed in the coordination issue as: RT-VULKAN-DEVICE-MARKERS-BOUNDS-AND-UNMAP (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7193601247) AI-assisted: yes (Gemini)
wavehs commented 2026-11-26 19:42:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by wavehs on 2026-11-26T19:42:19Z.

Review: COMMENTED


macOS arm64 check of #2308 (vulkan_device_markers hardening). Portable header plus its CPU test; runs here without a GPU.

Tested head 64da95c60 merged onto main bdbc7e923 as merge 1e749c907 (clean, no conflicts).

Environment: Apple M3 Pro arm64, macOS 27.2.1, Apple clang 21.0.0, CMake 4.5.0, CPython 3.15.2, Vulkan-Headers 1.4.363.0.

On the merge

  • Build and run: cmake --build build2304 --target vulkan_device_markers_test then the binary - vulkan_device_markers: ok, exit 0. ctest -R "vulkan_device_markers|vulkan_device_fault" - 2/2 passed.
  • Under -fsanitize=undefined: also ok, exit 0.
  • The four new checks are the right four: started/finished on an empty MarkerTarget{} (null function pointers), create(nullptr, ...), ~DeviceMarkers after a failed vkMapMemory (must not unmap), and memory_type with memoryTypeCount > VK_MAX_MEMORY_TYPES (the 1u << i with i >= 32).

Negative control (test kept, runtime/src/vulkan_device_markers.h reverted to main)

  • Standalone build with the test's own flags: run exits 139 (SIGSEGV) at the null-loader case, which the old code calls through unconditionally.
  • With -fsanitize=undefined -fno-sanitize-recover=undefined the run aborts first at runtime/test/../src/vulkan_device_markers.h:223:49: runtime error: index 32 out of bounds for type 'const VkMemoryType[32]', i.e. the memoryTypeCount case is a real OOB read and UBSan catches it.
  • Same NC through the CMake target: base cmake run rc=139, merge rc=0.

Nit: the memoryTypeCount case only diagnoses the bug under a sanitizer
Without UBSan, the plain build dies later on the null-loader check, so on the old header the test's failure message is a segfault, not the OOB; and on a build where the null-loader path were guarded, the EXPECT(memory_type(oversized, ...) == UINT32_MAX) would likely pass on the buggy code, because reading memoryTypes[32..39] past the array is UB whose values are whatever happens to follow the struct (the loop's result only differs if those bytes look host-visible). So the case is worth keeping, but it is only a guard when this target runs under the project's ASan/UBSan config; the PR description should say that, or the sanitizer job should include vulkan_device_markers_test. I verified it does its job there.

Nits (no change requested)

  • create with a null loader returns VK_ERROR_EXTENSION_NOT_PRESENT, which a caller may log as "extension not supported" when it is really an internal bug; a distinct code (or a comment) would read better, but it is not worth another round.
  • memory_type silently ignores bits of allowed above 31 after the clamp. memoryTypeBits has at most 32 bits, so that is fine; a one-word comment would make the clamp's intent explicit.
  • The test's oversized struct sets memoryTypeCount past the array on purpose; that is itself technically UB at the declaration, but the code under test clamps before reading, and the sanitizer run is clean on the merge, so it is acceptable as a test device.

Not run: the Windows/AMD path this feeds (the markers are behind VK_AMD_buffer_marker on a real device); nothing in this PR needs a GPU, and the header/test are stage-independent.

python -I scripts/check_no_game_data.py on both changed files: clean.

No game files. AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6079679759 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2308#pullrequestreview-6079679759). Originally posted by [wavehs](https://github.com/wavehs) on 2026-11-26T19:42:19Z. **Review:** COMMENTED --- **macOS arm64 check of #2308** (vulkan_device_markers hardening). Portable header plus its CPU test; runs here without a GPU. Tested head `64da95c60` merged onto `main` `bdbc7e923` as merge `1e749c907` (clean, no conflicts). Environment: Apple M3 Pro arm64, macOS 27.2.1, Apple clang 21.0.0, CMake 4.5.0, CPython 3.15.2, Vulkan-Headers 1.4.363.0. **On the merge** - Build and run: `cmake --build build2304 --target vulkan_device_markers_test` then the binary - `vulkan_device_markers: ok`, exit 0. `ctest -R "vulkan_device_markers|vulkan_device_fault"` - 2/2 passed. - Under `-fsanitize=undefined`: also `ok`, exit 0. - The four new checks are the right four: `started`/`finished` on an empty `MarkerTarget{}` (null function pointers), `create(nullptr, ...)`, `~DeviceMarkers` after a failed `vkMapMemory` (must not unmap), and `memory_type` with `memoryTypeCount > VK_MAX_MEMORY_TYPES` (the `1u << i` with `i >= 32`). **Negative control (test kept, `runtime/src/vulkan_device_markers.h` reverted to `main`)** - Standalone build with the test's own flags: run exits 139 (SIGSEGV) at the null-loader case, which the old code calls through unconditionally. - With `-fsanitize=undefined -fno-sanitize-recover=undefined` the run aborts first at `runtime/test/../src/vulkan_device_markers.h:223:49: runtime error: index 32 out of bounds for type 'const VkMemoryType[32]'`, i.e. the `memoryTypeCount` case is a real OOB read and UBSan catches it. - Same NC through the CMake target: `base cmake run rc=139`, merge `rc=0`. **Nit: the `memoryTypeCount` case only diagnoses the bug under a sanitizer** Without UBSan, the plain build dies later on the null-loader check, so on the old header the test's failure message is a segfault, not the OOB; and on a build where the null-loader path were guarded, the `EXPECT(memory_type(oversized, ...) == UINT32_MAX)` would likely *pass* on the buggy code, because reading `memoryTypes[32..39]` past the array is UB whose values are whatever happens to follow the struct (the loop's result only differs if those bytes look host-visible). So the case is worth keeping, but it is only a guard when this target runs under the project's ASan/UBSan config; the PR description should say that, or the sanitizer job should include `vulkan_device_markers_test`. I verified it does its job there. **Nits (no change requested)** - `create` with a null loader returns `VK_ERROR_EXTENSION_NOT_PRESENT`, which a caller may log as "extension not supported" when it is really an internal bug; a distinct code (or a comment) would read better, but it is not worth another round. - `memory_type` silently ignores bits of `allowed` above 31 after the clamp. `memoryTypeBits` has at most 32 bits, so that is fine; a one-word comment would make the clamp's intent explicit. - The test's `oversized` struct sets `memoryTypeCount` past the array on purpose; that is itself technically UB at the declaration, but the code under test clamps before reading, and the sanitizer run is clean on the merge, so it is acceptable as a test device. **Not run**: the Windows/AMD path this feeds (the markers are behind `VK_AMD_buffer_marker` on a real device); nothing in this PR needs a GPU, and the header/test are stage-independent. `python -I scripts/check_no_game_data.py` on both changed files: clean. No game files. AI-assisted.
zmasi commented 2026-11-28 20:39:58 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T20:39:58Z.


#2308 | head 64da95c60e (merged cleanly with main 1bc055e6c2) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(vulkan_device_markers|vk_device_markers)$" --output-on-failure (LEONIDA_VK_VALIDATION=1) | red on main: vulkan_device_markers crashes (SEGFAULT dereferencing null PFN_vkGetDeviceProcAddr in DeviceMarkers::create) | green on PR+main: 2/2 CTests pass (vulkan_device_markers memoryTypeCount clamp + null proc/target + failed-vkMapMemory teardown, and vk_device_markers 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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7245451266 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2308#issuecomment-7245451266). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T20:39:58Z. --- #2308 | head 64da95c60e26dde57cae8d38dc515f3f16378bdd (merged cleanly with main 1bc055e6c2dedaa0de23ec79d8c4007c1dff1fb3) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^(vulkan_device_markers|vk_device_markers)$" --output-on-failure` (`LEONIDA_VK_VALIDATION=1`) | red on main: `vulkan_device_markers` crashes (`SEGFAULT` dereferencing null `PFN_vkGetDeviceProcAddr` in `DeviceMarkers::create`) | green on PR+main: 2/2 CTests pass (`vulkan_device_markers` `memoryTypeCount` clamp + null proc/target + failed-`vkMapMemory` teardown, and `vk_device_markers` 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
timbot103 commented 2026-11-29 04:29:41 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by timbot103 on 2026-11-29T04:29:41Z.

Review: COMMENTED


Static audit of head 64da95c60 against main at b6bf3aa0b. I found no issues. No builds, tests, or GPU runs were performed for this review.

In runtime/src/vulkan_device_markers.h, clamping to VK_MAX_MEMORY_TYPES bounds both the array index and the 32-bit mask shift. The null loader returns before any lookup. Empty targets and targets without either marker callback return safely, and valid targets still prefer the synchronization2 callback when available.

On partial initialization failure, the local owner in runtime/src/vulkan_device_fault.h releases acquired resources. Creation requires vkDestroyBuffer and vkFreeMemory before allocating anything. Teardown calls vkUnmapMemory only with a successful mapping and a loaded proc, then destroys the buffer before freeing its memory. If the unmap proc is absent, freeing mapped memory also unmaps it.

Both changed files match their base versions on current main. The changes use the existing Vulkan types and calling conventions on Windows/MSVC and Linux. The CPU cases in runtime/test/vulkan_device_markers_test.cpp cover the oversized count, null loader, empty target, and failed map. The reported UBSan negative control verifies detection of the bounds regression.

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6101402596 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2308#pullrequestreview-6101402596). Originally posted by [timbot103](https://github.com/timbot103) on 2026-11-29T04:29:41Z. **Review:** COMMENTED --- Static audit of head `64da95c60` against `main` at `b6bf3aa0b`. I found no issues. No builds, tests, or GPU runs were performed for this review. In `runtime/src/vulkan_device_markers.h`, clamping to `VK_MAX_MEMORY_TYPES` bounds both the array index and the 32-bit mask shift. The null loader returns before any lookup. Empty targets and targets without either marker callback return safely, and valid targets still prefer the synchronization2 callback when available. On partial initialization failure, the local owner in `runtime/src/vulkan_device_fault.h` releases acquired resources. Creation requires `vkDestroyBuffer` and `vkFreeMemory` before allocating anything. Teardown calls `vkUnmapMemory` only with a successful mapping and a loaded proc, then destroys the buffer before freeing its memory. If the unmap proc is absent, freeing mapped memory also unmaps it. Both changed files match their base versions on current `main`. The changes use the existing Vulkan types and calling conventions on Windows/MSVC and Linux. The CPU cases in `runtime/test/vulkan_device_markers_test.cpp` cover the oversized count, null loader, empty target, and failed map. The reported UBSan negative control verifies detection of the bounds regression. AI-assisted.
OreoFR commented 2026-11-29 14:57:58 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T14:57:58Z.


TAKEN by OreoFR (Windows, RTX 4070 8 GB)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7257713517 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2308#issuecomment-7257713517). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-29T14:57:58Z. --- TAKEN by OreoFR (Windows, RTX 4070 8 GB)
OreoFR commented 2026-11-29 21:33:03 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T21:33:03Z.


Verified by OreoFR — READY

  • Base: main c22a0a732 + this PR (head 64da95c60) merged with --no-ff
  • Red (main + only this PR's test): vulkan_device_markers crashes on main (SegFault): the new cases (oversized memoryTypeCount, null get_proc_addr, empty MarkerTarget, failed vkMapMemory) reach the unguarded paths.
  • Green (merge): vulkan_device_markers passes; ctest (Ninja RelWithDebInfo, all targets, gta6rt launchers excluded) in package p1 = main + #2198 + #2333 + #2308 + #2268 (no shared files): 506 tests, new failures vs main: none. d3d12_queue_wait_timeout, d3d12_pipeline_workers and dxgi_waitable_latency 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 p1; imports unchanged vs main (25 DLLs, 755 functions, no WS2_32)
  • Diff read: no issue. memory_type clamps the count to VK_MAX_MEMORY_TYPES, so the 1u << i shift stays under 32, and the destructor unmaps only when words_ was mapped.
  • Other runs: Consistent with zmasi's run and wavehs'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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7262964053 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2308#issuecomment-7262964053). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-29T21:33:03Z. --- **Verified by OreoFR** — READY - Base: main c22a0a732 + this PR (head 64da95c60) merged with --no-ff - Red (main + only this PR's test): `vulkan_device_markers` crashes on main (SegFault): the new cases (oversized `memoryTypeCount`, null `get_proc_addr`, empty `MarkerTarget`, failed `vkMapMemory`) reach the unguarded paths. - Green (merge): `vulkan_device_markers` passes; ctest (Ninja RelWithDebInfo, all targets, gta6rt launchers excluded) in package p1 = main + #2198 + #2333 + #2308 + #2268 (no shared files): 506 tests, new failures vs main: none. `d3d12_queue_wait_timeout`, `d3d12_pipeline_workers` and `dxgi_waitable_latency` 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 p1; imports unchanged vs main (25 DLLs, 755 functions, no WS2_32) - Diff read: no issue. `memory_type` clamps the count to `VK_MAX_MEMORY_TYPES`, so the `1u << i` shift stays under 32, and the destructor unmaps only when `words_` was mapped. - Other runs: Consistent with zmasi's run and wavehs'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.
KelboLive commented 2026-11-30 17:34:48 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-11-30T17:34:48Z.


Independent check on Windows (not the author), at 64da95c60.

  • Built vulkan_device_markers_test and ran it: vulkan_device_markers: ok — including the unavailable-marker-buffer fallback (could not be made (-3)).

Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711.

AI-assisted: yes (opencode).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7286528938 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2308#issuecomment-7286528938). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-30T17:34:48Z. --- Independent check on Windows (not the author), at `64da95c60`. - Built `vulkan_device_markers_test` and ran it: **`vulkan_device_markers: ok`** — including the unavailable-marker-buffer fallback (`could not be made (-3)`). Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711. AI-assisted: yes (opencode).
aslan2142 commented 2026-12-01 16:43:40 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:43:40Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7318114168 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2308#issuecomment-7318114168). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:43:40Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!2308
No description provided.