diff options
Diffstat (limited to 'runtime')
| -rw-r--r-- | runtime/CMakeLists.txt | 17 | ||||
| -rw-r--r-- | runtime/shims/dstorage.cpp | 1030 | ||||
| -rw-r--r-- | runtime/shims/dstorage_xs_abi.h | 378 | ||||
| -rw-r--r-- | runtime/shims/registry.cpp | 7 | ||||
| -rw-r--r-- | runtime/test/dstorage_test.cpp | 915 | ||||
| -rw-r--r-- | runtime/test/unimplemented_policy_test.cpp | 2 |
6 files changed, 2347 insertions, 2 deletions
diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index ed946d115..21bb7e0a3 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt | |||
| @@ -99,6 +99,7 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME) | |||
| 99 | shims/xgameruntime.cpp | 99 | shims/xgameruntime.cpp |
| 100 | shims/library_loader.cpp | 100 | shims/library_loader.cpp |
| 101 | shims/process.cpp | 101 | shims/process.cpp |
| 102 | shims/dstorage.cpp | ||
| 102 | ) | 103 | ) |
| 103 | add_dependencies(gta6rt gta6rt_tls_host) | 104 | add_dependencies(gta6rt gta6rt_tls_host) |
| 104 | target_compile_definitions(gta6rt PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX UNICODE _UNICODE) | 105 | target_compile_definitions(gta6rt PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX UNICODE _UNICODE) |
| @@ -210,6 +211,19 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME) | |||
| 210 | target_compile_options(file_system_test PRIVATE /W4 /permissive- /utf-8) | 211 | target_compile_options(file_system_test PRIVATE /W4 /permissive- /utf-8) |
| 211 | target_link_options(file_system_test PRIVATE /OPT:NOREF) | 212 | target_link_options(file_system_test PRIVATE /OPT:NOREF) |
| 212 | 213 | ||
| 214 | # Xbox DirectStorage (DStorageGetFactory under dstorage_xs.dll, xcrdapi.dll and dstorage.dll) over synthetic files: | ||
| 215 | # GAME: paths through the file shims, queues, status arrays, fences and errors. No GPU, no game data. | ||
| 216 | add_executable(dstorage_test | ||
| 217 | test/dstorage_test.cpp | ||
| 218 | shims/dstorage.cpp | ||
| 219 | shims/file_system.cpp | ||
| 220 | shims/registry.cpp | ||
| 221 | ) | ||
| 222 | target_include_directories(dstorage_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/shims) | ||
| 223 | target_compile_definitions(dstorage_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) | ||
| 224 | target_compile_options(dstorage_test PRIVATE /W4 /permissive- /utf-8) | ||
| 225 | target_link_options(dstorage_test PRIVATE /OPT:NOREF) | ||
| 226 | |||
| 213 | # Offline WinHTTP for mapped images (the package's XCurl.dll and libHttpClient.GDK.dll); no network is touched. | 227 | # Offline WinHTTP for mapped images (the package's XCurl.dll and libHttpClient.GDK.dll); no network is touched. |
| 214 | add_executable(winhttp_offline_test | 228 | add_executable(winhttp_offline_test |
| 215 | test/winhttp_offline_test.cpp | 229 | test/winhttp_offline_test.cpp |
| @@ -740,6 +754,9 @@ if(BUILD_TESTING) | |||
| 740 | if(WIN32 AND TARGET winhttp_offline_test) | 754 | if(WIN32 AND TARGET winhttp_offline_test) |
| 741 | add_test(NAME winhttp_offline COMMAND winhttp_offline_test) | 755 | add_test(NAME winhttp_offline COMMAND winhttp_offline_test) |
| 742 | endif() | 756 | endif() |
| 757 | if(WIN32 AND TARGET dstorage_test) | ||
| 758 | add_test(NAME dstorage COMMAND dstorage_test) | ||
| 759 | endif() | ||
| 743 | if(WIN32 AND TARGET xgameruntime_test) | 760 | if(WIN32 AND TARGET xgameruntime_test) |
| 744 | add_test(NAME xgameruntime COMMAND xgameruntime_test) | 761 | add_test(NAME xgameruntime COMMAND xgameruntime_test) |
| 745 | endif() | 762 | endif() |
diff --git a/runtime/shims/dstorage.cpp b/runtime/shims/dstorage.cpp new file mode 100644 index 000000000..9766186fa --- /dev/null +++ b/runtime/shims/dstorage.cpp | |||
| @@ -0,0 +1,1030 @@ | |||
| 1 | // Xbox DirectStorage for a mapped title: DStorageGetFactory and the IDStorageFactoryX2 / IDStorageQueueX2 / | ||
| 2 | // IDStorageFileX / IDStorageStatusArrayX objects of the console API (dstorage_xs_abi.h). | ||
| 3 | // | ||
| 4 | // Served as dstorage_xs.dll, xcrdapi.dll and dstorage.dll!DStorageGetFactory; docs/directstorage.md has the evidence | ||
| 5 | // for those names, the console ABI and what each method does here. In short: | ||
| 6 | // - OpenFile resolves kernel32.dll!CreateFileW through the shim registry, so a GAME: path goes through the | ||
| 7 | // runtime's console path mapping and write overlay (file_system.cpp) exactly as the title's own CreateFileW. | ||
| 8 | // - Each queue has one worker thread that runs its submitted entries in order: a request reads a file range or | ||
| 9 | // copies a memory range into the destination, a status entry, fence signal or event runs when every entry before | ||
| 10 | // it has. Completion is therefore in queue order, as the API guarantees. | ||
| 11 | // - zlib, BCPack, swizzle and physical-page requests fail with E_NOTIMPL for now (later PRs: they become stages | ||
| 12 | // between execute_request's read and its destination write). | ||
| 13 | // Nothing here goes online or touches the GPU. | ||
| 14 | #include <windows.h> | ||
| 15 | #include <d3d12.h> | ||
| 16 | |||
| 17 | #include <algorithm> | ||
| 18 | #include <atomic> | ||
| 19 | #include <condition_variable> | ||
| 20 | #include <cstdio> | ||
| 21 | #include <cstring> | ||
| 22 | #include <deque> | ||
| 23 | #include <memory> | ||
| 24 | #include <mutex> | ||
| 25 | #include <new> | ||
| 26 | #include <set> | ||
| 27 | #include <string> | ||
| 28 | #include <thread> | ||
| 29 | #include <unordered_set> | ||
| 30 | #include <vector> | ||
| 31 | |||
| 32 | #include "dstorage_xs_abi.h" | ||
| 33 | #include "shims.h" | ||
| 34 | |||
| 35 | using namespace dsx; | ||
| 36 | |||
| 37 | namespace { | ||
| 38 | |||
| 39 | // ---- Logging ------------------------------------------------------------------------------------------------------ | ||
| 40 | |||
| 41 | // Process-lifetime state is allocated once and never freed: a worker thread may still run while statics are destroyed | ||
| 42 | // at exit. | ||
| 43 | std::mutex& log_mutex() { | ||
| 44 | static auto* m = new std::mutex; | ||
| 45 | return *m; | ||
| 46 | } | ||
| 47 | |||
| 48 | void log_once(const std::string& key, const std::string& line) { | ||
| 49 | static auto* seen = new std::set<std::string>; | ||
| 50 | std::lock_guard lock(log_mutex()); | ||
| 51 | if (!seen->insert(key).second) return; | ||
| 52 | std::fprintf(stderr, "[rt] dstorage: %s\n", line.c_str()); | ||
| 53 | } | ||
| 54 | |||
| 55 | std::string guid_text(const GUID& g) { | ||
| 56 | char b[48]; | ||
| 57 | std::snprintf(b, sizeof b, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}", g.Data1, g.Data2, g.Data3, | ||
| 58 | g.Data4[0], g.Data4[1], g.Data4[2], g.Data4[3], g.Data4[4], g.Data4[5], g.Data4[6], g.Data4[7]); | ||
| 59 | return b; | ||
| 60 | } | ||
| 61 | |||
| 62 | bool same_guid(const GUID& a, const GUID& b) { return std::memcmp(&a, &b, sizeof(GUID)) == 0; } | ||
| 63 | |||
| 64 | std::string hr_text(HRESULT hr) { | ||
| 65 | char b[16]; | ||
| 66 | std::snprintf(b, sizeof b, "0x%08lx", static_cast<unsigned long>(hr)); | ||
| 67 | return b; | ||
| 68 | } | ||
| 69 | |||
| 70 | // ---- Interface ids ------------------------------------------------------------------------------------------------ | ||
| 71 | // | ||
| 72 | // The X interfaces' ids are not public. A creating call (DStorageGetFactory, CreateQueue*, OpenFile, | ||
| 73 | // CreateStatusArray) accepts an id it has not seen for the kind of object it hands out and logs it the first time, so | ||
| 74 | // the first boot records the console's ids. It refuses an id seen for another kind, a PC DirectStorage id of another | ||
| 75 | // shape, and the system COM interfaces (IMarshal, IAgileObject, ...), which a DirectStorage object never implements. | ||
| 76 | // QueryInterface is stricter (docs/d3d12_vulkan.md: an unknown id is E_NOINTERFACE): it accepts IUnknown, the | ||
| 77 | // declared ids (dstorage_xs_abi.h) and the ids a creating call accepted for that kind, and logs anything else. This is | ||
| 78 | // a policy, not a fact; it lives here so a measured id list can replace it. | ||
| 79 | |||
| 80 | enum class Kind { Factory, Queue, File, StatusArray }; | ||
| 81 | |||
| 82 | const char* kind_name(Kind k) { | ||
| 83 | switch (k) { | ||
| 84 | case Kind::Factory: return "factory"; | ||
| 85 | case Kind::Queue: return "queue"; | ||
| 86 | case Kind::File: return "file"; | ||
| 87 | case Kind::StatusArray: return "status array"; | ||
| 88 | } | ||
| 89 | return "?"; | ||
| 90 | } | ||
| 91 | |||
| 92 | // System interfaces: the COM base range {xxxxxxxx-0000-0000-C000-000000000046} (IClassFactory, IMarshal, | ||
| 93 | // IStdMarshalInfo, IExternalConnection, IMultiQI, IClientSecurity, IServerSecurity, IRpcOptions, IMarshal2, | ||
| 94 | // IWeakReferenceSource, IDispatch, IPersist, ...) and these others. | ||
| 95 | constexpr GUID kSystemInterfaceIds[] = { | ||
| 96 | {0x94ea2b94, 0xe9cc, 0x49e0, {0xc0, 0xff, 0xee, 0x64, 0xca, 0x8f, 0x5b, 0x90}}, // IAgileObject | ||
| 97 | {0xecc8691b, 0xc1db, 0x4dc0, {0x85, 0x5e, 0x65, 0xf6, 0xc5, 0x51, 0xaf, 0x49}}, // INoMarshal | ||
| 98 | {0x1c733a30, 0x2a1c, 0x11ce, {0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d}}, // ICallFactory | ||
| 99 | {0xaf86e2e0, 0xb12d, 0x4c6a, {0x9c, 0x5a, 0xd7, 0xaa, 0x65, 0x10, 0x1e, 0x90}}, // IInspectable | ||
| 100 | {0xdf0b3d60, 0x548f, 0x101b, {0x8e, 0x65, 0x08, 0x00, 0x2b, 0x2b, 0xd1, 0x19}}, // ISupportErrorInfo | ||
| 101 | {0xb196b283, 0xbab4, 0x101a, {0xb6, 0x9c, 0x00, 0xaa, 0x00, 0x34, 0x1d, 0x07}}, // IProvideClassInfo | ||
| 102 | {0xb196b284, 0xbab4, 0x101a, {0xb6, 0x9c, 0x00, 0xaa, 0x00, 0x34, 0x1d, 0x07}}, // IConnectionPointContainer | ||
| 103 | {0xfc4801a3, 0x2ba9, 0x11cf, {0xa2, 0x29, 0x00, 0xaa, 0x00, 0x3d, 0x73, 0x52}}, // IObjectWithSite | ||
| 104 | }; | ||
| 105 | |||
| 106 | bool system_interface(const GUID& g) { | ||
| 107 | static const uint8_t kComBase[8] = {0xc0, 0, 0, 0, 0, 0, 0, 0x46}; | ||
| 108 | if (g.Data2 == 0 && g.Data3 == 0 && std::memcmp(g.Data4, kComBase, 8) == 0) return true; | ||
| 109 | for (const GUID& s : kSystemInterfaceIds) | ||
| 110 | if (same_guid(g, s)) return true; | ||
| 111 | return false; | ||
| 112 | } | ||
| 113 | |||
| 114 | // The ids declared for a kind: the PC IDStorageStatusArray id, whose interface has the console one's shape (console | ||
| 115 | // D3D12 binaries reuse the PC SDK's ids). No other X interface id is known. | ||
| 116 | bool declared(const GUID& g, Kind kind) { | ||
| 117 | for (const GUID& id : kStatusArrayIds) | ||
| 118 | if (same_guid(g, id)) return kind == Kind::StatusArray; | ||
| 119 | return false; | ||
| 120 | } | ||
| 121 | |||
| 122 | bool declared_for_any(const GUID& g) { | ||
| 123 | for (const GUID& id : kStatusArrayIds) | ||
| 124 | if (same_guid(g, id)) return true; | ||
| 125 | return false; | ||
| 126 | } | ||
| 127 | |||
| 128 | class IidPolicy { | ||
| 129 | public: | ||
| 130 | bool accept_creating(REFIID riid, Kind kind, const char* caller) { | ||
| 131 | if (same_guid(riid, __uuidof(IUnknown)) || declared(riid, kind)) return true; | ||
| 132 | if (system_interface(riid)) return refuse(riid, caller, "a system COM interface"); | ||
| 133 | if (declared_for_any(riid)) return refuse(riid, caller, "an id declared for another kind of object"); | ||
| 134 | for (const GUID& pc : kPcInterfaceIds) | ||
| 135 | if (same_guid(riid, pc)) | ||
| 136 | return refuse(riid, caller, "a PC DirectStorage interface; only the console ABI is served"); | ||
| 137 | std::lock_guard lock(mutex_); | ||
| 138 | for (const auto& [id, k] : learned_) | ||
| 139 | if (same_guid(id, riid)) { | ||
| 140 | if (k == kind) return true; | ||
| 141 | return refuse(riid, caller, std::string("seen before as a ") + kind_name(k) + " interface"); | ||
| 142 | } | ||
| 143 | learned_.push_back({riid, kind}); | ||
| 144 | log_once("learn" + guid_text(riid), std::string(caller) + " asked for " + guid_text(riid) + ": serving the " + | ||
| 145 | kind_name(kind) + " interface"); | ||
| 146 | return true; | ||
| 147 | } | ||
| 148 | |||
| 149 | bool accept_query(REFIID riid, Kind kind, const char* caller) { | ||
| 150 | if (same_guid(riid, __uuidof(IUnknown)) || declared(riid, kind)) return true; | ||
| 151 | { | ||
| 152 | std::lock_guard lock(mutex_); | ||
| 153 | for (const auto& [id, k] : learned_) | ||
| 154 | if (same_guid(id, riid) && k == kind) return true; | ||
| 155 | } | ||
| 156 | return refuse(riid, caller, std::string("not an id a ") + kind_name(kind) + " was created with"); | ||
| 157 | } | ||
| 158 | |||
| 159 | private: | ||
| 160 | static bool refuse(REFIID riid, const char* caller, const std::string& why) { | ||
| 161 | log_once(std::string("refuse") + caller + guid_text(riid), | ||
| 162 | std::string(caller) + " asked for " + guid_text(riid) + ": " + why + " (E_NOINTERFACE)"); | ||
| 163 | return false; | ||
| 164 | } | ||
| 165 | |||
| 166 | std::mutex mutex_; | ||
| 167 | std::vector<std::pair<GUID, Kind>> learned_; | ||
| 168 | }; | ||
| 169 | |||
| 170 | IidPolicy& iid_policy() { | ||
| 171 | static auto* p = new IidPolicy; | ||
| 172 | return *p; | ||
| 173 | } | ||
| 174 | |||
| 175 | // A bad argument to a factory or queue method, logged once per method and kind: a wrong vtable guess for the console | ||
| 176 | // interfaces would show up here first, as arguments that make no sense for the method. | ||
| 177 | HRESULT bad_call(const char* method, HRESULT hr, const char* what) { | ||
| 178 | log_once(std::string("bad") + method + what, | ||
| 179 | std::string(method) + ": " + what + " (" + hr_text(hr) + | ||
| 180 | "; logged once; a wrong vtable slot guess would show up like this)"); | ||
| 181 | return hr; | ||
| 182 | } | ||
| 183 | |||
| 184 | // ---- Statistics (IDStorageFactoryX1::GetStats) -------------------------------------------------------------------- | ||
| 185 | |||
| 186 | struct Stats { | ||
| 187 | std::mutex mutex; | ||
| 188 | DSTORAGE_STATS s{}; | ||
| 189 | uint32_t live_queues = 0; // all priorities; SetStagingBufferSize is refused while queues or files exist | ||
| 190 | }; | ||
| 191 | |||
| 192 | Stats& stats() { | ||
| 193 | static auto* s = new Stats; | ||
| 194 | return *s; | ||
| 195 | } | ||
| 196 | |||
| 197 | DSTORAGE_PRIORITY_STATS& priority_stats(DSTORAGE_STATS& s, DSTORAGE_PRIORITY p) { | ||
| 198 | switch (p) { | ||
| 199 | case DSTORAGE_PRIORITY_LOW: return s.LowPriorityStats; | ||
| 200 | case DSTORAGE_PRIORITY_HIGH: return s.HighPriorityStats; | ||
| 201 | case DSTORAGE_PRIORITY_REALTIME: return s.RealtimePriorityStats; | ||
| 202 | default: return s.NormalPriorityStats; | ||
| 203 | } | ||
| 204 | } | ||
| 205 | |||
| 206 | void count_error() { | ||
| 207 | std::lock_guard lock(stats().mutex); | ||
| 208 | ++stats().s.ErrorCount; | ||
| 209 | } | ||
| 210 | |||
| 211 | // ---- Live objects --------------------------------------------------------------------------------------------------- | ||
| 212 | // | ||
| 213 | // A request names its file and a status entry its array by pointer. Only objects this shim made and that are still | ||
| 214 | // alive are used; anything else is an error in the request, never a call through a stranger's vtable. | ||
| 215 | |||
| 216 | std::mutex& live_mutex() { | ||
| 217 | static auto* m = new std::mutex; | ||
| 218 | return *m; | ||
| 219 | } | ||
| 220 | // One set per kind, so a status array passed as a file (or the other way round) is not found. | ||
| 221 | std::unordered_set<const void*>& live_files() { | ||
| 222 | static auto* s = new std::unordered_set<const void*>; | ||
| 223 | return *s; | ||
| 224 | } | ||
| 225 | std::unordered_set<const void*>& live_status_arrays() { | ||
| 226 | static auto* s = new std::unordered_set<const void*>; | ||
| 227 | return *s; | ||
| 228 | } | ||
| 229 | |||
| 230 | // AddRef only if the count has not reached zero (an object being released is not revived). | ||
| 231 | bool add_ref_if_live(std::atomic<ULONG>& refs) { | ||
| 232 | ULONG r = refs.load(); | ||
| 233 | while (r != 0) | ||
| 234 | if (refs.compare_exchange_weak(r, r + 1)) return true; | ||
| 235 | return false; | ||
| 236 | } | ||
| 237 | |||
| 238 | // ---- Status arrays ------------------------------------------------------------------------------------------------ | ||
| 239 | |||
| 240 | class StatusArray final : public IDStorageStatusArrayX { | ||
| 241 | public: | ||
| 242 | StatusArray(uint32_t capacity, std::unique_ptr<std::atomic<HRESULT>[]> entries) | ||
| 243 | : capacity_(capacity), entries_(std::move(entries)) { | ||
| 244 | for (uint32_t i = 0; i < capacity_; ++i) entries_[i].store(S_OK, std::memory_order_relaxed); | ||
| 245 | std::lock_guard lock(live_mutex()); | ||
| 246 | live_status_arrays().insert(static_cast<IDStorageStatusArrayX*>(this)); | ||
| 247 | } | ||
| 248 | |||
| 249 | HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppv) override { | ||
| 250 | if (!ppv) return E_POINTER; | ||
| 251 | *ppv = nullptr; | ||
| 252 | if (!iid_policy().accept_query(riid, Kind::StatusArray, "IDStorageStatusArrayX::QueryInterface")) | ||
| 253 | return E_NOINTERFACE; | ||
| 254 | AddRef(); | ||
| 255 | *ppv = static_cast<IDStorageStatusArrayX*>(this); | ||
| 256 | return S_OK; | ||
| 257 | } | ||
| 258 | ULONG STDMETHODCALLTYPE AddRef() override { return ++refs_; } | ||
| 259 | ULONG STDMETHODCALLTYPE Release() override { | ||
| 260 | ULONG r; | ||
| 261 | { | ||
| 262 | std::lock_guard lock(live_mutex()); | ||
| 263 | r = --refs_; | ||
| 264 | if (r == 0) live_status_arrays().erase(static_cast<IDStorageStatusArrayX*>(this)); | ||
| 265 | } | ||
| 266 | if (r == 0) delete this; | ||
| 267 | return r; | ||
| 268 | } | ||
| 269 | |||
| 270 | // IsComplete is GetHResult(index) != E_PENDING (the reference says so); an index past the array is not pending. | ||
| 271 | bool STDMETHODCALLTYPE IsComplete(UINT32 index) override { return GetHResult(index) != E_PENDING; } | ||
| 272 | HRESULT STDMETHODCALLTYPE GetHResult(UINT32 index) override { | ||
| 273 | if (index >= capacity_) return E_DSTORAGE_INDEX_BOUND; | ||
| 274 | return entries_[index].load(std::memory_order_acquire); | ||
| 275 | } | ||
| 276 | |||
| 277 | uint32_t capacity() const { return capacity_; } | ||
| 278 | void set_pending(uint32_t index) { entries_[index].store(E_PENDING, std::memory_order_release); } | ||
| 279 | void complete(uint32_t index, HRESULT hr) { entries_[index].store(hr, std::memory_order_release); } | ||
| 280 | std::atomic<ULONG>& refs() { return refs_; } | ||
| 281 | |||
| 282 | private: | ||
| 283 | std::atomic<ULONG> refs_{1}; | ||
| 284 | const uint32_t capacity_; | ||
| 285 | std::unique_ptr<std::atomic<HRESULT>[]> entries_; | ||
| 286 | }; | ||
| 287 | |||
| 288 | // ---- Files -------------------------------------------------------------------------------------------------------- | ||
| 289 | |||
| 290 | class File final : public IDStorageFileX { | ||
| 291 | public: | ||
| 292 | File(HANDLE handle, std::wstring path) : handle_(handle), path_(std::move(path)) { | ||
| 293 | { | ||
| 294 | std::lock_guard lock(live_mutex()); | ||
| 295 | live_files().insert(static_cast<IDStorageFileX*>(this)); | ||
| 296 | } | ||
| 297 | std::lock_guard lock(stats().mutex); | ||
| 298 | auto& s = stats().s; | ||
| 299 | ++s.CurrentOpenFileCount; | ||
| 300 | ++s.CumulativeOpenFileCount; | ||
| 301 | s.MaxOpenFileCount = std::max(s.MaxOpenFileCount, s.CurrentOpenFileCount); | ||
| 302 | } | ||
| 303 | ~File() { | ||
| 304 | Close(); | ||
| 305 | std::lock_guard lock(stats().mutex); | ||
| 306 | --stats().s.CurrentOpenFileCount; | ||
| 307 | } | ||
| 308 | |||
| 309 | HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppv) override { | ||
| 310 | if (!ppv) return E_POINTER; | ||
| 311 | *ppv = nullptr; | ||
| 312 | if (!iid_policy().accept_query(riid, Kind::File, "IDStorageFileX::QueryInterface")) return E_NOINTERFACE; | ||
| 313 | AddRef(); | ||
| 314 | *ppv = static_cast<IDStorageFileX*>(this); | ||
| 315 | return S_OK; | ||
| 316 | } | ||
| 317 | ULONG STDMETHODCALLTYPE AddRef() override { return ++refs_; } | ||
| 318 | ULONG STDMETHODCALLTYPE Release() override { | ||
| 319 | ULONG r; | ||
| 320 | { | ||
| 321 | std::lock_guard lock(live_mutex()); | ||
| 322 | r = --refs_; | ||
| 323 | if (r == 0) live_files().erase(static_cast<IDStorageFileX*>(this)); | ||
| 324 | } | ||
| 325 | if (r == 0) delete this; | ||
| 326 | return r; | ||
| 327 | } | ||
| 328 | |||
| 329 | // Waits for the submitted requests that read this file, then closes the handle. A request submitted later fails | ||
| 330 | // with E_DSTORAGE_FILE_NOT_OPEN. Waiting only for submitted requests keeps a Close from blocking forever on a | ||
| 331 | // request that sits unsubmitted in a queue with auto-submit disabled. | ||
| 332 | void STDMETHODCALLTYPE Close() override { | ||
| 333 | std::unique_lock lock(mutex_); | ||
| 334 | closed_ = true; | ||
| 335 | idle_.wait(lock, [&] { return pending_ == 0; }); | ||
| 336 | if (handle_ != INVALID_HANDLE_VALUE) { | ||
| 337 | CloseHandle(handle_); | ||
| 338 | handle_ = INVALID_HANDLE_VALUE; | ||
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 | // A new handle the caller closes (GENERIC_READ, FILE_SHARE_READ, as the reference describes it). ReOpenFile, | ||
| 343 | // not DuplicateHandle: a duplicate shares the file position with the shim's own reads, so the title's ReadFile | ||
| 344 | // through it would start wherever the worker's last read ended. | ||
| 345 | HANDLE STDMETHODCALLTYPE GetHandle() override { | ||
| 346 | std::lock_guard lock(mutex_); | ||
| 347 | if (closed_ || handle_ == INVALID_HANDLE_VALUE) { | ||
| 348 | SetLastError(ERROR_INVALID_HANDLE); | ||
| 349 | return INVALID_HANDLE_VALUE; | ||
| 350 | } | ||
| 351 | return ReOpenFile(handle_, GENERIC_READ, FILE_SHARE_READ, 0); | ||
| 352 | } | ||
| 353 | |||
| 354 | // Called when a request naming this file is submitted. False once the file is closed. | ||
| 355 | bool add_pending() { | ||
| 356 | std::lock_guard lock(mutex_); | ||
| 357 | if (closed_) return false; | ||
| 358 | ++pending_; | ||
| 359 | return true; | ||
| 360 | } | ||
| 361 | void finish_pending() { | ||
| 362 | std::lock_guard lock(mutex_); | ||
| 363 | if (--pending_ == 0) idle_.notify_all(); | ||
| 364 | } | ||
| 365 | // Valid while the caller holds a pending count: Close does not close the handle before it drops to zero. | ||
| 366 | HANDLE handle() const { return handle_; } | ||
| 367 | const std::wstring& path() const { return path_; } | ||
| 368 | std::atomic<ULONG>& refs() { return refs_; } | ||
| 369 | |||
| 370 | private: | ||
| 371 | std::atomic<ULONG> refs_{1}; | ||
| 372 | std::mutex mutex_; | ||
| 373 | std::condition_variable idle_; | ||
| 374 | HANDLE handle_; | ||
| 375 | const std::wstring path_; | ||
| 376 | uint32_t pending_ = 0; | ||
| 377 | bool closed_ = false; | ||
| 378 | }; | ||
| 379 | |||
| 380 | StatusArray* live_status_array(IDStorageStatusArrayX* p) { | ||
| 381 | std::lock_guard lock(live_mutex()); | ||
| 382 | if (!p || !live_status_arrays().count(p)) return nullptr; | ||
| 383 | auto* a = static_cast<StatusArray*>(p); | ||
| 384 | return add_ref_if_live(a->refs()) ? a : nullptr; | ||
| 385 | } | ||
| 386 | |||
| 387 | File* live_file(IDStorageFileX* p) { | ||
| 388 | std::lock_guard lock(live_mutex()); | ||
| 389 | if (!p || !live_files().count(p)) return nullptr; | ||
| 390 | auto* f = static_cast<File*>(p); | ||
| 391 | return add_ref_if_live(f->refs()) ? f : nullptr; | ||
| 392 | } | ||
| 393 | |||
| 394 | // ---- Reads -------------------------------------------------------------------------------------------------------- | ||
| 395 | |||
| 396 | // memmove that turns an access violation into an error instead of ending the process. No C++ objects here: a | ||
| 397 | // function with __try cannot have any that need unwinding. | ||
| 398 | bool guarded_move(void* destination, const void* source, size_t size) { | ||
| 399 | __try { | ||
| 400 | std::memmove(destination, source, size); | ||
| 401 | return true; | ||
| 402 | } __except (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ? EXCEPTION_EXECUTE_HANDLER | ||
| 403 | : EXCEPTION_CONTINUE_SEARCH) { | ||
| 404 | return false; | ||
| 405 | } | ||
| 406 | } | ||
| 407 | |||
| 408 | HRESULT read_error(DWORD error) { | ||
| 409 | switch (error) { | ||
| 410 | case ERROR_NOACCESS: | ||
| 411 | case ERROR_INVALID_USER_BUFFER: return E_DSTORAGE_ACCESS_VIOLATION; | ||
| 412 | case ERROR_HANDLE_EOF: return E_DSTORAGE_END_OF_FILE; | ||
| 413 | default: return HRESULT_FROM_WIN32(error); | ||
| 414 | } | ||
| 415 | } | ||
| 416 | |||
| 417 | // Reads exactly "size" bytes at "offset" of a synchronous handle into "destination". | ||
| 418 | HRESULT read_file_range(HANDLE handle, uint64_t offset, void* destination, uint32_t size) { | ||
| 419 | LARGE_INTEGER length{}; | ||
| 420 | if (!GetFileSizeEx(handle, &length)) return HRESULT_FROM_WIN32(GetLastError()); | ||
| 421 | const uint64_t file_size = static_cast<uint64_t>(length.QuadPart); | ||
| 422 | if (offset > file_size || size > file_size - offset) return E_DSTORAGE_END_OF_FILE; | ||
| 423 | auto* out = static_cast<uint8_t*>(destination); | ||
| 424 | uint32_t done = 0; | ||
| 425 | while (done < size) { | ||
| 426 | OVERLAPPED at{}; | ||
| 427 | const uint64_t position = offset + done; | ||
| 428 | at.Offset = static_cast<DWORD>(position); | ||
| 429 | at.OffsetHigh = static_cast<DWORD>(position >> 32); | ||
| 430 | DWORD got = 0; | ||
| 431 | if (!ReadFile(handle, out + done, size - done, &got, &at)) return read_error(GetLastError()); | ||
| 432 | if (got == 0) return E_DSTORAGE_END_OF_FILE; // the file shrank since the size check | ||
| 433 | done += got; | ||
| 434 | } | ||
| 435 | return S_OK; | ||
| 436 | } | ||
| 437 | |||
| 438 | // ---- Queues ------------------------------------------------------------------------------------------------------- | ||
| 439 | |||
| 440 | struct Entry { | ||
| 441 | enum class Type { Request, Status, Signal, Event } type = Type::Request; | ||
| 442 | DSTORAGE_REQUEST request{}; | ||
| 443 | File* file = nullptr; // a reference, for a file request naming a live file | ||
| 444 | bool file_counted = false; // the file's pending count was raised at submit | ||
| 445 | bool cancelled = false; // CancelRequestsWithTag matched it before it started | ||
| 446 | HRESULT enqueue_error = S_OK; // found at enqueue: reported when the entry is reached, in order | ||
| 447 | StatusArray* status = nullptr; // a reference | ||
| 448 | IDStorageStatusArrayX* status_as_given = nullptr; | ||
| 449 | uint32_t index = 0; | ||
| 450 | ID3D12Fence* fence = nullptr; // a reference | ||
| 451 | uint64_t value = 0; | ||
| 452 | HANDLE event = nullptr; // a duplicate the entry owns | ||
| 453 | }; | ||
| 454 | |||
| 455 | class Queue final : public IDStorageQueueX2 { | ||
| 456 | public: | ||
| 457 | explicit Queue(const DSTORAGE_QUEUE_DESC1& desc) : desc_(desc) { | ||
| 458 | error_event_ = CreateEventW(nullptr, TRUE, FALSE, nullptr); | ||
| 459 | clear_record_locked(); | ||
| 460 | std::lock_guard lock(stats().mutex); | ||
| 461 | auto& p = priority_stats(stats().s, desc_.Priority); | ||
| 462 | ++p.CurrentQueueCount; | ||
| 463 | ++p.CumulativeQueueCount; | ||
| 464 | p.MaxQueueCount = std::max(p.MaxQueueCount, p.CurrentQueueCount); | ||
| 465 | ++stats().live_queues; | ||
| 466 | } | ||
| 467 | // Separate from the constructor so a failure to start the thread can be reported. | ||
| 468 | bool start() { | ||
| 469 | if (!error_event_) return false; | ||
| 470 | try { | ||
| 471 | worker_ = std::thread([this] { run(); }); | ||
| 472 | } catch (...) { | ||
| 473 | return false; | ||
| 474 | } | ||
| 475 | return true; | ||
| 476 | } | ||
| 477 | ~Queue() { | ||
| 478 | Close(); | ||
| 479 | if (error_event_) CloseHandle(error_event_); | ||
| 480 | std::lock_guard lock(stats().mutex); | ||
| 481 | --priority_stats(stats().s, desc_.Priority).CurrentQueueCount; | ||
| 482 | --stats().live_queues; | ||
| 483 | } | ||
| 484 | |||
| 485 | HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppv) override { | ||
| 486 | if (!ppv) return E_POINTER; | ||
| 487 | *ppv = nullptr; | ||
| 488 | if (!iid_policy().accept_query(riid, Kind::Queue, "IDStorageQueueX::QueryInterface")) return E_NOINTERFACE; | ||
| 489 | AddRef(); | ||
| 490 | *ppv = static_cast<IDStorageQueueX2*>(this); | ||
| 491 | return S_OK; | ||
| 492 | } | ||
| 493 | ULONG STDMETHODCALLTYPE AddRef() override { return ++refs_; } | ||
| 494 | // The last Release closes the queue, which runs everything enqueued first. | ||
| 495 | ULONG STDMETHODCALLTYPE Release() override { | ||
| 496 | const ULONG r = --refs_; | ||
| 497 | if (r == 0) delete this; | ||
| 498 | return r; | ||
| 499 | } | ||
| 500 | |||
| 501 | void STDMETHODCALLTYPE EnqueueRequest(const DSTORAGE_REQUEST* request) override { | ||
| 502 | Entry e; | ||
| 503 | e.type = Entry::Type::Request; | ||
| 504 | if (!request) { | ||
| 505 | e.enqueue_error = bad_call("IDStorageQueueX::EnqueueRequest", E_INVALIDARG, "null request"); | ||
| 506 | } else { | ||
| 507 | e.request = *request; // the title may reuse its structure as soon as this returns | ||
| 508 | const uint64_t o = e.request.Options.AsUINT64; | ||
| 509 | if (options::source_type(o) == DSTORAGE_REQUEST_SOURCE_FILE && | ||
| 510 | desc_.SourceType == DSTORAGE_REQUEST_SOURCE_FILE && e.request.File) { | ||
| 511 | e.file = live_file(e.request.File); | ||
| 512 | if (!e.file) e.enqueue_error = E_DSTORAGE_INVALID_FILE_HANDLE; | ||
| 513 | } | ||
| 514 | std::lock_guard lock(stats().mutex); | ||
| 515 | auto& p = priority_stats(stats().s, desc_.Priority); | ||
| 516 | ++p.TotalRequestCount; | ||
| 517 | p.TotalRequestSourceByteCount += request->SourceSize; | ||
| 518 | p.TotalRequestDestinationByteCount += request->DestinationSize; | ||
| 519 | } | ||
| 520 | enqueue(std::move(e)); | ||
| 521 | } | ||
| 522 | |||
| 523 | void STDMETHODCALLTYPE EnqueueStatus(IDStorageStatusArrayX* status_array, UINT32 index) override { | ||
| 524 | Entry e; | ||
| 525 | e.type = Entry::Type::Status; | ||
| 526 | e.status_as_given = status_array; | ||
| 527 | e.index = index; | ||
| 528 | e.status = live_status_array(status_array); | ||
| 529 | if (!e.status) { | ||
| 530 | e.enqueue_error = E_DSTORAGE_INVALID_STATUS_ARRAY; | ||
| 531 | } else if (index >= e.status->capacity()) { | ||
| 532 | e.enqueue_error = E_DSTORAGE_INDEX_BOUND; | ||
| 533 | } else { | ||
| 534 | e.status->set_pending(index); // polling after this call sees E_PENDING until the entry is reached | ||
| 535 | } | ||
| 536 | { | ||
| 537 | std::lock_guard lock(stats().mutex); | ||
| 538 | ++priority_stats(stats().s, desc_.Priority).TotalStatusCount; | ||
| 539 | } | ||
| 540 | enqueue(std::move(e)); | ||
| 541 | } | ||
| 542 | |||
| 543 | void STDMETHODCALLTYPE EnqueueSignal(ID3D12Fence* fence, UINT64 value) override { | ||
| 544 | Entry e; | ||
| 545 | e.type = Entry::Type::Signal; | ||
| 546 | e.fence = fence; | ||
| 547 | e.value = value; | ||
| 548 | if (fence) { | ||
| 549 | fence->AddRef(); | ||
| 550 | } else { | ||
| 551 | e.enqueue_error = E_DSTORAGE_INVALID_FENCE; | ||
| 552 | } | ||
| 553 | { | ||
| 554 | std::lock_guard lock(stats().mutex); | ||
| 555 | ++priority_stats(stats().s, desc_.Priority).TotalFenceCount; | ||
| 556 | } | ||
| 557 | enqueue(std::move(e)); | ||
| 558 | } | ||
| 559 | |||
| 560 | // The handle is duplicated here and the duplicate closed once it has been set (or dropped), so an event the | ||
| 561 | // title closes, and whose handle value is then reused for another object, is never the one set. | ||
| 562 | void STDMETHODCALLTYPE EnqueueSetEvent(HANDLE handle) override { | ||
| 563 | Entry e; | ||
| 564 | e.type = Entry::Type::Event; | ||
| 565 | if (!handle) { | ||
| 566 | e.enqueue_error = bad_call("IDStorageQueueX1::EnqueueSetEvent", HRESULT_FROM_WIN32(ERROR_INVALID_HANDLE), | ||
| 567 | "null event handle"); | ||
| 568 | } else if (!DuplicateHandle(GetCurrentProcess(), handle, GetCurrentProcess(), &e.event, 0, FALSE, | ||
| 569 | DUPLICATE_SAME_ACCESS)) { | ||
| 570 | e.event = nullptr; | ||
| 571 | e.enqueue_error = HRESULT_FROM_WIN32(GetLastError()); | ||
| 572 | } | ||
| 573 | enqueue(std::move(e)); | ||
| 574 | } | ||
| 575 | |||
| 576 | void STDMETHODCALLTYPE Submit() override { | ||
| 577 | std::lock_guard lock(mutex_); | ||
| 578 | submit_locked(); | ||
| 579 | } | ||
| 580 | |||
| 581 | // Marks the requests enqueued before this call that have not started. A cancelled request completes as a success | ||
| 582 | // without being read (the overview: the title may no longer use its result). | ||
| 583 | void STDMETHODCALLTYPE CancelRequestsWithTag(UINT64 mask, UINT64 value) override { | ||
| 584 | std::lock_guard lock(mutex_); | ||
| 585 | for (Entry& e : entries_) | ||
| 586 | if (e.type == Entry::Type::Request && (e.request.CancellationTag & mask) == value) e.cancelled = true; | ||
| 587 | } | ||
| 588 | |||
| 589 | // Submits what is enqueued, waits until the worker has run all of it and stops the worker. Later enqueues are | ||
| 590 | // dropped and recorded as E_DSTORAGE_QUEUE_CLOSED. | ||
| 591 | void STDMETHODCALLTYPE Close() override { | ||
| 592 | std::lock_guard close_lock(close_mutex_); | ||
| 593 | { | ||
| 594 | std::unique_lock lock(mutex_); | ||
| 595 | if (closed_) return; | ||
| 596 | closing_ = true; | ||
| 597 | submit_locked(); | ||
| 598 | space_.notify_all(); // an enqueue waiting for space gives up (the queue is closing) | ||
| 599 | idle_.wait(lock, [&] { return entries_.empty() && !in_flight_; }); | ||
| 600 | closed_ = true; | ||
| 601 | work_.notify_all(); | ||
| 602 | } | ||
| 603 | if (worker_.joinable()) worker_.join(); | ||
| 604 | } | ||
| 605 | |||
| 606 | HANDLE STDMETHODCALLTYPE GetErrorEvent() override { | ||
| 607 | HANDLE dup = nullptr; | ||
| 608 | if (!error_event_ || !DuplicateHandle(GetCurrentProcess(), error_event_, GetCurrentProcess(), &dup, 0, FALSE, | ||
| 609 | DUPLICATE_SAME_ACCESS)) | ||
| 610 | return nullptr; | ||
| 611 | return dup; // the reference says the title closes it | ||
| 612 | } | ||
| 613 | |||
| 614 | void STDMETHODCALLTYPE RetrieveErrorRecord(DSTORAGE_ERROR_RECORD* record) override { | ||
| 615 | if (!record) { | ||
| 616 | bad_call("IDStorageQueueX::RetrieveErrorRecord", E_POINTER, "null record"); | ||
| 617 | return; | ||
| 618 | } | ||
| 619 | std::lock_guard lock(error_mutex_); | ||
| 620 | *record = record_; | ||
| 621 | clear_record_locked(); | ||
| 622 | if (error_event_) ResetEvent(error_event_); | ||
| 623 | } | ||
| 624 | |||
| 625 | void STDMETHODCALLTYPE Query(DSTORAGE_QUEUE_INFO* info) override { | ||
| 626 | if (!info) { | ||
| 627 | bad_call("IDStorageQueueX::Query", E_POINTER, "null info"); | ||
| 628 | return; | ||
| 629 | } | ||
| 630 | DSTORAGE_QUEUE_INFO1 full{}; | ||
| 631 | Query1(&full); | ||
| 632 | info->Desc.SourceType = full.Desc.SourceType; | ||
| 633 | info->Desc.Capacity = full.Desc.Capacity; | ||
| 634 | info->Desc.Priority = full.Desc.Priority; | ||
| 635 | info->Desc.Name = full.Desc.Name; | ||
| 636 | info->EmptySlotCount = full.EmptySlotCount; | ||
| 637 | info->RequestCountUntilAutoSubmit = full.RequestCountUntilAutoSubmit; | ||
| 638 | } | ||
| 639 | |||
| 640 | // An empty queue has capacity - 1 empty slots (one slot tells empty from full). The automatic submit happens when | ||
| 641 | // the entries enqueued since the last submit reach half the capacity; with it disabled the count is 0. | ||
| 642 | void STDMETHODCALLTYPE Query1(DSTORAGE_QUEUE_INFO1* info) override { | ||
| 643 | if (!info) { | ||
| 644 | bad_call("IDStorageQueueX2::Query1", E_POINTER, "null info"); | ||
| 645 | return; | ||
| 646 | } | ||
| 647 | std::lock_guard lock(mutex_); | ||
| 648 | info->Desc = desc_; | ||
| 649 | info->EmptySlotCount = static_cast<UINT16>(usable() - used_locked()); | ||
| 650 | const size_t unsubmitted = entries_.size() - submitted_; | ||
| 651 | info->RequestCountUntilAutoSubmit = | ||
| 652 | auto_submit() ? static_cast<UINT16>(half() > unsubmitted ? half() - unsubmitted : 0) : 0; | ||
| 653 | } | ||
| 654 | |||
| 655 | private: | ||
| 656 | size_t usable() const { return desc_.Capacity - 1u; } | ||
| 657 | size_t half() const { return desc_.Capacity / 2u; } | ||
| 658 | bool auto_submit() const { return !desc_.Options.AutoSubmitDisabled; } | ||
| 659 | size_t used_locked() const { return entries_.size() + (in_flight_ ? 1 : 0); } | ||
| 660 | |||
| 661 | void enqueue(Entry&& e) { | ||
| 662 | { | ||
| 663 | std::unique_lock lock(mutex_); | ||
| 664 | // A full queue waits for the worker to free a slot. With auto-submit on, half the queue is always | ||
| 665 | // submitted by then; with it off, only a Submit from another thread frees one (as the reference warns). | ||
| 666 | space_.wait(lock, [&] { return closing_ || used_locked() < usable(); }); | ||
| 667 | if (!closing_) { | ||
| 668 | entries_.push_back(std::move(e)); | ||
| 669 | if (auto_submit() && entries_.size() - submitted_ >= half()) submit_locked(); | ||
| 670 | return; | ||
| 671 | } | ||
| 672 | } | ||
| 673 | DSTORAGE_ERROR_FIRST_FAILURE f{}; | ||
| 674 | f.HResult = E_DSTORAGE_QUEUE_CLOSED; | ||
| 675 | describe(e, f); | ||
| 676 | record_failure(f); | ||
| 677 | log_once("closed", "an entry was enqueued on a closed queue (E_DSTORAGE_QUEUE_CLOSED); it never completes"); | ||
| 678 | release_references(e); | ||
| 679 | } | ||
| 680 | |||
| 681 | void submit_locked() { | ||
| 682 | for (size_t i = submitted_; i < entries_.size(); ++i) { | ||
| 683 | Entry& e = entries_[i]; | ||
| 684 | if (e.file) e.file_counted = e.file->add_pending(); | ||
| 685 | } | ||
| 686 | if (entries_.size() > submitted_) { | ||
| 687 | submitted_ = entries_.size(); | ||
| 688 | work_.notify_one(); | ||
| 689 | } | ||
| 690 | } | ||
| 691 | |||
| 692 | void run() { | ||
| 693 | for (;;) { | ||
| 694 | Entry e; | ||
| 695 | { | ||
| 696 | std::unique_lock lock(mutex_); | ||
| 697 | work_.wait(lock, [&] { return submitted_ > 0 || closed_; }); | ||
| 698 | if (submitted_ == 0) return; // closed and drained | ||
| 699 | e = std::move(entries_.front()); | ||
| 700 | entries_.pop_front(); | ||
| 701 | --submitted_; | ||
| 702 | in_flight_ = true; | ||
| 703 | } | ||
| 704 | process(e); | ||
| 705 | release_references(e); | ||
| 706 | { | ||
| 707 | std::lock_guard lock(mutex_); | ||
| 708 | in_flight_ = false; | ||
| 709 | space_.notify_all(); | ||
| 710 | if (entries_.empty()) idle_.notify_all(); | ||
| 711 | } | ||
| 712 | } | ||
| 713 | } | ||
| 714 | |||
| 715 | // Runs on the worker. since_notify_ is the first failure since the last status entry or fence signal: the | ||
| 716 | // HRESULT a status entry reports. | ||
| 717 | void process(Entry& e) { | ||
| 718 | HRESULT hr = e.enqueue_error; | ||
| 719 | switch (e.type) { | ||
| 720 | case Entry::Type::Request: | ||
| 721 | // A cancelled request is a success whatever else is wrong with it (the GDK: not counted as a failure). | ||
| 722 | if (e.cancelled) { | ||
| 723 | hr = S_OK; | ||
| 724 | } else if (SUCCEEDED(hr)) { | ||
| 725 | hr = execute_request(e); | ||
| 726 | } | ||
| 727 | if (e.file && e.file_counted) e.file->finish_pending(); | ||
| 728 | if (FAILED(hr) && SUCCEEDED(since_notify_)) since_notify_ = hr; | ||
| 729 | break; | ||
| 730 | case Entry::Type::Status: | ||
| 731 | if (SUCCEEDED(hr)) { | ||
| 732 | e.status->complete(e.index, since_notify_); | ||
| 733 | since_notify_ = S_OK; | ||
| 734 | } | ||
| 735 | break; | ||
| 736 | case Entry::Type::Signal: | ||
| 737 | if (SUCCEEDED(hr)) hr = e.fence->Signal(e.value); | ||
| 738 | since_notify_ = S_OK; | ||
| 739 | break; | ||
| 740 | case Entry::Type::Event: | ||
| 741 | if (SUCCEEDED(hr) && !SetEvent(e.event)) hr = HRESULT_FROM_WIN32(GetLastError()); | ||
| 742 | break; | ||
| 743 | } | ||
| 744 | if (FAILED(hr)) { | ||
| 745 | DSTORAGE_ERROR_FIRST_FAILURE f{}; | ||
| 746 | f.HResult = hr; | ||
| 747 | describe(e, f); | ||
| 748 | record_failure(f); | ||
| 749 | } | ||
| 750 | } | ||
| 751 | |||
| 752 | HRESULT execute_request(const Entry& e) { | ||
| 753 | const DSTORAGE_REQUEST& r = e.request; | ||
| 754 | const uint64_t o = r.Options.AsUINT64; | ||
| 755 | if (o & options::kReservedMask) return E_DSTORAGE_RESERVED_FIELDS; | ||
| 756 | if (options::source_type(o) != desc_.SourceType) return E_DSTORAGE_INVALID_SOURCE_TYPE; | ||
| 757 | if (options::bcpack(o) > DSTORAGE_BCPACK_MODE_BC7) return E_DSTORAGE_INVALID_BCPACK_MODE; | ||
| 758 | if (options::swizzle(o) > DSTORAGE_SWIZZLE_MODE_4K_12K_4K_12K) return E_DSTORAGE_INVALID_SWIZZLE_MODE; | ||
| 759 | if (options::zlib(o) || options::bcpack(o) != 0 || options::swizzle(o) != 0) { | ||
| 760 | log_once("decompress", "zlib, BCPack and swizzle requests are not implemented yet (E_NOTIMPL)"); | ||
| 761 | return E_NOTIMPL; | ||
| 762 | } | ||
| 763 | if (o & (options::kDestinationPages | options::kSourcePages)) { | ||
| 764 | log_once("pages", "physical-page requests are not implemented yet (E_NOTIMPL)"); | ||
| 765 | return E_NOTIMPL; | ||
| 766 | } | ||
| 767 | if (r.IntermediateSize != 0) return E_DSTORAGE_INVALID_INTERMEDIATE_SIZE; // only zlib + BCPack use it | ||
| 768 | if (r.DestinationSize != r.SourceSize) return E_DSTORAGE_INVALID_DESTINATION_SIZE; | ||
| 769 | const bool memory = desc_.SourceType == DSTORAGE_REQUEST_SOURCE_MEMORY; | ||
| 770 | const uint64_t total = static_cast<uint64_t>(r.DestinationSize) + (memory ? r.SourceSize : 0u); | ||
| 771 | if (total > kMaxRequestBytes) return E_DSTORAGE_REQUEST_TOO_LARGE; | ||
| 772 | if (!r.Destination) return E_INVALIDARG; | ||
| 773 | if (memory) { | ||
| 774 | if (!r.Source) return E_INVALIDARG; | ||
| 775 | } else { | ||
| 776 | if (!r.File) return E_INVALIDARG; | ||
| 777 | if (!e.file_counted) return E_DSTORAGE_FILE_NOT_OPEN; | ||
| 778 | } | ||
| 779 | if (r.SourceSize == 0) return S_OK; // nothing to move; the reference does not say this fails | ||
| 780 | if (memory) return guarded_move(r.Destination, r.Source, r.SourceSize) ? S_OK : E_DSTORAGE_ACCESS_VIOLATION; | ||
| 781 | return read_file_range(e.file->handle(), r.FileOffset, r.Destination, r.SourceSize); | ||
| 782 | } | ||
| 783 | |||
| 784 | void describe(const Entry& e, DSTORAGE_ERROR_FIRST_FAILURE& f) const { | ||
| 785 | switch (e.type) { | ||
| 786 | case Entry::Type::Request: { | ||
| 787 | f.CommandType = DSTORAGE_COMMAND_TYPE_REQUEST; | ||
| 788 | auto& q = f.Request; | ||
| 789 | const DSTORAGE_REQUEST& r = e.request; | ||
| 790 | q.Options = r.Options; | ||
| 791 | q.Destination = r.Destination; | ||
| 792 | q.DestinationSize = r.DestinationSize; | ||
| 793 | q.SourceSize = r.SourceSize; | ||
| 794 | q.CancellationTag = r.CancellationTag; | ||
| 795 | if (options::source_type(r.Options.AsUINT64) == DSTORAGE_REQUEST_SOURCE_MEMORY) { | ||
| 796 | q.Source = r.Source; | ||
| 797 | } else { | ||
| 798 | q.FileOffset = r.FileOffset; | ||
| 799 | if (e.file) wcsncpy_s(q.Filename, e.file->path().c_str(), _TRUNCATE); | ||
| 800 | } | ||
| 801 | // The reference says the name stays readable until the request completes. | ||
| 802 | if (r.Name) | ||
| 803 | for (size_t i = 0; i + 1 < kRequestMaxName && r.Name[i]; ++i) q.Name[i] = r.Name[i]; | ||
| 804 | break; | ||
| 805 | } | ||
| 806 | case Entry::Type::Status: | ||
| 807 | f.CommandType = DSTORAGE_COMMAND_TYPE_STATUS; | ||
| 808 | f.Status.StatusArray = e.status_as_given; | ||
| 809 | f.Status.Index = e.index; | ||
| 810 | break; | ||
| 811 | case Entry::Type::Signal: | ||
| 812 | f.CommandType = DSTORAGE_COMMAND_TYPE_SIGNAL; | ||
| 813 | f.Signal.Fence = e.fence; | ||
| 814 | f.Signal.Value = e.value; | ||
| 815 | break; | ||
| 816 | case Entry::Type::Event: | ||
| 817 | f.CommandType = kCommandTypeEvent; | ||
| 818 | break; | ||
| 819 | } | ||
| 820 | } | ||
| 821 | |||
| 822 | void record_failure(const DSTORAGE_ERROR_FIRST_FAILURE& f) { | ||
| 823 | count_error(); | ||
| 824 | { | ||
| 825 | std::lock_guard lock(error_mutex_); | ||
| 826 | if (record_.FailureCount++ == 0) record_.FirstFailure = f; | ||
| 827 | if (error_event_) SetEvent(error_event_); | ||
| 828 | } | ||
| 829 | log_once("fail" + hr_text(f.HResult) + std::to_string(f.CommandType), | ||
| 830 | "an entry failed with " + hr_text(f.HResult) + " (command type " + std::to_string(f.CommandType) + | ||
| 831 | "; logged once per code)"); | ||
| 832 | } | ||
| 833 | |||
| 834 | void clear_record_locked() { | ||
| 835 | record_ = DSTORAGE_ERROR_RECORD{}; | ||
| 836 | record_.FirstFailure.CommandType = DSTORAGE_COMMAND_TYPE_NONE; | ||
| 837 | } | ||
| 838 | |||
| 839 | static void release_references(Entry& e) { | ||
| 840 | if (e.file) e.file->Release(); | ||
| 841 | if (e.status) e.status->Release(); | ||
| 842 | if (e.fence) e.fence->Release(); | ||
| 843 | if (e.event) CloseHandle(e.event); | ||
| 844 | e.event = nullptr; | ||
| 845 | e.file = nullptr; | ||
| 846 | e.status = nullptr; | ||
| 847 | e.fence = nullptr; | ||
| 848 | } | ||
| 849 | |||
| 850 | std::atomic<ULONG> refs_{1}; | ||
| 851 | const DSTORAGE_QUEUE_DESC1 desc_; | ||
| 852 | std::mutex close_mutex_; // one Close at a time | ||
| 853 | std::mutex mutex_; // entries_, submitted_, in_flight_, closing_, closed_ | ||
| 854 | std::condition_variable work_, space_, idle_; | ||
| 855 | std::deque<Entry> entries_; | ||
| 856 | size_t submitted_ = 0; // the first submitted_ entries are submitted | ||
| 857 | bool in_flight_ = false; | ||
| 858 | bool closing_ = false; | ||
| 859 | bool closed_ = false; | ||
| 860 | std::thread worker_; | ||
| 861 | HRESULT since_notify_ = S_OK; // worker only | ||
| 862 | std::mutex error_mutex_; | ||
| 863 | DSTORAGE_ERROR_RECORD record_{}; | ||
| 864 | HANDLE error_event_ = nullptr; | ||
| 865 | }; | ||
| 866 | |||
| 867 | // ---- The factory ---------------------------------------------------------------------------------------------------- | ||
| 868 | |||
| 869 | HRESULT validate_queue_desc(const DSTORAGE_QUEUE_DESC1& d) { | ||
| 870 | if (d.SourceType != DSTORAGE_REQUEST_SOURCE_FILE && d.SourceType != DSTORAGE_REQUEST_SOURCE_MEMORY) | ||
| 871 | return E_DSTORAGE_INVALID_SOURCE_TYPE; | ||
| 872 | if (d.Capacity < kMinQueueCapacity || d.Capacity > kMaxQueueCapacity) return E_DSTORAGE_INVALID_QUEUE_CAPACITY; | ||
| 873 | if (d.Priority < DSTORAGE_PRIORITY_FIRST || d.Priority > DSTORAGE_PRIORITY_LAST) | ||
| 874 | return E_DSTORAGE_INVALID_QUEUE_PRIORITY; | ||
| 875 | if (d.SourceType == DSTORAGE_REQUEST_SOURCE_MEMORY && d.Priority != DSTORAGE_PRIORITY_REALTIME) | ||
| 876 | return E_DSTORAGE_INVALID_MEMORY_QUEUE_PRIORITY; | ||
| 877 | if (d.Options.Reserved) return E_DSTORAGE_RESERVED_FIELDS; | ||
| 878 | return S_OK; | ||
| 879 | } | ||
| 880 | |||
| 881 | using CreateFileWFn = HANDLE(WINAPI*)(LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE); | ||
| 882 | |||
| 883 | // The title's own CreateFileW: the GAME: mapping and overlay when file_system.cpp is linked, else the host's. | ||
| 884 | CreateFileWFn create_file_w() { | ||
| 885 | if (void* fn = rt::shims::find("kernel32.dll", "CreateFileW")) return reinterpret_cast<CreateFileWFn>(fn); | ||
| 886 | return &::CreateFileW; | ||
| 887 | } | ||
| 888 | |||
| 889 | class Factory final : public IDStorageFactoryX2 { | ||
| 890 | public: | ||
| 891 | HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppv) override { | ||
| 892 | if (!ppv) return E_POINTER; | ||
| 893 | *ppv = nullptr; | ||
| 894 | if (!iid_policy().accept_query(riid, Kind::Factory, "IDStorageFactoryX::QueryInterface")) return E_NOINTERFACE; | ||
| 895 | AddRef(); | ||
| 896 | *ppv = static_cast<IDStorageFactoryX2*>(this); | ||
| 897 | return S_OK; | ||
| 898 | } | ||
| 899 | ULONG STDMETHODCALLTYPE AddRef() override { return ++refs_; } | ||
| 900 | // One factory for the process; its count never drops below one, so it is never freed. | ||
| 901 | ULONG STDMETHODCALLTYPE Release() override { | ||
| 902 | ULONG r = refs_.load(); | ||
| 903 | while (r > 1 && !refs_.compare_exchange_weak(r, r - 1)) {} | ||
| 904 | return r > 1 ? r - 1 : 1; | ||
| 905 | } | ||
| 906 | |||
| 907 | HRESULT STDMETHODCALLTYPE CreateQueue(const DSTORAGE_QUEUE_DESC* desc, REFIID riid, void** ppv) override { | ||
| 908 | if (!ppv) return bad_call("IDStorageFactoryX::CreateQueue", E_POINTER, "null ppv"); | ||
| 909 | *ppv = nullptr; | ||
| 910 | if (!desc) return bad_call("IDStorageFactoryX::CreateQueue", E_INVALIDARG, "null desc"); | ||
| 911 | DSTORAGE_QUEUE_DESC1 d{}; | ||
| 912 | d.SourceType = desc->SourceType; | ||
| 913 | d.Capacity = desc->Capacity; | ||
| 914 | d.Priority = desc->Priority; | ||
| 915 | d.Name = desc->Name; | ||
| 916 | return create_queue(d, riid, ppv, "IDStorageFactoryX::CreateQueue"); | ||
| 917 | } | ||
| 918 | |||
| 919 | HRESULT STDMETHODCALLTYPE CreateQueue1(const DSTORAGE_QUEUE_DESC1* desc, REFIID riid, void** ppv) override { | ||
| 920 | if (!ppv) return bad_call("IDStorageFactoryX2::CreateQueue1", E_POINTER, "null ppv"); | ||
| 921 | *ppv = nullptr; | ||
| 922 | if (!desc) return bad_call("IDStorageFactoryX2::CreateQueue1", E_INVALIDARG, "null desc"); | ||
| 923 | return create_queue(*desc, riid, ppv, "IDStorageFactoryX2::CreateQueue1"); | ||
| 924 | } | ||
| 925 | |||
| 926 | HRESULT STDMETHODCALLTYPE OpenFile(const WCHAR* path, REFIID riid, void** ppv) override { | ||
| 927 | if (!ppv) return bad_call("IDStorageFactoryX::OpenFile", E_POINTER, "null ppv"); | ||
| 928 | *ppv = nullptr; | ||
| 929 | if (!path || !*path) return bad_call("IDStorageFactoryX::OpenFile", E_INVALIDARG, "null or empty path"); | ||
| 930 | if (!iid_policy().accept_creating(riid, Kind::File, "IDStorageFactoryX::OpenFile")) return E_NOINTERFACE; | ||
| 931 | const HANDLE h = create_file_w()(path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, | ||
| 932 | FILE_ATTRIBUTE_NORMAL, nullptr); | ||
| 933 | if (h == INVALID_HANDLE_VALUE) { | ||
| 934 | const DWORD error = GetLastError(); | ||
| 935 | return HRESULT_FROM_WIN32(error ? error : ERROR_FILE_NOT_FOUND); | ||
| 936 | } | ||
| 937 | File* f = new (std::nothrow) File(h, path); | ||
| 938 | if (!f) { | ||
| 939 | CloseHandle(h); | ||
| 940 | return E_OUTOFMEMORY; | ||
| 941 | } | ||
| 942 | *ppv = static_cast<IDStorageFileX*>(f); | ||
| 943 | return S_OK; | ||
| 944 | } | ||
| 945 | |||
| 946 | HRESULT STDMETHODCALLTYPE CreateStatusArray(UINT32 capacity, PCSTR, REFIID riid, void** ppv) override { | ||
| 947 | if (!ppv) return bad_call("IDStorageFactoryX::CreateStatusArray", E_POINTER, "null ppv"); | ||
| 948 | *ppv = nullptr; | ||
| 949 | if (capacity == 0) return bad_call("IDStorageFactoryX::CreateStatusArray", E_INVALIDARG, "capacity 0"); | ||
| 950 | if (!iid_policy().accept_creating(riid, Kind::StatusArray, "IDStorageFactoryX::CreateStatusArray")) | ||
| 951 | return E_NOINTERFACE; | ||
| 952 | std::unique_ptr<std::atomic<HRESULT>[]> entries(new (std::nothrow) std::atomic<HRESULT>[capacity]); | ||
| 953 | if (!entries) return E_OUTOFMEMORY; | ||
| 954 | auto* a = new (std::nothrow) StatusArray(capacity, std::move(entries)); | ||
| 955 | if (!a) return E_OUTOFMEMORY; | ||
| 956 | *ppv = static_cast<IDStorageStatusArrayX*>(a); | ||
| 957 | return S_OK; | ||
| 958 | } | ||
| 959 | |||
| 960 | // DirectStorage's own worker work would be pinned to these cores on the console. The mask names console cores, | ||
| 961 | // so it is stored and not applied to the host's. | ||
| 962 | HRESULT STDMETHODCALLTYPE SetCpuAffinity(UINT64 affinity) override { | ||
| 963 | cpu_affinity_.store(affinity); | ||
| 964 | return S_OK; | ||
| 965 | } | ||
| 966 | void STDMETHODCALLTYPE SetDebugFlags(UINT32 flags) override { debug_flags_.store(flags); } | ||
| 967 | |||
| 968 | HRESULT STDMETHODCALLTYPE SetStagingBufferSize(UINT32 size) override { | ||
| 969 | if (std::find(std::begin(kStagingBufferSizes), std::end(kStagingBufferSizes), size) == | ||
| 970 | std::end(kStagingBufferSizes)) | ||
| 971 | return bad_call("IDStorageFactoryX::SetStagingBufferSize", E_DSTORAGE_INVALID_STAGING_BUFFER_SIZE, | ||
| 972 | "not one of 0, 16, 20, 24, 28 or 32 MiB"); | ||
| 973 | std::lock_guard lock(stats().mutex); | ||
| 974 | if (stats().live_queues || stats().s.CurrentOpenFileCount) return E_DSTORAGE_STAGING_BUFFER_LOCKED; | ||
| 975 | staging_size_ = size; // there is no staging buffer here; the size is only kept | ||
| 976 | return S_OK; | ||
| 977 | } | ||
| 978 | |||
| 979 | void STDMETHODCALLTYPE GetStats(DSTORAGE_STATS* out) override { | ||
| 980 | if (!out) { | ||
| 981 | bad_call("IDStorageFactoryX1::GetStats", E_POINTER, "null stats"); | ||
| 982 | return; | ||
| 983 | } | ||
| 984 | std::lock_guard lock(stats().mutex); | ||
| 985 | *out = stats().s; | ||
| 986 | } | ||
| 987 | |||
| 988 | private: | ||
| 989 | HRESULT create_queue(const DSTORAGE_QUEUE_DESC1& desc, REFIID riid, void** ppv, const char* caller) { | ||
| 990 | if (const HRESULT hr = validate_queue_desc(desc); FAILED(hr)) return bad_call(caller, hr, "invalid queue desc"); | ||
| 991 | if (!iid_policy().accept_creating(riid, Kind::Queue, caller)) return E_NOINTERFACE; | ||
| 992 | auto* q = new (std::nothrow) Queue(desc); | ||
| 993 | if (!q) return E_OUTOFMEMORY; | ||
| 994 | if (!q->start()) { | ||
| 995 | q->Release(); | ||
| 996 | return E_OUTOFMEMORY; | ||
| 997 | } | ||
| 998 | *ppv = static_cast<IDStorageQueueX2*>(q); | ||
| 999 | return S_OK; | ||
| 1000 | } | ||
| 1001 | |||
| 1002 | std::atomic<ULONG> refs_{1}; | ||
| 1003 | std::atomic<uint64_t> cpu_affinity_{~0ull}; | ||
| 1004 | std::atomic<uint32_t> debug_flags_{0}; | ||
| 1005 | uint32_t staging_size_ = 32u << 20; // under stats().mutex | ||
| 1006 | }; | ||
| 1007 | |||
| 1008 | Factory& factory() { | ||
| 1009 | static auto* f = new Factory; | ||
| 1010 | return *f; | ||
| 1011 | } | ||
| 1012 | |||
| 1013 | HRESULT WINAPI dstorage_get_factory(REFIID riid, void** ppv) { | ||
| 1014 | if (!ppv) return bad_call("DStorageGetFactory", E_POINTER, "null ppv"); | ||
| 1015 | *ppv = nullptr; | ||
| 1016 | if (!iid_policy().accept_creating(riid, Kind::Factory, "DStorageGetFactory")) return E_NOINTERFACE; | ||
| 1017 | Factory& f = factory(); | ||
| 1018 | f.AddRef(); | ||
| 1019 | *ppv = static_cast<IDStorageFactoryX2*>(&f); | ||
| 1020 | return S_OK; | ||
| 1021 | } | ||
| 1022 | |||
| 1023 | } // namespace | ||
| 1024 | |||
| 1025 | // The console module names; docs/directstorage.md has the evidence and the confidence for each. | ||
| 1026 | SHIM_TABLE(dstorage, | ||
| 1027 | {"dstorage_xs.dll", "DStorageGetFactory", reinterpret_cast<void*>(&dstorage_get_factory)}, | ||
| 1028 | {"xcrdapi.dll", "DStorageGetFactory", reinterpret_cast<void*>(&dstorage_get_factory)}, | ||
| 1029 | {"dstorage.dll", "DStorageGetFactory", reinterpret_cast<void*>(&dstorage_get_factory)}, | ||
| 1030 | ); | ||
diff --git a/runtime/shims/dstorage_xs_abi.h b/runtime/shims/dstorage_xs_abi.h new file mode 100644 index 000000000..854f8dbf9 --- /dev/null +++ b/runtime/shims/dstorage_xs_abi.h | |||
| @@ -0,0 +1,378 @@ | |||
| 1 | // Xbox DirectStorage ABI: the console API (dstorage_xs.h on the console) a title calls through DStorageGetFactory. | ||
| 2 | // | ||
| 3 | // These are Leonida's own declarations. Names, member order and types follow the public GDK reference | ||
| 4 | // (learn.microsoft.com/gaming/gdk/docs/reference/system/dstorage); docs/directstorage.md has the evidence for which | ||
| 5 | // DLL serves it and how sure each part is. No Microsoft header is copied. | ||
| 6 | // | ||
| 7 | // VERIFY (docs/directstorage.md "What is not public"): | ||
| 8 | // - Vtable order. The reference lists methods alphabetically; the order below is the PC dstorage.h order for the | ||
| 9 | // methods both APIs share, with the console-only ones (SetCpuAffinity, GetStats, CreateQueue1, GetHandle, | ||
| 10 | // Query1) where the GDK overview lists them. Reorder a declaration here and every object follows. | ||
| 11 | // - Interface ids. The X interfaces' ids are not public. The one declared id is the PC IDStorageStatusArray's, whose | ||
| 12 | // interface has the same shape (console D3D12 binaries reuse PC SDK ids); dstorage.cpp's creating calls learn and | ||
| 13 | // log the others. The other PC ids are declared only so they can be refused. | ||
| 14 | // - The capacity bounds, DSTORAGE_REQUEST_MAX_NAME and the E_DSTORAGE_* values are the PC header's. | ||
| 15 | #pragma once | ||
| 16 | #include <windows.h> | ||
| 17 | #include <unknwn.h> | ||
| 18 | |||
| 19 | #include <cstddef> | ||
| 20 | #include <cstdint> | ||
| 21 | |||
| 22 | struct ID3D12Fence; // d3d12.h; the shim calls Signal through it | ||
| 23 | |||
| 24 | namespace dsx { | ||
| 25 | |||
| 26 | #pragma warning(push) | ||
| 27 | #pragma warning(disable : 4201) // nameless struct/union: the reference declares the members that way | ||
| 28 | |||
| 29 | // ---- Constants and enumerations ----------------------------------------------------------------------------------- | ||
| 30 | |||
| 31 | constexpr uint16_t kMinQueueCapacity = 0x80; // DSTORAGE_MIN_QUEUE_CAPACITY (PC value) | ||
| 32 | constexpr uint16_t kMaxQueueCapacity = 0x2000; // DSTORAGE_MAX_QUEUE_CAPACITY (PC value) | ||
| 33 | constexpr size_t kRequestMaxName = 64; // DSTORAGE_REQUEST_MAX_NAME (PC value) | ||
| 34 | constexpr uint64_t kMaxRequestBytes = 1ull << 30; // DSTORAGE_MAX_REQUEST_SIZE: source + destination | ||
| 35 | constexpr uint32_t kMax4kPageDestinationSize = 32u << 20; // DSTORAGE_MAX_4K_PAGE_DESTINATION_SIZE | ||
| 36 | |||
| 37 | enum DSTORAGE_PRIORITY : int8_t { | ||
| 38 | DSTORAGE_PRIORITY_LOW = -1, | ||
| 39 | DSTORAGE_PRIORITY_NORMAL = 0, | ||
| 40 | DSTORAGE_PRIORITY_HIGH = 1, | ||
| 41 | DSTORAGE_PRIORITY_REALTIME = 2, | ||
| 42 | DSTORAGE_PRIORITY_FIRST = DSTORAGE_PRIORITY_LOW, | ||
| 43 | DSTORAGE_PRIORITY_LAST = DSTORAGE_PRIORITY_REALTIME, | ||
| 44 | DSTORAGE_PRIORITY_COUNT = 4, | ||
| 45 | }; | ||
| 46 | |||
| 47 | enum DSTORAGE_REQUEST_SOURCE_TYPE : uint64_t { | ||
| 48 | DSTORAGE_REQUEST_SOURCE_FILE = 0, | ||
| 49 | DSTORAGE_REQUEST_SOURCE_MEMORY = 1, | ||
| 50 | }; | ||
| 51 | |||
| 52 | enum DSTORAGE_BCPACK_MODE : uint64_t { | ||
| 53 | DSTORAGE_BCPACK_MODE_NONE = 0, | ||
| 54 | DSTORAGE_BCPACK_MODE_BC1 = 1, | ||
| 55 | DSTORAGE_BCPACK_MODE_BC2 = 2, | ||
| 56 | DSTORAGE_BCPACK_MODE_BC3 = 3, | ||
| 57 | DSTORAGE_BCPACK_MODE_BC4 = 4, | ||
| 58 | DSTORAGE_BCPACK_MODE_BC5 = 5, | ||
| 59 | DSTORAGE_BCPACK_MODE_BC6H = 6, | ||
| 60 | DSTORAGE_BCPACK_MODE_BC7 = 7, | ||
| 61 | }; | ||
| 62 | |||
| 63 | enum DSTORAGE_SWIZZLE_MODE : uint64_t { | ||
| 64 | DSTORAGE_SWIZZLE_MODE_NONE = 0, | ||
| 65 | DSTORAGE_SWIZZLE_MODE_4K_4K_8K = 1, | ||
| 66 | DSTORAGE_SWIZZLE_MODE_4K_4K_24K = 2, | ||
| 67 | DSTORAGE_SWIZZLE_MODE_4K_12K_4K_4K_8K = 3, | ||
| 68 | DSTORAGE_SWIZZLE_MODE_4K_12K_4K_12K = 4, | ||
| 69 | }; | ||
| 70 | |||
| 71 | enum DSTORAGE_COMMAND_TYPE : int32_t { | ||
| 72 | DSTORAGE_COMMAND_TYPE_NONE = -1, | ||
| 73 | DSTORAGE_COMMAND_TYPE_REQUEST = 0, | ||
| 74 | DSTORAGE_COMMAND_TYPE_STATUS = 1, | ||
| 75 | DSTORAGE_COMMAND_TYPE_SIGNAL = 2, | ||
| 76 | }; | ||
| 77 | // Not in the console enum: the PC API's DSTORAGE_COMMAND_TYPE_EVENT, used here for a failed EnqueueSetEvent. | ||
| 78 | constexpr DSTORAGE_COMMAND_TYPE kCommandTypeEvent = static_cast<DSTORAGE_COMMAND_TYPE>(3); | ||
| 79 | |||
| 80 | enum DSTORAGE_DEBUG : uint32_t { | ||
| 81 | DSTORAGE_DEBUG_NONE = 0x00, | ||
| 82 | DSTORAGE_DEBUG_SHOW_ERRORS = 0x01, | ||
| 83 | DSTORAGE_DEBUG_BREAK_ON_ERROR = 0x02, | ||
| 84 | DSTORAGE_DEBUG_RECORD_OBJECT_NAMES = 0x04, | ||
| 85 | }; | ||
| 86 | |||
| 87 | // The staging-buffer sizes the GDK overview lists as valid (its enumeration page is not public). | ||
| 88 | constexpr uint32_t kStagingBufferSizes[] = {0, 16u << 20, 20u << 20, 24u << 20, 28u << 20, 32u << 20}; | ||
| 89 | |||
| 90 | // ---- Errors (FACILITY_GAME, the PC dstorageerr.h values) ---------------------------------------------------------- | ||
| 91 | |||
| 92 | constexpr HRESULT E_DSTORAGE_INVALID_QUEUE_CAPACITY = static_cast<HRESULT>(0x89240003L); | ||
| 93 | constexpr HRESULT E_DSTORAGE_END_OF_FILE = static_cast<HRESULT>(0x89240007L); | ||
| 94 | constexpr HRESULT E_DSTORAGE_REQUEST_TOO_LARGE = static_cast<HRESULT>(0x89240008L); | ||
| 95 | constexpr HRESULT E_DSTORAGE_ACCESS_VIOLATION = static_cast<HRESULT>(0x89240009L); | ||
| 96 | constexpr HRESULT E_DSTORAGE_FILE_NOT_OPEN = static_cast<HRESULT>(0x8924000BL); | ||
| 97 | constexpr HRESULT E_DSTORAGE_RESERVED_FIELDS = static_cast<HRESULT>(0x8924000CL); | ||
| 98 | constexpr HRESULT E_DSTORAGE_INVALID_BCPACK_MODE = static_cast<HRESULT>(0x8924000DL); | ||
| 99 | constexpr HRESULT E_DSTORAGE_INVALID_SWIZZLE_MODE = static_cast<HRESULT>(0x8924000EL); | ||
| 100 | constexpr HRESULT E_DSTORAGE_INVALID_DESTINATION_SIZE = static_cast<HRESULT>(0x8924000FL); | ||
| 101 | constexpr HRESULT E_DSTORAGE_QUEUE_CLOSED = static_cast<HRESULT>(0x89240010L); | ||
| 102 | constexpr HRESULT E_DSTORAGE_INVALID_QUEUE_PRIORITY = static_cast<HRESULT>(0x89240013L); | ||
| 103 | constexpr HRESULT E_DSTORAGE_INDEX_BOUND = static_cast<HRESULT>(0x89240015L); | ||
| 104 | constexpr HRESULT E_DSTORAGE_INVALID_FILE_HANDLE = static_cast<HRESULT>(0x89240017L); | ||
| 105 | constexpr HRESULT E_DSTORAGE_INVALID_FILE_OFFSET = static_cast<HRESULT>(0x8924001AL); | ||
| 106 | constexpr HRESULT E_DSTORAGE_INVALID_SOURCE_TYPE = static_cast<HRESULT>(0x8924001BL); | ||
| 107 | constexpr HRESULT E_DSTORAGE_INVALID_INTERMEDIATE_SIZE = static_cast<HRESULT>(0x8924001CL); | ||
| 108 | constexpr HRESULT E_DSTORAGE_STAGING_BUFFER_LOCKED = static_cast<HRESULT>(0x8924001FL); | ||
| 109 | constexpr HRESULT E_DSTORAGE_INVALID_STAGING_BUFFER_SIZE = static_cast<HRESULT>(0x89240020L); | ||
| 110 | constexpr HRESULT E_DSTORAGE_INVALID_FENCE = static_cast<HRESULT>(0x89240022L); | ||
| 111 | constexpr HRESULT E_DSTORAGE_INVALID_STATUS_ARRAY = static_cast<HRESULT>(0x89240023L); | ||
| 112 | constexpr HRESULT E_DSTORAGE_INVALID_MEMORY_QUEUE_PRIORITY = static_cast<HRESULT>(0x89240024L); | ||
| 113 | |||
| 114 | // ---- Structures --------------------------------------------------------------------------------------------------- | ||
| 115 | |||
| 116 | // DSTORAGE_REQUEST_OPTIONS as the reference declares it. The shim reads AsUINT64 through the bit positions below | ||
| 117 | // (MSVC allocates bit-fields from bit 0 up); dstorage_test checks that the two agree. | ||
| 118 | union DSTORAGE_REQUEST_OPTIONS { | ||
| 119 | struct { | ||
| 120 | uint64_t ZlibDecompress : 1; | ||
| 121 | DSTORAGE_BCPACK_MODE BcpackMode : 4; | ||
| 122 | DSTORAGE_SWIZZLE_MODE SwizzleMode : 16; | ||
| 123 | uint64_t DestinationIsPhysicalPages : 1; | ||
| 124 | DSTORAGE_REQUEST_SOURCE_TYPE SourceType : 1; | ||
| 125 | uint64_t SourceIsPhysicalPages : 1; | ||
| 126 | uint64_t Reserved : 40; | ||
| 127 | }; | ||
| 128 | uint64_t AsUINT64; | ||
| 129 | }; | ||
| 130 | static_assert(sizeof(DSTORAGE_REQUEST_OPTIONS) == 8); | ||
| 131 | |||
| 132 | namespace options { | ||
| 133 | constexpr uint64_t kZlib = 1ull << 0; | ||
| 134 | constexpr unsigned kBcpackShift = 1; | ||
| 135 | constexpr uint64_t kBcpackMask = 0xFull << kBcpackShift; | ||
| 136 | constexpr unsigned kSwizzleShift = 5; | ||
| 137 | constexpr uint64_t kSwizzleMask = 0xFFFFull << kSwizzleShift; | ||
| 138 | constexpr uint64_t kDestinationPages = 1ull << 21; | ||
| 139 | constexpr uint64_t kSourceMemory = 1ull << 22; // SourceType == DSTORAGE_REQUEST_SOURCE_MEMORY | ||
| 140 | constexpr uint64_t kSourcePages = 1ull << 23; | ||
| 141 | constexpr uint64_t kReservedMask = ~((1ull << 24) - 1); | ||
| 142 | |||
| 143 | constexpr bool zlib(uint64_t o) { return (o & kZlib) != 0; } | ||
| 144 | constexpr uint64_t bcpack(uint64_t o) { return (o & kBcpackMask) >> kBcpackShift; } | ||
| 145 | constexpr uint64_t swizzle(uint64_t o) { return (o & kSwizzleMask) >> kSwizzleShift; } | ||
| 146 | constexpr DSTORAGE_REQUEST_SOURCE_TYPE source_type(uint64_t o) { | ||
| 147 | return (o & kSourceMemory) ? DSTORAGE_REQUEST_SOURCE_MEMORY : DSTORAGE_REQUEST_SOURCE_FILE; | ||
| 148 | } | ||
| 149 | } // namespace options | ||
| 150 | |||
| 151 | struct IDStorageFileX; | ||
| 152 | struct IDStorageStatusArrayX; | ||
| 153 | |||
| 154 | struct DSTORAGE_REQUEST { | ||
| 155 | DSTORAGE_REQUEST_OPTIONS Options; | ||
| 156 | union { | ||
| 157 | void* Destination; | ||
| 158 | struct { | ||
| 159 | PULONG_PTR DestinationPageArray; | ||
| 160 | UINT16 DestinationPageOffset; | ||
| 161 | }; | ||
| 162 | }; | ||
| 163 | UINT32 DestinationSize; | ||
| 164 | union { | ||
| 165 | struct { | ||
| 166 | IDStorageFileX* File; | ||
| 167 | UINT64 FileOffset; | ||
| 168 | }; | ||
| 169 | const void* Source; | ||
| 170 | struct { | ||
| 171 | PULONG_PTR SourcePageArray; | ||
| 172 | UINT16 SourcePageOffset; | ||
| 173 | }; | ||
| 174 | }; | ||
| 175 | UINT32 SourceSize; | ||
| 176 | UINT32 IntermediateSize; | ||
| 177 | UINT64 CancellationTag; | ||
| 178 | const CHAR* Name; | ||
| 179 | }; | ||
| 180 | static_assert(offsetof(DSTORAGE_REQUEST, Destination) == 8 && offsetof(DSTORAGE_REQUEST, DestinationPageOffset) == 16); | ||
| 181 | static_assert(offsetof(DSTORAGE_REQUEST, DestinationSize) == 24 && offsetof(DSTORAGE_REQUEST, File) == 32); | ||
| 182 | static_assert(offsetof(DSTORAGE_REQUEST, FileOffset) == 40 && offsetof(DSTORAGE_REQUEST, Source) == 32); | ||
| 183 | static_assert(offsetof(DSTORAGE_REQUEST, SourcePageOffset) == 40 && offsetof(DSTORAGE_REQUEST, SourceSize) == 48); | ||
| 184 | static_assert(offsetof(DSTORAGE_REQUEST, IntermediateSize) == 52 && offsetof(DSTORAGE_REQUEST, CancellationTag) == 56); | ||
| 185 | static_assert(offsetof(DSTORAGE_REQUEST, Name) == 64 && sizeof(DSTORAGE_REQUEST) == 72); | ||
| 186 | |||
| 187 | struct DSTORAGE_QUEUE_DESC { | ||
| 188 | DSTORAGE_REQUEST_SOURCE_TYPE SourceType; | ||
| 189 | UINT16 Capacity; | ||
| 190 | DSTORAGE_PRIORITY Priority; | ||
| 191 | const CHAR* Name; | ||
| 192 | }; | ||
| 193 | static_assert(offsetof(DSTORAGE_QUEUE_DESC, Capacity) == 8 && offsetof(DSTORAGE_QUEUE_DESC, Priority) == 10); | ||
| 194 | static_assert(offsetof(DSTORAGE_QUEUE_DESC, Name) == 16 && sizeof(DSTORAGE_QUEUE_DESC) == 24); | ||
| 195 | |||
| 196 | union DSTORAGE_QUEUE_OPTIONS { | ||
| 197 | struct { | ||
| 198 | uint64_t AutoSubmitDisabled : 1; | ||
| 199 | uint64_t Reserved : 63; | ||
| 200 | }; | ||
| 201 | uint64_t AsUINT64; | ||
| 202 | }; | ||
| 203 | static_assert(sizeof(DSTORAGE_QUEUE_OPTIONS) == 8); | ||
| 204 | |||
| 205 | struct DSTORAGE_QUEUE_DESC1 { | ||
| 206 | DSTORAGE_REQUEST_SOURCE_TYPE SourceType; | ||
| 207 | UINT16 Capacity; | ||
| 208 | DSTORAGE_PRIORITY Priority; | ||
| 209 | const CHAR* Name; | ||
| 210 | DSTORAGE_QUEUE_OPTIONS Options; | ||
| 211 | }; | ||
| 212 | static_assert(offsetof(DSTORAGE_QUEUE_DESC1, Options) == 24 && sizeof(DSTORAGE_QUEUE_DESC1) == 32); | ||
| 213 | |||
| 214 | struct DSTORAGE_QUEUE_INFO { | ||
| 215 | DSTORAGE_QUEUE_DESC Desc; | ||
| 216 | UINT16 EmptySlotCount; | ||
| 217 | UINT16 RequestCountUntilAutoSubmit; | ||
| 218 | }; | ||
| 219 | static_assert(offsetof(DSTORAGE_QUEUE_INFO, EmptySlotCount) == 24 && sizeof(DSTORAGE_QUEUE_INFO) == 32); | ||
| 220 | |||
| 221 | struct DSTORAGE_QUEUE_INFO1 { | ||
| 222 | DSTORAGE_QUEUE_DESC1 Desc; | ||
| 223 | UINT16 EmptySlotCount; | ||
| 224 | UINT16 RequestCountUntilAutoSubmit; | ||
| 225 | }; | ||
| 226 | static_assert(offsetof(DSTORAGE_QUEUE_INFO1, EmptySlotCount) == 32 && sizeof(DSTORAGE_QUEUE_INFO1) == 40); | ||
| 227 | |||
| 228 | // The reference declares the three failure kinds as unnamed structs inside DSTORAGE_ERROR_RECORD; they are named | ||
| 229 | // here (Leonida's names) so the shim can fill them. The layout is the same. | ||
| 230 | struct DSTORAGE_ERROR_REQUEST { | ||
| 231 | DSTORAGE_REQUEST_OPTIONS Options; | ||
| 232 | void* Destination; | ||
| 233 | UINT32 DestinationSize; | ||
| 234 | WCHAR Filename[MAX_PATH]; | ||
| 235 | UINT64 FileOffset; | ||
| 236 | const void* Source; | ||
| 237 | UINT32 SourceSize; | ||
| 238 | UINT64 CancellationTag; | ||
| 239 | CHAR Name[kRequestMaxName]; | ||
| 240 | }; | ||
| 241 | struct DSTORAGE_ERROR_STATUS { | ||
| 242 | IDStorageStatusArrayX* StatusArray; | ||
| 243 | UINT32 Index; | ||
| 244 | }; | ||
| 245 | struct DSTORAGE_ERROR_SIGNAL { | ||
| 246 | ID3D12Fence* Fence; | ||
| 247 | UINT64 Value; | ||
| 248 | }; | ||
| 249 | struct DSTORAGE_ERROR_FIRST_FAILURE { | ||
| 250 | HRESULT HResult; | ||
| 251 | DSTORAGE_COMMAND_TYPE CommandType; | ||
| 252 | union { | ||
| 253 | DSTORAGE_ERROR_REQUEST Request; | ||
| 254 | DSTORAGE_ERROR_STATUS Status; | ||
| 255 | DSTORAGE_ERROR_SIGNAL Signal; | ||
| 256 | }; | ||
| 257 | }; | ||
| 258 | struct DSTORAGE_ERROR_RECORD { | ||
| 259 | UINT32 FailureCount; | ||
| 260 | DSTORAGE_ERROR_FIRST_FAILURE FirstFailure; | ||
| 261 | }; | ||
| 262 | static_assert(offsetof(DSTORAGE_ERROR_RECORD, FirstFailure) == 8); | ||
| 263 | static_assert(offsetof(DSTORAGE_ERROR_FIRST_FAILURE, Request) == 8); | ||
| 264 | static_assert(offsetof(DSTORAGE_ERROR_REQUEST, Filename) == 20 && offsetof(DSTORAGE_ERROR_REQUEST, FileOffset) == 544); | ||
| 265 | static_assert(offsetof(DSTORAGE_ERROR_REQUEST, Name) == 576 && sizeof(DSTORAGE_ERROR_REQUEST) == 640); | ||
| 266 | static_assert(sizeof(DSTORAGE_ERROR_RECORD) == 656); | ||
| 267 | |||
| 268 | struct DSTORAGE_PRIORITY_STATS { | ||
| 269 | UINT32 CurrentQueueCount; | ||
| 270 | UINT32 MaxQueueCount; | ||
| 271 | UINT32 CumulativeQueueCount; | ||
| 272 | UINT32 TotalStatusCount; | ||
| 273 | UINT32 TotalFenceCount; | ||
| 274 | UINT32 PendingRequestCount; | ||
| 275 | UINT32 TotalRequestCount; | ||
| 276 | UINT64 TotalRequestSourceByteCount; | ||
| 277 | UINT64 TotalRequestDestinationByteCount; | ||
| 278 | }; | ||
| 279 | static_assert(sizeof(DSTORAGE_PRIORITY_STATS) == 48); | ||
| 280 | |||
| 281 | struct DSTORAGE_STATS { | ||
| 282 | DSTORAGE_PRIORITY_STATS LowPriorityStats; | ||
| 283 | DSTORAGE_PRIORITY_STATS NormalPriorityStats; | ||
| 284 | DSTORAGE_PRIORITY_STATS HighPriorityStats; | ||
| 285 | DSTORAGE_PRIORITY_STATS RealtimePriorityStats; | ||
| 286 | UINT8 CurrentOpenVirtualDiskCount; | ||
| 287 | UINT8 MaxOpenVirtualDiskCount; | ||
| 288 | UINT32 CumulativeOpenVirtualDiskCount; | ||
| 289 | UINT32 PendingStorageRequestCount; | ||
| 290 | UINT32 TotalStorageRequestCount; | ||
| 291 | UINT32 StorageRetries; | ||
| 292 | UINT32 StorageErrors; | ||
| 293 | UINT32 PendingDecompressionRequestCount; | ||
| 294 | UINT32 TotalDecompressionRequestCount; | ||
| 295 | UINT32 DecompressionErrors; | ||
| 296 | UINT32 StagingBufferStallCount; | ||
| 297 | UINT32 CurrentOpenFileCount; | ||
| 298 | UINT32 MaxOpenFileCount; | ||
| 299 | UINT32 CumulativeOpenFileCount; | ||
| 300 | UINT32 CurrentTitleMemoryUsageBytes; | ||
| 301 | UINT32 MaxTitleMemoryUsageBytes; | ||
| 302 | UINT32 ErrorCount; | ||
| 303 | }; | ||
| 304 | |||
| 305 | #pragma warning(pop) | ||
| 306 | |||
| 307 | // ---- Interfaces, in the vtable order chosen above --------------------------------------------------------------- | ||
| 308 | |||
| 309 | struct IDStorageStatusArrayX : IUnknown { | ||
| 310 | virtual bool STDMETHODCALLTYPE IsComplete(UINT32 index) = 0; | ||
| 311 | virtual HRESULT STDMETHODCALLTYPE GetHResult(UINT32 index) = 0; | ||
| 312 | }; | ||
| 313 | |||
| 314 | struct IDStorageFileX : IUnknown { | ||
| 315 | virtual void STDMETHODCALLTYPE Close() = 0; | ||
| 316 | virtual HANDLE STDMETHODCALLTYPE GetHandle() = 0; | ||
| 317 | }; | ||
| 318 | |||
| 319 | struct IDStorageQueueX : IUnknown { | ||
| 320 | virtual void STDMETHODCALLTYPE EnqueueRequest(const DSTORAGE_REQUEST* request) = 0; | ||
| 321 | virtual void STDMETHODCALLTYPE EnqueueStatus(IDStorageStatusArrayX* status_array, UINT32 index) = 0; | ||
| 322 | virtual void STDMETHODCALLTYPE EnqueueSignal(ID3D12Fence* fence, UINT64 value) = 0; | ||
| 323 | virtual void STDMETHODCALLTYPE Submit() = 0; | ||
| 324 | virtual void STDMETHODCALLTYPE CancelRequestsWithTag(UINT64 mask, UINT64 value) = 0; | ||
| 325 | virtual void STDMETHODCALLTYPE Close() = 0; | ||
| 326 | virtual HANDLE STDMETHODCALLTYPE GetErrorEvent() = 0; | ||
| 327 | virtual void STDMETHODCALLTYPE RetrieveErrorRecord(DSTORAGE_ERROR_RECORD* record) = 0; | ||
| 328 | virtual void STDMETHODCALLTYPE Query(DSTORAGE_QUEUE_INFO* info) = 0; | ||
| 329 | }; | ||
| 330 | |||
| 331 | struct IDStorageQueueX1 : IDStorageQueueX { | ||
| 332 | virtual void STDMETHODCALLTYPE EnqueueSetEvent(HANDLE handle) = 0; | ||
| 333 | }; | ||
| 334 | |||
| 335 | struct IDStorageQueueX2 : IDStorageQueueX1 { | ||
| 336 | virtual void STDMETHODCALLTYPE Query1(DSTORAGE_QUEUE_INFO1* info) = 0; | ||
| 337 | }; | ||
| 338 | |||
| 339 | struct IDStorageFactoryX : IUnknown { | ||
| 340 | virtual HRESULT STDMETHODCALLTYPE CreateQueue(const DSTORAGE_QUEUE_DESC* desc, REFIID riid, void** ppv) = 0; | ||
| 341 | virtual HRESULT STDMETHODCALLTYPE OpenFile(const WCHAR* path, REFIID riid, void** ppv) = 0; | ||
| 342 | virtual HRESULT STDMETHODCALLTYPE CreateStatusArray(UINT32 capacity, PCSTR name, REFIID riid, void** ppv) = 0; | ||
| 343 | virtual HRESULT STDMETHODCALLTYPE SetCpuAffinity(UINT64 affinity) = 0; | ||
| 344 | virtual void STDMETHODCALLTYPE SetDebugFlags(UINT32 flags) = 0; | ||
| 345 | virtual HRESULT STDMETHODCALLTYPE SetStagingBufferSize(UINT32 size) = 0; | ||
| 346 | }; | ||
| 347 | |||
| 348 | struct IDStorageFactoryX1 : IDStorageFactoryX { | ||
| 349 | virtual void STDMETHODCALLTYPE GetStats(DSTORAGE_STATS* stats) = 0; | ||
| 350 | }; | ||
| 351 | |||
| 352 | struct IDStorageFactoryX2 : IDStorageFactoryX1 { | ||
| 353 | virtual HRESULT STDMETHODCALLTYPE CreateQueue1(const DSTORAGE_QUEUE_DESC1* desc, REFIID riid, void** ppv) = 0; | ||
| 354 | }; | ||
| 355 | |||
| 356 | using DStorageGetFactoryFn = HRESULT(WINAPI*)(REFIID riid, void** ppv); | ||
| 357 | |||
| 358 | // ---- Interface ids ------------------------------------------------------------------------------------------------- | ||
| 359 | |||
| 360 | // Accepted for a status array: IDStorageStatusArray (PC dstorage.h 1.3.0), same methods in the same order. | ||
| 361 | constexpr GUID kStatusArrayIds[] = { | ||
| 362 | {0x82397587, 0x7cd5, 0x453b, {0xa0, 0x2e, 0x31, 0x37, 0x9b, 0xd6, 0x46, 0x56}}, | ||
| 363 | }; | ||
| 364 | |||
| 365 | // The other PC DirectStorage ids (dstorage.h 1.3.0), refused: their objects have another shape. | ||
| 366 | constexpr GUID kPcInterfaceIds[] = { | ||
| 367 | {0x6924ea0c, 0xc3cd, 0x4826, {0xb1, 0x0a, 0xf6, 0x4f, 0x4e, 0xd9, 0x27, 0xc1}}, // IDStorageFactory | ||
| 368 | {0x5de95e7b, 0x955a, 0x4868, {0xa7, 0x3c, 0x24, 0x3b, 0x29, 0xf4, 0xb8, 0xda}}, // IDStorageFile | ||
| 369 | {0xcfdbd83f, 0x9e06, 0x4fda, {0x8e, 0xa5, 0x69, 0x04, 0x21, 0x37, 0xf4, 0x9b}}, // IDStorageQueue | ||
| 370 | {0xdd2f482c, 0x5eff, 0x41e8, {0x9c, 0x9e, 0xd2, 0x37, 0x4b, 0x27, 0x81, 0x28}}, // IDStorageQueue1 | ||
| 371 | {0xb1c9d643, 0x3a49, 0x44a2, {0xb4, 0x6f, 0x65, 0x36, 0x49, 0x47, 0x0d, 0x18}}, // IDStorageQueue2 | ||
| 372 | {0xdeb54c52, 0xeca8, 0x46b3, {0x82, 0xa7, 0x03, 0x1b, 0x72, 0x26, 0x26, 0x53}}, // IDStorageQueue3 | ||
| 373 | {0x97179b2f, 0x2c21, 0x49ca, {0x82, 0x91, 0x4e, 0x1b, 0xf4, 0xa1, 0x60, 0xdf}}, // ...CustomDecompressionQueue | ||
| 374 | {0x0d47c6c9, 0xe61a, 0x4706, {0x93, 0xb4, 0x68, 0xbf, 0xe3, 0xf4, 0xaa, 0x4a}}, // ...CustomDecompressionQueue1 | ||
| 375 | {0x84ef5121, 0x9b43, 0x4d03, {0xb5, 0xc1, 0xcc, 0x34, 0x60, 0x6b, 0x26, 0x2d}}, // IDStorageCompressionCodec | ||
| 376 | }; | ||
| 377 | |||
| 378 | } // namespace dsx | ||
diff --git a/runtime/shims/registry.cpp b/runtime/shims/registry.cpp index 37ff540c5..047645401 100644 --- a/runtime/shims/registry.cpp +++ b/runtime/shims/registry.cpp | |||
| @@ -80,7 +80,10 @@ std::vector<Lookup> g_lookups; | |||
| 80 | // xaudio2_9.dll XAudio2Create: HRESULT (learn.microsoft.com/windows/win32/api/xaudio2/ | 80 | // xaudio2_9.dll XAudio2Create: HRESULT (learn.microsoft.com/windows/win32/api/xaudio2/ |
| 81 | // xaudio2_9redist.dll nf-xaudio2-xaudio2create). xaudio2_9.dll is the console name (docs/d3d12_vulkan.md §7). | 81 | // xaudio2_9redist.dll nf-xaudio2-xaudio2create). xaudio2_9.dll is the console name (docs/d3d12_vulkan.md §7). |
| 82 | // dstorage.dll DStorageGetFactory: HRESULT (learn.microsoft.com/windows/win32/api/dstorage/ | 82 | // dstorage.dll DStorageGetFactory: HRESULT (learn.microsoft.com/windows/win32/api/dstorage/ |
| 83 | // dstorage_xs.dll nf-dstorage-dstoragegetfactory). dstorage_xs.dll is the console name. | 83 | // dstorage_xs.dll nf-dstorage-dstoragegetfactory, and the GDK's gaming/gdk/docs/reference/system/dstorage/ |
| 84 | // xcrdapi.dll functions/dstoragegetfactory). dstorage_xs.dll is the console name; xcrdapi.dll is a | ||
| 85 | // console module the title may import it from (docs/directstorage.md). dstorage.cpp serves | ||
| 86 | // it; the class is kept for a build without that shim. | ||
| 84 | const char* const kNotHResult[] = { | 87 | const char* const kNotHResult[] = { |
| 85 | "XAsyncCancel", "XAsyncComplete", | 88 | "XAsyncCancel", "XAsyncComplete", |
| 86 | "XTaskQueueCloseHandle", "XTaskQueueDispatch", "XTaskQueueGetCurrentProcessTaskQueue", | 89 | "XTaskQueueCloseHandle", "XTaskQueueDispatch", "XTaskQueueGetCurrentProcessTaskQueue", |
| @@ -111,7 +114,7 @@ ReturnClass classify(const std::string& dll, const std::string& name) { | |||
| 111 | if (name == "XAudio2Create") return ReturnClass::HResult; | 114 | if (name == "XAudio2Create") return ReturnClass::HResult; |
| 112 | } else if (dll == "gameinput.dll") { | 115 | } else if (dll == "gameinput.dll") { |
| 113 | if (name == "GameInputCreate") return ReturnClass::HResult; | 116 | if (name == "GameInputCreate") return ReturnClass::HResult; |
| 114 | } else if (dll == "dstorage.dll" || dll == "dstorage_xs.dll") { | 117 | } else if (dll == "dstorage.dll" || dll == "dstorage_xs.dll" || dll == "xcrdapi.dll") { |
| 115 | if (name == "DStorageGetFactory") return ReturnClass::HResult; | 118 | if (name == "DStorageGetFactory") return ReturnClass::HResult; |
| 116 | } else if (dll == "xgameruntime.dll") { | 119 | } else if (dll == "xgameruntime.dll") { |
| 117 | if (name == "XGameRuntimeInitialize" || name == "XGameEventWrite") return ReturnClass::HResult; | 120 | if (name == "XGameRuntimeInitialize" || name == "XGameEventWrite") return ReturnClass::HResult; |
diff --git a/runtime/test/dstorage_test.cpp b/runtime/test/dstorage_test.cpp new file mode 100644 index 000000000..e97ad7314 --- /dev/null +++ b/runtime/test/dstorage_test.cpp | |||
| @@ -0,0 +1,915 @@ | |||
| 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 <atomic> | ||
| 7 | #include <chrono> | ||
| 8 | #include <cstdio> | ||
| 9 | #include <cstdlib> | ||
| 10 | #include <cstring> | ||
| 11 | #include <filesystem> | ||
| 12 | #include <functional> | ||
| 13 | #include <string> | ||
| 14 | #include <thread> | ||
| 15 | #include <vector> | ||
| 16 | |||
| 17 | #include "dstorage_xs_abi.h" | ||
| 18 | #include "file_system.h" | ||
| 19 | #include "shims.h" | ||
| 20 | |||
| 21 | using namespace dsx; | ||
| 22 | namespace fs = std::filesystem; | ||
| 23 | |||
| 24 | namespace { | ||
| 25 | |||
| 26 | int g_checks = 0; | ||
| 27 | int g_failures = 0; | ||
| 28 | |||
| 29 | void check(bool ok, const char* what) { | ||
| 30 | ++g_checks; | ||
| 31 | if (!ok) { | ||
| 32 | ++g_failures; | ||
| 33 | std::printf("FAIL: %s\n", what); | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | void check_hr(HRESULT got, HRESULT want, const char* what) { | ||
| 38 | ++g_checks; | ||
| 39 | if (got != want) { | ||
| 40 | ++g_failures; | ||
| 41 | std::printf("FAIL: %s (got 0x%08lx, want 0x%08lx)\n", what, static_cast<unsigned long>(got), | ||
| 42 | static_cast<unsigned long>(want)); | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | // Interface ids a title might pass. The console's own ids are not public, so these are made up; the shim accepts any | ||
| 47 | // id per kind of object. | ||
| 48 | constexpr GUID kFactoryId = {0x0d5e0001, 0x1111, 0x4111, {0x81, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x01}}; | ||
| 49 | constexpr GUID kQueueId = {0x0d5e0002, 0x1111, 0x4111, {0x81, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x02}}; | ||
| 50 | constexpr GUID kFileId = {0x0d5e0003, 0x1111, 0x4111, {0x81, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x03}}; | ||
| 51 | constexpr GUID kStatusId = {0x0d5e0004, 0x1111, 0x4111, {0x81, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x04}}; | ||
| 52 | // IDStorageFactory of the PC API (dstorage.h): refused, its object has another shape. | ||
| 53 | constexpr GUID kPcFactoryId = {0x6924ea0c, 0xc3cd, 0x4826, {0xb1, 0x0a, 0xf6, 0x4f, 0x4e, 0xd9, 0x27, 0xc1}}; | ||
| 54 | |||
| 55 | // A CPU fence with the PC ID3D12Fence vtable (the one the d3d12_xs fence proxies forward). It records the values it is | ||
| 56 | // signalled with and runs a check at each signal. | ||
| 57 | class TestFence final : public ID3D12Fence { | ||
| 58 | public: | ||
| 59 | std::function<void(UINT64)> on_signal; | ||
| 60 | std::atomic<UINT64> value{0}; | ||
| 61 | std::atomic<ULONG> refs{1}; | ||
| 62 | |||
| 63 | HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppv) override { | ||
| 64 | if (!ppv) return E_POINTER; | ||
| 65 | *ppv = nullptr; | ||
| 66 | if (riid != __uuidof(IUnknown) && riid != __uuidof(ID3D12Fence)) return E_NOINTERFACE; | ||
| 67 | AddRef(); | ||
| 68 | *ppv = this; | ||
| 69 | return S_OK; | ||
| 70 | } | ||
| 71 | ULONG STDMETHODCALLTYPE AddRef() override { return ++refs; } | ||
| 72 | ULONG STDMETHODCALLTYPE Release() override { return --refs; } // lives on the test's stack | ||
| 73 | HRESULT STDMETHODCALLTYPE GetPrivateData(REFGUID, UINT*, void*) override { return E_NOTIMPL; } | ||
| 74 | HRESULT STDMETHODCALLTYPE SetPrivateData(REFGUID, UINT, const void*) override { return E_NOTIMPL; } | ||
| 75 | HRESULT STDMETHODCALLTYPE SetPrivateDataInterface(REFGUID, const IUnknown*) override { return E_NOTIMPL; } | ||
| 76 | HRESULT STDMETHODCALLTYPE SetName(LPCWSTR) override { return S_OK; } | ||
| 77 | HRESULT STDMETHODCALLTYPE GetDevice(REFIID, void** ppv) override { | ||
| 78 | if (ppv) *ppv = nullptr; | ||
| 79 | return E_NOTIMPL; | ||
| 80 | } | ||
| 81 | UINT64 STDMETHODCALLTYPE GetCompletedValue() override { return value.load(); } | ||
| 82 | HRESULT STDMETHODCALLTYPE SetEventOnCompletion(UINT64, HANDLE) override { return E_NOTIMPL; } | ||
| 83 | HRESULT STDMETHODCALLTYPE Signal(UINT64 v) override { | ||
| 84 | if (on_signal) on_signal(v); | ||
| 85 | value.store(v); | ||
| 86 | return S_OK; | ||
| 87 | } | ||
| 88 | }; | ||
| 89 | |||
| 90 | std::vector<uint8_t> pattern(size_t n, uint32_t seed) { | ||
| 91 | std::vector<uint8_t> v(n); | ||
| 92 | uint32_t x = seed * 2654435761u + 1; | ||
| 93 | for (auto& b : v) { | ||
| 94 | x ^= x << 13; | ||
| 95 | x ^= x >> 17; | ||
| 96 | x ^= x << 5; | ||
| 97 | b = static_cast<uint8_t>(x); | ||
| 98 | } | ||
| 99 | return v; | ||
| 100 | } | ||
| 101 | |||
| 102 | void write_file(const fs::path& p, const std::vector<uint8_t>& bytes) { | ||
| 103 | fs::create_directories(p.parent_path()); | ||
| 104 | FILE* f = nullptr; | ||
| 105 | if (_wfopen_s(&f, p.c_str(), L"wb") != 0 || !f) { | ||
| 106 | std::printf("FAIL: cannot write %ls\n", p.c_str()); | ||
| 107 | std::exit(1); | ||
| 108 | } | ||
| 109 | if (!bytes.empty()) std::fwrite(bytes.data(), 1, bytes.size(), f); | ||
| 110 | std::fclose(f); | ||
| 111 | } | ||
| 112 | |||
| 113 | bool wait_complete(IDStorageStatusArrayX* s, UINT32 index, int ms = 10000) { | ||
| 114 | const auto end = std::chrono::steady_clock::now() + std::chrono::milliseconds(ms); | ||
| 115 | while (!s->IsComplete(index)) { | ||
| 116 | if (std::chrono::steady_clock::now() > end) return false; | ||
| 117 | Sleep(1); | ||
| 118 | } | ||
| 119 | return true; | ||
| 120 | } | ||
| 121 | |||
| 122 | bool wait_until(const std::function<bool()>& done, int ms = 10000) { | ||
| 123 | const auto end = std::chrono::steady_clock::now() + std::chrono::milliseconds(ms); | ||
| 124 | while (!done()) { | ||
| 125 | if (std::chrono::steady_clock::now() > end) return false; | ||
| 126 | Sleep(1); | ||
| 127 | } | ||
| 128 | return true; | ||
| 129 | } | ||
| 130 | |||
| 131 | DSTORAGE_REQUEST file_request(IDStorageFileX* f, uint64_t offset, void* dst, uint32_t n, uint64_t tag = 0, | ||
| 132 | const char* name = nullptr) { | ||
| 133 | DSTORAGE_REQUEST r{}; | ||
| 134 | r.File = f; | ||
| 135 | r.FileOffset = offset; | ||
| 136 | r.Destination = dst; | ||
| 137 | r.DestinationSize = n; | ||
| 138 | r.SourceSize = n; | ||
| 139 | r.CancellationTag = tag; | ||
| 140 | r.Name = name; | ||
| 141 | return r; | ||
| 142 | } | ||
| 143 | |||
| 144 | struct Env { | ||
| 145 | fs::path root, game, overlay; | ||
| 146 | IDStorageFactoryX2* factory = nullptr; | ||
| 147 | std::vector<uint8_t> a_package, a_overlay, b, big; | ||
| 148 | }; | ||
| 149 | |||
| 150 | IDStorageQueueX2* make_queue(Env& env, uint16_t capacity = 0x80, bool auto_submit = true, | ||
| 151 | DSTORAGE_REQUEST_SOURCE_TYPE type = DSTORAGE_REQUEST_SOURCE_FILE, | ||
| 152 | DSTORAGE_PRIORITY priority = DSTORAGE_PRIORITY_NORMAL, const char* name = "test queue") { | ||
| 153 | DSTORAGE_QUEUE_DESC1 d{}; | ||
| 154 | d.SourceType = type; | ||
| 155 | d.Capacity = capacity; | ||
| 156 | d.Priority = priority; | ||
| 157 | d.Name = name; | ||
| 158 | d.Options.AutoSubmitDisabled = auto_submit ? 0 : 1; | ||
| 159 | void* q = nullptr; | ||
| 160 | const HRESULT hr = env.factory->CreateQueue1(&d, kQueueId, &q); | ||
| 161 | check_hr(hr, S_OK, "CreateQueue1"); | ||
| 162 | return static_cast<IDStorageQueueX2*>(q); | ||
| 163 | } | ||
| 164 | |||
| 165 | IDStorageFileX* open(Env& env, const wchar_t* path) { | ||
| 166 | void* f = nullptr; | ||
| 167 | check_hr(env.factory->OpenFile(path, kFileId, &f), S_OK, "OpenFile"); | ||
| 168 | return static_cast<IDStorageFileX*>(f); | ||
| 169 | } | ||
| 170 | |||
| 171 | IDStorageStatusArrayX* make_status(Env& env, UINT32 capacity) { | ||
| 172 | void* s = nullptr; | ||
| 173 | check_hr(env.factory->CreateStatusArray(capacity, "test status", kStatusId, &s), S_OK, "CreateStatusArray"); | ||
| 174 | return static_cast<IDStorageStatusArrayX*>(s); | ||
| 175 | } | ||
| 176 | |||
| 177 | // ---- tests ---------------------------------------------------------------------------------------------------------- | ||
| 178 | |||
| 179 | void test_layout_and_options() { | ||
| 180 | DSTORAGE_REQUEST_OPTIONS o{}; | ||
| 181 | o.ZlibDecompress = 1; | ||
| 182 | check(o.AsUINT64 == options::kZlib, "ZlibDecompress is bit 0"); | ||
| 183 | o.AsUINT64 = 0; | ||
| 184 | o.BcpackMode = DSTORAGE_BCPACK_MODE_BC7; | ||
| 185 | check(options::bcpack(o.AsUINT64) == 7 && o.AsUINT64 == (7ull << options::kBcpackShift), "BcpackMode is bits 1-4"); | ||
| 186 | o.AsUINT64 = 0; | ||
| 187 | o.SwizzleMode = DSTORAGE_SWIZZLE_MODE_4K_12K_4K_12K; | ||
| 188 | check(options::swizzle(o.AsUINT64) == 4 && o.AsUINT64 == (4ull << options::kSwizzleShift), | ||
| 189 | "SwizzleMode is bits 5-20"); | ||
| 190 | o.AsUINT64 = 0; | ||
| 191 | o.DestinationIsPhysicalPages = 1; | ||
| 192 | check(o.AsUINT64 == options::kDestinationPages, "DestinationIsPhysicalPages is bit 21"); | ||
| 193 | o.AsUINT64 = 0; | ||
| 194 | o.SourceType = DSTORAGE_REQUEST_SOURCE_MEMORY; | ||
| 195 | check(o.AsUINT64 == options::kSourceMemory, "SourceType is bit 22"); | ||
| 196 | o.AsUINT64 = 0; | ||
| 197 | o.SourceIsPhysicalPages = 1; | ||
| 198 | check(o.AsUINT64 == options::kSourcePages, "SourceIsPhysicalPages is bit 23"); | ||
| 199 | o.AsUINT64 = 0; | ||
| 200 | o.Reserved = 1; | ||
| 201 | check(o.AsUINT64 == (1ull << 24) && (o.AsUINT64 & options::kReservedMask), "Reserved starts at bit 24"); | ||
| 202 | DSTORAGE_QUEUE_OPTIONS q{}; | ||
| 203 | q.AutoSubmitDisabled = 1; | ||
| 204 | check(q.AsUINT64 == 1, "AutoSubmitDisabled is bit 0"); | ||
| 205 | } | ||
| 206 | |||
| 207 | void test_registry(Env& env) { | ||
| 208 | const char* names[] = {"dstorage_xs.dll", "xcrdapi.dll", "dstorage.dll"}; | ||
| 209 | void* first = rt::shims::find("dstorage_xs.dll", "DStorageGetFactory"); | ||
| 210 | check(first != nullptr, "dstorage_xs.dll!DStorageGetFactory is registered"); | ||
| 211 | for (const char* n : names) { | ||
| 212 | check(rt::shims::find(n, "DStorageGetFactory") == first, "every module name serves the same function"); | ||
| 213 | check(rt::shims::owns_dll(n), "the shims own the module (LoadLibrary gets a pseudo-handle)"); | ||
| 214 | } | ||
| 215 | auto get = reinterpret_cast<DStorageGetFactoryFn>(first); | ||
| 216 | if (!get) std::exit(1); | ||
| 217 | |||
| 218 | check_hr(get(kFactoryId, nullptr), E_POINTER, "DStorageGetFactory without ppv"); | ||
| 219 | void* pc = reinterpret_cast<void*>(1); | ||
| 220 | check_hr(get(kPcFactoryId, &pc), E_NOINTERFACE, "the PC IDStorageFactory id is refused"); | ||
| 221 | check(pc == nullptr, "a refused factory clears ppv"); | ||
| 222 | |||
| 223 | void* f1 = nullptr; | ||
| 224 | void* f2 = nullptr; | ||
| 225 | check_hr(get(kFactoryId, &f1), S_OK, "DStorageGetFactory"); | ||
| 226 | check_hr(reinterpret_cast<DStorageGetFactoryFn>(rt::shims::find("xcrdapi.dll", "DStorageGetFactory"))( | ||
| 227 | __uuidof(IUnknown), &f2), | ||
| 228 | S_OK, "DStorageGetFactory(IUnknown) under xcrdapi.dll"); | ||
| 229 | check(f1 && f1 == f2, "the factory is one object"); | ||
| 230 | env.factory = static_cast<IDStorageFactoryX2*>(f1); | ||
| 231 | static_cast<IUnknown*>(f2)->Release(); | ||
| 232 | check(env.factory->Release() >= 1 && env.factory->Release() >= 1, "the factory's count does not drop below one"); | ||
| 233 | env.factory->AddRef(); | ||
| 234 | |||
| 235 | void* again = nullptr; | ||
| 236 | check_hr(env.factory->QueryInterface(kFactoryId, &again), S_OK, "factory QueryInterface for its own id"); | ||
| 237 | if (again) static_cast<IUnknown*>(again)->Release(); | ||
| 238 | } | ||
| 239 | |||
| 240 | void test_factory_checks(Env& env) { | ||
| 241 | // Before any queue or file exists. | ||
| 242 | check_hr(env.factory->SetStagingBufferSize(17u << 20), E_DSTORAGE_INVALID_STAGING_BUFFER_SIZE, "staging 17 MiB"); | ||
| 243 | check_hr(env.factory->SetStagingBufferSize(16u << 20), S_OK, "staging 16 MiB"); | ||
| 244 | check_hr(env.factory->SetStagingBufferSize(0), S_OK, "staging 0"); | ||
| 245 | check_hr(env.factory->SetCpuAffinity(0x3c), S_OK, "SetCpuAffinity"); | ||
| 246 | env.factory->SetDebugFlags(DSTORAGE_DEBUG_SHOW_ERRORS); | ||
| 247 | |||
| 248 | auto create = [&](DSTORAGE_REQUEST_SOURCE_TYPE type, uint16_t cap, int prio, uint64_t opts = 0) { | ||
| 249 | DSTORAGE_QUEUE_DESC1 d{}; | ||
| 250 | d.SourceType = type; | ||
| 251 | d.Capacity = cap; | ||
| 252 | d.Priority = static_cast<DSTORAGE_PRIORITY>(prio); | ||
| 253 | d.Options.AsUINT64 = opts; | ||
| 254 | void* q = reinterpret_cast<void*>(1); | ||
| 255 | const HRESULT hr = env.factory->CreateQueue1(&d, kQueueId, &q); | ||
| 256 | if (SUCCEEDED(hr)) static_cast<IUnknown*>(q)->Release(); | ||
| 257 | else check(q == nullptr, "a failed CreateQueue1 clears ppv"); | ||
| 258 | return hr; | ||
| 259 | }; | ||
| 260 | check_hr(create(DSTORAGE_REQUEST_SOURCE_FILE, 0x7f, 0), E_DSTORAGE_INVALID_QUEUE_CAPACITY, "capacity 0x7f"); | ||
| 261 | check_hr(create(DSTORAGE_REQUEST_SOURCE_FILE, 0x2001, 0), E_DSTORAGE_INVALID_QUEUE_CAPACITY, "capacity 0x2001"); | ||
| 262 | check_hr(create(DSTORAGE_REQUEST_SOURCE_FILE, 0x80, 3), E_DSTORAGE_INVALID_QUEUE_PRIORITY, "priority 3"); | ||
| 263 | check_hr(create(DSTORAGE_REQUEST_SOURCE_FILE, 0x80, -2), E_DSTORAGE_INVALID_QUEUE_PRIORITY, "priority -2"); | ||
| 264 | check_hr(create(DSTORAGE_REQUEST_SOURCE_MEMORY, 0x80, 0), E_DSTORAGE_INVALID_MEMORY_QUEUE_PRIORITY, | ||
| 265 | "a memory queue must be realtime"); | ||
| 266 | check_hr(create(static_cast<DSTORAGE_REQUEST_SOURCE_TYPE>(2), 0x80, 0), E_DSTORAGE_INVALID_SOURCE_TYPE, | ||
| 267 | "source type 2"); | ||
| 268 | check_hr(create(DSTORAGE_REQUEST_SOURCE_FILE, 0x80, 0, 2), E_DSTORAGE_RESERVED_FIELDS, "queue option bit 1"); | ||
| 269 | check_hr(create(DSTORAGE_REQUEST_SOURCE_FILE, 0x2000, -1), S_OK, "capacity 0x2000, low priority"); | ||
| 270 | check_hr(create(DSTORAGE_REQUEST_SOURCE_MEMORY, 0x80, 2), S_OK, "a realtime memory queue"); | ||
| 271 | check_hr(env.factory->CreateQueue1(nullptr, kQueueId, nullptr), E_POINTER, "CreateQueue1 without ppv"); | ||
| 272 | void* q = nullptr; | ||
| 273 | check_hr(env.factory->CreateQueue(nullptr, kQueueId, &q), E_INVALIDARG, "CreateQueue without a desc"); | ||
| 274 | check_hr(env.factory->CreateQueue1(nullptr, kQueueId, &q), E_INVALIDARG, "CreateQueue1 without a desc"); | ||
| 275 | void* s = nullptr; | ||
| 276 | check_hr(env.factory->CreateStatusArray(0, nullptr, kStatusId, &s), E_INVALIDARG, "a status array of 0"); | ||
| 277 | |||
| 278 | // The id policy: an id seen for a queue is not a factory id. | ||
| 279 | void* wrong = nullptr; | ||
| 280 | check_hr(env.factory->QueryInterface(kQueueId, &wrong), E_NOINTERFACE, "a queue id is not a factory id"); | ||
| 281 | check_hr(env.factory->CreateStatusArray(4, nullptr, kFactoryId, &s), E_NOINTERFACE, | ||
| 282 | "a factory id is not a status array id"); | ||
| 283 | |||
| 284 | // CreateQueue (DSTORAGE_QUEUE_DESC) and Query reflect the desc. | ||
| 285 | DSTORAGE_QUEUE_DESC d{}; | ||
| 286 | d.SourceType = DSTORAGE_REQUEST_SOURCE_FILE; | ||
| 287 | d.Capacity = 0x100; | ||
| 288 | d.Priority = DSTORAGE_PRIORITY_HIGH; | ||
| 289 | d.Name = "named"; | ||
| 290 | check_hr(env.factory->CreateQueue(&d, kQueueId, &q), S_OK, "CreateQueue"); | ||
| 291 | auto* queue = static_cast<IDStorageQueueX2*>(q); | ||
| 292 | DSTORAGE_QUEUE_INFO info{}; | ||
| 293 | queue->Query(&info); | ||
| 294 | check(info.Desc.Capacity == 0x100 && info.Desc.Priority == DSTORAGE_PRIORITY_HIGH && info.Desc.Name == d.Name && | ||
| 295 | info.Desc.SourceType == DSTORAGE_REQUEST_SOURCE_FILE, | ||
| 296 | "Query returns the creation desc"); | ||
| 297 | check(info.EmptySlotCount == 0xff, "an empty queue has capacity - 1 empty slots"); | ||
| 298 | check(info.RequestCountUntilAutoSubmit == 0x80, "auto-submit at half the capacity"); | ||
| 299 | DSTORAGE_QUEUE_INFO1 info1{}; | ||
| 300 | queue->Query1(&info1); | ||
| 301 | check(info1.Desc.Options.AsUINT64 == 0 && info1.EmptySlotCount == 0xff, "Query1"); | ||
| 302 | check_hr(env.factory->SetStagingBufferSize(32u << 20), E_DSTORAGE_STAGING_BUFFER_LOCKED, | ||
| 303 | "staging size is locked while a queue exists"); | ||
| 304 | void* qi = nullptr; | ||
| 305 | check_hr(queue->QueryInterface(kQueueId, &qi), S_OK, "queue QueryInterface"); | ||
| 306 | if (qi) static_cast<IUnknown*>(qi)->Release(); | ||
| 307 | check(queue->Release() == 0, "the last queue reference frees it"); | ||
| 308 | check_hr(env.factory->SetStagingBufferSize(32u << 20), S_OK, "staging size unlocked again"); | ||
| 309 | } | ||
| 310 | |||
| 311 | void test_open_paths(Env& env) { | ||
| 312 | void* f = reinterpret_cast<void*>(1); | ||
| 313 | check_hr(env.factory->OpenFile(L"GAME:\\data\\missing.bin", kFileId, &f), HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), | ||
| 314 | "a missing GAME: file"); | ||
| 315 | check(f == nullptr, "a failed OpenFile clears ppv"); | ||
| 316 | check_hr(env.factory->OpenFile(L"GAME:\\..\\outside.bin", kFileId, &f), HRESULT_FROM_WIN32(ERROR_INVALID_NAME), | ||
| 317 | "a GAME: path that climbs out is refused"); | ||
| 318 | check_hr(env.factory->OpenFile(nullptr, kFileId, &f), E_INVALIDARG, "OpenFile without a path"); | ||
| 319 | check_hr(env.factory->OpenFile(L"GAME:\\data\\a.bin", kFileId, nullptr), E_POINTER, "OpenFile without ppv"); | ||
| 320 | |||
| 321 | // The overlay copy wins over the package; a host path is passed through. | ||
| 322 | IDStorageFileX* a = open(env, L"GAME:\\data\\a.bin"); | ||
| 323 | IDStorageFileX* b = open(env, L"game:/data/b.bin"); | ||
| 324 | const std::wstring host = (env.game / L"data" / L"b.bin").wstring(); | ||
| 325 | IDStorageFileX* h = open(env, host.c_str()); | ||
| 326 | if (!a || !b || !h) std::exit(1); | ||
| 327 | |||
| 328 | IDStorageQueueX2* q = make_queue(env); | ||
| 329 | IDStorageStatusArrayX* s = make_status(env, 4); | ||
| 330 | std::vector<uint8_t> da(4096), db(1000), dh(1000); | ||
| 331 | DSTORAGE_REQUEST r = file_request(a, 0, da.data(), 4096); | ||
| 332 | q->EnqueueRequest(&r); | ||
| 333 | r = file_request(b, 500, db.data(), 1000); | ||
| 334 | q->EnqueueRequest(&r); | ||
| 335 | r = file_request(h, 500, dh.data(), 1000); | ||
| 336 | q->EnqueueRequest(&r); | ||
| 337 | q->EnqueueStatus(s, 0); | ||
| 338 | check_hr(s->GetHResult(0), E_PENDING, "a status entry is pending once enqueued"); | ||
| 339 | q->Submit(); | ||
| 340 | check(wait_complete(s, 0), "the reads complete"); | ||
| 341 | check_hr(s->GetHResult(0), S_OK, "the reads succeed"); | ||
| 342 | check(std::memcmp(da.data(), env.a_overlay.data(), 4096) == 0 && da != env.a_package, | ||
| 343 | "GAME: reads the overlay copy, not the package"); | ||
| 344 | |||
| 345 | // GetHandle: a handle of its own (ReOpenFile), so its file position does not follow the shim's reads, which | ||
| 346 | // just ended at byte 4096 of this file. | ||
| 347 | HANDLE own = a->GetHandle(); | ||
| 348 | LARGE_INTEGER size{}; | ||
| 349 | check(own != INVALID_HANDLE_VALUE && GetFileSizeEx(own, &size) && size.QuadPart == 5000, | ||
| 350 | "GetHandle names the overlay copy (5000 bytes, the package has 4096)"); | ||
| 351 | std::vector<uint8_t> via_handle(6000); | ||
| 352 | DWORD got = 0; | ||
| 353 | check(own != INVALID_HANDLE_VALUE && ReadFile(own, via_handle.data(), 6000, &got, nullptr) && got == 5000 && | ||
| 354 | std::memcmp(via_handle.data(), env.a_overlay.data(), 5000) == 0, | ||
| 355 | "a ReadFile through GetHandle's handle starts at byte 0 and reads the overlay copy"); | ||
| 356 | if (own != INVALID_HANDLE_VALUE) CloseHandle(own); | ||
| 357 | r = file_request(a, 100, da.data(), 50); | ||
| 358 | q->EnqueueRequest(&r); | ||
| 359 | q->EnqueueStatus(s, 1); | ||
| 360 | q->Submit(); | ||
| 361 | check(wait_complete(s, 1) && std::memcmp(da.data(), env.a_overlay.data() + 100, 50) == 0, | ||
| 362 | "the shim's reads do not follow a GetHandle handle's position either"); | ||
| 363 | check(std::memcmp(db.data(), env.b.data() + 500, 1000) == 0, | ||
| 364 | "a GAME: read at an offset (forward slashes, any case)"); | ||
| 365 | check(db == dh, "a host path reads the same file"); | ||
| 366 | a->Release(); | ||
| 367 | b->Release(); | ||
| 368 | h->Release(); | ||
| 369 | s->Release(); | ||
| 370 | q->Release(); | ||
| 371 | } | ||
| 372 | |||
| 373 | void test_reads_and_errors(Env& env) { | ||
| 374 | IDStorageFileX* b = open(env, L"GAME:\\data\\b.bin"); // 10,000 bytes | ||
| 375 | IDStorageFileX* big = open(env, L"GAME:\\data\\big.bin"); | ||
| 376 | IDStorageQueueX2* q = make_queue(env, 0x80, false); | ||
| 377 | IDStorageStatusArrayX* s = make_status(env, 32); | ||
| 378 | HANDLE error_event = q->GetErrorEvent(); | ||
| 379 | check(error_event != nullptr, "GetErrorEvent returns a handle"); | ||
| 380 | |||
| 381 | std::vector<uint8_t> big_out(env.big.size()); | ||
| 382 | std::vector<uint8_t> few(64, 0xAB); | ||
| 383 | std::vector<uint8_t> zero(16, 0xCD); | ||
| 384 | struct Case { | ||
| 385 | DSTORAGE_REQUEST r; | ||
| 386 | HRESULT want; | ||
| 387 | const char* what; | ||
| 388 | }; | ||
| 389 | std::vector<Case> cases; | ||
| 390 | cases.push_back({file_request(big, 0, big_out.data(), static_cast<uint32_t>(big_out.size())), S_OK, | ||
| 391 | "a 3 MiB read"}); | ||
| 392 | cases.push_back({file_request(b, 10000, zero.data(), 0), S_OK, "a zero-size read at the end of the file"}); | ||
| 393 | cases.push_back({file_request(b, 9990, few.data(), 11, 0x77, "past-the-end"), E_DSTORAGE_END_OF_FILE, | ||
| 394 | "a read one byte past the end"}); | ||
| 395 | cases.push_back({file_request(b, ~0ull - 4, few.data(), 8), E_DSTORAGE_END_OF_FILE, "an offset near 2^64"}); | ||
| 396 | cases.push_back({file_request(b, 0, few.data(), 8), S_OK, "a good read after failures"}); | ||
| 397 | { | ||
| 398 | DSTORAGE_REQUEST r = file_request(b, 0, few.data(), 8); | ||
| 399 | r.DestinationSize = 16; | ||
| 400 | cases.push_back({r, E_DSTORAGE_INVALID_DESTINATION_SIZE, "DestinationSize differs from SourceSize"}); | ||
| 401 | r = file_request(b, 0, few.data(), 8); | ||
| 402 | r.Options.ZlibDecompress = 1; | ||
| 403 | cases.push_back({r, E_NOTIMPL, "zlib is not implemented yet"}); | ||
| 404 | r = file_request(b, 0, few.data(), 8); | ||
| 405 | r.Options.BcpackMode = DSTORAGE_BCPACK_MODE_BC1; | ||
| 406 | cases.push_back({r, E_NOTIMPL, "BCPack is not implemented yet"}); | ||
| 407 | r = file_request(b, 0, few.data(), 8); | ||
| 408 | r.Options.AsUINT64 = 8ull << options::kBcpackShift; | ||
| 409 | cases.push_back({r, E_DSTORAGE_INVALID_BCPACK_MODE, "BCPack mode 8"}); | ||
| 410 | r = file_request(b, 0, few.data(), 8); | ||
| 411 | r.Options.AsUINT64 = 5ull << options::kSwizzleShift; | ||
| 412 | cases.push_back({r, E_DSTORAGE_INVALID_SWIZZLE_MODE, "swizzle mode 5"}); | ||
| 413 | r = file_request(b, 0, few.data(), 8); | ||
| 414 | r.Options.Reserved = 1; | ||
| 415 | cases.push_back({r, E_DSTORAGE_RESERVED_FIELDS, "a reserved option bit"}); | ||
| 416 | r = file_request(b, 0, few.data(), 8); | ||
| 417 | r.Options.DestinationIsPhysicalPages = 1; | ||
| 418 | cases.push_back({r, E_NOTIMPL, "physical pages are not implemented yet"}); | ||
| 419 | r = file_request(b, 0, few.data(), 8); | ||
| 420 | r.IntermediateSize = 4; | ||
| 421 | cases.push_back({r, E_DSTORAGE_INVALID_INTERMEDIATE_SIZE, "IntermediateSize without zlib and BCPack"}); | ||
| 422 | r = file_request(b, 0, nullptr, 8); | ||
| 423 | cases.push_back({r, E_INVALIDARG, "a null destination"}); | ||
| 424 | r = file_request(nullptr, 0, few.data(), 8); | ||
| 425 | cases.push_back({r, E_INVALIDARG, "a null file"}); | ||
| 426 | r = file_request(reinterpret_cast<IDStorageFileX*>(s), 0, few.data(), 8); | ||
| 427 | cases.push_back({r, E_DSTORAGE_INVALID_FILE_HANDLE, "a status array passed as a file"}); | ||
| 428 | r = file_request(b, 0, few.data(), 8); | ||
| 429 | r.Options.SourceType = DSTORAGE_REQUEST_SOURCE_MEMORY; | ||
| 430 | cases.push_back({r, E_DSTORAGE_INVALID_SOURCE_TYPE, "a memory request on a file queue"}); | ||
| 431 | } | ||
| 432 | cases.push_back({file_request(b, 0, nullptr, 0), E_INVALIDARG, "a zero-size read still needs a destination"}); | ||
| 433 | cases.push_back({file_request(nullptr, 0, zero.data(), 0), E_INVALIDARG, "a zero-size read still needs a file"}); | ||
| 434 | void* reserved = VirtualAlloc(nullptr, 1 << 16, MEM_RESERVE, PAGE_READWRITE); | ||
| 435 | cases.push_back({file_request(b, 0, reserved, 4096), E_DSTORAGE_ACCESS_VIOLATION, | ||
| 436 | "a destination the process cannot write"}); | ||
| 437 | check(cases.size() <= 31, "the cases fit the status array"); | ||
| 438 | |||
| 439 | for (UINT32 i = 0; i < cases.size(); ++i) { | ||
| 440 | q->EnqueueRequest(&cases[i].r); | ||
| 441 | q->EnqueueStatus(s, i); | ||
| 442 | } | ||
| 443 | Sleep(30); | ||
| 444 | check(!s->IsComplete(0), "nothing runs before Submit when auto-submit is disabled"); | ||
| 445 | DSTORAGE_QUEUE_INFO1 info{}; | ||
| 446 | q->Query1(&info); | ||
| 447 | check(info.RequestCountUntilAutoSubmit == 0, "no auto-submit count when it is disabled"); | ||
| 448 | check(info.EmptySlotCount == 0x7f - 2 * cases.size(), "EmptySlotCount counts every entry"); | ||
| 449 | q->Submit(); | ||
| 450 | const UINT32 last = static_cast<UINT32>(cases.size() - 1); | ||
| 451 | check(wait_complete(s, last), "all cases complete"); | ||
| 452 | for (UINT32 i = 0; i < cases.size(); ++i) check_hr(s->GetHResult(i), cases[i].want, cases[i].what); | ||
| 453 | check(big_out == env.big, "the 3 MiB read has the file's bytes"); | ||
| 454 | check(zero == std::vector<uint8_t>(16, 0xCD), "a zero-size read writes nothing"); | ||
| 455 | check(std::memcmp(few.data(), env.b.data(), 8) == 0, "the good read after failures"); | ||
| 456 | |||
| 457 | check(WaitForSingleObject(error_event, 0) == WAIT_OBJECT_0, "the error event is set"); | ||
| 458 | DSTORAGE_ERROR_RECORD rec{}; | ||
| 459 | q->RetrieveErrorRecord(&rec); | ||
| 460 | size_t failures = 0; | ||
| 461 | for (const auto& c : cases) failures += FAILED(c.want) ? 1 : 0; | ||
| 462 | check(rec.FailureCount == failures, "FailureCount counts every failed request"); | ||
| 463 | check_hr(rec.FirstFailure.HResult, E_DSTORAGE_END_OF_FILE, "the first failure is recorded"); | ||
| 464 | check(rec.FirstFailure.CommandType == DSTORAGE_COMMAND_TYPE_REQUEST, "its command type is REQUEST"); | ||
| 465 | check(rec.FirstFailure.Request.FileOffset == 9990 && rec.FirstFailure.Request.SourceSize == 11 && | ||
| 466 | rec.FirstFailure.Request.CancellationTag == 0x77, | ||
| 467 | "the record has the request's offset, size and tag"); | ||
| 468 | check(std::wcscmp(rec.FirstFailure.Request.Filename, L"GAME:\\data\\b.bin") == 0, | ||
| 469 | "the record names the file as the title opened it"); | ||
| 470 | check(std::strcmp(rec.FirstFailure.Request.Name, "past-the-end") == 0, "the record has the request's name"); | ||
| 471 | check(WaitForSingleObject(error_event, 0) == WAIT_TIMEOUT, "RetrieveErrorRecord resets the event"); | ||
| 472 | q->RetrieveErrorRecord(&rec); | ||
| 473 | check(rec.FailureCount == 0 && rec.FirstFailure.CommandType == DSTORAGE_COMMAND_TYPE_NONE, | ||
| 474 | "a second RetrieveErrorRecord has nothing"); | ||
| 475 | |||
| 476 | // Status and fence entries that fail. | ||
| 477 | q->EnqueueStatus(s, 1000); | ||
| 478 | q->EnqueueStatus(nullptr, 0); | ||
| 479 | q->EnqueueSignal(nullptr, 1); | ||
| 480 | q->EnqueueStatus(s, 31); | ||
| 481 | q->Submit(); | ||
| 482 | check(wait_complete(s, 31), "the entries after the failed notifications complete"); | ||
| 483 | check_hr(s->GetHResult(31), S_OK, "notification failures do not fail the next status entry"); | ||
| 484 | check_hr(s->GetHResult(1000), E_DSTORAGE_INDEX_BOUND, "GetHResult past the array"); | ||
| 485 | check(s->IsComplete(1000), "IsComplete past the array is not pending"); | ||
| 486 | q->RetrieveErrorRecord(&rec); | ||
| 487 | check(rec.FailureCount == 3 && rec.FirstFailure.CommandType == DSTORAGE_COMMAND_TYPE_STATUS && | ||
| 488 | rec.FirstFailure.HResult == E_DSTORAGE_INDEX_BOUND && rec.FirstFailure.Status.Index == 1000 && | ||
| 489 | rec.FirstFailure.Status.StatusArray == s, | ||
| 490 | "an index past the array is recorded as a STATUS failure"); | ||
| 491 | |||
| 492 | VirtualFree(reserved, 0, MEM_RELEASE); | ||
| 493 | CloseHandle(error_event); | ||
| 494 | s->Release(); | ||
| 495 | q->Release(); | ||
| 496 | b->Release(); | ||
| 497 | big->Release(); | ||
| 498 | } | ||
| 499 | |||
| 500 | void test_fence_order_and_events(Env& env) { | ||
| 501 | IDStorageFileX* b = open(env, L"GAME:\\data\\b.bin"); | ||
| 502 | IDStorageQueueX2* q = make_queue(env, 0x200); | ||
| 503 | TestFence fence; | ||
| 504 | constexpr int kReads = 100; | ||
| 505 | std::vector<std::vector<uint8_t>> outs(kReads, std::vector<uint8_t>(100)); | ||
| 506 | std::atomic<bool> in_place{false}; | ||
| 507 | fence.on_signal = [&](UINT64 v) { | ||
| 508 | bool ok = v == 7; | ||
| 509 | for (int i = 0; i < kReads; ++i) ok = ok && std::memcmp(outs[i].data(), env.b.data() + i * 100, 100) == 0; | ||
| 510 | in_place.store(ok); | ||
| 511 | }; | ||
| 512 | for (int i = 0; i < kReads; ++i) { | ||
| 513 | DSTORAGE_REQUEST r = file_request(b, static_cast<uint64_t>(i) * 100, outs[i].data(), 100); | ||
| 514 | q->EnqueueRequest(&r); | ||
| 515 | } | ||
| 516 | q->EnqueueSignal(&fence, 7); | ||
| 517 | check(fence.refs.load() == 2, "an enqueued signal holds a fence reference"); | ||
| 518 | HANDLE ev = CreateEventW(nullptr, FALSE, FALSE, nullptr); | ||
| 519 | q->EnqueueSetEvent(ev); | ||
| 520 | q->Submit(); | ||
| 521 | check(WaitForSingleObject(ev, 10000) == WAIT_OBJECT_0, "EnqueueSetEvent sets the event"); | ||
| 522 | check(fence.GetCompletedValue() == 7, "EnqueueSignal signals the fence with its value"); | ||
| 523 | check(in_place.load(), "the fence is signalled after every read before it is in place"); | ||
| 524 | check(wait_until([&] { return fence.refs.load() == 1; }), "the fence reference is released after the signal"); | ||
| 525 | |||
| 526 | // A failed request before a fence: the fence still signals, and the status after the fence is clean. | ||
| 527 | IDStorageStatusArrayX* s = make_status(env, 2); | ||
| 528 | std::vector<uint8_t> tmp(8); | ||
| 529 | DSTORAGE_REQUEST bad = file_request(b, 20000, tmp.data(), 8); | ||
| 530 | q->EnqueueRequest(&bad); | ||
| 531 | q->EnqueueSignal(&fence, 8); | ||
| 532 | q->EnqueueStatus(s, 0); | ||
| 533 | q->Submit(); | ||
| 534 | check(wait_complete(s, 0), "the status after a fence completes"); | ||
| 535 | check_hr(s->GetHResult(0), S_OK, "a failure before a fence is not reported by a status after it"); | ||
| 536 | check(fence.GetCompletedValue() == 8, "a fence after a failed read still signals"); | ||
| 537 | CloseHandle(ev); | ||
| 538 | s->Release(); | ||
| 539 | q->Release(); | ||
| 540 | b->Release(); | ||
| 541 | } | ||
| 542 | |||
| 543 | void test_cancel(Env& env) { | ||
| 544 | IDStorageFileX* b = open(env, L"GAME:\\data\\b.bin"); | ||
| 545 | IDStorageQueueX2* q = make_queue(env, 0x80, false); | ||
| 546 | IDStorageStatusArrayX* s = make_status(env, 1); | ||
| 547 | std::vector<std::vector<uint8_t>> outs(6, std::vector<uint8_t>(16, 0)); | ||
| 548 | const uint64_t tags[] = {0x11, 0x12, 0x21, 0x22, 0x31}; | ||
| 549 | for (int i = 0; i < 5; ++i) { | ||
| 550 | DSTORAGE_REQUEST r = file_request(b, 0, outs[i].data(), 16, tags[i]); | ||
| 551 | q->EnqueueRequest(&r); | ||
| 552 | } | ||
| 553 | q->CancelRequestsWithTag(0xF0, 0x10); // 0x11 and 0x12 | ||
| 554 | q->CancelRequestsWithTag(0xFF, 0x22); // 0x22 | ||
| 555 | DSTORAGE_REQUEST later = file_request(b, 0, outs[5].data(), 16, 0x11); // enqueued after: not cancelled | ||
| 556 | q->EnqueueRequest(&later); | ||
| 557 | q->EnqueueStatus(s, 0); | ||
| 558 | q->Submit(); | ||
| 559 | check(wait_complete(s, 0), "cancelled requests complete"); | ||
| 560 | check_hr(s->GetHResult(0), S_OK, "a cancelled request is a success"); | ||
| 561 | const std::vector<uint8_t> zeros(16, 0); | ||
| 562 | check(outs[0] == zeros && outs[1] == zeros && outs[3] == zeros, "cancelled requests read nothing"); | ||
| 563 | check(std::memcmp(outs[2].data(), env.b.data(), 16) == 0 && std::memcmp(outs[4].data(), env.b.data(), 16) == 0, | ||
| 564 | "requests the mask did not match are read"); | ||
| 565 | check(std::memcmp(outs[5].data(), env.b.data(), 16) == 0, "a request enqueued after the cancel is read"); | ||
| 566 | s->Release(); | ||
| 567 | q->Release(); | ||
| 568 | b->Release(); | ||
| 569 | } | ||
| 570 | |||
| 571 | void test_auto_submit_and_full_queue(Env& env) { | ||
| 572 | IDStorageFileX* b = open(env, L"GAME:\\data\\b.bin"); | ||
| 573 | IDStorageQueueX2* q = make_queue(env, 0x80, true); | ||
| 574 | std::vector<std::vector<uint8_t>> outs(64, std::vector<uint8_t>(8, 0)); | ||
| 575 | for (int i = 0; i < 63; ++i) { | ||
| 576 | DSTORAGE_REQUEST r = file_request(b, i, outs[i].data(), 8); | ||
| 577 | q->EnqueueRequest(&r); | ||
| 578 | } | ||
| 579 | DSTORAGE_QUEUE_INFO info{}; | ||
| 580 | q->Query(&info); | ||
| 581 | check(info.RequestCountUntilAutoSubmit == 1, "one entry before the automatic submit"); | ||
| 582 | Sleep(20); | ||
| 583 | check(outs[0] == std::vector<uint8_t>(8, 0), "below half the capacity nothing is submitted"); | ||
| 584 | DSTORAGE_REQUEST r = file_request(b, 63, outs[63].data(), 8); | ||
| 585 | q->EnqueueRequest(&r); // the 64th: half of 0x80 | ||
| 586 | check(wait_until([&] { return std::memcmp(outs[63].data(), env.b.data() + 63, 8) == 0; }), | ||
| 587 | "reaching half the capacity submits without Submit"); | ||
| 588 | bool all = true; | ||
| 589 | for (int i = 0; i < 64; ++i) all = all && std::memcmp(outs[i].data(), env.b.data() + i, 8) == 0; | ||
| 590 | check(all, "every auto-submitted read has its bytes"); | ||
| 591 | q->Release(); | ||
| 592 | |||
| 593 | // Auto-submit disabled and full: an enqueue waits until another thread submits. | ||
| 594 | q = make_queue(env, 0x80, false); | ||
| 595 | IDStorageStatusArrayX* s = make_status(env, 1); | ||
| 596 | constexpr int kMany = 400; | ||
| 597 | std::vector<uint8_t> sink(kMany * 4); | ||
| 598 | std::atomic<bool> done{false}; | ||
| 599 | std::thread producer([&] { | ||
| 600 | for (int i = 0; i < kMany; ++i) { | ||
| 601 | DSTORAGE_REQUEST rr = file_request(b, i, sink.data() + i * 4, 4); | ||
| 602 | q->EnqueueRequest(&rr); | ||
| 603 | } | ||
| 604 | q->EnqueueStatus(s, 0); | ||
| 605 | done.store(true); | ||
| 606 | }); | ||
| 607 | check(wait_until([&] { | ||
| 608 | DSTORAGE_QUEUE_INFO i{}; | ||
| 609 | q->Query(&i); | ||
| 610 | return i.EmptySlotCount == 0; | ||
| 611 | }), | ||
| 612 | "the queue fills to capacity - 1"); | ||
| 613 | Sleep(20); | ||
| 614 | check(!done.load(), "an enqueue into a full queue waits"); | ||
| 615 | while (!done.load()) { | ||
| 616 | q->Submit(); | ||
| 617 | Sleep(1); | ||
| 618 | } | ||
| 619 | producer.join(); | ||
| 620 | q->Submit(); | ||
| 621 | check(wait_complete(s, 0), "the producer's reads complete"); | ||
| 622 | bool same = true; | ||
| 623 | for (int i = 0; i < kMany; ++i) same = same && std::memcmp(sink.data() + i * 4, env.b.data() + i, 4) == 0; | ||
| 624 | check(same, "every read of the full-queue run has its bytes"); | ||
| 625 | s->Release(); | ||
| 626 | q->Release(); | ||
| 627 | b->Release(); | ||
| 628 | } | ||
| 629 | |||
| 630 | void test_memory_queue(Env& env) { | ||
| 631 | IDStorageQueueX2* q = make_queue(env, 0x80, true, DSTORAGE_REQUEST_SOURCE_MEMORY, DSTORAGE_PRIORITY_REALTIME); | ||
| 632 | IDStorageStatusArrayX* s = make_status(env, 4); | ||
| 633 | std::vector<uint8_t> buf = pattern(8192, 9); | ||
| 634 | std::vector<uint8_t> want = buf; | ||
| 635 | std::memmove(want.data() + 1024, want.data(), 4096); | ||
| 636 | DSTORAGE_REQUEST r{}; | ||
| 637 | r.Options.SourceType = DSTORAGE_REQUEST_SOURCE_MEMORY; | ||
| 638 | r.Source = buf.data(); | ||
| 639 | r.Destination = buf.data() + 1024; | ||
| 640 | r.SourceSize = r.DestinationSize = 4096; | ||
| 641 | q->EnqueueRequest(&r); | ||
| 642 | q->EnqueueStatus(s, 0); | ||
| 643 | void* reserved = VirtualAlloc(nullptr, 1 << 16, MEM_RESERVE, PAGE_READWRITE); | ||
| 644 | DSTORAGE_REQUEST bad = r; | ||
| 645 | bad.Destination = reserved; | ||
| 646 | q->EnqueueRequest(&bad); | ||
| 647 | q->EnqueueStatus(s, 1); | ||
| 648 | DSTORAGE_REQUEST file_on_memory{}; // a file request on a memory queue | ||
| 649 | file_on_memory.Destination = buf.data(); | ||
| 650 | file_on_memory.SourceSize = file_on_memory.DestinationSize = 4; | ||
| 651 | q->EnqueueRequest(&file_on_memory); | ||
| 652 | q->EnqueueStatus(s, 2); | ||
| 653 | DSTORAGE_REQUEST huge = r; | ||
| 654 | huge.SourceSize = huge.DestinationSize = (1u << 29) + 1; // more than 1 GiB together | ||
| 655 | q->EnqueueRequest(&huge); | ||
| 656 | q->EnqueueStatus(s, 3); | ||
| 657 | q->Submit(); | ||
| 658 | check(wait_complete(s, 3), "memory requests complete"); | ||
| 659 | check_hr(s->GetHResult(0), S_OK, "a memory-to-memory copy"); | ||
| 660 | check(buf == want, "an overlapping memory copy moves like memmove"); | ||
| 661 | check_hr(s->GetHResult(1), E_DSTORAGE_ACCESS_VIOLATION, "a memory copy to memory the process cannot write"); | ||
| 662 | check_hr(s->GetHResult(2), E_DSTORAGE_INVALID_SOURCE_TYPE, "a file request on a memory queue"); | ||
| 663 | check_hr(s->GetHResult(3), E_DSTORAGE_REQUEST_TOO_LARGE, "source plus destination over 1 GiB"); | ||
| 664 | VirtualFree(reserved, 0, MEM_RELEASE); | ||
| 665 | s->Release(); | ||
| 666 | q->Release(); | ||
| 667 | } | ||
| 668 | |||
| 669 | void test_close_and_release(Env& env) { | ||
| 670 | IDStorageFileX* big = open(env, L"GAME:\\data\\big.bin"); | ||
| 671 | // Close with requests in flight: it returns after they all ran. | ||
| 672 | IDStorageQueueX2* q = make_queue(env, 0x100, true); | ||
| 673 | constexpr int kReads = 300; | ||
| 674 | constexpr uint32_t kChunk = 8192; | ||
| 675 | std::vector<uint8_t> out(kReads * kChunk); | ||
| 676 | for (int i = 0; i < kReads; ++i) { | ||
| 677 | DSTORAGE_REQUEST r = file_request(big, static_cast<uint64_t>(i) * kChunk, out.data() + i * kChunk, kChunk); | ||
| 678 | q->EnqueueRequest(&r); | ||
| 679 | } | ||
| 680 | q->Close(); | ||
| 681 | check(std::memcmp(out.data(), env.big.data(), out.size()) == 0, "Close returns after every enqueued read ran"); | ||
| 682 | IDStorageStatusArrayX* s = make_status(env, 1); | ||
| 683 | std::vector<uint8_t> tmp(8, 0); | ||
| 684 | DSTORAGE_REQUEST r = file_request(big, 0, tmp.data(), 8); | ||
| 685 | q->EnqueueRequest(&r); | ||
| 686 | q->EnqueueStatus(s, 0); | ||
| 687 | q->Submit(); | ||
| 688 | q->Close(); // a second Close is harmless | ||
| 689 | Sleep(20); | ||
| 690 | check(tmp == std::vector<uint8_t>(8, 0), "a request enqueued after Close is not run"); | ||
| 691 | DSTORAGE_ERROR_RECORD rec{}; | ||
| 692 | q->RetrieveErrorRecord(&rec); | ||
| 693 | check(rec.FailureCount == 2 && rec.FirstFailure.HResult == E_DSTORAGE_QUEUE_CLOSED, | ||
| 694 | "enqueues after Close are recorded"); | ||
| 695 | check(q->Release() == 0, "a closed queue is freed by its last Release"); | ||
| 696 | |||
| 697 | // The last Release with requests in flight finishes them first. | ||
| 698 | q = make_queue(env, 0x100, true); | ||
| 699 | std::fill(out.begin(), out.end(), uint8_t{0}); | ||
| 700 | for (int i = 0; i < kReads; ++i) { | ||
| 701 | DSTORAGE_REQUEST rr = file_request(big, static_cast<uint64_t>(i) * kChunk, out.data() + i * kChunk, kChunk); | ||
| 702 | q->EnqueueRequest(&rr); | ||
| 703 | } | ||
| 704 | q->Submit(); | ||
| 705 | big->Release(); // the requests hold the file | ||
| 706 | check(q->Release() == 0, "the last Release of a busy queue"); | ||
| 707 | check(std::memcmp(out.data(), env.big.data(), out.size()) == 0, "the last Release ran the reads in flight"); | ||
| 708 | |||
| 709 | // IDStorageFileX::Close: submitted reads finish, a later one fails with E_DSTORAGE_FILE_NOT_OPEN. | ||
| 710 | IDStorageFileX* b = open(env, L"GAME:\\data\\b.bin"); | ||
| 711 | q = make_queue(env, 0x80, false); | ||
| 712 | IDStorageStatusArrayX* s2 = make_status(env, 3); | ||
| 713 | std::vector<uint8_t> first(100, 0), second(100, 0); | ||
| 714 | DSTORAGE_REQUEST r1 = file_request(b, 0, first.data(), 100); | ||
| 715 | q->EnqueueRequest(&r1); | ||
| 716 | q->EnqueueStatus(s2, 0); | ||
| 717 | q->Submit(); | ||
| 718 | DSTORAGE_REQUEST r2 = file_request(b, 0, second.data(), 100); | ||
| 719 | q->EnqueueRequest(&r2); // not submitted yet | ||
| 720 | b->Close(); | ||
| 721 | check(std::memcmp(first.data(), env.b.data(), 100) == 0, | ||
| 722 | "IDStorageFileX::Close waits for the file's submitted reads"); | ||
| 723 | check(wait_complete(s2, 0) && s2->GetHResult(0) == S_OK, "the submitted read succeeded"); | ||
| 724 | check(b->GetHandle() == INVALID_HANDLE_VALUE, "GetHandle on a closed file"); | ||
| 725 | q->EnqueueStatus(s2, 1); | ||
| 726 | DSTORAGE_REQUEST r3 = file_request(b, 0, second.data(), 0); | ||
| 727 | q->EnqueueRequest(&r3); | ||
| 728 | q->EnqueueStatus(s2, 2); | ||
| 729 | q->Submit(); | ||
| 730 | check(wait_complete(s2, 2), "the reads after the close complete"); | ||
| 731 | check_hr(s2->GetHResult(1), E_DSTORAGE_FILE_NOT_OPEN, "a read of a closed file"); | ||
| 732 | check_hr(s2->GetHResult(2), E_DSTORAGE_FILE_NOT_OPEN, "a zero-size read of a closed file"); | ||
| 733 | check(second == std::vector<uint8_t>(100, 0), "a read of a closed file writes nothing"); | ||
| 734 | q->Release(); | ||
| 735 | b->Release(); | ||
| 736 | s->Release(); | ||
| 737 | s2->Release(); | ||
| 738 | } | ||
| 739 | |||
| 740 | void test_threads(Env& env) { | ||
| 741 | IDStorageFileX* big = open(env, L"GAME:\\data\\big.bin"); | ||
| 742 | IDStorageQueueX2* q = make_queue(env, 0x80, true); | ||
| 743 | constexpr int kThreads = 4, kPer = 250; | ||
| 744 | constexpr uint32_t kChunk = 512; | ||
| 745 | std::vector<std::vector<uint8_t>> outs(kThreads, std::vector<uint8_t>(kPer * kChunk)); | ||
| 746 | std::vector<IDStorageStatusArrayX*> arrays; | ||
| 747 | for (int t = 0; t < kThreads; ++t) arrays.push_back(make_status(env, 1)); | ||
| 748 | std::vector<std::thread> threads; | ||
| 749 | for (int t = 0; t < kThreads; ++t) | ||
| 750 | threads.emplace_back([&, t] { | ||
| 751 | for (int i = 0; i < kPer; ++i) { | ||
| 752 | const uint64_t offset = (static_cast<uint64_t>(t) * kPer + i) * kChunk % (env.big.size() - kChunk); | ||
| 753 | DSTORAGE_REQUEST r = file_request(big, offset, outs[t].data() + i * kChunk, kChunk, t); | ||
| 754 | q->EnqueueRequest(&r); | ||
| 755 | } | ||
| 756 | q->EnqueueStatus(arrays[t], 0); | ||
| 757 | q->Submit(); | ||
| 758 | }); | ||
| 759 | for (auto& th : threads) th.join(); | ||
| 760 | bool ok = true; | ||
| 761 | for (int t = 0; t < kThreads; ++t) { | ||
| 762 | ok = ok && wait_complete(arrays[t], 0) && arrays[t]->GetHResult(0) == S_OK; | ||
| 763 | for (int i = 0; i < kPer; ++i) { | ||
| 764 | const uint64_t offset = (static_cast<uint64_t>(t) * kPer + i) * kChunk % (env.big.size() - kChunk); | ||
| 765 | ok = ok && std::memcmp(outs[t].data() + i * kChunk, env.big.data() + offset, kChunk) == 0; | ||
| 766 | } | ||
| 767 | } | ||
| 768 | check(ok, "four threads enqueueing at once all read their bytes"); | ||
| 769 | for (auto* a : arrays) a->Release(); | ||
| 770 | q->Release(); | ||
| 771 | big->Release(); | ||
| 772 | } | ||
| 773 | |||
| 774 | void test_iid_policy_events_and_cancel(Env& env) { | ||
| 775 | // Well-known system interfaces and made-up ids: QueryInterface refuses all of them. | ||
| 776 | const GUID kRandom = {0x5eed0001, 0x2222, 0x4222, {0x82, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x01}}; | ||
| 777 | const GUID kIMarshal = {0x00000003, 0x0000, 0x0000, {0xc0, 0, 0, 0, 0, 0, 0, 0x46}}; | ||
| 778 | const GUID kIAgileObject = {0x94ea2b94, 0xe9cc, 0x49e0, {0xc0, 0xff, 0xee, 0x64, 0xca, 0x8f, 0x5b, 0x90}}; | ||
| 779 | const GUID kINoMarshal = {0xecc8691b, 0xc1db, 0x4dc0, {0x85, 0x5e, 0x65, 0xf6, 0xc5, 0x51, 0xaf, 0x49}}; | ||
| 780 | const GUID kIClientSecurity = {0x0000013d, 0x0000, 0x0000, {0xc0, 0, 0, 0, 0, 0, 0, 0x46}}; | ||
| 781 | const GUID kPcStatusArrayId = {0x82397587, 0x7cd5, 0x453b, {0xa0, 0x2e, 0x31, 0x37, 0x9b, 0xd6, 0x46, 0x56}}; | ||
| 782 | const GUID kPcQueueId = {0xcfdbd83f, 0x9e06, 0x4fda, {0x8e, 0xa5, 0x69, 0x04, 0x21, 0x37, 0xf4, 0x9b}}; | ||
| 783 | const GUID kQueueId2 = {0x0d5e0012, 0x1111, 0x4111, {0x81, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x12}}; | ||
| 784 | const GUID refused[] = {kRandom, kIMarshal, kIAgileObject, kINoMarshal, kIClientSecurity}; | ||
| 785 | |||
| 786 | IDStorageQueueX2* q = make_queue(env, 0x80, false); | ||
| 787 | IDStorageFileX* b = open(env, L"GAME:\\data\\b.bin"); | ||
| 788 | IDStorageStatusArrayX* s = make_status(env, 8); | ||
| 789 | IUnknown* objects[] = {env.factory, q, b, s}; | ||
| 790 | for (IUnknown* o : objects) | ||
| 791 | for (const GUID& g : refused) { | ||
| 792 | void* out = reinterpret_cast<void*>(1); | ||
| 793 | check_hr(o->QueryInterface(g, &out), E_NOINTERFACE, "QueryInterface refuses an unknown or system id"); | ||
| 794 | check(out == nullptr, "a refused QueryInterface clears ppv"); | ||
| 795 | } | ||
| 796 | void* out = nullptr; | ||
| 797 | check_hr(q->QueryInterface(kFileId, &out), E_NOINTERFACE, "a queue refuses a file id"); | ||
| 798 | check_hr(b->QueryInterface(kQueueId, &out), E_NOINTERFACE, "a file refuses a queue id"); | ||
| 799 | check_hr(s->QueryInterface(kFactoryId, &out), E_NOINTERFACE, "a status array refuses a factory id"); | ||
| 800 | check_hr(q->QueryInterface(__uuidof(IUnknown), &out), S_OK, "QueryInterface(IUnknown)"); | ||
| 801 | if (out) static_cast<IUnknown*>(out)->Release(); | ||
| 802 | |||
| 803 | // Creating calls learn a new id but refuse system ones. | ||
| 804 | auto get = reinterpret_cast<DStorageGetFactoryFn>(rt::shims::find("dstorage_xs.dll", "DStorageGetFactory")); | ||
| 805 | check_hr(get(kIMarshal, &out), E_NOINTERFACE, "DStorageGetFactory refuses IMarshal"); | ||
| 806 | check_hr(get(kIAgileObject, &out), E_NOINTERFACE, "DStorageGetFactory refuses IAgileObject"); | ||
| 807 | DSTORAGE_QUEUE_DESC1 d{}; | ||
| 808 | d.Capacity = 0x80; | ||
| 809 | check_hr(env.factory->CreateQueue1(&d, kINoMarshal, &out), E_NOINTERFACE, "CreateQueue1 refuses INoMarshal"); | ||
| 810 | check_hr(env.factory->OpenFile(L"GAME:\\data\\b.bin", kIClientSecurity, &out), E_NOINTERFACE, | ||
| 811 | "OpenFile refuses IClientSecurity"); | ||
| 812 | check_hr(env.factory->CreateQueue1(&d, kPcQueueId, &out), E_NOINTERFACE, "CreateQueue1 refuses the PC queue id"); | ||
| 813 | check_hr(env.factory->CreateQueue1(&d, kPcStatusArrayId, &out), E_NOINTERFACE, | ||
| 814 | "the status array id is not a queue id"); | ||
| 815 | check_hr(env.factory->CreateQueue1(&d, kQueueId2, &out), S_OK, "a creating call learns a new queue id"); | ||
| 816 | if (out) static_cast<IUnknown*>(out)->Release(); | ||
| 817 | check_hr(q->QueryInterface(kQueueId2, &out), S_OK, "QueryInterface accepts an id a queue was created with"); | ||
| 818 | if (out) static_cast<IUnknown*>(out)->Release(); | ||
| 819 | |||
| 820 | // The PC IDStorageStatusArray id has the console interface's shape and is accepted. | ||
| 821 | check_hr(env.factory->CreateStatusArray(2, nullptr, kPcStatusArrayId, &out), S_OK, | ||
| 822 | "CreateStatusArray accepts the PC IDStorageStatusArray id"); | ||
| 823 | if (out) static_cast<IUnknown*>(out)->Release(); | ||
| 824 | check_hr(s->QueryInterface(kPcStatusArrayId, &out), S_OK, "a status array answers the PC IDStorageStatusArray id"); | ||
| 825 | if (out) static_cast<IUnknown*>(out)->Release(); | ||
| 826 | |||
| 827 | // EnqueueSetEvent duplicates the handle: closing the title's handle, even with its value reused, changes nothing. | ||
| 828 | HANDLE first = CreateEventW(nullptr, TRUE, FALSE, nullptr); | ||
| 829 | q->EnqueueSetEvent(first); | ||
| 830 | CloseHandle(first); | ||
| 831 | HANDLE second = CreateEventW(nullptr, TRUE, FALSE, nullptr); // may get the same handle value | ||
| 832 | q->EnqueueSetEvent(reinterpret_cast<HANDLE>(static_cast<uintptr_t>(0x7ffffff0))); // not a handle | ||
| 833 | q->EnqueueSetEvent(nullptr); | ||
| 834 | q->EnqueueStatus(s, 0); | ||
| 835 | q->Submit(); | ||
| 836 | check(wait_complete(s, 0), "the event entries complete"); | ||
| 837 | check(WaitForSingleObject(second, 0) == WAIT_TIMEOUT, "a closed event's reused handle value is not set"); | ||
| 838 | DSTORAGE_ERROR_RECORD rec{}; | ||
| 839 | q->RetrieveErrorRecord(&rec); | ||
| 840 | check(rec.FailureCount == 2 && rec.FirstFailure.CommandType == kCommandTypeEvent && | ||
| 841 | FAILED(rec.FirstFailure.HResult), | ||
| 842 | "an invalid and a null event handle are recorded as EVENT failures; the closed one is not"); | ||
| 843 | CloseHandle(second); | ||
| 844 | |||
| 845 | // A cancelled request that also had an enqueue error is still a success, and not counted as a failure. | ||
| 846 | std::vector<uint8_t> tmp(8, 0); | ||
| 847 | DSTORAGE_REQUEST foreign = file_request(reinterpret_cast<IDStorageFileX*>(s), 0, tmp.data(), 8, 0x99); | ||
| 848 | q->EnqueueRequest(&foreign); | ||
| 849 | DSTORAGE_REQUEST null_file = file_request(nullptr, 0, tmp.data(), 8, 0x99); | ||
| 850 | q->EnqueueRequest(&null_file); | ||
| 851 | q->CancelRequestsWithTag(~0ull, 0x99); | ||
| 852 | q->EnqueueStatus(s, 1); | ||
| 853 | q->Submit(); | ||
| 854 | check(wait_complete(s, 1), "cancelled bad requests complete"); | ||
| 855 | check_hr(s->GetHResult(1), S_OK, "a cancelled request with an enqueue error is a success"); | ||
| 856 | q->RetrieveErrorRecord(&rec); | ||
| 857 | check(rec.FailureCount == 0, "a cancelled request is not counted as a failure"); | ||
| 858 | |||
| 859 | s->Release(); | ||
| 860 | b->Release(); | ||
| 861 | q->Release(); | ||
| 862 | } | ||
| 863 | |||
| 864 | void test_stats(Env& env) { | ||
| 865 | DSTORAGE_STATS st{}; | ||
| 866 | env.factory->GetStats(&st); | ||
| 867 | check(st.CurrentOpenFileCount == 0, "every file was released"); | ||
| 868 | check(st.NormalPriorityStats.CurrentQueueCount == 0 && st.RealtimePriorityStats.CurrentQueueCount == 0, | ||
| 869 | "every queue was released"); | ||
| 870 | check(st.NormalPriorityStats.TotalRequestCount > 1000 && st.NormalPriorityStats.TotalFenceCount >= 2 && | ||
| 871 | st.RealtimePriorityStats.TotalRequestCount == 4, | ||
| 872 | "requests and fences are counted per priority"); | ||
| 873 | check(st.CumulativeOpenFileCount >= 10 && st.MaxOpenFileCount >= 3, "files are counted"); | ||
| 874 | check(st.ErrorCount > 10, "errors are counted"); | ||
| 875 | } | ||
| 876 | |||
| 877 | } // namespace | ||
| 878 | |||
| 879 | int main() { | ||
| 880 | wchar_t temp[MAX_PATH]; | ||
| 881 | GetTempPathW(MAX_PATH, temp); | ||
| 882 | Env env; | ||
| 883 | env.root = fs::path(temp) / (L"leonida_dstorage_test_" + std::to_wstring(GetCurrentProcessId())); | ||
| 884 | env.game = env.root / L"game"; | ||
| 885 | env.overlay = env.root / L"overlay"; | ||
| 886 | std::error_code ec; | ||
| 887 | fs::remove_all(env.root, ec); | ||
| 888 | env.a_package = pattern(4096, 1); | ||
| 889 | env.a_overlay = pattern(5000, 2); // another size and other bytes than the package's copy | ||
| 890 | env.b = pattern(10000, 3); | ||
| 891 | env.big = pattern(3u << 20, 4); | ||
| 892 | write_file(env.game / L"data" / L"a.bin", env.a_package); | ||
| 893 | write_file(env.overlay / L"data" / L"a.bin", env.a_overlay); | ||
| 894 | write_file(env.game / L"data" / L"b.bin", env.b); | ||
| 895 | write_file(env.game / L"data" / L"big.bin", env.big); | ||
| 896 | rt::fs::configure(env.game, env.overlay); | ||
| 897 | |||
| 898 | test_layout_and_options(); | ||
| 899 | test_registry(env); | ||
| 900 | test_factory_checks(env); | ||
| 901 | test_open_paths(env); | ||
| 902 | test_reads_and_errors(env); | ||
| 903 | test_fence_order_and_events(env); | ||
| 904 | test_cancel(env); | ||
| 905 | test_auto_submit_and_full_queue(env); | ||
| 906 | test_memory_queue(env); | ||
| 907 | test_close_and_release(env); | ||
| 908 | test_threads(env); | ||
| 909 | test_iid_policy_events_and_cancel(env); | ||
| 910 | test_stats(env); | ||
| 911 | |||
| 912 | fs::remove_all(env.root, ec); | ||
| 913 | std::printf("dstorage: %d checks, %d failures\n", g_checks, g_failures); | ||
| 914 | return g_failures ? 1 : 0; | ||
| 915 | } | ||
diff --git a/runtime/test/unimplemented_policy_test.cpp b/runtime/test/unimplemented_policy_test.cpp index 77a6e5595..776f88ceb 100644 --- a/runtime/test/unimplemented_policy_test.cpp +++ b/runtime/test/unimplemented_policy_test.cpp | |||
| @@ -231,6 +231,8 @@ void test_classification() { | |||
| 231 | check(cls("gameinput.dll", "GameInputCreate") == ReturnClass::HResult, "GameInputCreate is hresult"); | 231 | check(cls("gameinput.dll", "GameInputCreate") == ReturnClass::HResult, "GameInputCreate is hresult"); |
| 232 | check(cls("dstorage_xs.dll", "DStorageGetFactory") == ReturnClass::HResult, | 232 | check(cls("dstorage_xs.dll", "DStorageGetFactory") == ReturnClass::HResult, |
| 233 | "the console dstorage name is hresult"); | 233 | "the console dstorage name is hresult"); |
| 234 | check(cls("xcrdapi.dll", "DStorageGetFactory") == ReturnClass::HResult, "xcrdapi's DStorageGetFactory is hresult"); | ||
| 235 | check(cls("xcrdapi.dll", "XCrdLeonidaPolicyProbe") == ReturnClass::Other, "other xcrdapi imports are other"); | ||
| 234 | check(cls("xg_xs.dll", "XGCreateTexture2DComputer") == ReturnClass::Other, "unmeasured xg_xs import is other"); | 236 | check(cls("xg_xs.dll", "XGCreateTexture2DComputer") == ReturnClass::Other, "unmeasured xg_xs import is other"); |
| 235 | check(cls("acphal.dll", "AcpHalCreate") == ReturnClass::Other, "unmeasured acphal import is other"); | 237 | check(cls("acphal.dll", "AcpHalCreate") == ReturnClass::Other, "unmeasured acphal import is other"); |
| 236 | check(cls("xgameruntime.dll", "XGameRuntimeInitialize") == ReturnClass::HResult, "XGameRuntimeInitialize"); | 238 | check(cls("xgameruntime.dll", "XGameRuntimeInitialize") == ReturnClass::HResult, "XGameRuntimeInitialize"); |