feat(relinker): split unaligned 256-bit stores for Rosetta #4194

Merged
fable-void merged 1 commit from upstream-pr/4194 into main 2026-10-12 12:22:45 +00:00
fable-void commented 2026-10-12 01:03:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fable-void on 2026-10-12T01:03:05Z.


What

On macOS, --to-rosetta leaves unaligned 256-bit stores as they are, and Rosetta 2 ends the process when such a store crosses into a page it may not write instead of raising the fault. A store inside such a page, and a 128-bit store across one, fault normally. The macOS write watch keeps watched pages read-only between collections, so Hollow Knight: Silksong (PPSA12544) ends at its first vmovups %ymm0, 0x68(%r15) (c4 c1 7c 11 47 68) across a watched page:

rosetta error: unexpectedly need to EmulateForward on a synchronous exception x86_rip=0x143ce887f arm_pc=0x146d81de0 num_insts=3 inst_index=2 x86 instruction bytes: 0xfac56847117cc1c4 0x416600b535a70510

main does not start guests on macOS yet; this line is from a macOS port of the runtime with a relinker that does not split the store. On current main (34a16b7f) the new converter tests do not build: codegen/x86/WideStoreOperands.hpp does not exist.

--to-rosetta now lowers VMOVUPS, VMOVUPD and VMOVDQU with a 256-bit memory destination to a stub that stores the low half with the same instruction at VEX.L=0 and the high half with VEXTRACTF128 at the operand plus 16, so each half faults like any other store. RIP-relative operands are relocated from the instruction's place in the site. A store whose upper half has no 32-bit displacement fails the conversion. Aligned stores (VMOVAPS, VMOVAPD, VMOVDQA and the non-temporal forms) cannot cross a page and are left alone.

Every store is lowered; none is kept. A 4-byte store has no room for the jump to its stub, so the stub also takes one neighbouring instruction:

  • the following one: a return ends the stub, a call pushes its original return address and jumps, a jump or conditional branch is re-encoded with a 32-bit displacement, another 256-bit store is lowered in the same stub;
  • otherwise the preceding one, unless it is a call, a jump, a return or a trap.

Register calls, LOOP, JRCXZ and branches with a prefix that changes them do not move. When neither neighbour can move, the store becomes a short jump to an island: a run of movable instructions within reach of a short jump moves to its own stub, and the bytes it frees hold the jump to the store's stub. A store with neither fails the conversion.

Tested

