diff --git a/runtime/shims/xgameruntime_gamesave.cpp b/runtime/shims/xgameruntime_gamesave.cpp index 4433ea7e14..fdc5ddec31 100644 --- a/runtime/shims/xgameruntime_gamesave.cpp +++ b/runtime/shims/xgameruntime_gamesave.cpp @@ -183,10 +183,12 @@ std::recursive_mutex g_mutex; struct XGameSaveProvider { fs::path user_dir; // \local-user std::string config_id; + uint64_t serial = 0; }; struct XGameSaveContainer { fs::path user_dir; // the provider's, copied: a container outlives a closed provider safely std::string name; + uint64_t serial = 0; fs::path dir() const { return user_dir / widen(encode_name(name)); } }; struct Op { @@ -195,16 +197,34 @@ struct Op { }; struct XGameSaveUpdate { XGameSaveContainer* container; + uint64_t container_serial; // the container's serial when the update was made std::string display_name; bool has_display_name; std::map ops; // blob name -> its one write or delete uint64_t bytes; + uint64_t serial = 0; }; std::unordered_set g_providers, g_containers, g_updates; bool live(const std::unordered_set& set, const void* h) { return h && set.count(h) != 0; } +// Every handle gets a serial when it is made, and no two get the same one. The heap may hand a closed handle's address +// to the next handle of its kind, so whatever keeps a handle past the call that gave it (an update its container, an +// async job its provider, container or update) keeps the serial as well, and the handle counts as the same one only +// while both match. A reused address is then refused as expired instead of acting on another save. Under g_mutex. +uint64_t g_next_serial = 1; + +template bool live_as(const std::unordered_set& set, const T* h, uint64_t serial) { + return live(set, h) && h->serial == serial; +} + +// The serial of a live handle; 0, which no handle has, for one that isn't, so a job made from it fails as expired. +template uint64_t serial_of(const std::unordered_set& set, const T* h) { + std::lock_guard lock(g_mutex); + return live(set, h) ? h->serial : 0; +} + // ---- Files ---- struct BlobFile { @@ -405,6 +425,7 @@ HRESULT open_provider(XUserHandle user, const char* config_id, XGameSaveProvider if (ec) return E_GS_OUT_OF_LOCAL_STORAGE; auto* p = new XGameSaveProvider{user_dir, config_id}; std::lock_guard lock(g_mutex); + p->serial = g_next_serial++; g_providers.insert(p); *out = p; static std::atomic s_logged{0}; @@ -433,7 +454,7 @@ HRESULT delete_container(XGameSaveProviderHandle p, const char* name) { HRESULT submit_update(XGameSaveUpdateHandle u) { std::lock_guard lock(g_mutex); - if (!live(g_updates, u) || !live(g_containers, u->container)) return E_GS_HANDLE_EXPIRED; + if (!live(g_updates, u) || !live_as(g_containers, u->container, u->container_serial)) return E_GS_HANDLE_EXPIRED; return commit(u); } @@ -446,6 +467,7 @@ struct Job { XGameSaveProviderHandle provider = nullptr; XGameSaveContainerHandle container = nullptr; XGameSaveUpdateHandle update = nullptr; + uint64_t serial = 0; // the serial of the provider, container or update above, when the job was made std::vector names; bool all_blobs = false; // results @@ -473,15 +495,25 @@ HRESULT do_work(Job& job, size_t& size) { return S_OK; } if (job.identity == &kQuotaIdentity) { + std::lock_guard lock(g_mutex); + if (!live_as(g_providers, job.provider, job.serial)) return E_GS_HANDLE_EXPIRED; const HRESULT hr = remaining_quota(job.provider, &job.quota); if (SUCCEEDED(hr)) size = sizeof(int64_t); return hr; } - if (job.identity == &kDeleteIdentity) return delete_container(job.provider, job.text.c_str()); - if (job.identity == &kSubmitIdentity) return submit_update(job.update); + if (job.identity == &kDeleteIdentity) { + std::lock_guard lock(g_mutex); + if (!live_as(g_providers, job.provider, job.serial)) return E_GS_HANDLE_EXPIRED; + return delete_container(job.provider, job.text.c_str()); + } + if (job.identity == &kSubmitIdentity) { + std::lock_guard lock(g_mutex); + if (!live_as(g_updates, job.update, job.serial)) return E_GS_HANDLE_EXPIRED; + return submit_update(job.update); + } if (job.identity == &kReadIdentity) { std::lock_guard lock(g_mutex); - if (!live(g_containers, job.container)) return E_GS_HANDLE_EXPIRED; + if (!live_as(g_containers, job.container, job.serial)) return E_GS_HANDLE_EXPIRED; std::vector names; for (const std::string& n : job.names) names.push_back(n.c_str()); const HRESULT hr = read_blobs(job.container, job.all_blobs ? nullptr : names.data(), @@ -597,6 +629,7 @@ HRESULT STDMETHODCALLTYPE save_quota(void*, XGameSaveProviderHandle p, int64_t* HRESULT STDMETHODCALLTYPE save_quota_async(void*, XGameSaveProviderHandle p, XAsyncBlock* async) { auto* job = new Job{&kQuotaIdentity}; job->provider = p; + job->serial = serial_of(g_providers, p); return begin(async, job, "XGameSaveGetRemainingQuotaAsync"); } @@ -616,6 +649,7 @@ HRESULT STDMETHODCALLTYPE save_delete_async(void*, XGameSaveProviderHandle p, co if (!valid_container_name(name)) return E_GS_INVALID_CONTAINER_NAME; auto* job = new Job{&kDeleteIdentity}; job->provider = p; + job->serial = serial_of(g_providers, p); job->text = name; return begin(async, job, "XGameSaveDeleteContainerAsync"); } @@ -673,6 +707,7 @@ HRESULT STDMETHODCALLTYPE save_create_container(void*, XGameSaveProviderHandle p if (!live(g_providers, p)) return E_GS_HANDLE_EXPIRED; if (!valid_container_name(name)) return E_GS_INVALID_CONTAINER_NAME; auto* c = new XGameSaveContainer{p->user_dir, name}; + c->serial = g_next_serial++; g_containers.insert(c); *out = c; return S_OK; @@ -733,6 +768,7 @@ HRESULT STDMETHODCALLTYPE save_read_async(void*, XGameSaveContainerHandle c, con XAsyncBlock* async) { auto* job = new Job{&kReadIdentity}; job->container = c; + job->serial = serial_of(g_containers, c); job->all_blobs = names == nullptr; for (uint32_t i = 0; names && i < count; ++i) { if (!valid_blob_name(names[i])) { @@ -764,7 +800,8 @@ HRESULT STDMETHODCALLTYPE save_create_update(void*, XGameSaveContainerHandle c, *out = nullptr; std::lock_guard lock(g_mutex); if (!live(g_containers, c)) return E_GS_HANDLE_EXPIRED; - auto* u = new XGameSaveUpdate{c, display_name ? display_name : "", display_name != nullptr, {}, 0}; + auto* u = new XGameSaveUpdate{c, c->serial, display_name ? display_name : "", display_name != nullptr, {}, 0}; + u->serial = g_next_serial++; g_updates.insert(u); *out = u; return S_OK; @@ -809,6 +846,7 @@ HRESULT STDMETHODCALLTYPE save_submit(void*, XGameSaveUpdateHandle u) { return s HRESULT STDMETHODCALLTYPE save_submit_async(void*, XGameSaveUpdateHandle u, XAsyncBlock* async) { auto* job = new Job{&kSubmitIdentity}; job->update = u; + job->serial = serial_of(g_updates, u); return begin(async, job, "XGameSaveSubmitUpdateAsync"); } diff --git a/runtime/test/xgameruntime_gamesave_test.cpp b/runtime/test/xgameruntime_gamesave_test.cpp index 08ae20b65c..d2d7f45eb3 100644 --- a/runtime/test/xgameruntime_gamesave_test.cpp +++ b/runtime/test/xgameruntime_gamesave_test.cpp @@ -296,6 +296,72 @@ int main() { check(at(save, 32)(save, user, "SCID", &fquota) == S_OK && fquota == (int64_t{1} << 30), "FilesGetRemainingQuota of an empty folder"); + // ---- a handle that outlives its container, after the heap hands the address out again ---- + // An update and an async read keep the container handle they were made from. Once that container is closed, the + // next container may get the same address; the stale update and read must still be refused, not write into or + // read from the newer container (another save). + { + Handle first = nullptr, stale_update = nullptr; + check(at(save, 16)(save, provider, "first", &first) == S_OK && + at(save, 23)(save, first, nullptr, &stale_update) == S_OK && + at(save, 25)(save, stale_update, "mine", reinterpret_cast("first's"), 7) == S_OK, + "an update of container \"first\" buffers a blob"); + XTaskQueueHandle manual = nullptr; + XTaskQueueCreate(XTaskQueueDispatchMode::Manual, XTaskQueueDispatchMode::Manual, &manual); + XAsyncBlock stale_read{}; + stale_read.queue = manual; + check(manual && at(save, 21)(save, first, nullptr, 0, &stale_read) == S_OK, + "a ReadBlobDataAsync of \"first\" waits on a manual queue"); + at(save, 17)(save, first); + + // Containers are made until one lands at the closed container's address (most heaps hand it back at once). + Handle second = nullptr; + std::vector others; + for (int i = 0; i < 4096 && !second; ++i) { + Handle c = nullptr; + if (at(save, 16)(save, provider, "second", &c) != S_OK) break; + if (c == first) second = c; + else others.push_back(c); + } + for (Handle c : others) at(save, 17)(save, c); + if (!second) { + std::printf("SKIP the heap never reused a closed container's address; the stale-handle checks need it\n"); + } else { + Handle theirs = nullptr; + check(at(save, 23)(save, second, nullptr, &theirs) == S_OK && + at(save, 25)(save, theirs, "theirs", reinterpret_cast("second's"), 8) == + S_OK && + at(save, 27)(save, theirs) == S_OK, + "container \"second\", at the closed container's address, gets a blob of its own"); + at(save, 24)(save, theirs); + check(at(save, 27)(save, stale_update) == E_GS_HANDLE_EXPIRED, + "SubmitUpdate of an update whose container was closed is refused, though a new container has its " + "address"); + check(!std::filesystem::exists(root / L"local-user" / L"second" / L"mine"), + "the stale update wrote nothing into the container that took its container's address"); + } + while (XTaskQueueDispatch(manual, XTaskQueuePort::Work, 0) || + XTaskQueueDispatch(manual, XTaskQueuePort::Completion, 0)) {} + if (second) { + size_t stale_size = 0; + const HRESULT read_hr = XAsyncGetStatus(&stale_read, false); + XAsyncGetResultSize(&stale_read, &stale_size); + check(read_hr == E_GS_HANDLE_EXPIRED && stale_size == 0, + "ReadBlobDataAsync of a closed container completes with E_GS_HANDLE_EXPIRED, not with the blobs of " + "the container that took its address"); + at(save, 17)(save, second); + } + if (XAsyncGetStatus(&stale_read, false) == S_OK) { // collect a result so the async state is released + std::vector drain(1024); + uint32_t drained = 0; + at(save, 22)(save, &stale_read, drain.size() * 8, reinterpret_cast(drain.data()), &drained); + } + at(save, 24)(save, stale_update); + if (manual) XTaskQueueCloseHandle(manual); + std::filesystem::remove_all(root / L"local-user" / L"first", ec); + std::filesystem::remove_all(root / L"local-user" / L"second", ec); + } + at(save, 6)(save, provider); at(save, 6)(save, provider2); at(save, 6)(save, provider); // twice: ignored