feat(relinker): support Windows guest TLS reads by ALU instructions #767

Merged
011010 merged 3 commits from upstream-pr/767 into main 2026-10-06 03:35:21 +00:00
011010 commented 2026-10-05 22:25:43 +00:00 (Migrated from github.com)

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 check xor rbx, fs:[0x28] reported in #217 after #340. Accept the eight read-direction forms add (0x03), or (0x0b), adc (0x13), sbb (0x1b), and (0x23), sub (0x2b), xor (0x33) and cmp (0x3b) encoded as OP r64, fs:[disp32] with REX.W, ModRM mod=00, r/m=100 and SIB 0x25.

The input shape matches the existing mov r64, fs:[disp32] load: reject rsp destinations 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 (-blockSize to -1) or the stack guard at 0x28.

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 rax uses rdx as the value scratch so the original accumulator survives the read.

The coverage test moves cmp/sub/add/xor out of the rejected list and adds byte-level checks for the eight opcodes across destinations rax, rbx, r8, r13 and displacements 0, 0x28, -8, verifying the value-load and operation bytes in the generated stub. rsp ALU destinations stay rejected.

Tested

Base: bc24783 on main. No other open PR touches WindowsTlsBuilder.cpp or 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.txt and all 18 relinker Python tests pass. The coverage test was run red against unmodified main (fails with Unsupported Windows guest TLS instruction at 0x1240) 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 contains movq 0x28(%rax), %rax followed by xorq %rax, %rbx, and the rax and cmp destinations 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

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4751491053 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/767). Originally posted by [011010](https://github.com/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 check `xor rbx, fs:[0x28]` reported in #217 after #340. Accept the eight read-direction forms `add` (`0x03`), `or` (`0x0b`), `adc` (`0x13`), `sbb` (`0x1b`), `and` (`0x23`), `sub` (`0x2b`), `xor` (`0x33`) and `cmp` (`0x3b`) encoded as `OP r64, fs:[disp32]` with REX.W, ModRM `mod=00, r/m=100` and SIB `0x25`. The input shape matches the existing `mov r64, fs:[disp32]` load: reject `rsp` destinations 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 (`-blockSize` to `-1`) or the stack guard at `0x28`. 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 `rax` uses `rdx` as the value scratch so the original accumulator survives the read. The coverage test moves `cmp`/`sub`/`add`/`xor` out of the rejected list and adds byte-level checks for the eight opcodes across destinations `rax`, `rbx`, `r8`, `r13` and displacements `0`, `0x28`, `-8`, verifying the value-load and operation bytes in the generated stub. `rsp` ALU destinations stay rejected. ### Tested Base: `bc24783` on `main`. No other open PR touches `WindowsTlsBuilder.cpp` or 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.txt` and all 18 relinker Python tests pass. The coverage test was run red against unmodified `main` (fails with `Unsupported Windows guest TLS instruction` at `0x1240`) 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 contains `movq 0x28(%rax), %rax` followed by `xorq %rax, %rbx`, and the `rax` and `cmp` destinations 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 - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
oneandonlydean commented 2026-10-05 23:37:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T23:37:32Z.


Thanks for this. The decode side and the rbx case 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, because loadPointer overwrites rax and rcx (mov ecx, [index], mov rax, gs:[0x58], mov rax, [rax+rcx*8], lea rax, [rax+blockSize]):

  • rax (WindowsTlsBuilder.cpp:165): the stub saves rdx and rcx, but not rax, then calls loadPointer. So 48 <op> c2 computes <TLS block pointer> op value instead 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 (the else branch, preserveCounter = access.Register != 1 at line 176): for a plain load, skipping the save is fine because rcx is overwritten. For an ALU read, rcx is also a source, and by the time of <op> rcx, rax it holds the TLS index from loadPointer.

One way to handle every destination is to save rcx and rax (and rdx for rax), 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 rcx
  • rcx: push rcx; push rax; loadPointer; mov rax, [rax+disp]; mov rcx, [rsp+8]; <op> rcx, rax; pop rax; lea rsp, [rsp+8]

pop, mov and lea leave 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 rcx isn't in the register list. convert() already runs the relinked image on Windows (returncode == 42), so a few execution cases in the style of displacement_load(..., round_trip=True), with known values in rax, rcx and rbx and a check of the result and of ZF/CF after cmp/sub, would catch this on the Windows CI job.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6005562591 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/767#issuecomment-6005562591). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T23:37:32Z. --- Thanks for this. The decode side and the `rbx` case 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, because `loadPointer` overwrites `rax` and `rcx` (`mov ecx, [index]`, `mov rax, gs:[0x58]`, `mov rax, [rax+rcx*8]`, `lea rax, [rax+blockSize]`): - **`rax`** (`WindowsTlsBuilder.cpp:165`): the stub saves `rdx` and `rcx`, but not `rax`, then calls `loadPointer`. So `48 <op> c2` computes `<TLS block pointer> op value` instead 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`** (the `else` branch, `preserveCounter = access.Register != 1` at line 176): for a plain load, skipping the save is fine because `rcx` is overwritten. For an ALU read, `rcx` is also a source, and by the time of `<op> rcx, rax` it holds the TLS index from `loadPointer`. One way to handle every destination is to save `rcx` and `rax` (and `rdx` for `rax`), 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 rcx` - `rcx`: `push rcx; push rax; loadPointer; mov rax, [rax+disp]; mov rcx, [rsp+8]; <op> rcx, rax; pop rax; lea rsp, [rsp+8]` `pop`, `mov` and `lea` leave 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 `rcx` isn't in the register list. `convert()` already runs the relinked image on Windows (`returncode == 42`), so a few execution cases in the style of `displacement_load(..., round_trip=True)`, with known values in `rax`, `rcx` and `rbx` and a check of the result and of ZF/CF after `cmp`/`sub`, would catch this on the Windows CI job.
011010 commented 2026-10-05 23:51:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by 011010 on 2026-10-05T23:51:56Z.


Fixed both, thanks. loadPointer overwrites rax and rcx, so a destination of rax or rcx used the thread pointer or the TLS index as the first operand:

  • rax: push rcx; push rdx; push rax; loadPointer; mov rdx, [rax+disp]; pop rax; <op> rax, rdx; pop rdx; pop rcx
  • rcx: push 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):

  • Byte-level: rcx was not covered before. The cases now include rcx and rdx and assert the exact preservation sequence, so the previous stub fails them (... 58 48 <op> c2 for rax, ... 48 8b 4c 24 08 48 <op> c8 for rcx).
  • Execution: self-checking bodies for rax, rcx and rbx in the style of displacement_load. Each stores a canary at fs: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; xor for rax, mov rcx, [rsp+8]; xor for rcx). No Windows host here, so the execution cases themselves were not run locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6005816051 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/767#issuecomment-6005816051). Originally posted by [011010](https://github.com/011010) on 2026-10-05T23:51:56Z. --- Fixed both, thanks. `loadPointer` overwrites rax and rcx, so a destination of rax or rcx used the thread pointer or the TLS index as the first operand: - rax: `push rcx; push rdx; push rax; loadPointer; mov rdx, [rax+disp]; pop rax; <op> rax, rdx; pop rdx; pop rcx` - rcx: `push 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`): - Byte-level: rcx was not covered before. The cases now include rcx and rdx and assert the exact preservation sequence, so the previous stub fails them (`... 58 48 <op> c2` for rax, `... 48 8b 4c 24 08 48 <op> c8` for rcx). - Execution: self-checking bodies for rax, rcx and rbx in the style of `displacement_load`. Each stores a canary at `fs: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; xor` for rax, `mov rcx, [rsp+8]; xor` for rcx). No Windows host here, so the execution cases themselves were not run locally.
