Skip to content

initialise ext_count in linux_sort_physical_device_groups - #2082

Merged
charles-lunarg merged 1 commit into
KhronosGroup:mainfrom
aizu-m:linux-group-sort-ext-count-init
Oct 24, 2026
Merged

charles-lunarg merged 1 commit into
KhronosGroup:mainfrom
aizu-m:linux-group-sort-ext-count-init

Conversation

@aizu-m

@aizu-m aizu-m commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Valgrind, Linux Debug build, running the new test against the unpatched loader:

==20== Conditional jump or move depends on uninitialised value(s)
==20==    at 0x527CAA8: linux_sort_physical_device_groups (loader_linux.c:413)
==20==    by 0x5264023: terminator_EnumeratePhysicalDeviceGroups (loader.c:7578)
==20==    by 0x52722B7: vkEnumeratePhysicalDeviceGroups (trampoline.c:2704)
==20==  Uninitialised value was created by a stack allocation
==20==    at 0x527C5AC: linux_sort_physical_device_groups (loader_linux.c:379)

Found while reading the two Linux sorting functions after the alloca removal and the initialiser sweep in 470f90e, which touched the dev_props line two above this one. linux_read_sorted_physical_devices initialises its ext_count to 0. linux_sort_physical_device_groups declares it uninitialised and ignores the result of the count query. When the driver fails that query before writing the count (VK_ERROR_OUT_OF_HOST_MEMORY is a permitted return), the loader branches on whatever was on the stack, sizes the ext_props allocation from it and hands it to the driver as the capacity of the second query. Initialise it to 0 like the sibling, so a failed query skips the extension scan.

The test ICD gains enum_device_extension_properties_return_code, which makes vkEnumerateDeviceExtensionProperties return the given code without writing its outputs. SortedPhysicalDevices.DeviceGroupsSortedExtensionQueryFails drives a two device group through that path. It only reports the bug under Valgrind or MemorySanitizer, since an ordinary build just sees whatever value the stack held. With the fix Valgrind is clean. Full suite green under ASAN/UBSAN on Linux and macOS, clang-format 16 clean.

linux_sort_physical_device_groups passes &ext_count to the driver's
vkEnumerateDeviceExtensionProperties count query without initialising
it and without checking the result. A driver that fails the query
before writing the count leaves ext_count indeterminate, and the loader
then branches on it, sizes a heap allocation from it and passes it back
to the driver as the capacity of the second query.

The sibling linux_read_sorted_physical_devices already initialises its
ext_count to 0, so a failed query is skipped there. Do the same here.

Add a test ICD knob so vkEnumerateDeviceExtensionProperties can fail
without writing its outputs, and a device group sorting test that runs
through the failure path. Valgrind reports the uninitialised read at
loader_linux.c:413 on the unpatched loader and is clean afterwards.
@ci-tester-lunarg

Copy link
Copy Markdown

User aizu-m not on autobuild list. Waiting for curator authorization before starting CI build.

1 similar comment
@ci-tester-lunarg

Copy link
Copy Markdown

User aizu-m not on autobuild list. Waiting for curator authorization before starting CI build.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build queued with queue ID 170422.

Comment thread loader/loader_linux.c
uint32_t ext_count;
uint32_t ext_count = 0;
icd_term->dispatch.EnumerateDeviceExtensionProperties(
sorted_group_term[group].internal_device_info[gpu].physical_device, NULL, &ext_count, NULL);

@charles-lunarg charles-lunarg Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a request, just a comment about pre-existing code, but it certainly is bad form to not check the return value of vkEnumerateDeviceExtensionProperties, so thats something to fix in the future.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, the ignored VkResult is the real smell there. Zeroing the count only papers over it. I will look at checking it properly in a follow-up. Thanks for merging this.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build # 3909 running.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build # 3909 passed.

@charles-lunarg
charles-lunarg merged commit 3172c48 into KhronosGroup:main Oct 24, 2026
52 checks passed
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.

3 participants