Public bug reported: [Impact] Multithreaded Vulkan applications crash at random, inside libvulkan, when one thread destroys a VkDevice while another thread names an object (VK_EXT_debug_utils, which wgpu uses in debug builds), loads device functions, or uses swapchain functions. Rust test suites using wgpu or ash hit it, because their tests run on parallel threads in one process.
[Test Plan] Build the attached reproduction (loader-device-race.tar.gz; see "Reproduction" below). Run it 20 times with GLIBC_TUNABLES=glibc.malloc.perturb=165. With 1.4.341.0-1 every run crashes; with the fixed package every run prints "PASS: no crash" and exits 0. Also run any Vulkan application (e.g. vkcube) to check that nothing else changed. [Where problems could occur] The fix changes the loader's locking: all loader mutexes become recursive, and the instance-list lock is merged into the global loader lock. A regression would show as a deadlock or as slower device-level lookups (object naming, vkGetDeviceProcAddr) while another thread is creating or destroying an instance or a device. Upstream has shipped this since v1.4.345 (March 2026), and Debian testing and Ubuntu's newer series carry it. [Other Info] Fixed upstream by https://github.com/KhronosGroup/Vulkan- Loader/pull/1866 (commit 5ee27b30c08505b868cb17a0683291d3a1d4543d, first released in v1.4.345), in answer to https://github.com/KhronosGroup/Vulkan-Loader/issues/1863. The commit cherry-picks onto v1.4.341 without conflicts; the result is attached as backport-5ee27b30-onto-v1.4.341.patch. A self-contained reproduction (loader-device-race.tar.gz: Cargo.toml, Cargo.lock, src/main.rs) and the log of the test runs (runs-log.md) are attached. === Affected versions === Observed (tested on this machine): - Ubuntu 26.04.1 LTS, package libvulkan1 1.4.341.0-1 (source vulkan-loader), file /usr/lib/x86_64-linux-gnu/libvulkan.so.1.4.341. - Driver: Mesa ANV (Intel Vulkan), package mesa-vulkan-drivers 26.0.8-1ubuntu0.3, on Intel UHD Graphics P630 (Comet Lake GT2), kernel i915. - Kernel 7.0.0-38-generic, glibc 2.43-2ubuntu2.7, x86_64. - Implicit layers present: VK_LAYER_MESA_device_select and VK_LAYER_MESA_anti_lag. The crash also occurs with VK_LOADER_LAYERS_DISABLE=~implicit~, so the layers are not involved. - An unmodified upstream build of tag v1.4.341 (32fcb949) crashes the same way. A build of v1.4.341 with 5ee27b30 cherry-picked does not. Inferred from reading upstream source, not tested: - The defect is in the loader itself and does not depend on the driver: the freed structure is the loader's own struct loader_device. - Every upstream release from v1.3.231 (commit 23a050bc, "Guard loader.instances access with mutex", which added the second lock) through v1.4.344 has the same two-lock pattern. That includes 1.3.275 (Ubuntu 24.04) and 1.4.309 (Debian 13). Releases before v1.3.231 were not examined. - I did not check whether the Ubuntu package carries patches of its own. The machine code of the installed library matches the unpatched source (see "Mechanism"). === Summary === The loader looks a VkDevice up by walking its global list of instances, ICD terms and logical devices while holding loader_global_instance_list_lock. vkDestroyDevice holds a different mutex, loader_lock, while it unlinks and frees that device's struct loader_device from the same list. A thread that destroys a device can therefore free a node that another thread's lookup is reading. The lookup then follows freed memory, and the process crashes with SIGSEGV or a general protection fault inside libvulkan. Both calls are legal at the same time under Vulkan's threading rules, because no externally synchronized object is shared. Using a separate VkInstance per thread does not help either, because the walk covers every instance in the process. === Mechanism (upstream tag v1.4.341, commit 32fcb949e253cbeb40cda7ea76122b492db579ae) === The reader is loader_get_icd_and_device() in loader/loader.c:1628-1652. It takes loader_global_instance_list_lock (line 1634) and walks loader.instances -> inst->icd_terms -> icd_term->logical_device_list -> dev->next. It reads dev->icd_device and dev->chain_device (line 1641) on every node until it finds the device. Callers of the reader, all of which run outside loader_lock on an application thread: - vkGetDeviceProcAddr for "vkGetDeviceQueue2" (loader/trampoline.c:140-151, call at 142). Device-loading helpers such as ash's Device::load and wgpu-hal ask for it once per device, right after vkCreateDevice. - terminator_SetDebugUtilsObjectNameEXT and terminator_SetDebugUtilsObjectTagEXT (calls at loader/generated/vk_loader_extensions.c:6350 and 6412). wgpu calls these for every labelled object in debug builds. - terminator_DebugMarkerSetObjectNameEXT and ...TagEXT (calls at vk_loader_extensions.c:5733, 5675), loader_gpa_device_terminator (call at loader.c:4615), vkSetDeviceDispatch (call at loader.c:240), vkDevExtError (vk_loader_extensions.c:43), and the swapchain terminators in loader/wsi.c (587, 2202, 2265) and loader/extension_manual.c:300. The writer is vkDestroyDevice() in loader/trampoline.c:1003-1021. It takes loader_lock (line 1016, not the instance-list lock), then calls loader_layer_destroy_device() (loader/loader.c:4953, called at trampoline.c:1018). That function calls the driver's DestroyDevice and then loader_remove_logical_device() (loader.c:1684-1701). The latter unlinks the node from icd_term->logical_device_list and frees it through loader_destroy_logical_device() (loader.c:1654). The lookup at loader.c:4961 takes and releases the instance-list lock before the unlink, so the unlink and the free happen with only loader_lock held. vkCreateDevice() (loader/trampoline.c:990-1001, lock at 997) likewise holds only loader_lock. It links the new node in loader_add_logical_device() (loader.c:1679), called from terminator_CreateDevice at loader.c:6491. Device creation alone did not crash in the tests below. Supporting evidence from the installed binary (libvulkan.so.1.4.341 from libvulkan1 1.4.341.0-1, x86_64, stripped): - The function at file offset 0x23640 is the reader. It checks the device dispatch magic 0x10aded040410aded, locks the mutex at 0x8f260, and walks the list at 0x8f240, with ICD terms at +0x380, logical device list at +0x10, next at +0x1c98, and dispatch pointers at +0x1c40/+0x1c48. - vkDestroyDevice (0x45970) locks only the mutex at 0x8f2a0 and calls 0x2b630. That function calls the reader, calls the driver, clears +0x1c40, unlinks the node through +0x1c98 and frees it. - vkGetDeviceProcAddr (0x45750) calls the reader only for the name "GetDeviceQueue2". - The faulting instructions are 0x236d4 to 0x23706, all inside the walk loop. === Reproduction === Files (attached as loader-device-race.tar.gz): Cargo.toml, Cargo.lock and src/main.rs. The program is about 90 lines of Rust on ash 0.38.0 (pinned with =0.38.0; Cargo.lock pins ash 0.38.0+1.3.281, libloading 0.8.9, cfg-if 1.0.5 and windows-link 0.2.1). It creates one VkInstance with VK_EXT_debug_utils. Four walker threads each create their own VkDevice, then call vkSetDebugUtilsObjectNameEXT on it in a loop. Meanwhile the main thread creates and destroys 300 devices. With LEAK=1 it creates the 300 devices and never destroys them. No object is used by two threads at once. Build and run (Rust 1.100.0 was used; any recent stable should do): cargo build --release GLIBC_TUNABLES=glibc.malloc.perturb=165 ./target/release/loader-device-race # crashes ./target/release/loader-device-race # usually passes GLIBC_TUNABLES=glibc.malloc.perturb=165 LEAK=1 ./target/release/loader-device-race # passes Optional variables: WALKERS (default 4), CYCLES (default 300), DEVICE (physical device index, default 0). To run against another loader build for one process, set LD_LIBRARY_PATH=<build>/loader. glibc.malloc.perturb=165 makes glibc fill freed memory with a fixed byte. Without it, a freed loader_device (about 7 KB) usually keeps its old contents until it is reused, so the stale walk mostly succeeds. With it, the first stale read finds garbage. A passing run prints, and exits 0: device: Intel(R) UHD Graphics P630 (CML GT2); walkers: 4; name calls: 2708495; devices created: 300; destroyed: 300 PASS: no crash A crashing run prints nothing and is killed by SIGSEGV (shell exit status 139). The kernel logs, for example: traps: loader-device-r[...] general protection fault ip:7d9c3f2b4706 sp:7d9c33f98630 error:0 in libvulkan.so.1.4.341[23706,7d9c3f291000+57000] Results (each run in its own process; the full table is in the attached runs-log.md): | Loader | Fill | Devices destroyed | Crashed / runs | |---|---|---|---| | system 1.4.341.0-1 | yes | yes | 20 / 20 | | system 1.4.341.0-1 | no | yes | 1 / 20 | | system 1.4.341.0-1 | yes | no (LEAK=1) | 0 / 20 | | system, implicit layers disabled | yes | yes | 10 / 10 | | upstream v1.4.341 built from source | yes | yes | 30 / 30 | | v1.4.341 + 5ee27b30 cherry-picked | yes | yes | 0 / 20 | | upstream fcb45d97 (parent of 5ee27b30) | yes | yes | 10 / 10 | | upstream 5ee27b30 | yes | yes | 0 / 10 | | upstream main 7d76ff3c (2026-11-23) | yes / no | yes | 0 / 10, 0 / 10 | === Crash signature === SIGSEGV (si_code 128, i.e. a general protection fault on a non-canonical address; also seen as a plain segfault at small or garbage addresses). It is always at libvulkan.so.1.4.341 file offsets 0x236d4 to 0x23706. The same signature appeared in real applications: Rust test binaries (wgpu and ash) whose tests open and drop devices on several threads at once. Symbolised stack of the crashing thread, from a core of the upstream v1.4.341 build with debug info: #0 loader_get_icd_and_device loader/loader.c:1641 #1 terminator_SetDebugUtilsObjectNameEXT loader/generated/vk_loader_extensions.c:6351 #2 SetDebugUtilsObjectNameEXT loader/generated/vk_loader_extensions.c:6344 #3 application thread (ash ext::debug_utils::Device::set_debug_utils_object_name) The stripped system library shows the same shape: 0x23706 <- 0x1c34b <- 0x1bf88 <- application. === Existing reports checked === - Vulkan-Loader GitHub: #1863 is the same defect, fixed by #1866 (5ee27b30). #2018 is the same mechanism reported against 1.4.341 and closed as not reproducing on main. I found these with the GitHub search vkDestroyDevice thread and by looking up the pull request of commit 5ee27b30. #1739 (a deadlock on device destruction) is fixed by the same pull request, but it is a different symptom. - Red Hat bug 2416557 (https://bugzilla.redhat.com/show_bug.cgi?id=2416557): a SIGSEGV in the validation layers' thread-safety tracker (threadsafety::Counter<VkInstance_T*>::StartRead, vkcube with validation and MESA_device_select). It is similar in theme only: a different component and a different object, and validation layers are not installed here. - Launchpad, source package vulkan-loader: no report of this. The only open bug, #2171977, is a stack-smashing abort during instance setup (vkEnumerateInstanceExtensionProperties), which is a different defect. - Debian BTS, source vulkan-loader: no report of this. #1129753 is a fault inside libvulkan_nouveau.so, which is a different component. === Fix === Upstream's fix (5ee27b30) makes every loader mutex recursive. It removes loader_global_instance_list_lock and has loader_get_icd_and_device() and the other instance-list users take loader_lock, the lock that vkCreateDevice and vkDestroyDevice already hold. The walk and the free are then mutually exclusive. The commit also comments out two writes to cJSON's global error state (a data race the thread sanitizer reported) and adds a threading test that names objects from several threads. It cherry-picks onto v1.4.341 without conflicts, as 9 files changed, 89 insertions and 24 deletions. The patch is attached as backport-5ee27b30-onto-v1.4.341.patch (author and message as upstream's). A build of v1.4.341 with it passes the reproduction 20 of 20 times with the fill. No separate fix was written, because the newest upstream no longer has the defect. Cost, measured roughly: with the fix, a lookup waits for loader_lock, which vkCreateDevice and vkDestroyDevice hold for their whole duration. In the reproduction the walkers completed 68,000 to 82,000 name calls per run (backport) instead of 2.2 to 2.7 million (system loader). The run of 100 device cycles also finished faster (0.18 s against 0.3 s), so in this test device creation was not slowed. The uncontended cost of a lookup was not measured. It adds one recursive-mutex lock and unlock per call, as before but on a different mutex. === Workaround for applications === Do not destroy a VkDevice (or a VkInstance) while any other thread of the process may be making Vulkan calls. In practice that means keeping devices alive until the process exits and reusing them, rather than creating and destroying one per short-lived renderer. It also works to run each test that opens a device in its own process, or to serialise every device-level call, which is only practical when the application controls all of them. Creating devices while other threads run lookups did not crash in testing: 20 runs with the fill and LEAK=1 all passed. ** Affects: vulkan-loader (Ubuntu) Importance: Undecided Status: New ** Tags: resolute -- You received this bug notification because you are a member of Ubuntu Bugs, which is subscribed to Ubuntu. https://bugs.launchpad.net/bugs/2175727 Title: vulkan-loader 1.4.341: use-after-free (SIGSEGV) when vkDestroyDevice races a device lookup on another thread; please backport upstream commit 5ee27b30 To manage notifications about this bug go to: https://bugs.launchpad.net/ubuntu/+source/vulkan-loader/+bug/2175727/+subscriptions -- ubuntu-bugs mailing list [email protected] https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs
