fix(relinker): refuse branches into trapped guest GS instructions on macOS #7706

Merged
Destroyers212x merged 3 commits from upstream-pr/7706 into main 2026-10-24 10:54:08 +00:00
Destroyers212x commented 2026-10-19 01:52:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Destroyers212x on 2026-10-19T01:52:05Z.


What

#6458 replaces every guest instruction with a GS prefix by UD2 and NOP padding in macOS output. The guest TLS stubs on the same path refuse a direct branch whose target is inside the instruction they rewrite ("Branch enters a guest TLS instruction", MacOsImage.cpp). The GS-trap loop has no such check. So a branch into the middle of a GS-prefixed instruction is converted, and the code at its target is overwritten:

74 01                 je   +1          ; target = X+1
65 b8 2a 00 00 00     X: gs mov eax,42 ; X+1 decodes as b8 2a 00 00 00 = mov eax,42
c3                    ret
  • Before #6458: refused, Unsupported macOS guest TLS instruction (bytes: 65 b8 2a 00 00 00).
  • Current main: converted to 74 01 0f 0b 90 90 90 90 c3. The taken branch now runs 0b 90 90 90 90 c3 (or edx, [rax-0x3c6f6f70], which also consumes the ret) instead of mov eax,42; ret. A path that never touched GS executes a different memory access instead of faulting at the trap.
  • This PR: refused with Branch enters a guest GS instruction, using the same BranchTargets test as the TLS stubs. Traps with no branch into them are unchanged.

test_macos_gs_traps.py gains this case. It fails on current main (the relinker converts the image) and passes with the change.

Overlap: #6802 and #7450 change other parts of MacOsImage.cpp. #7332, the macOS integration branch, carries #6458's trap code without this check. #8063 stops trapping GS instructions with no memory operand, so the regression case here branches into a GS memory access (65 48 8b 0c 25 78 03 00 00); the check passes with this branch and #8063 merged together, apart from a textual conflict in the test file.

Tested

Apple M1, macOS 26.6.2, AppleClang, relinker-only Release builds:

  • Native arm64: 199/199 tests pass.
  • -DCMAKE_OSX_ARCHITECTURES=x86_64, tests under Rosetta: 199/199 pass.
  • test_macos_gs_traps.py fails against the unfixed build and passes with the fix.
  • python3 tools/check_conventions.py --base origin/main passes.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4914708663 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7706). Originally posted by [Destroyers212x](https://github.com/Destroyers212x) on 2026-10-19T01:52:05Z. --- ### What #6458 replaces every guest instruction with a GS prefix by `UD2` and NOP padding in macOS output. The guest TLS stubs on the same path refuse a direct branch whose target is inside the instruction they rewrite ("Branch enters a guest TLS instruction", `MacOsImage.cpp`). The GS-trap loop has no such check. So a branch into the middle of a GS-prefixed instruction is converted, and the code at its target is overwritten: ``` 74 01 je +1 ; target = X+1 65 b8 2a 00 00 00 X: gs mov eax,42 ; X+1 decodes as b8 2a 00 00 00 = mov eax,42 c3 ret ``` - Before #6458: refused, `Unsupported macOS guest TLS instruction (bytes: 65 b8 2a 00 00 00)`. - Current `main`: converted to `74 01 0f 0b 90 90 90 90 c3`. The taken branch now runs `0b 90 90 90 90 c3` (`or edx, [rax-0x3c6f6f70]`, which also consumes the `ret`) instead of `mov eax,42; ret`. A path that never touched GS executes a different memory access instead of faulting at the trap. - This PR: refused with `Branch enters a guest GS instruction`, using the same `BranchTargets` test as the TLS stubs. Traps with no branch into them are unchanged. `test_macos_gs_traps.py` gains this case. It fails on current `main` (the relinker converts the image) and passes with the change. Overlap: #6802 and #7450 change other parts of `MacOsImage.cpp`. #7332, the macOS integration branch, carries #6458's trap code without this check. #8063 stops trapping GS instructions with no memory operand, so the regression case here branches into a GS memory access (`65 48 8b 0c 25 78 03 00 00`); the check passes with this branch and #8063 merged together, apart from a textual conflict in the test file. ### Tested Apple M1, macOS 26.6.2, AppleClang, relinker-only Release builds: - Native arm64: 199/199 tests pass. - `-DCMAKE_OSX_ARCHITECTURES=x86_64`, tests under Rosetta: 199/199 pass. - `test_macos_gs_traps.py` fails against the unfixed build and passes with the fix. - `python3 tools/check_conventions.py --base origin/main` passes. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
AasishDairelSahayaGrinspan commented 2026-10-19 19:35:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AasishDairelSahayaGrinspan on 2026-10-19T19:35:31Z.


Sequencing note: a change that stops trapping a GS prefix on an instruction with no memory operand (65 90, 65 48 89 c8, 65 b8 ...) is up as #8063. With it, this PR's new case 74 01 65 b8 2a 00 00 00 c3 converts instead of being refused, because 65 b8 2a 00 00 00 has no memory operand. A memory-operand form keeps the case, e.g. 74 01 65 8b 04 25 30 00 00 00 c3. If #7706 merges first I will rebase #8063 and use the memory form; if #8063 merges first, this test case will need the same adjustment.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6275621392 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7706#issuecomment-6275621392). Originally posted by [AasishDairelSahayaGrinspan](https://github.com/AasishDairelSahayaGrinspan) on 2026-10-19T19:35:31Z. --- Sequencing note: a change that stops trapping a GS prefix on an instruction with no memory operand (`65 90`, `65 48 89 c8`, `65 b8 ...`) is up as #8063. With it, this PR's new case `74 01 65 b8 2a 00 00 00 c3` converts instead of being refused, because `65 b8 2a 00 00 00` has no memory operand. A memory-operand form keeps the case, e.g. `74 01 65 8b 04 25 30 00 00 00 c3`. If #7706 merges first I will rebase #8063 and use the memory form; if #8063 merges first, this test case will need the same adjustment.
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!7706
No description provided.