loader: VICTORY_LOOKUP_EXIT gives the title's object lookup a not-found exit (#456) #565

Open
maaalrey wants to merge 1 commit from upstream-pr/565 into main
maaalrey commented 2026-11-28 05:18:51 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by maaalrey on 2026-11-28T05:18:51Z.


What this changes

Refs #456.

Adds VICTORY_LOOKUP_EXIT=1, off by default: a way out of the title lookup that the stuck threads of #456 sit in.

  • The loop at image+0x50eb80f searches the title's pooled-object table group by group and has no exit for a handle that is in no group. When the title frees an object while a job still holds it, that job spins for good, the job that empties the table waits for it, and the main thread waits behind that (dumps and the write watch are in #456).
  • With the switch set, native::lookup_exit::arm turns the first byte of the loop's "not in this group, try the next" path (image+0x50eb9d1) into an int3 before any title code runs. on_breakpoint, called first in the exception dispatch, then does what that path did, or, when the group just searched has an empty control byte (0x80), sets rip to the function's own epilogue (image+0x50eb910), which the function already takes for an object with nothing to draw.
  • Nothing is patched unless the code at that place is the counter step and the relative jump back to the loop head that this build has there; the check is on the two instruction forms and the jump target, with no title data copied beyond those forms. The page gets back the protection it had.
  • Exits are logged (the first eight, then at powers of two), with the handle and the counts of exits and of steps to a next group so far.
  • docs/native-host.md: the switch in the diagnostics list, and a section "A title lookup that never ends" with what was observed, what is read from the code only, what is not checked, and the two guesses (that a group with an empty byte ends the search; that timing is what lets the free run ahead here).

It does not fix the cause: what orders the free behind the lookups on the console is still unknown. As I read it, a freed object is skipped for that frame. I have not checked what else the early return skips, and I have not compared pictures.

The offsets are for the one build we test, like VICTORY_HOLD_BYTE's address.

