Fix Linux sort order crash
authorMark Young <marky@lunarg.com>
Wed, 26 Jan 2022 19:15:53 +0000 (12:15 -0700)
committerMark Young <marky@lunarg.com>
Wed, 26 Jan 2022 19:51:13 +0000 (12:51 -0700)
We were completely re-creating the physical device lists every time
EnumeratePhysicalDevices was triggered in the loader.  This could
cause applications to have out-dated physical device handles.
We need to copy existing handles that match devices whenever they're
found.

loader/loader.c

index 9c0aa134e708f0c059f7b2a0b29191eaa7bf8388..c34ed891a0cec6c7a2f63cad28c2ec534dc5113f 100644 (file)
@@ -6204,6 +6204,20 @@ VkResult setup_loader_term_phys_devs(struct loader_instance *inst) {
 
         // Get the physical devices supported by platform sorting mechanism into a separate list
         res = linux_read_sorted_physical_devices(inst, icd_idx, icd_phys_dev_array, new_phys_devs);
+
+        // Keep previously allocated physical device info since apps may already be using that!
+        for (uint32_t new_idx = 0; new_idx < inst->total_gpu_count; new_idx++) {
+            for (uint32_t old_idx = 0; old_idx < inst->phys_dev_count_term; old_idx++) {
+                if (new_phys_devs[new_idx]->phys_dev == inst->phys_devs_term[old_idx]->phys_dev) {
+                    loader_log(inst, VULKAN_LOADER_INFO_BIT | VULKAN_LOADER_DRIVER_BIT, 0,
+                                "Copying old device %u into new device %u", old_idx, new_idx);
+                    // Free the old new_phys_devs info since we're not using it before we assign the new info
+                    loader_instance_heap_free(inst, new_phys_devs[new_idx]);
+                    new_phys_devs[new_idx] = inst->phys_devs_term[old_idx];
+                    break;
+                }
+            }
+        }
         goto out;
     }
 #endif  // LOADER_ENABLE_LINUX_SORT