loader: replace whole-byte insertq with a stub of moves at its first fault #440
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!440
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-440"
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?
@TreXito on GitHub · 2026-12-04 04:13 UTC · original
TreXito:pr/insertq-stub→main· +273 −11 · 2 files · 2 commits · openOn a CPU without SSE4a (any Intel), every
insertqfaults and is emulated in the exception handler. While driving that is about 1,450 to 1,700 exceptions a second, each a round trip through Wine's exception dispatch.The immediate form with a field of whole bytes (length and index multiples of 8) is now replaced at its first fault by a jump to a stub of plain moves, the same way the
clzeroloop and the wait instructions already are:lea rsp, [rsp-32], both registers to the stack,push rax, the field copied in 8/4/2/1-byte pieces throughrax,pop rax,movqback (the high quadword zero, as the emulation leaves it),lea rsp, [rsp+32],jmpback.lea,pushandpop: no flags change, no other register changes, the scratch is stack below the caller's.int3padding inside one aligned 16 bytes (patch), so no thread can be inside the rest of the instruction.ripand simply retries (the first try without this crashed at the jump bytes).VICTORY_INSERTQ_PATCH=0keeps everyinsertqemulated.Tests
an_insertq_stub_does_what_the_emulation_doesruns stubs for eight field shapes in a page of its own and compares them withdecode::insertq: low quadword equal, high quadword zero, source,raxand the arithmetic flags unchanged. Non-whole-byte fields and fields past the quadword get no stub.victory-loader's 85 library tests pass under Wine (GE-Proton10-34), the sixconsole_insnones also with--test-threads=1.raxand the flags preserved.Measured
i5-13600KF, RX 7800 XT, release build, 300 s standing, two runs per side (ABAB):
VICTORY_INSERTQ_PATCH=0+1.6% average. A 7-minute ride with the patch logged 0
insertqexceptions after the first pass over each site, and no crashes in any run. On AMD CPUs nothing changes:insertqruns natively and never faults.🤖 Generated with Claude Code
https://claude.ai/code/session_01WNCfbX5dPKJzSkgbf16GX4
@Kal0s-design on GitHub · 2026-12-04 04:30 UTC · original
Tested
9c3c84cnatively on Windows 11 (MSVC toolchain, not under Wine).cargo test -p victory-loaderpasses, 85 lib tests against 84 on the merge base, the new one being the only difference, and nothing is ignored natively.an_insertq_stub_does_what_the_emulation_doespassed 20 of 20 runs. Clippy shows the same 4 transmute warnings as the base and nothing new.I also ran a local differential (not pushed). All 256 destination/source pairs, including same register and xmm8 to xmm15, times 70 whole-byte length/index encodings, 430,080 runs on a stack of their own, all matched
decode::insertqwith the high quadword zero, the other xmm registers, rax, the arithmetic flags and rsp unchanged, and nothing written outside the 40 bytes under the caller's rsp. Drivingpatch_insertqon fake code also worked for 6 and 7 byte forms and left the ones crossing their 16 bytes alone.A few things.
emulateread the live bytes atriptwice. If another thread's patch lands between the0xe9test anddecode::decode, decode sees the jump, returnsNoneand the handler returns 0, which is the same crash the check was added for, just in a smaller window. Copying the bytes into a local array once and using that for both, or checking for the stub jump again when decode fails, would close it.usedgrows beforepatchis known to succeed, so a lost race wastes 64 bytes of the stub area.patch_clzerodoes the same and it is unlikely to matter, but bumping it only on success would be cleaner.STUBSlock and build a stub on every fault before being rejected. Doing the alignment and length checks before the lock would make that path cheap.leacould step past 128 bytes first (that needs the disp32 form, a few bytes longer).AI-assisted: yes
@Luxuride on GitHub · 2026-12-04 11:16 UTC · original
I hand-decoded the generated bytes rather than trusting the comments, and they are right:
F3 44 0F 7Fismovdqu [rsp+disp], xmmwith REX.R only forreg >= 8,F3 44 0F 7Ezeroes 127:64 exactly as the emulation'sd.High = 0(crates/victory-loader/src/console_insn.rs:706) requires, and thelea rsp,[rsp-32]frame puts dst at[rsp+8]and src at[rsp+24]as documented, with every disp8 in range. The field gate (len & 63,idx & 63,bits == 64for 0) matchescrates/victory-loader/src/console_insn_decode.rs:50-54,70-71, so the stub reproduces the same rules the decoder applies. Stub-before-jump, thenpatch()'s cmpxchg16b verify, then the jump withint3padding inside one aligned 16 bytes — that ordering is right, as is the re-entry guard for a thread that faulted on the old instruction.Three things before merge, none of them deep:
/// Room for [clzero_stub]s near the title's codeatconsole_insn.rs:201, andnear()at:205, now describe an arena that also holdsinsertq_stubs. Reword to "stubs" — the allocation andnext_multiple_of(16)accounting is shared now, and the doc will be read as a promise aboutclzeroalone.VICTORY_INSERTQ_PATCH(console_insn.rs:198) is the only escape hatch on this path that is missing from the "Runtime-only settings" table indocs/SETTINGS.md, and it is the one thing that lets a user compare patched against emulated runs.documented_switches_are_readonly enforces docs -> source, so nothing fails today.dst == src, which the code handles correctly (both copies are taken before either is written) but which production will hit. A(1, 1)case costs nothing.The 40 bytes below
rspare fine on Windows x64 — there is no red zone to violate, and a guard page there just grows the stack — but it is the one place where leaf-function behaviour is assumed rather than shown.@TreXito on GitHub · 2026-12-04 11:48 UTC · original
Thanks both. Fixed in
b0d7eae:emulatenow copies the faulting bytes into a local array once, and both the check for another thread's jump anddecode::decoderead that copy, so a patch landing between them can no longer end in the handler returning 0.patch_insertqandpatch_clzerocheck the 16-byte fit before taking theSTUBSlock, so a site that crosses its 16 bytes no longer builds a stub on every fault before it is rejected.usedgrows only oncepatchsucceeds. A lost race leaves its stub bytes unreferenced, and the next stub reuses that space.STUBSdoc now names both stubs.xmm1, xmm1at three field shapes) and checks it againstdecode::insertq(d, d, ..).VICTORY_INSERTQ_PATCH=0is now in the runtime-only settings table.On the 40 bytes under
rsp: Windows x64 has no red zone. The ABI treats memory belowrspas volatile, and the OS can overwrite it at any time on the same stack (exception dispatch, APCs), so the title cannot keep anything there. That matches what Luxuride says above.Checked after the change:
victory-loaderlib tests: 85 of 85 pass under Wine, including the new same-register cases.AI-assisted: yes
@Kal0s-design on GitHub · 2026-12-04 13:57 UTC · original
Re-tested
b0d7eaenatively on Windows 11 (MSVC toolchain, not under Wine).cargo test -p victory-loader --lib -- --test-threads=2gives 85 passed, 0 failed, 0 ignored, twice.an_insertq_stub_does_what_the_emulation_doespassed 20 of 20 runs on its own.What I checked in the new commit.
emulate(console_insn.rs:526-529) readsnbytes into a 16 byte array, andnstill comes from the samereadable(rip, n)probe as before (:514,readableat:436-449checks the whole range lies in one committed region). So it reads no further than main's slice could, and the0xe9check anddecode::decodenow see the same bytes.:157for insertq,:246for clzero), andusedonly grows whenpatchreturns true (:194-196,:268-270). Because theSTUBSlock is held from:161through the patch, two threads never build at the same offset at once, so a loser's stub can only be overwritten after a winner has already movedusedpast its own stub. I checked this with a local test I'm not pushing: 6 threads racingpatch_insertqon 8 fake sites per round, 120 rounds. Every site had exactly one winner, every jump pointed belowusedat a byte-for-byte intact stub of its own, running the sites (old rounds included) gavedecode::insertq, andusedended at 46,080 for 960 stubs, so 48 each with nothing lost to the 4,800 failed attempts. As a control, leavingusedunbumped makes the same test fail on the first site.:824-848) checks againstdecode::insertq(d, d, ..), which is what the emulation does, since it copies the source before writing the destination (:708,:715). As a control I added apxorof the destination right after its copy, before the source is saved. The eight old cases still passed and the new one failed atxmm1, xmm1, len 8 idx 8, so it catches exactly that ordering mistake.VICTORY_INSERTQ_PATCH=0is indocs/SETTINGS.md:155.Agree on the red zone, nothing more from me there. Looks good to me.
AI-assisted: yes
View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.