loader: fix range allocator TOCTOU race and sentinel check #466

Manually merged
github merged 1 commit from github-bot/gh-466 into main 2026-12-04 17:31:57 +00:00 AGit
Owner

@⁠vMohammad24 on GitHub · 2026-12-04 14:06 UTC · original
vMohammad24:fix/range-allocator-race → main · +236 −0 · 3 files · 1 commits · merged 2026-12-04 17:30 UTC

Fixes #436 and #142.

Summary

The title's 16-bit-indexed range allocator (0x14908f020 / 0x14908eff0) had two bugs causing long-session hangs (followed by the title's 30 s watchdog null write at +0x2215c35) and
immediate 0xc0000005 crashes (0x4fffffffb / -1 link at +0xad533c and +0x2626911):

  1. Lock-gap TOCTOU in +0xbc0380: +0xbc0380 checked a slot's A/B sizes unlocked before calling slot_free_a / slot_alloc_a (+0xbc04d0 / +0xbc0530) and slot_free_b /
    slot_alloc_b (+0xbc05b0 / +0xbc0670), and tail-called +0xbc0750 (which also read the slot unlocked before locking). Concurrent workers hitting +0xbc0380 for the same slot could both
    fail the size check and overwrite a live node in place, breaking the doubly linked lists into a cycle or a dangling -1 link.
    • Wrapped +0xbc0380 with the title's recursive sysCriticalSectionToken (+0x76b8e70 via +0x4b40 / +0x4d9dab0) using an RWX trampoline for its 15-byte prologue.
  2. is_free_node(0xffff) in +0xad52d0: +0xad52d0 checked cx >= [+0x908f02e] and cx != [+0x908f030] and cx != [+0x908f032], allowing 0xffff (-1) to pass as a free node.
    • Patched +0xad52e2 (je, 0x74 -> jbe, 0x76) so any index >= [+0x908f030] (including 0xffff) returns false.

Changes

  • crates/victory-loader/src/range_allocator.rs: load-time prologue hook on +0xbc0380 and byte patch at +0xad52db (+0xad52e2), each verified against expected bytes before patching.
  • crates/victory-loader/src/lib.rs: expose range_allocator.
  • apps/victory/src/run.rs: apply victory_loader::range_allocator::apply at load time alongside the other image patches.

Testing

  • Built x86_64-pc-windows-msvc release binary (./build.sh) without warnings or errors.
  • Confirmed both patches apply at launch (range allocator: slot ensure wrapped with lock site=0xbc0380 and range allocator: is_free_node sentinel check patched site=0xad52db).
  • Played for 40 minutes and finished 2 missions with no hangs or crashes (previously hitting the +0x262686f / +0x2215c35 freeze with the detail patches enabled).
**@⁠vMohammad24** on GitHub · 2026-12-04 14:06 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/466) `vMohammad24:fix/range-allocator-race` → `main` · +236 −0 · 3 files · 1 commits · merged 2026-12-04 17:30 UTC Fixes #436 and #142. ### Summary The title's 16-bit-indexed range allocator (`0x14908f020` / `0x14908eff0`) had two bugs causing long-session hangs (followed by the title's 30 s watchdog null write at `+0x2215c35`) and immediate `0xc0000005` crashes (`0x4fffffffb` / `-1` link at `+0xad533c` and `+0x2626911`): 1. **Lock-gap TOCTOU in `+0xbc0380`:** `+0xbc0380` checked a slot's A/B sizes unlocked before calling `slot_free_a` / `slot_alloc_a` (`+0xbc04d0` / `+0xbc0530`) and `slot_free_b` / `slot_alloc_b` (`+0xbc05b0` / `+0xbc0670`), and tail-called `+0xbc0750` (which also read the slot unlocked before locking). Concurrent workers hitting `+0xbc0380` for the same slot could both fail the size check and overwrite a live node in place, breaking the doubly linked lists into a cycle or a dangling `-1` link. - Wrapped `+0xbc0380` with the title's recursive `sysCriticalSectionToken` (`+0x76b8e70` via `+0x4b40` / `+0x4d9dab0`) using an RWX trampoline for its 15-byte prologue. 2. **`is_free_node(0xffff)` in `+0xad52d0`:** `+0xad52d0` checked `cx >= [+0x908f02e]` and `cx != [+0x908f030]` and `cx != [+0x908f032]`, allowing `0xffff` (`-1`) to pass as a free node. - Patched `+0xad52e2` (`je`, `0x74` -> `jbe`, `0x76`) so any index `>= [+0x908f030]` (including `0xffff`) returns `false`. ### Changes - `crates/victory-loader/src/range_allocator.rs`: load-time prologue hook on `+0xbc0380` and byte patch at `+0xad52db` (`+0xad52e2`), each verified against expected bytes before patching. - `crates/victory-loader/src/lib.rs`: expose `range_allocator`. - `apps/victory/src/run.rs`: apply `victory_loader::range_allocator::apply` at load time alongside the other image patches. ### Testing - Built `x86_64-pc-windows-msvc` release binary (`./build.sh`) without warnings or errors. - Confirmed both patches apply at launch (`range allocator: slot ensure wrapped with lock site=0xbc0380` and `range allocator: is_free_node sentinel check patched site=0xad52db`). - Played for 40 minutes and finished 2 missions with no hangs or crashes (previously hitting the `+0x262686f` / `+0x2215c35` freeze with the detail patches enabled).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-04 14:09:11 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-04 14:43 UTC · original

