diff options
Diffstat (limited to 'runtime/test/vk_queue_constructor_oom_test.cpp')
| -rw-r--r-- | runtime/test/vk_queue_constructor_oom_test.cpp | 217 |
1 files changed, 217 insertions, 0 deletions
diff --git a/runtime/test/vk_queue_constructor_oom_test.cpp b/runtime/test/vk_queue_constructor_oom_test.cpp new file mode 100644 index 000000000..dbd5d0a11 --- /dev/null +++ b/runtime/test/vk_queue_constructor_oom_test.cpp | |||
| @@ -0,0 +1,217 @@ | |||
| 1 | // A failed QueueHolds registration must release VulkanQueue's command pool and timeline semaphore. | ||
| 2 | // Public runtime classes with fake Vulkan procedures: no loader, GPU, or game data. | ||
| 3 | #include "../src/vulkan_backend.h" | ||
| 4 | |||
| 5 | #include <array> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <cstdint> | ||
| 8 | #include <cstdio> | ||
| 9 | #include <cstdlib> | ||
| 10 | #include <new> | ||
| 11 | #include <type_traits> | ||
| 12 | |||
| 13 | namespace { | ||
| 14 | bool fail_next_new = false; | ||
| 15 | bool arm_after_semaphore = false; | ||
| 16 | bool fail_pool = false; | ||
| 17 | bool fail_semaphore = false; | ||
| 18 | int failures = 0; | ||
| 19 | std::uintptr_t next_handle = 100; | ||
| 20 | |||
| 21 | template<typename Handle> Handle handle() { | ||
| 22 | const auto value = ++next_handle; | ||
| 23 | if constexpr (std::is_pointer_v<Handle>) return reinterpret_cast<Handle>(value); | ||
| 24 | else return static_cast<Handle>(value); | ||
| 25 | } | ||
| 26 | template<typename Handle> std::uintptr_t number(Handle value) { | ||
| 27 | if constexpr (std::is_pointer_v<Handle>) return reinterpret_cast<std::uintptr_t>(value); | ||
| 28 | else return static_cast<std::uintptr_t>(value); | ||
| 29 | } | ||
| 30 | |||
| 31 | struct Handles { | ||
| 32 | std::array<std::uintptr_t, 16> live{}; | ||
| 33 | int created = 0; | ||
| 34 | int destroyed = 0; | ||
| 35 | int invalid_destroy = 0; | ||
| 36 | void add(std::uintptr_t value) { live[created++] = value; } | ||
| 37 | void remove(std::uintptr_t value) { | ||
| 38 | for (auto& item : live) { | ||
| 39 | if (item == value) { | ||
| 40 | item = 0; | ||
| 41 | ++destroyed; | ||
| 42 | return; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | ++invalid_destroy; | ||
| 46 | } | ||
| 47 | bool exact(int made, int freed) const { | ||
| 48 | return created == made && destroyed == freed && invalid_destroy == 0; | ||
| 49 | } | ||
| 50 | }; | ||
| 51 | Handles pools, semaphores; | ||
| 52 | |||
| 53 | void expect(bool ok, const char* what) { | ||
| 54 | if (!ok) { | ||
| 55 | std::fprintf(stderr, "FAIL: %s\n", what); | ||
| 56 | ++failures; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | PFN_vkVoidFunction VKAPI_CALL no_proc(VkDevice, const char*) { return nullptr; } | ||
| 61 | VkResult VKAPI_CALL create_pool(VkDevice, const VkCommandPoolCreateInfo*, const VkAllocationCallbacks*, | ||
| 62 | VkCommandPool* out) { | ||
| 63 | *out = VK_NULL_HANDLE; | ||
| 64 | if (fail_pool) return VK_ERROR_OUT_OF_HOST_MEMORY; | ||
| 65 | *out = handle<VkCommandPool>(); | ||
| 66 | pools.add(number(*out)); | ||
| 67 | return VK_SUCCESS; | ||
| 68 | } | ||
| 69 | void VKAPI_CALL destroy_pool(VkDevice, VkCommandPool pool, const VkAllocationCallbacks*) { | ||
| 70 | pools.remove(number(pool)); | ||
| 71 | } | ||
| 72 | VkResult VKAPI_CALL create_semaphore(VkDevice, const VkSemaphoreCreateInfo*, const VkAllocationCallbacks*, | ||
| 73 | VkSemaphore* out) { | ||
| 74 | *out = VK_NULL_HANDLE; | ||
| 75 | if (!fail_semaphore) { | ||
| 76 | *out = handle<VkSemaphore>(); | ||
| 77 | semaphores.add(number(*out)); | ||
| 78 | } | ||
| 79 | // The next host allocation is QueueHolds::attach's holder-vector growth. Context and queue members already | ||
| 80 | // exist, so MSVC Debug's empty-container iterator proxies are constructed before injection is armed. | ||
| 81 | if (arm_after_semaphore) { | ||
| 82 | arm_after_semaphore = false; | ||
| 83 | fail_next_new = true; | ||
| 84 | } | ||
| 85 | return fail_semaphore ? VK_ERROR_OUT_OF_HOST_MEMORY : VK_SUCCESS; | ||
| 86 | } | ||
| 87 | void VKAPI_CALL destroy_semaphore(VkDevice, VkSemaphore semaphore, const VkAllocationCallbacks*) { | ||
| 88 | semaphores.remove(number(semaphore)); | ||
| 89 | } | ||
| 90 | VkResult VKAPI_CALL idle(VkQueue) { return VK_SUCCESS; } | ||
| 91 | VkResult VKAPI_CALL completed(VkDevice, VkSemaphore, std::uint64_t* out) { | ||
| 92 | *out = UINT64_MAX; | ||
| 93 | return VK_SUCCESS; | ||
| 94 | } | ||
| 95 | void install(leonida::VulkanContext& context) { | ||
| 96 | context.vk.vkCreateCommandPool = create_pool; | ||
| 97 | context.vk.vkDestroyCommandPool = destroy_pool; | ||
| 98 | context.vk.vkCreateSemaphore = create_semaphore; | ||
| 99 | context.vk.vkDestroySemaphore = destroy_semaphore; | ||
| 100 | context.vk.vkQueueWaitIdle = idle; | ||
| 101 | context.vk.vkGetSemaphoreCounterValue = completed; | ||
| 102 | } | ||
| 103 | void reset() { | ||
| 104 | pools = {}; | ||
| 105 | semaphores = {}; | ||
| 106 | fail_pool = fail_semaphore = arm_after_semaphore = fail_next_new = false; | ||
| 107 | } | ||
| 108 | |||
| 109 | void initial_failure_and_retry() { | ||
| 110 | reset(); | ||
| 111 | { | ||
| 112 | leonida::VulkanContext context(no_proc, handle<VkDevice>(), false); | ||
| 113 | install(context); | ||
| 114 | arm_after_semaphore = true; | ||
| 115 | bool threw = false; | ||
| 116 | try { leonida::VulkanQueue failed(context, handle<VkQueue>(), 0); } | ||
| 117 | catch (const std::bad_alloc&) { threw = true; } | ||
| 118 | const bool consumed = !fail_next_new; | ||
| 119 | fail_next_new = false; | ||
| 120 | expect(threw && consumed, "first holder insertion did not reject the injected allocation"); | ||
| 121 | expect(pools.exact(1, 1) && semaphores.exact(1, 1), | ||
| 122 | "failed first queue did not clean both Vulkan handles exactly once"); | ||
| 123 | { | ||
| 124 | leonida::VulkanQueue retry(context, handle<VkQueue>(), 0); | ||
| 125 | expect(retry.ok(), "retry in the same context did not construct a queue"); | ||
| 126 | } | ||
| 127 | } | ||
| 128 | expect(pools.exact(2, 2) && semaphores.exact(2, 2), "retry did not clean its handles exactly once"); | ||
| 129 | } | ||
| 130 | |||
| 131 | void failure_with_live_queue() { | ||
| 132 | reset(); | ||
| 133 | { | ||
| 134 | leonida::VulkanContext context(no_proc, handle<VkDevice>(), false); | ||
| 135 | install(context); | ||
| 136 | leonida::VulkanQueue first(context, handle<VkQueue>(), 0); | ||
| 137 | expect(first.ok(), "first live queue failed to construct"); | ||
| 138 | arm_after_semaphore = true; | ||
| 139 | bool threw = false; | ||
| 140 | try { leonida::VulkanQueue failed(context, handle<VkQueue>(), 0); } | ||
| 141 | catch (const std::bad_alloc&) { threw = true; } | ||
| 142 | const bool consumed = !fail_next_new; | ||
| 143 | fail_next_new = false; | ||
| 144 | expect(threw && consumed, "holder-vector growth did not reject the injected allocation"); | ||
| 145 | expect(pools.exact(2, 1) && semaphores.exact(2, 1), | ||
| 146 | "failed second queue did not clean its own handles while the first stayed live"); | ||
| 147 | { | ||
| 148 | leonida::VulkanQueue retry(context, handle<VkQueue>(), 0); | ||
| 149 | expect(retry.ok(), "queue registration did not recover after failed growth"); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | expect(pools.exact(3, 3) && semaphores.exact(3, 3), | ||
| 153 | "live and retried queues did not each clean their handles once"); | ||
| 154 | } | ||
| 155 | |||
| 156 | void partial_creation_failures() { | ||
| 157 | reset(); | ||
| 158 | { | ||
| 159 | leonida::VulkanContext context(no_proc, handle<VkDevice>(), false); | ||
| 160 | install(context); | ||
| 161 | fail_pool = true; | ||
| 162 | leonida::VulkanQueue failed(context, handle<VkQueue>(), 0); | ||
| 163 | expect(!failed.ok(), "failed command-pool creation appeared usable"); | ||
| 164 | } | ||
| 165 | expect(pools.exact(0, 0) && semaphores.exact(0, 0), "pool failure touched an invalid handle"); | ||
| 166 | |||
| 167 | reset(); | ||
| 168 | { | ||
| 169 | leonida::VulkanContext context(no_proc, handle<VkDevice>(), false); | ||
| 170 | install(context); | ||
| 171 | fail_semaphore = true; | ||
| 172 | leonida::VulkanQueue failed(context, handle<VkQueue>(), 0); | ||
| 173 | expect(!failed.ok(), "failed semaphore creation appeared usable"); | ||
| 174 | } | ||
| 175 | expect(pools.exact(1, 1) && semaphores.exact(0, 0), | ||
| 176 | "ordinary semaphore failure did not clean only its command pool"); | ||
| 177 | |||
| 178 | reset(); | ||
| 179 | { | ||
| 180 | leonida::VulkanContext context(no_proc, handle<VkDevice>(), false); | ||
| 181 | install(context); | ||
| 182 | fail_semaphore = true; | ||
| 183 | arm_after_semaphore = true; | ||
| 184 | bool threw = false; | ||
| 185 | try { leonida::VulkanQueue failed(context, handle<VkQueue>(), 0); } | ||
| 186 | catch (const std::bad_alloc&) { threw = true; } | ||
| 187 | const bool consumed = !fail_next_new; | ||
| 188 | fail_next_new = false; | ||
| 189 | expect(threw && consumed, "partial creation did not reach the injected holder allocation"); | ||
| 190 | } | ||
| 191 | expect(pools.exact(1, 1) && semaphores.exact(0, 0), | ||
| 192 | "attach failure after semaphore failure did not clean only the command pool"); | ||
| 193 | } | ||
| 194 | } // namespace | ||
| 195 | |||
| 196 | void* operator new(std::size_t size) { | ||
| 197 | if (fail_next_new) { | ||
| 198 | fail_next_new = false; | ||
| 199 | throw std::bad_alloc{}; | ||
| 200 | } | ||
| 201 | if (void* p = std::malloc(size ? size : 1)) return p; | ||
| 202 | throw std::bad_alloc{}; | ||
| 203 | } | ||
| 204 | void* operator new[](std::size_t size) { return operator new(size); } | ||
| 205 | void operator delete(void* p) noexcept { std::free(p); } | ||
| 206 | void operator delete[](void* p) noexcept { std::free(p); } | ||
| 207 | void operator delete(void* p, std::size_t) noexcept { std::free(p); } | ||
| 208 | void operator delete[](void* p, std::size_t) noexcept { std::free(p); } | ||
| 209 | |||
| 210 | int main() { | ||
| 211 | initial_failure_and_retry(); | ||
| 212 | failure_with_live_queue(); | ||
| 213 | partial_creation_failures(); | ||
| 214 | if (failures) return 1; | ||
| 215 | std::puts("vk_queue_constructor_oom_test: ok"); | ||
| 216 | return 0; | ||
| 217 | } | ||