upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/vk_queue_query_sync_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/test/vk_queue_query_sync_test.cpp')
-rw-r--r--runtime/test/vk_queue_query_sync_test.cpp118
1 files changed, 118 insertions, 0 deletions
diff --git a/runtime/test/vk_queue_query_sync_test.cpp b/runtime/test/vk_queue_query_sync_test.cpp
index a03e369b9..9aecd81cb 100644
--- a/runtime/test/vk_queue_query_sync_test.cpp
+++ b/runtime/test/vk_queue_query_sync_test.cpp
@@ -29,10 +29,14 @@ namespace {
29namespace driver { 29namespace driver {
30PFN_vkGetDeviceProcAddr real_get_device = nullptr; 30PFN_vkGetDeviceProcAddr real_get_device = nullptr;
31PFN_vkQueueSubmit real_submit = nullptr; 31PFN_vkQueueSubmit real_submit = nullptr;
32PFN_vkGetSemaphoreCounterValue real_counter = nullptr;
32PFN_vkCmdCopyQueryPoolResults real_copy_queries = nullptr; 33PFN_vkCmdCopyQueryPoolResults real_copy_queries = nullptr;
33PFN_vkCmdFillBuffer real_fill_buffer = nullptr; 34PFN_vkCmdFillBuffer real_fill_buffer = nullptr;
34 35
35std::atomic<VkResult> fail_next_submit{VK_SUCCESS}; 36std::atomic<VkResult> fail_next_submit{VK_SUCCESS};
37std::atomic<VkResult> fail_next_counter{VK_SUCCESS};
38std::atomic<unsigned> injected_counter_errors{0};
39std::thread::id counter_test_thread;
36std::atomic<bool> capture_next_waits{false}; 40std::atomic<bool> capture_next_waits{false};
37std::atomic<std::uint32_t> captured_wait_count{0}; 41std::atomic<std::uint32_t> captured_wait_count{0};
38std::atomic<std::uintptr_t> captured_wait_sem{0}; 42std::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,
70 return real_submit(queue, count, submits, fence); 74 return real_submit(queue, count, submits, fence);
71} 75}
72 76
77VKAPI_ATTR VkResult VKAPI_CALL get_counter(VkDevice device, VkSemaphore semaphore, std::uint64_t* value) {
78 if (std::this_thread::get_id() == counter_test_thread) {
79 const VkResult result = fail_next_counter.exchange(VK_SUCCESS);
80 if (result != VK_SUCCESS) {
81 *value = 999; // failure output is undefined, so make accidental reuse detectable
82 injected_counter_errors.fetch_add(1);
83 return result;
84 }
85 }
86 return real_counter(device, semaphore, value);
87}
88
73VKAPI_ATTR void VKAPI_CALL cmd_copy_query_pool_results(VkCommandBuffer cb, VkQueryPool pool, std::uint32_t first, 89VKAPI_ATTR void VKAPI_CALL cmd_copy_query_pool_results(VkCommandBuffer cb, VkQueryPool pool, std::uint32_t first,
74 std::uint32_t count, VkBuffer dst, VkDeviceSize offset, 90 std::uint32_t count, VkBuffer dst, VkDeviceSize offset,
75 VkDeviceSize stride, VkQueryResultFlags flags) { 91 VkDeviceSize stride, VkQueryResultFlags flags) {
@@ -86,6 +102,7 @@ VKAPI_ATTR void VKAPI_CALL cmd_fill_buffer(VkCommandBuffer cb, VkBuffer dst, VkD
86VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_device(VkDevice device, const char* name) { 102VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_device(VkDevice device, const char* name) {
87 const std::string_view n = name; 103 const std::string_view n = name;
88 if (n == "vkQueueSubmit") return reinterpret_cast<PFN_vkVoidFunction>(queue_submit); 104 if (n == "vkQueueSubmit") return reinterpret_cast<PFN_vkVoidFunction>(queue_submit);
105 if (n == "vkGetSemaphoreCounterValue") return reinterpret_cast<PFN_vkVoidFunction>(get_counter);
89 if (n == "vkCmdCopyQueryPoolResults") return reinterpret_cast<PFN_vkVoidFunction>(cmd_copy_query_pool_results); 106 if (n == "vkCmdCopyQueryPoolResults") return reinterpret_cast<PFN_vkVoidFunction>(cmd_copy_query_pool_results);
90 if (n == "vkCmdFillBuffer") return reinterpret_cast<PFN_vkVoidFunction>(cmd_fill_buffer); 107 if (n == "vkCmdFillBuffer") return reinterpret_cast<PFN_vkVoidFunction>(cmd_fill_buffer);
91 return real_get_device(device, name); 108 return real_get_device(device, name);
@@ -94,6 +111,8 @@ VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL get_device(VkDevice device, const char*
94void install(PFN_vkGetDeviceProcAddr device_proc, VkDevice device) { 111void install(PFN_vkGetDeviceProcAddr device_proc, VkDevice device) {
95 real_get_device = device_proc; 112 real_get_device = device_proc;
96 real_submit = reinterpret_cast<PFN_vkQueueSubmit>(device_proc(device, "vkQueueSubmit")); 113 real_submit = reinterpret_cast<PFN_vkQueueSubmit>(device_proc(device, "vkQueueSubmit"));
114 real_counter = reinterpret_cast<PFN_vkGetSemaphoreCounterValue>(
115 device_proc(device, "vkGetSemaphoreCounterValue"));
97 real_copy_queries = 116 real_copy_queries =
98 reinterpret_cast<PFN_vkCmdCopyQueryPoolResults>(device_proc(device, "vkCmdCopyQueryPoolResults")); 117 reinterpret_cast<PFN_vkCmdCopyQueryPoolResults>(device_proc(device, "vkCmdCopyQueryPoolResults"));
99 real_fill_buffer = reinterpret_cast<PFN_vkCmdFillBuffer>(device_proc(device, "vkCmdFillBuffer")); 118 real_fill_buffer = reinterpret_cast<PFN_vkCmdFillBuffer>(device_proc(device, "vkCmdFillBuffer"));
@@ -111,6 +130,7 @@ int main() {
111 return 77; 130 return 77;
112 } 131 }
113 driver::install(d.device_proc, d.device); 132 driver::install(d.device_proc, d.device);
133 driver::counter_test_thread = std::this_thread::get_id();
114 134
115 const auto create_query_pool = d.fn<PFN_vkCreateQueryPool>("vkCreateQueryPool"); 135 const auto create_query_pool = d.fn<PFN_vkCreateQueryPool>("vkCreateQueryPool");
116 const auto destroy_query_pool = d.fn<PFN_vkDestroyQueryPool>("vkDestroyQueryPool"); 136 const auto destroy_query_pool = d.fn<PFN_vkDestroyQueryPool>("vkDestroyQueryPool");
@@ -129,6 +149,7 @@ int main() {
129 const VkQueryPool pool_race = make_occlusion_pool(2); 149 const VkQueryPool pool_race = make_occlusion_pool(2);
130 const VkQueryPool pool_stress = make_occlusion_pool(32); 150 const VkQueryPool pool_stress = make_occlusion_pool(32);
131 EXPECT(pool_uaf && pool_race && pool_stress); 151 EXPECT(pool_uaf && pool_race && pool_stress);
152 bool progress_loss_ok = true; // deferred verdict: release Vulkan query pools even when main is expected-red
132 153
133 { 154 {
134 VulkanContext context(driver::get_device, d.device, true); 155 VulkanContext context(driver::get_device, d.device, true);
@@ -291,6 +312,73 @@ int main() {
291 std::printf("queue/query sync: pending Wait() preserved across failed vkQueueSubmit ok\n"); 312 std::printf("queue/query sync: pending Wait() preserved across failed vkQueueSubmit ok\n");
292 } 313 }
293 314
315 // 3b. The same for a batch held behind a Wait with no submitted Signal (queue_holds.h): its submission
316 // fails when the CPU Signal releases it, the failure is reported through take_held_failure(), and its
317 // waits stay for the next submission, as when the call is not held. A Signal queued behind the failed
318 // batch still runs.
319 {
320 VulkanFence gate(context, 0), after(context, 0);
321 EXPECT(gate.semaphore() != VK_NULL_HANDLE && after.semaphore() != VK_NULL_HANDLE);
322 const std::uint64_t pattern = 0x0badf00dcafe1234ULL;
323 std::memcpy(src_ptr, &pattern, sizeof(pattern));
324 std::memset(dst_ptr, 0, sizeof(pattern));
325 CommandList copy_list;
326 EXPECT(copy_list.copy_buffer({2, 0, 1, 0, sizeof(pattern)}));
327 EXPECT(copy_list.close());
328 const CommandList* ptr = &copy_list;
329
330 queue_a.wait(gate.semaphore(), 1);
331 EXPECT(queue_a.execute({&ptr, 1}) == ReplayResult::success); // held: the Signal is not submitted yet
332 EXPECT(queue_a.signal(after.semaphore(), 1)); // held behind it
333 EXPECT(after.completed() == 0 && queue_a.take_held_failure() == ReplayResult::success);
334 driver::fail_next_submit.store(VK_ERROR_OUT_OF_HOST_MEMORY);
335 EXPECT(gate.signal(1)); // releases the batch on this thread
336 EXPECT(queue_a.take_held_failure() == ReplayResult::unsupported);
337 EXPECT(queue_a.take_held_failure() == ReplayResult::success); // reported once
338 EXPECT(queue_a.idle() && after.completed() == 1); // the Signal behind it still signaled
339
340 driver::captured_wait_count.store(0);
341 driver::captured_wait_sem.store(0);
342 driver::captured_wait_val.store(0);
343 driver::capture_next_waits.store(true);
344 EXPECT(queue_a.execute({&ptr, 1}) == ReplayResult::success);
345 EXPECT(queue_a.idle());
346 EXPECT(driver::captured_wait_count.load() == 1); // the failed batch's wait on (gate, 1) came along
347 EXPECT(driver::captured_wait_sem.load() == reinterpret_cast<std::uintptr_t>(gate.semaphore()));
348 EXPECT(driver::captured_wait_val.load() == 1);
349 std::uint64_t got = 0;
350 std::memcpy(&got, dst_ptr, sizeof(got));
351 EXPECT(got == pattern);
352 std::printf("queue/query sync: failed held batch reported, its wait kept for the retry, Signal behind it ran\n");
353 }
354
355 // 3c. A Signal whose vkQueueSubmit fails signals nothing: a queue waiting for it stays held (the value is not
356 // counted as submitted), and the application's retry of the Signal goes through and releases it.
357 {
358 VulkanFence fence(context, 0);
359 EXPECT(fence.semaphore() != VK_NULL_HANDLE);
360 const std::uint64_t pattern = 0x5ca1ab1e0ddba11ULL;
361 std::memcpy(src_ptr, &pattern, sizeof(pattern));
362 std::memset(dst_ptr, 0, sizeof(pattern));
363 CommandList copy_list;
364 EXPECT(copy_list.copy_buffer({2, 0, 1, 0, sizeof(pattern)}));
365 EXPECT(copy_list.close());
366 const CommandList* ptr = &copy_list;
367 queue_b.wait(fence.semaphore(), 1);
368 EXPECT(queue_b.execute({&ptr, 1}) == ReplayResult::success); // held for the Signal
369 driver::fail_next_submit.store(VK_ERROR_OUT_OF_HOST_MEMORY);
370 EXPECT(!queue_a.signal(fence.semaphore(), 1)); // the submit fails
371 EXPECT(queue_b.idle() && fence.completed() == 0);
372 std::uint64_t got = 1;
373 std::memcpy(&got, dst_ptr, sizeof(got));
374 EXPECT(got == 0); // the waiter did not run
375 EXPECT(queue_a.signal(fence.semaphore(), 1)); // the retry signals, and releases it
376 EXPECT(queue_b.idle() && queue_a.idle() && fence.completed() == 1);
377 std::memcpy(&got, dst_ptr, sizeof(got));
378 EXPECT(got == pattern);
379 std::printf("queue/query sync: a failed Signal does not release its waiters; the retry does\n");
380 }
381
294 // 4. Concurrent multi-queue EndQuery + ResolveQueries + temporary query pool add/remove stress. 382 // 4. Concurrent multi-queue EndQuery + ResolveQueries + temporary query pool add/remove stress.
295 // Each iteration uses a distinct query slot so no slot is reset while another submission resolves it. 383 // Each iteration uses a distinct query slot so no slot is reset while another submission resolves it.
296 { 384 {
@@ -341,6 +429,35 @@ int main() {
341 context.remove_query_pool(main_heap); 429 context.remove_query_pool(main_heap);
342 std::printf("queue/query sync: concurrent multi-queue query stress ok\n"); 430 std::printf("queue/query sync: concurrent multi-queue query stress ok\n");
343 } 431 }
432
433 // 5. A progress-semaphore query in acquire() can be the first Vulkan operation to discover
434 // device loss. A transient query failure must not poison a healthy queue, while DEVICE_LOST
435 // must prevent acquiring/recycling and submitting new command buffers.
436 {
437 EXPECT(!context.is_device_lost());
438 CommandList empty;
439 EXPECT(empty.close());
440 const CommandList* ptr = &empty;
441 EXPECT(queue_a.idle() && queue_b.idle());
442 (void)context.completed_retire_ticket(); // drain old submits before faulting queue's own progress query
443 driver::fail_next_counter.store(VK_ERROR_OUT_OF_HOST_MEMORY);
444 const ReplayResult transient = queue_a.execute({&ptr, 1});
445 const bool transient_ok = transient == ReplayResult::success && !context.is_device_lost() &&
446 driver::injected_counter_errors.load() == 1;
447
448 EXPECT(queue_a.idle());
449 (void)context.completed_retire_ticket();
450 driver::fail_next_counter.store(VK_ERROR_DEVICE_LOST);
451 const ReplayResult after_loss = queue_a.execute({&ptr, 1});
452 const bool lost = context.is_device_lost();
453 std::printf("queue/query sync: injected nonfatal execute=%d, device-loss execute=%d, "
454 "context_lost=%d, interceptions=%u\n", int(transient), int(after_loss), int(lost),
455 driver::injected_counter_errors.load());
456 progress_loss_ok = transient_ok && after_loss == ReplayResult::unsupported && lost &&
457 driver::injected_counter_errors.load() == 2;
458 std::printf("queue/query sync: progress counter device loss %s\n",
459 progress_loss_ok ? "marks context and rejects work" : "NOT propagated");
460 }
344 } 461 }
345 462
346 destroy_query_pool(d.device, pool_uaf, nullptr); 463 destroy_query_pool(d.device, pool_uaf, nullptr);
@@ -348,6 +465,7 @@ int main() {
348 destroy_query_pool(d.device, pool_stress, nullptr); 465 destroy_query_pool(d.device, pool_stress, nullptr);
349 466
350 EXPECT(d.validation_errors() == 0); 467 EXPECT(d.validation_errors() == 0);
468 EXPECT(progress_loss_ok);
351 std::printf("vk_queue_query_sync_test: ok\n"); 469 std::printf("vk_queue_query_sync_test: ok\n");
352 return 0; 470 return 0;
353} 471}