fix(relinker): load Windows guest TLS values at signed displacements #340

Merged
QDarkRex merged 4 commits from upstream-pr/340 into main 2026-10-04 07:39:51 +00:00
QDarkRex commented 2026-10-02 09:45:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by QDarkRex on 2026-10-02T09:45:03Z.


What

The relinker rejects mov r64, fs:[disp32] when the displacement is nonzero, including the canary load reported in #217.
Accept the absolute disp32 form for every destination except rsp. Preserve the existing thread-pointer result for displacement zero. For nonzero displacements, read a 64-bit value at the thread pointer plus the signed displacement. Reuse the same thread pointer as the existing dword store at fs:0x28. Preserve every other register, flags, stack position and red zone.
After scanning the accesses and determining the aligned TLS block size, accept a nonzero load only if its displacement lies in the TLS block (-blockSize to -1) or is the stack guard at 0x28. Loads at fs:1 through fs:0x27 and beyond fail the relink: on the FreeBSD layout they read fields such as tcb_dtv (fs:8) and tcb_thread (fs:0x10), which are not modelled. The relink error names the signed displacement and reports the instruction's input-file offset; no stub is emitted for a rejected load.
Only the fs:0 self pointer is initialized in the thread-control block. The fs:0x28 stack-protector canary reads as zero unless guest code stores it; loads and stores at fs:0x28 agree, so canary checks pass but do not detect corruption.
Follow-up work: FS cmp, sub, add and xor operands, 32-bit loads, FS register-address forms, GS accesses and loads of the other thread-control block fields remain rejected.

Tested