oneandonlydean commented 2026-10-06 01:12:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-06T01:12:46Z.


Thanks, both look right now. I went through 31e9e67: in the rax path the destination is restored before the op and rdx carries the value, and in the rcx path the original rcx is reloaded from [rsp+8] and the slot is dropped with lea, so nothing after the op touches the flags. loadPointer has 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 after sub/cmp) are what the Windows job needed.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6007249657 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/767#issuecomment-6007249657). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-06T01:12:46Z. --- Thanks, both look right now. I went through `31e9e67`: in the `rax` path the destination is restored before the op and `rdx` carries the value, and in the `rcx` path the original `rcx` is reloaded from `[rsp+8]` and the slot is dropped with `lea`, so nothing after the op touches the flags. `loadPointer` has 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 after `sub`/`cmp`) are what the Windows job needed.
011010 commented 2026-10-06 01:43:38 +00:00 (Migrated from github.com)

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_cases derived ZF from result, but for cmp the destination is left untouched, so result is the operand and not the difference. The cmp-equal case 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 for sub/cmp and from the actual sum for add; CF is unchanged. The stub in WindowsTlsBuilder.cpp needed 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, rcx and rbx with the ZF/CF checks. The re-run is pending workflow approval.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6007605738 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/767#issuecomment-6007605738). Originally posted by [011010](https://github.com/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_cases` derived ZF from `result`, but for `cmp` the destination is left untouched, so `result` is the operand and not the difference. The `cmp-equal` case 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 for `sub`/`cmp` and from the actual sum for `add`; CF is unchanged. The stub in `WindowsTlsBuilder.cpp` needed 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`, `rcx` and `rbx` with the ZF/CF checks. The re-run is pending workflow approval.
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
boykopovar/AnyPS5!767
No description provided.