fix(relinker): root code addresses taken by RIP-relative LEA #2238

Merged
mrk1987 merged 1 commit from upstream-pr/2238 into main 2026-10-10 04:45:40 +00:00
mrk1987 commented 2026-10-09 12:45:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mrk1987 on 2026-10-09T12:45:33Z.


What

Windows conversion only patches guest TLS accesses (mov %fs:0x0, ...) in instructions that code analysis reaches. #1117 added roots for code that no root reaches. A function whose only reference is a 64-bit RIP-relative LEA is still missed: a constructor, callback or thread entry whose address is stored and later called indirectly. Its TLS access stays unpatched, and the first call reads FS:0 as a null pointer on Windows.

A REX.W LEA with a RIP-relative displacement that resolves to executable code now adds that target as a code root. This applies both in the main instruction walk and while following the control-flow graph of unreached code. BuildControlFlowGraph gets an opt-in followCodeAddresses flag for the second case. Existing callers keep their behaviour.

A new tls_function_coverage case, address-taken-tls-callback, puts a TLS access in a function that is only reached through lea rax, [rip+fn]; call rax. It fails on current main (the access is not rewritten to a jump) and passes with this change.

Tested

Windows 11, MinGW-w64 GCC (project toolchain), Release/Ninja on upstream main d70b8998.

  • tls_function_coverage passes; on unchanged production code the new case fails with AssertionError: address-taken-tls-callback.
  • Full ctest: 507/507 pass (1 skipped: guest_video_out_wqhd_detection_no_vulkan).
  • tools/check_conventions.py and git diff --check pass.
  • A retail Unreal Engine title: converted with current main, the title crashes early with a null read at a raw mov %fs:0x0, %rax in a function installed by address (OutputDeviceRedirector thread). With this change that site is patched and startup continues.

#1913 splits the same test script into groups. If it merges first, this four-line case moves into one of its groups.

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; no silent stubs added
  • No notes, investigation files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes. The investigation, code, tests and this description were written by Claude (Claude Code), reviewed and run on my machine.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4800587774 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2238). Originally posted by [mrk1987](https://github.com/mrk1987) on 2026-10-09T12:45:33Z. --- ### What Windows conversion only patches guest TLS accesses (`mov %fs:0x0, ...`) in instructions that code analysis reaches. #1117 added roots for code that no root reaches. A function whose only reference is a 64-bit RIP-relative `LEA` is still missed: a constructor, callback or thread entry whose address is stored and later called indirectly. Its TLS access stays unpatched, and the first call reads `FS:0` as a null pointer on Windows. A REX.W `LEA` with a RIP-relative displacement that resolves to executable code now adds that target as a code root. This applies both in the main instruction walk and while following the control-flow graph of unreached code. `BuildControlFlowGraph` gets an opt-in `followCodeAddresses` flag for the second case. Existing callers keep their behaviour. A new `tls_function_coverage` case, `address-taken-tls-callback`, puts a TLS access in a function that is only reached through `lea rax, [rip+fn]; call rax`. It fails on current `main` (the access is not rewritten to a jump) and passes with this change. ### Tested Windows 11, MinGW-w64 GCC (project toolchain), Release/Ninja on upstream main `d70b8998`. - `tls_function_coverage` passes; on unchanged production code the new case fails with `AssertionError: address-taken-tls-callback`. - Full `ctest`: 507/507 pass (1 skipped: `guest_video_out_wqhd_detection_no_vulkan`). - `tools/check_conventions.py` and `git diff --check` pass. - A retail Unreal Engine title: converted with current `main`, the title crashes early with a null read at a raw `mov %fs:0x0, %rax` in a function installed by address (`OutputDeviceRedirector` thread). With this change that site is patched and startup continues. #1913 splits the same test script into groups. If it merges first, this four-line case moves into one of its groups. ### 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; no silent stubs added - [x] No notes, investigation files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes. The investigation, code, tests and this description were written by Claude (Claude Code), reviewed and run on my machine. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-09 15:29:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T15:29:01Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#1913 core/relinker/relinker/tests/test_tls_function_coverage.py L425-562
#2304 core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp L277-279
core/relinker/relinker/src/analysis/UnusedNidFilter/ControlFlowGraph.cpp L26
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6083983616 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2238#issuecomment-6083983616). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T15:29:01Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #1913 | `core/relinker/relinker/tests/test_tls_function_coverage.py` [L425-562](https://github.com/boykopovar/AnyPS5/blob/698254f048952385120dba5b7af7658be2fd49e6/core/relinker/relinker/tests/test_tls_function_coverage.py#L425-L562) | | | | #2304 | `core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp` [L277-279](https://github.com/boykopovar/AnyPS5/blob/698254f048952385120dba5b7af7658be2fd49e6/core/relinker/relinker/src/analysis/CodeInstructionCollector.cpp#L277-L279)<br>`core/relinker/relinker/src/analysis/UnusedNidFilter/ControlFlowGraph.cpp` [L26](https://github.com/boykopovar/AnyPS5/blob/698254f048952385120dba5b7af7658be2fd49e6/core/relinker/relinker/src/analysis/UnusedNidFilter/ControlFlowGraph.cpp#L26) | | |
Kikoarl commented 2026-10-09 17:44:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Kikoarl on 2026-10-09T17:44:23Z.


Linux run: Linux 7.2 (x86_64), GCC 16.2.1, Python 3.14, Release, -DBUILD_TESTING=ON, #2238 (ba525633) tested on current main (0fdda7be).

  • ctest -R "^tls_function_coverage$": passes cleanly in 12.8s (TLS function coverage integration tests passed).
  • tools/check_conventions.py: passes cleanly.
  • Confirms RIP-relative LEA TLS code address rooting under Linux relinker pipeline.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6086157528 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2238#issuecomment-6086157528). Originally posted by [Kikoarl](https://github.com/Kikoarl) on 2026-10-09T17:44:23Z. --- Linux run: Linux 7.2 (x86_64), GCC 16.2.1, Python 3.14, Release, `-DBUILD_TESTING=ON`, #2238 (`ba525633`) tested on current `main` (`0fdda7be`). - `ctest -R "^tls_function_coverage$"`: passes cleanly in 12.8s (`TLS function coverage integration tests passed`). - `tools/check_conventions.py`: passes cleanly. - Confirms RIP-relative LEA TLS code address rooting under Linux relinker pipeline.
reyani-dev commented 2026-10-10 05:35:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-10T05:35:14Z.


Heads-up @mrk1987: rooting LEA targets now reaches CPU-dispatched XOP code (AMD 8F escape), which the x86 decoder doesn't support. The Windows conversion of three catalog titles (Stremio Plus, OpenNOW, PPSA99420) fails with Code analysis: overlapping instruction boundaries since this landed. Details, the bisect and a ready fix branch that adds XOP decoding are in #2559. The change itself is fine; it just exposed the decoder gap.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094260296 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2238#issuecomment-6094260296). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-10T05:35:14Z. --- Heads-up @mrk1987: rooting LEA targets now reaches CPU-dispatched XOP code (AMD `8F` escape), which the x86 decoder doesn't support. The Windows conversion of three catalog titles (Stremio Plus, OpenNOW, PPSA99420) fails with `Code analysis: overlapping instruction boundaries` since this landed. Details, the bisect and a ready fix branch that adds XOP decoding are in #2559. The change itself is fine; it just exposed the decoder gap.
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!2238
No description provided.