Skip to content

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

Open
narekshaghoyandev wants to merge 1 commit into
ProjectLeonida:mainfrom
narekshaghoyandev:ubiq-vulkan-device-markers-guards
Open

narekshaghoyandev wants to merge 1 commit into
ProjectLeonida:mainfrom
narekshaghoyandev:ubiq-vulkan-device-markers-guards

Conversation

@narekshaghoyandev

Copy link
Copy Markdown
Contributor

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

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

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

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

#2308 | head 64da95c (merged cleanly with main 1bc055e) | 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 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.

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

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

TAKEN by OreoFR (Windows, RTX 4070 8 GB)

@OreoFR

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Verified by OreoFR — READY

  • Base: main c22a0a7 + this PR (head 64da95c) 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 + runtime: verify offline compute pipeline journal hits (RT-NATIVE-COMPUTE-WARMUP) #2198 + vulkan_memory_budget: saturate multi-heap uint64_t sums in sum_vram_budget #2333 + vulkan_device_markers: clamp memoryTypeCount, guard null proc/target and unmap #2308 + Quantize RGB565 render-target clears at each channel width #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.

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.

5 participants