Runtime: propagate queue progress counter device loss

This commit is contained in:
pmilewsskiedu-gif
2026-11-22 20:51:10 -05:00
parent 03b09a0865
commit 00832c18cd
2 changed files with 61 additions and 3 deletions

View File

@ -2198,9 +2198,12 @@ private:
}
std::uint64_t completed() const {
if (context_.is_device_lost()) return 0;
std::uint64_t done = 0;
context_.vk.vkGetSemaphoreCounterValue(context_.device(), progress_, &done);
return done;
const VkResult result = context_.vk.vkGetSemaphoreCounterValue(context_.device(), progress_, &done);
if (result == VK_ERROR_DEVICE_LOST) context_.mark_device_lost();
// A failed timeline query cannot be used as a resource-retirement or command-buffer reuse ticket.
return result == VK_SUCCESS ? done : 0;
}
void collect_bindings(std::uint64_t done) {
@ -2213,6 +2216,7 @@ private:
if (context_.is_device_lost()) return nullptr;
context_.drain_deleted_resources(context_.completed_retire_ticket());
const auto done = completed();
if (context_.is_device_lost()) return nullptr;
collect_bindings(done);
CommandSlot* reuse = nullptr;
for (auto& slot : buffers_) {
@ -2259,7 +2263,10 @@ private:
bool submit(VkCommandBuffer command_buffer, VkSemaphore fence, std::uint64_t value) {
if (context_.is_device_lost()) return false;
const std::uint64_t ticket = submitted_ + 1;
if (last_sparse_) collect_bindings(completed());
if (last_sparse_) {
collect_bindings(completed());
if (context_.is_device_lost()) return false;
}
std::span<const VkSemaphore> semaphores = waits_;
std::span<const std::uint64_t> values = wait_values_;
std::vector<VkSemaphore> sparse_waits;

View File

@ -29,10 +29,14 @@ namespace {
namespace driver {
PFN_vkGetDeviceProcAddr real_get_device = nullptr;
PFN_vkQueueSubmit real_submit = nullptr;
PFN_vkGetSemaphoreCounterValue real_counter = nullptr;
PFN_vkCmdCopyQueryPoolResults real_copy_queries = nullptr;
PFN_vkCmdFillBuffer real_fill_buffer = nullptr;
std::atomic<VkResult> fail_next_submit{VK_SUCCESS};
std::atomic<VkResult> fail_next_counter{VK_SUCCESS};
std::atomic<unsigned> injected_counter_errors{0};
std::thread::id counter_test_thread;
std::atomic<bool> capture_next_waits{false};
std::atomic<std::uint32_t> captured_wait_count{0};
std::atomic<std::uintptr_t> captured_wait_sem{0};
@ -70,6 +74,18 @@ VKAPI_ATTR VkResult VKAPI_CALL queue_submit(VkQueue queue, std::uint32_t count,
return real_submit(queue, count, submits, fence);
}
VKAPI_ATTR VkResult VKAPI_CALL get_counter(VkDevice device, VkSemaphore semaphore, std::uint64_t* value) {
if (std::this_thread::get_id() == counter_test_thread) {
const VkResult result = fail_next_counter.exchange(VK_SUCCESS);
if (result != VK_SUCCESS) {
*value = 999; // failure output is undefined, so make accidental reuse detectable
injected_counter_errors.fetch_add(1);
return result;
}
}
return real_counter(device, semaphore, value);
}
VKAPI_ATTR void VKAPI_CALL cmd_copy_query_pool_results(VkCommandBuffer cb, VkQueryPool pool, std::uint32_t first,
std::uint32_t count, VkBuffer dst, VkDeviceSize offset,
VkDeviceSize stride, VkQueryResultFlags flags) {
@ -86,6 +102,7 @@ VKAPI_ATTR void VKAPI_CALL cmd_fill_buffer(VkCommandBuffer cb, VkBuffer dst, VkD
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_device(VkDevice device, const char* name) {
const std::string_view n = name;
if (n == "vkQueueSubmit") return reinterpret_cast<PFN_vkVoidFunction>(queue_submit);
if (n == "vkGetSemaphoreCounterValue") return reinterpret_cast<PFN_vkVoidFunction>(get_counter);
if (n == "vkCmdCopyQueryPoolResults") return reinterpret_cast<PFN_vkVoidFunction>(cmd_copy_query_pool_results);
if (n == "vkCmdFillBuffer") return reinterpret_cast<PFN_vkVoidFunction>(cmd_fill_buffer);
return real_get_device(device, name);
@ -94,6 +111,8 @@ VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_device(VkDevice device, const char*
void install(PFN_vkGetDeviceProcAddr device_proc, VkDevice device) {
real_get_device = device_proc;
real_submit = reinterpret_cast<PFN_vkQueueSubmit>(device_proc(device, "vkQueueSubmit"));
real_counter = reinterpret_cast<PFN_vkGetSemaphoreCounterValue>(
device_proc(device, "vkGetSemaphoreCounterValue"));
real_copy_queries =
reinterpret_cast<PFN_vkCmdCopyQueryPoolResults>(device_proc(device, "vkCmdCopyQueryPoolResults"));
real_fill_buffer = reinterpret_cast<PFN_vkCmdFillBuffer>(device_proc(device, "vkCmdFillBuffer"));
@ -111,6 +130,7 @@ int main() {
return 77;
}
driver::install(d.device_proc, d.device);
driver::counter_test_thread = std::this_thread::get_id();
const auto create_query_pool = d.fn<PFN_vkCreateQueryPool>("vkCreateQueryPool");
const auto destroy_query_pool = d.fn<PFN_vkDestroyQueryPool>("vkDestroyQueryPool");
@ -129,6 +149,7 @@ int main() {
const VkQueryPool pool_race = make_occlusion_pool(2);
const VkQueryPool pool_stress = make_occlusion_pool(32);
EXPECT(pool_uaf && pool_race && pool_stress);
bool progress_loss_ok = true; // deferred verdict: release Vulkan query pools even when main is expected-red
{
VulkanContext context(driver::get_device, d.device, true);
@ -341,6 +362,35 @@ int main() {
context.remove_query_pool(main_heap);
std::printf("queue/query sync: concurrent multi-queue query stress ok\n");
}
// 5. A progress-semaphore query in acquire() can be the first Vulkan operation to discover
// device loss. A transient query failure must not poison a healthy queue, while DEVICE_LOST
// must prevent acquiring/recycling and submitting new command buffers.
{
EXPECT(!context.is_device_lost());
CommandList empty;
EXPECT(empty.close());
const CommandList* ptr = &empty;
EXPECT(queue_a.idle() && queue_b.idle());
(void)context.completed_retire_ticket(); // drain old submits before faulting queue's own progress query
driver::fail_next_counter.store(VK_ERROR_OUT_OF_HOST_MEMORY);
const ReplayResult transient = queue_a.execute({&ptr, 1});
const bool transient_ok = transient == ReplayResult::success && !context.is_device_lost() &&
driver::injected_counter_errors.load() == 1;
EXPECT(queue_a.idle());
(void)context.completed_retire_ticket();
driver::fail_next_counter.store(VK_ERROR_DEVICE_LOST);
const ReplayResult after_loss = queue_a.execute({&ptr, 1});
const bool lost = context.is_device_lost();
std::printf("queue/query sync: injected nonfatal execute=%d, device-loss execute=%d, "
"context_lost=%d, interceptions=%u\n", int(transient), int(after_loss), int(lost),
driver::injected_counter_errors.load());
progress_loss_ok = transient_ok && after_loss == ReplayResult::unsupported && lost &&
driver::injected_counter_errors.load() == 2;
std::printf("queue/query sync: progress counter device loss %s\n",
progress_loss_ok ? "marks context and rejects work" : "NOT propagated");
}
}
destroy_query_pool(d.device, pool_uaf, nullptr);
@ -348,6 +398,7 @@ int main() {
destroy_query_pool(d.device, pool_stress, nullptr);
EXPECT(d.validation_errors() == 0);
EXPECT(progress_loss_ok);
std::printf("vk_queue_query_sync_test: ok\n");
return 0;
}