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
2 changes: 1 addition & 1 deletion core/libs/prx/libSceAgcDriver/tests/BdaResources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ void failedImportRemapTests(const Context& context, const BdaTestAccess& access)
access.limitMemory(0);
const auto attempts = access.allocationAttempts();
Require(HostImportFor(importing, address, 2 * piece) == nullptr && access.allocationAttempts() == attempts + 1, "host import failure was not exercised");
Require(owner.use_count() == 1, "a failed import pins its owner");
Require(owner.use_count() == 2, "a failed import pins its owner beyond the registry and its kept lease snapshot");
Require(HostImportFor(importing, address, 2 * piece) == nullptr && access.allocationAttempts() == attempts + 1, "an unchanged failed import was retried");
add(3);
Require(HostImportFor(importing, address, 2 * piece) == nullptr && access.allocationAttempts() == attempts + 1, "an unrelated mapping retried a failed import");
Expand Down
37 changes: 36 additions & 1 deletion core/libs/prx/libSceAgcDriver/tests/GuestAllocations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,42 @@ void leaseSnapshotTests() {
const auto third = GuestAllocations::GuestAllocationsAcquire_nid_postfix();
Require(&third[0] != &first[0] && std::any_of(third.begin(), third.end(), [&](const auto& range) { return range->address == address + 64; }), "a lease taken after a registry change still shares the old snapshot");
}
Require(held.use_count() == 1, "a snapshot outlived its leases");
const auto* kept = &GuestAllocations::GuestAllocationsAcquire_nid_postfix()[0];
Require(&GuestAllocations::GuestAllocationsAcquire_nid_postfix()[0] == kept, "a lease taken after the others ended, without a registry change, rebuilt the snapshot");
Require(held.use_count() == 2, "the kept snapshot does not hold the range once");
{
GuestAllocations::Mutation mutation;
}
Require(held.use_count() == 1, "a snapshot outlived a registry change");
std::weak_ptr<const GuestAllocations::Range> readable;
{
const auto lease = GuestAllocations::GuestAllocationsAcquire_nid_postfix();
const auto found = std::find_if(lease.begin(), lease.end(), [&](const auto& range) { return range->address == address + 64; });
Require(found != lease.end(), "the snapshot misses a readable range");
readable = *found;
Require(!GuestAllocations::GuestAllocationsAcquireAll_nid_postfix().empty(), "the snapshot of all ranges is empty");
}
Require(readable.use_count() == 3, "the kept snapshots do not hold a readable range once each");
{
GuestAllocations::Mutation mutation;
mutation.Protect(memory.data() + 64, 64, false, false, true, [] {});
}
Require(readable.expired(), "a kept snapshot holds a range that a protection change replaced");
{
const auto lease = GuestAllocations::GuestAllocationsAcquire_nid_postfix();
Require(std::none_of(lease.begin(), lease.end(), [&](const auto& range) { return range->address == address + 64; }), "a lease taken after a range turned unreadable returned the old snapshot");
const auto all = GuestAllocations::GuestAllocationsAcquireAll_nid_postfix();
const auto found = std::find_if(all.begin(), all.end(), [&](const auto& range) { return range->address == address + 64; });
Require(found != all.end() && !(*found)->readable && !(*found)->writable, "a lease of all ranges taken after a range turned unreadable returned the old snapshot");
}
{
GuestAllocations::Mutation mutation;
mutation.Protect(memory.data() + 64, 64, true, true, true, [] {});
}
{
const auto lease = GuestAllocations::GuestAllocationsAcquire_nid_postfix();
Require(std::any_of(lease.begin(), lease.end(), [&](const auto& range) { return range->address == address + 64 && range->readable; }), "a lease taken after a range turned readable again returned the old snapshot");
}
GuestAllocations::Mutation mutation;
mutation.Remove(memory.data());
mutation.Remove(memory.data() + 64);
Expand Down
8 changes: 4 additions & 4 deletions core/libs/prx/libc/src/GuestAllocations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ struct Registry {
std::map<std::uint64_t, std::shared_ptr<const Range>> ranges;
bool mainImageRegistered = false;
std::map<std::uintptr_t, std::uintptr_t> registeredImages;
std::weak_ptr<const Lease::Ranges> readableLease;
std::weak_ptr<const Lease::Ranges> allLease;
std::shared_ptr<const Lease::Ranges> readableLease;
std::shared_ptr<const Lease::Ranges> allLease;
};

Registry& registry() {
Expand Down Expand Up @@ -606,7 +606,7 @@ void GuestAllocationsSetUnmapObserver_nid_postfix(void (*callback)(std::uintptr_

Lease GuestAllocationsAcquire_nid_postfix() {
std::lock_guard lock(registry().mutex);
if (auto shared = registry().readableLease.lock()) return Lease(std::move(shared));
if (auto shared = registry().readableLease) return Lease(std::move(shared));
auto ranges = std::make_shared<Lease::Ranges>();
for (const auto& [address, range] : registry().ranges) {
if (range->readable && range->bytes != 0) ranges->push_back(range);
Expand All @@ -633,7 +633,7 @@ std::shared_ptr<const Range> GuestAllocationsLookup_nid_postfix(std::uintptr_t a

Lease GuestAllocationsAcquireAll_nid_postfix() {
std::lock_guard lock(registry().mutex);
if (auto shared = registry().allLease.lock()) return Lease(std::move(shared));
if (auto shared = registry().allLease) return Lease(std::move(shared));
auto ranges = std::make_shared<Lease::Ranges>();
ranges->reserve(registry().ranges.size());
for (const auto& [address, range] : registry().ranges) {
Expand Down
Loading