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.cpp49
1 files changed, 40 insertions, 9 deletions
diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp
index cdc42ff6d..871438f7c 100644
--- a/runtime/test/xmem_test.cpp
+++ b/runtime/test/xmem_test.cpp
@@ -459,7 +459,19 @@ void test_physical_pages(const PhysicalApi& api) {
459 // 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.
460} 460}
461 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
462// Splitting a view briefly unmaps the view's other pages; a thread reading one of them meanwhile must not fault. 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.
463void test_split_under_reader(const PhysicalApi& api) { 475void test_split_under_reader(const PhysicalApi& api) {
464 constexpr uint64_t kPages = 2048; // two capped views 476 constexpr uint64_t kPages = 2048; // two capped views
465 auto* const window = static_cast<uint8_t*>( 477 auto* const window = static_cast<uint8_t*>(
@@ -470,24 +482,30 @@ void test_split_under_reader(const PhysicalApi& api) {
470 api.map(window, kPages, pages.data()) == window, 482 api.map(window, kPages, pages.data()) == window,
471 "a 2,048-page run maps for the split test"); 483 "a 2,048-page run maps for the split test");
472 if (!window || count != kPages) return; 484 if (!window || count != kPages) return;
473 fill_page(window + 11 * kPage64k, 0x11000u); 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);
474 std::atomic<bool> done{false}; 488 std::atomic<bool> done{false};
475 std::atomic<uint64_t> reads{0}, wrong{0}; 489 std::atomic<uint64_t> reads{0}, wrong{0};
476 std::thread reader([&] { 490 std::thread reader([&] {
477 while (!done.load(std::memory_order_relaxed)) { 491 for (uint64_t i = 0; !done.load(std::memory_order_relaxed); ++i) {
478 wrong += read_u32(window + 11 * kPage64k, 0) != 0x11000u; 492 wrong += read_u32(read_pages[i & 1], 0) != 0x11000u;
479 ++reads; 493 ++reads;
480 } 494 }
481 }); 495 });
482 void* const page = window + 10 * kPage64k; 496 bool unmapped = true;
483 bool remapped = true; 497 for (uint64_t k = 0; k < kPages - 1; ++k) {
484 for (int i = 0; i < 2000; ++i) { 498 if (k == 1023) continue;
485 remapped &= api.scatter(&page, 1, nullptr) == TRUE && api.scatter(&page, 1, &pages[10]) == TRUE; 499 void* const page = window + k * kPage64k;
500 unmapped &= api.scatter(&page, 1, nullptr) == TRUE;
486 } 501 }
487 done = true; 502 done = true;
488 reader.join(); 503 reader.join();
489 check(remapped, "a page next to a reader unmaps and maps again 2,000 times"); 504 check(unmapped, "2,046 pages unmap one at a time, each splitting the view a reader is in");
490 check(reads.load() > 0 && wrong.load() == 0, "the reader of the neighbouring page never faults or misreads"); 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");
491 api.map(window, kPages, nullptr); 509 api.map(window, kPages, nullptr);
492 api.free_pages(kPages, pages.data()); 510 api.free_pages(kPages, pages.data());
493} 511}
@@ -531,6 +549,19 @@ void test_long_run(const PhysicalApi& api) {
531 "the whole run unmaps"); 549 "the whole run unmaps");
532 check(api.free_pages(kPages, pages.data()) == TRUE, "the long run's pages are freed"); 550 check(api.free_pages(kPages, pages.data()) == TRUE, "the long run's pages are freed");
533 check(api.map(window, 1, pages.data()) == nullptr && reserved_only(window), "a freed page does not map again"); 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);
534} 565}
535 566
536// 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.