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

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

@⁠narekshaghoyandev on GitHub · 2026-11-26 13:39 UTC · original
narekshaghoyandev:ubiq-vulkan-device-markers-guards → main · +36 −3 · 2 files · 1 commits · open

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 (#1608)
AI-assisted: yes (Gemini)

**@⁠narekshaghoyandev** on GitHub · 2026-11-26 13:39 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2308) `narekshaghoyandev:ubiq-vulkan-device-markers-guards` → `main` · +36 −3 · 2 files · 1 commits · open 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://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) AI-assisted: yes (Gemini)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 13:44:29 +00:00
Author
Owner

@⁠wavehs on GitHub · 2026-11-26 19:42 UTC · original · reviewed

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.

**@⁠wavehs** on GitHub · 2026-11-26 19:42 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2308#pullrequestreview-6079679759) · **reviewed** **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.
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/2308/head:github-bot/gh-2308'
git switch 'github-bot/gh-2308'

Merge

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