diff options
| author | MoonEyes <ducoinbenjamin@yahoo.fr> | 2026-11-29 16:45:59 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-29 16:45:59 +0100 |
| commit | 0cedc3a625bfde8cb58bbcaaaa0c4ea19fa2ae6e (patch) | |
| tree | b998942ab27b8962d2382c6f4f250f392315dadf | |
| parent | cec4182c2db2f171f8a2a1f61b0ffe90739ad556 (diff) | |
| parent | 767ce20cb9816acee7baad8526d996fa3dab41af (diff) | |
Merge pull request #1918 from kawaii-shakal/fault-address-owner
d3d12: the device-fault report names what the process and the driver had at each fault address (RT-FAULT-ADDRESS-OWNER)
| -rw-r--r-- | docs/d3d12_vulkan.md | 21 | ||||
| -rw-r--r-- | runtime/CMakeLists.txt | 4 | ||||
| -rw-r--r-- | runtime/shims/d3d12_vulkan_device.cpp | 10 | ||||
| -rw-r--r-- | runtime/src/address_binding.h | 303 | ||||
| -rw-r--r-- | runtime/src/vulkan_device_fault.h | 21 | ||||
| -rw-r--r-- | runtime/test/address_binding_test.cpp | 265 |
6 files changed, 623 insertions, 1 deletions
diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index d41fcc68a..b6df484bc 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md | |||
| @@ -5004,6 +5004,27 @@ when it cannot sample the submission lock for that long. A progress warning leav | |||
| 5004 | device state intact. The driver checkpoint and fault-info queries run after Vulkan reports device loss. This mode | 5004 | device state intact. The driver checkpoint and fault-info queries run after Vulkan reports device loss. This mode |
| 5005 | is off by default. | 5005 | is off by default. |
| 5006 | 5006 | ||
| 5007 | Each fault address is also looked up in this process (RT-FAULT-ADDRESS-OWNER). On Windows, `VirtualQuery` of the | ||
| 5008 | same number gives the region's base, allocation base, state, protection and type. A GPU address isn't a CPU one, | ||
| 5009 | but a host import, a range of the title's memory and a raw CPU pointer that reached a shader all share the | ||
| 5010 | number. With `LEONIDA_ADDRESS_BINDINGS=1` as well, the device enables `VK_EXT_device_address_binding_report`, and | ||
| 5011 | a debug messenger records the GPU virtual address range of every buffer, image and allocation the driver binds | ||
| 5012 | and unbinds (`runtime/src/address_binding.h`). The fault report then lists, for each address: | ||
| 5013 | - the live objects that contain the address ("containing"), and those that only share its reported page, whose | ||
| 5014 | size is the fault's `addressPrecision` ("on the page"); | ||
| 5015 | - the live objects that end nearest below it and start nearest above it; | ||
| 5016 | - up to 16 recently unbound objects that held it, with their type, range, handle and the times they were bound | ||
| 5017 | and unbound. | ||
| 5018 | |||
| 5019 | "Nothing live or recently unbound there" means that no object of this device had that GPU address. On an | ||
| 5020 | RTX 5070 Ti the world-start loss of 1.0.2.1 reported write addresses far above every bound range, at about 50,000 | ||
| 5021 | binds. The record keeps the last 16,384 unbinds and costs one map entry per live binding. | ||
| 5022 | |||
| 5023 | There is one record per process. A second Vulkan instance in the same process (a second device) is refused a | ||
| 5024 | messenger, and its device doesn't enable the extension, so the two devices' GPU addresses aren't mixed. If | ||
| 5025 | `LEONIDA_ADDRESS_BINDINGS=1` is set but no messenger records the device, the fault report says that the bindings | ||
| 5026 | were not recorded, so this can't be mistaken for "nothing live or recently unbound there". | ||
| 5027 | |||
| 5007 | Each dispatch line carries a `seq`: a number every recorded dispatch gets, across queues and submissions, in the | 5028 | Each dispatch line carries a `seq`: a number every recorded dispatch gets, across queues and submissions, in the |
| 5008 | order the submissions were recorded. The list keeps the last 64, so its numbers can have gaps where a submission | 5029 | order the submissions were recorded. The list keeps the last 64, so its numbers can have gaps where a submission |
| 5009 | recorded more than that. | 5030 | recorded more than that. |
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index b0b4dcbca..b5fca3b27 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt | |||
| @@ -2154,6 +2154,10 @@ if(TARGET Vulkan::Headers) | |||
| 2154 | add_executable(vulkan_device_fault_test test/vulkan_device_fault_test.cpp) | 2154 | add_executable(vulkan_device_fault_test test/vulkan_device_fault_test.cpp) |
| 2155 | target_link_libraries(vulkan_device_fault_test PRIVATE Vulkan::Headers) | 2155 | target_link_libraries(vulkan_device_fault_test PRIVATE Vulkan::Headers) |
| 2156 | add_test(NAME vulkan_device_fault COMMAND vulkan_device_fault_test) | 2156 | add_test(NAME vulkan_device_fault COMMAND vulkan_device_fault_test) |
| 2157 | add_executable(address_binding_test test/address_binding_test.cpp) | ||
| 2158 | target_link_libraries(address_binding_test PRIVATE Vulkan::Headers) | ||
| 2159 | add_test(NAME address_binding COMMAND address_binding_test) | ||
| 2160 | |||
| 2157 | # The AMD breadcrumbs (LEONIDA_DEVICE_MARKERS): seq numbering, marker writes and the likely-hang verdict, on the CPU. | 2161 | # The AMD breadcrumbs (LEONIDA_DEVICE_MARKERS): seq numbering, marker writes and the likely-hang verdict, on the CPU. |
| 2158 | add_executable(vulkan_device_markers_test test/vulkan_device_markers_test.cpp) | 2162 | add_executable(vulkan_device_markers_test test/vulkan_device_markers_test.cpp) |
| 2159 | target_link_libraries(vulkan_device_markers_test PRIVATE Vulkan::Headers) | 2163 | target_link_libraries(vulkan_device_markers_test PRIVATE Vulkan::Headers) |
diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index af361905e..5c33a03df 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp | |||
| @@ -16311,7 +16311,10 @@ public: | |||
| 16311 | state_.context.reset(); | 16311 | state_.context.reset(); |
| 16312 | state_.image_memory.reset(); // the context's deferred queue gave its ranges back | 16312 | state_.image_memory.reset(); // the context's deferred queue gave its ranges back |
| 16313 | if (owned_.device) owned_.destroy_device(owned_.device, nullptr); | 16313 | if (owned_.device) owned_.destroy_device(owned_.device, nullptr); |
| 16314 | if (owned_.instance) owned_.destroy_instance(owned_.instance, nullptr); | 16314 | if (owned_.instance) { |
| 16315 | leonida::address_binding::uninstall_messenger(owned_.instance); | ||
| 16316 | owned_.destroy_instance(owned_.instance, nullptr); | ||
| 16317 | } | ||
| 16315 | } | 16318 | } |
| 16316 | 16319 | ||
| 16317 | bool ok() const { return state_.context && state_.context->loaded(); } | 16320 | bool ok() const { return state_.context && state_.context->loaded(); } |
| @@ -22748,8 +22751,11 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin | |||
| 22748 | return E_FAIL; | 22751 | return E_FAIL; |
| 22749 | } | 22752 | } |
| 22750 | owned.destroy_instance = reinterpret_cast<PFN_vkDestroyInstance>(get(owned.instance, "vkDestroyInstance")); | 22753 | owned.destroy_instance = reinterpret_cast<PFN_vkDestroyInstance>(get(owned.instance, "vkDestroyInstance")); |
| 22754 | // LEONIDA_ADDRESS_BINDINGS: the device's bindings reach this messenger from vkCreateDevice on (address_binding.h). | ||
| 22755 | if (has_debug_utils) leonida::address_binding::install_messenger(owned.instance, get); | ||
| 22751 | const auto fail = [&](const char* why) { | 22756 | const auto fail = [&](const char* why) { |
| 22752 | std::fprintf(stderr, "[rt] d3d12: %s\n", why); | 22757 | std::fprintf(stderr, "[rt] d3d12: %s\n", why); |
| 22758 | leonida::address_binding::uninstall_messenger(owned.instance); | ||
| 22753 | owned.destroy_instance(owned.instance, nullptr); | 22759 | owned.destroy_instance(owned.instance, nullptr); |
| 22754 | return E_FAIL; | 22760 | return E_FAIL; |
| 22755 | }; | 22761 | }; |
| @@ -23502,6 +23508,8 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin | |||
| 23502 | if (aftermath && !info.device_diagnostics_config) | 23508 | if (aftermath && !info.device_diagnostics_config) |
| 23503 | std::fprintf(stderr, "[rt] aftermath: VK_NV_device_diagnostics_config unavailable; the dump has no shader " | 23509 | std::fprintf(stderr, "[rt] aftermath: VK_NV_device_diagnostics_config unavailable; the dump has no shader " |
| 23504 | "debug info, resource tracking or checkpoints\n"); | 23510 | "debug info, resource tracking or checkpoints\n"); |
| 23511 | leonida::address_binding::enable_device(owned.instance, info.physical, vkGetPhysicalDeviceFeatures2, extensions, | ||
| 23512 | device_extensions, device_info); | ||
| 23505 | device_info.enabledExtensionCount = std::uint32_t(device_extensions.size()); | 23513 | device_info.enabledExtensionCount = std::uint32_t(device_extensions.size()); |
| 23506 | device_info.ppEnabledExtensionNames = device_extensions.empty() ? nullptr : device_extensions.data(); | 23514 | device_info.ppEnabledExtensionNames = device_extensions.empty() ? nullptr : device_extensions.data(); |
| 23507 | if (vkCreateDevice(info.physical, &device_info, nullptr, &owned.device) != VK_SUCCESS) | 23515 | if (vkCreateDevice(info.physical, &device_info, nullptr, &owned.device) != VK_SUCCESS) |
diff --git a/runtime/src/address_binding.h b/runtime/src/address_binding.h new file mode 100644 index 000000000..b8c54e18b --- /dev/null +++ b/runtime/src/address_binding.h | |||
| @@ -0,0 +1,303 @@ | |||
| 1 | #pragma once | ||
| 2 | // LEONIDA_ADDRESS_BINDINGS=1, with LEONIDA_DEVICE_FAULT=1: names what the driver had bound at a device-fault address | ||
| 3 | // (RT-FAULT-ADDRESS-OWNER). VK_EXT_device_address_binding_report has the driver report the GPU virtual address range | ||
| 4 | // of every buffer, image and allocation as it is bound and unbound, to the instance's debug messengers. This keeps | ||
| 5 | // the live ranges and the most recent unbound ones, so the device-fault report (vulkan_device_fault.h) can say which | ||
| 6 | // object contains a faulting address, which objects end right below it and start right above it, and which objects | ||
| 7 | // held it before. An address that none of them covers is a GPU address no object of this device was ever given. | ||
| 8 | // | ||
| 9 | // The record and the report are pure (record_binding, report); the Vulkan side is install_messenger before | ||
| 10 | // vkCreateDevice's bindings start and enable_device on the device's create chain. | ||
| 11 | |||
| 12 | #include <vulkan/vulkan.h> | ||
| 13 | |||
| 14 | #include <algorithm> | ||
| 15 | #include <atomic> | ||
| 16 | #include <chrono> | ||
| 17 | #include <cinttypes> | ||
| 18 | #include <cstdint> | ||
| 19 | #include <cstdio> | ||
| 20 | #include <cstdlib> | ||
| 21 | #include <cstring> | ||
| 22 | #include <deque> | ||
| 23 | #include <map> | ||
| 24 | #include <mutex> | ||
| 25 | #include <span> | ||
| 26 | #include <string> | ||
| 27 | #include <vector> | ||
| 28 | |||
| 29 | namespace leonida::address_binding { | ||
| 30 | |||
| 31 | struct Entry { | ||
| 32 | std::uint64_t base = 0, size = 0; | ||
| 33 | VkObjectType type = VK_OBJECT_TYPE_UNKNOWN; | ||
| 34 | std::uint64_t handle = 0; | ||
| 35 | bool internal = false; // VK_DEVICE_ADDRESS_BINDING_INTERNAL_OBJECT_BIT_EXT | ||
| 36 | double bound_at = 0, unbound_at = 0; // seconds since the record started; unbound_at 0 while live | ||
| 37 | }; | ||
| 38 | |||
| 39 | class Record { | ||
| 40 | public: | ||
| 41 | static constexpr std::size_t kept_unbound = 16384; | ||
| 42 | |||
| 43 | // One report: bound, or unbound (matched to its live entry by base, handle and size). | ||
| 44 | void record(Entry e, bool bind, double now) { | ||
| 45 | std::lock_guard lock(mutex_); | ||
| 46 | if (bind) { | ||
| 47 | e.bound_at = now; | ||
| 48 | live_.emplace(e.base, e); | ||
| 49 | ++binds_; | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | ++unbinds_; | ||
| 53 | auto [first, last] = live_.equal_range(e.base); | ||
| 54 | for (auto it = first; it != last; ++it) { | ||
| 55 | if (it->second.handle != e.handle || it->second.size != e.size) continue; | ||
| 56 | e = it->second; | ||
| 57 | live_.erase(it); | ||
| 58 | break; | ||
| 59 | } | ||
| 60 | e.unbound_at = now; | ||
| 61 | unbound_.push_back(e); | ||
| 62 | if (unbound_.size() > kept_unbound) unbound_.pop_front(); | ||
| 63 | } | ||
| 64 | |||
| 65 | // What was bound at and around the `precision`-sized page of `address`, one line per finding. | ||
| 66 | std::string report(std::uint64_t address, std::uint64_t precision) const { | ||
| 67 | std::lock_guard lock(mutex_); | ||
| 68 | std::string out; | ||
| 69 | char line[160]; | ||
| 70 | std::snprintf(line, sizeof line, "0x%" PRIx64 " (precision 0x%" PRIx64 "): %" PRIu64 " binds, %" PRIu64 | ||
| 71 | " unbinds, %zu live\n", | ||
| 72 | address, precision, binds_, unbinds_, live_.size()); | ||
| 73 | out += line; | ||
| 74 | const std::uint64_t lo = precision > 1 ? address & ~(precision - 1) : address; | ||
| 75 | const std::uint64_t hi = lo + (precision > 1 ? precision : 1); | ||
| 76 | const auto overlaps = [&](const Entry& e) { return e.base < hi && e.base + e.size > lo; }; | ||
| 77 | int containing = 0; | ||
| 78 | const Entry* below = nullptr; | ||
| 79 | const Entry* above = nullptr; | ||
| 80 | // "containing" only for an object the address itself is in; one that shares only the reported page with it is | ||
| 81 | // "on the page", since the driver gives the fault address no closer than `precision`. | ||
| 82 | const auto inside = [&](const Entry& e) { return address >= e.base && address - e.base < e.size; }; | ||
| 83 | for (const auto& [base, e] : live_) { | ||
| 84 | if (overlaps(e)) { | ||
| 85 | if (containing++ < 16) out += describe(inside(e) ? "live, containing" : "live, on the page", e, address); | ||
| 86 | continue; | ||
| 87 | } | ||
| 88 | if (base + e.size <= lo && (!below || base + e.size > below->base + below->size)) below = &e; | ||
| 89 | if (base >= hi && (!above || base < above->base)) above = &e; | ||
| 90 | } | ||
| 91 | if (below) out += describe("live, nearest below", *below, address); | ||
| 92 | if (above) out += describe("live, nearest above", *above, address); | ||
| 93 | int held = 0; | ||
| 94 | for (auto it = unbound_.rbegin(); it != unbound_.rend() && held < 16; ++it) | ||
| 95 | if (overlaps(*it)) { | ||
| 96 | out += describe(inside(*it) ? "unbound, held it" : "unbound, held the page", *it, address); | ||
| 97 | ++held; | ||
| 98 | } | ||
| 99 | if (!containing && !held) out += " nothing live or recently unbound there\n"; | ||
| 100 | return out; | ||
| 101 | } | ||
| 102 | |||
| 103 | private: | ||
| 104 | static const char* type_name(VkObjectType t) { | ||
| 105 | switch (t) { | ||
| 106 | case VK_OBJECT_TYPE_BUFFER: return "buffer"; | ||
| 107 | case VK_OBJECT_TYPE_IMAGE: return "image"; | ||
| 108 | case VK_OBJECT_TYPE_DEVICE_MEMORY: return "memory"; | ||
| 109 | case VK_OBJECT_TYPE_ACCELERATION_STRUCTURE_KHR: return "acceleration structure"; | ||
| 110 | case VK_OBJECT_TYPE_COMMAND_BUFFER: return "command buffer"; | ||
| 111 | case VK_OBJECT_TYPE_PIPELINE: return "pipeline"; | ||
| 112 | case VK_OBJECT_TYPE_QUERY_POOL: return "query pool"; | ||
| 113 | case VK_OBJECT_TYPE_DESCRIPTOR_POOL: return "descriptor pool"; | ||
| 114 | default: return "object"; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | static std::string describe(const char* what, const Entry& e, std::uint64_t address) { | ||
| 119 | const std::uint64_t end = e.base + e.size; | ||
| 120 | const char* relation; | ||
| 121 | std::uint64_t distance; | ||
| 122 | if (address >= e.base && address < end) { | ||
| 123 | relation = "into"; | ||
| 124 | distance = address - e.base; | ||
| 125 | } else if (address >= end) { | ||
| 126 | relation = "past the end of"; | ||
| 127 | distance = address - end; | ||
| 128 | } else { | ||
| 129 | relation = "before"; | ||
| 130 | distance = e.base - address; | ||
| 131 | } | ||
| 132 | char line[320]; | ||
| 133 | std::snprintf(line, sizeof line, | ||
| 134 | " %s: %s%s 0x%" PRIx64 "..0x%" PRIx64 " (%" PRIu64 " KiB) handle 0x%" PRIx64 | ||
| 135 | " bound at %.1f s%s; the address is 0x%" PRIx64 " %s it\n", | ||
| 136 | what, type_name(e.type), e.internal ? " (driver-internal)" : "", e.base, end, e.size >> 10, | ||
| 137 | e.handle, e.bound_at, e.unbound_at ? (", unbound at " + std::to_string(e.unbound_at) + " s").c_str() : "", | ||
| 138 | distance, relation); | ||
| 139 | return line; | ||
| 140 | } | ||
| 141 | |||
| 142 | mutable std::mutex mutex_; | ||
| 143 | std::multimap<std::uint64_t, Entry> live_; // by base address | ||
| 144 | std::deque<Entry> unbound_; // the most recent last | ||
| 145 | std::uint64_t binds_ = 0, unbinds_ = 0; | ||
| 146 | }; | ||
| 147 | |||
| 148 | struct State { | ||
| 149 | std::mutex install; // the messenger fields below; the record has a lock of its own | ||
| 150 | Record record; | ||
| 151 | std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); | ||
| 152 | VkPhysicalDeviceAddressBindingReportFeaturesEXT feature{ | ||
| 153 | VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ADDRESS_BINDING_REPORT_FEATURES_EXT}; | ||
| 154 | VkDebugUtilsMessengerEXT messenger = VK_NULL_HANDLE; | ||
| 155 | VkInstance messenger_instance = VK_NULL_HANDLE; | ||
| 156 | PFN_vkDestroyDebugUtilsMessengerEXT destroy_messenger = nullptr; | ||
| 157 | // A device enabled the report and this process's messenger records it. Read at a device fault on whatever thread | ||
| 158 | // notices it, so atomic. | ||
| 159 | std::atomic<bool> on{false}; | ||
| 160 | }; | ||
| 161 | |||
| 162 | inline State& state() { | ||
| 163 | static State s; | ||
| 164 | return s; | ||
| 165 | } | ||
| 166 | |||
| 167 | inline bool requested() { | ||
| 168 | const char* v = std::getenv("LEONIDA_ADDRESS_BINDINGS"); | ||
| 169 | return v && v[0] == '1' && !v[1]; | ||
| 170 | } | ||
| 171 | |||
| 172 | inline VKAPI_ATTR VkBool32 VKAPI_CALL on_message(VkDebugUtilsMessageSeverityFlagBitsEXT, | ||
| 173 | VkDebugUtilsMessageTypeFlagsEXT types, | ||
| 174 | const VkDebugUtilsMessengerCallbackDataEXT* data, void*) { | ||
| 175 | if (!(types & VK_DEBUG_UTILS_MESSAGE_TYPE_DEVICE_ADDRESS_BINDING_BIT_EXT) || !data) return VK_FALSE; | ||
| 176 | const VkDeviceAddressBindingCallbackDataEXT* binding = nullptr; | ||
| 177 | for (auto* p = static_cast<const VkBaseInStructure*>(data->pNext); p; p = p->pNext) | ||
| 178 | if (p->sType == VK_STRUCTURE_TYPE_DEVICE_ADDRESS_BINDING_CALLBACK_DATA_EXT) | ||
| 179 | binding = reinterpret_cast<const VkDeviceAddressBindingCallbackDataEXT*>(p); | ||
| 180 | if (!binding) return VK_FALSE; | ||
| 181 | State& s = state(); | ||
| 182 | Entry e; | ||
| 183 | e.base = binding->baseAddress; | ||
| 184 | e.size = binding->size; | ||
| 185 | e.internal = (binding->flags & VK_DEVICE_ADDRESS_BINDING_INTERNAL_OBJECT_BIT_EXT) != 0; | ||
| 186 | if (data->objectCount && data->pObjects) { | ||
| 187 | e.type = data->pObjects[0].objectType; | ||
| 188 | e.handle = data->pObjects[0].objectHandle; | ||
| 189 | } | ||
| 190 | const double now = std::chrono::duration<double>(std::chrono::steady_clock::now() - s.start).count(); | ||
| 191 | s.record.record(e, binding->bindingType == VK_DEVICE_ADDRESS_BINDING_TYPE_BIND_EXT, now); | ||
| 192 | return VK_FALSE; | ||
| 193 | } | ||
| 194 | |||
| 195 | // Before vkCreateDevice: adds the extension and its feature to `device`'s chain when the device has them and | ||
| 196 | // `instance` has this process's messenger. Without the messenger nothing would record the reports, so the device is | ||
| 197 | // left as it is and a fault report says the bindings were not recorded. | ||
| 198 | inline bool enable_device(VkInstance instance, VkPhysicalDevice physical, PFN_vkGetPhysicalDeviceFeatures2 query, | ||
| 199 | std::span<const VkExtensionProperties> advertised, std::vector<const char*>& extensions, | ||
| 200 | VkDeviceCreateInfo& device) { | ||
| 201 | if (!requested() || !query) return false; | ||
| 202 | { | ||
| 203 | State& s = state(); | ||
| 204 | std::lock_guard lock(s.install); | ||
| 205 | if (!s.messenger || s.messenger_instance != instance) { | ||
| 206 | std::fprintf(stderr, "[rt] address-binding: no messenger on this device's instance; bindings are not " | ||
| 207 | "recorded\n"); | ||
| 208 | return false; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | const bool has = std::any_of(advertised.begin(), advertised.end(), [](const auto& e) { | ||
| 212 | return std::strcmp(e.extensionName, VK_EXT_DEVICE_ADDRESS_BINDING_REPORT_EXTENSION_NAME) == 0; | ||
| 213 | }); | ||
| 214 | State& s = state(); | ||
| 215 | if (!has) { | ||
| 216 | std::fprintf(stderr, "[rt] address-binding: the device has no VK_EXT_device_address_binding_report\n"); | ||
| 217 | return false; | ||
| 218 | } | ||
| 219 | s.feature.pNext = nullptr; | ||
| 220 | VkPhysicalDeviceFeatures2 features{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &s.feature}; | ||
| 221 | query(physical, &features); | ||
| 222 | if (!s.feature.reportAddressBinding) { | ||
| 223 | std::fprintf(stderr, "[rt] address-binding: reportAddressBinding is not supported\n"); | ||
| 224 | return false; | ||
| 225 | } | ||
| 226 | extensions.push_back(VK_EXT_DEVICE_ADDRESS_BINDING_REPORT_EXTENSION_NAME); | ||
| 227 | s.feature.pNext = const_cast<void*>(device.pNext); | ||
| 228 | device.pNext = &s.feature; | ||
| 229 | s.on = true; | ||
| 230 | return true; | ||
| 231 | } | ||
| 232 | |||
| 233 | // After vkCreateInstance with VK_EXT_debug_utils and before vkCreateDevice: bindings reach the instance's messengers | ||
| 234 | // as they happen. | ||
| 235 | inline void install_messenger(VkInstance instance, PFN_vkGetInstanceProcAddr get) { | ||
| 236 | if (!requested() || !instance || !get) return; | ||
| 237 | const auto create = | ||
| 238 | reinterpret_cast<PFN_vkCreateDebugUtilsMessengerEXT>(get(instance, "vkCreateDebugUtilsMessengerEXT")); | ||
| 239 | if (!create) { | ||
| 240 | std::fprintf(stderr, "[rt] address-binding: no vkCreateDebugUtilsMessengerEXT\n"); | ||
| 241 | return; | ||
| 242 | } | ||
| 243 | VkDebugUtilsMessengerCreateInfoEXT info{VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT}; | ||
| 244 | info.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT | | ||
| 245 | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT; | ||
| 246 | info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_DEVICE_ADDRESS_BINDING_BIT_EXT; | ||
| 247 | info.pfnUserCallback = &on_message; | ||
| 248 | State& s = state(); | ||
| 249 | std::lock_guard lock(s.install); | ||
| 250 | // One record per process: a second instance (a second device in one process) would mix its GPU addresses with | ||
| 251 | // the first's, and overwriting the handle would leave the first messenger alive past its instance. So the | ||
| 252 | // second one is refused, and its device is not enabled. | ||
| 253 | if (s.messenger) { | ||
| 254 | std::fprintf(stderr, "[rt] address-binding: a messenger is already installed for another instance; this " | ||
| 255 | "instance's bindings are not recorded\n"); | ||
| 256 | return; | ||
| 257 | } | ||
| 258 | VkDebugUtilsMessengerEXT messenger = VK_NULL_HANDLE; | ||
| 259 | const VkResult r = create(instance, &info, nullptr, &messenger); | ||
| 260 | if (r == VK_SUCCESS) { | ||
| 261 | s.messenger = messenger; | ||
| 262 | s.messenger_instance = instance; | ||
| 263 | s.destroy_messenger = reinterpret_cast<PFN_vkDestroyDebugUtilsMessengerEXT>( | ||
| 264 | get(instance, "vkDestroyDebugUtilsMessengerEXT")); | ||
| 265 | } | ||
| 266 | std::fprintf(stderr, "[rt] address-binding: messenger %s\n", r == VK_SUCCESS ? "installed" : "failed"); | ||
| 267 | } | ||
| 268 | |||
| 269 | // Before vkDestroyInstance: a messenger may not outlive its instance. What it recorded stays. | ||
| 270 | inline void uninstall_messenger(VkInstance instance) { | ||
| 271 | State& s = state(); | ||
| 272 | std::lock_guard lock(s.install); | ||
| 273 | if (!s.messenger || s.messenger_instance != instance) return; | ||
| 274 | if (s.destroy_messenger) s.destroy_messenger(instance, s.messenger, nullptr); | ||
| 275 | s.messenger = VK_NULL_HANDLE; | ||
| 276 | s.messenger_instance = VK_NULL_HANDLE; | ||
| 277 | s.destroy_messenger = nullptr; | ||
| 278 | s.on = false; | ||
| 279 | } | ||
| 280 | |||
| 281 | // At a device fault: what the driver had bound at and around `address`, on stderr. | ||
| 282 | inline void describe(std::uint64_t address, std::uint64_t precision) { | ||
| 283 | State& s = state(); | ||
| 284 | if (!s.on) { | ||
| 285 | // Asked for but not recording: say so, rather than leave a report that reads as "no object there". | ||
| 286 | if (requested()) | ||
| 287 | std::fprintf(stderr, "[rt] address-binding: 0x%" PRIx64 ": bindings were not recorded for this device (no " | ||
| 288 | "messenger or no VK_EXT_device_address_binding_report; see the address-binding " | ||
| 289 | "lines at startup)\n", address); | ||
| 290 | return; | ||
| 291 | } | ||
| 292 | const std::string text = s.record.report(address, precision); | ||
| 293 | std::size_t at = 0; | ||
| 294 | while (at < text.size()) { | ||
| 295 | const std::size_t end = text.find('\n', at); | ||
| 296 | std::fprintf(stderr, "[rt] address-binding: %.*s\n", int((end == std::string::npos ? text.size() : end) - at), | ||
| 297 | text.c_str() + at); | ||
| 298 | if (end == std::string::npos) break; | ||
| 299 | at = end + 1; | ||
| 300 | } | ||
| 301 | } | ||
| 302 | |||
| 303 | } // namespace leonida::address_binding | ||
diff --git a/runtime/src/vulkan_device_fault.h b/runtime/src/vulkan_device_fault.h index 3c49fce8d..9b55ef268 100644 --- a/runtime/src/vulkan_device_fault.h +++ b/runtime/src/vulkan_device_fault.h | |||
| @@ -1,6 +1,14 @@ | |||
| 1 | #pragma once | 1 | #pragma once |
| 2 | 2 | ||
| 3 | #include <vulkan/vulkan.h> | 3 | #include <vulkan/vulkan.h> |
| 4 | #ifdef _WIN32 | ||
| 5 | #ifndef NOMINMAX | ||
| 6 | #define NOMINMAX | ||
| 7 | #endif | ||
| 8 | #include <windows.h> // VirtualQuery: what the process has at a fault address | ||
| 9 | #endif | ||
| 10 | |||
| 11 | #include "address_binding.h" // LEONIDA_ADDRESS_BINDINGS: the bound object at a fault address | ||
| 4 | 12 | ||
| 5 | #include "compile_gate_policy.h" | 13 | #include "compile_gate_policy.h" |
| 6 | #include "vulkan_device_markers.h" | 14 | #include "vulkan_device_markers.h" |
| @@ -1578,6 +1586,19 @@ private: | |||
| 1578 | std::fprintf(stderr, "[rt] device-fault: address[%zu] type=%d address=0x%" PRIx64 | 1586 | std::fprintf(stderr, "[rt] device-fault: address[%zu] type=%d address=0x%" PRIx64 |
| 1579 | " precision=%" PRIu64 "\n", | 1587 | " precision=%" PRIu64 "\n", |
| 1580 | i, int(address.addressType), address.reportedAddress, address.addressPrecision); | 1588 | i, int(address.addressType), address.reportedAddress, address.addressPrecision); |
| 1589 | #ifdef _WIN32 | ||
| 1590 | // The page in this process at the same number. A GPU address is not a CPU one, but a host import, a title | ||
| 1591 | // range or a raw CPU pointer that reached the GPU shares it, so the region says which of those it can be. | ||
| 1592 | MEMORY_BASIC_INFORMATION page{}; | ||
| 1593 | if (VirtualQuery(reinterpret_cast<const void*>(static_cast<std::uintptr_t>(address.reportedAddress)), &page, | ||
| 1594 | sizeof page)) | ||
| 1595 | std::fprintf(stderr, "[rt] device-fault: address[%zu] in this process: base %p allocation %p size 0x%zx " | ||
| 1596 | "state 0x%lx protect 0x%lx type 0x%lx\n", | ||
| 1597 | i, page.BaseAddress, page.AllocationBase, page.RegionSize, | ||
| 1598 | static_cast<unsigned long>(page.State), static_cast<unsigned long>(page.Protect), | ||
| 1599 | static_cast<unsigned long>(page.Type)); | ||
| 1600 | #endif | ||
| 1601 | leonida::address_binding::describe(address.reportedAddress, address.addressPrecision); | ||
| 1581 | } | 1602 | } |
| 1582 | for (std::size_t i = 0; i < counts.vendorInfoCount; ++i) { | 1603 | for (std::size_t i = 0; i < counts.vendorInfoCount; ++i) { |
| 1583 | const auto& vendor = vendors[i]; | 1604 | const auto& vendor = vendors[i]; |
diff --git a/runtime/test/address_binding_test.cpp b/runtime/test/address_binding_test.cpp new file mode 100644 index 000000000..ebb87cde3 --- /dev/null +++ b/runtime/test/address_binding_test.cpp | |||
| @@ -0,0 +1,265 @@ | |||
| 1 | // src/address_binding.h: the record of VK_EXT_device_address_binding_report reports and what it says about a | ||
| 2 | // device-fault address. Synthetic reports only: no Vulkan device, no GPU. | ||
| 3 | #include "../src/address_binding.h" | ||
| 4 | |||
| 5 | #include <cstdint> | ||
| 6 | #include <cstdio> | ||
| 7 | #include <cstdlib> | ||
| 8 | #include <cstring> | ||
| 9 | #include <string> | ||
| 10 | #include <thread> | ||
| 11 | #include <vector> | ||
| 12 | |||
| 13 | #ifndef _WIN32 | ||
| 14 | static int _putenv_s(const char* name, const char* value) { return setenv(name, value, 1); } | ||
| 15 | #endif | ||
| 16 | |||
| 17 | #define CHECK(condition) do { \ | ||
| 18 | if (!(condition)) { \ | ||
| 19 | std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \ | ||
| 20 | return 1; \ | ||
| 21 | } \ | ||
| 22 | } while (0) | ||
| 23 | |||
| 24 | using namespace leonida::address_binding; | ||
| 25 | |||
| 26 | static bool has(const std::string& text, const char* part) { return text.find(part) != std::string::npos; } | ||
| 27 | |||
| 28 | static Entry entry(std::uint64_t base, std::uint64_t size, VkObjectType type, std::uint64_t handle) { | ||
| 29 | Entry e; | ||
| 30 | e.base = base; | ||
| 31 | e.size = size; | ||
| 32 | e.type = type; | ||
| 33 | e.handle = handle; | ||
| 34 | return e; | ||
| 35 | } | ||
| 36 | |||
| 37 | static int test_record() { | ||
| 38 | Record r; | ||
| 39 | r.record(entry(0x10000, 0x1cf000, VK_OBJECT_TYPE_IMAGE, 0xa), true, 1.0); // ends at 0x1df000 | ||
| 40 | r.record(entry(0x200000, 0x10000, VK_OBJECT_TYPE_BUFFER, 0xb), true, 2.0); | ||
| 41 | r.record(entry(0x400000, 0x1000, VK_OBJECT_TYPE_BUFFER, 0xc), true, 3.0); | ||
| 42 | r.record(entry(0x400000, 0x1000, VK_OBJECT_TYPE_BUFFER, 0xc), false, 4.0); // unbound again | ||
| 43 | |||
| 44 | // Inside the image: it contains the page. | ||
| 45 | std::string text = r.report(0x1ce000, 0x1000); | ||
| 46 | CHECK(has(text, "3 binds, 1 unbinds, 2 live")); | ||
| 47 | CHECK(has(text, "live, containing: image 0x10000..0x1df000")); | ||
| 48 | CHECK(has(text, "0x1be000 into it")); | ||
| 49 | |||
| 50 | // The page just past the image and below the buffer: no owner, the neighbours either side. | ||
| 51 | text = r.report(0x1e0123, 0x1000); | ||
| 52 | CHECK(!has(text, "containing")); | ||
| 53 | CHECK(has(text, "nearest below: image")); | ||
| 54 | CHECK(has(text, "0x1123 past the end of it")); // from the address, not its page | ||
| 55 | CHECK(has(text, "nearest above: buffer 0x200000")); | ||
| 56 | CHECK(has(text, "nothing live or recently unbound there")); | ||
| 57 | |||
| 58 | // Where the unbound buffer was: it held the page, and nothing live does. | ||
| 59 | text = r.report(0x400800, 0x1000); | ||
| 60 | CHECK(has(text, "unbound, held it: buffer 0x400000..0x401000")); | ||
| 61 | CHECK(has(text, "unbound at 4.0")); | ||
| 62 | CHECK(!has(text, "nothing live")); | ||
| 63 | |||
| 64 | // Far above everything: only the nearest below, which is the highest live object. | ||
| 65 | text = r.report(0x29b52a80000, 0x1000); | ||
| 66 | CHECK(has(text, "nearest below: buffer 0x200000")); | ||
| 67 | CHECK(!has(text, "nearest above")); | ||
| 68 | return 0; | ||
| 69 | } | ||
| 70 | |||
| 71 | // The edges of an object and of the reported page (review of RT-FAULT-ADDRESS-OWNER). | ||
| 72 | static int test_boundaries() { | ||
| 73 | Record r; | ||
| 74 | r.record(entry(0x10000, 0x1cf000, VK_OBJECT_TYPE_IMAGE, 0xa), true, 1.0); // ends at 0x1df000 | ||
| 75 | // The last byte of the image is in it. | ||
| 76 | std::string text = r.report(0x1defff, 0x1000); | ||
| 77 | CHECK(has(text, "live, containing: image")); | ||
| 78 | CHECK(has(text, "0x1cefff into it")); | ||
| 79 | // The byte right after its end is not, whatever the precision of a page that starts there. | ||
| 80 | text = r.report(0x1df000, 0x1000); | ||
| 81 | CHECK(!has(text, "live, containing") && !has(text, "on the page")); | ||
| 82 | CHECK(has(text, "nearest below: image") && has(text, "0x0 past the end of it")); | ||
| 83 | text = r.report(0x1df000, 1); | ||
| 84 | CHECK(!has(text, "live, containing") && has(text, "nearest below: image")); | ||
| 85 | // A coarse page that reaches back into the image: the image is on the page, but does not contain the address. | ||
| 86 | text = r.report(0x1df010, 0x2000); | ||
| 87 | CHECK(has(text, "live, on the page: image") && !has(text, "live, containing")); | ||
| 88 | CHECK(has(text, "0x10 past the end of it")); | ||
| 89 | CHECK(!has(text, "nothing live")); | ||
| 90 | // The same address at byte precision: no object there, only the neighbour. | ||
| 91 | text = r.report(0x1df010, 1); | ||
| 92 | CHECK(!has(text, "on the page") && has(text, "nearest below: image")); | ||
| 93 | CHECK(has(text, "nothing live or recently unbound there")); | ||
| 94 | // The first byte of the image, and the one before it. | ||
| 95 | CHECK(has(r.report(0x10000, 1), "live, containing: image")); | ||
| 96 | text = r.report(0xffff, 1); | ||
| 97 | CHECK(has(text, "nearest above: image") && has(text, "0x1 before it")); | ||
| 98 | // An unbound object that only shares the page is "held the page", not "held it". | ||
| 99 | r.record(entry(0x300000, 0x800, VK_OBJECT_TYPE_BUFFER, 0xd), true, 2.0); | ||
| 100 | r.record(entry(0x300000, 0x800, VK_OBJECT_TYPE_BUFFER, 0xd), false, 3.0); | ||
| 101 | CHECK(has(r.report(0x300900, 0x1000), "unbound, held the page: buffer 0x300000..0x300800")); | ||
| 102 | CHECK(has(r.report(0x300100, 0x1000), "unbound, held it: buffer 0x300000..0x300800")); | ||
| 103 | return 0; | ||
| 104 | } | ||
| 105 | |||
| 106 | // An unbind removes only the live entry with the same base, handle and size; the unbound list is capped. | ||
| 107 | static int test_unbind_matching_and_cap() { | ||
| 108 | Record r; | ||
| 109 | r.record(entry(0x100000, 0x1000, VK_OBJECT_TYPE_BUFFER, 0x1), true, 1.0); | ||
| 110 | r.record(entry(0x100000, 0x1000, VK_OBJECT_TYPE_BUFFER, 0x2), true, 1.0); // aliased: same base, other handle | ||
| 111 | r.record(entry(0x100000, 0x2000, VK_OBJECT_TYPE_BUFFER, 0x2), false, 2.0); // size differs: no live entry goes | ||
| 112 | CHECK(has(r.report(0x100000, 0x1000), "2 live")); | ||
| 113 | r.record(entry(0x100000, 0x1000, VK_OBJECT_TYPE_BUFFER, 0x3), false, 2.0); // handle differs: none goes | ||
| 114 | CHECK(has(r.report(0x100000, 0x1000), "2 live")); | ||
| 115 | r.record(entry(0x100000, 0x1000, VK_OBJECT_TYPE_BUFFER, 0x2), false, 3.0); // matches the second only | ||
| 116 | std::string text = r.report(0x100000, 0x1000); | ||
| 117 | CHECK(has(text, "1 live")); | ||
| 118 | CHECK(has(text, "live, containing: buffer 0x100000..0x101000 (4 KiB) handle 0x1")); | ||
| 119 | CHECK(!has(text, "live, containing: buffer 0x100000..0x101000 (4 KiB) handle 0x2")); | ||
| 120 | |||
| 121 | // Past kept_unbound, the oldest unbound objects are forgotten and the newest kept. | ||
| 122 | Record capped; | ||
| 123 | const std::uint64_t n = Record::kept_unbound + 8; | ||
| 124 | for (std::uint64_t i = 0; i < n; ++i) { | ||
| 125 | const auto e = entry(0x1000000 + i * 0x1000, 0x1000, VK_OBJECT_TYPE_BUFFER, 0x100 + i); | ||
| 126 | capped.record(e, true, 1.0); | ||
| 127 | capped.record(e, false, 2.0); | ||
| 128 | } | ||
| 129 | CHECK(has(capped.report(0x1000000, 0x1000), "nothing live or recently unbound there")); // the first, dropped | ||
| 130 | CHECK(has(capped.report(0x1000000 + 7 * 0x1000, 0x1000), "nothing live or recently unbound")); // the 8th | ||
| 131 | CHECK(has(capped.report(0x1000000 + 8 * 0x1000, 0x1000), "unbound, held it")); // the oldest one kept | ||
| 132 | CHECK(has(capped.report(0x1000000 + (n - 1) * 0x1000, 0x1000), "unbound, held it")); | ||
| 133 | return 0; | ||
| 134 | } | ||
| 135 | |||
| 136 | // The messenger's thread records while a device-fault report reads. | ||
| 137 | static int test_threads() { | ||
| 138 | Record r; | ||
| 139 | constexpr int threads = 4, per = 2000; | ||
| 140 | std::vector<std::thread> pool; | ||
| 141 | for (int t = 0; t < threads; ++t) | ||
| 142 | pool.emplace_back([&r, t] { | ||
| 143 | for (int i = 0; i < per; ++i) { | ||
| 144 | const auto e = entry(0x10000000ull * (t + 1) + std::uint64_t(i) * 0x1000, 0x1000, | ||
| 145 | VK_OBJECT_TYPE_BUFFER, std::uint64_t(t) << 32 | std::uint64_t(i)); | ||
| 146 | r.record(e, true, 1.0); | ||
| 147 | if (i % 2) r.record(e, false, 2.0); | ||
| 148 | } | ||
| 149 | }); | ||
| 150 | pool.emplace_back([&r] { | ||
| 151 | for (int i = 0; i < 200; ++i) (void)r.report(0x10000000ull + std::uint64_t(i) * 0x1000, 0x1000); | ||
| 152 | }); | ||
| 153 | for (auto& thread : pool) thread.join(); | ||
| 154 | char expect[96]; | ||
| 155 | std::snprintf(expect, sizeof expect, "%d binds, %d unbinds, %d live", threads * per, threads * per / 2, | ||
| 156 | threads * per / 2); | ||
| 157 | CHECK(has(r.report(0, 0x1000), expect)); | ||
| 158 | return 0; | ||
| 159 | } | ||
| 160 | |||
| 161 | static int test_messages() { | ||
| 162 | // Through the messenger callback, as the driver delivers them. | ||
| 163 | VkDeviceAddressBindingCallbackDataEXT binding{VK_STRUCTURE_TYPE_DEVICE_ADDRESS_BINDING_CALLBACK_DATA_EXT}; | ||
| 164 | binding.baseAddress = 0x800000; | ||
| 165 | binding.size = 0x2000; | ||
| 166 | binding.bindingType = VK_DEVICE_ADDRESS_BINDING_TYPE_BIND_EXT; | ||
| 167 | binding.flags = VK_DEVICE_ADDRESS_BINDING_INTERNAL_OBJECT_BIT_EXT; | ||
| 168 | VkDebugUtilsObjectNameInfoEXT object{VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT}; | ||
| 169 | object.objectType = VK_OBJECT_TYPE_DEVICE_MEMORY; | ||
| 170 | object.objectHandle = 0x77; | ||
| 171 | VkDebugUtilsMessengerCallbackDataEXT data{VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CALLBACK_DATA_EXT, &binding}; | ||
| 172 | data.objectCount = 1; | ||
| 173 | data.pObjects = &object; | ||
| 174 | // A message of another type is not a binding. | ||
| 175 | on_message(VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT, VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT, &data, nullptr); | ||
| 176 | CHECK(has(state().record.report(0x800000, 0x1000), "0 binds")); | ||
| 177 | on_message(VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT, VK_DEBUG_UTILS_MESSAGE_TYPE_DEVICE_ADDRESS_BINDING_BIT_EXT, | ||
| 178 | &data, nullptr); | ||
| 179 | const std::string text = state().record.report(0x801000, 0x1000); | ||
| 180 | CHECK(has(text, "1 binds")); | ||
| 181 | CHECK(has(text, "live, containing: memory (driver-internal) 0x800000..0x802000")); | ||
| 182 | CHECK(has(text, "handle 0x77")); | ||
| 183 | return 0; | ||
| 184 | } | ||
| 185 | |||
| 186 | static VKAPI_ATTR void VKAPI_CALL features(VkPhysicalDevice, VkPhysicalDeviceFeatures2* f) { | ||
| 187 | for (auto* p = static_cast<VkBaseOutStructure*>(f->pNext); p; p = p->pNext) | ||
| 188 | if (p->sType == VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ADDRESS_BINDING_REPORT_FEATURES_EXT) | ||
| 189 | reinterpret_cast<VkPhysicalDeviceAddressBindingReportFeaturesEXT*>(p)->reportAddressBinding = VK_TRUE; | ||
| 190 | } | ||
| 191 | |||
| 192 | // A fake debug-utils instance: messengers are numbered as they are made and counted as they are destroyed. | ||
| 193 | static int created_messengers = 0, destroyed_messengers = 0; | ||
| 194 | |||
| 195 | static VKAPI_ATTR VkResult VKAPI_CALL fake_create(VkInstance, const VkDebugUtilsMessengerCreateInfoEXT*, | ||
| 196 | const VkAllocationCallbacks*, VkDebugUtilsMessengerEXT* out) { | ||
| 197 | *out = reinterpret_cast<VkDebugUtilsMessengerEXT>(static_cast<std::uintptr_t>(++created_messengers)); | ||
| 198 | return VK_SUCCESS; | ||
| 199 | } | ||
| 200 | |||
| 201 | static VKAPI_ATTR void VKAPI_CALL fake_destroy(VkInstance, VkDebugUtilsMessengerEXT, const VkAllocationCallbacks*) { | ||
| 202 | ++destroyed_messengers; | ||
| 203 | } | ||
| 204 | |||
| 205 | static VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL fake_get(VkInstance, const char* name) { | ||
| 206 | if (std::strcmp(name, "vkCreateDebugUtilsMessengerEXT") == 0) return reinterpret_cast<PFN_vkVoidFunction>(&fake_create); | ||
| 207 | if (std::strcmp(name, "vkDestroyDebugUtilsMessengerEXT") == 0) | ||
| 208 | return reinterpret_cast<PFN_vkVoidFunction>(&fake_destroy); | ||
| 209 | return nullptr; | ||
| 210 | } | ||
| 211 | |||
| 212 | static VkInstance fake_instance(std::uintptr_t n) { return reinterpret_cast<VkInstance>(n); } | ||
| 213 | |||
| 214 | static int test_enable() { | ||
| 215 | VkExtensionProperties ext{}; | ||
| 216 | std::snprintf(ext.extensionName, sizeof ext.extensionName, "%s", VK_EXT_DEVICE_ADDRESS_BINDING_REPORT_EXTENSION_NAME); | ||
| 217 | std::vector<const char*> extensions; | ||
| 218 | VkDeviceCreateInfo device{VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO}; | ||
| 219 | // Not asked for: nothing changes. | ||
| 220 | _putenv_s("LEONIDA_ADDRESS_BINDINGS", ""); | ||
| 221 | CHECK(!enable_device(VK_NULL_HANDLE, VK_NULL_HANDLE, &features, {&ext, 1}, extensions, device)); | ||
| 222 | CHECK(extensions.empty() && !device.pNext); | ||
| 223 | _putenv_s("LEONIDA_ADDRESS_BINDINGS", "1"); | ||
| 224 | const VkInstance first = fake_instance(0x1000), second = fake_instance(0x2000); | ||
| 225 | // Asked for, but no messenger records the reports: the device is left as it is, and a fault says so. | ||
| 226 | CHECK(!enable_device(first, VK_NULL_HANDLE, &features, {&ext, 1}, extensions, device)); | ||
| 227 | CHECK(extensions.empty() && !device.pNext && !state().on); | ||
| 228 | install_messenger(first, &fake_get); | ||
| 229 | CHECK(created_messengers == 1 && state().messenger_instance == first); | ||
| 230 | // A second instance in the same process: refused, so the first messenger is neither overwritten nor leaked. | ||
| 231 | install_messenger(second, &fake_get); | ||
| 232 | CHECK(created_messengers == 1 && state().messenger_instance == first); | ||
| 233 | CHECK(!enable_device(second, VK_NULL_HANDLE, &features, {&ext, 1}, extensions, device)); | ||
| 234 | CHECK(extensions.empty() && !device.pNext && !state().on); | ||
| 235 | uninstall_messenger(second); // not its messenger: nothing happens | ||
| 236 | CHECK(destroyed_messengers == 0 && state().messenger); | ||
| 237 | // Asked for, the instance has the messenger and the device has the extension: it goes on the chain. | ||
| 238 | CHECK(enable_device(first, VK_NULL_HANDLE, &features, {&ext, 1}, extensions, device)); | ||
| 239 | CHECK(extensions.size() == 1 && device.pNext == &state().feature && state().feature.reportAddressBinding); | ||
| 240 | CHECK(state().on); | ||
| 241 | // Asked for, and the device does not list it: refused. | ||
| 242 | std::vector<const char*> none; | ||
| 243 | VkDeviceCreateInfo other{VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO}; | ||
| 244 | CHECK(!enable_device(first, VK_NULL_HANDLE, &features, {}, none, other)); | ||
| 245 | CHECK(none.empty() && !other.pNext); | ||
| 246 | // Teardown of the first instance destroys its messenger; the second instance can then have one. | ||
| 247 | uninstall_messenger(first); | ||
| 248 | CHECK(destroyed_messengers == 1 && !state().messenger && !state().on); | ||
| 249 | install_messenger(second, &fake_get); | ||
| 250 | CHECK(created_messengers == 2 && state().messenger_instance == second); | ||
| 251 | uninstall_messenger(second); | ||
| 252 | CHECK(destroyed_messengers == 2); | ||
| 253 | return 0; | ||
| 254 | } | ||
| 255 | |||
| 256 | int main() { | ||
| 257 | if (const int r = test_record()) return r; | ||
| 258 | if (const int r = test_boundaries()) return r; | ||
| 259 | if (const int r = test_unbind_matching_and_cap()) return r; | ||
| 260 | if (const int r = test_threads()) return r; | ||
| 261 | if (const int r = test_messages()) return r; | ||
| 262 | if (const int r = test_enable()) return r; | ||
| 263 | std::printf("address_binding: containing, neighbouring and unbound objects at a fault address\n"); | ||
| 264 | return 0; | ||
| 265 | } | ||