upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/vk_queue_constructor_oom_test.cpp
blob: dbd5d0a11e316e72d19cba78154557c443058713 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// A failed QueueHolds registration must release VulkanQueue's command pool and timeline semaphore.
// Public runtime classes with fake Vulkan procedures: no loader, GPU, or game data.
#include "../src/vulkan_backend.h"

#include <array>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <new>
#include <type_traits>

namespace {
bool fail_next_new = false;
bool arm_after_semaphore = false;
bool fail_pool = false;
bool fail_semaphore = false;
int failures = 0;
std::uintptr_t next_handle = 100;

template<typename Handle> Handle handle() {
    const auto value = ++next_handle;
    if constexpr (std::is_pointer_v<Handle>) return reinterpret_cast<Handle>(value);
    else return static_cast<Handle>(value);
}
template<typename Handle> std::uintptr_t number(Handle value) {
    if constexpr (std::is_pointer_v<Handle>) return reinterpret_cast<std::uintptr_t>(value);
    else return static_cast<std::uintptr_t>(value);
}

struct Handles {
    std::array<std::uintptr_t, 16> live{};
    int created = 0;
    int destroyed = 0;
    int invalid_destroy = 0;
    void add(std::uintptr_t value) { live[created++] = value; }
    void remove(std::uintptr_t value) {
        for (auto& item : live) {
            if (item == value) {
                item = 0;
                ++destroyed;
                return;
            }
        }
        ++invalid_destroy;
    }
    bool exact(int made, int freed) const {
        return created == made && destroyed == freed && invalid_destroy == 0;
    }
};
Handles pools, semaphores;

void expect(bool ok, const char* what) {
    if (!ok) {
        std::fprintf(stderr, "FAIL: %s\n", what);
        ++failures;
    }
}

PFN_vkVoidFunction VKAPI_CALL no_proc(VkDevice, const char*) { return nullptr; }
VkResult VKAPI_CALL create_pool(VkDevice, const VkCommandPoolCreateInfo*, const VkAllocationCallbacks*,
                                VkCommandPool* out) {
    *out = VK_NULL_HANDLE;
    if (fail_pool) return VK_ERROR_OUT_OF_HOST_MEMORY;
    *out = handle<VkCommandPool>();
    pools.add(number(*out));
    return VK_SUCCESS;
}
void VKAPI_CALL destroy_pool(VkDevice, VkCommandPool pool, const VkAllocationCallbacks*) {
    pools.remove(number(pool));
}
VkResult VKAPI_CALL create_semaphore(VkDevice, const VkSemaphoreCreateInfo*, const VkAllocationCallbacks*,
                                     VkSemaphore* out) {
    *out = VK_NULL_HANDLE;
    if (!fail_semaphore) {
        *out = handle<VkSemaphore>();
        semaphores.add(number(*out));
    }
    // The next host allocation is QueueHolds::attach's holder-vector growth. Context and queue members already
    // exist, so MSVC Debug's empty-container iterator proxies are constructed before injection is armed.
    if (arm_after_semaphore) {
        arm_after_semaphore = false;
        fail_next_new = true;
    }
    return fail_semaphore ? VK_ERROR_OUT_OF_HOST_MEMORY : VK_SUCCESS;
}
void VKAPI_CALL destroy_semaphore(VkDevice, VkSemaphore semaphore, const VkAllocationCallbacks*) {
    semaphores.remove(number(semaphore));
}
VkResult VKAPI_CALL idle(VkQueue) { return VK_SUCCESS; }
VkResult VKAPI_CALL completed(VkDevice, VkSemaphore, std::uint64_t* out) {
    *out = UINT64_MAX;
    return VK_SUCCESS;
}
void install(leonida::VulkanContext& context) {
    context.vk.vkCreateCommandPool = create_pool;
    context.vk.vkDestroyCommandPool = destroy_pool;
    context.vk.vkCreateSemaphore = create_semaphore;
    context.vk.vkDestroySemaphore = destroy_semaphore;
    context.vk.vkQueueWaitIdle = idle;
    context.vk.vkGetSemaphoreCounterValue = completed;
}
void reset() {
    pools = {};
    semaphores = {};
    fail_pool = fail_semaphore = arm_after_semaphore = fail_next_new = false;
}

void initial_failure_and_retry() {
    reset();
    {
        leonida::VulkanContext context(no_proc, handle<VkDevice>(), false);
        install(context);
        arm_after_semaphore = true;
        bool threw = false;
        try { leonida::VulkanQueue failed(context, handle<VkQueue>(), 0); }
        catch (const std::bad_alloc&) { threw = true; }
        const bool consumed = !fail_next_new;
        fail_next_new = false;
        expect(threw && consumed, "first holder insertion did not reject the injected allocation");
        expect(pools.exact(1, 1) && semaphores.exact(1, 1),
               "failed first queue did not clean both Vulkan handles exactly once");
        {
            leonida::VulkanQueue retry(context, handle<VkQueue>(), 0);
            expect(retry.ok(), "retry in the same context did not construct a queue");
        }
    }
    expect(pools.exact(2, 2) && semaphores.exact(2, 2), "retry did not clean its handles exactly once");
}

void failure_with_live_queue() {
    reset();
    {
        leonida::VulkanContext context(no_proc, handle<VkDevice>(), false);
        install(context);
        leonida::VulkanQueue first(context, handle<VkQueue>(), 0);
        expect(first.ok(), "first live queue failed to construct");
        arm_after_semaphore = true;
        bool threw = false;
        try { leonida::VulkanQueue failed(context, handle<VkQueue>(), 0); }
        catch (const std::bad_alloc&) { threw = true; }
        const bool consumed = !fail_next_new;
        fail_next_new = false;
        expect(threw && consumed, "holder-vector growth did not reject the injected allocation");
        expect(pools.exact(2, 1) && semaphores.exact(2, 1),
               "failed second queue did not clean its own handles while the first stayed live");
        {
            leonida::VulkanQueue retry(context, handle<VkQueue>(), 0);
            expect(retry.ok(), "queue registration did not recover after failed growth");
        }
    }
    expect(pools.exact(3, 3) && semaphores.exact(3, 3),
           "live and retried queues did not each clean their handles once");
}

void partial_creation_failures() {
    reset();
    {
        leonida::VulkanContext context(no_proc, handle<VkDevice>(), false);
        install(context);
        fail_pool = true;
        leonida::VulkanQueue failed(context, handle<VkQueue>(), 0);
        expect(!failed.ok(), "failed command-pool creation appeared usable");
    }
    expect(pools.exact(0, 0) && semaphores.exact(0, 0), "pool failure touched an invalid handle");

    reset();
    {
        leonida::VulkanContext context(no_proc, handle<VkDevice>(), false);
        install(context);
        fail_semaphore = true;
        leonida::VulkanQueue failed(context, handle<VkQueue>(), 0);
        expect(!failed.ok(), "failed semaphore creation appeared usable");
    }
    expect(pools.exact(1, 1) && semaphores.exact(0, 0),
           "ordinary semaphore failure did not clean only its command pool");

    reset();
    {
        leonida::VulkanContext context(no_proc, handle<VkDevice>(), false);
        install(context);
        fail_semaphore = true;
        arm_after_semaphore = true;
        bool threw = false;
        try { leonida::VulkanQueue failed(context, handle<VkQueue>(), 0); }
        catch (const std::bad_alloc&) { threw = true; }
        const bool consumed = !fail_next_new;
        fail_next_new = false;
        expect(threw && consumed, "partial creation did not reach the injected holder allocation");
    }
    expect(pools.exact(1, 1) && semaphores.exact(0, 0),
           "attach failure after semaphore failure did not clean only the command pool");
}
}  // namespace

void* operator new(std::size_t size) {
    if (fail_next_new) {
        fail_next_new = false;
        throw std::bad_alloc{};
    }
    if (void* p = std::malloc(size ? size : 1)) return p;
    throw std::bad_alloc{};
}
void* operator new[](std::size_t size) { return operator new(size); }
void operator delete(void* p) noexcept { std::free(p); }
void operator delete[](void* p) noexcept { std::free(p); }
void operator delete(void* p, std::size_t) noexcept { std::free(p); }
void operator delete[](void* p, std::size_t) noexcept { std::free(p); }

int main() {
    initial_failure_and_retry();
    failure_with_live_queue();
    partial_creation_failures();
    if (failures) return 1;
    std::puts("vk_queue_constructor_oom_test: ok");
    return 0;
}