Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions runtime/shims/xgameruntime_gamesave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,12 @@ std::recursive_mutex g_mutex;
struct XGameSaveProvider {
fs::path user_dir; // <root>\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 {
Expand All @@ -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<std::string, Op> ops; // blob name -> its one write or delete
uint64_t bytes;
uint64_t serial = 0;
};

std::unordered_set<const void*> g_providers, g_containers, g_updates;

bool live(const std::unordered_set<const void*>& 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 <class T> bool live_as(const std::unordered_set<const void*>& 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 <class T> uint64_t serial_of(const std::unordered_set<const void*>& set, const T* h) {
std::lock_guard<std::recursive_mutex> lock(g_mutex);
return live(set, h) ? h->serial : 0;
}

// ---- Files ----

struct BlobFile {
Expand Down Expand Up @@ -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<std::recursive_mutex> lock(g_mutex);
p->serial = g_next_serial++;
g_providers.insert(p);
*out = p;
static std::atomic<int> s_logged{0};
Expand Down Expand Up @@ -433,7 +454,7 @@ HRESULT delete_container(XGameSaveProviderHandle p, const char* name) {

HRESULT submit_update(XGameSaveUpdateHandle u) {
std::lock_guard<std::recursive_mutex> 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);
}

Expand All @@ -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<std::string> names;
bool all_blobs = false;
// results
Expand Down Expand Up @@ -473,15 +495,25 @@ HRESULT do_work(Job& job, size_t& size) {
return S_OK;
}
if (job.identity == &kQuotaIdentity) {
std::lock_guard<std::recursive_mutex> 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<std::recursive_mutex> 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<std::recursive_mutex> 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<std::recursive_mutex> 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<const char*> 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(),
Expand Down Expand Up @@ -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");
}

Expand All @@ -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");
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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])) {
Expand Down Expand Up @@ -764,7 +800,8 @@ HRESULT STDMETHODCALLTYPE save_create_update(void*, XGameSaveContainerHandle c,
*out = nullptr;
std::lock_guard<std::recursive_mutex> 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;
Expand Down Expand Up @@ -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");
}

Expand Down
66 changes: 66 additions & 0 deletions runtime/test/xgameruntime_gamesave_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,72 @@ int main() {
check(at<FilesQuota>(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<Create>(save, 16)(save, provider, "first", &first) == S_OK &&
at<Create>(save, 23)(save, first, nullptr, &stale_update) == S_OK &&
at<Write>(save, 25)(save, stale_update, "mine", reinterpret_cast<const uint8_t*>("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<ReadAsync>(save, 21)(save, first, nullptr, 0, &stale_read) == S_OK,
"a ReadBlobDataAsync of \"first\" waits on a manual queue");
at<Close>(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<Handle> others;
for (int i = 0; i < 4096 && !second; ++i) {
Handle c = nullptr;
if (at<Create>(save, 16)(save, provider, "second", &c) != S_OK) break;
if (c == first) second = c;
else others.push_back(c);
}
for (Handle c : others) at<Close>(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<Create>(save, 23)(save, second, nullptr, &theirs) == S_OK &&
at<Write>(save, 25)(save, theirs, "theirs", reinterpret_cast<const uint8_t*>("second's"), 8) ==
S_OK &&
at<Submit>(save, 27)(save, theirs) == S_OK,
"container \"second\", at the closed container's address, gets a blob of its own");
at<Close>(save, 24)(save, theirs);
check(at<Submit>(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<Close>(save, 17)(save, second);
}
if (XAsyncGetStatus(&stale_read, false) == S_OK) { // collect a result so the async state is released
std::vector<uint64_t> drain(1024);
uint32_t drained = 0;
at<ReadResult>(save, 22)(save, &stale_read, drain.size() * 8, reinterpret_cast<Blob*>(drain.data()), &drained);
}
at<Close>(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<Close>(save, 6)(save, provider);
at<Close>(save, 6)(save, provider2);
at<Close>(save, 6)(save, provider); // twice: ignored
Expand Down