fix(relinker): leave GS-prefixed LEA and NOP instructions unchanged #9719

Merged
AasishDairelSahayaGrinspan merged 1 commit from upstream-pr/9719 into main 2026-10-25 23:15:59 +00:00
AasishDairelSahayaGrinspan commented 2026-10-22 09:46:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AasishDairelSahayaGrinspan on 2026-10-22T09:46:45Z.


What

HasMemoryOperand is set for every ModRM memory form, including LEA (8D /r) and the multi-byte NOP (0F 1F /0). Neither accesses memory: LEA computes an effective address into a register and the multi-byte NOP does not issue a memory operation (Intel SDM Vol. 2A, LEA; Vol. 2B, NOP), so a GS prefix on either is ignored. The Windows TLS builder and the macOS image writer trap every GS-prefixed instruction whose HasMemoryOperand is set, replacing it with UD2 and NOP padding, so a converted binary faults on an instruction the console executes. This is a follow-up to #8063, which limited the trap to memory accesses.

objdump still prints %gs: for both encodings (65 8D 05 ... is leal %gs:..., %eax and 65 0F 1F 05 ... is nopl %gs:...), so an audit that equates that text with a memory access counts these two forms as accesses. The instruction definitions above say otherwise: the prefix is decoded and ignored, and no memory is read or written.

  • The decoder clears HasMemoryOperand for LEA and 0F 1F after reading their ModRM byte. Length, HasModRm, RIP-relative displacement and branch metadata are unchanged.
  • DecoderLengthTests gains 65 8D 05 11 11 11 11 and 65 0F 1F 05 11 11 11 11 as non-memory forms.
  • test_macos_gs_traps.py gains the same two forms as untouched cases, and the Windows TLS fixture gains a gs_non_memory group with both. The group is separate so each CTest group stays under its 30 s timeout.
  • No debt entry changes: the existing entries already say that only a GS memory access is trapped.

Tested

