fix(relinker): tolerate trailing data in TLS scan #299

Merged
Pouare514 merged 3 commits from upstream-pr/299 into main 2026-10-04 23:44:42 +00:00
Pouare514 commented 2026-10-01 23:33:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-01T23:33:45Z.


What

The TLS code scan over unwind function ranges failed the whole relink when a range ended with undecodable bytes. TLOU Part I has an FDE range ending with trailing jump-table data, so the scan now tolerates a short tail (at most 15 bytes, the longest x64 instruction) without an FS prefix instead of aborting. A branch target that lands inside a skipped tail is still rejected, and an overlapping instruction that starts with, or lands inside, an FS-prefixed instruction is rejected as overlapping boundaries (the 0x64 scan now starts at the overlapping address instead of after the previous instruction).

Tested

  • Windows, test_tls_function_coverage.py against the rebuilt relinker: all pass, including the restored overlapping-entry expectation (Code analysis: overlapping instruction boundaries) and the new overlapping-fs-prefix case (FS-prefixed instruction starting inside the previous one is rejected).
  • ctest empty_tls, tls_function_coverage, guest_tls: 3/3 pass.

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:4710857054 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/299). Originally posted by [Pouare514](https://github.com/Pouare514) on 2026-10-01T23:33:45Z. --- ### What The TLS code scan over unwind function ranges failed the whole relink when a range ended with undecodable bytes. TLOU Part I has an FDE range ending with trailing jump-table data, so the scan now tolerates a short tail (at most 15 bytes, the longest x64 instruction) without an FS prefix instead of aborting. A branch target that lands inside a skipped tail is still rejected, and an overlapping instruction that starts with, or lands inside, an FS-prefixed instruction is rejected as overlapping boundaries (the 0x64 scan now starts at the overlapping address instead of after the previous instruction). ### Tested - Windows, `test_tls_function_coverage.py` against the rebuilt relinker: all pass, including the restored `overlapping-entry` expectation (`Code analysis: overlapping instruction boundaries`) and the new `overlapping-fs-prefix` case (FS-prefixed instruction starting inside the previous one is rejected). - ctest `empty_tls`, `tls_function_coverage`, `guest_tls`: 3/3 pass. ### 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 - [x] Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt - [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 - [ ] Depends on: none - [x] AI-assisted: yes
oneandonlydean commented 2026-10-04 05:40:22 +00:00 (Migrated from github.com)

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


Thanks, the tail rule reads as sound to me: an FS instruction starting in the skipped bytes would have its 0x64 byte there, so the no-0x64 check plus the branch check covers it. The overlap part has a hole though: the 0x64 search starts at previousEnd, so an FS-prefixed instruction that starts inside the previous one (0x64 at address, before previousEnd) is accepted, stays in the instruction set, and its TLS patch would overwrite bytes of the earlier instruction. Searching from address to spanEnd would close it. Could you also restore the PR description (it stops after the first sentence) with the Tested section and the checklist, and add a new test case rather than changing what overlapping-entry expects?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977005867 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/299#issuecomment-5977005867). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T05:40:22Z. --- Thanks, the tail rule reads as sound to me: an FS instruction starting in the skipped bytes would have its 0x64 byte there, so the no-0x64 check plus the branch check covers it. The overlap part has a hole though: the 0x64 search starts at previousEnd, so an FS-prefixed instruction that starts inside the previous one (0x64 at `address`, before previousEnd) is accepted, stays in the instruction set, and its TLS patch would overwrite bytes of the earlier instruction. Searching from `address` to spanEnd would close it. Could you also restore the PR description (it stops after the first sentence) with the Tested section and the checklist, and add a new test case rather than changing what overlapping-entry expects?
Pouare514 commented 2026-10-04 13:42:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-04T13:42:35Z.


Done: the 0x64 scan now starts at the overlapping address, plus a prefix scan of the overlapped previous instruction so a branch landing inside an FS-prefixed instruction (like overlapping-entry) is still rejected as overlapping boundaries. overlapping-entry expectation restored, new overlapping-fs-prefix case added. ctest empty_tls/tls_function_coverage/guest_tls 3/3 green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5980623393 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/299#issuecomment-5980623393). Originally posted by [Pouare514](https://github.com/Pouare514) on 2026-10-04T13:42:35Z. --- Done: the 0x64 scan now starts at the overlapping address, plus a prefix scan of the overlapped previous instruction so a branch landing inside an FS-prefixed instruction (like overlapping-entry) is still rejected as overlapping boundaries. overlapping-entry expectation restored, new overlapping-fs-prefix case added. ctest empty_tls/tls_function_coverage/guest_tls 3/3 green.
oneandonlydean commented 2026-10-04 21:07:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T21:07:43Z.


Thanks, starting the scan at address closes the hole, and the new case covers it. One more thing about the check on the previous instruction: it looks for 0x64 only in its first 4 bytes, but WindowsTlsBuilder patches any instruction whose decoded SegmentPrefix is 0x64 and accepts any number of 0x66 prefixes before it. So a previous instruction like 66 66 66 66 64 48 8b 04 25 ... passes this check and still gets patched. Decoding the previous instruction and checking its SegmentPrefix matches exactly what gets patched. With that it looks ready to merge from my side. The red Windows job is the guest_audio_in_no_device flake that #532 fixed.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5984393730 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/299#issuecomment-5984393730). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T21:07:43Z. --- Thanks, starting the scan at `address` closes the hole, and the new case covers it. One more thing about the check on the previous instruction: it looks for 0x64 only in its first 4 bytes, but WindowsTlsBuilder patches any instruction whose decoded `SegmentPrefix` is 0x64 and accepts any number of 0x66 prefixes before it. So a previous instruction like `66 66 66 66 64 48 8b 04 25 ...` passes this check and still gets patched. Decoding the previous instruction and checking its `SegmentPrefix` matches exactly what gets patched. With that it looks ready to merge from my side. The red Windows job is the guest_audio_in_no_device flake that #532 fixed.
Pouare514 commented 2026-10-04 22:57:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Pouare514 on 2026-10-04T22:57:25Z.


Done: the previous-instruction check now decodes it and compares SegmentPrefix to 0x64, matching exactly what WindowsTlsBuilder patches (any number of 0x66 prefixes included). Added an overlapping-fs-prefix-66 case with four 0x66 prefixes; verified it fails without the fix and passes with it. Also dropped the stray libSceAudioIn hunk from this branch.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5985361702 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/299#issuecomment-5985361702). Originally posted by [Pouare514](https://github.com/Pouare514) on 2026-10-04T22:57:25Z. --- Done: the previous-instruction check now decodes it and compares SegmentPrefix to 0x64, matching exactly what WindowsTlsBuilder patches (any number of 0x66 prefixes included). Added an overlapping-fs-prefix-66 case with four 0x66 prefixes; verified it fails without the fix and passes with it. Also dropped the stray libSceAudioIn hunk from this branch.
oneandonlydean commented 2026-10-04 23:43:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T23:43:40Z.


Thanks, decoding the previous instruction and comparing its SegmentPrefix with 0x64 is exactly the set WindowsTlsBuilder patches, and overlapping-fs-prefix-66 covers the four 0x66 case. tls_function_coverage and empty_tls pass here on this head merged with current main, and CI is green. Ready to merge from my side.

A small note, no action needed: the libSceAudioIn line in this branch is now the same as main's #532, so it merges to nothing. It only shows up in the diff because the branch is based before #532.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5985700119 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/299#issuecomment-5985700119). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T23:43:40Z. --- Thanks, decoding the previous instruction and comparing its `SegmentPrefix` with 0x64 is exactly the set WindowsTlsBuilder patches, and `overlapping-fs-prefix-66` covers the four 0x66 case. `tls_function_coverage` and `empty_tls` pass here on this head merged with current main, and CI is green. Ready to merge from my side. A small note, no action needed: the `libSceAudioIn` line in this branch is now the same as main's #532, so it merges to nothing. It only shows up in the diff because the branch is based before #532.
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!299
No description provided.