Skip to content

feat(relinker): execute supported scalar guest GS accesses on Windows - #11628

Draft
Felipebmaitan wants to merge 26 commits into
boykopovar:mainfrom
Felipebmaitan:feat/windows-guest-gs-scalar
Draft

Felipebmaitan wants to merge 26 commits into
boykopovar:mainfrom
Felipebmaitan:feat/windows-guest-gs-scalar

Conversation

@Felipebmaitan

@Felipebmaitan Felipebmaitan commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

What

Replay supported scalar guest GS memory instructions against the Windows per-thread guest GS base. Unmodeled forms retain their own-length UD2 fallback.

Address MarcZX10’s November 14 review. A first-chance fault in replay leaves the live replay RIP, stack and scratch register available to a later Windows handler. Guest fault delivery receives the original guest context and retains callback edits to the continuation. If guest delivery declines, the runtime reports the guest location without claiming the fault is fatal. The terminal UD2 restores guest state and resumes at the next guest instruction.

Preserved published history in normal merges through main e481b135558fc4316bc3b24e7408156737eff08d. The ELF changes retain main’s covered SCE RELRO file checks and #10518’s load and uncovered SCE RELRO alignment checks.

Draft pending #10518 and the #10861 integration gate. A trial merge with #10518’s current head conflicts in ElfReaderBoundsTests.cpp. #10861’s collector and callers need to retain generated-trap continuation entries when composed with scalar replay. The overlap scan covered 2,260 open PRs with 111 shared-file or export candidates and 37 inconclusive comparisons. #18523 shares the Windows entry-stub signatures and #19470 shares the ELF checks. The scan did not identify a competing scalar replay implementation. GitHub requires upstream write access for a formal re-request to MarcZX10. The review reply is posted in the conversation.

Tested

Windows x86-64 build 26300, Ryzen 7 9800X3D, WinLibs GCC 15.2 r7. The old f013da9725 replay handler failed the new late-handler test because the context had already been rewritten. The updated standalone runner passed all 50 scalar cases, including in-place retry of a load, a store and LOCK XADD.

The native libc component run passed the same 50 cases plus the guest-fault callback and unrepaired access-violation checks. The callback test validates original guest RIP, RSP, scratch registers and exception address, then verifies that edited RIP and RAX take effect. The unrepaired-fault test verifies the native exception code and guest-coordinate diagnostic. The component runs used historical converter and supporting objects with freshly compiled replay and reporting changes. The current entry-stub and ELF reader sources and bounds tests also passed when rebuilt against the archived relinker support library.

One independent reviewer checked 4446f4f0c4b4a0dc2e5241f492f34cd0c90b63aa, found no blocking source problem, and identified a gap covering callback-edited continuation registers. 870917228a closes that gap and the native component test passed again. The final merge changes none of the replay or relinker files reviewed.

Conventions and whitespace checks pass against origin/main. The exact-head public-header audit passed 413 libraries and 34,756 exports. The native Release CMake/Ninja build at 5922e359e9a3e74ad8366698f3f15bbd0e7cf4b5 completed with two compiler jobs. 52/52 focused CTests passed in 30.94 s, including standalone and production-libc scalar replay and fault retry, emitted executable and module startup, relocations, Windows entry-stub and ELF bounds, TLS coverage, and the libkernel sysarch, pthread, signal context and exception delivery tests. JUnit SHA-256 b7295bd809427ee44b4d424c34b6b77ba01e0f80044ce1dee0b34393661e2708.

Historical evidence remains attached to its tested revisions. The previous description recorded 23 focused Windows tests on f013da9725. JJenkx independently tested that head on an earlier main merge. See JJenkx’s November 8 comment in this PR for the tested revisions.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: fix(relinker): reject an executable PT_LOAD alignment that is not a power of two #10518
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

lulebo and others added 5 commits October 24, 2026 16:31
…ower of two

ElfReader accepted any p_align on an executable's PT_LOAD, and on the
uncovered PT_SCE_RELRO it turns into one, although the gABI says
p_align should be 0, 1 or a power of two. When such a segment starts
on a page that an earlier segment maps with other bytes, the Linux
patcher pads the output one byte at a time to the next offset
congruent to its address modulo that alignment: a 3221-byte executable
with an alignment of 0xfffffffffffffffb ran for 46 s, reached 12.9 GiB
and failed with std::bad_alloc. Both segments are now refused, as the
reader already refuses a PT_TLS alignment that is not a power of two.