macOS on Apple silicon, relinker-only build for x86-64 (ANYPS5_RELINKER_ONLY=ON, Release); Linux (Ubuntu 24.04, GCC 13) and Windows (WinLibs GCC 15.2) relinker-only builds.

  • amd64_only_converter does not build on main and passes with this change. It compares the halves with clang's encodings (base, SIB, disp8 overflowing to disp32, disp32, RSP-relative, FS-absolute, r13-based, RIP-relative) and checks that aligned stores, loads, register forms and masked stores are not split. It checks the stubs of a short store followed by a return, a call (direct, with an address-size prefix, through a RIP-relative pointer), short, near and RIP-relative jumps, short and near conditional branches and another store; that a following register call, LOOP, JRCXZ, prefixed branch or CLWB makes the store take the instruction before it; islands past instructions already moved, a store that needs its own stub, a branch target, a register call and the short-jump reach; and that a store with no room fails. Every line the change adds runs in these tests.
  • Relinker tests: all pass on the three hosts.
  • PPSA12544 with --macos --to-rosetta: 55295 stores split in the executable, 278 of them in the stub of the store before them; every module converts with none kept. With the same conversion on the macOS port, the title boots, plays through the opening and into the first areas.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4823711359 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4194). Originally posted by [fable-void](https://github.com/fable-void) on 2026-10-12T01:03:05Z. --- ### What On macOS, `--to-rosetta` leaves unaligned 256-bit stores as they are, and Rosetta 2 ends the process when such a store crosses into a page it may not write instead of raising the fault. A store inside such a page, and a 128-bit store across one, fault normally. The macOS write watch keeps watched pages read-only between collections, so Hollow Knight: Silksong (PPSA12544) ends at its first `vmovups %ymm0, 0x68(%r15)` (`c4 c1 7c 11 47 68`) across a watched page: ``` rosetta error: unexpectedly need to EmulateForward on a synchronous exception x86_rip=0x143ce887f arm_pc=0x146d81de0 num_insts=3 inst_index=2 x86 instruction bytes: 0xfac56847117cc1c4 0x416600b535a70510 ``` `main` does not start guests on macOS yet; this line is from a macOS port of the runtime with a relinker that does not split the store. On current `main` (`34a16b7f`) the new converter tests do not build: `codegen/x86/WideStoreOperands.hpp` does not exist. `--to-rosetta` now lowers VMOVUPS, VMOVUPD and VMOVDQU with a 256-bit memory destination to a stub that stores the low half with the same instruction at VEX.L=0 and the high half with VEXTRACTF128 at the operand plus 16, so each half faults like any other store. RIP-relative operands are relocated from the instruction's place in the site. A store whose upper half has no 32-bit displacement fails the conversion. Aligned stores (VMOVAPS, VMOVAPD, VMOVDQA and the non-temporal forms) cannot cross a page and are left alone. Every store is lowered; none is kept. A 4-byte store has no room for the jump to its stub, so the stub also takes one neighbouring instruction: - the following one: a return ends the stub, a call pushes its original return address and jumps, a jump or conditional branch is re-encoded with a 32-bit displacement, another 256-bit store is lowered in the same stub; - otherwise the preceding one, unless it is a call, a jump, a return or a trap. Register calls, LOOP, JRCXZ and branches with a prefix that changes them do not move. When neither neighbour can move, the store becomes a short jump to an island: a run of movable instructions within reach of a short jump moves to its own stub, and the bytes it frees hold the jump to the store's stub. A store with neither fails the conversion. ### Tested macOS on Apple silicon, relinker-only build for x86-64 (`ANYPS5_RELINKER_ONLY=ON`, Release); Linux (Ubuntu 24.04, GCC 13) and Windows (WinLibs GCC 15.2) relinker-only builds. - `amd64_only_converter` does not build on `main` and passes with this change. It compares the halves with clang's encodings (base, SIB, disp8 overflowing to disp32, disp32, RSP-relative, FS-absolute, r13-based, RIP-relative) and checks that aligned stores, loads, register forms and masked stores are not split. It checks the stubs of a short store followed by a return, a call (direct, with an address-size prefix, through a RIP-relative pointer), short, near and RIP-relative jumps, short and near conditional branches and another store; that a following register call, LOOP, JRCXZ, prefixed branch or CLWB makes the store take the instruction before it; islands past instructions already moved, a store that needs its own stub, a branch target, a register call and the short-jump reach; and that a store with no room fails. Every line the change adds runs in these tests. - Relinker tests: all pass on the three hosts. - PPSA12544 with `--macos --to-rosetta`: 55295 stores split in the executable, 278 of them in the stub of the store before them; every module converts with none kept. With the same conversion on the macOS port, the title boots, plays through the opening and into the first areas. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
fable-void commented 2026-10-12 02:45:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fable-void on 2026-10-12T02:45:48Z.


Pushed an update, rebased on f49f9587. Going back over the change, I found the window placement was more general than it needed to be, so I simplified it:

  • A 4-byte store only ever needs one neighbour to make room for the jump, so the stub now takes the next instruction, or the one before it if the next one can't move. The old search over up to four neighbours never used more than one.
  • Calls with an address-size prefix (67 e8, which the linker leaves behind when it relaxes a GOT call) now move like normal calls. LOOP and JRCXZ are no longer moved; none of the three titles I checked has any.
  • A store whose upper half can't be encoded with a 32-bit displacement now fails the conversion instead of quietly staying unsplit, and RIP-relative stores near the end of their range are now split.
  • Every placement path has a test now, and every line the change adds runs in amd64_only_converter.

The relinked PPSA12544 output is byte-for-byte the same as before, and the relinker tests pass on macOS, Linux and Windows.

About the overlap with #3601: both PRs edit the same MatchSequence dispatch, but for different instructions (legacy RCPPS/RSQRTPS there, 256-bit stores here), and neither needs the other, so I've left Depends on as none. This PR moves that dispatch into _emitOutOfLine so MatchSequence and the store windows can share it. Whichever one lands second needs a small rebase: keep the _emitOutOfLine call and switch DecodeVexReciprocal to DecodeReciprocal inside it. I merged both locally that way and the relinker tests pass (115 of 115). If #3601 goes in first, I'll rebase this one.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116822339 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4194#issuecomment-6116822339). Originally posted by [fable-void](https://github.com/fable-void) on 2026-10-12T02:45:48Z. --- Pushed an update, rebased on f49f9587. Going back over the change, I found the window placement was more general than it needed to be, so I simplified it: - A 4-byte store only ever needs one neighbour to make room for the jump, so the stub now takes the next instruction, or the one before it if the next one can't move. The old search over up to four neighbours never used more than one. - Calls with an address-size prefix (67 e8, which the linker leaves behind when it relaxes a GOT call) now move like normal calls. LOOP and JRCXZ are no longer moved; none of the three titles I checked has any. - A store whose upper half can't be encoded with a 32-bit displacement now fails the conversion instead of quietly staying unsplit, and RIP-relative stores near the end of their range are now split. - Every placement path has a test now, and every line the change adds runs in amd64_only_converter. The relinked PPSA12544 output is byte-for-byte the same as before, and the relinker tests pass on macOS, Linux and Windows. About the overlap with #3601: both PRs edit the same MatchSequence dispatch, but for different instructions (legacy RCPPS/RSQRTPS there, 256-bit stores here), and neither needs the other, so I've left Depends on as none. This PR moves that dispatch into _emitOutOfLine so MatchSequence and the store windows can share it. Whichever one lands second needs a small rebase: keep the _emitOutOfLine call and switch DecodeVexReciprocal to DecodeReciprocal inside it. I merged both locally that way and the relinker tests pass (115 of 115). If #3601 goes in first, I'll rebase this one.
fable-void commented 2026-10-12 02:55:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fable-void on 2026-10-12T02:55:34Z.


That didn't take long...Rebased onto 34a16b7f with the one-line fix, and all relinker tests pass on macOS, Linux and Windows.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116914596 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4194#issuecomment-6116914596). Originally posted by [fable-void](https://github.com/fable-void) on 2026-10-12T02:55:34Z. --- That didn't take long...Rebased onto 34a16b7f with the one-line fix, and all relinker tests pass on macOS, Linux and Windows.
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!4194
No description provided.