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:
Diffstat (limited to 'runtime/test/xmem_test.cpp')
-rw-r--r--runtime/test/xmem_test.cpp126
1 files changed, 126 insertions, 0 deletions
diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp
index 53df7fcc5..87359ca67 100644
--- a/runtime/test/xmem_test.cpp
+++ b/runtime/test/xmem_test.cpp
@@ -11,6 +11,7 @@
11#include <windows.h> 11#include <windows.h>
12#include <psapi.h> 12#include <psapi.h>
13 13
14#include <atomic>
14#include <cstdint> 15#include <cstdint>
15#include <cstdio> 16#include <cstdio>
16#include <cstring> 17#include <cstring>
@@ -18,6 +19,7 @@
18#include <limits> 19#include <limits>
19#include <sstream> 20#include <sstream>
20#include <string> 21#include <string>
22#include <thread>
21 23
22#include "shims.h" 24#include "shims.h"
23#include "startup_trace.h" 25#include "startup_trace.h"
@@ -369,10 +371,27 @@ void test_physical_pages(const PhysicalApi& api) {
369 VirtualQuery(window, &info, sizeof(info)); 371 VirtualQuery(window, &info, sizeof(info));
370 check(info.State == MEM_COMMIT && info.Type == MEM_MAPPED && info.Protect == PAGE_READWRITE, 372 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"); 373 "a mapped page is a read-write view with the window's protection");
374 check(info.AllocationBase == window && info.RegionSize == kPages * kPage64k,
375 "consecutive pages of one allocation are mapped as one view");
372 } 376 }
373 377
378 // A page unmapped or remapped inside that view splits it; its neighbours keep their pages.
379 void* const middle = window + kPage64k;
380 check(api.scatter(&middle, 1, nullptr) == TRUE && reserved_only(window + kPage64k),
381 "a page inside a view unmaps on its own");
382 check(page_holds(window, 0x1000u) && page_holds(window + 2 * kPage64k, 0x3000u) &&
383 page_holds(window + 3 * kPage64k, 0x4000u),
384 "the pages around it stay mapped with their contents");
385 check(api.map(middle, 1, &pages[1]) == middle && page_holds(window + kPage64k, 0x2000u),
386 "a page maps again into the gap");
387
374 check(api.map(window, kPages, nullptr) == window, "XMemMapPhysicalPages with no page array unmaps"); 388 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"); 389 check(reserved_only(window) && reserved_only(window + 3 * kPage64k), "unmapped pages are reserved again");
390 // The split above left the range as several host placeholders; the whole run maps again over them.
391 check(api.map(window, kPages, pages) == window && page_holds(window, 0x1000u) &&
392 page_holds(window + 3 * kPage64k, 0x4000u),
393 "a run maps again over a range an earlier partial unmap split");
394 check(api.map(window, kPages, nullptr) == window && reserved_only(window), "and unmaps again");
376 395
377 // Remap in reverse order into the other window: each page keeps its contents. 396 // Remap in reverse order into the other window: each page keeps its contents.
378 uint64_t reversed[kPages]; 397 uint64_t reversed[kPages];
@@ -440,6 +459,111 @@ void test_physical_pages(const PhysicalApi& api) {
440 // The windows were split into one placeholder per page; the process exit releases them. 459 // The windows were split into one placeholder per page; the process exit releases them.
441} 460}
442 461
462// True when a read fault raised for `address` reaches this handler rather than being retried by the split handler.
463bool read_fault_reaches_caller(const void* address) {
464 const ULONG_PTR info[2] = {0, reinterpret_cast<ULONG_PTR>(address)};
465 __try {
466 RaiseException(EXCEPTION_ACCESS_VIOLATION, 0, 2, info);
467 } __except (EXCEPTION_EXECUTE_HANDLER) {
468 return true;
469 }
470 return false;
471}
472
473// Splitting a view briefly unmaps the view's other pages; a thread reading one of them meanwhile must not fault.
474// Unmapping page k of the view [k, end) splits it every time, and the last page of each view is the one being read.
475void test_split_under_reader(const PhysicalApi& api) {
476 constexpr uint64_t kPages = 2048; // two capped views
477 auto* const window = static_cast<uint8_t*>(
478 api.virtual_alloc(nullptr, kPages * kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE));
479 std::vector<uint64_t> pages(kPages);
480 uint64_t count = kPages;
481 check(window && api.allocate(0x280, &count, pages.data()) == TRUE && count == kPages &&
482 api.map(window, kPages, pages.data()) == window,
483 "a 2,048-page run maps for the split test");
484 if (!window || count != kPages) return;
485 uint8_t* const read_pages[2] = {window + 1023 * kPage64k, window + 2047 * kPage64k};
486 fill_page(read_pages[0], 0x11000u);
487 fill_page(read_pages[1], 0x11000u);
488 std::atomic<bool> done{false};
489 std::atomic<uint64_t> reads{0}, wrong{0};
490 std::thread reader([&] {
491 for (uint64_t i = 0; !done.load(std::memory_order_relaxed); ++i) {
492 wrong += read_u32(read_pages[i & 1], 0) != 0x11000u;
493 ++reads;
494 }
495 });
496 bool unmapped = true;
497 for (uint64_t k = 0; k < kPages - 1; ++k) {
498 if (k == 1023) continue;
499 void* const page = window + k * kPage64k;
500 unmapped &= api.scatter(&page, 1, nullptr) == TRUE;
501 }
502 done = true;
503 reader.join();
504 check(unmapped, "2,046 pages unmap one at a time, each splitting the view a reader is in");
505 check(reads.load() > 0 && wrong.load() == 0, "the reader of the views' last pages never faults or misreads");
506 // The last split was of [2046, 2048): a fault there is retried, one on another mapped page is not.
507 check(!read_fault_reaches_caller(read_pages[1]), "a read fault inside the last split span is retried");
508 check(read_fault_reaches_caller(read_pages[0]), "a read fault on a mapped page outside it reaches the caller");
509 api.map(window, kPages, nullptr);
510 api.free_pages(kPages, pages.data());
511}
512
513// A run longer than one host view is mapped as capped views, so unmapping one page inside it only remaps the rest of
514// that view; every other page keeps its contents.
515void test_long_run(const PhysicalApi& api) {
516 constexpr uint64_t kPages = 1100; // more than the 1024-page view cap
517 auto* const window = static_cast<uint8_t*>(
518 api.virtual_alloc(nullptr, kPages * kPage64k, MEM_RESERVE | kXmem64kPages, 0x23, PAGE_READWRITE));
519 std::vector<uint64_t> pages(kPages);
520 uint64_t count = kPages;
521 check(window && api.allocate(0x280, &count, pages.data()) == TRUE, "a long run's window and pages are allocated");
522 if (!window || count != kPages) return;
523 check(api.map(window, kPages, pages.data()) == window, "a run longer than the view cap maps");
524 MEMORY_BASIC_INFORMATION info{};
525 VirtualQuery(window, &info, sizeof(info));
526 check(info.State == MEM_COMMIT && info.RegionSize == 1024 * kPage64k, "it is mapped as views of at most 1024 pages");
527 for (uint64_t i = 0; i < kPages; ++i) fill_page(window + i * kPage64k, static_cast<uint32_t>(i + 1));
528 void* const hole = window + 10 * kPage64k;
529 check(api.scatter(&hole, 1, nullptr) == TRUE && reserved_only(hole), "a page inside the first view unmaps");
530 bool kept = true;
531 for (uint64_t i = 0; i < kPages; ++i) {
532 if (i != 10) kept &= page_holds(window + i * kPage64k, static_cast<uint32_t>(i + 1));
533 }
534 check(kept, "every other page of the run keeps its contents");
535 // Several pages across the boundary of two capped views unmap together; the pages on both sides stay.
536 check(api.map(window + 1000 * kPage64k, 50, nullptr) == window + 1000 * kPage64k &&
537 reserved_only(window + 1000 * kPage64k) && reserved_only(window + 1049 * kPage64k),
538 "a multi-page range across two views unmaps");
539 check(page_holds(window + 999 * kPage64k, 1000u) && page_holds(window + 1050 * kPage64k, 1051u),
540 "the pages on both sides of it keep their contents");
541 // Mapping other pages over the middle of a live run replaces just those pages.
542 check(api.map(window + 500 * kPage64k, 3, &pages[600]) == window + 500 * kPage64k &&
543 page_holds(window + 500 * kPage64k, 601u) && page_holds(window + 502 * kPage64k, 603u),
544 "pages mapped over the middle of a live run replace it there");
545 check(page_holds(window + 499 * kPage64k, 500u) && page_holds(window + 503 * kPage64k, 504u),
546 "and the run's pages around them keep their contents");
547 check(api.map(window, kPages, nullptr) == window && reserved_only(window) &&
548 reserved_only(window + (kPages - 1) * kPage64k),
549 "the whole run unmaps");
550 check(api.free_pages(kPages, pages.data()) == TRUE, "the long run's pages are freed");
551 check(api.map(window, 1, pages.data()) == nullptr && reserved_only(window), "a freed page does not map again");
552 // Pages freed while still mapped keep their memory, and splitting their view later still maps the rest again.
553 count = 4;
554 check(api.allocate(0x280, &count, pages.data()) == TRUE && count == 4 && api.map(window, 4, pages.data()) == window,
555 "four pages map as one view");
556 if (count != 4) return;
557 for (uint64_t i = 0; i < 4; ++i) fill_page(window + i * kPage64k, static_cast<uint32_t>(i + 1));
558 check(api.free_pages(4, pages.data()) == TRUE, "they are freed while mapped");
559 void* const second = window + kPage64k;
560 check(api.scatter(&second, 1, nullptr) == TRUE && !reserved_only(window) &&
561 !reserved_only(window + 2 * kPage64k) && page_holds(window, 1u) &&
562 page_holds(window + 2 * kPage64k, 3u) && page_holds(window + 3 * kPage64k, 4u),
563 "unmapping one of them afterwards keeps the others' memory");
564 api.map(window, 4, nullptr);
565}
566
443// The placeholder flags pass through: reserve a placeholder, carve windows and committed memory out of it. 567// The placeholder flags pass through: reserve a placeholder, carve windows and committed memory out of it.
444void test_placeholders(const PhysicalApi& api) { 568void test_placeholders(const PhysicalApi& api) {
445 auto* const base = static_cast<uint8_t*>( 569 auto* const base = static_cast<uint8_t*>(
@@ -746,6 +870,8 @@ int main() {
746 if (tools_mem) check(tools_mem() == FALSE, "XMemIsToolsMemAvailable returns FALSE (no devkit tools memory)"); 870 if (tools_mem) check(tools_mem() == FALSE, "XMemIsToolsMemAvailable returns FALSE (no devkit tools memory)");
747 if (virtual_alloc && physical.allocate && physical.free_pages && physical.map && physical.scatter) { 871 if (virtual_alloc && physical.allocate && physical.free_pages && physical.map && physical.scatter) {
748 test_physical_pages(physical); 872 test_physical_pages(physical);
873 test_long_run(physical);
874 test_split_under_reader(physical);
749 test_placeholders(physical); 875 test_placeholders(physical);
750 test_write_combine_window(physical); 876 test_write_combine_window(physical);
751 } 877 }