Checked a8df01c natively on Windows (x86_64-pc-windows-msvc, not Wine). I can't run the title here, so the title side is checked against the patch bytes and the notes in #142 and #436, and the hook against a synthetic image.

cargo test -p victory-loader --lib -- --test-threads=2 gives 86 passed on the PR and 84 on the merge base 0fd7614. Clippy shows no new warnings. The two new tests only compare constants, so they don't exercise the hook.

The 15-byte prologue (range_allocator.rs:28-36) ends on an instruction boundary and has nothing RIP-relative, so the 14-byte jump plus a nop is safe. It's the same pattern as the haptics hook (haptics.rs:244-316). The wrapper (range_allocator.rs:65-79) takes no loader-side lock, and nothing else in the tree touches these offsets, so any deadlock risk is on the title side.

To exercise the hook I wrote a local test that isn't part of the PR. It builds a fake image with the same prologue at +0xbc0380, followed by a racy body. The body does an unlocked check, then a locked write through stubs at +0x4b40 and +0x4d9dab0 that go to a recursive lock. Then 8 threads make 20,000 calls each. Before apply() I got 159,643 overlapping entries and 119,170 lost updates. After the real apply() on that image both were 0, every argument and return value came back intact, and the 280,152 lock calls matched the unlocks, with the inner relocks taken recursively. That covers the hook mechanics only, not the title's allocator.

For the jbe byte, 0xad52db + 7 + 0x085b9d4e = 0x908f030, so the compare reads the right word. I ran the PR's 9 bytes in the fake image with that word at 0x15e1. With je only 0x15e1 took the jump. With jbe 0x15e1, 0x15e2, 0xfffe and 0xffff all take it, while 0, 0xaf0 and 0x15e0 don't. With the numbers in #142 (5603 nodes, sentinels 0x15e1 and 0x15e2), that rejects only the two sentinels and non-nodes. This assumes the +0xa target is the not-free exit, as the PR says. It also means correctness now depends on that word always being the top index. With 0 there, every index gets rejected.

Some questions

  1. Is +0x908f030 fixed at node count minus 2 for the whole session, or can the array grow or be rebuilt with live nodes above it?
  2. Does +0xbc0380 take only three arguments? The wrapper (range_allocator.rs:55, 65) forwards rcx, rdx and r8 and calls the lock first, so a fourth argument in r9 or on the stack would be lost.
  3. Does anything reach +0xbc0750 or the slot helpers directly, bypassing +0xbc0380?
  4. This is applied unconditionally (run.rs:434-437), while the LOD, pad effect, haptics and tile heap patches next to it are behind env switches. Is that intended?