How it was tested

  • cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory (and -p xtask): all pass. Eight new tests in native::lookup_exit, among them the_patch_changes_one_byte_and_keeps_the_pages_protection (on a stand-in page), a_missing_key_leaves_the_loop_at_the_first_group_with_an_empty_entry and nothing_is_handled_before_the_patch_is_in.
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings
  • cargo xtask policy
  • Not run: the victory-gpu tests (#548; this change does not touch that crate).
  • Ran the game: M4 Max, 128 GB, macOS 27.2.1, Series S, windowed, loading one story save, 260 s a run, thread dump at 200 s.

All on main 2f4d81e0, with one local change for the runs only (the sound output replaced by the silent one):

VICTORY_LOOKUP_EXIT code runs stuck on the loading screen, a thread in the lookup loop in the dump in the world
unset first revision of this commit 4 4 0
1 first revision (ended the search at any control byte with bit 7 set) 4 0 4
1 this commit 4 0 4

The first two rows are four pairs, alternating. With this commit the log has VICTORY_LOOKUP_EXIT: armed and, in three of the four runs, the exit lines: at least 8 exits each (the log prints the first eight, then powers of two), every handle a pool-4 handle with generation 2, after 19 to 151 steps to a next group; in the fourth run no lookup needed the exit. The world started 105 to 192 s into those runs and ran at 1.3 to 1.6 fps mean to the end of the run. The stuck runs go quiet after 76 to 78 thousand DirectStorage requests, as in #456.

Before the rewrite for this branch, the same idea as a local experiment on main 8efcff13: 0 of 10 runs stuck with it against 4 of 4 without, and one new-game run of 480 s reached the world and never took the exit (numbers in #456).

Checklist

  • No game files, game code, disassembly listings or SDK files are included
  • Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md
  • New diagnostics are behind a VICTORY_* variable and documented
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5536986741 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/565). Originally posted by [maaalrey](https://github.com/maaalrey) on 2026-11-28T05:18:51Z. --- ## What this changes Refs #456. Adds `VICTORY_LOOKUP_EXIT=1`, off by default: a way out of the title lookup that the stuck threads of #456 sit in. - The loop at image+0x50eb80f searches the title's pooled-object table group by group and has no exit for a handle that is in no group. When the title frees an object while a job still holds it, that job spins for good, the job that empties the table waits for it, and the main thread waits behind that (dumps and the write watch are in #456). - With the switch set, `native::lookup_exit::arm` turns the first byte of the loop's "not in this group, try the next" path (image+0x50eb9d1) into an `int3` before any title code runs. `on_breakpoint`, called first in the exception dispatch, then does what that path did, or, when the group just searched has an empty control byte (0x80), sets `rip` to the function's own epilogue (image+0x50eb910), which the function already takes for an object with nothing to draw. - Nothing is patched unless the code at that place is the counter step and the relative jump back to the loop head that this build has there; the check is on the two instruction forms and the jump target, with no title data copied beyond those forms. The page gets back the protection it had. - Exits are logged (the first eight, then at powers of two), with the handle and the counts of exits and of steps to a next group so far. - docs/native-host.md: the switch in the diagnostics list, and a section "A title lookup that never ends" with what was observed, what is read from the code only, what is not checked, and the two guesses (that a group with an empty byte ends the search; that timing is what lets the free run ahead here). It does not fix the cause: what orders the free behind the lookups on the console is still unknown. As I read it, a freed object is skipped for that frame. I have not checked what else the early return skips, and I have not compared pictures. The offsets are for the one build we test, like `VICTORY_HOLD_BYTE`'s address. ## How it was tested - [x] `cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory` (and `-p xtask`): all pass. Eight new tests in `native::lookup_exit`, among them `the_patch_changes_one_byte_and_keeps_the_pages_protection` (on a stand-in page), `a_missing_key_leaves_the_loop_at_the_first_group_with_an_empty_entry` and `nothing_is_handled_before_the_patch_is_in`. - [x] `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings` - [x] `cargo xtask policy` - [ ] Not run: the `victory-gpu` tests (#548; this change does not touch that crate). - [x] Ran the game: M4 Max, 128 GB, macOS 27.2.1, Series S, windowed, loading one story save, 260 s a run, thread dump at 200 s. All on main 2f4d81e0, with one local change for the runs only (the sound output replaced by the silent one): | `VICTORY_LOOKUP_EXIT` | code | runs | stuck on the loading screen, a thread in the lookup loop in the dump | in the world | |---|---|---|---|---| | unset | first revision of this commit | 4 | 4 | 0 | | 1 | first revision (ended the search at any control byte with bit 7 set) | 4 | 0 | 4 | | 1 | this commit | 4 | 0 | 4 | The first two rows are four pairs, alternating. With this commit the log has `VICTORY_LOOKUP_EXIT: armed` and, in three of the four runs, the exit lines: at least 8 exits each (the log prints the first eight, then powers of two), every handle a pool-4 handle with generation 2, after 19 to 151 steps to a next group; in the fourth run no lookup needed the exit. The world started 105 to 192 s into those runs and ran at 1.3 to 1.6 fps mean to the end of the run. The stuck runs go quiet after 76 to 78 thousand DirectStorage requests, as in #456. Before the rewrite for this branch, the same idea as a local experiment on main 8efcff13: 0 of 10 runs stuck with it against 4 of 4 without, and one new-game run of 480 s reached the world and never took the exit (numbers in #456). ## Checklist - [x] No game files, game code, disassembly listings or SDK files are included - [x] Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md - [x] New diagnostics are behind a `VICTORY_*` variable and documented
derz92 commented 2026-11-28 06:16:09 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by derz92 on 2026-11-28T06:16:09Z.


Independent test of this on another Mac: it rescues the #456 freeze here.

Setup: M5 Max, macOS 26.5, Series S, warm cache. This commit on lit-world/base-3 plus local speed changes, #437's switches. To make the race more frequent, both arms also ran with VICTORY_SKIP_PIPELINES=6a04b371a8e26853,7de0f9db96ddd295, which raised the freeze rate here to about 3 in 4 (data on #456). One binary, alternating on and off, 200 s world cap.

VICTORY_LOOKUP_EXIT runs froze runs where the exit fired
1 4 0 3
unset 4 1 (stopped presenting at frame 1517) n/a
  • In each run where it fired, it logged 8 exits ("a lookup met an empty entry before its handle; it returns", steps=313 each). That was at about 71 s, frame ~1500, exactly where the freezes happen here. The run then kept presenting to its cap (frames 1832–1884).
  • It armed at image+0x50eb9d1 on base-3 too, so the byte check matched this build.
  • The picture after the exits looked the same as runs without them: the same prologue window shot, captured every 5–20 s. Nothing visibly missing, though I haven't diffed images.

I'm keeping it on for play here. Thanks for this.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7235187189 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/565#issuecomment-7235187189). Originally posted by [derz92](https://github.com/derz92) on 2026-11-28T06:16:09Z. --- Independent test of this on another Mac: **it rescues the #456 freeze here.** Setup: M5 Max, macOS 26.5, Series S, warm cache. This commit on `lit-world/base-3` plus local speed changes, #437's switches. To make the race more frequent, both arms also ran with `VICTORY_SKIP_PIPELINES=6a04b371a8e26853,7de0f9db96ddd295`, which raised the freeze rate here to about 3 in 4 (data on #456). One binary, alternating on and off, 200 s world cap. | `VICTORY_LOOKUP_EXIT` | runs | froze | runs where the exit fired | |---|---|---|---| | 1 | 4 | **0** | 3 | | unset | 4 | 1 (stopped presenting at frame 1517) | n/a | - In each run where it fired, it logged 8 exits ("a lookup met an empty entry before its handle; it returns", `steps=313` each). That was at about 71 s, frame ~1500, exactly where the freezes happen here. The run then kept presenting to its cap (frames 1832–1884). - It armed at image+0x50eb9d1 on base-3 too, so the byte check matched this build. - The picture after the exits looked the same as runs without them: the same prologue window shot, captured every 5–20 s. Nothing visibly missing, though I haven't diffed images. I'm keeping it on for play here. Thanks for this.
maaalrey commented 2026-11-28 06:27:13 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by maaalrey on 2026-11-28T06:27:13Z.


Thank you for running it. Good to know the exits come on the new-game path too, at the same frames where it froze for you without it: so that freeze and the one I get loading a save look like the same thing.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7235303529 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/565#issuecomment-7235303529). Originally posted by [maaalrey](https://github.com/maaalrey) on 2026-11-28T06:27:13Z. --- Thank you for running it. Good to know the exits come on the new-game path too, at the same frames where it froze for you without it: so that freeze and the one I get loading a save look like the same thing.
AnastasisKon commented 2026-11-28 18:14:22 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-28T18:14:22Z.

Review: APPROVED


Reviewed and verified locally on head 03bf3367 (worktree /private/tmp/review-565):

  • cargo test --target x86_64-apple-darwin -p victory-loader --locked -- --test-threads=1: 37 passed, 0 failed, including all eight new lookup_exit tests (one-byte patch keeps page protection, only the expected miss path is recognised, empty-entry ends the probe, unreadable table takes the title's own step, switch on only for "1", nothing handled before armed, etc.).
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings: exit 0.
  • cargo xtask policy: exit 0.

The patch discipline is right for a build-specific hook: the seven bytes at image+0x50eb9d1 are verified to be + near jump to the loop head before anything is written, only one byte becomes int3, the page gets its old protection back and the icache is flushed, and arm() runs before any title thread so Rosetta translates the patched bytes. on_breakpoint costs one atomic load when not armed and is called first in dispatch. The guesses (empty control byte 0x80 ends the probe; free-ahead ordering is timing) are documented in the module docs and native-host.md, and the PR honestly says it does not fix the cause and has not compared pictures — appropriate for an off-by-default diagnostic. The A/B game table (4/4 stuck without, 4/4 in the world with) is exactly the evidence this needs. Approving.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6099504904 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/565#pullrequestreview-6099504904). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-28T18:14:22Z. **Review:** APPROVED --- Reviewed and verified locally on head 03bf3367 (worktree /private/tmp/review-565): - cargo test --target x86_64-apple-darwin -p victory-loader --locked -- --test-threads=1: 37 passed, 0 failed, including all eight new lookup_exit tests (one-byte patch keeps page protection, only the expected miss path is recognised, empty-entry ends the probe, unreadable table takes the title's own step, switch on only for "1", nothing handled before armed, etc.). - cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings: exit 0. - cargo xtask policy: exit 0. The patch discipline is right for a build-specific hook: the seven bytes at image+0x50eb9d1 are verified to be + near jump to the loop head before anything is written, only one byte becomes int3, the page gets its old protection back and the icache is flushed, and arm() runs before any title thread so Rosetta translates the patched bytes. on_breakpoint costs one atomic load when not armed and is called first in dispatch. The guesses (empty control byte 0x80 ends the probe; free-ahead ordering is timing) are documented in the module docs and native-host.md, and the PR honestly says it does not fix the cause and has not compared pictures — appropriate for an off-by-default diagnostic. The A/B game table (4/4 stuck without, 4/4 in the world with) is exactly the evidence this needs. Approving.
AnastasisKon commented 2026-11-28 18:14:37 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-28T18:14:37Z.


Small clarification to my review above (a shell ate the inline code): the seven bytes at image+0x50eb9d1 are verified to be inc eax (ff c0) followed by the near jump (e9) back to the loop head before anything is written.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7243641210 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/565#issuecomment-7243641210). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-28T18:14:37Z. --- Small clarification to my review above (a shell ate the inline code): the seven bytes at image+0x50eb9d1 are verified to be `inc eax` (ff c0) followed by the near jump (e9) back to the loop head before anything is written.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/565:upstream-pr/565'
git switch 'upstream-pr/565'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/565'
git switch 'upstream-pr/565'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/565'
git switch 'upstream-pr/565'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/565'
git switch 'main'
git merge --squash 'upstream-pr/565'
git switch 'main'
git merge --ff-only 'upstream-pr/565'
git switch 'main'
git merge 'upstream-pr/565'
git push origin 'main'
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!565
No description provided.