Skip to content

title_page_watch: no endless alias walk, mark() covers watched parts, no end wraps (RT-TITLE-PAGE-WATCH-BOUNDS) - #2546

Open
Tyrannys wants to merge 1 commit into
ProjectLeonida:mainfrom
Tyrannys:rt-title-page-watch-bounds
Open

Tyrannys wants to merge 1 commit into
ProjectLeonida:mainfrom
Tyrannys:rt-title-page-watch-bounds

Conversation

@Tyrannys

Copy link
Copy Markdown
Contributor

Package RT-TITLE-PAGE-WATCH-BOUNDS: item 2 of @kevincommits' Finding 19 on #1608 (#1608 (comment)). Thank you for the find. Claim: #1608 (comment).

What changes (runtime/src/title_page_watch.h)

  1. title_page_aliases::for_each_in always ends.
    • Before, it walked 64 KiB pages with va < end; va += kPageSize. When end is near UINTPTR_MAX (the whole range title_memory_map::changed() reports) and a page is aliased, va wrapped to 0 and the walk never ended.
    • It now counts the pages instead of comparing addresses.
    • A range with more pages than there are aliased pages checks each aliased page instead of walking the range. Its one caller (update_aliased in d3d12_vulkan_device.cpp) does not depend on the order.
  2. title_write_watch::mark.
    • Rounding to up wrapped to 0 near the top of the address space, so the mark was dropped. It now saturates, as title_memory_map::changed does.
    • It only checked contains(from), so a range that starts before a reservation marked nothing. It now marks every write-watched part of the range through for_each_in.
    • The parts are gathered before the journal's lock, keeping the existing lock order. If there are more than 16 parts, or more than kKeep pages in all, every reader takes all of its pages as written, as before.
    • Pages outside any reservation are no longer marked.
  3. No wrapping ends.
    • title_write_watch::add, kernel_writes::begin_write and hold, and title_texel_writes::note cap their end at UINTPTR_MAX. Before, begin + bytes could wrap, and for_each_pin then never found the pin.
    • read_file_scatter counts its pages in 64 bits. Before, a bytes value near 4 GiB rounded to 0 pages in a DWORD.

Tests

New title_page_watch_bounds_test (CPU only). It does address arithmetic only: nothing is mapped or touched at the addresses it uses.

  • Aliased pages near the top, walked over [top - 8 pages, UINTPTR_MAX): the walk ends and finds exactly the two aliased pages in range.
  • mark(base - 1 page, base + 1 page) marks the reservation's first page, and not the unwatched page before it.
  • add of a reservation running past the top keeps it, and mark(..., UINTPTR_MAX) marks its pages.
  • begin_write and hold of [UINTPTR_MAX - 0xfff, +0x2000) give pins that for_each_pin finds.
  • note past the top is kept as [begin, UINTPTR_MAX).

On main, the first check hangs. The test runs that walk on a thread with a 5 s limit and reports it as a failure. With that case left out, 6 of the other 7 checks fail on main. With the fix, all 10 pass.

read_file_scatter's count has no test, because it would need a read with a million segments.

Nearby tests pass 16/16: ctest -R "title_page_watch|title_write_watch|title_memory_map|kernel_writes|geometry_views|xmem|title_va". gta6rt_d3d12_com builds.

Profile M: RTX 4070 SUPER, Windows 11, Ryzen 7 9800X3D, MSVC RelWithDebInfo. The full suite was not run because a boot was running. git diff --check and scripts/check_no_game_data.py are clean. No open PR touches this file. No game files used.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

… no end wraps (RT-TITLE-PAGE-WATCH-BOUNDS)

From kevincommits' Finding 19 on ProjectLeonida#1608 (item 2), in runtime/src/title_page_watch.h:

