loader: fix range allocator TOCTOU race and sentinel check #466
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/lobomfz--VIctory-xmem!466
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-466"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@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 UTCFixes #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) andimmediate
0xc0000005crashes (0x4fffffffb/-1link at+0xad533cand+0x2626911):+0xbc0380:+0xbc0380checked a slot's A/B sizes unlocked before callingslot_free_a/slot_alloc_a(+0xbc04d0/+0xbc0530) andslot_free_b/slot_alloc_b(+0xbc05b0/+0xbc0670), and tail-called+0xbc0750(which also read the slot unlocked before locking). Concurrent workers hitting+0xbc0380for the same slot could bothfail the size check and overwrite a live node in place, breaking the doubly linked lists into a cycle or a dangling
-1link.+0xbc0380with the title's recursivesysCriticalSectionToken(+0x76b8e70via+0x4b40/+0x4d9dab0) using an RWX trampoline for its 15-byte prologue.is_free_node(0xffff)in+0xad52d0:+0xad52d0checkedcx >= [+0x908f02e]andcx != [+0x908f030]andcx != [+0x908f032], allowing0xffff(-1) to pass as a free node.+0xad52e2(je,0x74->jbe,0x76) so any index>= [+0x908f030](including0xffff) returnsfalse.Changes
crates/victory-loader/src/range_allocator.rs: load-time prologue hook on+0xbc0380and byte patch at+0xad52db(+0xad52e2), each verified against expected bytes before patching.crates/victory-loader/src/lib.rs: exposerange_allocator.apps/victory/src/run.rs: applyvictory_loader::range_allocator::applyat load time alongside the other image patches.Testing
x86_64-pc-windows-msvcrelease binary (./build.sh) without warnings or errors.range allocator: slot ensure wrapped with lock site=0xbc0380andrange allocator: is_free_node sentinel check patched site=0xad52db).+0x262686f/+0x2215c35freeze with the detail patches enabled).@Kal0s-design on GitHub · 2026-12-04 14:43 UTC · original
Checked
a8df01cnatively 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=2gives 86 passed on the PR and 84 on the merge base0fd7614. 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 realapply()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
I couldn't check the claim in #142 that nothing under +0xbc0380 takes another lock without the image.
AI-assisted: yes
@vMohammad24 on GitHub · 2026-12-04 15:01 UTC · original
(antigravity)
@Kal0s-design
Is
+0x908f030fixed atnode_count - 2, or can live nodes exist above it?0x140ad519b..0x140ad51d6(free-slot search), the title scans from[+0x908f02e]up to[+0x908f01c](node_count), looking for a node withsize == 0, and treatsax == [+0x908f030]as "no free slot":[+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].Does
+0xbc0380take only three arguments?+0xbc0380only readsrcx(this, saved torsi),edx(size, saved toebx), andr8(slot_ptr, saved tordi). It never readsr9or any stack argument, and its tail-call target+0xbc0750only readsecx,dl, andr8b. Its caller at+0x82cd30..+0x82cd41likewise only setsrcx,edx, andr8.Does anything reach
+0xbc0750or 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+0x76b8e70around their own critical sections (+0xbc0750reads[+0x908eff0]before locking, which is why wrapping+0xbc0380closes 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.Is applying it unconditionally in
run.rs:434-437(without an env switch) intended?detail,pad_effects,haptics, andmemory_budget(which are user preferences or hardware-specific features), this is a correctness fix for a title TOCTOU race and0xffffsentinel bug, and it validates the exact expected bytes before patching.@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.