xmem: a slot-31 copy of a reused page number gives the table page the memory behind the number now (RT-MEMORY-MODEL)

Found by two world runs on the step-2 build (60, 61) that lost the device at the start of the 3D opening with a GPU
read of a garbage address, deterministic, where a run without step 2 (62) went past it.

map_table_page returned early when the table page already held the copy's page number. A number is not a page of
memory: XMemFreePhysicalPages puts the slot on g_free_slots and the next XMemAllocatePhysicalPages takes it back
first, naming a page of a new section. A stream pool's pages are freed and allocated again while the title keeps the
table entries, so the next copy of the same number left the table page on the freed page's frame (its entry holds
that section alive), and the readers of the GPU-only page and its CPU twin got the freed page's old bytes. Before
step 2 the twin was a host view, and map_page maps the number's current page every time, so the readers followed;
the GPU-only page had no readers.

The early return now also requires the frame the entry holds to be the number's current page (section and offset).

Test: xmem_test test_copy_page_mappings_reused_number: copy a page onto a paired GPU-only page, free it, allocate
again (the same number, asserted), map and fill it, copy again: the GPU-only page and its twin read the new page.
It failed before the change ("the table pages read the new page behind the number, not the freed one") and passes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jan Trencansky
2026-11-27 04:07:03 -05:00
parent 92800c06ea
commit b73375f59d
2 changed files with 54 additions and 1 deletions

View File

@ -2155,7 +2155,18 @@ bool map_table_page(uintptr_t address, uint64_t page) {
uint64_t was = 0;
if (const auto table = g_table_pages.find(address); table != g_table_pages.end()) {
const auto mapped = g_mapped.find(address);
if (mapped != g_mapped.end() && mapped->second == page) return true;
// Unchanged only when the frame it holds is the memory behind the number now: a number the title freed comes
// back with its next allocation (g_free_slots, last in first out) naming another page, while the table entry
// still holds the freed one's frame. (g_table_frames is written with g_phys_mutex held, so it is read here
// without g_frame_lock.)
if (mapped != g_mapped.end() && mapped->second == page) {
const auto frame = g_table_frames.find(address);
const auto backing = page ? g_pages.find(page) : g_pages.end();
if (!page || (frame != g_table_frames.end() && backing != g_pages.end() &&
frame->second.section == backing->second.section &&
frame->second.offset == backing->second.offset))
return true;
}
if (mapped != g_mapped.end()) {
was = mapped->second;
g_mapped.erase(mapped);

View File

@ -840,6 +840,47 @@ void test_copy_page_mappings_cpu_view(const PhysicalApi& api) {
check(api.free_pages(kPool, pages) == TRUE, "the pool's pages are freed");
}
// A physical page number the title frees comes back with its next allocation (the free slots are reused last in, first
// out), now naming another page of memory. A slot-31 copy of that number onto a table page that still holds the freed
// page's number must give it the new page: the table's frame is the memory behind the number now, not the one it was
// given before (the readers of a stream pool's pages read through it: a stale frame showed them the freed page's
// bytes).
void test_copy_page_mappings_reused_number(const PhysicalApi& api) {
const auto copy = rt::xmem::copy_page_mappings_fn;
if (!copy) return; // test_copy_page_mappings reports it
auto* const pool = static_cast<uint8_t*>(api.virtual_alloc(
nullptr, kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE | PAGE_GRAPHICS_READWRITE));
auto* const cpu = static_cast<uint8_t*>(api.virtual_alloc(
nullptr, kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE | PAGE_GRAPHICS_READWRITE));
auto* const gpu = static_cast<uint8_t*>(
api.virtual_alloc(nullptr, kPage64k, MEM_RESERVE | kXmem64kPages, 0x3, 0x801));
check(pool && cpu && gpu, "XMemVirtualAlloc reserves a pool and a CPU + GPU-only pair");
if (!pool || !cpu || !gpu) return;
const auto frame_word = [](const uint8_t* at) {
leonida::title_frames::Frame frame;
return leonida::title_frames::find(reinterpret_cast<uintptr_t>(at), frame) ? read_u32(frame.bytes, 0) : 0u;
};
uint64_t first = 0, count = 1;
check(api.allocate(0x280, &count, &first) == TRUE && count == 1 && api.map(pool, 1, &first) == pool,
"a page for the pool");
fill_page(pool, 0x7500u);
check(copy(gpu, pool, 1, false) && frame_word(gpu) == 0x7500u && frame_word(cpu) == 0x7500u,
"the GPU-only page and its twin take the pool's page");
// The title lets the page go and allocates again: the same number, another page of memory.
check(api.map(pool, 1, nullptr) == pool && api.free_pages(1, &first) == TRUE, "the title frees the page");
uint64_t second = 0;
count = 1;
check(api.allocate(0x280, &count, &second) == TRUE && count == 1 && second == first,
"the next allocation reuses the freed page's number");
check(api.map(pool, 1, &second) == pool, "the pool maps the new page");
fill_page(pool, 0x7600u);
check(copy(gpu, pool, 1, false), "a copy of the reused number onto the table pages");
check(frame_word(gpu) == 0x7600u && frame_word(cpu) == 0x7600u,
"the table pages read the new page behind the number, not the freed one");
check(copy(gpu, nullptr, 1, false) && api.map(pool, 1, nullptr) == pool && api.free_pages(1, &second) == TRUE,
"the pages are unmapped and freed");
}
bool access_faults(volatile uint32_t* address, bool write) {
__try {
if (write) {
@ -2783,6 +2824,7 @@ int main(int argc, char** argv) {
}
test_copy_page_mappings(physical);
test_copy_page_mappings_cpu_view(physical);
test_copy_page_mappings_reused_number(physical);
test_stream_cpu_twin(physical);
test_unowned_guards(physical);
test_physical_pages_commit_on_touch(physical);