[PR #1875] runtime: fix title_page_watch mark clipping, split-tail ask, UINTPTR_MAX bounds, and ReadFileScatter pins (RT-TITLE-PAGE-WATCH-RANGE-AND-PINS) #1887

Open
opened 2026-11-26 21:19:18 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1875
Author: @ambroseautomation
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/rt-title-page-watch-range-and-pins


📝 Commits (1)

  • e9ca38b RT-TITLE-PAGE-WATCH-RANGE-AND-PINS: fix mark clipping, split-tail ask, UINTPTR_MAX bounds, and ReadFileScatter pins

📊 Changes

3 files changed (+214 additions, -27 deletions)

View changed files

📝 runtime/src/title_page_watch.h (+77 -27)
📝 runtime/test/kernel_writes_test.cpp (+63 -0)
📝 runtime/test/title_write_watch_test.cpp (+74 -0)

📄 Description

Coordination item: RT-TITLE-PAGE-WATCH-RANGE-AND-PINS (claimed in https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164267570).

Problem

Five range-clipping, boundary, and pin-lifetime bugs in runtime/src/title_page_watch.h:

  1. title_write_watch::mark(from, to) dropped marks or over-marked past reservation boundaries (title_page_watch.h:579-591):
    • Checked only !contains(from): a range starting before a write-watched reservation and overlapping its interior was silently dropped.
    • A range starting inside a write-watched reservation and extending past its end into unwatched memory wrote unwatched pages into s.written—and if (to - from) / kPage > kKeep, raised s.floor = now, forcing every unrelated shadow in the process to re-copy all of its memory.
    • Rounding (to + kPage - 1) & ~(kPage - 1) wrapped to 0 near UINTPTR_MAX.
  2. title_write_watch::ask() checked only born(from) (title_page_watch.h:660):
    • If a run [from, to) spanned into a newly added/replaced write-watched reservation whose tail was added after the reader's last asking while the reservation at from predated it, born(from) returned the old born <= since and ask() missed the newly zeroed pages in the tail.
  3. Unsigned wraparound / infinite loop near UINTPTR_MAX (title_page_watch.h:213, 518, 773, 811, 833, 875):
    • title_page_aliases::for_each_in() looped va += kPageSize without stopping before wraparound when end > UINTPTR_MAX - (kPageSize - 1).
    • title_write_watch::add() could wrap end below base when base + size exceeded UINTPTR_MAX, breaking map order invariants.
    • kernel_writes::begin_write(), hold(), reopen(), and write() computed begin + bytes without saturation (wrapping end below begin near UINTPTR_MAX).
  4. kernel_writes::read_file_scatter() pin leak on synchronous completion & DWORD page-count overflow (title_page_watch.h:927, 936):
    • Passed bool(ok) || error == ERROR_IO_PENDING to end_write() instead of !ok && error == ERROR_IO_PENDING (as read_file() already does): if ReadFileScatter completes synchronously (ok == TRUE), pins were kept with returned = true instead of released immediately, so reusing the OVERLAPPED for another pending request kept the completed scatter buffers pinned.
    • (bytes + info.dwPageSize - 1) / info.dwPageSize used 32-bit DWORD addition, overflowing for large bytes.
  5. Missing .returned initializer in kernel_writes::hold() (title_page_watch.h:811):
    • Triggered -Wmissing-field-initializers under clang-cl /W4.

Changes

  • runtime/src/title_page_watch.h:
    • Updated title_write_watch::mark(from, to) to round with overflow saturation and clip directly against overlapping reservations in s.regions under s.journal -> s.mutex lock order (matching ask()'s lock order, zero process-heap allocations), raising s.floor only when total overlapping watched pages exceed kKeep.
    • Added title_write_watch::born_covering(from, to) and used it in ask() so a run is only treated as covered by an old reservation when a single reservation covers the entire [from, to).
    • Made title_write_watch::add(), title_page_aliases::for_each_in(), and kernel_writes::{begin_write, hold, reopen, write} overflow-safe near UINTPTR_MAX.
    • Fixed kernel_writes::read_file_scatter() to use 64-bit page count arithmetic and release pins on synchronous completion (!ok && error == ERROR_IO_PENDING), and explicitly initialized .returned = false in kernel_writes::hold().
  • runtime/test/title_write_watch_test.cpp & runtime/test/kernel_writes_test.cpp:
    • Added regression tests for mark() leading-margin clipping, trailing-margin clipping without spurious s.floor bumps, multi-reservation spans, ask() detecting split/new-tail reservations, UINTPTR_MAX saturation/termination in add()/mark()/title_page_aliases::for_each_in()/hold(), and kernel_writes::read_file_scatter().

Verification

  • Before fix (new tests on unpatched main 5e95fa2f4):
    • title_write_watch_test: FAILED (3 failures: leading-margin mark(), trailing-margin mark(), and split-tail ask())
    • kernel_writes_test: FAILED (1 failure: hold() wrapped end near UINTPTR_MAX)
  • After fix (rebased onto main 77baf9062, MSVC 14.51.36231 /W4 /WX /permissive- /std:c++20 /O2 /MD and LLVM clang-cl /W4 /WX):
    • title_write_watch_test.exe — OK (26 checks, 0 failures)
    • kernel_writes_test.exe — 34 checks, 0 failures
    • title_memory_map_test.exe — 14 checks, 0 failures
    • xmem_test.exe — OK (480 checks)
    • clang-cl /W4 /WX on title_write_watch_test.cpp and kernel_writes_test.cpp — exit 0, 0 warnings
    • python -I scripts/check_no_game_data.py runtime/src/title_page_watch.h runtime/test/title_write_watch_test.cpp runtime/test/kernel_writes_test.cpp — clean (exit 0)

Hardware / Environment

  • GPU / driver: n/a (CPU/Win32 memory-watch runtime tests; no GPU or Vulkan SDK required)
  • Host: Windows 11 Pro 64-bit, MSVC 14.51.36231 / LLVM clang-cl

AI-assisted: yes


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1875 **Author:** [@ambroseautomation](https://github.com/ambroseautomation) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rt-title-page-watch-range-and-pins` --- ### 📝 Commits (1) - [`e9ca38b`](https://github.com/ubiquitarianism/leonida/commit/e9ca38b1012514226b4c285377f0e03a5eed9b8c) RT-TITLE-PAGE-WATCH-RANGE-AND-PINS: fix mark clipping, split-tail ask, UINTPTR_MAX bounds, and ReadFileScatter pins ### 📊 Changes **3 files changed** (+214 additions, -27 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/title_page_watch.h` (+77 -27) 📝 `runtime/test/kernel_writes_test.cpp` (+63 -0) 📝 `runtime/test/title_write_watch_test.cpp` (+74 -0) </details> ### 📄 Description Coordination item: `RT-TITLE-PAGE-WATCH-RANGE-AND-PINS` (claimed in https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7164267570). ## Problem Five range-clipping, boundary, and pin-lifetime bugs in `runtime/src/title_page_watch.h`: 1. **`title_write_watch::mark(from, to)` dropped marks or over-marked past reservation boundaries (`title_page_watch.h:579-591`)**: - Checked only `!contains(from)`: a range starting before a write-watched reservation and overlapping its interior was silently dropped. - A range starting inside a write-watched reservation and extending past its end into unwatched memory wrote unwatched pages into `s.written`—and if `(to - from) / kPage > kKeep`, raised `s.floor = now`, forcing every unrelated shadow in the process to re-copy all of its memory. - Rounding `(to + kPage - 1) & ~(kPage - 1)` wrapped to `0` near `UINTPTR_MAX`. 2. **`title_write_watch::ask()` checked only `born(from)` (`title_page_watch.h:660`)**: - If a run `[from, to)` spanned into a newly added/replaced write-watched reservation whose tail was added after the reader's last asking while the reservation at `from` predated it, `born(from)` returned the old `born <= since` and `ask()` missed the newly zeroed pages in the tail. 3. **Unsigned wraparound / infinite loop near `UINTPTR_MAX` (`title_page_watch.h:213`, `518`, `773`, `811`, `833`, `875`)**: - `title_page_aliases::for_each_in()` looped `va += kPageSize` without stopping before wraparound when `end > UINTPTR_MAX - (kPageSize - 1)`. - `title_write_watch::add()` could wrap `end` below `base` when `base + size` exceeded `UINTPTR_MAX`, breaking map order invariants. - `kernel_writes::begin_write()`, `hold()`, `reopen()`, and `write()` computed `begin + bytes` without saturation (wrapping `end` below `begin` near `UINTPTR_MAX`). 4. **`kernel_writes::read_file_scatter()` pin leak on synchronous completion & `DWORD` page-count overflow (`title_page_watch.h:927`, `936`)**: - Passed `bool(ok) || error == ERROR_IO_PENDING` to `end_write()` instead of `!ok && error == ERROR_IO_PENDING` (as `read_file()` already does): if `ReadFileScatter` completes synchronously (`ok == TRUE`), pins were kept with `returned = true` instead of released immediately, so reusing the `OVERLAPPED` for another pending request kept the completed scatter buffers pinned. - `(bytes + info.dwPageSize - 1) / info.dwPageSize` used 32-bit `DWORD` addition, overflowing for large `bytes`. 5. **Missing `.returned` initializer in `kernel_writes::hold()` (`title_page_watch.h:811`)**: - Triggered `-Wmissing-field-initializers` under `clang-cl /W4`. ## Changes - **`runtime/src/title_page_watch.h`**: - Updated `title_write_watch::mark(from, to)` to round with overflow saturation and clip directly against overlapping reservations in `s.regions` under `s.journal -> s.mutex` lock order (matching `ask()`'s lock order, zero process-heap allocations), raising `s.floor` only when total overlapping watched pages exceed `kKeep`. - Added `title_write_watch::born_covering(from, to)` and used it in `ask()` so a run is only treated as covered by an old reservation when a single reservation covers the entire `[from, to)`. - Made `title_write_watch::add()`, `title_page_aliases::for_each_in()`, and `kernel_writes::{begin_write, hold, reopen, write}` overflow-safe near `UINTPTR_MAX`. - Fixed `kernel_writes::read_file_scatter()` to use 64-bit page count arithmetic and release pins on synchronous completion (`!ok && error == ERROR_IO_PENDING`), and explicitly initialized `.returned = false` in `kernel_writes::hold()`. - **`runtime/test/title_write_watch_test.cpp`** & **`runtime/test/kernel_writes_test.cpp`**: - Added regression tests for `mark()` leading-margin clipping, trailing-margin clipping without spurious `s.floor` bumps, multi-reservation spans, `ask()` detecting split/new-tail reservations, `UINTPTR_MAX` saturation/termination in `add()`/`mark()`/`title_page_aliases::for_each_in()`/`hold()`, and `kernel_writes::read_file_scatter()`. ## Verification - **Before fix** (new tests on unpatched `main` `5e95fa2f4`): - `title_write_watch_test`: FAILED (3 failures: leading-margin `mark()`, trailing-margin `mark()`, and split-tail `ask()`) - `kernel_writes_test`: FAILED (1 failure: `hold()` wrapped `end` near `UINTPTR_MAX`) - **After fix** (rebased onto `main` `77baf9062`, MSVC 14.51.36231 `/W4 /WX /permissive- /std:c++20 /O2 /MD` and LLVM `clang-cl /W4 /WX`): - `title_write_watch_test.exe` — OK (26 checks, 0 failures) - `kernel_writes_test.exe` — 34 checks, 0 failures - `title_memory_map_test.exe` — 14 checks, 0 failures - `xmem_test.exe` — OK (480 checks) - `clang-cl /W4 /WX` on `title_write_watch_test.cpp` and `kernel_writes_test.cpp` — exit 0, 0 warnings - `python -I scripts/check_no_game_data.py runtime/src/title_page_watch.h runtime/test/title_write_watch_test.cpp runtime/test/kernel_writes_test.cpp` — clean (exit 0) ## Hardware / Environment - GPU / driver: n/a (CPU/Win32 memory-watch runtime tests; no GPU or Vulkan SDK required) - Host: Windows 11 Pro 64-bit, MSVC 14.51.36231 / LLVM `clang-cl` AI-assisted: yes --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1887
No description provided.