diff options
| author | ubiquitarianism <283527159+ubiquitarianism@users.noreply.github.com> | 2026-11-23 22:36:12 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-23 22:36:12 -0500 |
| commit | fabe844cd88e6fd6469ffd3d869bbe062ebefe1f (patch) | |
| tree | ffd126d5d76910e65b140c235dfdcf629f292cc5 /runtime/test | |
| parent | f9e7e3e1bfa718eb4d8ca84fd1dbe617038d48d7 (diff) | |
| parent | 2d7be48dec529446e0ead3393e2b3acabb29ff64 (diff) | |
Merge pull request #131 from Mumogg/restore-directstorage
runtime: restore the console DirectStorage shim, with the typed-error inflate it needs (RT-DSTORAGE-RESTORE, #47)
Diffstat (limited to 'runtime/test')
| -rw-r--r-- | runtime/test/dstorage_test.cpp | 1260 |
1 files changed, 1260 insertions, 0 deletions
diff --git a/runtime/test/dstorage_test.cpp b/runtime/test/dstorage_test.cpp new file mode 100644 index 000000000..1e8ea7318 --- /dev/null +++ b/runtime/test/dstorage_test.cpp | |||
| @@ -0,0 +1,1260 @@ | |||
| 1 | // DirectStorage shim (runtime/shims/dstorage.cpp): DStorageGetFactory resolved through the shim registry under its | ||
| 2 | // three module names, driven the way a console title calls it, over synthetic files in %TEMP%. No game data, no GPU. | ||
| 3 | #include <windows.h> | ||
| 4 | #include <d3d12.h> | ||
| 5 | |||
| 6 | #include <algorithm> | ||
| 7 | #include <atomic> | ||
| 8 | #include <chrono> | ||
| 9 | #include <cstdio> | ||
| 10 | #include <cstdlib> | ||
| 11 | #include <cstring> | ||
| 12 | #include <filesystem> | ||
| 13 | #include <functional> | ||
| 14 | #include <string> | ||
| 15 | #include <thread> | ||
| 16 | #include <vector> | ||
| 17 | |||
| 18 | #include "dstorage_xs_abi.h" | ||
| 19 | #include "file_system.h" | ||
| 20 | #include "shims.h" | ||
| 21 | |||
| 22 | using namespace dsx; | ||
| 23 | namespace fs = std::filesystem; | ||
| 24 | |||
| 25 | namespace { | ||
| 26 | |||
| 27 | int g_checks = 0; | ||
| 28 | int g_failures = 0; | ||
| 29 | |||
| 30 | void check(bool ok, const char* what) { | ||
| 31 | ++g_checks; | ||
| 32 | if (!ok) { | ||
| 33 | ++g_failures; | ||
| 34 | std::printf("FAIL: %s\n", what); | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | void check_hr(HRESULT got, HRESULT want, const char* what) { | ||
| 39 | ++g_checks; | ||
| 40 | if (got != want) { | ||
| 41 | ++g_failures; | ||
| 42 | std::printf("FAIL: %s (got 0x%08lx, want 0x%08lx)\n", what, static_cast<unsigned long>(got), | ||
| 43 | static_cast<unsigned long>(want)); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | // Interface ids a title might pass. The console's own ids are not public, so these are made up; the shim accepts any | ||
| 48 | // id per kind of object. | ||
| 49 | constexpr GUID kFactoryId = {0x0d5e0001, 0x1111, 0x4111, {0x81, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x01}}; | ||
| 50 | constexpr GUID kQueueId = {0x0d5e0002, 0x1111, 0x4111, {0x81, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x02}}; | ||
| 51 | constexpr GUID kFileId = {0x0d5e0003, 0x1111, 0x4111, {0x81, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x03}}; | ||
| 52 | constexpr GUID kStatusId = {0x0d5e0004, 0x1111, 0x4111, {0x81, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x04}}; | ||
| 53 | // IDStorageFactory of the PC API (dstorage.h): refused, its object has another shape. | ||
| 54 | constexpr GUID kPcFactoryId = {0x6924ea0c, 0xc3cd, 0x4826, {0xb1, 0x0a, 0xf6, 0x4f, 0x4e, 0xd9, 0x27, 0xc1}}; | ||
| 55 | |||
| 56 | // A CPU fence with the PC ID3D12Fence vtable (the one the d3d12_xs fence proxies forward). It records the values it is | ||
| 57 | // signalled with and runs a check at each signal. | ||
| 58 | class TestFence final : public ID3D12Fence { | ||
| 59 | public: | ||
| 60 | std::function<void(UINT64)> on_signal; | ||
| 61 | std::atomic<UINT64> value{0}; | ||
| 62 | std::atomic<ULONG> refs{1}; | ||
| 63 | |||
| 64 | HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppv) override { | ||
| 65 | if (!ppv) return E_POINTER; | ||
| 66 | *ppv = nullptr; | ||
| 67 | if (riid != __uuidof(IUnknown) && riid != __uuidof(ID3D12Fence)) return E_NOINTERFACE; | ||
| 68 | AddRef(); | ||
| 69 | *ppv = this; | ||
| 70 | return S_OK; | ||
| 71 | } | ||
| 72 | ULONG STDMETHODCALLTYPE AddRef() override { return ++refs; } | ||
| 73 | ULONG STDMETHODCALLTYPE Release() override { return --refs; } // lives on the test's stack | ||
| 74 | HRESULT STDMETHODCALLTYPE GetPrivateData(REFGUID, UINT*, void*) override { return E_NOTIMPL; } | ||
| 75 | HRESULT STDMETHODCALLTYPE SetPrivateData(REFGUID, UINT, const void*) override { return E_NOTIMPL; } | ||
| 76 | HRESULT STDMETHODCALLTYPE SetPrivateDataInterface(REFGUID, const IUnknown*) override { return E_NOTIMPL; } | ||
| 77 | HRESULT STDMETHODCALLTYPE SetName(LPCWSTR) override { return S_OK; } | ||
| 78 | HRESULT STDMETHODCALLTYPE GetDevice(REFIID, void** ppv) override { | ||
| 79 | if (ppv) *ppv = nullptr; | ||
| 80 | return E_NOTIMPL; | ||
| 81 | } | ||
| 82 | UINT64 STDMETHODCALLTYPE GetCompletedValue() override { return value.load(); } | ||
| 83 | HRESULT STDMETHODCALLTYPE SetEventOnCompletion(UINT64, HANDLE) override { return E_NOTIMPL; } | ||
| 84 | HRESULT STDMETHODCALLTYPE Signal(UINT64 v) override { | ||
| 85 | if (on_signal) on_signal(v); | ||
| 86 | value.store(v); | ||
| 87 | return S_OK; | ||
| 88 | } | ||
| 89 | }; | ||
| 90 | |||
| 91 | std::vector<uint8_t> pattern(size_t n, uint32_t seed) { | ||
| 92 | std::vector<uint8_t> v(n); | ||
| 93 | uint32_t x = seed * 2654435761u + 1; | ||
| 94 | for (auto& b : v) { | ||
| 95 | x ^= x << 13; | ||
| 96 | x ^= x >> 17; | ||
| 97 | x ^= x << 5; | ||
| 98 | b = static_cast<uint8_t>(x); | ||
| 99 | } | ||
| 100 | return v; | ||
| 101 | } | ||
| 102 | |||
| 103 | void write_file(const fs::path& p, const std::vector<uint8_t>& bytes) { | ||
| 104 | fs::create_directories(p.parent_path()); | ||
| 105 | FILE* f = nullptr; | ||
| 106 | if (_wfopen_s(&f, p.c_str(), L"wb") != 0 || !f) { | ||
| 107 | std::printf("FAIL: cannot write %ls\n", p.c_str()); | ||
| 108 | std::exit(1); | ||
| 109 | } | ||
| 110 | if (!bytes.empty()) std::fwrite(bytes.data(), 1, bytes.size(), f); | ||
| 111 | std::fclose(f); | ||
| 112 | } | ||
| 113 | |||
| 114 | bool wait_complete(IDStorageStatusArrayX* s, UINT32 index, int ms = 10000) { | ||
| 115 | const auto end = std::chrono::steady_clock::now() + std::chrono::milliseconds(ms); | ||
| 116 | while (!s->IsComplete(index)) { | ||
| 117 | if (std::chrono::steady_clock::now() > end) return false; | ||
| 118 | Sleep(1); | ||
| 119 | } | ||
| 120 | return true; | ||
| 121 | } | ||
| 122 | |||
| 123 | bool wait_until(const std::function<bool()>& done, int ms = 10000) { | ||
| 124 | const auto end = std::chrono::steady_clock::now() + std::chrono::milliseconds(ms); | ||
| 125 | while (!done()) { | ||
| 126 | if (std::chrono::steady_clock::now() > end) return false; | ||
| 127 | Sleep(1); | ||
| 128 | } | ||
| 129 | return true; | ||
| 130 | } | ||
| 131 | |||
| 132 | DSTORAGE_REQUEST file_request(IDStorageFileX* f, uint64_t offset, void* dst, uint32_t n, uint64_t tag = 0, | ||
| 133 | const char* name = nullptr) { | ||
| 134 | DSTORAGE_REQUEST r{}; | ||
| 135 | r.File = f; | ||
| 136 | r.FileOffset = offset; | ||
| 137 | r.Destination = dst; | ||
| 138 | r.DestinationSize = n; | ||
| 139 | r.SourceSize = n; | ||
| 140 | r.CancellationTag = tag; | ||
| 141 | r.Name = name; | ||
| 142 | return r; | ||
| 143 | } | ||
| 144 | |||
| 145 | struct Env { | ||
| 146 | fs::path root, game, overlay; | ||
| 147 | IDStorageFactoryX2* factory = nullptr; | ||
| 148 | std::vector<uint8_t> a_package, a_overlay, b, big; | ||
| 149 | }; | ||
| 150 | |||
| 151 | IDStorageQueueX2* make_queue(Env& env, uint16_t capacity = 0x80, bool auto_submit = true, | ||
| 152 | DSTORAGE_REQUEST_SOURCE_TYPE type = DSTORAGE_REQUEST_SOURCE_FILE, | ||
| 153 | DSTORAGE_PRIORITY priority = DSTORAGE_PRIORITY_NORMAL, const char* name = "test queue") { | ||
| 154 | DSTORAGE_QUEUE_DESC1 d{}; | ||
| 155 | d.SourceType = type; | ||
| 156 | d.Capacity = capacity; | ||
| 157 | d.Priority = priority; | ||
| 158 | d.Name = name; | ||
| 159 | d.Options.AutoSubmitDisabled = auto_submit ? 0 : 1; | ||
| 160 | void* q = nullptr; | ||
| 161 | const HRESULT hr = env.factory->CreateQueue1(&d, kQueueId, &q); | ||
| 162 | check_hr(hr, S_OK, "CreateQueue1"); | ||
| 163 | return static_cast<IDStorageQueueX2*>(q); | ||
| 164 | } | ||
| 165 | |||
| 166 | IDStorageFileX* open(Env& env, const wchar_t* path) { | ||
| 167 | void* f = nullptr; | ||
| 168 | check_hr(env.factory->OpenFile(path, kFileId, &f), S_OK, "OpenFile"); | ||
| 169 | return static_cast<IDStorageFileX*>(f); | ||
| 170 | } | ||
| 171 | |||
| 172 | IDStorageStatusArrayX* make_status(Env& env, UINT32 capacity) { | ||
| 173 | void* s = nullptr; | ||
| 174 | check_hr(env.factory->CreateStatusArray(capacity, "test status", kStatusId, &s), S_OK, "CreateStatusArray"); | ||
| 175 | return static_cast<IDStorageStatusArrayX*>(s); | ||
| 176 | } | ||
| 177 | |||
| 178 | // ---- tests ---------------------------------------------------------------------------------------------------------- | ||
| 179 | |||
| 180 | void test_layout_and_options() { | ||
| 181 | DSTORAGE_REQUEST_OPTIONS o{}; | ||
| 182 | o.ZlibDecompress = 1; | ||
| 183 | check(o.AsUINT64 == options::kZlib, "ZlibDecompress is bit 0"); | ||
| 184 | o.AsUINT64 = 0; | ||
| 185 | o.BcpackMode = DSTORAGE_BCPACK_MODE_BC7; | ||
| 186 | check(options::bcpack(o.AsUINT64) == 7 && o.AsUINT64 == (7ull << options::kBcpackShift), "BcpackMode is bits 1-4"); | ||
| 187 | o.AsUINT64 = 0; | ||
| 188 | o.SwizzleMode = DSTORAGE_SWIZZLE_MODE_4K_12K_4K_12K; | ||
| 189 | check(options::swizzle(o.AsUINT64) == 4 && o.AsUINT64 == (4ull << options::kSwizzleShift), | ||
| 190 | "SwizzleMode is bits 5-20"); | ||
| 191 | o.AsUINT64 = 0; | ||
| 192 | o.DestinationIsPhysicalPages = 1; | ||
| 193 | check(o.AsUINT64 == options::kDestinationPages, "DestinationIsPhysicalPages is bit 21"); | ||
| 194 | o.AsUINT64 = 0; | ||
| 195 | o.SourceType = DSTORAGE_REQUEST_SOURCE_MEMORY; | ||
| 196 | check(o.AsUINT64 == options::kSourceMemory, "SourceType is bit 22"); | ||
| 197 | o.AsUINT64 = 0; | ||
| 198 | o.SourceIsPhysicalPages = 1; | ||
| 199 | check(o.AsUINT64 == options::kSourcePages, "SourceIsPhysicalPages is bit 23"); | ||
| 200 | o.AsUINT64 = 0; | ||
| 201 | o.Reserved = 1; | ||
| 202 | check(o.AsUINT64 == (1ull << 24) && (o.AsUINT64 & options::kReservedMask), "Reserved starts at bit 24"); | ||
| 203 | DSTORAGE_QUEUE_OPTIONS q{}; | ||
| 204 | q.AutoSubmitDisabled = 1; | ||
| 205 | check(q.AsUINT64 == 1, "AutoSubmitDisabled is bit 0"); | ||
| 206 | } | ||
| 207 | |||
| 208 | void test_registry(Env& env) { | ||
| 209 | const char* names[] = {"dstorage_xs.dll", "xcrdapi.dll", "dstorage.dll"}; | ||
| 210 | void* first = rt::shims::find("dstorage_xs.dll", "DStorageGetFactory"); | ||
| 211 | check(first != nullptr, "dstorage_xs.dll!DStorageGetFactory is registered"); | ||
| 212 | for (const char* n : names) { | ||
| 213 | check(rt::shims::find(n, "DStorageGetFactory") == first, "every module name serves the same function"); | ||
| 214 | check(rt::shims::owns_dll(n), "the shims own the module (LoadLibrary gets a pseudo-handle)"); | ||
| 215 | } | ||
| 216 | auto get = reinterpret_cast<DStorageGetFactoryFn>(first); | ||
| 217 | if (!get) std::exit(1); | ||
| 218 | |||
| 219 | check_hr(get(kFactoryId, nullptr), E_POINTER, "DStorageGetFactory without ppv"); | ||
| 220 | void* pc = reinterpret_cast<void*>(1); | ||
| 221 | check_hr(get(kPcFactoryId, &pc), E_NOINTERFACE, "the PC IDStorageFactory id is refused"); | ||
| 222 | check(pc == nullptr, "a refused factory clears ppv"); | ||
| 223 | |||
| 224 | void* f1 = nullptr; | ||
| 225 | void* f2 = nullptr; | ||
| 226 | check_hr(get(kFactoryId, &f1), S_OK, "DStorageGetFactory"); | ||
| 227 | check_hr(reinterpret_cast<DStorageGetFactoryFn>(rt::shims::find("xcrdapi.dll", "DStorageGetFactory"))( | ||
| 228 | __uuidof(IUnknown), &f2), | ||
| 229 | S_OK, "DStorageGetFactory(IUnknown) under xcrdapi.dll"); | ||
| 230 | check(f1 && f1 == f2, "the factory is one object"); | ||
| 231 | env.factory = static_cast<IDStorageFactoryX2*>(f1); | ||
| 232 | static_cast<IUnknown*>(f2)->Release(); | ||
| 233 | check(env.factory->Release() >= 1 && env.factory->Release() >= 1, "the factory's count does not drop below one"); | ||
| 234 | env.factory->AddRef(); | ||
| 235 | |||
| 236 | void* again = nullptr; | ||
| 237 | check_hr(env.factory->QueryInterface(kFactoryId, &again), S_OK, "factory QueryInterface for its own id"); | ||
| 238 | if (again) static_cast<IUnknown*>(again)->Release(); | ||
| 239 | } | ||
| 240 | |||
| 241 | void test_factory_checks(Env& env) { | ||
| 242 | // Before any queue or file exists. | ||
| 243 | check_hr(env.factory->SetStagingBufferSize(17u << 20), E_DSTORAGE_INVALID_STAGING_BUFFER_SIZE, "staging 17 MiB"); | ||
| 244 | check_hr(env.factory->SetStagingBufferSize(16u << 20), S_OK, "staging 16 MiB"); | ||
| 245 | check_hr(env.factory->SetStagingBufferSize(0), S_OK, "staging 0"); | ||
| 246 | check_hr(env.factory->SetCpuAffinity(0x3c), S_OK, "SetCpuAffinity"); | ||
| 247 | env.factory->SetDebugFlags(DSTORAGE_DEBUG_SHOW_ERRORS); | ||
| 248 | |||
| 249 | auto create = [&](DSTORAGE_REQUEST_SOURCE_TYPE type, uint16_t cap, int prio, uint64_t opts = 0) { | ||
| 250 | DSTORAGE_QUEUE_DESC1 d{}; | ||
| 251 | d.SourceType = type; | ||
| 252 | d.Capacity = cap; | ||
| 253 | d.Priority = static_cast<DSTORAGE_PRIORITY>(prio); | ||
| 254 | d.Options.AsUINT64 = opts; | ||
| 255 | void* q = reinterpret_cast<void*>(1); | ||
| 256 | const HRESULT hr = env.factory->CreateQueue1(&d, kQueueId, &q); | ||
| 257 | if (SUCCEEDED(hr)) static_cast<IUnknown*>(q)->Release(); | ||
| 258 | else check(q == nullptr, "a failed CreateQueue1 clears ppv"); | ||
| 259 | return hr; | ||
| 260 | }; | ||
| 261 | check_hr(create(DSTORAGE_REQUEST_SOURCE_FILE, 0x7f, 0), E_DSTORAGE_INVALID_QUEUE_CAPACITY, "capacity 0x7f"); | ||
| 262 | check_hr(create(DSTORAGE_REQUEST_SOURCE_FILE, 0x2001, 0), E_DSTORAGE_INVALID_QUEUE_CAPACITY, "capacity 0x2001"); | ||
| 263 | check_hr(create(DSTORAGE_REQUEST_SOURCE_FILE, 0x80, 3), E_DSTORAGE_INVALID_QUEUE_PRIORITY, "priority 3"); | ||
| 264 | check_hr(create(DSTORAGE_REQUEST_SOURCE_FILE, 0x80, -2), E_DSTORAGE_INVALID_QUEUE_PRIORITY, "priority -2"); | ||
| 265 | check_hr(create(DSTORAGE_REQUEST_SOURCE_MEMORY, 0x80, 0), E_DSTORAGE_INVALID_MEMORY_QUEUE_PRIORITY, | ||
| 266 | "a memory queue must be realtime"); | ||
| 267 | check_hr(create(static_cast<DSTORAGE_REQUEST_SOURCE_TYPE>(2), 0x80, 0), E_DSTORAGE_INVALID_SOURCE_TYPE, | ||
| 268 | "source type 2"); | ||
| 269 | check_hr(create(DSTORAGE_REQUEST_SOURCE_FILE, 0x80, 0, 2), E_DSTORAGE_RESERVED_FIELDS, "queue option bit 1"); | ||
| 270 | check_hr(create(DSTORAGE_REQUEST_SOURCE_FILE, 0x2000, -1), S_OK, "capacity 0x2000, low priority"); | ||
| 271 | check_hr(create(DSTORAGE_REQUEST_SOURCE_MEMORY, 0x80, 2), S_OK, "a realtime memory queue"); | ||
| 272 | check_hr(env.factory->CreateQueue1(nullptr, kQueueId, nullptr), E_POINTER, "CreateQueue1 without ppv"); | ||
| 273 | void* q = nullptr; | ||
| 274 | check_hr(env.factory->CreateQueue(nullptr, kQueueId, &q), E_INVALIDARG, "CreateQueue without a desc"); | ||
| 275 | check_hr(env.factory->CreateQueue1(nullptr, kQueueId, &q), E_INVALIDARG, "CreateQueue1 without a desc"); | ||
| 276 | void* s = nullptr; | ||
| 277 | check_hr(env.factory->CreateStatusArray(0, nullptr, kStatusId, &s), E_INVALIDARG, "a status array of 0"); | ||
| 278 | |||
| 279 | // The id policy: an id seen for a queue is not a factory id. | ||
| 280 | void* wrong = nullptr; | ||
| 281 | check_hr(env.factory->QueryInterface(kQueueId, &wrong), E_NOINTERFACE, "a queue id is not a factory id"); | ||
| 282 | check_hr(env.factory->CreateStatusArray(4, nullptr, kFactoryId, &s), E_NOINTERFACE, | ||
| 283 | "a factory id is not a status array id"); | ||
| 284 | |||
| 285 | // CreateQueue (DSTORAGE_QUEUE_DESC) and Query reflect the desc. | ||
| 286 | DSTORAGE_QUEUE_DESC d{}; | ||
| 287 | d.SourceType = DSTORAGE_REQUEST_SOURCE_FILE; | ||
| 288 | d.Capacity = 0x100; | ||
| 289 | d.Priority = DSTORAGE_PRIORITY_HIGH; | ||
| 290 | d.Name = "named"; | ||
| 291 | check_hr(env.factory->CreateQueue(&d, kQueueId, &q), S_OK, "CreateQueue"); | ||
| 292 | auto* queue = static_cast<IDStorageQueueX2*>(q); | ||
| 293 | DSTORAGE_QUEUE_INFO info{}; | ||
| 294 | queue->Query(&info); | ||
| 295 | check(info.Desc.Capacity == 0x100 && info.Desc.Priority == DSTORAGE_PRIORITY_HIGH && info.Desc.Name == d.Name && | ||
| 296 | info.Desc.SourceType == DSTORAGE_REQUEST_SOURCE_FILE, | ||
| 297 | "Query returns the creation desc"); | ||
| 298 | check(info.EmptySlotCount == 0xff, "an empty queue has capacity - 1 empty slots"); | ||
| 299 | check(info.RequestCountUntilAutoSubmit == 0x80, "auto-submit at half the capacity"); | ||
| 300 | DSTORAGE_QUEUE_INFO1 info1{}; | ||
| 301 | queue->Query1(&info1); | ||
| 302 | check(info1.Desc.Options.AsUINT64 == 0 && info1.EmptySlotCount == 0xff, "Query1"); | ||
| 303 | check_hr(env.factory->SetStagingBufferSize(32u << 20), E_DSTORAGE_STAGING_BUFFER_LOCKED, | ||
| 304 | "staging size is locked while a queue exists"); | ||
| 305 | void* qi = nullptr; | ||
| 306 | check_hr(queue->QueryInterface(kQueueId, &qi), S_OK, "queue QueryInterface"); | ||
| 307 | if (qi) static_cast<IUnknown*>(qi)->Release(); | ||
| 308 | check(queue->Release() == 0, "the last queue reference frees it"); | ||
| 309 | check_hr(env.factory->SetStagingBufferSize(32u << 20), S_OK, "staging size unlocked again"); | ||
| 310 | } | ||
| 311 | |||
| 312 | void test_open_paths(Env& env) { | ||
| 313 | void* f = reinterpret_cast<void*>(1); | ||
| 314 | check_hr(env.factory->OpenFile(L"GAME:\\data\\missing.bin", kFileId, &f), HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), | ||
| 315 | "a missing GAME: file"); | ||
| 316 | check(f == nullptr, "a failed OpenFile clears ppv"); | ||
| 317 | check_hr(env.factory->OpenFile(L"GAME:\\..\\outside.bin", kFileId, &f), HRESULT_FROM_WIN32(ERROR_INVALID_NAME), | ||
| 318 | "a GAME: path that climbs out is refused"); | ||
| 319 | check_hr(env.factory->OpenFile(nullptr, kFileId, &f), E_INVALIDARG, "OpenFile without a path"); | ||
| 320 | check_hr(env.factory->OpenFile(L"GAME:\\data\\a.bin", kFileId, nullptr), E_POINTER, "OpenFile without ppv"); | ||
| 321 | |||
| 322 | // The overlay copy wins over the package; a host path is passed through. | ||
| 323 | IDStorageFileX* a = open(env, L"GAME:\\data\\a.bin"); | ||
| 324 | IDStorageFileX* b = open(env, L"game:/data/b.bin"); | ||
| 325 | const std::wstring host = (env.game / L"data" / L"b.bin").wstring(); | ||
| 326 | IDStorageFileX* h = open(env, host.c_str()); | ||
| 327 | if (!a || !b || !h) std::exit(1); | ||
| 328 | |||
| 329 | IDStorageQueueX2* q = make_queue(env); | ||
| 330 | IDStorageStatusArrayX* s = make_status(env, 4); | ||
| 331 | std::vector<uint8_t> da(4096), db(1000), dh(1000); | ||
| 332 | DSTORAGE_REQUEST r = file_request(a, 0, da.data(), 4096); | ||
| 333 | q->EnqueueRequest(&r); | ||
| 334 | r = file_request(b, 500, db.data(), 1000); | ||
| 335 | q->EnqueueRequest(&r); | ||
| 336 | r = file_request(h, 500, dh.data(), 1000); | ||
| 337 | q->EnqueueRequest(&r); | ||
| 338 | q->EnqueueStatus(s, 0); | ||
| 339 | check_hr(s->GetHResult(0), E_PENDING, "a status entry is pending once enqueued"); | ||
| 340 | q->Submit(); | ||
| 341 | check(wait_complete(s, 0), "the reads complete"); | ||
| 342 | check_hr(s->GetHResult(0), S_OK, "the reads succeed"); | ||
| 343 | check(std::memcmp(da.data(), env.a_overlay.data(), 4096) == 0 && da != env.a_package, | ||
| 344 | "GAME: reads the overlay copy, not the package"); | ||
| 345 | |||
| 346 | // GetHandle: a handle of its own (ReOpenFile), so its file position does not follow the shim's reads, which | ||
| 347 | // just ended at byte 4096 of this file. | ||
| 348 | HANDLE own = a->GetHandle(); | ||
| 349 | LARGE_INTEGER size{}; | ||
| 350 | check(own != INVALID_HANDLE_VALUE && GetFileSizeEx(own, &size) && size.QuadPart == 5000, | ||
| 351 | "GetHandle names the overlay copy (5000 bytes, the package has 4096)"); | ||
| 352 | std::vector<uint8_t> via_handle(6000); | ||
| 353 | DWORD got = 0; | ||
| 354 | check(own != INVALID_HANDLE_VALUE && ReadFile(own, via_handle.data(), 6000, &got, nullptr) && got == 5000 && | ||
| 355 | std::memcmp(via_handle.data(), env.a_overlay.data(), 5000) == 0, | ||
| 356 | "a ReadFile through GetHandle's handle starts at byte 0 and reads the overlay copy"); | ||
| 357 | if (own != INVALID_HANDLE_VALUE) CloseHandle(own); | ||
| 358 | r = file_request(a, 100, da.data(), 50); | ||
| 359 | q->EnqueueRequest(&r); | ||
| 360 | q->EnqueueStatus(s, 1); | ||
| 361 | q->Submit(); | ||
| 362 | check(wait_complete(s, 1) && std::memcmp(da.data(), env.a_overlay.data() + 100, 50) == 0, | ||
| 363 | "the shim's reads do not follow a GetHandle handle's position either"); | ||
| 364 | check(std::memcmp(db.data(), env.b.data() + 500, 1000) == 0, | ||
| 365 | "a GAME: read at an offset (forward slashes, any case)"); | ||
| 366 | check(db == dh, "a host path reads the same file"); | ||
| 367 | a->Release(); | ||
| 368 | b->Release(); | ||
| 369 | h->Release(); | ||
| 370 | s->Release(); | ||
| 371 | q->Release(); | ||
| 372 | } | ||
| 373 | |||
| 374 | void test_reads_and_errors(Env& env) { | ||
| 375 | IDStorageFileX* b = open(env, L"GAME:\\data\\b.bin"); // 10,000 bytes | ||
| 376 | IDStorageFileX* big = open(env, L"GAME:\\data\\big.bin"); | ||
| 377 | IDStorageQueueX2* q = make_queue(env, 0x80, false); | ||
| 378 | IDStorageStatusArrayX* s = make_status(env, 32); | ||
| 379 | HANDLE error_event = q->GetErrorEvent(); | ||
| 380 | check(error_event != nullptr, "GetErrorEvent returns a handle"); | ||
| 381 | |||
| 382 | std::vector<uint8_t> big_out(env.big.size()); | ||
| 383 | std::vector<uint8_t> few(64, 0xAB); | ||
| 384 | std::vector<uint8_t> zero(16, 0xCD); | ||
| 385 | struct Case { | ||
| 386 | DSTORAGE_REQUEST r; | ||
| 387 | HRESULT want; | ||
| 388 | const char* what; | ||
| 389 | }; | ||
| 390 | std::vector<Case> cases; | ||
| 391 | cases.push_back({file_request(big, 0, big_out.data(), static_cast<uint32_t>(big_out.size())), S_OK, | ||
| 392 | "a 3 MiB read"}); | ||
| 393 | cases.push_back({file_request(b, 10000, zero.data(), 0), S_OK, "a zero-size read at the end of the file"}); | ||
| 394 | cases.push_back({file_request(b, 9990, few.data(), 11, 0x77, "past-the-end"), E_DSTORAGE_END_OF_FILE, | ||
| 395 | "a read one byte past the end"}); | ||
| 396 | cases.push_back({file_request(b, ~0ull - 4, few.data(), 8), E_DSTORAGE_END_OF_FILE, "an offset near 2^64"}); | ||
| 397 | cases.push_back({file_request(b, 0, few.data(), 8), S_OK, "a good read after failures"}); | ||
| 398 | { | ||
| 399 | DSTORAGE_REQUEST r = file_request(b, 0, few.data(), 8); | ||
| 400 | r.DestinationSize = 16; | ||
| 401 | cases.push_back({r, E_DSTORAGE_INVALID_DESTINATION_SIZE, "DestinationSize differs from SourceSize"}); | ||
| 402 | r = file_request(b, 0, few.data(), 8); | ||
| 403 | r.Options.ZlibDecompress = 1; | ||
| 404 | cases.push_back({r, E_DSTORAGE_INVALID_DESTINATION_SIZE, "zlib with DestinationSize equal to SourceSize"}); | ||
| 405 | r = file_request(b, 0, few.data(), 8); | ||
| 406 | r.Options.BcpackMode = DSTORAGE_BCPACK_MODE_BC1; | ||
| 407 | cases.push_back({r, E_NOTIMPL, "BCPack is not implemented yet"}); | ||
| 408 | r = file_request(b, 0, few.data(), 8); | ||
| 409 | r.Options.SwizzleMode = DSTORAGE_SWIZZLE_MODE_4K_4K_8K; | ||
| 410 | cases.push_back({r, E_NOTIMPL, "swizzle is not implemented yet"}); | ||
| 411 | r = file_request(b, 0, few.data(), 8); | ||
| 412 | r.Options.AsUINT64 = 8ull << options::kBcpackShift; | ||
| 413 | cases.push_back({r, E_DSTORAGE_INVALID_BCPACK_MODE, "BCPack mode 8"}); | ||
| 414 | r = file_request(b, 0, few.data(), 8); | ||
| 415 | r.Options.AsUINT64 = 5ull << options::kSwizzleShift; | ||
| 416 | cases.push_back({r, E_DSTORAGE_INVALID_SWIZZLE_MODE, "swizzle mode 5"}); | ||
| 417 | r = file_request(b, 0, few.data(), 8); | ||
| 418 | r.Options.Reserved = 1; | ||
| 419 | cases.push_back({r, E_DSTORAGE_RESERVED_FIELDS, "a reserved option bit"}); | ||
| 420 | r = file_request(b, 0, few.data(), 8); | ||
| 421 | r.Options.DestinationIsPhysicalPages = 1; | ||
| 422 | cases.push_back({r, E_NOTIMPL, "physical pages are not implemented yet"}); | ||
| 423 | r = file_request(b, 0, few.data(), 8); | ||
| 424 | r.IntermediateSize = 4; | ||
| 425 | cases.push_back({r, E_DSTORAGE_INVALID_INTERMEDIATE_SIZE, "IntermediateSize without zlib and BCPack"}); | ||
| 426 | r = file_request(b, 0, nullptr, 8); | ||
| 427 | cases.push_back({r, E_INVALIDARG, "a null destination"}); | ||
| 428 | r = file_request(nullptr, 0, few.data(), 8); | ||
| 429 | cases.push_back({r, E_INVALIDARG, "a null file"}); | ||
| 430 | r = file_request(reinterpret_cast<IDStorageFileX*>(s), 0, few.data(), 8); | ||
| 431 | cases.push_back({r, E_DSTORAGE_INVALID_FILE_HANDLE, "a status array passed as a file"}); | ||
| 432 | r = file_request(b, 0, few.data(), 8); | ||
| 433 | r.Options.SourceType = DSTORAGE_REQUEST_SOURCE_MEMORY; | ||
| 434 | cases.push_back({r, E_DSTORAGE_INVALID_SOURCE_TYPE, "a memory request on a file queue"}); | ||
| 435 | } | ||
| 436 | cases.push_back({file_request(b, 0, nullptr, 0), E_INVALIDARG, "a zero-size read still needs a destination"}); | ||
| 437 | cases.push_back({file_request(nullptr, 0, zero.data(), 0), E_INVALIDARG, "a zero-size read still needs a file"}); | ||
| 438 | void* reserved = VirtualAlloc(nullptr, 1 << 16, MEM_RESERVE, PAGE_READWRITE); | ||
| 439 | cases.push_back({file_request(b, 0, reserved, 4096), E_DSTORAGE_ACCESS_VIOLATION, | ||
| 440 | "a destination the process cannot write"}); | ||
| 441 | check(cases.size() <= 31, "the cases fit the status array"); | ||
| 442 | |||
| 443 | for (UINT32 i = 0; i < cases.size(); ++i) { | ||
| 444 | q->EnqueueRequest(&cases[i].r); | ||
| 445 | q->EnqueueStatus(s, i); | ||
| 446 | } | ||
| 447 | Sleep(30); | ||
| 448 | check(!s->IsComplete(0), "nothing runs before Submit when auto-submit is disabled"); | ||
| 449 | DSTORAGE_QUEUE_INFO1 info{}; | ||
| 450 | q->Query1(&info); | ||
| 451 | check(info.RequestCountUntilAutoSubmit == 0, "no auto-submit count when it is disabled"); | ||
| 452 | check(info.EmptySlotCount == 0x7f - 2 * cases.size(), "EmptySlotCount counts every entry"); | ||
| 453 | q->Submit(); | ||
| 454 | const UINT32 last = static_cast<UINT32>(cases.size() - 1); | ||
| 455 | check(wait_complete(s, last), "all cases complete"); | ||
| 456 | for (UINT32 i = 0; i < cases.size(); ++i) check_hr(s->GetHResult(i), cases[i].want, cases[i].what); | ||
| 457 | check(big_out == env.big, "the 3 MiB read has the file's bytes"); | ||
| 458 | check(zero == std::vector<uint8_t>(16, 0xCD), "a zero-size read writes nothing"); | ||
| 459 | check(std::memcmp(few.data(), env.b.data(), 8) == 0, "the good read after failures"); | ||
| 460 | |||
| 461 | check(WaitForSingleObject(error_event, 0) == WAIT_OBJECT_0, "the error event is set"); | ||
| 462 | DSTORAGE_ERROR_RECORD rec{}; | ||
| 463 | q->RetrieveErrorRecord(&rec); | ||
| 464 | size_t failures = 0; | ||
| 465 | for (const auto& c : cases) failures += FAILED(c.want) ? 1 : 0; | ||
| 466 | check(rec.FailureCount == failures, "FailureCount counts every failed request"); | ||
| 467 | check_hr(rec.FirstFailure.HResult, E_DSTORAGE_END_OF_FILE, "the first failure is recorded"); | ||
| 468 | check(rec.FirstFailure.CommandType == DSTORAGE_COMMAND_TYPE_REQUEST, "its command type is REQUEST"); | ||
| 469 | check(rec.FirstFailure.Request.FileOffset == 9990 && rec.FirstFailure.Request.SourceSize == 11 && | ||
| 470 | rec.FirstFailure.Request.CancellationTag == 0x77, | ||
| 471 | "the record has the request's offset, size and tag"); | ||
| 472 | check(std::wcscmp(rec.FirstFailure.Request.Filename, L"GAME:\\data\\b.bin") == 0, | ||
| 473 | "the record names the file as the title opened it"); | ||
| 474 | check(std::strcmp(rec.FirstFailure.Request.Name, "past-the-end") == 0, "the record has the request's name"); | ||
| 475 | check(WaitForSingleObject(error_event, 0) == WAIT_TIMEOUT, "RetrieveErrorRecord resets the event"); | ||
| 476 | q->RetrieveErrorRecord(&rec); | ||
| 477 | check(rec.FailureCount == 0 && rec.FirstFailure.CommandType == DSTORAGE_COMMAND_TYPE_NONE, | ||
| 478 | "a second RetrieveErrorRecord has nothing"); | ||
| 479 | |||
| 480 | // Status and fence entries that fail. | ||
| 481 | q->EnqueueStatus(s, 1000); | ||
| 482 | q->EnqueueStatus(nullptr, 0); | ||
| 483 | q->EnqueueSignal(nullptr, 1); | ||
| 484 | q->EnqueueStatus(s, 31); | ||
| 485 | q->Submit(); | ||
| 486 | check(wait_complete(s, 31), "the entries after the failed notifications complete"); | ||
| 487 | check_hr(s->GetHResult(31), S_OK, "notification failures do not fail the next status entry"); | ||
| 488 | check_hr(s->GetHResult(1000), E_DSTORAGE_INDEX_BOUND, "GetHResult past the array"); | ||
| 489 | check(s->IsComplete(1000), "IsComplete past the array is not pending"); | ||
| 490 | q->RetrieveErrorRecord(&rec); | ||
| 491 | check(rec.FailureCount == 3 && rec.FirstFailure.CommandType == DSTORAGE_COMMAND_TYPE_STATUS && | ||
| 492 | rec.FirstFailure.HResult == E_DSTORAGE_INDEX_BOUND && rec.FirstFailure.Status.Index == 1000 && | ||
| 493 | rec.FirstFailure.Status.StatusArray == s, | ||
| 494 | "an index past the array is recorded as a STATUS failure"); | ||
| 495 | |||
| 496 | VirtualFree(reserved, 0, MEM_RELEASE); | ||
| 497 | CloseHandle(error_event); | ||
| 498 | s->Release(); | ||
| 499 | q->Release(); | ||
| 500 | b->Release(); | ||
| 501 | big->Release(); | ||
| 502 | } | ||
| 503 | |||
| 504 | void test_fence_order_and_events(Env& env) { | ||
| 505 | IDStorageFileX* b = open(env, L"GAME:\\data\\b.bin"); | ||
| 506 | IDStorageQueueX2* q = make_queue(env, 0x200); | ||
| 507 | TestFence fence; | ||
| 508 | constexpr int kReads = 100; | ||
| 509 | std::vector<std::vector<uint8_t>> outs(kReads, std::vector<uint8_t>(100)); | ||
| 510 | std::atomic<bool> in_place{false}; | ||
| 511 | fence.on_signal = [&](UINT64 v) { | ||
| 512 | bool ok = v == 7; | ||
| 513 | for (int i = 0; i < kReads; ++i) ok = ok && std::memcmp(outs[i].data(), env.b.data() + i * 100, 100) == 0; | ||
| 514 | in_place.store(ok); | ||
| 515 | }; | ||
| 516 | for (int i = 0; i < kReads; ++i) { | ||
| 517 | DSTORAGE_REQUEST r = file_request(b, static_cast<uint64_t>(i) * 100, outs[i].data(), 100); | ||
| 518 | q->EnqueueRequest(&r); | ||
| 519 | } | ||
| 520 | q->EnqueueSignal(&fence, 7); | ||
| 521 | check(fence.refs.load() == 2, "an enqueued signal holds a fence reference"); | ||
| 522 | HANDLE ev = CreateEventW(nullptr, FALSE, FALSE, nullptr); | ||
| 523 | q->EnqueueSetEvent(ev); | ||
| 524 | q->Submit(); | ||
| 525 | check(WaitForSingleObject(ev, 10000) == WAIT_OBJECT_0, "EnqueueSetEvent sets the event"); | ||
| 526 | check(fence.GetCompletedValue() == 7, "EnqueueSignal signals the fence with its value"); | ||
| 527 | check(in_place.load(), "the fence is signalled after every read before it is in place"); | ||
| 528 | check(wait_until([&] { return fence.refs.load() == 1; }), "the fence reference is released after the signal"); | ||
| 529 | |||
| 530 | // A failed request before a fence: the fence still signals, and the status after the fence is clean. | ||
| 531 | IDStorageStatusArrayX* s = make_status(env, 2); | ||
| 532 | std::vector<uint8_t> tmp(8); | ||
| 533 | DSTORAGE_REQUEST bad = file_request(b, 20000, tmp.data(), 8); | ||
| 534 | q->EnqueueRequest(&bad); | ||
| 535 | q->EnqueueSignal(&fence, 8); | ||
| 536 | q->EnqueueStatus(s, 0); | ||
| 537 | q->Submit(); | ||
| 538 | check(wait_complete(s, 0), "the status after a fence completes"); | ||
| 539 | check_hr(s->GetHResult(0), S_OK, "a failure before a fence is not reported by a status after it"); | ||
| 540 | check(fence.GetCompletedValue() == 8, "a fence after a failed read still signals"); | ||
| 541 | CloseHandle(ev); | ||
| 542 | s->Release(); | ||
| 543 | q->Release(); | ||
| 544 | b->Release(); | ||
| 545 | } | ||
| 546 | |||
| 547 | void test_cancel(Env& env) { | ||
| 548 | IDStorageFileX* b = open(env, L"GAME:\\data\\b.bin"); | ||
| 549 | IDStorageQueueX2* q = make_queue(env, 0x80, false); | ||
| 550 | IDStorageStatusArrayX* s = make_status(env, 1); | ||
| 551 | std::vector<std::vector<uint8_t>> outs(6, std::vector<uint8_t>(16, 0)); | ||
| 552 | const uint64_t tags[] = {0x11, 0x12, 0x21, 0x22, 0x31}; | ||
| 553 | for (int i = 0; i < 5; ++i) { | ||
| 554 | DSTORAGE_REQUEST r = file_request(b, 0, outs[i].data(), 16, tags[i]); | ||
| 555 | q->EnqueueRequest(&r); | ||
| 556 | } | ||
| 557 | q->CancelRequestsWithTag(0xF0, 0x10); // 0x11 and 0x12 | ||
| 558 | q->CancelRequestsWithTag(0xFF, 0x22); // 0x22 | ||
| 559 | DSTORAGE_REQUEST later = file_request(b, 0, outs[5].data(), 16, 0x11); // enqueued after: not cancelled | ||
| 560 | q->EnqueueRequest(&later); | ||
| 561 | q->EnqueueStatus(s, 0); | ||
| 562 | q->Submit(); | ||
| 563 | check(wait_complete(s, 0), "cancelled requests complete"); | ||
| 564 | check_hr(s->GetHResult(0), S_OK, "a cancelled request is a success"); | ||
| 565 | const std::vector<uint8_t> zeros(16, 0); | ||
| 566 | check(outs[0] == zeros && outs[1] == zeros && outs[3] == zeros, "cancelled requests read nothing"); | ||
| 567 | check(std::memcmp(outs[2].data(), env.b.data(), 16) == 0 && std::memcmp(outs[4].data(), env.b.data(), 16) == 0, | ||
| 568 | "requests the mask did not match are read"); | ||
| 569 | check(std::memcmp(outs[5].data(), env.b.data(), 16) == 0, "a request enqueued after the cancel is read"); | ||
| 570 | s->Release(); | ||
| 571 | q->Release(); | ||
| 572 | b->Release(); | ||
| 573 | } | ||
| 574 | |||
| 575 | void test_auto_submit_and_full_queue(Env& env) { | ||
| 576 | IDStorageFileX* b = open(env, L"GAME:\\data\\b.bin"); | ||
| 577 | IDStorageQueueX2* q = make_queue(env, 0x80, true); | ||
| 578 | std::vector<std::vector<uint8_t>> outs(64, std::vector<uint8_t>(8, 0)); | ||
| 579 | for (int i = 0; i < 63; ++i) { | ||
| 580 | DSTORAGE_REQUEST r = file_request(b, i, outs[i].data(), 8); | ||
| 581 | q->EnqueueRequest(&r); | ||
| 582 | } | ||
| 583 | DSTORAGE_QUEUE_INFO info{}; | ||
| 584 | q->Query(&info); | ||
| 585 | check(info.RequestCountUntilAutoSubmit == 1, "one entry before the automatic submit"); | ||
| 586 | Sleep(20); | ||
| 587 | check(outs[0] == std::vector<uint8_t>(8, 0), "below half the capacity nothing is submitted"); | ||
| 588 | DSTORAGE_REQUEST r = file_request(b, 63, outs[63].data(), 8); | ||
| 589 | q->EnqueueRequest(&r); // the 64th: half of 0x80 | ||
| 590 | check(wait_until([&] { return std::memcmp(outs[63].data(), env.b.data() + 63, 8) == 0; }), | ||
| 591 | "reaching half the capacity submits without Submit"); | ||
| 592 | bool all = true; | ||
| 593 | for (int i = 0; i < 64; ++i) all = all && std::memcmp(outs[i].data(), env.b.data() + i, 8) == 0; | ||
| 594 | check(all, "every auto-submitted read has its bytes"); | ||
| 595 | q->Release(); | ||
| 596 | |||
| 597 | // Auto-submit disabled and full: an enqueue waits until another thread submits. | ||
| 598 | q = make_queue(env, 0x80, false); | ||
| 599 | IDStorageStatusArrayX* s = make_status(env, 1); | ||
| 600 | constexpr int kMany = 400; | ||
| 601 | std::vector<uint8_t> sink(kMany * 4); | ||
| 602 | std::atomic<bool> done{false}; | ||
| 603 | std::thread producer([&] { | ||
| 604 | for (int i = 0; i < kMany; ++i) { | ||
| 605 | DSTORAGE_REQUEST rr = file_request(b, i, sink.data() + i * 4, 4); | ||
| 606 | q->EnqueueRequest(&rr); | ||
| 607 | } | ||
| 608 | q->EnqueueStatus(s, 0); | ||
| 609 | done.store(true); | ||
| 610 | }); | ||
| 611 | check(wait_until([&] { | ||
| 612 | DSTORAGE_QUEUE_INFO i{}; | ||
| 613 | q->Query(&i); | ||
| 614 | return i.EmptySlotCount == 0; | ||
| 615 | }), | ||
| 616 | "the queue fills to capacity - 1"); | ||
| 617 | Sleep(20); | ||
| 618 | check(!done.load(), "an enqueue into a full queue waits"); | ||
| 619 | while (!done.load()) { | ||
| 620 | q->Submit(); | ||
| 621 | Sleep(1); | ||
| 622 | } | ||
| 623 | producer.join(); | ||
| 624 | q->Submit(); | ||
| 625 | check(wait_complete(s, 0), "the producer's reads complete"); | ||
| 626 | bool same = true; | ||
| 627 | for (int i = 0; i < kMany; ++i) same = same && std::memcmp(sink.data() + i * 4, env.b.data() + i, 4) == 0; | ||
| 628 | check(same, "every read of the full-queue run has its bytes"); | ||
| 629 | s->Release(); | ||
| 630 | q->Release(); | ||
| 631 | b->Release(); | ||
| 632 | } | ||
| 633 | |||
| 634 | void test_memory_queue(Env& env) { | ||
| 635 | IDStorageQueueX2* q = make_queue(env, 0x80, true, DSTORAGE_REQUEST_SOURCE_MEMORY, DSTORAGE_PRIORITY_REALTIME); | ||
| 636 | IDStorageStatusArrayX* s = make_status(env, 6); | ||
| 637 | std::vector<uint8_t> buf = pattern(8192, 9); | ||
| 638 | std::vector<uint8_t> want = buf; | ||
| 639 | std::memmove(want.data() + 1024, want.data(), 4096); | ||
| 640 | DSTORAGE_REQUEST r{}; | ||
| 641 | r.Options.SourceType = DSTORAGE_REQUEST_SOURCE_MEMORY; | ||
| 642 | r.Source = buf.data(); | ||
| 643 | r.Destination = buf.data() + 1024; | ||
| 644 | r.SourceSize = r.DestinationSize = 4096; | ||
| 645 | q->EnqueueRequest(&r); | ||
| 646 | q->EnqueueStatus(s, 0); | ||
| 647 | void* reserved = VirtualAlloc(nullptr, 1 << 16, MEM_RESERVE, PAGE_READWRITE); | ||
| 648 | DSTORAGE_REQUEST bad = r; | ||
| 649 | bad.Destination = reserved; | ||
| 650 | q->EnqueueRequest(&bad); | ||
| 651 | q->EnqueueStatus(s, 1); | ||
| 652 | DSTORAGE_REQUEST file_on_memory{}; // a file request on a memory queue | ||
| 653 | file_on_memory.Destination = buf.data(); | ||
| 654 | file_on_memory.SourceSize = file_on_memory.DestinationSize = 4; | ||
| 655 | q->EnqueueRequest(&file_on_memory); | ||
| 656 | q->EnqueueStatus(s, 2); | ||
| 657 | DSTORAGE_REQUEST huge = r; | ||
| 658 | huge.SourceSize = huge.DestinationSize = (1u << 29) + 1; // more than 1 GiB together | ||
| 659 | q->EnqueueRequest(&huge); | ||
| 660 | q->EnqueueStatus(s, 3); | ||
| 661 | // Buffers over 32 MiB need 64 KiB or 2 MiB pages (DSTORAGE_REQUEST). Reserved memory: a copy that is not refused | ||
| 662 | // faults and reports an access violation instead. | ||
| 663 | constexpr uint32_t k32MiB = 32u << 20; | ||
| 664 | void* wide = VirtualAlloc(nullptr, 2 * (k32MiB + 4096), MEM_RESERVE, PAGE_READWRITE); | ||
| 665 | check(wide != nullptr, "reserve 64 MiB for the over-limit copy"); | ||
| 666 | DSTORAGE_REQUEST over = r; | ||
| 667 | over.Source = wide; | ||
| 668 | over.Destination = static_cast<uint8_t*>(wide) + k32MiB + 4096; | ||
| 669 | over.SourceSize = over.DestinationSize = k32MiB + 1; | ||
| 670 | q->EnqueueRequest(&over); | ||
| 671 | q->EnqueueStatus(s, 4); | ||
| 672 | std::vector<uint8_t> at_limit_source = pattern(k32MiB, 10), at_limit(k32MiB, 0); | ||
| 673 | DSTORAGE_REQUEST limit = r; | ||
| 674 | limit.Source = at_limit_source.data(); | ||
| 675 | limit.Destination = at_limit.data(); | ||
| 676 | limit.SourceSize = limit.DestinationSize = k32MiB; | ||
| 677 | q->EnqueueRequest(&limit); | ||
| 678 | q->EnqueueStatus(s, 5); | ||
| 679 | q->Submit(); | ||
| 680 | check(wait_complete(s, 5), "memory requests complete"); | ||
| 681 | check_hr(s->GetHResult(4), E_DSTORAGE_REQUEST_TOO_LARGE, "an unpaged buffer over 32 MiB is refused untouched"); | ||
| 682 | check_hr(s->GetHResult(5), S_OK, "a 32 MiB unpaged copy"); | ||
| 683 | check(at_limit == at_limit_source, "the 32 MiB copy moved every byte"); | ||
| 684 | VirtualFree(wide, 0, MEM_RELEASE); | ||
| 685 | check_hr(s->GetHResult(0), S_OK, "a memory-to-memory copy"); | ||
| 686 | check(buf == want, "an overlapping memory copy moves like memmove"); | ||
| 687 | check_hr(s->GetHResult(1), E_DSTORAGE_ACCESS_VIOLATION, "a memory copy to memory the process cannot write"); | ||
| 688 | check_hr(s->GetHResult(2), E_DSTORAGE_INVALID_SOURCE_TYPE, "a file request on a memory queue"); | ||
| 689 | check_hr(s->GetHResult(3), E_DSTORAGE_REQUEST_TOO_LARGE, "source plus destination over 1 GiB"); | ||
| 690 | VirtualFree(reserved, 0, MEM_RELEASE); | ||
| 691 | s->Release(); | ||
| 692 | q->Release(); | ||
| 693 | } | ||
| 694 | |||
| 695 | void test_close_and_release(Env& env) { | ||
| 696 | IDStorageFileX* big = open(env, L"GAME:\\data\\big.bin"); | ||
| 697 | // Close with requests in flight: it returns after they all ran. | ||
| 698 | IDStorageQueueX2* q = make_queue(env, 0x100, true); | ||
| 699 | constexpr int kReads = 300; | ||
| 700 | constexpr uint32_t kChunk = 8192; | ||
| 701 | std::vector<uint8_t> out(kReads * kChunk); | ||
| 702 | for (int i = 0; i < kReads; ++i) { | ||
| 703 | DSTORAGE_REQUEST r = file_request(big, static_cast<uint64_t>(i) * kChunk, out.data() + i * kChunk, kChunk); | ||
| 704 | q->EnqueueRequest(&r); | ||
| 705 | } | ||
| 706 | q->Close(); | ||
| 707 | check(std::memcmp(out.data(), env.big.data(), out.size()) == 0, "Close returns after every enqueued read ran"); | ||
| 708 | IDStorageStatusArrayX* s = make_status(env, 1); | ||
| 709 | std::vector<uint8_t> tmp(8, 0); | ||
| 710 | DSTORAGE_REQUEST r = file_request(big, 0, tmp.data(), 8); | ||
| 711 | q->EnqueueRequest(&r); | ||
| 712 | q->EnqueueStatus(s, 0); | ||
| 713 | q->Submit(); | ||
| 714 | q->Close(); // a second Close is harmless | ||
| 715 | Sleep(20); | ||
| 716 | check(tmp == std::vector<uint8_t>(8, 0), "a request enqueued after Close is not run"); | ||
| 717 | DSTORAGE_ERROR_RECORD rec{}; | ||
| 718 | q->RetrieveErrorRecord(&rec); | ||
| 719 | check(rec.FailureCount == 2 && rec.FirstFailure.HResult == E_DSTORAGE_QUEUE_CLOSED, | ||
| 720 | "enqueues after Close are recorded"); | ||
| 721 | check(q->Release() == 0, "a closed queue is freed by its last Release"); | ||
| 722 | |||
| 723 | // The last Release with requests in flight finishes them first. | ||
| 724 | q = make_queue(env, 0x100, true); | ||
| 725 | std::fill(out.begin(), out.end(), uint8_t{0}); | ||
| 726 | for (int i = 0; i < kReads; ++i) { | ||
| 727 | DSTORAGE_REQUEST rr = file_request(big, static_cast<uint64_t>(i) * kChunk, out.data() + i * kChunk, kChunk); | ||
| 728 | q->EnqueueRequest(&rr); | ||
| 729 | } | ||
| 730 | q->Submit(); | ||
| 731 | big->Release(); // the requests hold the file | ||
| 732 | check(q->Release() == 0, "the last Release of a busy queue"); | ||
| 733 | check(std::memcmp(out.data(), env.big.data(), out.size()) == 0, "the last Release ran the reads in flight"); | ||
| 734 | |||
| 735 | // IDStorageFileX::Close: submitted reads finish, a later one fails with E_DSTORAGE_FILE_NOT_OPEN. | ||
| 736 | IDStorageFileX* b = open(env, L"GAME:\\data\\b.bin"); | ||
| 737 | q = make_queue(env, 0x80, false); | ||
| 738 | IDStorageStatusArrayX* s2 = make_status(env, 3); | ||
| 739 | std::vector<uint8_t> first(100, 0), second(100, 0); | ||
| 740 | DSTORAGE_REQUEST r1 = file_request(b, 0, first.data(), 100); | ||
| 741 | q->EnqueueRequest(&r1); | ||
| 742 | q->EnqueueStatus(s2, 0); | ||
| 743 | q->Submit(); | ||
| 744 | DSTORAGE_REQUEST r2 = file_request(b, 0, second.data(), 100); | ||
| 745 | q->EnqueueRequest(&r2); // not submitted yet | ||
| 746 | b->Close(); | ||
| 747 | check(std::memcmp(first.data(), env.b.data(), 100) == 0, | ||
| 748 | "IDStorageFileX::Close waits for the file's submitted reads"); | ||
| 749 | check(wait_complete(s2, 0) && s2->GetHResult(0) == S_OK, "the submitted read succeeded"); | ||
| 750 | check(b->GetHandle() == INVALID_HANDLE_VALUE, "GetHandle on a closed file"); | ||
| 751 | q->EnqueueStatus(s2, 1); | ||
| 752 | DSTORAGE_REQUEST r3 = file_request(b, 0, second.data(), 0); | ||
| 753 | q->EnqueueRequest(&r3); | ||
| 754 | q->EnqueueStatus(s2, 2); | ||
| 755 | q->Submit(); | ||
| 756 | check(wait_complete(s2, 2), "the reads after the close complete"); | ||
| 757 | check_hr(s2->GetHResult(1), E_DSTORAGE_FILE_NOT_OPEN, "a read of a closed file"); | ||
| 758 | check_hr(s2->GetHResult(2), E_DSTORAGE_FILE_NOT_OPEN, "a zero-size read of a closed file"); | ||
| 759 | check(second == std::vector<uint8_t>(100, 0), "a read of a closed file writes nothing"); | ||
| 760 | q->Release(); | ||
| 761 | b->Release(); | ||
| 762 | s->Release(); | ||
| 763 | s2->Release(); | ||
| 764 | } | ||
| 765 | |||
| 766 | void test_threads(Env& env) { | ||
| 767 | IDStorageFileX* big = open(env, L"GAME:\\data\\big.bin"); | ||
| 768 | IDStorageQueueX2* q = make_queue(env, 0x80, true); | ||
| 769 | constexpr int kThreads = 4, kPer = 250; | ||
| 770 | constexpr uint32_t kChunk = 512; | ||
| 771 | std::vector<std::vector<uint8_t>> outs(kThreads, std::vector<uint8_t>(kPer * kChunk)); | ||
| 772 | std::vector<IDStorageStatusArrayX*> arrays; | ||
| 773 | for (int t = 0; t < kThreads; ++t) arrays.push_back(make_status(env, 1)); | ||
| 774 | std::vector<std::thread> threads; | ||
| 775 | for (int t = 0; t < kThreads; ++t) | ||
| 776 | threads.emplace_back([&, t] { | ||
| 777 | for (int i = 0; i < kPer; ++i) { | ||
| 778 | const uint64_t offset = (static_cast<uint64_t>(t) * kPer + i) * kChunk % (env.big.size() - kChunk); | ||
| 779 | DSTORAGE_REQUEST r = file_request(big, offset, outs[t].data() + i * kChunk, kChunk, t); | ||
| 780 | q->EnqueueRequest(&r); | ||
| 781 | } | ||
| 782 | q->EnqueueStatus(arrays[t], 0); | ||
| 783 | q->Submit(); | ||
| 784 | }); | ||
| 785 | for (auto& th : threads) th.join(); | ||
| 786 | bool ok = true; | ||
| 787 | for (int t = 0; t < kThreads; ++t) { | ||
| 788 | ok = ok && wait_complete(arrays[t], 0) && arrays[t]->GetHResult(0) == S_OK; | ||
| 789 | for (int i = 0; i < kPer; ++i) { | ||
| 790 | const uint64_t offset = (static_cast<uint64_t>(t) * kPer + i) * kChunk % (env.big.size() - kChunk); | ||
| 791 | ok = ok && std::memcmp(outs[t].data() + i * kChunk, env.big.data() + offset, kChunk) == 0; | ||
| 792 | } | ||
| 793 | } | ||
| 794 | check(ok, "four threads enqueueing at once all read their bytes"); | ||
| 795 | for (auto* a : arrays) a->Release(); | ||
| 796 | q->Release(); | ||
| 797 | big->Release(); | ||
| 798 | } | ||
| 799 | |||
| 800 | void test_iid_policy_events_and_cancel(Env& env) { | ||
| 801 | // Well-known system interfaces and made-up ids: QueryInterface refuses all of them. | ||
| 802 | const GUID kRandom = {0x5eed0001, 0x2222, 0x4222, {0x82, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x01}}; | ||
| 803 | const GUID kIMarshal = {0x00000003, 0x0000, 0x0000, {0xc0, 0, 0, 0, 0, 0, 0, 0x46}}; | ||
| 804 | const GUID kIAgileObject = {0x94ea2b94, 0xe9cc, 0x49e0, {0xc0, 0xff, 0xee, 0x64, 0xca, 0x8f, 0x5b, 0x90}}; | ||
| 805 | const GUID kINoMarshal = {0xecc8691b, 0xc1db, 0x4dc0, {0x85, 0x5e, 0x65, 0xf6, 0xc5, 0x51, 0xaf, 0x49}}; | ||
| 806 | const GUID kIClientSecurity = {0x0000013d, 0x0000, 0x0000, {0xc0, 0, 0, 0, 0, 0, 0, 0x46}}; | ||
| 807 | const GUID kPcStatusArrayId = {0x82397587, 0x7cd5, 0x453b, {0xa0, 0x2e, 0x31, 0x37, 0x9b, 0xd6, 0x46, 0x56}}; | ||
| 808 | const GUID kPcQueueId = {0xcfdbd83f, 0x9e06, 0x4fda, {0x8e, 0xa5, 0x69, 0x04, 0x21, 0x37, 0xf4, 0x9b}}; | ||
| 809 | const GUID kQueueId2 = {0x0d5e0012, 0x1111, 0x4111, {0x81, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x12}}; | ||
| 810 | const GUID refused[] = {kRandom, kIMarshal, kIAgileObject, kINoMarshal, kIClientSecurity}; | ||
| 811 | |||
| 812 | IDStorageQueueX2* q = make_queue(env, 0x80, false); | ||
| 813 | IDStorageFileX* b = open(env, L"GAME:\\data\\b.bin"); | ||
| 814 | IDStorageStatusArrayX* s = make_status(env, 8); | ||
| 815 | IUnknown* objects[] = {env.factory, q, b, s}; | ||
| 816 | for (IUnknown* o : objects) | ||
| 817 | for (const GUID& g : refused) { | ||
| 818 | void* out = reinterpret_cast<void*>(1); | ||
| 819 | check_hr(o->QueryInterface(g, &out), E_NOINTERFACE, "QueryInterface refuses an unknown or system id"); | ||
| 820 | check(out == nullptr, "a refused QueryInterface clears ppv"); | ||
| 821 | } | ||
| 822 | void* out = nullptr; | ||
| 823 | check_hr(q->QueryInterface(kFileId, &out), E_NOINTERFACE, "a queue refuses a file id"); | ||
| 824 | check_hr(b->QueryInterface(kQueueId, &out), E_NOINTERFACE, "a file refuses a queue id"); | ||
| 825 | check_hr(s->QueryInterface(kFactoryId, &out), E_NOINTERFACE, "a status array refuses a factory id"); | ||
| 826 | check_hr(q->QueryInterface(__uuidof(IUnknown), &out), S_OK, "QueryInterface(IUnknown)"); | ||
| 827 | if (out) static_cast<IUnknown*>(out)->Release(); | ||
| 828 | |||
| 829 | // Creating calls learn a new id but refuse system ones. | ||
| 830 | auto get = reinterpret_cast<DStorageGetFactoryFn>(rt::shims::find("dstorage_xs.dll", "DStorageGetFactory")); | ||
| 831 | check_hr(get(kIMarshal, &out), E_NOINTERFACE, "DStorageGetFactory refuses IMarshal"); | ||
| 832 | check_hr(get(kIAgileObject, &out), E_NOINTERFACE, "DStorageGetFactory refuses IAgileObject"); | ||
| 833 | DSTORAGE_QUEUE_DESC1 d{}; | ||
| 834 | d.Capacity = 0x80; | ||
| 835 | check_hr(env.factory->CreateQueue1(&d, kINoMarshal, &out), E_NOINTERFACE, "CreateQueue1 refuses INoMarshal"); | ||
| 836 | check_hr(env.factory->OpenFile(L"GAME:\\data\\b.bin", kIClientSecurity, &out), E_NOINTERFACE, | ||
| 837 | "OpenFile refuses IClientSecurity"); | ||
| 838 | check_hr(env.factory->CreateQueue1(&d, kPcQueueId, &out), E_NOINTERFACE, "CreateQueue1 refuses the PC queue id"); | ||
| 839 | check_hr(env.factory->CreateQueue1(&d, kPcStatusArrayId, &out), E_NOINTERFACE, | ||
| 840 | "the status array id is not a queue id"); | ||
| 841 | check_hr(env.factory->CreateQueue1(&d, kQueueId2, &out), S_OK, "a creating call learns a new queue id"); | ||
| 842 | if (out) static_cast<IUnknown*>(out)->Release(); | ||
| 843 | check_hr(q->QueryInterface(kQueueId2, &out), S_OK, "QueryInterface accepts an id a queue was created with"); | ||
| 844 | if (out) static_cast<IUnknown*>(out)->Release(); | ||
| 845 | |||
| 846 | // The PC IDStorageStatusArray id has the console interface's shape and is accepted. | ||
| 847 | check_hr(env.factory->CreateStatusArray(2, nullptr, kPcStatusArrayId, &out), S_OK, | ||
| 848 | "CreateStatusArray accepts the PC IDStorageStatusArray id"); | ||
| 849 | if (out) static_cast<IUnknown*>(out)->Release(); | ||
| 850 | check_hr(s->QueryInterface(kPcStatusArrayId, &out), S_OK, "a status array answers the PC IDStorageStatusArray id"); | ||
| 851 | if (out) static_cast<IUnknown*>(out)->Release(); | ||
| 852 | |||
| 853 | // EnqueueSetEvent duplicates the handle: closing the title's handle, even with its value reused, changes nothing. | ||
| 854 | HANDLE first = CreateEventW(nullptr, TRUE, FALSE, nullptr); | ||
| 855 | q->EnqueueSetEvent(first); | ||
| 856 | CloseHandle(first); | ||
| 857 | HANDLE second = CreateEventW(nullptr, TRUE, FALSE, nullptr); // may get the same handle value | ||
| 858 | q->EnqueueSetEvent(reinterpret_cast<HANDLE>(static_cast<uintptr_t>(0x7ffffff0))); // not a handle | ||
| 859 | q->EnqueueSetEvent(nullptr); | ||
| 860 | q->EnqueueStatus(s, 0); | ||
| 861 | q->Submit(); | ||
| 862 | check(wait_complete(s, 0), "the event entries complete"); | ||
| 863 | check(WaitForSingleObject(second, 0) == WAIT_TIMEOUT, "a closed event's reused handle value is not set"); | ||
| 864 | DSTORAGE_ERROR_RECORD rec{}; | ||
| 865 | q->RetrieveErrorRecord(&rec); | ||
| 866 | check(rec.FailureCount == 2 && rec.FirstFailure.CommandType == kCommandTypeEvent && | ||
| 867 | FAILED(rec.FirstFailure.HResult), | ||
| 868 | "an invalid and a null event handle are recorded as EVENT failures; the closed one is not"); | ||
| 869 | CloseHandle(second); | ||
| 870 | |||
| 871 | // A cancelled request that also had an enqueue error is still a success, and not counted as a failure. | ||
| 872 | std::vector<uint8_t> tmp(8, 0); | ||
| 873 | DSTORAGE_REQUEST foreign = file_request(reinterpret_cast<IDStorageFileX*>(s), 0, tmp.data(), 8, 0x99); | ||
| 874 | q->EnqueueRequest(&foreign); | ||
| 875 | DSTORAGE_REQUEST null_file = file_request(nullptr, 0, tmp.data(), 8, 0x99); | ||
| 876 | q->EnqueueRequest(&null_file); | ||
| 877 | q->CancelRequestsWithTag(~0ull, 0x99); | ||
| 878 | q->EnqueueStatus(s, 1); | ||
| 879 | q->Submit(); | ||
| 880 | check(wait_complete(s, 1), "cancelled bad requests complete"); | ||
| 881 | check_hr(s->GetHResult(1), S_OK, "a cancelled request with an enqueue error is a success"); | ||
| 882 | q->RetrieveErrorRecord(&rec); | ||
| 883 | check(rec.FailureCount == 0, "a cancelled request is not counted as a failure"); | ||
| 884 | |||
| 885 | s->Release(); | ||
| 886 | b->Release(); | ||
| 887 | q->Release(); | ||
| 888 | } | ||
| 889 | |||
| 890 | // Holds the queue's worker inside a copy: the copy's source is a guard page, and this handler waits on the first | ||
| 891 | // touch until the test releases it. The guard is gone after that fault, so the copy then goes on. | ||
| 892 | struct GuardHold { | ||
| 893 | uint8_t* page = nullptr; | ||
| 894 | HANDLE entered = nullptr; | ||
| 895 | HANDLE release = nullptr; | ||
| 896 | }; | ||
| 897 | GuardHold* g_hold = nullptr; | ||
| 898 | |||
| 899 | LONG CALLBACK hold_on_guard(EXCEPTION_POINTERS* x) { | ||
| 900 | const EXCEPTION_RECORD* e = x->ExceptionRecord; | ||
| 901 | if (!g_hold || e->ExceptionCode != STATUS_GUARD_PAGE_VIOLATION || e->NumberParameters < 2) | ||
| 902 | return EXCEPTION_CONTINUE_SEARCH; | ||
| 903 | const auto* address = reinterpret_cast<const uint8_t*>(e->ExceptionInformation[1]); | ||
| 904 | if (address < g_hold->page || address >= g_hold->page + 4096) return EXCEPTION_CONTINUE_SEARCH; | ||
| 905 | SetEvent(g_hold->entered); | ||
| 906 | WaitForSingleObject(g_hold->release, 10000); | ||
| 907 | return EXCEPTION_CONTINUE_EXECUTION; | ||
| 908 | } | ||
| 909 | |||
| 910 | // DSTORAGE_PRIORITY_STATS: TotalRequestCount and the byte totals count submitted requests; PendingRequestCount counts | ||
| 911 | // requests whose processing has started and not completed. A memory queue (realtime, the only priority it may have): | ||
| 912 | // the counts are compared with the ones before. | ||
| 913 | void test_stats_timing(Env& env) { | ||
| 914 | auto realtime = [&] { | ||
| 915 | DSTORAGE_STATS st{}; | ||
| 916 | env.factory->GetStats(&st); | ||
| 917 | return st.RealtimePriorityStats; | ||
| 918 | }; | ||
| 919 | const DSTORAGE_PRIORITY_STATS before = realtime(); | ||
| 920 | IDStorageQueueX2* q = make_queue(env, 0x80, false, DSTORAGE_REQUEST_SOURCE_MEMORY, DSTORAGE_PRIORITY_REALTIME); | ||
| 921 | if (!q) return; | ||
| 922 | IDStorageStatusArrayX* s = make_status(env, 1); | ||
| 923 | GuardHold hold; | ||
| 924 | hold.page = static_cast<uint8_t*>(VirtualAlloc(nullptr, 4096, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE)); | ||
| 925 | hold.entered = CreateEventW(nullptr, TRUE, FALSE, nullptr); | ||
| 926 | hold.release = CreateEventW(nullptr, TRUE, FALSE, nullptr); | ||
| 927 | if (!hold.page || !hold.entered || !hold.release) { | ||
| 928 | check(false, "allocate the guard page and its events"); | ||
| 929 | return; | ||
| 930 | } | ||
| 931 | std::memset(hold.page, 0x5A, 4096); | ||
| 932 | DWORD old = 0; | ||
| 933 | VirtualProtect(hold.page, 4096, PAGE_READWRITE | PAGE_GUARD, &old); | ||
| 934 | std::vector<uint8_t> out(4096, 0); | ||
| 935 | DSTORAGE_REQUEST r{}; | ||
| 936 | r.Options.SourceType = DSTORAGE_REQUEST_SOURCE_MEMORY; | ||
| 937 | r.Source = hold.page; | ||
| 938 | r.Destination = out.data(); | ||
| 939 | r.SourceSize = r.DestinationSize = 4096; | ||
| 940 | q->EnqueueRequest(&r); | ||
| 941 | q->EnqueueStatus(s, 0); | ||
| 942 | DSTORAGE_PRIORITY_STATS p = realtime(); | ||
| 943 | check(p.TotalRequestCount == before.TotalRequestCount && | ||
| 944 | p.TotalRequestSourceByteCount == before.TotalRequestSourceByteCount && | ||
| 945 | p.TotalRequestDestinationByteCount == before.TotalRequestDestinationByteCount, | ||
| 946 | "an enqueued request is not counted before Submit"); | ||
| 947 | |||
| 948 | g_hold = &hold; | ||
| 949 | void* handler = AddVectoredExceptionHandler(1, hold_on_guard); | ||
| 950 | q->Submit(); | ||
| 951 | check(WaitForSingleObject(hold.entered, 10000) == WAIT_OBJECT_0, "the copy reached the guard page"); | ||
| 952 | p = realtime(); | ||
| 953 | check(p.TotalRequestCount == before.TotalRequestCount + 1 && | ||
| 954 | p.TotalRequestSourceByteCount == before.TotalRequestSourceByteCount + 4096 && | ||
| 955 | p.TotalRequestDestinationByteCount == before.TotalRequestDestinationByteCount + 4096, | ||
| 956 | "a submitted request is counted, with its sizes"); | ||
| 957 | check(p.PendingRequestCount == before.PendingRequestCount + 1, "a running request is pending"); | ||
| 958 | SetEvent(hold.release); | ||
| 959 | check(wait_complete(s, 0), "the held copy completes"); | ||
| 960 | check_hr(s->GetHResult(0), S_OK, "the held copy succeeds"); | ||
| 961 | check(out == std::vector<uint8_t>(4096, 0x5A), "the held copy moved every byte"); | ||
| 962 | check(realtime().PendingRequestCount == before.PendingRequestCount, "a completed request is no longer pending"); | ||
| 963 | RemoveVectoredExceptionHandler(handler); | ||
| 964 | g_hold = nullptr; | ||
| 965 | CloseHandle(hold.entered); | ||
| 966 | CloseHandle(hold.release); | ||
| 967 | VirtualFree(hold.page, 0, MEM_RELEASE); | ||
| 968 | s->Release(); | ||
| 969 | q->Release(); | ||
| 970 | } | ||
| 971 | |||
| 972 | void test_stats(Env& env) { | ||
| 973 | DSTORAGE_STATS st{}; | ||
| 974 | env.factory->GetStats(&st); | ||
| 975 | check(st.CurrentOpenFileCount == 0, "every file was released"); | ||
| 976 | check(st.NormalPriorityStats.CurrentQueueCount == 0 && st.RealtimePriorityStats.CurrentQueueCount == 0, | ||
| 977 | "every queue was released"); | ||
| 978 | check(st.NormalPriorityStats.TotalRequestCount > 1000 && st.NormalPriorityStats.TotalFenceCount >= 2 && | ||
| 979 | st.RealtimePriorityStats.TotalRequestCount == 7, | ||
| 980 | "requests and fences are counted per priority"); | ||
| 981 | check(st.CumulativeOpenFileCount >= 10 && st.MaxOpenFileCount >= 3, "files are counted"); | ||
| 982 | check(st.ErrorCount > 10, "errors are counted"); | ||
| 983 | } | ||
| 984 | |||
| 985 | |||
| 986 | // ---- zlib requests ------------------------------------------------------------------------------------------------ | ||
| 987 | // | ||
| 988 | // Synthetic streams only. The two Huffman streams were made with Python's zlib (3.14, zlib-ng 1.3.1 API) from the | ||
| 989 | // text zlib_text() builds, which in Python is | ||
| 990 | // text = lambda n: b"".join(b"line %04d: a zlib request for the queue, value %d\n" % (i, (i * i) % 97) | ||
| 991 | // for i in range(n)) | ||
| 992 | // kZlibDynamic = zlib.compress(text(64), 9): one final dynamic-Huffman block (BTYPE 2), 345 bytes for 3,191. | ||
| 993 | // kZlibFixed = zlib.compress(text(4), 1): one final fixed-Huffman block (BTYPE 1), 77 bytes for 196. | ||
| 994 | // Streams with stored blocks (BTYPE 0) are built here by stored_stream(); for a single block that is byte for byte | ||
| 995 | // what zlib.compress(data, 0) gives. | ||
| 996 | constexpr uint8_t kZlibDynamic[] = { | ||
| 997 | 0x78, 0xda, 0x95, 0xd4, 0x4d, 0x4e, 0x03, 0x31, 0x0c, 0x05, 0xe0, 0x3d, 0xa7, 0xc8, 0x01, 0x58, 0x4c, 0xfc, | ||
| 998 | 0x97, 0x84, 0xdb, 0x14, 0x69, 0x10, 0x95, 0x46, 0x20, 0x4a, 0xcb, 0x82, 0xd3, 0x33, 0x0b, 0xf4, 0xbc, 0x7d, | ||
| 999 | 0xee, 0x6e, 0xa4, 0x3e, 0x25, 0xce, 0x67, 0xfb, 0xb8, 0x7e, 0xec, 0x6d, 0x3b, 0x7f, 0x2f, 0xed, 0xd2, 0x7e, | ||
| 1000 | 0x8f, 0xeb, 0x6b, 0xbb, 0xed, 0x5f, 0x8f, 0xfd, 0xfb, 0xde, 0xde, 0x3e, 0x6f, 0xed, 0xfe, 0xbe, 0xb7, 0xf3, | ||
| 1001 | 0xeb, 0xb1, 0x3f, 0xb7, 0x9f, 0xcb, 0xf1, 0x38, 0xff, 0xf9, 0x74, 0xfc, 0x07, 0x3a, 0x17, 0xe8, 0x08, 0x08, | ||
| 1002 | 0x17, 0x30, 0x04, 0x94, 0x0b, 0x2c, 0x04, 0x8c, 0xbc, 0x52, 0x20, 0xe1, 0x5c, 0x42, 0x1c, 0x89, 0xe0, 0x12, | ||
| 1003 | 0x9a, 0x67, 0x0c, 0xb2, 0xee, 0xac, 0x63, 0x72, 0x89, 0xc8, 0xa7, 0x5a, 0x5c, 0x62, 0x42, 0xa3, 0x93, 0xde, | ||
| 1004 | 0x8a, 0x00, 0xe9, 0x2d, 0xb8, 0x54, 0x67, 0xc1, 0x07, 0x12, 0xa4, 0xf8, 0x10, 0x24, 0x48, 0xf2, 0x0c, 0x90, | ||
| 1005 | 0xe2, 0x9a, 0x2f, 0x45, 0x8a, 0x47, 0x9e, 0x41, 0x8a, 0x2f, 0x74, 0x55, 0x27, 0xc5, 0x35, 0x39, 0x48, 0xf1, | ||
| 1006 | 0x81, 0x81, 0x15, 0x52, 0xbc, 0xa3, 0x0e, 0x21, 0xc9, 0x1d, 0xb7, 0x12, 0x92, 0x7c, 0x61, 0x3a, 0x84, 0x24, | ||
| 1007 | 0x37, 0xb4, 0x95, 0x90, 0xe4, 0x0b, 0x82, 0x42, 0x9a, 0x5b, 0xd6, 0x41, 0x9a, 0xaf, 0xbc, 0x15, 0x69, 0xee, | ||
| 1008 | 0xe9, 0x41, 0x9a, 0x4f, 0x04, 0x48, 0xf2, 0x40, 0x5b, 0x29, 0x49, 0x2e, 0x98, 0x40, 0x25, 0xc9, 0x27, 0x6e, | ||
| 1009 | 0xa5, 0x24, 0xb9, 0xe3, 0xa9, 0x94, 0x24, 0x17, 0x34, 0xa2, 0x92, 0xe4, 0x13, 0x2b, 0x54, 0x49, 0xf2, 0x40, | ||
| 1010 | 0x93, 0x28, 0xbb, 0xd8, 0xf3, 0x75, 0x49, 0xf2, 0x9e, 0x67, 0xb0, 0xe4, 0x18, 0x0f, 0x65, 0xcd, 0x91, 0x30, | ||
| 1011 | 0xd2, 0xdc, 0x20, 0x68, 0xa4, 0xb9, 0xc2, 0xc3, 0x48, 0xf3, 0x9e, 0x67, 0x90, 0xe6, 0x59, 0x06, 0x3b, 0xe5, | ||
| 1012 | 0x98, 0x59, 0x23, 0xc9, 0x27, 0x00, 0x8d, 0x24, 0x1f, 0x68, 0x2b, 0x23, 0xc9, 0x47, 0x9e, 0x41, 0x92, 0x8f, | ||
| 1013 | 0xac, 0x63, 0x55, 0x13, 0xbe, 0x55, 0x6f, 0xe5, 0xbd, 0x5a, 0xb9, 0x4b, 0xf5, 0x75, 0x5d, 0xab, 0x82, 0x6e, | ||
| 1014 | 0xc5, 0x26, 0x71, 0xaf, 0xf6, 0xa1, 0x47, 0xb5, 0xd7, 0x7d, 0x54, 0xe7, 0xc9, 0x67, 0x75, 0x66, 0x7d, 0x55, | ||
| 1015 | 0xf7, 0x42, 0x6c, 0xd5, 0xdd, 0x13, 0xbd, 0xba, 0xdf, 0x42, 0xaa, 0x3b, 0x34, 0x94, 0xde, 0xd3, 0x7f, 0x97, | ||
| 1016 | 0x8b, 0x11, 0xce, | ||
| 1017 | }; | ||
| 1018 | constexpr uint8_t kZlibFixed[] = { | ||
| 1019 | 0x78, 0x01, 0xcb, 0xc9, 0xcc, 0x4b, 0x55, 0x30, 0x30, 0x30, 0x30, 0xb0, 0x52, 0x48, 0x54, 0xa8, 0xca, 0xc9, | ||
| 1020 | 0x4c, 0x52, 0x28, 0x4a, 0x2d, 0x2c, 0x4d, 0x2d, 0x2e, 0x51, 0x48, 0xcb, 0x2f, 0x52, 0x28, 0xc9, 0x48, 0x55, | ||
| 1021 | 0x28, 0x2c, 0x4d, 0x2d, 0x4d, 0xd5, 0x51, 0x28, 0x4b, 0xcc, 0x29, 0x4d, 0x55, 0x30, 0xe0, 0x82, 0x69, 0x30, | ||
| 1022 | 0x24, 0x4e, 0x83, 0x21, 0x5c, 0x83, 0x11, 0x71, 0x1a, 0x4c, 0xe0, 0x1a, 0x8c, 0x89, 0xd3, 0x60, 0xc9, 0x05, | ||
| 1023 | 0x00, 0xb0, 0x6e, 0x40, 0x49, | ||
| 1024 | }; | ||
| 1025 | |||
| 1026 | std::vector<uint8_t> zlib_text(int lines) { | ||
| 1027 | std::string t; | ||
| 1028 | char line[96]; | ||
| 1029 | for (int i = 0; i < lines; ++i) { | ||
| 1030 | std::snprintf(line, sizeof line, "line %04d: a zlib request for the queue, value %d\n", i, (i * i) % 97); | ||
| 1031 | t += line; | ||
| 1032 | } | ||
| 1033 | return {t.begin(), t.end()}; | ||
| 1034 | } | ||
| 1035 | |||
| 1036 | uint32_t adler32(const std::vector<uint8_t>& data) { | ||
| 1037 | uint32_t a = 1, b = 0; | ||
| 1038 | for (const uint8_t x : data) { | ||
| 1039 | a = (a + x) % 65521; | ||
| 1040 | b = (b + a) % 65521; | ||
| 1041 | } | ||
| 1042 | return (b << 16) | a; | ||
| 1043 | } | ||
| 1044 | |||
| 1045 | // A zlib stream (header 78 01) whose first block stores `stored` (a final block when there is no tail), followed by | ||
| 1046 | // the deflate blocks of the zlib stream `tail` (its header and Adler-32 dropped: a stored block ends on a byte | ||
| 1047 | // boundary, so they follow as they are) and the Adler-32 of the whole result, `stored` then `tail_plain`. | ||
| 1048 | std::vector<uint8_t> stored_stream(const std::vector<uint8_t>& stored, const uint8_t* tail = nullptr, | ||
| 1049 | size_t tail_size = 0, const std::vector<uint8_t>& tail_plain = {}) { | ||
| 1050 | const auto n = static_cast<uint16_t>(stored.size()); | ||
| 1051 | std::vector<uint8_t> z = {0x78, 0x01, static_cast<uint8_t>(tail ? 0 : 1), static_cast<uint8_t>(n), | ||
| 1052 | static_cast<uint8_t>(n >> 8), static_cast<uint8_t>(~n), static_cast<uint8_t>(~n >> 8)}; | ||
| 1053 | z.insert(z.end(), stored.begin(), stored.end()); | ||
| 1054 | if (tail) z.insert(z.end(), tail + 2, tail + tail_size - 4); | ||
| 1055 | std::vector<uint8_t> plain = stored; | ||
| 1056 | plain.insert(plain.end(), tail_plain.begin(), tail_plain.end()); | ||
| 1057 | const uint32_t check = adler32(plain); | ||
| 1058 | for (int shift = 24; shift >= 0; shift -= 8) z.push_back(static_cast<uint8_t>(check >> shift)); | ||
| 1059 | return z; | ||
| 1060 | } | ||
| 1061 | |||
| 1062 | void test_zlib(Env& env) { | ||
| 1063 | const std::vector<uint8_t> dynamic(std::begin(kZlibDynamic), std::end(kZlibDynamic)); | ||
| 1064 | const std::vector<uint8_t> fixed(std::begin(kZlibFixed), std::end(kZlibFixed)); | ||
| 1065 | const std::vector<uint8_t> dynamic_plain = zlib_text(64), fixed_plain = zlib_text(4); | ||
| 1066 | // The first block header is the low three bits after the two header bytes: BFINAL, then BTYPE (RFC 1951 3.2.3). | ||
| 1067 | check(dynamic.size() == 345 && (dynamic[2] & 7) == (1 | 2 << 1), "kZlibDynamic is one final dynamic block"); | ||
| 1068 | check(fixed.size() == 77 && (fixed[2] & 7) == (1 | 1 << 1), "kZlibFixed is one final fixed-Huffman block"); | ||
| 1069 | check(dynamic_plain.size() == 3191 && fixed_plain.size() == 196, "the synthetic text has the generator's sizes"); | ||
| 1070 | |||
| 1071 | const std::vector<uint8_t> stored_part = pattern(300, 21); | ||
| 1072 | const std::vector<uint8_t> mixed = stored_stream(stored_part, kZlibDynamic, sizeof kZlibDynamic, dynamic_plain); | ||
| 1073 | std::vector<uint8_t> mixed_plain = stored_part; | ||
| 1074 | mixed_plain.insert(mixed_plain.end(), dynamic_plain.begin(), dynamic_plain.end()); | ||
| 1075 | check((mixed[2] & 7) == 0, "the mixed stream starts with a stored block that is not the last"); | ||
| 1076 | const std::vector<uint8_t> stored_only = stored_stream(pattern(40, 22)); // larger than what it inflates to | ||
| 1077 | std::vector<uint8_t> bad_method = dynamic, bad_type = fixed, bad_check = dynamic, preset = dynamic; | ||
| 1078 | bad_method[0] ^= 1; // CM 9: not deflate | ||
| 1079 | preset[1] = 0xbb; // 78 BB: a valid check value with FDICT set (a preset dictionary) | ||
| 1080 | bad_type[2] |= 0x06; // BTYPE 3, reserved | ||
| 1081 | bad_check.back() ^= 1; // the Adler-32 | ||
| 1082 | |||
| 1083 | // One file, every stream at a 16-byte aligned offset and followed by 32 zero bytes. | ||
| 1084 | std::vector<uint8_t> file; | ||
| 1085 | auto put = [&](const std::vector<uint8_t>& z) { | ||
| 1086 | const uint64_t at = file.size(); | ||
| 1087 | file.insert(file.end(), z.begin(), z.end()); | ||
| 1088 | file.resize((file.size() + 32 + 15) & ~size_t{15}, 0); | ||
| 1089 | return at; | ||
| 1090 | }; | ||
| 1091 | const uint64_t at_dynamic = put(dynamic), at_fixed = put(fixed), at_mixed = put(mixed), | ||
| 1092 | at_stored = put(stored_only), at_method = put(bad_method), at_preset = put(preset), | ||
| 1093 | at_type = put(bad_type), at_check = put(bad_check); | ||
| 1094 | write_file(env.game / L"data" / L"zlib.bin", file); | ||
| 1095 | |||
| 1096 | struct Case { | ||
| 1097 | uint64_t offset; | ||
| 1098 | uint32_t source, destination; | ||
| 1099 | HRESULT want; | ||
| 1100 | const std::vector<uint8_t>* result; // the expected bytes; null when the destination stays untouched | ||
| 1101 | const char* what; | ||
| 1102 | uint64_t options = 0; | ||
| 1103 | uint32_t intermediate = 0; | ||
| 1104 | }; | ||
| 1105 | const auto z = static_cast<uint32_t>(dynamic.size()); | ||
| 1106 | std::vector<Case> cases = { | ||
| 1107 | {at_dynamic, z, 3191, S_OK, &dynamic_plain, "a zlib file request: dynamic Huffman"}, | ||
| 1108 | {at_fixed, 77, 196, S_OK, &fixed_plain, "a zlib file request: fixed Huffman"}, | ||
| 1109 | {at_mixed, static_cast<uint32_t>(mixed.size()), 3491, S_OK, &mixed_plain, | ||
| 1110 | "a zlib file request: a stored block, then dynamic Huffman"}, | ||
| 1111 | {at_dynamic, z + 16, 3191, S_OK, &dynamic_plain, "bytes after the Adler-32 are not read"}, | ||
| 1112 | {at_stored, static_cast<uint32_t>(stored_only.size()), 40, E_DSTORAGE_INVALID_DESTINATION_SIZE, nullptr, | ||
| 1113 | "a zlib DestinationSize not larger than SourceSize"}, | ||
| 1114 | {at_method, z, 3191, E_DSTORAGE_ZLIB_BAD_HEADER, nullptr, "a header that is not deflate"}, | ||
| 1115 | {at_preset, z, 3191, E_DSTORAGE_ZLIB_BAD_HEADER, nullptr, "a header with a preset dictionary"}, | ||
| 1116 | {at_type, 77, 196, E_DSTORAGE_ZLIB_BAD_DATA, nullptr, "a reserved block type"}, | ||
| 1117 | {at_dynamic, z - 10, 3191, E_DSTORAGE_ZLIB_BAD_DATA, nullptr, "a truncated stream"}, | ||
| 1118 | {at_check, z, 3191, E_DSTORAGE_ZLIB_PARITY_FAIL, nullptr, "an Adler-32 mismatch"}, | ||
| 1119 | {at_dynamic, z, 3192, E_DSTORAGE_DECOMPRESSION_ERROR, nullptr, "a stream shorter than DestinationSize"}, | ||
| 1120 | {at_dynamic, z, 3190, E_DSTORAGE_DECOMPRESSION_ERROR, nullptr, "a stream longer than DestinationSize"}, | ||
| 1121 | {at_dynamic + 8, z, 3191, E_DSTORAGE_INVALID_FILE_OFFSET, nullptr, "a zlib offset not 16-byte aligned"}, | ||
| 1122 | {file.size() - 16, 32, 64, E_DSTORAGE_END_OF_FILE, nullptr, "a zlib source past the end of the file"}, | ||
| 1123 | {at_dynamic, z, 3191, E_DSTORAGE_INVALID_INTERMEDIATE_SIZE, nullptr, "zlib with an IntermediateSize", 0, | ||
| 1124 | 16}, | ||
| 1125 | {at_dynamic, z, 3191, E_NOTIMPL, nullptr, "zlib and BCPack is not implemented yet", | ||
| 1126 | static_cast<uint64_t>(DSTORAGE_BCPACK_MODE_BC1) << options::kBcpackShift, 64}, | ||
| 1127 | {at_dynamic, z, 3191, E_NOTIMPL, nullptr, "zlib and swizzle is not implemented yet", | ||
| 1128 | static_cast<uint64_t>(DSTORAGE_SWIZZLE_MODE_4K_4K_8K) << options::kSwizzleShift}, | ||
| 1129 | }; | ||
| 1130 | |||
| 1131 | DSTORAGE_STATS before{}; | ||
| 1132 | env.factory->GetStats(&before); | ||
| 1133 | IDStorageFileX* f = open(env, L"GAME:\\data\\zlib.bin"); | ||
| 1134 | IDStorageQueueX2* q = make_queue(env, 0x80, false); | ||
| 1135 | IDStorageStatusArrayX* s = make_status(env, static_cast<UINT32>(cases.size())); | ||
| 1136 | constexpr size_t kSlack = 64; // past DestinationSize: must stay untouched | ||
| 1137 | std::vector<std::vector<uint8_t>> out(cases.size()); | ||
| 1138 | for (size_t i = 0; i < cases.size(); ++i) { | ||
| 1139 | const Case& c = cases[i]; | ||
| 1140 | out[i].assign(c.destination + kSlack, 0xEE); | ||
| 1141 | DSTORAGE_REQUEST r = file_request(f, c.offset, out[i].data(), c.destination); | ||
| 1142 | r.SourceSize = c.source; | ||
| 1143 | r.IntermediateSize = c.intermediate; | ||
| 1144 | r.Options.AsUINT64 = options::kZlib | c.options; | ||
| 1145 | q->EnqueueRequest(&r); | ||
| 1146 | q->EnqueueStatus(s, static_cast<UINT32>(i)); | ||
| 1147 | } | ||
| 1148 | q->Submit(); | ||
| 1149 | check(wait_complete(s, static_cast<UINT32>(cases.size() - 1)), "the zlib file requests complete"); | ||
| 1150 | for (size_t i = 0; i < cases.size(); ++i) { | ||
| 1151 | const Case& c = cases[i]; | ||
| 1152 | check_hr(s->GetHResult(static_cast<UINT32>(i)), c.want, c.what); | ||
| 1153 | const std::vector<uint8_t> untouched(c.destination, 0xEE); | ||
| 1154 | const bool head = c.result ? std::equal(c.result->begin(), c.result->end(), out[i].begin()) | ||
| 1155 | : std::equal(untouched.begin(), untouched.end(), out[i].begin()); | ||
| 1156 | const bool tail = std::all_of(out[i].end() - kSlack, out[i].end(), [](uint8_t x) { return x == 0xEE; }); | ||
| 1157 | check(head && tail, c.result ? "the inflated bytes are the synthetic text, and nothing past them" | ||
| 1158 | : "a failed zlib request leaves the destination untouched"); | ||
| 1159 | } | ||
| 1160 | s->Release(); | ||
| 1161 | q->Release(); | ||
| 1162 | f->Release(); | ||
| 1163 | |||
| 1164 | // A memory source: the same stream, one inflated in place (the compressed bytes sit at the end of their own | ||
| 1165 | // destination, as a title may load them), a corrupt one, a source and a destination the process cannot touch. | ||
| 1166 | IDStorageQueueX2* mq = make_queue(env, 0x80, true, DSTORAGE_REQUEST_SOURCE_MEMORY, DSTORAGE_PRIORITY_REALTIME); | ||
| 1167 | IDStorageStatusArrayX* ms = make_status(env, 5); | ||
| 1168 | void* reserved = VirtualAlloc(nullptr, 1 << 16, MEM_RESERVE, PAGE_READWRITE); | ||
| 1169 | std::vector<uint8_t> m0(3191, 0xEE), in_place(3191, 0xEE), m2(3191, 0xEE), m3(3191, 0xEE); | ||
| 1170 | std::copy(dynamic.begin(), dynamic.end(), in_place.end() - dynamic.size()); | ||
| 1171 | DSTORAGE_REQUEST r{}; | ||
| 1172 | r.Options.AsUINT64 = options::kZlib | options::kSourceMemory; | ||
| 1173 | r.SourceSize = z; | ||
| 1174 | r.DestinationSize = 3191; | ||
| 1175 | r.Source = dynamic.data(); | ||
| 1176 | r.Destination = m0.data(); | ||
| 1177 | mq->EnqueueRequest(&r); | ||
| 1178 | mq->EnqueueStatus(ms, 0); | ||
| 1179 | r.Source = in_place.data() + in_place.size() - dynamic.size(); | ||
| 1180 | r.Destination = in_place.data(); | ||
| 1181 | mq->EnqueueRequest(&r); | ||
| 1182 | mq->EnqueueStatus(ms, 1); | ||
| 1183 | r.Source = bad_check.data(); | ||
| 1184 | r.Destination = m2.data(); | ||
| 1185 | mq->EnqueueRequest(&r); | ||
| 1186 | mq->EnqueueStatus(ms, 2); | ||
| 1187 | r.Source = reserved; | ||
| 1188 | r.Destination = m3.data(); | ||
| 1189 | mq->EnqueueRequest(&r); | ||
| 1190 | mq->EnqueueStatus(ms, 3); | ||
| 1191 | r.Source = dynamic.data(); | ||
| 1192 | r.Destination = reserved; | ||
| 1193 | mq->EnqueueRequest(&r); | ||
| 1194 | mq->EnqueueStatus(ms, 4); | ||
| 1195 | mq->Submit(); | ||
| 1196 | check(wait_complete(ms, 4), "the zlib memory requests complete"); | ||
| 1197 | check_hr(ms->GetHResult(0), S_OK, "a zlib memory request"); | ||
| 1198 | check(m0 == dynamic_plain, "the zlib memory request inflates to the synthetic text"); | ||
| 1199 | check_hr(ms->GetHResult(1), S_OK, "a zlib memory request whose source lies inside its destination"); | ||
| 1200 | check(in_place == dynamic_plain, "inflating in place gives the synthetic text"); | ||
| 1201 | check_hr(ms->GetHResult(2), E_DSTORAGE_ZLIB_PARITY_FAIL, "a zlib memory request with an Adler-32 mismatch"); | ||
| 1202 | check(m2 == std::vector<uint8_t>(3191, 0xEE), "the failed zlib memory request leaves the destination untouched"); | ||
| 1203 | check_hr(ms->GetHResult(3), E_DSTORAGE_ACCESS_VIOLATION, "a zlib source the process cannot read"); | ||
| 1204 | check_hr(ms->GetHResult(4), E_DSTORAGE_ACCESS_VIOLATION, "a zlib destination the process cannot write"); | ||
| 1205 | VirtualFree(reserved, 0, MEM_RELEASE); | ||
| 1206 | ms->Release(); | ||
| 1207 | mq->Release(); | ||
| 1208 | |||
| 1209 | // Counted when the inflate starts: the 11 file requests (4 good, 7 bad streams) and 4 memory requests (all but the | ||
| 1210 | // unreadable source) whose checks passed and whose source was read. Failed: the 7 bad file streams and the memory | ||
| 1211 | // one with an Adler-32 mismatch; a destination the process cannot write is not a decompression error. | ||
| 1212 | DSTORAGE_STATS after{}; | ||
| 1213 | env.factory->GetStats(&after); | ||
| 1214 | check(after.TotalDecompressionRequestCount - before.TotalDecompressionRequestCount == 15, | ||
| 1215 | "TotalDecompressionRequestCount counts the zlib requests that reached the inflate"); | ||
| 1216 | check(after.DecompressionErrors - before.DecompressionErrors == 8, "DecompressionErrors counts the bad streams"); | ||
| 1217 | check(after.PendingDecompressionRequestCount == 0, "no decompression is pending afterwards"); | ||
| 1218 | } | ||
| 1219 | |||
| 1220 | } // namespace | ||
| 1221 | |||
| 1222 | int main() { | ||
| 1223 | wchar_t temp[MAX_PATH]; | ||
| 1224 | GetTempPathW(MAX_PATH, temp); | ||
| 1225 | Env env; | ||
| 1226 | env.root = fs::path(temp) / (L"leonida_dstorage_test_" + std::to_wstring(GetCurrentProcessId())); | ||
| 1227 | env.game = env.root / L"game"; | ||
| 1228 | env.overlay = env.root / L"overlay"; | ||
| 1229 | std::error_code ec; | ||
| 1230 | fs::remove_all(env.root, ec); | ||
| 1231 | env.a_package = pattern(4096, 1); | ||
| 1232 | env.a_overlay = pattern(5000, 2); // another size and other bytes than the package's copy | ||
| 1233 | env.b = pattern(10000, 3); | ||
| 1234 | env.big = pattern(3u << 20, 4); | ||
| 1235 | write_file(env.game / L"data" / L"a.bin", env.a_package); | ||
| 1236 | write_file(env.overlay / L"data" / L"a.bin", env.a_overlay); | ||
| 1237 | write_file(env.game / L"data" / L"b.bin", env.b); | ||
| 1238 | write_file(env.game / L"data" / L"big.bin", env.big); | ||
| 1239 | rt::fs::configure(env.game, env.overlay); | ||
| 1240 | |||
| 1241 | test_layout_and_options(); | ||
| 1242 | test_registry(env); | ||
| 1243 | test_factory_checks(env); | ||
| 1244 | test_open_paths(env); | ||
| 1245 | test_reads_and_errors(env); | ||
| 1246 | test_fence_order_and_events(env); | ||
| 1247 | test_cancel(env); | ||
| 1248 | test_auto_submit_and_full_queue(env); | ||
| 1249 | test_memory_queue(env); | ||
| 1250 | test_close_and_release(env); | ||
| 1251 | test_threads(env); | ||
| 1252 | test_iid_policy_events_and_cancel(env); | ||
| 1253 | test_stats_timing(env); | ||
| 1254 | test_stats(env); | ||
| 1255 | test_zlib(env); // after test_stats, whose request totals it would change | ||
| 1256 | |||
| 1257 | fs::remove_all(env.root, ec); | ||
| 1258 | std::printf("dstorage: %d checks, %d failures\n", g_checks, g_failures); | ||
| 1259 | return g_failures ? 1 : 0; | ||
| 1260 | } | ||