I couldn't check the claim in #142 that nothing under +0xbc0380 takes another lock without the image.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-04 14:43 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/466#issuecomment-7414752812) Checked a8df01c natively on Windows (x86_64-pc-windows-msvc, not Wine). I can't run the title here, so the title side is checked against the patch bytes and the notes in #142 and #436, and the hook against a synthetic image. `cargo test -p victory-loader --lib -- --test-threads=2` gives 86 passed on the PR and 84 on the merge base 0fd7614. Clippy shows no new warnings. The two new tests only compare constants, so they don't exercise the hook. The 15-byte prologue (range_allocator.rs:28-36) ends on an instruction boundary and has nothing RIP-relative, so the 14-byte jump plus a nop is safe. It's the same pattern as the haptics hook (haptics.rs:244-316). The wrapper (range_allocator.rs:65-79) takes no loader-side lock, and nothing else in the tree touches these offsets, so any deadlock risk is on the title side. To exercise the hook I wrote a local test that isn't part of the PR. It builds a fake image with the same prologue at +0xbc0380, followed by a racy body. The body does an unlocked check, then a locked write through stubs at +0x4b40 and +0x4d9dab0 that go to a recursive lock. Then 8 threads make 20,000 calls each. Before `apply()` I got 159,643 overlapping entries and 119,170 lost updates. After the real `apply()` on that image both were 0, every argument and return value came back intact, and the 280,152 lock calls matched the unlocks, with the inner relocks taken recursively. That covers the hook mechanics only, not the title's allocator. For the jbe byte, 0xad52db + 7 + 0x085b9d4e = 0x908f030, so the compare reads the right word. I ran the PR's 9 bytes in the fake image with that word at 0x15e1. With je only 0x15e1 took the jump. With jbe 0x15e1, 0x15e2, 0xfffe and 0xffff all take it, while 0, 0xaf0 and 0x15e0 don't. With the numbers in #142 (5603 nodes, sentinels 0x15e1 and 0x15e2), that rejects only the two sentinels and non-nodes. This assumes the +0xa target is the not-free exit, as the PR says. It also means correctness now depends on that word always being the top index. With 0 there, every index gets rejected. Some questions 1. Is +0x908f030 fixed at node count minus 2 for the whole session, or can the array grow or be rebuilt with live nodes above it? 2. Does +0xbc0380 take only three arguments? The wrapper (range_allocator.rs:55, 65) forwards rcx, rdx and r8 and calls the lock first, so a fourth argument in r9 or on the stack would be lost. 3. Does anything reach +0xbc0750 or the slot helpers directly, bypassing +0xbc0380? 4. This is applied unconditionally (run.rs:434-437), while the LOD, pad effect, haptics and tile heap patches next to it are behind env switches. Is that intended? I couldn't check the claim in #142 that nothing under +0xbc0380 takes another lock without the image. AI-assisted: yes
Author
Owner

@⁠vMohammad24 on GitHub · 2026-12-04 15:01 UTC · original

(antigravity)

@⁠Kal0s-design

  1. Is +0x908f030 fixed at node_count - 2, or can live nodes exist above it?

    • In 0x140ad519b..0x140ad51d6 (free-slot search), the title scans from [+0x908f02e] up to [+0x908f01c] (node_count), looking for a node with size == 0, and treats ax == [+0x908f030] as "no free slot":
      140ad519b: movzx ecx, WORD PTR [0x14908f02e]
      140ad51a2: mov   edx, DWORD PTR [0x14908f01c]
      ...
      140ad51cf: cmp   ax, WORD PTR [0x14908f030]
      140ad51d6: je    0x140ad52ab
      
    • The sentinels ([+0x908f030] = node_count - 2, [+0x908f032] = node_count - 1) sit at the top of the fixed array; no live nodes are allocated at or above [+0x908f030].
  2. Does +0xbc0380 take only three arguments?

    • Yes. +0xbc0380 only reads rcx (this, saved to rsi), edx (size, saved to ebx), and r8 (slot_ptr, saved to rdi). It never reads r9 or any stack argument, and its tail-call target +0xbc0750 only reads ecx, dl, and r8b. Its caller at +0x82cd30..+0x82cd41 likewise only sets rcx, edx, and r8.
  3. Does anything reach +0xbc0750 or the slot helpers directly, bypassing +0xbc0380?

    • slot_free_a (+0xbc04d0), slot_alloc_a (+0xbc0530), slot_free_b (+0xbc05b0), slot_alloc_b (+0xbc0670), and +0xbc0750 (+0xbc0852) each acquire and release +0x76b8e70 around their own critical sections (+0xbc0750 reads [+0x908eff0] before locking, which is why wrapping +0xbc0380 closes the gap for that path). We did not do a full-binary xref scan of the 150 MB image for other callers of +0xbc0750, though the slot helpers themselves are already self-locking.
  4. Is applying it unconditionally in run.rs:434-437 (without an env switch) intended?

    • Yes — unlike detail, pad_effects, haptics, and memory_budget (which are user preferences or hardware-specific features), this is a correctness fix for a title TOCTOU race and 0xffff sentinel bug, and it validates the exact expected bytes before patching.
