upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/xmem.cpp
diff options
context:
space:
mode:
authorLogan <Logan@cattn.dev>2026-11-23 12:37:42 -0500
committerLogan <Logan@cattn.dev>2026-11-23 12:37:42 -0500
commit75c2b87a751ca1825caee3fae323cce43c1eb78a (patch)
tree193e1fd5d044086c9eb65c4fd20fcaae7f81aeb6 /runtime/shims/xmem.cpp
parentabd85df49f26245dbd8be81d750b6425c9f6e379 (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/shims/xmem.cpp')
-rw-r--r--runtime/shims/xmem.cpp11
1 files changed, 3 insertions, 8 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp
index e97f60f1e..9665a8766 100644
--- a/runtime/shims/xmem.cpp
+++ b/runtime/shims/xmem.cpp
@@ -453,19 +453,14 @@ void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type,
453 453
454// With g_phys_mutex held. Unmaps the page at `address` if one is mapped there; the address becomes a placeholder again. 454// With g_phys_mutex held. Unmaps the page at `address` if one is mapped there; the address becomes a placeholder again.
455bool unmap_page(uintptr_t address) { 455bool unmap_page(uintptr_t address) {
456 const FaultLockExclusive lock;
456 const auto it = g_mapped.find(address); 457 const auto it = g_mapped.find(address);
457 if (it == g_mapped.end()) return true; 458 if (it == g_mapped.end()) return true;
458 MappedPage mapped = std::move(it->second);
459 {
460 const FaultLockExclusive lock;
461 g_mapped.erase(it);
462 }
463 if (!placeholder_api().unmap_view2(GetCurrentProcess(), reinterpret_cast<void*>(address), 459 if (!placeholder_api().unmap_view2(GetCurrentProcess(), reinterpret_cast<void*>(address),
464 MEM_PRESERVE_PLACEHOLDER)) { 460 MEM_PRESERVE_PLACEHOLDER)) {
465 const FaultLockExclusive lock;
466 g_mapped.emplace(address, std::move(mapped));
467 return false; 461 return false;
468 } 462 }
463 g_mapped.erase(it);
469 return true; 464 return true;
470} 465}
471 466
@@ -481,11 +476,11 @@ bool map_page(uintptr_t address, uint64_t page, DWORD protect) {
481 } 476 }
482 void* const target = reinterpret_cast<void*>(address); 477 void* const target = reinterpret_cast<void*>(address);
483 if (!isolate_placeholder(target, kPhysicalPageSize)) return false; 478 if (!isolate_placeholder(target, kPhysicalPageSize)) return false;
479 const FaultLockExclusive lock;
484 void* const view = placeholder_api().map_view3(it->second.section->handle, GetCurrentProcess(), target, 480 void* const view = placeholder_api().map_view3(it->second.section->handle, GetCurrentProcess(), target,
485 it->second.offset, kPhysicalPageSize, MEM_REPLACE_PLACEHOLDER, 481 it->second.offset, kPhysicalPageSize, MEM_REPLACE_PLACEHOLDER,
486 protect, nullptr, 0); 482 protect, nullptr, 0);
487 if (!view) return false; 483 if (!view) return false;
488 const FaultLockExclusive lock;
489 g_mapped[address] = MappedPage{page, it->second.section, it->second.offset}; 484 g_mapped[address] = MappedPage{page, it->second.section, it->second.offset};
490 return true; 485 return true;
491} 486}