macOS 15.7.7, x86_64, AppleClang 17, relinker-only Release build (-DANYPS5_RELINKER_ONLY=ON) on this branch based on main b7d1d47a5f:

  • ctest 231/231 pass, including decoder_length, tls_function_coverage_gs_non_memory and macos_gs_traps.
  • Removing the decoder change (keeping the new fixtures) fails all three: Wrong memory operand presence for 658d0511111111, the Windows gs_non_memory group, and gs-nop-memory: a non-memory GS instruction was replaced on macOS. This covers the decoder and both output writers.
  • The Windows fixtures convert to PE and the patched bytes are read in the test; they were not executed on Windows.
  • python3 tools/check_conventions.py --base origin/main passes. python3 tools/pr_overlap.py --branch fix/relinker-gs-non-memory-operands reports no overlap with open pull requests.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4965383610 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9719). Originally posted by [AasishDairelSahayaGrinspan](https://github.com/AasishDairelSahayaGrinspan) on 2026-10-22T09:46:45Z. --- ### What `HasMemoryOperand` is set for every ModRM memory form, including `LEA` (`8D /r`) and the multi-byte `NOP` (`0F 1F /0`). Neither accesses memory: `LEA` computes an effective address into a register and the multi-byte NOP does not issue a memory operation (Intel SDM Vol. 2A, `LEA`; Vol. 2B, `NOP`), so a GS prefix on either is ignored. The [Windows TLS builder](https://github.com/boykopovar/AnyPS5/blob/main/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp) and the [macOS image writer](https://github.com/boykopovar/AnyPS5/blob/main/core/relinker/elfpatcher/src/macos/MacOsImage.cpp) trap every GS-prefixed instruction whose `HasMemoryOperand` is set, replacing it with `UD2` and NOP padding, so a converted binary faults on an instruction the console executes. This is a follow-up to #8063, which limited the trap to memory accesses. `objdump` still prints `%gs:` for both encodings (`65 8D 05 ...` is `leal %gs:..., %eax` and `65 0F 1F 05 ...` is `nopl %gs:...`), so an audit that equates that text with a memory access counts these two forms as accesses. The instruction definitions above say otherwise: the prefix is decoded and ignored, and no memory is read or written. - The decoder clears `HasMemoryOperand` for `LEA` and `0F 1F` after reading their ModRM byte. Length, `HasModRm`, RIP-relative displacement and branch metadata are unchanged. - `DecoderLengthTests` gains `65 8D 05 11 11 11 11` and `65 0F 1F 05 11 11 11 11` as non-memory forms. - `test_macos_gs_traps.py` gains the same two forms as untouched cases, and the Windows TLS fixture gains a `gs_non_memory` group with both. The group is separate so each CTest group stays under its 30 s timeout. - No debt entry changes: the existing entries already say that only a GS memory access is trapped. ### Tested macOS 15.7.7, x86_64, AppleClang 17, relinker-only Release build (`-DANYPS5_RELINKER_ONLY=ON`) on this branch based on `main` b7d1d47a5f: - `ctest` 231/231 pass, including `decoder_length`, `tls_function_coverage_gs_non_memory` and `macos_gs_traps`. - Removing the decoder change (keeping the new fixtures) fails all three: `Wrong memory operand presence for 658d0511111111`, the Windows `gs_non_memory` group, and `gs-nop-memory: a non-memory GS instruction was replaced` on macOS. This covers the decoder and both output writers. - The Windows fixtures convert to PE and the patched bytes are read in the test; they were not executed on Windows. - `python3 tools/check_conventions.py --base origin/main` passes. `python3 tools/pr_overlap.py --branch fix/relinker-gs-non-memory-operands` reports no overlap with open pull requests. ### 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
jotitadev commented 2026-10-22 10:26:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-22T10:26:14Z.

Review: COMMENTED


Apple silicon data point, since the macOS output now leaves these two forms in place and on Apple silicon they run under Rosetta 2.

Apple M2 Max, macOS 27.2, relinker-only Release build of head f9b2a49ab (base main 7e2ba73e4c), arm64 and x86_64 under Rosetta with /usr/bin/python3 3.9.6:

  • 231/231 on both.
  • Red check: with X64InstructionDecoder.cpp and X64OpcodeConstants.hpp restored to main and the new fixtures kept, decoder_length, tls_function_coverage_gs_non_memory and macos_gs_traps fail on both architectures. With the PR they pass.

Rosetta runs the untouched forms as the SDM says. I used a standalone x86_64 probe (clang -arch x86_64) where sysctl.proc_translated reports 1 and the GS base is non-zero (%gs:0 reads 0x20506be00, the TSD self pointer):

  • 65 48 8D 04 25 10 00 00 00 (lea rax, gs:[0x10]) gives 0x10, the same as the unprefixed form. The GS base is not added.
  • 65 48 8D 07 (lea rax, gs:[rdi]) gives rdi unchanged.
  • 65 48 8D 15 <disp32> right after 48 8D 05 <disp32> gives a result exactly 8 bytes higher, which is the instruction length. That matches plain RIP-relative arithmetic.
  • 65 0F 1F 05 <disp32>, 65 0F 1F 04 25 <disp32> and 65 66 0F 1F 84 00 00 00 00 00 execute without faulting.

So on Apple silicon, a converted Mach-O that keeps these instructions computes the same values the console does.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5612740076 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9719#pullrequestreview-5612740076). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-22T10:26:14Z. **Review:** COMMENTED --- Apple silicon data point, since the macOS output now leaves these two forms in place and on Apple silicon they run under Rosetta 2. Apple M2 Max, macOS 27.2, relinker-only Release build of head f9b2a49ab (base main 7e2ba73e4c), arm64 and x86_64 under Rosetta with `/usr/bin/python3` 3.9.6: - 231/231 on both. - Red check: with `X64InstructionDecoder.cpp` and `X64OpcodeConstants.hpp` restored to main and the new fixtures kept, `decoder_length`, `tls_function_coverage_gs_non_memory` and `macos_gs_traps` fail on both architectures. With the PR they pass. **Rosetta runs the untouched forms as the SDM says.** I used a standalone x86_64 probe (`clang -arch x86_64`) where `sysctl.proc_translated` reports 1 and the GS base is non-zero (`%gs:0` reads `0x20506be00`, the TSD self pointer): - `65 48 8D 04 25 10 00 00 00` (`lea rax, gs:[0x10]`) gives `0x10`, the same as the unprefixed form. The GS base is not added. - `65 48 8D 07` (`lea rax, gs:[rdi]`) gives `rdi` unchanged. - `65 48 8D 15 <disp32>` right after `48 8D 05 <disp32>` gives a result exactly 8 bytes higher, which is the instruction length. That matches plain RIP-relative arithmetic. - `65 0F 1F 05 <disp32>`, `65 0F 1F 04 25 <disp32>` and `65 66 0F 1F 84 00 00 00 00 00` execute without faulting. So on Apple silicon, a converted Mach-O that keeps these instructions computes the same values the console does. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
MarcZX10 commented 2026-10-22 12:02:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T12:02:22Z.

Review: COMMENTED


Reviewed exact head f9b2a49ab676c813724b6c55d036b1d3d83426c6 against current upstream/main. I found no code-level blocker: the decoder change is restricted to legacy LEA and multi-byte NOP opcodes, preserves ModRM decoding/length metadata, and both Windows and macOS output fixtures verify the prefixed bytes remain untouched. Current conventions and diff checks pass; the branch merges cleanly. The described 231-test Linux/Windows/macOS relinker CI plus the Apple Silicon/Rosetta red/green report are directly relevant and detailed; I did not rerun them locally.

I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5613809892 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9719#pullrequestreview-5613809892). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T12:02:22Z. **Review:** COMMENTED --- Reviewed exact head f9b2a49ab676c813724b6c55d036b1d3d83426c6 against current upstream/main. I found no code-level blocker: the decoder change is restricted to legacy LEA and multi-byte NOP opcodes, preserves ModRM decoding/length metadata, and both Windows and macOS output fixtures verify the prefixed bytes remain untouched. Current conventions and diff checks pass; the branch merges cleanly. The described 231-test Linux/Windows/macOS relinker CI plus the Apple Silicon/Rosetta red/green report are directly relevant and detailed; I did not rerun them locally. I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.
AasishDairelSahayaGrinspan commented 2026-10-22 14:40:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AasishDairelSahayaGrinspan on 2026-10-22T14:40:13Z.


@oneandonlydean when you have a moment, could you review/merge this one? It is rebased onto current main (8b3e39832a); the relinker matrix is green on Linux, Windows, macOS arm64, macOS x86_64 and ASan, conventions pass, and both reviews above are positive, including the independent Apple silicon/Rosetta red/green run.

Manual overlap check for the bot note: #9408 edits a different region of core/relinker/CMakeLists.txt (the trace-import test around line 542) from this PR's TLS group list around line 498; #5021, #6044 and #6879 already conflict with main and touch other parts of the file. No same-line overlap, so no Depends on is needed.

No code changes since the reviews; the new head is only the rebase.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6343486354 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9719#issuecomment-6343486354). Originally posted by [AasishDairelSahayaGrinspan](https://github.com/AasishDairelSahayaGrinspan) on 2026-10-22T14:40:13Z. --- @oneandonlydean when you have a moment, could you review/merge this one? It is rebased onto current `main` (`8b3e39832a`); the relinker matrix is green on Linux, Windows, macOS arm64, macOS x86_64 and ASan, conventions pass, and both reviews above are positive, including the independent Apple silicon/Rosetta red/green run. Manual overlap check for the bot note: #9408 edits a different region of `core/relinker/CMakeLists.txt` (the trace-import test around line 542) from this PR's TLS group list around line 498; #5021, #6044 and #6879 already conflict with `main` and touch other parts of the file. No same-line overlap, so no `Depends on` is needed. No code changes since the reviews; the new head is only the rebase.
MarcZX10 commented 2026-10-22 15:28:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T15:28:47Z.

Review: DISMISSED


I compared the rebased head 0e916c87efc3bb077d9fc20a2cfe65b2119efa3c with the previously reviewed commit: the patch IDs are identical, so this is a rebase-only update. The current relinker matrix, ASan, conventions and eligibility checks are green, and the 11 commits now on upstream/main do not touch this PR’s six changed paths. The author and independent reviewer also report the 231-case red/green run and Apple Silicon/Rosetta probes; I did not rerun that suite locally. I found no code-level issue.

Before merge, please rebase onto the live upstream/main (b7d1d47a5f9f32c9c388ad8d47a0055a90ae6e33) and rerun the focused validation: CONTRIBUTING says to keep branches up to date. This head is 11 commits behind that main.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5616702010 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9719#pullrequestreview-5616702010). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T15:28:47Z. **Review:** DISMISSED --- I compared the rebased head `0e916c87efc3bb077d9fc20a2cfe65b2119efa3c` with the previously reviewed commit: the patch IDs are identical, so this is a rebase-only update. The current relinker matrix, ASan, conventions and eligibility checks are green, and the 11 commits now on upstream/main do not touch this PR’s six changed paths. The author and independent reviewer also report the 231-case red/green run and Apple Silicon/Rosetta probes; I did not rerun that suite locally. I found no code-level issue. Before merge, please rebase onto the live upstream/main (`b7d1d47a5f9f32c9c388ad8d47a0055a90ae6e33`) and rerun the focused validation: CONTRIBUTING says to keep branches up to date. This head is 11 commits behind that main.
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!9719
No description provided.