**@⁠vMohammad24** on GitHub · 2026-12-04 15:01 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/466#issuecomment-7415210701) (antigravity) @⁠Kal0s-design 1. **Is `+0x908f030` fixed at `node_count - 2`, or can live nodes exist above it?** - In `0x140ad519b..0x140ad51d6` (free-slot search), the title scans from `[+0x908f02e]` up to `[+0x908f01c]` (`node_count`), looking for a node with `size == 0`, and treats `ax == [+0x908f030]` as "no free slot": ```asm 140ad519b: movzx ecx, WORD PTR [0x14908f02e] 140ad51a2: mov edx, DWORD PTR [0x14908f01c] ... 140ad51cf: cmp ax, WORD PTR [0x14908f030] 140ad51d6: je 0x140ad52ab ``` - The sentinels (`[+0x908f030]` = `node_count - 2`, `[+0x908f032]` = `node_count - 1`) sit at the top of the fixed array; no live nodes are allocated at or above `[+0x908f030]`. 2. **Does `+0xbc0380` take only three arguments?** - Yes. `+0xbc0380` only reads `rcx` (`this`, saved to `rsi`), `edx` (`size`, saved to `ebx`), and `r8` (`slot_ptr`, saved to `rdi`). It never reads `r9` or any stack argument, and its tail-call target `+0xbc0750` only reads `ecx`, `dl`, and `r8b`. Its caller at `+0x82cd30..+0x82cd41` likewise only sets `rcx`, `edx`, and `r8`. 3. **Does anything reach `+0xbc0750` or the slot helpers directly, bypassing `+0xbc0380`?** - `slot_free_a` (`+0xbc04d0`), `slot_alloc_a` (`+0xbc0530`), `slot_free_b` (`+0xbc05b0`), `slot_alloc_b` (`+0xbc0670`), and `+0xbc0750` (`+0xbc0852`) each acquire and release `+0x76b8e70` around their own critical sections (`+0xbc0750` reads `[+0x908eff0]` before locking, which is why wrapping `+0xbc0380` closes the gap for that path). We did not do a full-binary xref scan of the 150 MB image for other callers of `+0xbc0750`, though the slot helpers themselves are already self-locking. 4. **Is applying it unconditionally in `run.rs:434-437` (without an env switch) intended?** - Yes — unlike `detail`, `pad_effects`, `haptics`, and `memory_budget` (which are user preferences or hardware-specific features), this is a correctness fix for a title TOCTOU race and `0xffff` sentinel bug, and it validates the exact expected bytes before patching.
github manually merged commit 792dc2c71f into main 2026-12-04 17:31:57 +00:00
Author
Owner

@⁠mancube on GitHub · 2026-12-04 21:14 UTC · original · edited 2026-12-04 23:05 UTC

Tested on my machine, no more crashes, even after multiple hours of gameplay, used to crash after 30 minutes/1 hour.

**@⁠mancube** on GitHub · 2026-12-04 21:14 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/466#issuecomment-7424422793) · edited 2026-12-04 23:05 UTC Tested on my machine, no more crashes, even after multiple hours of gameplay, used to crash after 30 minutes/1 hour.
This discussion has been locked. Commenting is limited to contributors.
No description provided.