loader: VICTORY_TRAP_WRITES can be silent — arm loudly, report faults on a trapped page, round a watched address to its word #641

Closed
opened 2026-11-28 20:02:04 +00:00 by github-bot · 1 comment
Owner

@⁠AnastasisKon on GitHub · 2026-11-28 19:57 UTC · original

VICTORY_TRAP_WRITES is the diagnostic the boot investigation leans on (item 3, the SGD5 write trap), and in two diagnostics-build runs it logged nothing at all — including a control page this repo records as written once by the main thread (docs/native-host.md, the VICTORY_TRAP_WRITES=0x181796000,0x181796a28 bullet). A silent trap is not a negative: today three different situations print exactly the same nothing.

What was measured, rather than assumed

The fault record the trap discriminates on is sound on this machine. A store to a PROT_READ page, delivered under Rosetta to an x86_64 handler, arrives with si_code 2 (SEGV_ACCERR) and __err 0x6; the load of a PROT_NONE page with __err 0x4. The loader's synthesized record (crates/victory-loader/src/native/seh/signal.rs:630, information[0] = err & 2) therefore turns those into 1 (write) and 0 (read), and write_trap's r.information[0] != 1 check does not swallow writes.

$ clang -O0 -arch x86_64 -o /tmp/traperr/t /tmp/traperr/t.c && arch -x86_64 /tmp/traperr/t
faulting store at 0x108fa8040
sig=10 si_code=2 si_addr=0x108fa8040 __err=0x6 (bit1=1) __trapno=0xe
store survived, value=2
sig=10 si_code=2 si_addr=0x108fa8080 __err=0x4 (bit1=0) __trapno=0xe