The guest module reader takes its headers from the same function, so
the power-of-two half of its own alignment check can no longer fail
and is dropped; its offset congruence check stays. A power-of-two
alignment still reaches the padding, which the relinker technical debt
now records.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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 the feature commit 1bb3d0cfcb8a7cfe102b9384b8e723bca8925d32 separately from the explicitly listed prerequisite stack.

[P1] Decode address-size-32 mod=00, r/m=101 as absolute disp32, not RIP-relative. In WindowsGuestGs::Append, the mod == 0 && rm == 5 case always sets base = 0xfe (RIP-relative). But when address-size override 0x67 is present in 64-bit mode, this ModRM form uses an absolute 32-bit displacement. The runtime then adds the instruction resume address before truncating the effective offset to 32 bits (GuestGs.cpp), so e.g. 65 67 48 8b 05 00 00 00 00 reads from GS.base + (resume & 0xffffffff) instead of GS.base + 0. That can read or write the wrong guest location. The current address32-wrap fixture uses a SIB form and does not cover this special non-SIB form.

Please set the no-base sentinel for this encoding when AddressSize32 is true, preserve the 32-bit displacement as an unsigned effective offset, and add a 67 + mod=00/rm=101 execution fixture (zero displacement is enough with the existing runner's nonzero GS base). The relinker-only build is in progress locally; conventions and git diff --check pass, and the hosted Linux/Windows relinker checks currently pass. No local Windows runtime test is claimed.

@MarcZX10

Copy link
Copy Markdown
Contributor

I confirmed the encoding case with the PR-head relinker: converting 65 67 48 8b 05 00 00 00 00 succeeds and emits .ggs metadata base=0xfe, address_size32=1, displacement=0. Since 0xfe is the runtime RIP-relative sentinel, the emitted metadata demonstrates the wrong effective address directly. The three focused Linux relinker tests (windows_guest_gs, windows_guest_gs_relocations, gs_trap_entry_roots) pass, but their current fixtures omit this encoding.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks, the replay design and its tests look solid. One change before this can merge: on main, a guest GS instruction the relinker can't handle becomes a UD2 trap and the conversion still succeeds, so the program only faults if it actually reaches that instruction. With this PR, a valid form the replay doesn't model (shorter than five bytes, string, SSE/VEX/EVEX/XOP, the implicit maskmov/monitor forms, GS selector/stack/FSGSBASE) makes the whole conversion fail, even if the instruction is never executed.

Please keep main's UD2 trap as the fallback for those forms and replay only the supported ones. Please also change the tests that now expect conversion to fail for those encodings (test_windows_guest_gs.py around lines 147-151, the gs_traps_implicit/short-instruction branch in test_tls_function_coverage.py, and the touched GuestSysarch expectations) so they no longer require a refusal: tests shouldn't assert that a valid but unimplemented instruction is refused. The positive, fault, atomic, relocation and entry-overwrite checks can stay as they are.

This also depends on #11624, #11090, #11276 and #10518 landing first, as the description says.

@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

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
PR 18523 core/relinker/elfpatcher/include/elfpatcher/windows/WindowsEntryStubBuilder.hpp L23
core/relinker/elfpatcher/include/elfpatcher/windows/WindowsLoadImage.hpp L18
core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp L76
core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp L108
PR 19470 core/relinker/relinker/src/parsing/ElfReader.cpp L178

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 6149 core/relinker/CMakeLists.txt
docs/dev/debt/libkernel.md
PR 6936 docs/dev/debt/libkernel.md
PR 7121 docs/dev/debt/libkernel.md
PR 7149 docs/dev/debt/libkernel.md
PR 7447 docs/dev/debt/libkernel.md
PR 8592 core/libs/prx/libkernel/System/src/Exception.cpp
PR 8605 docs/dev/debt/libkernel.md
PR 9156 docs/dev/debt/libkernel.md
PR 9293 docs/dev/debt/libkernel.md
PR 9297 docs/dev/debt/libkernel.md
PR 9302 docs/dev/debt/libkernel.md
PR 9389 docs/dev/debt/libkernel.md
PR 9557 docs/dev/debt/libkernel.md
PR 9566 docs/dev/debt/libkernel.md
PR 9938 docs/dev/debt/libkernel.md
PR 11215 docs/dev/debt/libkernel.md
PR 11525 docs/dev/debt/relinker.md
PR 11896 docs/dev/debt/libkernel.md
PR 12642 core/libs/prx/libkernel/System/src/Exception.cpp
PR 12678 core/relinker/CMakeLists.txt
docs/dev/debt/relinker.md
PR 13486 core/relinker/CMakeLists.txt
PR 13716 core/relinker/CMakeLists.txt
core/relinker/relinker/tests/test_tls_function_coverage.py
docs/dev/debt/relinker.md
PR 14241 docs/dev/debt/relinker.md
PR 15037 docs/dev/debt/libkernel.md
PR 15245 core/relinker/CMakeLists.txt
core/relinker/elfpatcher/include/elfpatcher/windows/WindowsEntryStubBuilder.hpp
core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp
core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp
docs/dev/debt/relinker.md
PR 16790 docs/dev/debt/libkernel.md
PR 17557 core/relinker/CMakeLists.txt
docs/dev/debt/relinker.md
PR 18449 core/relinker/CMakeLists.txt
PR 20389 docs/dev/debt/relinker.md
PR 20917 core/relinker/CMakeLists.txt
PR 23636 core/relinker/CMakeLists.txt
PR 23842 core/relinker/CMakeLists.txt
PR 23861 core/relinker/elfpatcher/src/windows/WindowsLoadImage.cpp
PR 24322 docs/dev/debt/libkernel.md
PR 24917 docs/dev/debt/libkernel.md
PR 25483 core/relinker/CMakeLists.txt

@MarcZX10

Copy link
Copy Markdown
Contributor

I independently checked the new unsupported-instruction failure path. WindowsGuestGs::Append throws RelinkerException for unmodeled forms, and the added test_windows_guest_gs.py cases assert that conversion returns 2 with no output for short, string, SIMD/VEX/EVEX, implicit-memory, and segment/stack examples. That differs from the existing Windows TLS builder's UD2 fallback on main, which preserves conversion and faults only if execution reaches the instruction. The collaborator's regression concern is therefore confirmed by the source and test expectations.

I reran the focused Linux relinker tests at this head: windows_guest_gs, windows_guest_gs_relocations, and gs_trap_entry_roots passed (3/3). These tests validate the current expectations; they don't establish that rejecting the whole conversion is compatible. The earlier 67 address-size override / mod=00, rm=5 misdecode finding also remains open.

@Felipebmaitan

Copy link
Copy Markdown
Contributor Author

@oneandonlydean Addressed in 2a621e5, with the branch now at 87ef2af.

Unsupported GS forms now retain the UD2 trap and NOP padding of their original length, so conversion succeeds and they fault only if reached. Supported scalar forms still use replay. Updated the GS conversion/TLS coverage tests and removed the GuestSysarch assertions that required valid but unimplemented operations to throw. Relocation and entry-overwrite checks remain; fallback traps protect their full overwritten length.

Windows validation passed: 61 focused relinker tests, the execution suite using production libc, four thread/sysarch tests, conventions and the public-header audit. New cases skip each of 16 unsupported encodings before executing supported replay; a directly launched fallback-only program also succeeds without a GS runtime dependency. Linux was not rerun because the configured WSL disk is missing.

The dependency order remains unchanged: #11624, #11090, #11276 and #10518 must land first.

@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 current head 87ef2af9591ba4fd21148fa1e1fd862d79f24abe. The unsupported-form UD2 fallback now appears to address the separate conversion-regression concern. A blocking address-size decoding issue remains in WindowsGuestGs::Append: for mod=00, r/m=101 the code always writes base sentinel 0xfe, which makes the runtime add the instruction resume address. With a 0x67 address-size override in 64-bit mode, this encoding is absolute disp32 with no base, not RIP-relative. The runtime then truncates the sum to 32 bits, so 65 67 48 8b 05 00 00 00 00 reads from GS.base + (resume & 0xffffffff) rather than GS.base + 0. The address32-wrap fixture uses a SIB form and does not cover this special non-SIB encoding.

Please select the no-base sentinel when AddressSize32 is true, preserve the unsigned 32-bit displacement, and add an execution fixture for 67 + mod=00/rm=101 (zero displacement with the runner's nonzero GS base is sufficient). This is still outstanding from my prior review and applies to this substantially updated head.

Validation: git diff --check upstream/main...upstream/pr/11628 passes. Hosted conventions and Linux/Windows relinker jobs pass; macOS relinker and one macOS register-context job remain in progress/queued. I did not run the Windows execution suite locally.

@MarcZX10

Copy link
Copy Markdown
Contributor

Correction to my review on this head: my claim that 0x67 changes mod=00, r/m=101 to absolute disp32 in 64-bit mode was wrong. Intel's SDM says this encoding remains RIP-relative in 64-bit mode regardless of address-size prefix; 0x67 truncates and zero-extends the computed effective address to 32 bits. See Intel SDM, Volume 2A, §2.2.1.6. The implementation's resume-relative calculation followed by uint32_t truncation is consistent with that rule. I withdraw the address-size finding and apologize for the incorrect request. The unrelated earlier fallback concern is addressed by this head's UD2 fallback. My focused review found no remaining blocker; the author reports extensive Windows/Linux fixture validation and hosted relinker/ASan checks are passing (some macOS jobs are still pending).

@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.

Corrected my prior address-size finding: Intel SDM confirms mod=00, r/m=101 remains RIP-relative with 0x67 in 64-bit mode and the effective address is truncated to 32 bits. The current resume-relative calculation and truncation are correct. The UD2 fallback changes also address my earlier concern. I found no remaining blocker on this head. Hosted Linux/Windows relinker, ASan and the completed macOS jobs pass; the remaining macOS checks are still pending. The author reports extensive Windows/Linux execution fixtures; I did not independently rerun those suites.

@Felipebmaitan

Felipebmaitan commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor Author

@MarcZX10 Following your correction, the resume-relative calculation and subsequent 32-bit truncation remain unchanged.

I moved the four 67 + mod=00/rm=101 execution regressions into this PR in 914c450: zero displacement, positive/unaligned displacement, negative displacement and a store. They were previously present only in the short-instruction follow-up. This keeps the scalar PR independently covered against the case raised in the review.

Fresh production libc/libkernel builds passed on Windows and native Linux under WSL. Nine Windows and seven Linux focused runtime/relinker tests passed, and all 50 normalized scalar execution dumps match between rewritten Windows and native Linux GS, including these four cases. Conventions against origin/main passed. Main b986c11 is merged. No console/title or performance verification is claimed.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 7, 2026
@JJenkx

JJenkx commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Windows check of head f013da9725, merged onto main 990481cdbf (6a6774eef1, clean merge).

Setup: Windows 11 Enterprise 22621, WinLibs GCC 15.2 UCRT, Release. The hosted Windows check does not run these tests on pull requests.

  • elf_reader_bounds, gs_trap_entry_roots, guest_load_ranges, tls_function_coverage_alu_adc, tls_function_coverage_alu_add, tls_function_coverage_alu_and, tls_function_coverage_alu_cmp, tls_function_coverage_alu_execution, tls_function_coverage_alu_or, tls_function_coverage_alu_sbb, tls_function_coverage_alu_sub, tls_function_coverage_alu_xor, tls_function_coverage_displacement_bounds, tls_function_coverage_displacement_r0, tls_function_coverage_displacement_r1, tls_function_coverage_displacement_r10, tls_function_coverage_displacement_r11, tls_function_coverage_displacement_r12, tls_function_coverage_displacement_r13, tls_function_coverage_displacement_r14, tls_function_coverage_displacement_r15, tls_function_coverage_displacement_r2, tls_function_coverage_displacement_r3, tls_function_coverage_displacement_r5, tls_function_coverage_displacement_r6, tls_function_coverage_displacement_r7, tls_function_coverage_displacement_r8, tls_function_coverage_displacement_r9, tls_function_coverage_gs_non_memory, tls_function_coverage_gs_traps, tls_function_coverage_gs_traps_implicit, tls_function_coverage_metadata, tls_function_coverage_moved_branch, tls_function_coverage_padding, tls_function_coverage_register_address_loads, tls_function_coverage_register_loads, tls_function_coverage_rejected, windows_guest_gs, windows_guest_gs_relocations, windows_guest_gs_runtime, windows_guest_gs_startup pass in 3 of 3 runs, none skipped.

AI-assisted: yes.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 9, 2026

@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 — changes requested

Changes are required before merge: make replay fault recovery distinguish a memory fault from a completed replay before redirecting the instruction pointer.

What I ran

Reviewed head f013da972573fa4dfca95607f2462501d6ad2234. Read the prepared description, commit list, manifest, diff, and all changed files listed as reviewed. Read targeted decoder, exception-handler, and caller context through repo_read.py. No build or tests were run by me; the Windows results in the PR description are author-reported.

Results

Check Result Note
Diff and targeted source review passed One verified blocking defect
Build and tests not run PR reports Windows checks

Findings

Blocking

  • On an access violation during the replayed memory instruction, the pending-replay branch restores the scratch register and adds 128 to Rsp, then redirects Rip to the original guest instruction. It returns false unless the exception is the terminal UD2 (core/libs/prx/libc/src/specifics/windows/GuestGs.cpp:107-113). The caller therefore continues searching for a handler, while the exception context has already been modified. If another handler repairs the memory fault and resumes execution, it resumes at the original GS-prefixed instruction rather than the replay instruction, causing the access to fault again or run with a corrupted stack. Recover nonterminal replay faults without discarding replay state or changing the context; restore the original context only when propagating the fault.

Non-blocking

None.

Not verified

I did not run the claimed Windows build or tests, hosted checks, PS5/title execution, or physical-console behavior. I read every changed file marked included in the manifest; no changed files were excluded there. files.json was not available through the reader.

To get this to a good state

Fix replay fault handling so a recoverable memory fault can retry the replay instruction with the pending state intact, and so propagated faults do not leave a modified stack or instruction pointer. Then rerun the focused Windows replay and fault-retry tests on the new head.

@Felipebmaitan

Copy link
Copy Markdown
Contributor Author

@MarcZX10 Addressed your November 14 replay-fault finding in 4446f4f0c4. The draft head is 5922e359e9 with current main e481b13555 integrated. A later Windows handler receives the replay RIP, scratch register, stack and exception address unchanged, so it can repair the mapping and resume the memory instruction in place. Guest fault delivery uses the original guest context and applies continuation changes only after the guest callback accepts the fault.

The native negative control fails on the old handler at the first late-handler load retry. With the fix, both Windows component runs pass 50 scalar cases, including replay-fault retries on load, store and LOCK XADD. Production reporter component coverage also checks callback-edited RIP and RAX and the original guest diagnostic on an unrepaired AV. One independent review found no blocking source issue and identified the callback-continuation coverage gap, which 870917228a adds and verifies. These component runs use a prior converter and supporting objects. The integrated Windows CMake validation is queued.

This remains draft for #10518 and the #10861 integration gate.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 20, 2026

@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 — no blocking findings

The reviewed head is 5922e359e9a3e74ad8366698f3f15bbd0e7cf4b5; the inspected replay and fault paths support the stated behavior, and no blocking defect was verified.

What I ran

No commands or tests were run. The PR description reports a native build and 52 focused CTests passed; these are author-reported results.

Results

Check Result Note
Source review passed Replay and fault paths inspected
Build and tests not run Author reports 52 focused tests passed

Findings

Blocking

None.

Non-blocking

None.

Not verified

The manifest lists 24 changed files, all marked included, but the reader interface did not expose the prepared review/files/ bundle. I therefore reviewed only the prepared diff and targeted repository source ranges, not every changed file's full text. Excluded-file details were not available. Windows runtime behavior, physical-console behavior, and the reported build and tests were not independently verified.

To get this to a good state

Nothing is required.

This branch has not been deployed

No deployments
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