diff options
| author | Logan <Logan@cattn.dev> | 2026-11-23 12:48:20 -0500 |
|---|---|---|
| committer | Logan <Logan@cattn.dev> | 2026-11-23 12:48:20 -0500 |
| commit | af75a43dc9376a3bf5cbd9ab5d7e09936c68cb11 (patch) | |
| tree | a0e1544c3d704d794827753b4bcff64742259fc0 /runtime/shims/xmem.cpp | |
| parent | 75c2b87a751ca1825caee3fae323cce43c1eb78a (diff) | |
xmem: judge a fault on a committed page by the protection at the faulting address
The handler queried the protection at the start of the 64 KiB page, so
a write to a 4 KiB part the title had made read-only looked allowed and
was resumed, faulting again forever instead of reaching the title's own
handler. It now looks the record up by the page and the protection up
by the faulting address. xmem_test writes to a read-only 4 KiB part of
a read-write page and expects the fault.
Diffstat (limited to 'runtime/shims/xmem.cpp')
| -rw-r--r-- | runtime/shims/xmem.cpp | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index 9665a8766..1e286d712 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp | |||
| @@ -487,11 +487,11 @@ bool map_page(uintptr_t address, uint64_t page, DWORD protect) { | |||
| 487 | 487 | ||
| 488 | enum class PageCommit { not_ours, committed_before, committed_now, failed }; | 488 | enum class PageCommit { not_ours, committed_before, committed_now, failed }; |
| 489 | 489 | ||
| 490 | PageCommit commit_mapped_page(uintptr_t address, DWORD& protect) { | 490 | PageCommit commit_mapped_page(uintptr_t address, uintptr_t accessed, DWORD& protect) { |
| 491 | const auto it = g_mapped.find(address); | 491 | const auto it = g_mapped.find(address); |
| 492 | if (it == g_mapped.end() || !it->second.section->scratch) return PageCommit::not_ours; | 492 | if (it == g_mapped.end() || !it->second.section->scratch) return PageCommit::not_ours; |
| 493 | MEMORY_BASIC_INFORMATION info{}; | 493 | MEMORY_BASIC_INFORMATION info{}; |
| 494 | if (!VirtualQuery(reinterpret_cast<void*>(address), &info, sizeof(info)) || info.Type != MEM_MAPPED) { | 494 | if (!VirtualQuery(reinterpret_cast<void*>(accessed), &info, sizeof(info)) || info.Type != MEM_MAPPED) { |
| 495 | return PageCommit::not_ours; | 495 | return PageCommit::not_ours; |
| 496 | } | 496 | } |
| 497 | if (info.State == MEM_COMMIT) { | 497 | if (info.State == MEM_COMMIT) { |
| @@ -530,13 +530,13 @@ LONG CALLBACK physical_page_fault(EXCEPTION_POINTERS* info) { | |||
| 530 | return EXCEPTION_CONTINUE_SEARCH; | 530 | return EXCEPTION_CONTINUE_SEARCH; |
| 531 | } | 531 | } |
| 532 | busy = true; | 532 | busy = true; |
| 533 | const uintptr_t address = | 533 | const uintptr_t accessed = static_cast<uintptr_t>(record->ExceptionInformation[1]); |
| 534 | static_cast<uintptr_t>(record->ExceptionInformation[1]) & ~static_cast<uintptr_t>(kPhysicalPageSize - 1); | 534 | const uintptr_t address = accessed & ~static_cast<uintptr_t>(kPhysicalPageSize - 1); |
| 535 | DWORD protect = 0; | 535 | DWORD protect = 0; |
| 536 | PageCommit result; | 536 | PageCommit result; |
| 537 | { | 537 | { |
| 538 | const FaultLockShared lock; | 538 | const FaultLockShared lock; |
| 539 | result = commit_mapped_page(address, protect); | 539 | result = commit_mapped_page(address, accessed, protect); |
| 540 | } | 540 | } |
| 541 | busy = false; | 541 | busy = false; |
| 542 | if (result == PageCommit::committed_now) return EXCEPTION_CONTINUE_EXECUTION; | 542 | if (result == PageCommit::committed_now) return EXCEPTION_CONTINUE_EXECUTION; |
| @@ -812,7 +812,8 @@ bool commit_physical_range(const void* address, size_t size) { | |||
| 812 | const FaultLockShared lock; | 812 | const FaultLockShared lock; |
| 813 | for (uintptr_t i = 0; i < pages; ++i) { | 813 | for (uintptr_t i = 0; i < pages; ++i) { |
| 814 | DWORD protect = 0; | 814 | DWORD protect = 0; |
| 815 | if (commit_mapped_page(first + i * kPhysicalPageSize, protect) == PageCommit::failed) return false; | 815 | const uintptr_t page = first + i * kPhysicalPageSize; |
| 816 | if (commit_mapped_page(page, page, protect) == PageCommit::failed) return false; | ||
| 816 | } | 817 | } |
| 817 | return true; | 818 | return true; |
| 818 | } | 819 | } |