Skip to content
Open
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
20 changes: 20 additions & 0 deletions core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryBacking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -921,12 +921,32 @@ bool IsFullyMapped(uint64_t vaddr, uint64_t size) {
return Shim::IsBacked(vaddr, size) || MappedPrefix(vaddr, size) == size;
}

namespace {

struct RememberedPrefix {
uint64_t vaddr = 0;
uint64_t size = 0;
uint64_t prefix = 0;
uint64_t probe_epoch = ~uint64_t {0};
uint64_t backing_epoch = ~uint64_t {0};
};

}

uint64_t ClampRangeSize(uint64_t vaddr, uint64_t size) {
if (!ValidRange(vaddr, size)) {
Shim::Fail("KytyShim memory: attempted to access invalid address 0x%016" PRIx64 " with size 0x%016" PRIx64 "\n", vaddr, size);
}
thread_local std::array<RememberedPrefix, 256> remembered {};
const auto probe_epoch = g_probe_epoch.load(std::memory_order_seq_cst);
const auto backing_epoch = g_backing_epoch.load(std::memory_order_acquire);
auto& slot = remembered[((vaddr >> PageBits) ^ (size * 0x9e3779b97f4a7c15ull >> 56u)) & 255u];
if (slot.vaddr == vaddr && slot.size == size && slot.probe_epoch == probe_epoch && slot.backing_epoch == backing_epoch) return slot.prefix;
if (Shim::IsBacked(vaddr, size)) return size;
const auto clamped = MappedPrefix(vaddr, size);
if (clamped != 0 && g_probe_epoch.load(std::memory_order_seq_cst) == probe_epoch && g_backing_epoch.load(std::memory_order_acquire) == backing_epoch) {
slot = {vaddr, size, clamped, probe_epoch, backing_epoch};
}
if (clamped == size) return size;
if (clamped == 0) {
Shim::Fail("KytyShim memory: attempted to access invalid address 0x%016" PRIx64 " with size 0x%016" PRIx64 "\n", vaddr, size);
Expand Down
27 changes: 27 additions & 0 deletions core/libs/prx/libSceAgcDriver/KytyShim/src/MemoryTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,32 @@ void ProtectOwned(int protection) {
GuestArena::GuestArenaNoteHostProtection_nid_no_patch(g_copyFaults.owned, Page, false);
}

void TestClampRememberedUntilViewChange() {
constexpr std::size_t bytes = Page * 4;
constexpr auto offset = 11u;
void* heap = GuestArena::GuestArenaAllocate_nid_postfix(bytes, Page);
Require(heap != nullptr);
const auto address = Address(heap) + offset;
const auto size = bytes - offset;
const auto expect = [&](std::uint64_t mapped) {
for (int i = 0; i < 4; ++i) Require(Memory::ClampRangeSize(address, size) == mapped);
std::uint64_t other = 0;
std::thread([&] { other = Memory::ClampRangeSize(address, size); }).join();
Require(other == mapped);
};
GuestArena::GuestArenaCommit_nid_postfix(heap, Page, 0x04, Page);
expect(Page - offset);
GuestArena::GuestArenaCommit_nid_postfix(heap, bytes, 0x04, bytes);
expect(size);
GuestArena::GuestArenaReset_nid_postfix(static_cast<std::uint8_t*>(heap) + Page * 2, Page * 2);
expect(Page * 2 - offset);
GuestArena::GuestArenaCommit_nid_postfix(heap, bytes, 0x04, bytes);
expect(size);
GuestArena::GuestArenaReset_nid_postfix(heap, bytes);
GuestArena::GuestArenaRelease_nid_postfix(heap, bytes);
std::printf("clamped range 0x%llx: the remembered mapped length follows every commit and reset, on this thread and another\n", static_cast<unsigned long long>(address));
}

void TestGuestCopyOut() {
void* owned = nullptr;
void* sealed = nullptr;
Expand Down Expand Up @@ -1136,6 +1162,7 @@ int main() {
TestHeapBlockEvicted();
TestHostView();
TestUnreportedPrivateMemory();
TestClampRememberedUntilViewChange();
TestGuestCopyOut();
TestNoCrossWaitWithGpuThread();
TestSparseReadsGuestFile();
Expand Down
Loading