Repository navigation
vulkan_device_markers: clamp memoryTypeCount, guard null proc/target and unmap - #2308
narekshaghoyandev wants to merge 1 commit into
Conversation
wavehs
left a comment
There was a problem hiding this comment.
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_testthen the binary -vulkan_device_markers: ok, exit 0.ctest -R "vulkan_device_markers|vulkan_device_fault"- 2/2 passed. - Under
-fsanitize=undefined: alsook, exit 0. - The four new checks are the right four:
started/finishedon an emptyMarkerTarget{}(null function pointers),create(nullptr, ...),~DeviceMarkersafter a failedvkMapMemory(must not unmap), andmemory_typewithmemoryTypeCount > VK_MAX_MEMORY_TYPES(the1u << iwithi >= 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=undefinedthe run aborts first atruntime/test/../src/vulkan_device_markers.h:223:49: runtime error: index 32 out of bounds for type 'const VkMemoryType[32]', i.e. thememoryTypeCountcase is a real OOB read and UBSan catches it. - Same NC through the CMake target:
base cmake run rc=139, mergerc=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)
createwith a null loader returnsVK_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_typesilently ignores bits ofallowedabove 31 after the clamp.memoryTypeBitshas at most 32 bits, so that is fine; a one-word comment would make the clamp's intent explicit.- The test's
oversizedstruct setsmemoryTypeCountpast 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.
|
#2308 | head 64da95c (merged cleanly with main 1bc055e) | |
timbot103
left a comment
There was a problem hiding this comment.
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.
|
TAKEN by OreoFR (Windows, RTX 4070 8 GB) |
|
Verified by OreoFR — READY
Ready to merge per the maintainers' plan. AI-assisted (Claude Code, Claude Opus 5.5), run for OreoFR. |
|
Independent check on Windows (not the author), at
Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711. AI-assisted: yes (opencode). |
Fixes bounds, null-pointer, and teardown bugs in
runtime/src/vulkan_device_markers.h:memory.memoryTypeCounttoVK_MAX_MEMORY_TYPESinDeviceMarkers::memory_type()so an oversized count cannot read pastmemory.memoryTypes[32]or shift1u << iby>= 32.DeviceMarkers::create()against a nullPFN_vkGetDeviceProcAddrloader pointer.MarkerTarget::started()/finished()against an empty target or null marker function pointers.vkUnmapMemoryin~DeviceMarkers()whenwords_ != nullptr(i.e.vkMapMemorysucceeded), avoiding unmapping unmapped memory after a failedvkMapMemory.runtime/test/vulkan_device_markers_test.cpp.Verification
g++ -std=c++20 -D_GLIBCXX_DEBUG -Wall -Iruntime/src runtime/test/vulkan_device_markers_test.cpppassedg++ -std=c++20 -fsanitize=address,undefined -Wall -Iruntime/src runtime/test/vulkan_device_markers_test.cpppassedubiq/mainpython3 -I scripts/check_no_game_data.py runtime/src/vulkan_device_markers.h runtime/test/vulkan_device_markers_test.cppcleanClaimed in the coordination issue as: RT-VULKAN-DEVICE-MARKERS-BOUNDS-AND-UNMAP (#1608 (comment))
AI-assisted: yes (Gemini)