upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/xmem_test.cpp
diff options
context:
space:
mode:
authorNiiRoZz <simonin.remi25@gmail.com>2026-11-23 17:40:07 +0100
committerGitHub <noreply@github.com>2026-11-23 10:40:07 -0600
commit042c5f37836dc2c8d5913c2fc5a88e99bdee083b (patch)
treebb8d8953a0de3d611e356808773f7a7636ccc2c2 /runtime/test/xmem_test.cpp
parentb2801e26e672f76ffc89d52e2038677bcca9de1f (diff)
xmem: physical pages, default allocators, XMemIsToolsMemAvailable; XMemVirtualAlloc takes 2 MiB pages and placeholders (#6919)
Squash-merged after diff review of PR #6919
Diffstat (limited to 'runtime/test/xmem_test.cpp')
-rw-r--r--runtime/test/xmem_test.cpp261
1 files changed, 255 insertions, 6 deletions
diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp
index edc5fd33c..eed030c12 100644
--- a/runtime/test/xmem_test.cpp
+++ b/runtime/test/xmem_test.cpp
@@ -5,7 +5,9 @@
5// cover what the measured call sites use: 64-byte-aligned allocations, XMemFree, working-set statistics with the 5// cover what the measured call sites use: 64-byte-aligned allocations, XMemFree, working-set statistics with the
6// size at +8, and XMemVirtualQuery's MEMORY_BASIC_INFORMATION prefix inside a zeroed 64-byte output. 6// size at +8, and XMemVirtualQuery's MEMORY_BASIC_INFORMATION prefix inside a zeroed 64-byte output.
7// XMemTransferMemory follows the call shape in Microsoft's public samples: TRUE for any direction and size, with 7// XMemTransferMemory follows the call shape in Microsoft's public samples: TRUE for any direction and size, with
8// nothing committed, and one trace event per distinct (type, size). 8// nothing committed, and one trace event per distinct (type, size). XMemVirtualAlloc accepts attribute words and console
9// graphics protection bits and commits 2 MiB guest pages; the placeholder flags pass through; physical pages keep their
10// contents across unmap and remap, alias when mapped twice, and scatter-map per address.
9#include <windows.h> 11#include <windows.h>
10#include <psapi.h> 12#include <psapi.h>
11 13
@@ -33,7 +35,17 @@ using XMemResetWriteWatchFn = UINT(WINAPI*)(void*, SIZE_T);
33// BOOL XMemTransferMemory(XMEM_TRANSFER_TYPE, SIZE_T): the shape Microsoft's public NetRumble and NativeAOT-Xbox 35// BOOL XMemTransferMemory(XMEM_TRANSFER_TYPE, SIZE_T): the shape Microsoft's public NetRumble and NativeAOT-Xbox
34// samples call (see runtime/shims/xmem.cpp). 36// samples call (see runtime/shims/xmem.cpp).
35using XMemTransferMemoryFn = BOOL(WINAPI*)(uint32_t, SIZE_T); 37using XMemTransferMemoryFn = BOOL(WINAPI*)(uint32_t, SIZE_T);
38// The shapes measured at a title's call sites (see runtime/shims/xmem.cpp).
39using XMemIsToolsMemAvailableFn = BOOL(WINAPI*)();
40using XMemAllocatePhysicalPagesFn = BOOL(WINAPI*)(uint64_t, uint64_t*, uint64_t*);
41using XMemFreePhysicalPagesFn = BOOL(WINAPI*)(uint64_t, const uint64_t*);
42using XMemMapPhysicalPagesFn = void*(WINAPI*)(void*, uint64_t, const uint64_t*);
43using XMemMapPhysicalPagesScatterFn = BOOL(WINAPI*)(void* const*, uint32_t, const uint64_t*);
36constexpr uint32_t kMeasuredKind = 0x20000001; 44constexpr uint32_t kMeasuredKind = 0x20000001;
45constexpr DWORD kXmemLargePages = 0x20000000;
46constexpr DWORD kXmem64kPages = 0x20400000;
47constexpr SIZE_T kPage64k = 0x10000;
48constexpr SIZE_T kPage2m = 0x200000;
37 49
38int g_checks = 0; 50int g_checks = 0;
39int g_failures = 0; 51int g_failures = 0;
@@ -218,11 +230,33 @@ void test_virtual_alloc(XMemVirtualAllocFn virtual_alloc, XMemGetWriteWatchFn ge
218 230
219 check(VirtualFree(base, 0, MEM_RELEASE), "release the synthetic write-watch region"); 231 check(VirtualFree(base, 0, MEM_RELEASE), "release the synthetic write-watch region");
220 232
221 SetLastError(ERROR_SUCCESS); 233 // 2 MiB guest pages: the region is 2 MiB-aligned, rounded up to whole 2 MiB pages and committed.
222 void* const large_page = virtual_alloc(nullptr, page_size, MEM_RESERVE | MEM_COMMIT | 0x20000000, 0, 234 void* const large_page = virtual_alloc(nullptr, kPage2m + page_size, MEM_RESERVE | MEM_COMMIT | kXmemLargePages,
223 PAGE_READWRITE); 235 0x183, PAGE_READWRITE | PAGE_GRAPHICS_READWRITE | PAGE_GRAPHICS_COHERENT);
224 check(large_page == nullptr && GetLastError() == ERROR_NOT_SUPPORTED, 236 check(large_page != nullptr, "XMemVirtualAlloc commits 2 MiB guest pages");
225 "unsupported large-page allocation is rejected before host allocation"); 237 if (large_page) {
238 check((reinterpret_cast<uintptr_t>(large_page) & (kPage2m - 1)) == 0, "a 2 MiB-page region is 2 MiB-aligned");
239 MEMORY_BASIC_INFORMATION info{};
240 VirtualQuery(large_page, &info, sizeof(info));
241 check(info.State == MEM_COMMIT && info.RegionSize >= 2 * kPage2m && info.Protect == PAGE_READWRITE,
242 "a 2 MiB-page region is rounded up to whole pages, committed read-write without the graphics bits");
243 static_cast<volatile uint8_t*>(large_page)[2 * kPage2m - 1] = 0x77;
244 check(VirtualFree(large_page, 0, MEM_RELEASE), "release the 2 MiB-page region");
245 }
246
247 // The attribute word is accepted (every measured call passes a nonzero one) and the protection loses its graphics
248 // bits, in the 64 KiB page mode as well.
249 void* const attributed = virtual_alloc(nullptr, kPage64k, MEM_RESERVE | MEM_COMMIT | kXmem64kPages, 0x181,
250 PAGE_READWRITE | PAGE_GRAPHICS_READWRITE);
251 check(attributed != nullptr, "XMemVirtualAlloc accepts a nonzero attribute word and console protection bits");
252 if (attributed) {
253 MEMORY_BASIC_INFORMATION info{};
254 VirtualQuery(attributed, &info, sizeof(info));
255 check(info.State == MEM_COMMIT && info.Protect == PAGE_READWRITE,
256 "the region is committed PAGE_READWRITE (graphics bits dropped)");
257 static_cast<volatile uint8_t*>(attributed)[0] = 0x42;
258 check(VirtualFree(attributed, 0, MEM_RELEASE), "release the attributed region");
259 }
226 260
227 // The retail executable's GPU pools: the attribute word is recorded, and the PAGE_GRAPHICS_* bits are dropped. 261 // The retail executable's GPU pools: the attribute word is recorded, and the PAGE_GRAPHICS_* bits are dropped.
228 MEMORY_BASIC_INFORMATION mbi{}; 262 MEMORY_BASIC_INFORMATION mbi{};
@@ -249,6 +283,195 @@ void test_virtual_alloc(XMemVirtualAllocFn virtual_alloc, XMemGetWriteWatchFn ge
249 check(static_cast<volatile uint8_t*>(gpu)[0] == 0x5a, "the 0x183 allocation is writable"); 283 check(static_cast<volatile uint8_t*>(gpu)[0] == 0x5a, "the 0x183 allocation is writable");
250 check(VirtualFree(gpu, 0, MEM_RELEASE), "release the 0x183 allocation"); 284 check(VirtualFree(gpu, 0, MEM_RELEASE), "release the 0x183 allocation");
251 } 285 }
286
287 SetLastError(ERROR_SUCCESS);
288 void* const replace_without_address =
289 virtual_alloc(nullptr, kPage64k, MEM_RESERVE | MEM_REPLACE_PLACEHOLDER | kXmem64kPages, 0, PAGE_READWRITE);
290 check(replace_without_address == nullptr && GetLastError() == ERROR_INVALID_PARAMETER,
291 "MEM_REPLACE_PLACEHOLDER without an address is rejected");
292}
293
294// XMemAllocDefault / XMemFreeDefault share XMemAlloc's heap: a block from either is freed by either.
295void test_default_alloc(XMemAllocFn alloc, XMemFreeFn free_mem, XMemAllocFn alloc_default,
296 XMemFreeFn free_default) {
297 void* const a = alloc_default(100, 0x5e50000);
298 check(aligned_64(a), "XMemAllocDefault returns a 64-byte aligned block");
299 void* const zero = alloc_default(0, 0);
300 check(zero != nullptr, "XMemAllocDefault(0) returns non-null");
301 void* const b = alloc(100, 0);
302 if (a) std::memset(a, 0x11, 100);
303 if (b) std::memset(b, 0x22, 100);
304 free_mem(a, 0x5e50000);
305 free_default(b, 0);
306 free_default(zero, 0);
307 check(true, "XMemFree releases an XMemAllocDefault block and XMemFreeDefault an XMemAlloc block");
308}
309
310uint32_t read_u32(const void* base, size_t offset) {
311 uint32_t value = 0;
312 std::memcpy(&value, static_cast<const uint8_t*>(base) + offset, sizeof(value));
313 return value;
314}
315
316void fill_page(void* page, uint32_t tag) {
317 auto* words = static_cast<uint32_t*>(page);
318 for (size_t i = 0; i < kPage64k / sizeof(uint32_t); i += 1024) words[i] = tag + static_cast<uint32_t>(i);
319}
320
321bool page_holds(const void* page, uint32_t tag) {
322 for (size_t i = 0; i < kPage64k / sizeof(uint32_t); i += 1024) {
323 if (read_u32(page, i * sizeof(uint32_t)) != tag + static_cast<uint32_t>(i)) return false;
324 }
325 return true;
326}
327
328bool reserved_only(const void* address) {
329 MEMORY_BASIC_INFORMATION info{};
330 return VirtualQuery(address, &info, sizeof(info)) && info.State == MEM_RESERVE;
331}
332
333struct PhysicalApi {
334 XMemVirtualAllocFn virtual_alloc;
335 XMemAllocatePhysicalPagesFn allocate;
336 XMemFreePhysicalPagesFn free_pages;
337 XMemMapPhysicalPagesFn map;
338 XMemMapPhysicalPagesScatterFn scatter;
339};
340
341// Physical pages in a 64 KiB-page window, as the measured callers use them: reserve a window, allocate pages, map them,
342// remap them elsewhere and in another order, unmap, free.
343void test_physical_pages(const PhysicalApi& api) {
344 constexpr uint64_t kPages = 4;
345 auto* const window = static_cast<uint8_t*>(api.virtual_alloc(
346 nullptr, kPages * kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE | PAGE_GRAPHICS_READWRITE));
347 check(window != nullptr, "XMemVirtualAlloc reserves a 64 KiB-page window");
348 auto* const second = static_cast<uint8_t*>(api.virtual_alloc(
349 nullptr, kPages * kPage64k, MEM_RESERVE | kXmem64kPages, 0x21, PAGE_READWRITE));
350 check(second != nullptr, "XMemVirtualAlloc reserves a second window");
351 if (!window || !second) return;
352 check(reserved_only(window), "the window is reserved, not committed");
353
354 uint64_t pages[kPages]{};
355 uint64_t count = kPages;
356 check(api.allocate(0x280, &count, pages) == TRUE && count == kPages,
357 "XMemAllocatePhysicalPages allocates every page asked for and keeps the count");
358 bool numbers_ok = true;
359 for (uint64_t i = 0; i < kPages; ++i) {
360 numbers_ok &= pages[i] != 0 && pages[i] % 16 == 0 && pages[i] < (uint64_t{1} << 25);
361 for (uint64_t j = 0; j < i; ++j) numbers_ok &= pages[i] != pages[j];
362 }
363 check(numbers_ok, "page numbers are distinct non-zero multiples of 16 below 2^25");
364
365 check(api.map(window, kPages, pages) == window, "XMemMapPhysicalPages returns the address it mapped");
366 for (uint64_t i = 0; i < kPages; ++i) fill_page(window + i * kPage64k, 0x1000u * static_cast<uint32_t>(i + 1));
367 {
368 MEMORY_BASIC_INFORMATION info{};
369 VirtualQuery(window, &info, sizeof(info));
370 check(info.State == MEM_COMMIT && info.Type == MEM_MAPPED && info.Protect == PAGE_READWRITE,
371 "a mapped page is a read-write view with the window's protection");
372 }
373
374 check(api.map(window, kPages, nullptr) == window, "XMemMapPhysicalPages with no page array unmaps");
375 check(reserved_only(window) && reserved_only(window + 3 * kPage64k), "unmapped pages are reserved again");
376
377 // Remap in reverse order into the other window: each page keeps its contents.
378 uint64_t reversed[kPages];
379 for (uint64_t i = 0; i < kPages; ++i) reversed[i] = pages[kPages - 1 - i];
380 check(api.map(second, kPages, reversed) == second, "the pages map into another window");
381 bool kept = true;
382 for (uint64_t i = 0; i < kPages; ++i) {
383 kept &= page_holds(second + i * kPage64k, 0x1000u * static_cast<uint32_t>(kPages - i));
384 }
385 check(kept, "a physical page keeps its contents across unmap and remap");
386
387 // One page mapped at two addresses is one memory.
388 check(api.map(window, 1, &pages[0]) == window, "a page already mapped elsewhere maps again");
389 static_cast<volatile uint32_t*>(static_cast<void*>(window))[1] = 0xfeedf00d;
390 check(read_u32(second + 3 * kPage64k, 4) == 0xfeedf00d, "both mappings of a page see the same memory");
391
392 // Mapping over a mapped page replaces it; a 0 entry unmaps one page.
393 const uint64_t replace[2] = {pages[1], 0};
394 check(api.map(window, 2, replace) == window, "mapping over mapped pages replaces them");
395 check(page_holds(window, 0x2000u) && reserved_only(window + kPage64k),
396 "the first page now shows page 1 and a 0 entry left the second unmapped");
397
398 // Scatter: entry i maps page i at address i, out of order; 0 unmaps.
399 void* const addresses[3] = {window + 3 * kPage64k, window + 2 * kPage64k, window};
400 const uint64_t scattered[3] = {pages[2], pages[3], 0};
401 check(api.scatter(addresses, 3, scattered) == TRUE, "XMemMapPhysicalPagesScatter maps each page at its address");
402 check(page_holds(window + 3 * kPage64k, 0x3000u) && page_holds(window + 2 * kPage64k, 0x4000u) &&
403 reserved_only(window),
404 "scattered pages land at their own addresses and a 0 entry unmaps");
405 check(api.scatter(addresses, 3, nullptr) == TRUE && reserved_only(window + 3 * kPage64k),
406 "XMemMapPhysicalPagesScatter with no page array unmaps");
407
408 uint8_t outside = 0;
409 SetLastError(ERROR_SUCCESS);
410 check(api.map(reinterpret_cast<void*>((reinterpret_cast<uintptr_t>(&outside) + 0xffff) & ~uintptr_t{0xffff}), 1,
411 pages) == nullptr,
412 "mapping outside any window fails");
413 check(api.map(window + 1, 1, pages) == nullptr && GetLastError() == ERROR_INVALID_PARAMETER,
414 "mapping at an address that is not 64 KiB-aligned fails");
415 const uint64_t bogus = 0x1fffff0; // the highest page number, far above the few this test allocates
416 check(api.map(window, 1, &bogus) == nullptr, "mapping a page number that was never allocated fails");
417
418 check(api.map(second, kPages, nullptr) == second, "unmap the second window");
419 check(api.free_pages(kPages, pages) == TRUE, "XMemFreePhysicalPages frees the pages");
420 check(api.free_pages(1, pages) == FALSE, "freeing a page twice fails");
421
422 // Freed numbers are reused.
423 uint64_t again[kPages]{};
424 count = kPages;
425 check(api.allocate(0x180, &count, again) == TRUE && count == kPages, "pages can be allocated again");
426 bool reused = true;
427 for (uint64_t i = 0; i < kPages; ++i) {
428 bool found = false;
429 for (uint64_t j = 0; j < kPages; ++j) found |= again[i] == pages[j];
430 reused &= found;
431 }
432 check(reused, "freed page numbers are reused");
433 check(api.map(window, 1, again) == window && read_u32(window, 0) == 0, "a fresh page maps and reads as zero");
434 api.map(window, 1, nullptr);
435 api.free_pages(kPages, again);
436
437 uint64_t zero_count = 0;
438 check(api.allocate(0x280, &zero_count, nullptr) == TRUE, "allocating zero pages succeeds");
439 check(api.allocate(0x280, nullptr, pages) == FALSE, "a null count fails");
440 // The windows were split into one placeholder per page; the process exit releases them.
441}
442
443// The placeholder flags pass through: reserve a placeholder, carve windows and committed memory out of it.
444void test_placeholders(const PhysicalApi& api) {
445 auto* const base = static_cast<uint8_t*>(
446 api.virtual_alloc(nullptr, 6 * kPage64k, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, 7, PAGE_NOACCESS));
447 check(base != nullptr, "XMemVirtualAlloc reserves a MEM_RESERVE_PLACEHOLDER region");
448 if (!base) return;
449
450 void* const window = api.virtual_alloc(base + kPage64k, 2 * kPage64k,
451 MEM_RESERVE | MEM_REPLACE_PLACEHOLDER | kXmem64kPages, 0x27,
452 PAGE_READWRITE | PAGE_GRAPHICS_READONLY);
453 check(window == base + kPage64k, "a reserve-only MEM_REPLACE_PLACEHOLDER returns the address it replaced");
454
455 auto* const committed = static_cast<uint8_t*>(
456 api.virtual_alloc(base + 4 * kPage64k, kPage64k,
457 MEM_RESERVE | MEM_COMMIT | MEM_REPLACE_PLACEHOLDER | kXmem64kPages, 0x27, PAGE_READWRITE));
458 check(committed == base + 4 * kPage64k, "a committing MEM_REPLACE_PLACEHOLDER returns the address it replaced");
459 if (committed) {
460 committed[0] = 0x5a;
461 check(committed[0] == 0x5a, "the committed replacement is writable");
462 }
463
464 uint64_t pages[2]{};
465 uint64_t count = 2;
466 check(api.allocate(0x380, &count, pages) == TRUE && count == 2, "allocate two pages for the carved window");
467 check(api.map(window, 2, pages) == window, "pages map into a window carved out of a placeholder");
468 if (window) {
469 fill_page(static_cast<uint8_t*>(window) + kPage64k, 0xabc0000u);
470 check(page_holds(static_cast<uint8_t*>(window) + kPage64k, 0xabc0000u), "the carved window holds data");
471 }
472 check(api.map(window, 2, nullptr) == window, "unmap the carved window");
473 check(api.free_pages(2, pages) == TRUE, "free the carved window's pages");
474 check(reserved_only(base), "the rest of the placeholder is still reserved");
252} 475}
253 476
254// gta6rt --trace: one "xmem_attributes" event per distinct raw attribute word and API, none with the trace off. 477// gta6rt --trace: one "xmem_attributes" event per distinct raw attribute word and API, none with the trace off.
@@ -453,6 +676,25 @@ int main() {
453 check(reset_write_watch != nullptr, "registry exposes xmem.dll!XMemResetWriteWatch"); 676 check(reset_write_watch != nullptr, "registry exposes xmem.dll!XMemResetWriteWatch");
454 check(transfer != nullptr, "registry exposes xmem.dll!XMemTransferMemory"); 677 check(transfer != nullptr, "registry exposes xmem.dll!XMemTransferMemory");
455 678
679 const auto alloc_default = reinterpret_cast<XMemAllocFn>(rt::shims::find("xmem.dll", "XMemAllocDefault"));
680 const auto free_default = reinterpret_cast<XMemFreeFn>(rt::shims::find("xmem.dll", "XMemFreeDefault"));
681 const auto tools_mem =
682 reinterpret_cast<XMemIsToolsMemAvailableFn>(rt::shims::find("xmem.dll", "XMemIsToolsMemAvailable"));
683 const PhysicalApi physical{
684 virtual_alloc,
685 reinterpret_cast<XMemAllocatePhysicalPagesFn>(rt::shims::find("xmem.dll", "XMemAllocatePhysicalPages")),
686 reinterpret_cast<XMemFreePhysicalPagesFn>(rt::shims::find("xmem.dll", "XMemFreePhysicalPages")),
687 reinterpret_cast<XMemMapPhysicalPagesFn>(rt::shims::find("xmem.dll", "XMemMapPhysicalPages")),
688 reinterpret_cast<XMemMapPhysicalPagesScatterFn>(rt::shims::find("xmem.dll", "XMemMapPhysicalPagesScatter")),
689 };
690 check(alloc_default != nullptr, "registry exposes xmem.dll!XMemAllocDefault");
691 check(free_default != nullptr, "registry exposes xmem.dll!XMemFreeDefault");
692 check(tools_mem != nullptr, "registry exposes xmem.dll!XMemIsToolsMemAvailable");
693 check(physical.allocate != nullptr, "registry exposes xmem.dll!XMemAllocatePhysicalPages");
694 check(physical.free_pages != nullptr, "registry exposes xmem.dll!XMemFreePhysicalPages");
695 check(physical.map != nullptr, "registry exposes xmem.dll!XMemMapPhysicalPages");
696 check(physical.scatter != nullptr, "registry exposes xmem.dll!XMemMapPhysicalPagesScatter");
697
456 if (!alloc || !free_mem || !query || !get_stats) { 698 if (!alloc || !free_mem || !query || !get_stats) {
457 print_result(); 699 print_result();
458 return 1; 700 return 1;
@@ -472,6 +714,13 @@ int main() {
472 714
473 test_title_virtual_alloc(); 715 test_title_virtual_alloc();
474 716
717 if (alloc_default && free_default) test_default_alloc(alloc, free_mem, alloc_default, free_default);
718 if (tools_mem) check(tools_mem() == FALSE, "XMemIsToolsMemAvailable returns FALSE (no devkit tools memory)");
719 if (virtual_alloc && physical.allocate && physical.free_pages && physical.map && physical.scatter) {
720 test_physical_pages(physical);
721 test_placeholders(physical);
722 }
723
475 print_result(); 724 print_result();
476 return g_failures ? 1 : 0; 725 return g_failures ? 1 : 0;
477} 726}