feat(relinker): support Windows guest TLS reads by ALU instructions #767
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/767"
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?
Imported from boykopovar/AnyPS5. Originally posted by 011010 on 2026-10-05T22:25:43Z.
What
The relinker rejected read-direction ALU forms that read a qword from
fs:[disp32], such as the stack canary checkxor rbx, fs:[0x28]reported in #217 after #340. Accept the eight read-direction formsadd(0x03),or(0x0b),adc(0x13),sbb(0x1b),and(0x23),sub(0x2b),xor(0x33) andcmp(0x3b) encoded asOP r64, fs:[disp32]with REX.W, ModRMmod=00, r/m=100and SIB0x25.The input shape matches the existing
mov r64, fs:[disp32]load: rejectrspdestinations and the REX.B/REX.X variants. The signed displacement is validated by the same rule as loads, so it must be in the TLS block (-blockSizeto-1) or the stack guard at0x28.The stub loads the value and applies the operation in place. The operation is the last flag-setting instruction, so the guest flags come from it and not from the pointer arithmetic. The accumulator and counter are preserved; a destination of
raxusesrdxas the value scratch so the original accumulator survives the read.The coverage test moves
cmp/sub/add/xorout of the rejected list and adds byte-level checks for the eight opcodes across destinationsrax,rbx,r8,r13and displacements0,0x28,-8, verifying the value-load and operation bytes in the generated stub.rspALU destinations stay rejected.Tested
Base:
bc24783onmain. No other open PR touchesWindowsTlsBuilder.cppor the coverage test.macOS 26.6.2 (arm64), Apple Clang 21, C++20: the relinker was built standalone from the sources listed in
core/relinker/CMakeLists.txtand all 18 relinker Python tests pass. The coverage test was run red against unmodifiedmain(fails withUnsupported Windows guest TLS instructionat0x1240) and green after the change. The modified translation unit compiles clean with-Wall -Wextra -Werror.The generated stub was disassembled with
llvm-objdump(target x86_64) to confirm the emitted instruction is the intended one: for the reported case the stub containsmovq 0x28(%rax), %raxfollowed byxorq %rax, %rbx, and theraxandcmpdestinations decode as expected.Not tested: execution of the relinked Windows executable (no Windows host). Value and flag behaviour is covered at the byte level, not at runtime. NHL 27 was not tested.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T23:37:32Z.
Thanks for this. The decode side and the
rbxcase from #217 look right to me, and keeping the ALU op as the last flag-setting instruction is the right approach. Two destinations get the wrong value, though, becauseloadPointeroverwritesraxandrcx(mov ecx, [index],mov rax, gs:[0x58],mov rax, [rax+rcx*8],lea rax, [rax+blockSize]):rax(WindowsTlsBuilder.cpp:165): the stub savesrdxandrcx, but notrax, then callsloadPointer. So48 <op> c2computes<TLS block pointer> op valueinstead of<original rax> op value. For the usual canary check (mov rax, [rsp+x]; sub/xor/cmp rax, fs:[0x28]; jne __stack_chk_fail) that would fail every check.rcx(theelsebranch,preserveCounter = access.Register != 1at line 176): for a plain load, skipping the save is fine becausercxis overwritten. For an ALU read,rcxis also a source, and by the time of<op> rcx, raxit holds the TLS index fromloadPointer.One way to handle every destination is to save
rcxandrax(andrdxforrax), load the value into a scratch register, restore the destination, then apply the op:rax:push rcx; push rdx; push rax; loadPointer; mov rdx, [rax+disp]; pop rax; <op> rax, rdx; pop rdx; pop rcxrcx:push rcx; push rax; loadPointer; mov rax, [rax+disp]; mov rcx, [rsp+8]; <op> rcx, rax; pop rax; lea rsp, [rsp+8]pop,movandlealeave the flags alone, so the op is still the last flag write.The new byte-level checks pass with the current stub because they only look for the load and op bytes, and
rcxisn't in the register list.convert()already runs the relinked image on Windows (returncode == 42), so a few execution cases in the style ofdisplacement_load(..., round_trip=True), with known values inrax,rcxandrbxand a check of the result and of ZF/CF aftercmp/sub, would catch this on the Windows CI job.Imported from boykopovar/AnyPS5. Originally posted by 011010 on 2026-10-05T23:51:56Z.
Fixed both, thanks.
loadPointeroverwrites rax and rcx, so a destination of rax or rcx used the thread pointer or the TLS index as the first operand:push rcx; push rdx; push rax; loadPointer; mov rdx, [rax+disp]; pop rax; <op> rax, rdx; pop rdx; pop rcxpush rcx; push rax; loadPointer; mov rax, [rax+disp]; mov rcx, [rsp+8]; <op> rcx, rax; pop rax; lea rsp, [rsp+8]The op remains the last flag-setting instruction in both.
Tests (
31e9e67):... 58 48 <op> c2for rax,... 48 8b 4c 24 08 48 <op> c8for rcx).displacement_load. Each stores a canary atfs:0x28, runs the op with known values and checks the result, the preservation of the other two registers and ZF/CF after sub and cmp. On non-Windows they are relinked and checked byte-wise; the execution runs on the Windows CI job.Local: 18/18 relinker tests on macOS 26.6.2 (arm64), Apple Clang 21; the generated stubs disassemble as intended (
pop rax; xorfor rax,mov rcx, [rsp+8]; xorfor rcx). No Windows host here, so the execution cases themselves were not run locally.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-06T01:12:46Z.
Thanks, both look right now. I went through
31e9e67: in theraxpath the destination is restored before the op andrdxcarries the value, and in thercxpath the originalrcxis reloaded from[rsp+8]and the slot is dropped withlea, so nothing after the op touches the flags.loadPointerhas no call in it, so the extra push doesn't matter for alignment. The exact-sequence checks fail against the previous stub, and the execution cases (result, the other two registers, and ZF/CF aftersub/cmp) are what the Windows job needed.Imported from boykopovar/AnyPS5. Originally posted by 011010 on 2026-10-06T01:43:38Z.
Thanks. The Windows job failed with exit code 8, and the cause was the new execution cases, not the stub.
alu_execution_casesderived ZF fromresult, but forcmpthe destination is left untouched, soresultis the operand and not the difference. Thecmp-equalcase therefore expected ZF=0 while the stub correctly produced ZF=1, the self-check jumped to the failure path and returned 1 instead of 42.Fixed in
7edf5d2: ZF now comes from the actual subtraction forsub/cmpand from the actual sum foradd; CF is unchanged. The stub inWindowsTlsBuilder.cppneeded no change.Verified on Windows by building the relinker standalone and running the 18 relinker Python tests: all pass, including the ALU execution cases for
rax,rcxandrbxwith the ZF/CF checks. The re-run is pending workflow approval.