Skip to content

check for live devices in unload_drivers_without_physical_devices - #2103

Merged
charles-lunarg merged 2 commits into
KhronosGroup:mainfrom
aizu-m:unload-driver-live-device
Nov 10, 2026
Merged

charles-lunarg merged 2 commits into
KhronosGroup:mainfrom
aizu-m:unload-driver-live-device

Conversation

@aizu-m

@aizu-m aizu-m commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

AddressSanitizer, macOS Debug build with LOADER_ENABLE_ADDRESS_SANITIZER=ON, new test run against the unpatched loader:

ERROR: AddressSanitizer: heap-use-after-free on address 0x62400000e100
READ of size 8 at 0x62400000e100 thread T0
    #0 in loader_get_dispatch loader.h:58
    #1 in loader_validate_and_get_device_dispatch loader.h:95
    #2 in vkGetDeviceQueue trampoline.c:1043
0x62400000e100 is located 0 bytes inside of 7792-byte region [0x62400000e100,0x62400000ff70)
freed by thread T0 here:
    #3 in loader_destroy_logical_device loader.c:1416
    #4 in loader_icd_destroy loader.c:1508
    #5 in unload_drivers_without_physical_devices loader.c:6508
    #6 in vkEnumeratePhysicalDevices trampoline.c:940
previously allocated by thread T0 here:
    #2 in loader_create_logical_device loader.c:1421
    #4 in vkCreateDevice trampoline.c:959

Found while checking what a second vkEnumeratePhysicalDevices call frees. The existing DriverUnloadingFromZeroPhysDevs tests only cover drivers that never had a device.

unload_drivers_without_physical_devices runs after every enumeration that fills an array. It unloads any driver whose latest enumeration gave no devices: vkDestroyInstance on the driver, loader_icd_destroy, then dlclose. loader_icd_destroy also frees every loader_device on the driver's logical_device_list.

physical_device_count is recomputed on each call. A driver that reported a device before and reports none now (an enumeration error, a lost or unplugged GPU) is unloaded while the app still holds a VkDevice from it. That VkDevice, its queues and its command buffers all dispatch through the freed loader_device. The next device call reads the freed table and calls a function pointer taken from it, into a driver library that has been closed. A plain vkDestroyDevice is enough to hit it.

The fix skips drivers that still have logical devices. Such a driver is unloaded by a later enumeration once its devices are gone, or at instance destruction. The check sits in the unload helper because that is the only place the loader decides a driver is unused.

DriverUnloadingFromZeroPhysDevs.DriverWithLiveDeviceNotUnloaded creates a VkDevice on each of two drivers, makes one driver fail vkEnumeratePhysicalDevices, re-enumerates and calls vkGetDeviceQueue on both devices. It fails as above before the change and passes after. The full ctest suite passes under ASan, except integration.find_package, which fails the same way on an untouched tree here because of a local SDK issue.

One related problem with the same trigger is left alone. loader_device::phys_dev_term still points at the terminator object that setup_loader_term_phys_devs frees when the device drops out of enumeration. loader_lookup_device_dispatch_table reads it on vkGetDeviceProcAddr, so ASan reports a second use-after-free if the test also calls vkGetDeviceProcAddr(dev, "vkCreateBuffer") on the orphaned device. Fixing that means changing the generated code, so it fits better as its own change.

unload_drivers_without_physical_devices runs after every
vkEnumeratePhysicalDevices call that fills an array. It unloads each
driver whose latest enumeration produced no physical devices: it
destroys the driver's VkInstance, frees the loader_icd_term through
loader_icd_destroy and closes the driver library. loader_icd_destroy
also frees every loader_device on the driver's logical_device_list.

physical_device_count is recomputed on every enumeration, so a driver
that reported a device earlier and reports none now is unloaded while
the application still holds a VkDevice created from that device. The
VkDevice, its queues and command buffers dispatch through the freed
loader_device, so the next device-level call reads the freed dispatch
table and calls a function pointer taken from it.

Skip drivers that still have logical devices. Such a driver is
unloaded by a later enumeration once its devices are destroyed, or at
instance destruction.

Add DriverUnloadingFromZeroPhysDevs.DriverWithLiveDeviceNotUnloaded.
On the unpatched loader AddressSanitizer reports a heap-use-after-free
in vkGetDeviceQueue; it passes with the change.
@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.

@charles-lunarg charles-lunarg left a comment

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.

While real world drivers probably won't do this, it is something that could happen (even if there isn't much support for hot-reloading in vulkan, it isn't explicitly unsupported) so the loader should be robust to changes.

Added suggested change for what should fix the CI errors.

Comment thread tests/loader_regression_tests.cpp Resolved
@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build queued with queue ID 188632.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build # 3979 running.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build # 3979 failed.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build queued with queue ID 191637.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build queued with queue ID 191654.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build # 3983 running.

@ci-tester-lunarg

Copy link
Copy Markdown

CI Vulkan-Loader build # 3983 passed.

@charles-lunarg

Copy link
Copy Markdown
Collaborator

Macos CI failed because the macos-14 runners were deprecated. I have #2105 to fix it, but I'd rather merge this now to avoid needless rerunning of CI.

@charles-lunarg
charles-lunarg merged commit 41fb81b into KhronosGroup:main Nov 10, 2026
46 of 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