diff options
| author | Gustavo Kissel <kisselgustavo@gmail.com> | 2026-11-23 14:43:57 -0300 |
|---|---|---|
| committer | Gustavo Kissel <kisselgustavo@gmail.com> | 2026-11-23 14:43:57 -0300 |
| commit | 4a7bfd5c4d86eb4cf6e63ff8fcbb508729d8a948 (patch) | |
| tree | 32ecba2f5b564ca18cffadfc467a2b49c05d05af /runtime/shims/xmem.cpp | |
| parent | 5ba2a7f95fbd6db34a2a3efcbb9602f02b20c917 (diff) | |
xmem: map a run as one view per host placeholder it spans
After a partial unmap or remap, a run of consecutive pages can lie across several host placeholders, and
MEM_REPLACE_PLACEHOLDER needs the view to fill one placeholder exactly, so mapping the whole run again failed with
ERROR_INVALID_PARAMETER. map_view now maps one view per placeholder the run spans; a fresh window is still one view.
Found and fixed by dotaku22 in review; xmem_test gains their check (the whole run maps again over a split range).
Diffstat (limited to 'runtime/shims/xmem.cpp')
| -rw-r--r-- | runtime/shims/xmem.cpp | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp index 43089ad91..d326b7d9b 100644 --- a/runtime/shims/xmem.cpp +++ b/runtime/shims/xmem.cpp | |||
| @@ -434,16 +434,32 @@ void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type, | |||
| 434 | return VirtualAlloc(address, size, host_type, host_prot); | 434 | return VirtualAlloc(address, size, host_type, host_prot); |
| 435 | } | 435 | } |
| 436 | 436 | ||
| 437 | // With g_phys_mutex held. Maps `view` at the 64 KiB-aligned `address`, which holds no mapped page. | 437 | // With g_phys_mutex held. Maps `view` at the 64 KiB-aligned `address`, which holds no mapped page. MEM_REPLACE_PLACEHOLDER |
| 438 | // needs the view to fill one placeholder exactly, and earlier partial unmaps can leave several host placeholders in one | ||
| 439 | // run, so the run is mapped as one view per placeholder it spans (one view for a fresh window). | ||
| 438 | bool map_view(uintptr_t address, const View& view) { | 440 | bool map_view(uintptr_t address, const View& view) { |
| 439 | const SIZE_T size = static_cast<SIZE_T>(view.count * kPhysicalPageSize); | 441 | for (uint64_t mapped = 0; mapped < view.count;) { |
| 440 | void* const target = reinterpret_cast<void*>(address); | 442 | const uintptr_t base = address + mapped * kPhysicalPageSize; |
| 441 | if (!isolate_placeholder(target, size)) return false; | 443 | void* const target = reinterpret_cast<void*>(base); |
| 442 | if (!placeholder_api().map_view3(view.section->handle, GetCurrentProcess(), target, view.offset, size, | 444 | MEMORY_BASIC_INFORMATION info{}; |
| 443 | MEM_REPLACE_PLACEHOLDER, view.protect, nullptr, 0)) { | 445 | if (!VirtualQuery(target, &info, sizeof(info)) || info.State != MEM_RESERVE || |
| 444 | return false; | 446 | info.RegionSize < kPhysicalPageSize) { |
| 447 | SetLastError(ERROR_INVALID_ADDRESS); | ||
| 448 | return false; | ||
| 449 | } | ||
| 450 | const uint64_t available = info.RegionSize / kPhysicalPageSize; | ||
| 451 | const uint64_t remaining = view.count - mapped; | ||
| 452 | const uint64_t count = available < remaining ? available : remaining; | ||
| 453 | const SIZE_T size = static_cast<SIZE_T>(count * kPhysicalPageSize); | ||
| 454 | const uint64_t offset = view.offset + mapped * kPhysicalPageSize; | ||
| 455 | if (!isolate_placeholder(target, size)) return false; | ||
| 456 | if (!placeholder_api().map_view3(view.section->handle, GetCurrentProcess(), target, offset, size, | ||
| 457 | MEM_REPLACE_PLACEHOLDER, view.protect, nullptr, 0)) { | ||
| 458 | return false; | ||
| 459 | } | ||
| 460 | g_views.emplace(base, View{count, view.section, offset, view.protect}); | ||
| 461 | mapped += count; | ||
| 445 | } | 462 | } |
| 446 | g_views.emplace(address, view); | ||
| 447 | return true; | 463 | return true; |
| 448 | } | 464 | } |
| 449 | 465 | ||