(The same source does not compile for the native arm64 host: __darwin_arm_exception_state64 has no __err. The title's x86_64 process is the one that matters, and it is the one measured.)

So the cause is in the arms, not in the record. Three ways to get silence, all of them currently invisible:

  1. The page never armed. start_write_trap called mprotect(page, 0x1000, PROT_READ) and dropped the result. A spec at the wrong base (the image is relocated; a VA from a different run, or an address outside the mapping) answers ENOMEM, nothing is trapped, and the run reports no writer for a page that is written.
  2. The write went to the page but not to a watched address. write_trap logs only when watched.contains(&(at & !7)); every other fault on that page is dropped after re-opening it. "Page is written elsewhere" and "page is never written" read the same.
  3. The open failed. mprotect(page, 0x1000, PROT_READ|PROT_WRITE) after a fault also dropped its result. If it fails, the faulting store faults again on return, forever: the run looks hung, and again nothing is logged.

A fourth, smaller one: parse_trap_spec stored watched addresses as written, while the match looks at at & !7, so 0x…a2c could never match anything.

The change (one PR)

  • start_write_trap says what it armed, per page: write trap: <page> is read-only, N watched, or write trap: <page> is not mapped, so it is not trapped, or a note when the page is outside the title image (wrong base). Pages that did not arm are left out, so the closer thread does not spin mprotect on an unmapped page at 3 kHz (which on its own perturbs a profiling run).
  • write_trap logs a fault on a trapped page named or not (the first eight unwatched ones), so "written elsewhere" is distinguishable from "never written".
  • A failed open is said once, so a spin is not mistaken for a hang.
  • A watched address is rounded to the eight bytes the match looks at, so an address inside a word watches that word.
  • docs/native-host.md carries the measurement above with its command line.

Two unit tests: the rounding/dedup of a spec, and arming a real anonymous page (arm, open, unmap, and the unmapped page must not arm) — the syscall path the silent runs depended on, exercised on this host.

This does not by itself say which of the three the two silent runs hit; it makes the next run say it in the first second, instead of after 30 s of nothing.

**@⁠AnastasisKon** on GitHub · 2026-11-28 19:57 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/637) `VICTORY_TRAP_WRITES` is the diagnostic the boot investigation leans on (item 3, the SGD5 write trap), and in two diagnostics-build runs it logged nothing at all — including a control page this repo records as written once by the main thread (`docs/native-host.md`, the `VICTORY_TRAP_WRITES=0x181796000,0x181796a28` bullet). A silent trap is not a negative: today three different situations print exactly the same nothing. ## What was measured, rather than assumed The fault record the trap discriminates on is sound on this machine. A store to a `PROT_READ` page, delivered under Rosetta to an x86_64 handler, arrives with `si_code` 2 (`SEGV_ACCERR`) and `__err` `0x6`; the load of a `PROT_NONE` page with `__err` `0x4`. The loader's synthesized record (`crates/victory-loader/src/native/seh/signal.rs:630`, `information[0] = err & 2`) therefore turns those into `1` (write) and `0` (read), and `write_trap`'s `r.information[0] != 1` check does not swallow writes. ``` $ clang -O0 -arch x86_64 -o /tmp/traperr/t /tmp/traperr/t.c && arch -x86_64 /tmp/traperr/t faulting store at 0x108fa8040 sig=10 si_code=2 si_addr=0x108fa8040 __err=0x6 (bit1=1) __trapno=0xe store survived, value=2 sig=10 si_code=2 si_addr=0x108fa8080 __err=0x4 (bit1=0) __trapno=0xe ``` (The same source does not compile for the native arm64 host: `__darwin_arm_exception_state64` has no `__err`. The title's x86_64 process is the one that matters, and it is the one measured.) So the cause is in the arms, not in the record. Three ways to get silence, all of them currently invisible: 1. **The page never armed.** `start_write_trap` called `mprotect(page, 0x1000, PROT_READ)` and dropped the result. A spec at the wrong base (the image is relocated; a VA from a different run, or an address outside the mapping) answers `ENOMEM`, nothing is trapped, and the run reports no writer for a page that is written. 2. **The write went to the page but not to a watched address.** `write_trap` logs only when `watched.contains(&(at & !7))`; every other fault on that page is dropped after re-opening it. "Page is written elsewhere" and "page is never written" read the same. 3. **The open failed.** `mprotect(page, 0x1000, PROT_READ|PROT_WRITE)` after a fault also dropped its result. If it fails, the faulting store faults again on return, forever: the run looks hung, and again nothing is logged. A fourth, smaller one: `parse_trap_spec` stored watched addresses as written, while the match looks at `at & !7`, so `0x…a2c` could never match anything. ## The change (one PR) * `start_write_trap` says what it armed, per page: `write trap: <page> is read-only, N watched`, or `write trap: <page> is not mapped, so it is not trapped`, or a note when the page is outside the title image (wrong base). Pages that did not arm are left out, so the closer thread does not spin `mprotect` on an unmapped page at 3 kHz (which on its own perturbs a profiling run). * `write_trap` logs a fault on a trapped page named or not (the first eight unwatched ones), so "written elsewhere" is distinguishable from "never written". * A failed open is said once, so a spin is not mistaken for a hang. * A watched address is rounded to the eight bytes the match looks at, so an address inside a word watches that word. * `docs/native-host.md` carries the measurement above with its command line. Two unit tests: the rounding/dedup of a spec, and arming a real anonymous page (arm, open, unmap, and the unmapped page must not arm) — the syscall path the silent runs depended on, exercised on this host. This does not by itself say which of the three the two silent runs hit; it makes the next run say it in the first second, instead of after 30 s of nothing.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 20:02:04 +00:00
Author
Owner

@⁠AnastasisKon on GitHub · 2026-11-28 20:03 UTC · original

Duplicate of what PR #75 already does (and it does more: it ran the game on it, and it answers part of the writer-state question in docs/native-host.md). Everything still usable from my version — a watched address stored as written but compared at at & !7, the dropped result of the open after a fault, and the measurement showing the x86 __err does distinguish a write from a load under Rosetta — is in a comment on https://github.com/AnastasisKon/Native-Victory-macOS/pull/75 so it is not lost. No second PR.

**@⁠AnastasisKon** on GitHub · 2026-11-28 20:03 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/637#issuecomment-7244998238) Duplicate of what PR #75 already does (and it does more: it ran the game on it, and it answers part of the writer-state question in docs/native-host.md). Everything still usable from my version — a watched address stored as written but compared at `at & !7`, the dropped result of the open after a fault, and the measurement showing the x86 `__err` does distinguish a write from a load under Rosetta — is in a comment on https://github.com/AnastasisKon/Native-Victory-macOS/pull/75 so it is not lost. No second PR.
This discussion has been locked. Commenting is limited to contributors.
No description provided.