fix(relinker): restore rbx in TLS coverage bodies run on the host #6929

Merged
jotitadev merged 1 commit from upstream-pr/6929 into main 2026-10-23 07:44:47 +00:00
jotitadev commented 2026-10-16 19:29:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-16T19:29:30Z.


What

tls_function_coverage_alu_execution and tls_function_coverage_moved_branch segfault on Linux x86-64 with Python 3.9:

AssertionError: ('alu-exec-xor-3', -11, b'')
AssertionError: ('moved-rel8-branch', -11, b'')

The fixture bodies in both groups write rbx (alu_check_body loads it as one of the ALU operands, the moved-branch prologue does mov rbx, -16) and return to the host without restoring it. rbx is callee-saved in both the SysV and the Windows x64 ABI. On Linux the converted image is entered from windows_image_runner.py through ctypes, and Python 3.9's caller uses rbx after the call. Python 3.14 happens not to, which is why the hosted matrix stays green.

This change saves rbx on entry and restores it before every ret in those two fixtures, the same way register_load_body, register_alu_body and displacement_load already save the callee-saved registers they use. The extra push shifts the patched TLS address (0x1240 -> 0x1241 for ALU execution, 0x124A -> 0x124B for moved branches) and the branch targets by one, and the pop before each ret grows the short branch distances by one. The cases themselves (registers, operands, branch encodings, expected stubs) are unchanged.

I fixed the fixtures rather than adding a register-saving trampoline to windows_image_runner.py: the bodies are the code breaking the ABI, the runner stays a plain call, and Windows runs the same images natively, where the bodies should also be well-behaved.

Tested

Intel i7-8700, Ubuntu 26.04, GCC 15.2, CMake 4.2.3, relinker-only Release build (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON), once with -DPython3_EXECUTABLE=<CPython 3.9.25> and once with the system Python 3.14.4.

  • On main, Python 3.9: both groups fail with the segfaults above. Python 3.14: both pass.
  • With this change: ctest -L tls_function_coverage 31/31, three runs each with Python 3.9 and 3.14.
  • Full relinker-only ctest:
    • Python 3.9: main 175/182, this branch 177/182. The five left are unrelated: linux_fixed_base (#6843), pr_overlap_tools and release_package (#6734, #6728), file_writer_binary/file_writer_text (this machine's coreutils, they fail with 3.14 too).
    • Python 3.14: 180/182 on both, the same two file_writer_*.
  • python3 tools/check_conventions.py --base origin/main passes.

Not checked: Windows and macOS (Windows runs the same fixtures natively and the change keeps the bodies ABI-correct there; macOS does not execute these cases).

Rebased onto main 1b1f8483c; the files this changes are unchanged since the tested base.

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 under Silent stubs in their library's technical debt file
  • 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
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4893278440 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6929). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-16T19:29:30Z. --- ### What `tls_function_coverage_alu_execution` and `tls_function_coverage_moved_branch` segfault on Linux x86-64 with Python 3.9: ``` AssertionError: ('alu-exec-xor-3', -11, b'') AssertionError: ('moved-rel8-branch', -11, b'') ``` The fixture bodies in both groups write `rbx` (`alu_check_body` loads it as one of the ALU operands, the moved-branch prologue does `mov rbx, -16`) and return to the host without restoring it. `rbx` is callee-saved in both the SysV and the Windows x64 ABI. On Linux the converted image is entered from `windows_image_runner.py` through ctypes, and Python 3.9's caller uses `rbx` after the call. Python 3.14 happens not to, which is why the hosted matrix stays green. This change saves `rbx` on entry and restores it before every `ret` in those two fixtures, the same way `register_load_body`, `register_alu_body` and `displacement_load` already save the callee-saved registers they use. The extra `push` shifts the patched TLS address (`0x1240` -> `0x1241` for ALU execution, `0x124A` -> `0x124B` for moved branches) and the branch targets by one, and the `pop` before each `ret` grows the short branch distances by one. The cases themselves (registers, operands, branch encodings, expected stubs) are unchanged. I fixed the fixtures rather than adding a register-saving trampoline to `windows_image_runner.py`: the bodies are the code breaking the ABI, the runner stays a plain call, and Windows runs the same images natively, where the bodies should also be well-behaved. ### Tested Intel i7-8700, Ubuntu 26.04, GCC 15.2, CMake 4.2.3, relinker-only Release build (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`), once with `-DPython3_EXECUTABLE=<CPython 3.9.25>` and once with the system Python 3.14.4. - On `main`, Python 3.9: both groups fail with the segfaults above. Python 3.14: both pass. - With this change: `ctest -L tls_function_coverage` 31/31, three runs each with Python 3.9 and 3.14. - Full relinker-only ctest: - Python 3.9: `main` 175/182, this branch 177/182. The five left are unrelated: `linux_fixed_base` (#6843), `pr_overlap_tools` and `release_package` (#6734, #6728), `file_writer_binary`/`file_writer_text` (this machine's coreutils, they fail with 3.14 too). - Python 3.14: 180/182 on both, the same two `file_writer_*`. - `python3 tools/check_conventions.py --base origin/main` passes. Not checked: Windows and macOS (Windows runs the same fixtures natively and the change keeps the bodies ABI-correct there; macOS does not execute these cases). Rebased onto `main` `1b1f8483c`; the files this changes are unchanged since the tested base. ### 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
OskarLGS commented 2026-10-17 21:26:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by OskarLGS on 2026-10-17T21:26:35Z.

