From da74b96810eba31eb15d1cfd576fe4c69f159912 Mon Sep 17 00:00:00 2001 From: quartz-heron-58 <245169279+quartz-heron-58@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:42:48 +0800 Subject: [PATCH 1/3] vulkan: print the driver's fault report when the device is lost (RT-DEVICE-FAULT-INFO) Where the device has VK_EXT_device_fault, enable it and, the first time the device is lost, query vkGetDeviceFaultInfoEXT and print the description, the address faults and the vendor faults after the "device was lost" line. Nothing is printed and nothing changes without the extension. Co-Authored-By: Claude Fable 5.2 --- runtime/CMakeLists.txt | 15 +++ runtime/shims/d3d12_vulkan_device.cpp | 18 +++ runtime/src/vulkan_backend.h | 30 ++++- runtime/src/vulkan_device_fault.h | 106 ++++++++++++++++ runtime/test/vk_device_fault_test.cpp | 168 ++++++++++++++++++++++++++ 5 files changed, 332 insertions(+), 5 deletions(-) create mode 100644 runtime/src/vulkan_device_fault.h create mode 100644 runtime/test/vk_device_fault_test.cpp diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index a23b79290f..cab2b76914 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1644,6 +1644,21 @@ if(TARGET Vulkan::Headers) add_test(NAME vk_fence_dispatcher_sync COMMAND vk_fence_dispatcher_sync_test) set_tests_properties(vk_fence_dispatcher_sync PROPERTIES SKIP_RETURN_CODE 77) endif() + # CPU-only: the lines printed for a driver's VK_EXT_device_fault report after a device loss (a stand-in driver). + add_executable(vk_device_fault_test test/vk_device_fault_test.cpp) + target_link_libraries(vk_device_fault_test PRIVATE Vulkan::Headers ${CMAKE_DL_LIBS}) + if(WIN32) + target_compile_definitions(vk_device_fault_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + else() + find_package(Threads REQUIRED) + target_link_libraries(vk_device_fault_test PRIVATE Threads::Threads) + endif() + if(MSVC) + target_compile_options(vk_device_fault_test PRIVATE /W4 /permissive- /utf-8) + endif() + if(BUILD_TESTING) + add_test(NAME vk_device_fault COMMAND vk_device_fault_test) + endif() # CPU-only: the bindless set's array sizes from synthetic device limits. add_executable(bindless_sizing_test test/bindless_sizing_test.cpp) target_link_libraries(bindless_sizing_test PRIVATE Vulkan::Headers) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 97ed334a11..d944950a4f 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -9905,6 +9905,23 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin info.host_pointer_alignment = host_memory.minImportedHostPointerAlignment; } + // Optional: VK_EXT_device_fault lets the layer ask the driver why the device was lost and print its report after + // the "device was lost" line (src/vulkan_device_fault.h). The vendor binary is not used, so that feature stays off. + VkPhysicalDeviceFaultFeaturesEXT device_fault{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FAULT_FEATURES_EXT}; + bool has_device_fault = std::any_of(extensions.begin(), extensions.end(), [](const auto& e) { + return std::strcmp(e.extensionName, VK_EXT_DEVICE_FAULT_EXTENSION_NAME) == 0; + }); + if (has_device_fault) { + VkPhysicalDeviceFeatures2 query{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &device_fault}; + vkGetPhysicalDeviceFeatures2(info.physical, &query); + has_device_fault = device_fault.deviceFault == VK_TRUE; + } + if (has_device_fault) { + device_fault.deviceFaultVendorBinary = VK_FALSE; + device_fault.pNext = *v13.next(); + *v13.next() = &device_fault; + } + VkDeviceCreateInfo device_info{VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, &enable12}; device_info.queueCreateInfoCount = std::uint32_t(queue_infos.size()); device_info.pQueueCreateInfos = queue_infos.data(); @@ -9941,6 +9958,7 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin info.swapchain_colorspace = info.presentation && swapchain_colorspace_enabled; if (info.custom_border_color) device_extensions.push_back(custom_border_name); if (info.host_pointer_alignment) device_extensions.push_back(VK_EXT_EXTERNAL_MEMORY_HOST_EXTENSION_NAME); + if (has_device_fault) device_extensions.push_back(VK_EXT_DEVICE_FAULT_EXTENSION_NAME); device_info.enabledExtensionCount = std::uint32_t(device_extensions.size()); device_info.ppEnabledExtensionNames = device_extensions.empty() ? nullptr : device_extensions.data(); if (vkCreateDevice(info.physical, &device_info, nullptr, &owned.device) != VK_SUCCESS) diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index dcf89476cd..7f07234839 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -17,6 +17,7 @@ #include "statistics_sum_spirv.h" #include "vulkan13_features.h" #include "vulkan_depth24.h" +#include "vulkan_device_fault.h" #include "vulkan_indirect_dispatch_count.h" #include "vulkan_scratch.h" #include "vulkan_replay.h" @@ -107,7 +108,7 @@ struct VulkanDeviceFunctions { X(vkCreatePipelineCache) X(vkDestroyPipelineCache) \ X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents) X(vkCmdDrawMeshTasksEXT) \ X(vkCreateRenderPass2) X(vkDestroyRenderPass) X(vkCreateFramebuffer) X(vkDestroyFramebuffer) \ - X(vkCmdBeginRenderPass) X(vkCmdEndRenderPass) + X(vkCmdBeginRenderPass) X(vkCmdEndRenderPass) X(vkGetDeviceFaultInfoEXT) #define LEONIDA_VK_MEMBER(name) PFN_##name name = nullptr; LEONIDA_VK_DEVICE_FUNCTIONS(LEONIDA_VK_MEMBER) #undef LEONIDA_VK_MEMBER @@ -565,6 +566,22 @@ inline bool set_device_lost(std::atomic& flag, const char* where) noexcept return true; } +// What the driver says about the loss, printed after the line above by the caller whose set_device_lost returned true, +// so once per device. Only where VK_EXT_device_fault is enabled: the entry point is null otherwise and nothing is +// printed (src/vulkan_device_fault.h). device_fault_reports counts the reports printed in this process (for tests). +inline std::atomic device_fault_reports{0}; +inline void report_device_fault(const VulkanDeviceFunctions& vk, VkDevice device) noexcept { + if (!vk.vkGetDeviceFaultInfoEXT || !device) return; + try { + const auto lines = device_fault_lines([&](VkDeviceFaultCountsEXT* counts, VkDeviceFaultInfoEXT* info) { + return vk.vkGetDeviceFaultInfoEXT(device, counts, info); + }); + for (const std::string& line : lines) std::fprintf(stderr, "[rt] vulkan: %s\n", line.c_str()); + device_fault_reports.fetch_add(1, std::memory_order_relaxed); + } catch (...) { // out of memory while the device is going down: the loss itself is already said + } +} + // Centralized single-thread fence dispatcher using Vulkan 1.2 vkWaitSemaphores(VK_SEMAPHORE_WAIT_ANY_BIT). // Eliminates per-fence OS threads and 10ms polling: a synthetic timeline semaphore (wakeup_sem_) at index 0 // wakes vkWaitSemaphores immediately whenever a new wait, a released fence, or shutdown arrives while waiting_ == true. @@ -837,7 +854,9 @@ class FenceWaitDispatcher { epoch_cv_.notify_all(); if (res == VK_ERROR_DEVICE_LOST || device_lost_.load(std::memory_order_acquire)) { - set_device_lost(device_lost_, "vkWaitSemaphores in the fence dispatcher returned VK_ERROR_DEVICE_LOST"); + if (set_device_lost(device_lost_, + "vkWaitSemaphores in the fence dispatcher returned VK_ERROR_DEVICE_LOST")) + report_device_fault(vk_, device_); std::vector drained = std::move(pending_); pending_.clear(); dispatch_outside_lock(lock, drained, signal_event_); @@ -867,8 +886,9 @@ class FenceWaitDispatcher { pending_.resize(write); for (Entry& e : pending_) ready.push_back(std::move(e)); pending_.clear(); - set_device_lost(device_lost_, - "vkGetSemaphoreCounterValue in the fence dispatcher returned VK_ERROR_DEVICE_LOST"); + if (set_device_lost(device_lost_, + "vkGetSemaphoreCounterValue in the fence dispatcher returned VK_ERROR_DEVICE_LOST")) + report_device_fault(vk_, device_); dispatch_outside_lock(lock, ready, signal_event_); return; } @@ -1556,7 +1576,7 @@ class VulkanContext { std::mutex& submit_mutex() { return submit_mutex_; } void mark_device_lost(const char* where = "a Vulkan call returned VK_ERROR_DEVICE_LOST") noexcept { - set_device_lost(device_lost_, where); + if (set_device_lost(device_lost_, where)) report_device_fault(vk, device_); if (fence_dispatcher_) { try { fence_dispatcher_->notify_device_lost(); diff --git a/runtime/src/vulkan_device_fault.h b/runtime/src/vulkan_device_fault.h new file mode 100644 index 0000000000..297280b833 --- /dev/null +++ b/runtime/src/vulkan_device_fault.h @@ -0,0 +1,106 @@ +// Why a lost Vulkan device was lost, as the driver tells it through VK_EXT_device_fault. +// +// A lost device says only VK_ERROR_DEVICE_LOST. With the extension enabled the driver also keeps a short report: a +// description, the addresses of the faults it saw (a shader's read or write of memory that is not there, for one) and +// its own vendor codes. device_fault_lines() turns that report into the lines the layer prints after its "the device +// was lost" line (vulkan_backend.h, set_device_lost). It takes the query as a callable so a test can stand in for the +// driver; the layer passes vkGetDeviceFaultInfoEXT bound to its device. +// +// The vendor binary (a crash dump for the vendor's own tools) is not asked for: its size is set to 0 in the second +// call, and the deviceFaultVendorBinary feature is left off at device creation. +#pragma once + +#include + +#include +#include +#include +#include +#include + +namespace leonida { + +#ifdef VK_EXT_device_fault + +// More entries than this are not printed; the line that follows the last one says how many were left out. +constexpr std::uint32_t kDeviceFaultPrintLimit = 16; + +inline const char* device_fault_address_type(VkDeviceFaultAddressTypeEXT type) { + switch (type) { + case VK_DEVICE_FAULT_ADDRESS_TYPE_NONE_EXT: return "no address"; + case VK_DEVICE_FAULT_ADDRESS_TYPE_READ_INVALID_EXT: return "read of an invalid address"; + case VK_DEVICE_FAULT_ADDRESS_TYPE_WRITE_INVALID_EXT: return "write to an invalid address"; + case VK_DEVICE_FAULT_ADDRESS_TYPE_EXECUTE_INVALID_EXT: return "execution of an invalid address"; + case VK_DEVICE_FAULT_ADDRESS_TYPE_INSTRUCTION_POINTER_UNKNOWN_EXT: return "instruction pointer, cause unknown"; + case VK_DEVICE_FAULT_ADDRESS_TYPE_INSTRUCTION_POINTER_INVALID_EXT: return "invalid instruction pointer"; + case VK_DEVICE_FAULT_ADDRESS_TYPE_INSTRUCTION_POINTER_FAULT_EXT: return "instruction pointer of a fault"; + default: return "address fault of an unknown type"; + } +} + +// The lines of a driver's fault report, without a prefix or a newline. `query(counts, info)` is +// vkGetDeviceFaultInfoEXT for one device: called once with a null info for the counts and once more for the report. +// Always at least one line: a query that fails, or a report with nothing in it, is said as such. +template +std::vector device_fault_lines(Query&& query) { + std::vector lines; + char text[512]; + VkDeviceFaultCountsEXT counts{VK_STRUCTURE_TYPE_DEVICE_FAULT_COUNTS_EXT}; + VkResult result = query(&counts, nullptr); + if (result != VK_SUCCESS) { + std::snprintf(text, sizeof text, "the driver gave no fault report (vkGetDeviceFaultInfoEXT returned %d)", + static_cast(result)); + lines.emplace_back(text); + return lines; + } + const std::uint32_t reported_addresses = counts.addressInfoCount, reported_vendors = counts.vendorInfoCount; + counts.addressInfoCount = std::min(reported_addresses, kDeviceFaultPrintLimit); + counts.vendorInfoCount = std::min(reported_vendors, kDeviceFaultPrintLimit); + counts.vendorBinarySize = 0; + std::vector addresses(counts.addressInfoCount); + std::vector vendors(counts.vendorInfoCount); + VkDeviceFaultInfoEXT info{VK_STRUCTURE_TYPE_DEVICE_FAULT_INFO_EXT}; + info.pAddressInfos = addresses.empty() ? nullptr : addresses.data(); + info.pVendorInfos = vendors.empty() ? nullptr : vendors.data(); + result = query(&counts, &info); + if (result != VK_SUCCESS && result != VK_INCOMPLETE) { // INCOMPLETE: more than was asked for, by design here + std::snprintf(text, sizeof text, "the driver gave no fault report (vkGetDeviceFaultInfoEXT returned %d)", + static_cast(result)); + lines.emplace_back(text); + return lines; + } + // The driver writes back how many entries it filled; never read past what was allocated. + const std::uint32_t address_count = std::min(counts.addressInfoCount, std::uint32_t(addresses.size())); + const std::uint32_t vendor_count = std::min(counts.vendorInfoCount, std::uint32_t(vendors.size())); + info.description[VK_MAX_DESCRIPTION_SIZE - 1] = '\0'; + std::snprintf(text, sizeof text, "the driver's fault report (VK_EXT_device_fault): %s", + info.description[0] ? info.description : "no description"); + lines.emplace_back(text); + for (std::uint32_t i = 0; i < address_count; ++i) { + // The fault is somewhere in [reportedAddress & ~(precision - 1), that + precision): precision is a power of two. + std::snprintf(text, sizeof text, " address fault %u: %s, address 0x%llx, precision 0x%llx", i, + device_fault_address_type(addresses[i].addressType), + static_cast(addresses[i].reportedAddress), + static_cast(addresses[i].addressPrecision)); + lines.emplace_back(text); + } + for (std::uint32_t i = 0; i < vendor_count; ++i) { + vendors[i].description[VK_MAX_DESCRIPTION_SIZE - 1] = '\0'; + std::snprintf(text, sizeof text, " vendor fault %u: %s, code 0x%llx, data 0x%llx", i, + vendors[i].description[0] ? vendors[i].description : "no description", + static_cast(vendors[i].vendorFaultCode), + static_cast(vendors[i].vendorFaultData)); + lines.emplace_back(text); + } + if (!address_count && !vendor_count) lines.emplace_back(" the report names no address and no vendor fault"); + if (reported_addresses > address_count || reported_vendors > vendor_count) { + std::snprintf(text, sizeof text, " not printed: %u more address fault(s), %u more vendor fault(s)", + reported_addresses - address_count, reported_vendors - vendor_count); + lines.emplace_back(text); + } + return lines; +} + +#endif // VK_EXT_device_fault + +} // namespace leonida diff --git a/runtime/test/vk_device_fault_test.cpp b/runtime/test/vk_device_fault_test.cpp new file mode 100644 index 0000000000..263a3e207b --- /dev/null +++ b/runtime/test/vk_device_fault_test.cpp @@ -0,0 +1,168 @@ +// The lines the layer prints for a driver's VK_EXT_device_fault report (runtime/src/vulkan_device_fault.h), and that +// report_device_fault (runtime/src/vulkan_backend.h) prints nothing without the entry point: +// 1. A report with address and vendor faults: the description, one line per fault, in the driver's order. +// 2. The two-call shape: counts first with a null info, then the report with the vendor binary size set to 0. +// 3. A failed count query, and a failed report query, each give one line naming the result. +// 4. An empty report says so; a description that is not terminated is cut, not read past. +// 5. More faults than the print limit: the first ones, then how many were left out; the driver is never handed +// more room than was allocated. +// 6. A driver that writes back larger counts than it was given room for is not read past the allocation. +// 7. report_device_fault with a null vkGetDeviceFaultInfoEXT (the extension is not enabled) prints nothing. +// A stand-in for the driver throughout: needs no GPU and no Vulkan loader. No game data. +#include "../src/vulkan_backend.h" + +#include +#include +#include +#include +#include + +#define EXPECT(condition) do { \ + if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); std::_Exit(1); } \ +} while (0) + +namespace { + +struct FakeDriver { + std::string description = "a page fault"; + std::vector addresses; + std::vector vendors; + VkResult count_result = VK_SUCCESS; + VkResult report_result = VK_SUCCESS; + bool terminate_description = true; + std::uint32_t overstate_by = 0; // written back on top of the true counts in the report call + // What the caller did. + int calls = 0; + bool counts_call_had_null_info = false; + VkDeviceSize binary_size_asked = ~VkDeviceSize{0}; + std::uint32_t address_room = 0, vendor_room = 0; + + VkResult operator()(VkDeviceFaultCountsEXT* counts, VkDeviceFaultInfoEXT* info) { + ++calls; + if (!info) { + counts_call_had_null_info = true; + if (count_result != VK_SUCCESS) return count_result; + counts->addressInfoCount = std::uint32_t(addresses.size()); + counts->vendorInfoCount = std::uint32_t(vendors.size()); + counts->vendorBinarySize = 4096; // a driver that has one; the layer must not ask for it + return VK_SUCCESS; + } + binary_size_asked = counts->vendorBinarySize; + address_room = counts->addressInfoCount; + vendor_room = counts->vendorInfoCount; + if (report_result != VK_SUCCESS && report_result != VK_INCOMPLETE) return report_result; + std::memset(info->description, terminate_description ? 0 : 'x', sizeof info->description); + if (terminate_description) std::snprintf(info->description, sizeof info->description, "%s", description.c_str()); + const std::uint32_t a = std::min(address_room, std::uint32_t(addresses.size())); + const std::uint32_t v = std::min(vendor_room, std::uint32_t(vendors.size())); + for (std::uint32_t i = 0; i < a; ++i) info->pAddressInfos[i] = addresses[i]; + for (std::uint32_t i = 0; i < v; ++i) info->pVendorInfos[i] = vendors[i]; + counts->addressInfoCount = a + overstate_by; + counts->vendorInfoCount = v + overstate_by; + return a < addresses.size() || v < vendors.size() ? VK_INCOMPLETE : report_result; + } +}; + +VkDeviceFaultVendorInfoEXT vendor(const char* text, std::uint64_t code, std::uint64_t data) { + VkDeviceFaultVendorInfoEXT v{}; + std::snprintf(v.description, sizeof v.description, "%s", text); + v.vendorFaultCode = code; + v.vendorFaultData = data; + return v; +} + +bool has(const std::vector& lines, const char* part) { + for (const std::string& line : lines) + if (line.find(part) != std::string::npos) return true; + return false; +} + +} // namespace + +int main() { + using leonida::device_fault_lines; + + { // 1 and 2 + FakeDriver driver; + driver.addresses = {{VK_DEVICE_FAULT_ADDRESS_TYPE_READ_INVALID_EXT, 0x12345000, 0x1000}, + {VK_DEVICE_FAULT_ADDRESS_TYPE_WRITE_INVALID_EXT, 0xABCD0000, 0x10000}}; + driver.vendors = {vendor("channel error", 31, 0x55)}; + const auto lines = device_fault_lines(driver); + EXPECT(driver.calls == 2); + EXPECT(driver.counts_call_had_null_info); + EXPECT(driver.binary_size_asked == 0); + EXPECT(driver.address_room == 2 && driver.vendor_room == 1); + EXPECT(lines.size() == 4); + EXPECT(lines[0] == "the driver's fault report (VK_EXT_device_fault): a page fault"); + EXPECT(lines[1] == " address fault 0: read of an invalid address, address 0x12345000, precision 0x1000"); + EXPECT(lines[2] == " address fault 1: write to an invalid address, address 0xabcd0000, precision 0x10000"); + EXPECT(lines[3] == " vendor fault 0: channel error, code 0x1f, data 0x55"); + } + { // 3: the count query fails + FakeDriver driver; + driver.count_result = VK_ERROR_OUT_OF_HOST_MEMORY; + const auto lines = device_fault_lines(driver); + EXPECT(driver.calls == 1); + EXPECT(lines.size() == 1); + EXPECT(lines[0] == "the driver gave no fault report (vkGetDeviceFaultInfoEXT returned -1)"); + } + { // 3: the report query fails + FakeDriver driver; + driver.addresses = {{VK_DEVICE_FAULT_ADDRESS_TYPE_READ_INVALID_EXT, 1, 1}}; + driver.report_result = VK_ERROR_OUT_OF_HOST_MEMORY; + const auto lines = device_fault_lines(driver); + EXPECT(driver.calls == 2); + EXPECT(lines.size() == 1); + EXPECT(has(lines, "returned -1")); + } + { // 4: nothing in the report + FakeDriver driver; + driver.description.clear(); + const auto lines = device_fault_lines(driver); + EXPECT(lines.size() == 2); + EXPECT(lines[0] == "the driver's fault report (VK_EXT_device_fault): no description"); + EXPECT(lines[1] == " the report names no address and no vendor fault"); + } + { // 4: a description without a terminator + FakeDriver driver; + driver.terminate_description = false; + const auto lines = device_fault_lines(driver); + EXPECT(lines.size() == 2); + EXPECT(lines[0].size() < 600); // one bounded line, however long the unterminated text + EXPECT(has(lines, "xxxx")); + } + { // 5: more than the print limit + FakeDriver driver; + for (std::uint32_t i = 0; i < leonida::kDeviceFaultPrintLimit + 5; ++i) + driver.addresses.push_back({VK_DEVICE_FAULT_ADDRESS_TYPE_READ_INVALID_EXT, 0x1000 * (i + 1), 0x1000}); + for (std::uint32_t i = 0; i < leonida::kDeviceFaultPrintLimit + 2; ++i) driver.vendors.push_back(vendor("v", i, i)); + const auto lines = device_fault_lines(driver); + EXPECT(driver.address_room == leonida::kDeviceFaultPrintLimit); + EXPECT(driver.vendor_room == leonida::kDeviceFaultPrintLimit); + EXPECT(lines.size() == 1 + 2 * leonida::kDeviceFaultPrintLimit + 1); + EXPECT(lines.back() == " not printed: 5 more address fault(s), 2 more vendor fault(s)"); + EXPECT(has(lines, "address fault 15:")); + EXPECT(!has(lines, "address fault 16:")); + } + { // 6: counts written back larger than the room given + FakeDriver driver; + driver.addresses = {{VK_DEVICE_FAULT_ADDRESS_TYPE_EXECUTE_INVALID_EXT, 0x40, 1}}; + driver.overstate_by = 1000; + const auto lines = device_fault_lines(driver); + EXPECT(lines.size() == 2); + EXPECT(lines[1] == " address fault 0: execution of an invalid address, address 0x40, precision 0x1"); + } + { // an address type this code does not know is still printed + FakeDriver driver; + driver.addresses = {{static_cast(0x7000), 0x8, 0x8}}; + EXPECT(has(device_fault_lines(driver), "address fault of an unknown type, address 0x8")); + } + { // 7: no entry point, no report + leonida::VulkanDeviceFunctions vk{}; + const unsigned before = leonida::device_fault_reports.load(); + leonida::report_device_fault(vk, reinterpret_cast(std::uintptr_t{1})); + EXPECT(leonida::device_fault_reports.load() == before); + } + std::puts("vk_device_fault: ok"); + return 0; +} From 35e38baee361f55cdbcb0c7b9febe4b48481fdcf Mon Sep 17 00:00:00 2001 From: quartz-heron-58 <245169279+quartz-heron-58@users.noreply.github.com> Date: Wed, 25 Nov 2026 00:57:36 +0800 Subject: [PATCH 2/3] vulkan: a loss the driver shows only as a counter of UINT64_MAX is recorded VulkanFence::completed() passed a counter of UINT64_MAX on to the title when vkGetSemaphoreCounterValue succeeded with it, so the layer never learned its device was lost. It now asks vkDeviceWaitIdle under the submit lock and records the loss on VK_ERROR_DEVICE_LOST, after which the driver answers vkGetDeviceFaultInfoEXT. Co-Authored-By: Claude Fable 5.2 --- runtime/CMakeLists.txt | 16 +++ runtime/src/vulkan_backend.h | 17 +++ .../test/vk_fence_lost_by_counter_test.cpp | 134 ++++++++++++++++++ 3 files changed, 167 insertions(+) create mode 100644 runtime/test/vk_fence_lost_by_counter_test.cpp diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index cab2b76914..93b34cf18a 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1659,6 +1659,22 @@ if(TARGET Vulkan::Headers) if(BUILD_TESTING) add_test(NAME vk_device_fault COMMAND vk_device_fault_test) endif() + # A device loss the driver shows only as a timeline counter of UINT64_MAX (driver functions interposed). + add_executable(vk_fence_lost_by_counter_test test/vk_fence_lost_by_counter_test.cpp) + target_link_libraries(vk_fence_lost_by_counter_test PRIVATE Vulkan::Headers ${CMAKE_DL_LIBS}) + if(WIN32) + target_compile_definitions(vk_fence_lost_by_counter_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + else() + find_package(Threads REQUIRED) + target_link_libraries(vk_fence_lost_by_counter_test PRIVATE Threads::Threads) + endif() + if(MSVC) + target_compile_options(vk_fence_lost_by_counter_test PRIVATE /W4 /permissive- /utf-8) + endif() + if(BUILD_TESTING) + add_test(NAME vk_fence_lost_by_counter COMMAND vk_fence_lost_by_counter_test) + set_tests_properties(vk_fence_lost_by_counter PROPERTIES SKIP_RETURN_CODE 77) + endif() # CPU-only: the bindless set's array sizes from synthetic device limits. add_executable(bindless_sizing_test test/bindless_sizing_test.cpp) target_link_libraries(bindless_sizing_test PRIVATE Vulkan::Headers) diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 7f07234839..b3ed5f6c33 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -1819,6 +1819,23 @@ class VulkanFence { return UINT64_MAX; } if (result != VK_SUCCESS) return 0; + // A driver can show a lost device here without an error: the query succeeds and the counter reads UINT64_MAX + // (measured on NVIDIA 621.24, RTX 4080; vkWaitSemaphores on it succeeds too). A title reads that value as + // "device removed" and stops, so without this the layer never learned its device was gone and said nothing. + // No fence of the layer is signalled to UINT64_MAX, but the value alone is not taken as proof: vkDeviceWaitIdle + // is asked, under the submit lock its queues need, and only its VK_ERROR_DEVICE_LOST records the loss. That + // call is also what lets the driver answer vkGetDeviceFaultInfoEXT, which it refuses before it has reported + // the loss through a return code. + if (value == UINT64_MAX) { + VkResult idle = VK_SUCCESS; + { + std::lock_guard lock(context_.submit_mutex()); + idle = context_.vk.vkDeviceWaitIdle(context_.device()); + } + if (idle == VK_ERROR_DEVICE_LOST) + context_.mark_device_lost("a fence's counter read UINT64_MAX and vkDeviceWaitIdle then returned " + "VK_ERROR_DEVICE_LOST"); + } return value; } diff --git a/runtime/test/vk_fence_lost_by_counter_test.cpp b/runtime/test/vk_fence_lost_by_counter_test.cpp new file mode 100644 index 0000000000..13018eec41 --- /dev/null +++ b/runtime/test/vk_fence_lost_by_counter_test.cpp @@ -0,0 +1,134 @@ +// A device loss the driver shows only through a timeline counter (runtime/src/vulkan_backend.h, VulkanFence::completed): +// vkGetSemaphoreCounterValue succeeds with UINT64_MAX and no call has returned VK_ERROR_DEVICE_LOST yet. +// 1. With vkDeviceWaitIdle then returning VK_ERROR_DEVICE_LOST: the context is marked lost, the loss is said once, +// and the driver's fault report is printed once (a stand-in vkGetDeviceFaultInfoEXT). +// 2. A second read on the lost device says nothing more. +// 3. With vkDeviceWaitIdle returning VK_SUCCESS, the value alone records no loss and nothing is said. +// 4. An ordinary counter value never reaches vkDeviceWaitIdle. +// The driver functions are interposed on a real device's table. Skips with exit code 77 when no Vulkan 1.3 device is +// available. No game data. +#include "vk_test_device.h" +#include "../src/vulkan_backend.h" + +#include +#include +#include +#include +#include + +#define EXPECT(condition) do { \ + if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); std::_Exit(1); } \ +} while (0) + +namespace { + +namespace driver { +PFN_vkGetDeviceProcAddr real_get_device = nullptr; +PFN_vkGetSemaphoreCounterValue real_get_counter = nullptr; + +std::atomic counter_reads_max{false}; +std::atomic idle_reports_loss{false}; +std::atomic idle_calls{0}; +std::atomic fault_calls{0}; + +VKAPI_ATTR VkResult VKAPI_CALL get_semaphore_counter_value(VkDevice device, VkSemaphore sem, std::uint64_t* value) { + if (counter_reads_max.load(std::memory_order_acquire)) { + *value = UINT64_MAX; + return VK_SUCCESS; + } + return real_get_counter(device, sem, value); +} + +VKAPI_ATTR VkResult VKAPI_CALL device_wait_idle(VkDevice) { + idle_calls.fetch_add(1, std::memory_order_relaxed); + return idle_reports_loss.load(std::memory_order_acquire) ? VK_ERROR_DEVICE_LOST : VK_SUCCESS; +} + +// One address fault and no vendor fault, in the two-call shape. +VKAPI_ATTR VkResult VKAPI_CALL get_device_fault_info(VkDevice, VkDeviceFaultCountsEXT* counts, VkDeviceFaultInfoEXT* info) { + fault_calls.fetch_add(1, std::memory_order_relaxed); + if (!info) { + counts->addressInfoCount = 1; + counts->vendorInfoCount = 0; + counts->vendorBinarySize = 0; + return VK_SUCCESS; + } + std::snprintf(info->description, sizeof info->description, "stand-in fault"); + if (counts->addressInfoCount >= 1 && info->pAddressInfos) + info->pAddressInfos[0] = {VK_DEVICE_FAULT_ADDRESS_TYPE_READ_INVALID_EXT, 0x1000, 0x1000}; + return VK_SUCCESS; +} + +VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_device(VkDevice device, const char* name) { + const std::string_view n = name; + if (n == "vkGetSemaphoreCounterValue") return reinterpret_cast(get_semaphore_counter_value); + if (n == "vkDeviceWaitIdle") return reinterpret_cast(device_wait_idle); + if (n == "vkGetDeviceFaultInfoEXT") return reinterpret_cast(get_device_fault_info); + return real_get_device(device, name); +} + +void install(PFN_vkGetDeviceProcAddr device_proc, VkDevice device) { + real_get_device = device_proc; + real_get_counter = + reinterpret_cast(device_proc(device, "vkGetSemaphoreCounterValue")); +} +} // namespace driver + +} // namespace + +int main() { + using namespace leonida; + + vktest::Device d; + if (!d.create(false)) { + std::fprintf(stderr, "vk_fence_lost_by_counter_test: no Vulkan 1.3 device, skipped\n"); + return 77; + } + driver::install(d.device_proc, d.device); + + { // 4, then 3: an ordinary value, then UINT64_MAX on a device that is not lost + VulkanContext context(driver::get_device, d.device, true); + EXPECT(context.loaded()); + VulkanFence fence(context, 7); + const unsigned lost_before = device_lost_reports.load(), faults_before = device_fault_reports.load(); + EXPECT(fence.completed() == 7); + EXPECT(driver::idle_calls.load() == 0); + + driver::counter_reads_max.store(true, std::memory_order_release); + EXPECT(fence.completed() == UINT64_MAX); // what the driver said; the layer adds nothing to it + driver::counter_reads_max.store(false, std::memory_order_release); + EXPECT(driver::idle_calls.load() == 1); + EXPECT(!context.is_device_lost()); + EXPECT(device_lost_reports.load() == lost_before); + EXPECT(device_fault_reports.load() == faults_before); + EXPECT(driver::fault_calls.load() == 0); + EXPECT(fence.completed() == 7); + } + { // 1 and 2 + VulkanContext context(driver::get_device, d.device, true); + EXPECT(context.loaded()); + VulkanFence fence(context, 0); + const unsigned lost_before = device_lost_reports.load(), faults_before = device_fault_reports.load(); + const unsigned idle_before = driver::idle_calls.load(); + + driver::counter_reads_max.store(true, std::memory_order_release); + driver::idle_reports_loss.store(true, std::memory_order_release); + EXPECT(fence.completed() == UINT64_MAX); + EXPECT(context.is_device_lost()); + EXPECT(driver::idle_calls.load() == idle_before + 1); + EXPECT(device_lost_reports.load() == lost_before + 1); + EXPECT(device_fault_reports.load() == faults_before + 1); + EXPECT(driver::fault_calls.load() == 2); // the counts, then the report + + EXPECT(fence.completed() == UINT64_MAX); // lost: answered without asking the driver again + context.mark_device_lost(); + EXPECT(driver::idle_calls.load() == idle_before + 1); + EXPECT(device_lost_reports.load() == lost_before + 1); + EXPECT(device_fault_reports.load() == faults_before + 1); + EXPECT(driver::fault_calls.load() == 2); + driver::counter_reads_max.store(false, std::memory_order_release); + driver::idle_reports_loss.store(false, std::memory_order_release); + } + std::puts("vk_fence_lost_by_counter: ok"); + return 0; +} From 427abf2613a49fd1a52a50ab7cd5fa630ec780a4 Mon Sep 17 00:00:00 2001 From: quartz-heron-58 <245169279+quartz-heron-58@users.noreply.github.com> Date: Wed, 25 Nov 2026 10:52:29 +0800 Subject: [PATCH 3/3] vulkan_backend: name only the driver versions the counter loss was seen on A comment-only change: 621.24 was measured, 617.14 was reported by another tester. Co-Authored-By: Claude Fable 5.2 --- runtime/src/vulkan_backend.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 94ababb6cc..5c3e884ba6 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -2092,7 +2092,7 @@ class VulkanFence { } if (result != VK_SUCCESS) return 0; if (value == UINT64_MAX) { - // A lost device can show itself here first: measured on NVIDIA 617.14, 621.14 and 621.24, the counter + // A lost device can show itself here first: measured on NVIDIA 621.24 and reported on 617.14, the counter // query succeeds with UINT64_MAX (and vkWaitSemaphores with a zero timeout succeeds too) before any call // has returned VK_ERROR_DEVICE_LOST. vkDeviceWaitIdle is what then says so, and it is also what a driver // needs to have returned before vkGetDeviceFaultInfoEXT answers (LEONIDA_DEVICE_FAULT=1). When