Skip to content

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

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
AasishDairelSahayaGrinspan:fix/relinker-gs-non-memory-operands
Oct 25, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
AasishDairelSahayaGrinspan:fix/relinker-gs-non-memory-operands

Conversation

@AasishDairelSahayaGrinspan

@AasishDairelSahayaGrinspan AasishDairelSahayaGrinspan commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

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 b7d1d47:

  • 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

@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/relinker/CMakeLists.txt
PR 6713 core/relinker/CMakeLists.txt

@jotitadev jotitadev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 f9b2a49 (base main 7e2ba73), 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 MarcZX10 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head f9b2a49 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
AasishDairelSahayaGrinspan force-pushed the fix/relinker-gs-non-memory-operands branch from f9b2a49 to 0e916c8 Compare October 22, 2026 14:11
@AasishDairelSahayaGrinspan

Copy link
Copy Markdown
Contributor Author

@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 MarcZX10 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

LEA computes an effective address and the multi-byte NOP ignores its operand, so neither accesses memory and a GS prefix on them is ignored. The Windows TLS builder and macOS image writer treated every ModRM memory form as an access and replaced these instructions with UD2 and NOP padding.
@AasishDairelSahayaGrinspan
AasishDairelSahayaGrinspan force-pushed the fix/relinker-gs-non-memory-operands branch from 0e916c8 to 8b86ca3 Compare October 22, 2026 15:33
@AasishDairelSahayaGrinspan

Copy link
Copy Markdown
Contributor Author

@MarcZX10 rebased onto live main (b7d1d47a5f9f32c9c388ad8d47a0055a90ae6e33); the 11 commits added since the last head do not touch this PR's six changed paths, and the rebase had no conflicts. The patch is unchanged.

Validation on the rebased head 8b86ca3984:

  • Relinker-only Release, ctest: 231/231.
  • tools/check_conventions.py --base origin/main: pass.
  • tools/pr_overlap.py --branch ...: no overlap (the bot note is the same four already-conflicting PRs; manual file/line check unchanged).
  • CI: Linux, Windows, macOS arm64, macOS x86_64 and ASan all pass; conventions, eligibility and labels pass.

Understood on the hold for title-agnostic PRs. I will leave this open and wait for the queue to decline; no further pings.

@MarcZX10

MarcZX10 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

I’ve noticed that asking you to hold this PR was unnecessary. It’s fine to keep submitting PRs; maintainers will handle them in due course.

@AasishDairelSahayaGrinspan

Copy link
Copy Markdown
Contributor Author

Thanks for the correction — noted.

The separate current-main validation is already complete: head 8b86ca3984 is rebased onto b7d1d47a5f9f32c9c388ad8d47a0055a90ae6e33, the patch is unchanged, relinker-only ctest is 231/231 locally, check_conventions.py and pr_overlap.py pass, and the full relinker matrix plus ASan are green on the rebased head.

Could you re-review when convenient so the CHANGES_REQUESTED is cleared?

@plexerPK

Copy link
Copy Markdown
Contributor

I have a small local follow-up for FS/GS prefixes on INS/STOS/SCAS: these instructions use fixed ES, so the decoder must ignore the segment override. This complements your LEA/NOP fix but touches the same insertion point.

Would landing this PR first, then rebasing the follow-up while preserving both checks, work for you? I tested that resolution together with #9549 and our SDK-stub boundary fix on main 4c06b21681bc46b762255ca25b7749f66b881ab4: all 237 relinker CTest tests passed on macOS arm64, including the new 96-case Windows/macOS conversion matrix. This verifies conversion and native tooling, not execution of the converted x86-64 programs. Analysis, implementation and testing were AI-assisted with Codex. No change to this PR is requested.

@AasishDairelSahayaGrinspan

Copy link
Copy Markdown
Contributor Author

Yes, landing #9719 first works for me. Rebase the INS/STOS/SCAS follow-up on the merged main and preserve both checks at the same insertion point; I’ll review it when it’s up.

Thanks for testing the combined resolution with #9549 and the SDK-stub boundary fix. The 237 relinker CTests and the 96-case conversion matrix are good coverage, and it’s clear that this validates conversion and native tooling rather than execution of the converted x86-64 programs.

No changes to this PR.

@adriadam10 adriadam10 self-assigned this Oct 25, 2026
@adriadam10
adriadam10 dismissed MarcZX10’s stale review October 25, 2026 18:50

Dismissing a stale review: the requested changes were addressed after it (new head) and the merge queue blocks on it. Please request changes again if anything is still open.

@wachi35676

Copy link
Copy Markdown
Contributor

Tested head 8b86ca3 merged onto main aeff26b (clean) on Windows 11, Ryzen 5 9600X, WinLibs GCC 15.2.0, Release.

  • decoder_length, all tls_function_coverage_* and macos_gs_traps: 36/36 pass.
  • Negative control: with main's X64InstructionDecoder.cpp and X64OpcodeConstants.hpp, all three new checks fail:
    • Wrong memory operand presence for 658d0511111111
    • ('gs-lea', '0f0b9090909090c3'): main replaces it with UD2
    • gs-nop-memory: a non-memory GS instruction was replaced
  • Native run on Windows, where GS points at the TEB. I executed 65 48 8d 05 <disp32>, 65 48 8d 40 08 (rax = 0xdead0000 → 0xdead0008, GS base not added), and the GS NOPs 65 0f 1f 05 <disp32>, 65 0f 1f 00 (rax = 0) and 65 0f 1f 40 08 (rax = 0xdead0000). All run without a fault on Zen 5. That covers the Windows execution the description lists as not done.
  • On main, HasMemoryOperand is only read by the GS trap code in WindowsTlsBuilder.cpp and MacOsImage.cpp, so clearing it for LEA and 0F 1F affects nothing else.
  • Four real homebrew titles convert byte-identically to main on Windows, macOS and Linux, so none of them contains these forms.

AI-assisted: yes (Claude Code); results above were run on this machine.

@adriadam10
adriadam10 merged commit 2b52256 into boykopovar:main Oct 25, 2026
13 of 14 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the decoder change (only 8D and 0F 1F; the 0F 38/0F 3A maps and VEX/EVEX paths are not affected), the only two consumers of HasMemoryOperand (Windows TLS builder, macOS image writer), and that the new fixtures fail without the change. Relinker CI is green on Linux, Windows, macOS arm64/x86_64 and ASan, and jotitadev's Apple silicon/Rosetta run confirms the red/green check; merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants