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:
authorGustavo Kissel <kisselgustavo@gmail.com>2026-11-23 18:11:14 -0300
committerGustavo Kissel <kisselgustavo@gmail.com>2026-11-23 18:11:14 -0300
commitcef56a296ece813ecbac9d430fe90f64a21f2aab (patch)
treeaf2abb184d1086ccbb8a6edf5814ecf47171c0e4 /runtime/shims/xmem.cpp
parentcf69f16b64c26109cd4b053abad5b2d076ff0537 (diff)
fix: publish each split span as one word so the handler never mixes two spans
Diffstat (limited to 'runtime/shims/xmem.cpp')
-rw-r--r--runtime/shims/xmem.cpp34
1 files changed, 17 insertions, 17 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp
index 153aeabac..9f8aec074 100644
--- a/runtime/shims/xmem.cpp
+++ b/runtime/shims/xmem.cpp
@@ -279,11 +279,18 @@ std::map<uintptr_t, View> g_views; // view base -> view
279// retries the access. The last finished split stays published too, for a fault the handler sees only after the split 279// retries the access. The last finished split stays published too, for a fault the handler sees only after the split
280// is done; any other access violation goes on to the next handler. 280// is done; any other access violation goes on to the next handler.
281// Only the last finished span is kept, so a fault delivered after two later splits is not retried. 281// Only the last finished span is kept, so a fault delivered after two later splits is not retried.
282std::atomic<uintptr_t> g_split_begin{0}; 282// A span is one word, so a reader never pairs one split's start with another's end: views are 64 KiB aligned and at
283std::atomic<uintptr_t> g_split_end{0}; 283// most kMaxViewPages long, so the base keeps its low 16 bits for the page count. 0 is no span.
284std::atomic<uintptr_t> g_last_split_begin{0}; 284std::atomic<uint64_t> g_split{0};
285std::atomic<uintptr_t> g_last_split_end{0}; 285std::atomic<uint64_t> g_last_split{0};
286std::atomic<uint64_t> g_splits{0}; // splits started 286std::atomic<uint64_t> g_splits{0}; // splits started
287
288uint64_t pack_span(uintptr_t begin, uintptr_t end) { return begin | (end - begin) / kPhysicalPageSize; }
289
290bool span_holds(uint64_t span, uintptr_t address) {
291 const uintptr_t begin = span & ~uint64_t{0xffff};
292 return span && address >= begin && address < begin + (span & 0xffff) * kPhysicalPageSize;
293}
287bool g_split_handler = false; // with g_phys_mutex held 294bool g_split_handler = false; // with g_phys_mutex held
288 295
289LONG CALLBACK wait_for_split(EXCEPTION_POINTERS* info) { 296LONG CALLBACK wait_for_split(EXCEPTION_POINTERS* info) {
@@ -297,16 +304,11 @@ LONG CALLBACK wait_for_split(EXCEPTION_POINTERS* info) {
297 for (;;) { 304 for (;;) {
298 const uint64_t splits = g_splits.load(std::memory_order_acquire); 305 const uint64_t splits = g_splits.load(std::memory_order_acquire);
299 bool in_split = false; 306 bool in_split = false;
300 for (;;) { 307 while (span_holds(g_split.load(std::memory_order_acquire), address)) {
301 const uintptr_t begin = g_split_begin.load(std::memory_order_acquire);
302 if (!begin || address < begin || address >= g_split_end.load(std::memory_order_acquire)) break;
303 in_split = true; 308 in_split = true;
304 SwitchToThread(); 309 SwitchToThread();
305 } 310 }
306 if (!in_split) { 311 in_split = in_split || span_holds(g_last_split.load(std::memory_order_acquire), address);
307 const uintptr_t last = g_last_split_begin.load(std::memory_order_acquire);
308 in_split = last && address >= last && address < g_last_split_end.load(std::memory_order_acquire);
309 }
310 if (!in_split) return EXCEPTION_CONTINUE_SEARCH; 312 if (!in_split) return EXCEPTION_CONTINUE_SEARCH;
311 // Retry only an access the page now allows: the fault may have come in during a split that has since finished. 313 // Retry only an access the page now allows: the fault may have come in during a split that has since finished.
312 MEMORY_BASIC_INFORMATION mbi{}; 314 MEMORY_BASIC_INFORMATION mbi{};
@@ -326,15 +328,12 @@ LONG CALLBACK wait_for_split(EXCEPTION_POINTERS* info) {
326struct SplitGuard { 328struct SplitGuard {
327 SplitGuard(uintptr_t begin, uintptr_t end) { 329 SplitGuard(uintptr_t begin, uintptr_t end) {
328 if (!g_split_handler) g_split_handler = AddVectoredExceptionHandler(1, &wait_for_split) != nullptr; 330 if (!g_split_handler) g_split_handler = AddVectoredExceptionHandler(1, &wait_for_split) != nullptr;
329 g_split_end.store(end, std::memory_order_release); 331 g_split.store(pack_span(begin, end), std::memory_order_release);
330 g_split_begin.store(begin, std::memory_order_release);
331 g_splits.fetch_add(1, std::memory_order_release); 332 g_splits.fetch_add(1, std::memory_order_release);
332 } 333 }
333 ~SplitGuard() { 334 ~SplitGuard() {
334 g_last_split_begin.store(0, std::memory_order_release); 335 g_last_split.store(g_split.load(std::memory_order_relaxed), std::memory_order_release);
335 g_last_split_end.store(g_split_end.load(std::memory_order_relaxed), std::memory_order_release); 336 g_split.store(0, std::memory_order_release);
336 g_last_split_begin.store(g_split_begin.load(std::memory_order_relaxed), std::memory_order_release);
337 g_split_begin.store(0, std::memory_order_release);
338 } 337 }
339 SplitGuard(const SplitGuard&) = delete; 338 SplitGuard(const SplitGuard&) = delete;
340 SplitGuard& operator=(const SplitGuard&) = delete; 339 SplitGuard& operator=(const SplitGuard&) = delete;
@@ -507,6 +506,7 @@ void* WINAPI XMemVirtualAlloc(void* address, SIZE_T size, DWORD allocation_type,
507// run, so the run is mapped as one view per placeholder it spans. Views are capped at kMaxViewPages so that unmapping a 506// run, so the run is mapped as one view per placeholder it spans. Views are capped at kMaxViewPages so that unmapping a
508// page inside a long run only remaps the rest of one capped view, not the whole run. 507// page inside a long run only remaps the rest of one capped view, not the whole run.
509constexpr uint64_t kMaxViewPages = 1024; // 64 MiB 508constexpr uint64_t kMaxViewPages = 1024; // 64 MiB
509static_assert(kMaxViewPages < 0x10000, "a split span keeps its page count in 16 bits");
510 510
511bool map_view(uintptr_t address, const View& view) { 511bool map_view(uintptr_t address, const View& view) {
512 for (uint64_t mapped = 0; mapped < view.count;) { 512 for (uint64_t mapped = 0; mapped < view.count;) {