Review: COMMENTED


Reproduced and verified on Linux x86-64 (relinker-only build, GCC 13):

  • With Python 3.9.25, the test on current main fails in both groups with SIGSEGV (('alu-exec-xor-3', -11, b''), ('moved-rel8-branch', -11, b'')). With this PR all groups pass under 3.9, both on the branch and merged into current main (which has since changed the same file in 2a65acd86; the merge is clean). Python 3.13 passes either way, as described.
  • Disassembled the three new moved_branch bodies at 0x1240: the TLS access is at 0x124B and the branch targets are 0x1258, 0x125B and 0x1258, matching the new tuple values; every return path pops rbx. In alu_check_body the push puts the TLS store at 0x1241, matching tls_address=0x1241, and len(code) - 7 is the start of the 7-byte failure block.
  • To check that no other executed fixture has the same problem, I dumped all 342 images the test builds and scanned their code for writes to rbx, rbp and r12-r15 without a preceding push. Besides the two groups fixed here, only displacement_bounds (r12) and the alu_ groups (r13) write a callee-saved register, and neither runs its output: displacement_bounds expects every conversion to fail, and the alu_ groups only inspect the stub. So the fix covers every executed body.
  • check_conventions.py passes.

Review done with AI assistance (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5560170941 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6929#pullrequestreview-5560170941). Originally posted by [OskarLGS](https://github.com/OskarLGS) on 2026-10-17T21:26:35Z. **Review:** COMMENTED --- Reproduced and verified on Linux x86-64 (relinker-only build, GCC 13): - With Python 3.9.25, the test on current main fails in both groups with SIGSEGV (`('alu-exec-xor-3', -11, b'')`, `('moved-rel8-branch', -11, b'')`). With this PR all groups pass under 3.9, both on the branch and merged into current main (which has since changed the same file in 2a65acd86; the merge is clean). Python 3.13 passes either way, as described. - Disassembled the three new moved_branch bodies at 0x1240: the TLS access is at 0x124B and the branch targets are 0x1258, 0x125B and 0x1258, matching the new tuple values; every return path pops rbx. In alu_check_body the push puts the TLS store at 0x1241, matching `tls_address=0x1241`, and `len(code) - 7` is the start of the 7-byte failure block. - To check that no other executed fixture has the same problem, I dumped all 342 images the test builds and scanned their code for writes to rbx, rbp and r12-r15 without a preceding push. Besides the two groups fixed here, only displacement_bounds (r12) and the alu_<op> groups (r13) write a callee-saved register, and neither runs its output: displacement_bounds expects every conversion to fail, and the alu_<op> groups only inspect the stub. So the fix covers every executed body. - check_conventions.py passes. Review done with AI assistance (Claude Code).
LeandroLP23 commented 2026-10-18 17:10:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-18T17:10:44Z.


Windows host run, since the description lists Windows as not checked. Radeon 860M (RDNA 3.5, driver 32.0.13058.8001), Windows 11 26300, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. Build: main 6efb7261b with #7524, #7006, #6612, #6929, #7119, #6896 and #6856 merged together (each merge clean, built once).

tls_function_coverage_alu_execution and tls_function_coverage_moved_branch pass 3 runs of 3 on the Windows host.

AI-assisted

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6252730157 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6929#issuecomment-6252730157). Originally posted by [LeandroLP23](https://github.com/LeandroLP23) on 2026-10-18T17:10:44Z. --- Windows host run, since the description lists Windows as not checked. Radeon 860M (RDNA 3.5, driver 32.0.13058.8001), Windows 11 26300, WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`. Build: `main` `6efb7261b` with #7524, #7006, #6612, #6929, #7119, #6896 and #6856 merged together (each merge clean, built once). `tls_function_coverage_alu_execution` and `tls_function_coverage_moved_branch` pass 3 runs of 3 on the Windows host. AI-assisted
MarcZX10 commented 2026-10-21 23:36:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T23:36:47Z.

Review: APPROVED


The test fixture was violating the x64 callee-saved register contract: both groups modify RBX and return through the host caller. The added push/pop pairs cover every return path, and the updated TLS addresses and relative branch targets match the one-byte prologue shift. Independent Linux reproduction and disassembly checks confirm the patched addresses and restores; external Windows runs also pass. The relinker matrix, conventions and intake checks are green. I did not run the tests locally. No blocking issue found; approved.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5607530549 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6929#pullrequestreview-5607530549). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T23:36:47Z. **Review:** APPROVED --- The test fixture was violating the x64 callee-saved register contract: both groups modify RBX and return through the host caller. The added push/pop pairs cover every return path, and the updated TLS addresses and relative branch targets match the one-byte prologue shift. Independent Linux reproduction and disassembly checks confirm the patched addresses and restores; external Windows runs also pass. The relinker matrix, conventions and intake checks are green. I did not run the tests locally. No blocking issue found; approved.
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!6929
No description provided.