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

Manually merged
github merged 2 commits from github-bot/gh-127 into main 2026-11-26 18:09:27 +00:00 AGit
Owner

@⁠Olima34 on GitHub · 2026-11-26 17:47 UTC · original
Olima34:feat/trace-descriptor-store-sites → main · +734 −0 · 11 files · 2 commits · merged 2026-11-26 18:09 UTC

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.

**@⁠Olima34** on GitHub · 2026-11-26 17:47 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/127) `Olima34:feat/trace-descriptor-store-sites` → `main` · +734 −0 · 11 files · 2 commits · merged 2026-11-26 18:09 UTC 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.
#43 asks how many descriptor records the title rewrites with plain
stores, which no device call reports. NativeHost::remirror_tables
already compares each bound table's records with those it last mirrored
at every draw and dispatch. With VICTORY_TRACE_DESCRIPTORS set, it now
counts per heap the records it checked and those it found changed, and
per submission the most changed; the frame log adds a "descriptor
remirror" line with the counts since its last line. Off, the cost is one
read of the switch per table and per submission.
#43, second step: which title instructions store the descriptor records,
and how many patch sites that means. With VICTORY_TRACE_DESCRIPTORS=sites,
each ExecuteCommandLists makes the pages of every descriptor heap's
records read-only (the title's own view), through a new write watch in
victory-platform. The loader's signal handler hands every write fault to
it first, whoever wrote: a fault in a watched page reopens that page and
the store runs again, so host writes (the device's view calls) are not
fatal. It counts as a site, the instruction as an image offset, only
when the instruction lies in the title image and the address in a heap's
records. The handler takes no lock and allocates nothing: a fixed table
of sites (overflow counted), and the watched ranges in two tables behind
sequence counts. An update writes the table not in use, publishes it,
protects its pages and only then opens the pages neither table holds,
and the handler reads both, so a store faulting during an update is
still the watch's. The frame log adds the distinct sites and those with
the most stores. What is counted is the first store per page per
submission, not every store.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 17:48:22 +00:00
github manually merged commit 3ec5bbb1f4 into main 2026-11-26 18:09:27 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 18:09 UTC · original

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

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 18:09 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/127#issuecomment-7200129421) Merged, thanks. Next is a world-load run with `VICTORY_TRACE_DESCRIPTORS=sites`, to choose between patching and remirroring (#43).
This discussion has been locked. Commenting is limited to contributors.
No description provided.