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

Open
github-bot wants to merge 1 commit from github-bot/gh-1875 into main AGit
Owner

@⁠ambroseautomation on GitHub · 2026-11-25 13:52 UTC · original
ambroseautomation:fix/rt-title-page-watch-range-and-pins → main · +214 −27 · 3 files · 1 commits · open

Coordination item: RT-TITLE-PAGE-WATCH-RANGE-AND-PINS (claimed in #1608).

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

**@⁠ambroseautomation** on GitHub · 2026-11-25 13:52 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1875) `ambroseautomation:fix/rt-title-page-watch-range-and-pins` → `main` · +214 −27 · 3 files · 1 commits · open Coordination item: `RT-TITLE-PAGE-WATCH-RANGE-AND-PINS` (claimed in https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608). ## 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
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:09:33 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1875/head:github-bot/gh-1875'
git switch 'github-bot/gh-1875'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1875'
git switch 'github-bot/gh-1875'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1875'
git switch 'github-bot/gh-1875'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1875'
git switch 'main'
git merge --squash 'github-bot/gh-1875'
git switch 'main'
git merge --ff-only 'github-bot/gh-1875'
git switch 'main'
git merge 'github-bot/gh-1875'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.