native: find the title's descriptor store sites (VICTORY_TRACE_DESCRIPTORS=sites) #127

Merged
Olima34 merged 2 commits from upstream-pr/127 into main 2026-11-26 18:09:23 +00:00
Olima34 commented 2026-11-26 17:47:11 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-26T17:47:11Z.


Second step for #43, on top of #119 (this branch contains #119's commit; review only the last commit, native: find the title's descriptor store sites). #119 measures how many records the title rewrites with plain stores; this finds where: which title instructions store into the descriptor heaps, which is what decides how many patch sites there are.

How (VICTORY_TRACE_DESCRIPTORS=sites, off by default).

  • New victory_platform::watch (macOS only). At each submission, glue::submit makes the pages of the title's shader-visible heaps read-only (its own CPU view only, never the GPU's).
  • The loader's signal handler hands every write fault to watch::on_write_fault first, before the title/host check, whoever wrote. A fault in a watched page makes that page writable again and returns, so the store runs again.
  • A fault is counted as a site (rip - image base) only when the RIP is in the title image and the address is inside a heap's record range. Host writes (the device's view calls) and title stores that only share the page go on unseen, and are never fatal.
  • The 64-frame log adds a descriptor store sites line: distinct sites, overflow, and the top 64 by count as image+0x...
  • Off, the handler's cost is one relaxed load.

Signal safety and races.

  • No allocation or lock in the fault path. Sites live in a fixed table of 4,096 atomics with open addressing, and overflow is counted. The handler calls mprotect directly.
  • The watched ranges live in two tables, each behind a sequence count (a seqlock). watch() writes the table not in use, publishes it, protects the new pages, and only then reopens the pages neither table covers.
  • The handler classifies against the current table and the previous one. So a store racing an update is still recognised, never escaping as a title exception. The first version did escape (review caught it): the new threaded test killed it with SIGBUS 5 times out of 5.
  • Remaining gap, documented: a handler delayed between its fault and its table read for longer than the end of one watch() and the start of the next. By then its page is already open.

Limits (in docs/native-host.md).

  • It counts the first store per page per submission, not every store.
  • At most 64 ranges.
  • A kernel write into a watched page (read() into it) would get EFAULT rather than a fault.
  • Reopened pages become read-write, which is right for descriptor records: the device's view calls write them too.

Tests.

  • victory-platform: sites_count_and_overflow, only_title_stores_into_a_range_count, a_store_into_the_old_tables_page_during_an_update_is_the_watchs (the pure rule, old plus new tables), and a_table_being_written_is_not_read (the seqlock).
  • victory-loader, macOS x86_64, through the real installed handler:
    • a_store_into_a_watched_page_lands_and_reopens_it: a store lands, its page reopens, and it is counted once; a store outside the range is not counted.
    • stores_during_updates_never_escape_the_watch: 20,000 watch() updates race stores into the shared pages. It dies with SIGBUS against the first version and passes with this one.

victory-platform and victory-gpu (arm64), victory-loader, victory-hle and victory-platform (x86_64-apple-darwin), Windows clippy and cargo xtask policy pass. Not run in game: whether Rosetta resumes every store form the title uses there (the probe covers a plain mov), and how much sites mode slows a frame.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6632250228 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/127). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-26T17:47:11Z. --- Second step for #43, on top of #119 (this branch contains #119's commit; review only the last commit, `native: find the title's descriptor store sites`). #119 measures how many records the title rewrites with plain stores; this finds **where**: which title instructions store into the descriptor heaps, which is what decides how many patch sites there are. **How (`VICTORY_TRACE_DESCRIPTORS=sites`, off by default).** - New `victory_platform::watch` (macOS only). At each submission, `glue::submit` makes the pages of the title's shader-visible heaps read-only (its own CPU view only, never the GPU's). - The loader's signal handler hands every write fault to `watch::on_write_fault` first, before the title/host check, whoever wrote. A fault in a watched page makes that page writable again and returns, so the store runs again. - A fault is counted as a site (`rip - image base`) only when the RIP is in the title image **and** the address is inside a heap's record range. Host writes (the device's view calls) and title stores that only share the page go on unseen, and are never fatal. - The 64-frame log adds a `descriptor store sites` line: distinct sites, overflow, and the top 64 by count as `image+0x..`. - Off, the handler's cost is one relaxed load. **Signal safety and races.** - No allocation or lock in the fault path. Sites live in a fixed table of 4,096 atomics with open addressing, and overflow is counted. The handler calls `mprotect` directly. - The watched ranges live in two tables, each behind a sequence count (a seqlock). `watch()` writes the table not in use, publishes it, protects the new pages, and only then reopens the pages neither table covers. - The handler classifies against the current table and the previous one. So a store racing an update is still recognised, never escaping as a title exception. The first version did escape (review caught it): the new threaded test killed it with SIGBUS 5 times out of 5. - Remaining gap, documented: a handler delayed between its fault and its table read for longer than the end of one `watch()` and the start of the next. By then its page is already open. **Limits (in docs/native-host.md).** - It counts the first store per page per submission, not every store. - At most 64 ranges. - A kernel write into a watched page (`read()` into it) would get EFAULT rather than a fault. - Reopened pages become read-write, which is right for descriptor records: the device's view calls write them too. **Tests.** - victory-platform: `sites_count_and_overflow`, `only_title_stores_into_a_range_count`, `a_store_into_the_old_tables_page_during_an_update_is_the_watchs` (the pure rule, old plus new tables), and `a_table_being_written_is_not_read` (the seqlock). - victory-loader, macOS x86_64, through the real installed handler: - `a_store_into_a_watched_page_lands_and_reopens_it`: a store lands, its page reopens, and it is counted once; a store outside the range is not counted. - `stores_during_updates_never_escape_the_watch`: 20,000 `watch()` updates race stores into the shared pages. It dies with SIGBUS against the first version and passes with this one. victory-platform and victory-gpu (arm64), victory-loader, victory-hle and victory-platform (x86_64-apple-darwin), Windows clippy and `cargo xtask policy` pass. Not run in game: whether Rosetta resumes every store form the title uses there (the probe covers a plain `mov`), and how much sites mode slows a frame.
LakshanSivakumar commented 2026-11-26 18:09:59 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T18:09:59Z.


Merged, thanks. Next is a world-load run with VICTORY_TRACE_DESCRIPTORS=sites, to choose between patching and remirroring (#43).

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7200129421 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/127#issuecomment-7200129421). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T18:09:59Z. --- Merged, thanks. Next is a world-load run with `VICTORY_TRACE_DESCRIPTORS=sites`, to choose between patching and remirroring (#43).
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
LakshanSivakumar/Native-Victory-macOS!127
No description provided.