upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-23 17:06:10 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-23 17:06:10 -0500
commit2a99db7d64eddd4cd84e8bc2c78e57eba6e9330a (patch)
tree18e2c016ab46cf4dde991922b4f05f4378d3217f /runtime
parented4b8fdd051a235246fd9c07c6c57b4134b8314c (diff)
parent0eb99fbe9acfe6458eb4dd177a057c4926915f7a (diff)
Merge PR #9505
Diffstat (limited to 'runtime')
-rw-r--r--runtime/CMakeLists.txt10
-rw-r--r--runtime/src/vulkan_backend.h9
-rw-r--r--runtime/test/vk_queue_constructor_oom_test.cpp217
3 files changed, 235 insertions, 1 deletions
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt
index 7080d87bf..743031365 100644
--- a/runtime/CMakeLists.txt
+++ b/runtime/CMakeLists.txt
@@ -3036,6 +3036,16 @@ if(TARGET Vulkan::Headers)
3036 add_executable(arena_size_test test/arena_size_test.cpp) 3036 add_executable(arena_size_test test/arena_size_test.cpp)
3037 target_link_libraries(arena_size_test PRIVATE Vulkan::Headers) 3037 target_link_libraries(arena_size_test PRIVATE Vulkan::Headers)
3038 add_test(NAME arena_size COMMAND arena_size_test) 3038 add_test(NAME arena_size COMMAND arena_size_test)
3039 # Queue construction cleans its Vulkan handles if holder registration runs out of host memory.
3040 add_executable(vk_queue_constructor_oom_test test/vk_queue_constructor_oom_test.cpp)
3041 target_link_libraries(vk_queue_constructor_oom_test PRIVATE Vulkan::Headers ${CMAKE_DL_LIBS})
3042 if(WIN32)
3043 target_compile_definitions(vk_queue_constructor_oom_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
3044 if(MSVC)
3045 target_compile_options(vk_queue_constructor_oom_test PRIVATE /W4 /permissive- /EHsc /utf-8)
3046 endif()
3047 endif()
3048 add_test(NAME vk_queue_constructor_oom COMMAND vk_queue_constructor_oom_test)
3039 endif() 3049 endif()
3040else() 3050else()
3041 include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/vulkan_headers_missing.cmake") 3051 include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/vulkan_headers_missing.cmake")
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index e659a762d..748705547 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -1809,7 +1809,14 @@ public:
1809 type.semaphoreType = VK_SEMAPHORE_TYPE_TIMELINE; 1809 type.semaphoreType = VK_SEMAPHORE_TYPE_TIMELINE;
1810 VkSemaphoreCreateInfo info{VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, &type}; 1810 VkSemaphoreCreateInfo info{VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, &type};
1811 ok_ = ok_ && vk.vkCreateSemaphore(context.device(), &info, nullptr, &progress_) == VK_SUCCESS; 1811 ok_ = ok_ && vk.vkCreateSemaphore(context.device(), &info, nullptr, &progress_) == VK_SUCCESS;
1812 context_.holds().attach(this); 1812 try {
1813 context_.holds().attach(this);
1814 } catch (...) {
1815 // A failed registration leaves no queue to run the destructor for these Vulkan handles.
1816 if (progress_) vk.vkDestroySemaphore(context.device(), progress_, nullptr);
1817 if (pool_) vk.vkDestroyCommandPool(context.device(), pool_, nullptr);
1818 throw;
1819 }
1813 } 1820 }
1814 1821
1815 ~VulkanQueue() { 1822 ~VulkanQueue() {
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
13namespace {
14bool fail_next_new = false;
15bool arm_after_semaphore = false;
16bool fail_pool = false;
17bool fail_semaphore = false;
18int failures = 0;
19std::uintptr_t next_handle = 100;
20
21template<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}
26template<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
31struct 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};
51Handles pools, semaphores;
52
53void expect(bool ok, const char* what) {
54 if (!ok) {
55 std::fprintf(stderr, "FAIL: %s\n", what);
56 ++failures;
57 }
58}
59
60PFN_vkVoidFunction VKAPI_CALL no_proc(VkDevice, const char*) { return nullptr; }
61VkResult 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}
69void VKAPI_CALL destroy_pool(VkDevice, VkCommandPool pool, const VkAllocationCallbacks*) {
70 pools.remove(number(pool));
71}
72VkResult 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}
87void VKAPI_CALL destroy_semaphore(VkDevice, VkSemaphore semaphore, const VkAllocationCallbacks*) {
88 semaphores.remove(number(semaphore));
89}
90VkResult VKAPI_CALL idle(VkQueue) { return VK_SUCCESS; }
91VkResult VKAPI_CALL completed(VkDevice, VkSemaphore, std::uint64_t* out) {
92 *out = UINT64_MAX;
93 return VK_SUCCESS;
94}
95void 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}
103void reset() {
104 pools = {};
105 semaphores = {};
106 fail_pool = fail_semaphore = arm_after_semaphore = fail_next_new = false;
107}
108
109void 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
131void 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
156void 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
196void* 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}
204void* operator new[](std::size_t size) { return operator new(size); }
205void operator delete(void* p) noexcept { std::free(p); }
206void operator delete[](void* p) noexcept { std::free(p); }
207void operator delete(void* p, std::size_t) noexcept { std::free(p); }
208void operator delete[](void* p, std::size_t) noexcept { std::free(p); }
209
210int 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}