Base: e1654a97; independent of the diagnostics branch.
Windows 11 Home 10.0.26300, MinGW-w64 GCC 15.2.0, Release, Python 3.14.6: all 15 relinker tests pass. The extended TLS coverage test executes 180 relinked fixtures: every allowed destination, two flag patterns, displacements 0, 40, -64, -40 and -8, and a dword-store/64-bit-load round trip at 40. Fixtures verify loaded values, all other general registers, flags, stack position and the red zone.
Twenty-two bounds fixtures cover displacements 1, 8, 0x10, 0x20, 0x27, 0x29, 0x2c, 0x30, -65 for a 64-byte aligned block and the signed disp32 extremes. Each requires the displacement-specific diagnostic and (offset 0x1240), the instruction's file offset. The extended test fails against the previous revision of this PR (508a971), which accepted fs:1 through fs:0x27, and against 102e042, which reported the image RVA 0x11240. Rejection guards cover rsp, REX.B/REX.X variants and the out-of-scope forms.
Non-Windows runs verify relinking and stub bytes; loaded values are verified by executing the relinked fixtures on Windows. Each nonzero-displacement case checks for 48 8b 80 followed by the little-endian displacement in its generated stub. Bounds cases also cover a PT_TLS header appearing after the executable segment.
Linux: Ubuntu 24.04, GCC 13.3, Release: all 15 relinker tests pass.
Synthetic fixtures only; 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:4715010136 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/340). Originally posted by [QDarkRex](https://github.com/QDarkRex) on 2026-10-02T09:45:03Z. --- ### What The relinker rejects `mov r64, fs:[disp32]` when the displacement is nonzero, including the canary load reported in #217. Accept the absolute disp32 form for every destination except `rsp`. Preserve the existing thread-pointer result for displacement zero. For nonzero displacements, read a 64-bit value at the thread pointer plus the signed displacement. Reuse the same thread pointer as the existing dword store at `fs:0x28`. Preserve every other register, flags, stack position and red zone. After scanning the accesses and determining the aligned TLS block size, accept a nonzero load only if its displacement lies in the TLS block (`-blockSize` to `-1`) or is the stack guard at `0x28`. Loads at `fs:1` through `fs:0x27` and beyond fail the relink: on the FreeBSD layout they read fields such as `tcb_dtv` (`fs:8`) and `tcb_thread` (`fs:0x10`), which are not modelled. The relink error names the signed displacement and reports the instruction's input-file offset; no stub is emitted for a rejected load. Only the fs:0 self pointer is initialized in the thread-control block. The fs:0x28 stack-protector canary reads as zero unless guest code stores it; loads and stores at fs:0x28 agree, so canary checks pass but do not detect corruption. Follow-up work: FS `cmp`, `sub`, `add` and `xor` operands, 32-bit loads, FS register-address forms, GS accesses and loads of the other thread-control block fields remain rejected. ### Tested Base: `e1654a97`; independent of the diagnostics branch. Windows 11 Home 10.0.26300, MinGW-w64 GCC 15.2.0, Release, Python 3.14.6: all 15 relinker tests pass. The extended TLS coverage test executes 180 relinked fixtures: every allowed destination, two flag patterns, displacements 0, 40, -64, -40 and -8, and a dword-store/64-bit-load round trip at 40. Fixtures verify loaded values, all other general registers, flags, stack position and the red zone. Twenty-two bounds fixtures cover displacements 1, 8, 0x10, 0x20, 0x27, 0x29, 0x2c, 0x30, -65 for a 64-byte aligned block and the signed disp32 extremes. Each requires the displacement-specific diagnostic and `(offset 0x1240)`, the instruction's file offset. The extended test fails against the previous revision of this PR (`508a971`), which accepted fs:1 through fs:0x27, and against `102e042`, which reported the image RVA `0x11240`. Rejection guards cover `rsp`, REX.B/REX.X variants and the out-of-scope forms. Non-Windows runs verify relinking and stub bytes; loaded values are verified by executing the relinked fixtures on Windows. Each nonzero-displacement case checks for `48 8b 80` followed by the little-endian displacement in its generated stub. Bounds cases also cover a PT_TLS header appearing after the executable segment. Linux: Ubuntu 24.04, GCC 13.3, Release: all 15 relinker tests pass. Synthetic fixtures only; 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-04 05:41:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T05:41:55Z.


This is careful work and the tests are great. One thing on exactness: loads at fs:8 through fs:0x27 are now accepted and return zero, but on FreeBSD amd64 those are tcb_dtv (fs:8) and tcb_thread (fs:0x10), see struct tcb in sys/x86/include/tls.h and _get_curthread in libthr. Code that inlines a curthread read would get null at run time instead of failing the relink. Could you limit the accepted range to the TLS block (negative displacements), 0 and the canary at 0x28, and keep rejecting 8 to 0x27 until those fields are modelled?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977015281 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/340#issuecomment-5977015281). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T05:41:55Z. --- This is careful work and the tests are great. One thing on exactness: loads at fs:8 through fs:0x27 are now accepted and return zero, but on FreeBSD amd64 those are tcb_dtv (fs:8) and tcb_thread (fs:0x10), see `struct tcb` in sys/x86/include/tls.h and `_get_curthread` in libthr. Code that inlines a curthread read would get null at run time instead of failing the relink. Could you limit the accepted range to the TLS block (negative displacements), 0 and the canary at 0x28, and keep rejecting 8 to 0x27 until those fields are modelled?
QDarkRex commented 2026-10-04 07:16:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by QDarkRex on 2026-10-04T07:16:03Z.


Thanks, that's a good catch: returning zero there is worse than failing. Pushed: nonzero loads are now accepted only inside the TLS block or at the stack guard 0x28, and everything from fs:1 through fs:0x27 (and past 0x28) fails the relink with the displacement and file offset. The bounds tests now cover 1, 8, 0x10, 0x20, 0x27, 0x29 and 0x2c; the executed fixtures at 8 and 0x10 are gone, and the fs:0 case now checks its pointer through 0x28. Relinker tests pass on Windows and Linux, and the updated test fails against the previous revision. I've updated the description to match.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977625131 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/340#issuecomment-5977625131). Originally posted by [QDarkRex](https://github.com/QDarkRex) on 2026-10-04T07:16:03Z. --- Thanks, that's a good catch: returning zero there is worse than failing. Pushed: nonzero loads are now accepted only inside the TLS block or at the stack guard 0x28, and everything from fs:1 through fs:0x27 (and past 0x28) fails the relink with the displacement and file offset. The bounds tests now cover 1, 8, 0x10, 0x20, 0x27, 0x29 and 0x2c; the executed fixtures at 8 and 0x10 are gone, and the fs:0 case now checks its pointer through 0x28. Relinker tests pass on Windows and Linux, and the updated test fails against the previous revision. I've updated the description to match.
oneandonlydean commented 2026-10-04 07:39:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T07:39:48Z.


Thanks, that's exactly what I meant: nonzero loads now only reach the TLS block or the stack guard at 0x28, and the bounds cases pin down 1 through 0x27 and past 0x28. It builds and passes the relinker tests here merged with current main, so I'll merge it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977778719 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/340#issuecomment-5977778719). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T07:39:48Z. --- Thanks, that's exactly what I meant: nonzero loads now only reach the TLS block or the stack guard at 0x28, and the bounds cases pin down 1 through 0x27 and past 0x28. It builds and passes the relinker tests here merged with current main, so I'll merge it.
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!340
No description provided.