- title_page_aliases::for_each_in walked 64 KiB pages until `end`; with
  end near UINTPTR_MAX (title_memory_map::changed()'s whole range) the
  page wrapped to 0 and the walk never ended. It now counts the pages,
  and a range with more pages than there are aliased pages looks through
  the aliased set instead.
- title_write_watch::mark rounded `to` up with a wrap to 0 (the mark was
  dropped) and only checked whether the range's first page was watched.
  It now saturates like title_memory_map::changed, and marks every
  write-watched part of the range through for_each_in (gathered before
  the journal's lock; more than 16 parts or kKeep pages mark everything).
- title_write_watch::add, kernel_writes::begin_write and hold, and
  title_texel_writes::note saturate their end at UINTPTR_MAX instead of
  wrapping; read_file_scatter counts its pages in 64 bits.

New CPU test title_page_watch_bounds_test (address arithmetic only):
on main its first check hangs (caught by a 5 s limit), and with that
case left out 6 of the other 7 checks fail; all 10 pass with the fix.
read_file_scatter's count has no test (it would need a million-segment
read).

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zmasi

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

#2546 | head aaff683 (merged cleanly with main 02f71cc) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(title_page_watch_bounds|title_write_watch|title_write_watch_on|title_memory_map)$" --output-on-failure | red on main: title_page_watch_bounds fails at runtime (FAIL for_each_in over a range ending at UINTPTR_MAX ends — hangs and times out after 5.15s as va += kPageSize wraps past UINTPTR_MAX to 0) | green on PR+main: 4/4 CTests pass (title_page_watch_bounds 10/10 checks in 0.07s, title_write_watch 18/18, title_write_watch_on 18/18, title_memory_map 20/20); test_ci_registration.py 18/18 OK, check_no_game_data.py 0 violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yes

Audited for_each_in page-count bounding and sparse-set fallback (pages > s.aliased.size()), lock-ordered gathering of watched subranges before s.journal in mark(), saturating UINTPTR_MAX bounds across add / begin_write / hold / note, and 64-bit page-count arithmetic in read_file_scatter.

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the diff and tested merged onto main (493867366) with Ninja Release (MSVC).

Saturating top-of-address-space bounds and fixing page counting in for_each_in (title_page_watch.h) cleanly avoid wrap-around edge cases, and the new bounds tests plus existing watch/memory-map suites all pass.

Tests run

  • ctest --output-on-failure -R "^(title_page_watch_bounds|title_write_watch|title_write_watch_on|title_memory_map)$": 4/4 passed (title_memory_map 0.03 s, title_write_watch 0.02 s, title_page_watch_bounds 0.03 s, title_write_watch_on 0.00 s).

AI-assisted (Antigravity, Gemini).

@timbot103 timbot103 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static audit of aaff683f3 against 48bae6cad. Please fix the alias ordering before merging.

  • runtime/src/title_page_watch.h:232 emits sparse alias hits in hash-set order, but runtime/shims/d3d12_vulkan_device.cpp:1211 binary-searches the resulting list. A missed hit releases an active guard entry and rebuilds it in the same refresh. Please sort the list before searching and cover guard retention across successive refreshes.
  • runtime/src/title_page_watch.h:631 and runtime/src/title_page_watch.h:645 can still overflow on a synthetic [0, UINTPTR_MAX) overlap. Use 1 + (span - 1) / kPage for a nonempty span. At runtime/src/title_page_watch.h:229 and runtime/src/title_page_watch.h:623, validate the original endpoints before rounding so empty or reversed ranges do nothing.

AI-assisted.

@lixonik

lixonik commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

On aaff683f3b0a5cac33d7c05dff98d0a6380d1e9e merged into main c8bf0e7b65, the stock bounds test passed all 10 checks. I also exercised the alias ordering from the later review on Windows 10/MSVC 19.29.

I inserted three synthetic aliases in a nonascending order and scanned a range with more pages than aliases. Main returned a sorted list, and the same std::binary_search used by the consumer found all three. With the PR, the list was unsorted and that search found only two. This confirms that the consumer needs a sort before deciding which guards to retain.

An empty interval wholly inside a single aliased page gave one callback on both builds. The PR also accepted a synthetic whole-address-space watch record, but mark(0, UINTPTR_MAX) did not advance the all-pages fallback, as the rounded page count wrapped to zero. No addresses were mapped or dereferenced.

AI-assisted. Codex (GPT-6).

@OreoFR

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Verified by OreoFR — FAILS (diff read)

  • Base: main c8bf0e7 + this PR (head aaff683) merged with --no-ff
  • Red (main + only this PR's tests): title_page_watch_bounds fails on main: for_each_in over a range ending at UINTPTR_MAX ends (the walk did not end; the test's 5 s limit cut it). The test's CMake registration was kept for the red run.
  • Green (merge): title_page_watch_bounds passes; ctest (Ninja RelWithDebInfo, all targets, gta6rt launchers excluded) in package p1 = main + title_page_watch: no endless alias walk, mark() covers watched parts, no end wraps (RT-TITLE-PAGE-WATCH-BOUNDS) #2546 + d3d12_vulkan: without Resizable BAR, textures and staging buffers leave the BAR window to the arena; tile heaps prefer VRAM (RT-ALLOC-NO-REBAR-RETRIES) #1681 + RT-STATE-AUDIT-FORMATS: quantize 16-bit packed UNORM and rgba4 fallback clears #2479 + windows-boot: the keyboard pad in focus mode with an input script (BOOT-PAD-FOCUS) #1790 (no shared files): 495 tests, new failures vs main: none; an xmem working-set check failed once in the package as it does on main, and passed 5/5 when re-run alone; xmem_write_watch failed once in the package (a view-split reader check already on main) and passed 5/5 when re-run alone on main + this PR (main alone: 494 tests, 5 failures: loader, xs_proxy_mock, xmem_no_write_watch, api_ref_state timeout, xbox_present; xbox_present fails in every package as on main)
  • gta6rt.exe links without the XMA decoder (LEONIDA_CARGO=OFF, LEONIDA_XMA2_DECODER_LIB empty), main + this PR alone; imports unchanged vs main (25 DLLs, 655 functions, no WS2_32)
  • Diff read: runtime/src/title_page_watch.h:232: when the range has more pages than the alias set, for_each_in now walks the unordered_set, so the pages come out in hash order. But runtime/shims/d3d12_vulkan_device.cpp:1239 fills aliased_list_ from it, and :1251 runs std::binary_search on that list. An unsorted list breaks the search, so blocks that are still aliased can be released and re-armed. The tracker range is usually much larger than the alias set, so this is the common path. Sorting aliased_list_ after the walk, or sorting in the set path, fixes it. The wrap guards are right.
  • Other runs: Confirms timbot103's static audit (alias ordering). The tests pass, as in lixonik's run.
  • Machine: Windows 11, Intel Core Ultra 7 155H, RTX 4070 Laptop 8 GB, NVIDIA 621.40, Python 3.12.10, MSVC 19.44

AI-assisted (Claude Code, Claude Opus 5.5), run for OreoFR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants