diff options
| author | Logan <Logan@cattn.dev> | 2026-11-23 12:37:42 -0500 |
|---|---|---|
| committer | Logan <Logan@cattn.dev> | 2026-11-23 12:37:42 -0500 |
| commit | 75c2b87a751ca1825caee3fae323cce43c1eb78a (patch) | |
| tree | 193e1fd5d044086c9eb65c4fd20fcaae7f81aeb6 /runtime/test/xmem_test.cpp | |
| parent | abd85df49f26245dbd8be81d750b6425c9f6e379 (diff) | |
xmem: take the fault lock before reading or changing a mapped page's record
unmap_page moved the record out of g_mapped before taking the exclusive
lock, so a first-touch fault on that page could read a moved-from
section, and map_page made the view live before recording it, so a
fault in between was not recognised. unmap_page now holds the exclusive
lock from the lookup to the erase, and map_page holds it across mapping
the view and recording it. xmem_test adds four threads writing into a
window while 256 slices of pages are mapped and unmapped under them.
Reported by tupacfan04 in the review of #7556.
Diffstat (limited to 'runtime/test/xmem_test.cpp')
| -rw-r--r-- | runtime/test/xmem_test.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/runtime/test/xmem_test.cpp b/runtime/test/xmem_test.cpp index a47e43bfd..4ea777007 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 | #include <vector> | 23 | #include <vector> |
| 22 | 24 | ||
| 23 | #include "shims.h" | 25 | #include "shims.h" |
| @@ -595,6 +597,63 @@ void test_commit_physical_pages(const PhysicalApi& api) { | |||
| 595 | check(api.free_pages(kPages, pages) == TRUE, "XMemFreePhysicalPages frees them"); | 597 | check(api.free_pages(kPages, pages) == TRUE, "XMemFreePhysicalPages frees them"); |
| 596 | } | 598 | } |
| 597 | 599 | ||
| 600 | void test_first_touch_during_remap(const PhysicalApi& api) { | ||
| 601 | constexpr uint64_t kSlice = 16; | ||
| 602 | constexpr uint64_t kSlices = 256; | ||
| 603 | std::vector<uint64_t> pages(kSlice * kSlices); | ||
| 604 | uint64_t count = pages.size(); | ||
| 605 | check(api.allocate(0x280, &count, pages.data()) == TRUE && count == pages.size(), | ||
| 606 | "4,096 physical pages for the remap race"); | ||
| 607 | if (count != pages.size()) return; | ||
| 608 | auto* const window = static_cast<uint8_t*>( | ||
| 609 | api.virtual_alloc(nullptr, kSlice * kPage64k, MEM_RESERVE | kXmem64kPages, 0x21, PAGE_READWRITE)); | ||
| 610 | check(window != nullptr, "XMemVirtualAlloc reserves a window for the remap race"); | ||
| 611 | if (!window) { | ||
| 612 | api.free_pages(count, pages.data()); | ||
| 613 | return; | ||
| 614 | } | ||
| 615 | std::atomic<bool> stop{false}; | ||
| 616 | std::atomic<uint64_t> touches{0}; | ||
| 617 | std::atomic<uint64_t> unmapped_faults{0}; | ||
| 618 | std::vector<std::thread> writers; | ||
| 619 | for (int t = 0; t < 4; ++t) { | ||
| 620 | writers.emplace_back([&, t] { | ||
| 621 | uint64_t i = static_cast<uint64_t>(t); | ||
| 622 | while (!stop.load(std::memory_order_relaxed)) { | ||
| 623 | auto* const word = | ||
| 624 | static_cast<volatile uint32_t*>(static_cast<void*>(window + (i % kSlice) * kPage64k)) + t; | ||
| 625 | if (access_faults(word, true)) { | ||
| 626 | unmapped_faults.fetch_add(1, std::memory_order_relaxed); | ||
| 627 | } else { | ||
| 628 | touches.fetch_add(1, std::memory_order_relaxed); | ||
| 629 | } | ||
| 630 | ++i; | ||
| 631 | } | ||
| 632 | }); | ||
| 633 | } | ||
| 634 | const ULONGLONG started = GetTickCount64(); | ||
| 635 | bool remapped = true; | ||
| 636 | for (uint64_t s = 0; s < kSlices; ++s) { | ||
| 637 | remapped &= api.map(window, kSlice, pages.data() + s * kSlice) == window; | ||
| 638 | Sleep(0); | ||
| 639 | remapped &= api.map(window, kSlice, nullptr) == window; | ||
| 640 | } | ||
| 641 | remapped &= api.map(window, kSlice, pages.data() + (kSlices - 1) * kSlice) == window; | ||
| 642 | stop.store(true); | ||
| 643 | for (auto& writer : writers) writer.join(); | ||
| 644 | const ULONGLONG elapsed = GetTickCount64() - started; | ||
| 645 | check(remapped, "256 slices map and unmap while 4 threads write into the window"); | ||
| 646 | check(elapsed < 30000, "first-touch faults during map and unmap finish without hanging"); | ||
| 647 | check(touches.load() > 0, "writes land while the pages are mapped"); | ||
| 648 | std::printf("remap race: %llu writes landed, %llu hit an unmapped page, %llu ms\n", | ||
| 649 | static_cast<unsigned long long>(touches.load()), | ||
| 650 | static_cast<unsigned long long>(unmapped_faults.load()), static_cast<unsigned long long>(elapsed)); | ||
| 651 | static_cast<volatile uint32_t*>(static_cast<void*>(window))[8] = 0x600dcafe; | ||
| 652 | check(read_u32(window, 32) == 0x600dcafe, "the last slice is still mapped and writable"); | ||
| 653 | api.map(window, kSlice, nullptr); | ||
| 654 | check(api.free_pages(count, pages.data()) == TRUE, "XMemFreePhysicalPages frees the remap race pages"); | ||
| 655 | } | ||
| 656 | |||
| 598 | // The placeholder flags pass through: reserve a placeholder, carve windows and committed memory out of it. | 657 | // The placeholder flags pass through: reserve a placeholder, carve windows and committed memory out of it. |
| 599 | void test_placeholders(const PhysicalApi& api) { | 658 | void test_placeholders(const PhysicalApi& api) { |
| 600 | auto* const base = static_cast<uint8_t*>( | 659 | auto* const base = static_cast<uint8_t*>( |
| @@ -875,6 +934,7 @@ int main() { | |||
| 875 | test_physical_pages(physical); | 934 | test_physical_pages(physical); |
| 876 | test_physical_pages_commit_on_touch(physical); | 935 | test_physical_pages_commit_on_touch(physical); |
| 877 | test_commit_physical_pages(physical); | 936 | test_commit_physical_pages(physical); |
| 937 | test_first_touch_during_remap(physical); | ||
| 878 | test_placeholders(physical); | 938 | test_placeholders(physical); |
| 879 | } | 939 | } |
| 880 | 940 | ||