loader: split 256-bit guest stores on macOS so Rosetta does not abort #984
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!984
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-984"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@wuboo on GitHub · 2026-10-02 20:42 UTC · original
wuboo:pr/macos-wide-store-split→main· +1010 −4 · 8 files · 7 commits · openProblem
Under Rosetta, a 256-bit vector store that takes a synchronous fault is not emulated forward. The process is aborted (signal 6) instead of the fault being delivered the way a 128-bit store is.
Astro Bot (PPSA21564) on an M3 Max under Rosetta, without this change, dies during boot with exit status −6. The emulator log ends with:
The first instruction word is little-endian
c4 c1 7c 11 ...: a 3-byte VEX prefix and opcode11, which is a 256-bitvmovupsstore.The same abort is reproduced by
macos_wide_store_testson an unpatched store that crosses into a protected page. The child prints the Rosetta error and is killed by signal 6. That case is expected; the patched cases in the same test pass.Change
On macOS, while a guest module is loaded, each plain 256-bit vector store is rewritten to a jump into an 8 MiB trampoline placed after the module. The trampoline performs the store as two 128-bit halves and jumps back. RIP-relative addresses are re-encoded for the trampoline.
How the jump is planted, depending on the instruction length and the bytes around it:
int3or post-jumpnoppadding in rel8 range: a rel8 jump through that cave to a rel32 jumpfs/gs, not a direct branch target): those instructions move into the trampoline and the store becomes a rel32 jump.KYTY_WIDE_STORE_MOVE=allalso does this at unprofiled sites.ud2, and the SIGILL handler jumps to the trampoline. A site that hits 1024 times is appended once toKYTY_WIDE_STORE_HOT. The next launch plants a static jump for sites on that list. Duplicates already in the file are not appended again.By default, splitting is on when the process runs under Rosetta (
sysctl.proc_translated) and off on native Intel Macs.KYTY_SPLIT_WIDE_STORES=0or=1overrides that. Linux and Windows are unchanged.SSE4a
EXTRQ/INSERTQis not in this change. That patch is #839.Testing
macos_wide_store_testson this branch, Release, macOS x86_64 under Rosetta: exit 0,all checks passed. The unpatched crossing store dies with signal 6, as the test requires. The patched copies (direct jump, cave, ud2, moved instructions, RIP-relative) write every byte and nothing past the target.Release
kyty_emulatorbuilds from this branch. I checkedsysctl.proc_translatedwith a small x86_64 and arm64 test binary on the same machine: it reports 1 under Rosetta and 0 natively.On my macOS integration tree (the bring-up stack from #715 and related), the same loader logic logged this for Astro Bot's
eboot.binin a level run:libc.prx: 798 patched (padding 102, ud2 1, moved 15).libSceNpCppWebApi.prx: 1252 patched (padding 5, ud2 0, moved 1). Nothing was left short or unsupported.Limits
KYTY_SPLIT_WIDE_STORES=0skips the allocation.ud2sites still take a SIGILL until they are on the hot list. After a profiled restart they become direct jumps.Under Rosetta, a 256-bit vector store that faults on a protected page is not emulated forward; Rosetta aborts the process ('unexpectedly need to EmulateForward on a synchronous exception'). Guest memory watched by the GPU caches is write-protected, so this happens in normal play. While a module loads, each plain 256-bit store is rewritten to reach an 8 MiB trampoline after the module, where it runs as two 128-bit halves. The jump is planted directly when the store is long enough, through int3/nop padding in rel8 range, by moving the following register-only instructions into the trampoline, or as ud2 handled in the SIGILL handler. ud2 sites that fire often are appended once to the KYTY_WIDE_STORE_HOT list and get a static jump on the next start. Splitting is on by default when the process runs under Rosetta and off on native Intel Macs; KYTY_SPLIT_WIDE_STORES=0 or =1 overrides it.@coderabbitai[bot] on GitHub · 2026-10-02 20:47 UTC · original · edited 2026-10-03 19:05 UTC
Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
📝 Walkthrough
Walkthrough
On macOS,
KYTY_SPLIT_WIDE_STORESenables wide-store splitting when set to a value other than"0". When unset, the loader enables splitting ifsysctl.proc_translatedreports Rosetta translation. The loader reserves trampoline space and rewrites eligible 256-bit stores in executable segments. Trap handling redirects execution to registered trampolines. New tests exercise rewriting and trap paths, and CMake registers the test executable.Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Bug fix
Merge Risk: 🔵 Low · up to
8863bUnder Rosetta, the loader also rewrites aligned wide stores, which can never cross a page. These rewrites use trampoline space, and some become slow trap sites in hot loops until the store profile is rebuilt and the game restarted. Correctness is not affected. The change can merge with this follow-up, though excluding the aligned forms is a small fix.
Security Architecture Review
Security architecture risk: 🟡 Moderate · up to
8863bThe compatibility change alters executable code and fault recovery. Recovery targets can survive module unloading, and an optional optimization can move instructions without accounting for indirect entry points. These risks affect the running application; broader privilege escalation has not been established.
Retained concerns
Security review details
Security Blast Radius
Security Findings and Attack Paths
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.@ -795,0 +1171,4 @@return value != nullptr && std::strcmp(value, "all") == 0;}();if (trap && (move_all || std::binary_search(hot_sites.begin(), hot_sites.end(),reinterpret_cast<uint64_t>(code)))) {@coderabbitai[bot] on GitHub · 2026-10-02 20:47 UTC · original · edited 2026-10-02 21:12 UTC
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Unprofiled trap sites are relocated by default, but only direct branch targets are checked.
move_allistrueunlessKYTY_WIDE_STORE_MOVE=hot. As a result, every trapped short store that has movable instructions after it is rewritten into ajmp rel32. The bytes of the moved instructions after the first five are filled with0xCC. That happens even when the site is not inKYTY_WIDE_STORE_HOT.The only protection is
branch_targets. That list holds only the targets of direct relative branches found by the linear sweep. It does not cover:jmp reg/jmp [mem]targets, such as switch cases that fall through after a store;If one of those targets lands on a moved instruction, execution enters the rel32 operand bytes or the
0xCCfill. The result is a crash or wrong code. The PR description says moved-instruction jumps apply to profiled hot sites on the next launch. Unprofiled sites should stay on theud2path by default.Proposed fix: limit moving to profiled sites unless explicitly enabled
✅ Addressed in commit e8f5158
@ -0,0 +195,4 @@const auto live_result = Loader::X64InstructionEmulator::SplitWideStores(reinterpret_cast<uint64_t>(area.code), live.size(), &cursor, area.end);Check(live_result.via_cave == 0 && live_result.trapped == 1,"reachable nop not used as a cave");@coderabbitai[bot] on GitHub · 2026-10-02 20:47 UTC · original · edited 2026-10-02 21:12 UTC
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
The "reachable nop" expectation is wrong under the default move mode.
In the
livesequence, the 4-byte store is followed by a 5-byte nop at offset 4. That nop is not a branch target, and it is not a ret, call, system, or interrupt instruction. Becausemove_allis the default, the splitter moves the nop. Thenlength + moved_original = 9 >= 5, sotrapbecomesfalseand the site is counted asrelocated, nottrapped.live_result.trapped == 1therefore fails.Choose the fix based on the intended default:
move_allstays the default, assertrelocated == 1.src/loader/x64InstructionEmulator.cppLine 1131-1136), keeptrapped == 1.✅ Addressed in commit e8f5158
@ -0,0 +301,4 @@TestShortStoreViaInt3Padding();TestShortStoreViaNopPadding();TestShortStoreViaLongStoreDeadBytes();TestRipRelativeStore();@coderabbitai[bot] on GitHub · 2026-10-02 20:47 UTC · original · edited 2026-10-02 21:13 UTC
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
mainnever callsTestShortStoreViaNopPaddingorTestShortStoreViaLongStoreDeadBytes.Both functions are defined but not called. The nop-padding cave path and the path that chains through a long store's dead bytes are therefore not tested. The PR objectives say the cave checks passed. Both functions sit in an anonymous namespace, so
-Wunused-functionwarns about them. Add the calls. Fix theTestShortStoreViaNopPaddingexpectation at Line 182 first, or that test fails.Proposed fix
📝 Committable suggestion
✅ Addressed in commit e8f5158
@coderabbitai[bot] on GitHub · 2026-10-02 20:47 UTC · original · reviewed
Actionable comments posted: 3
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
9fb1f61e-7ae1-4c74-829a-34ead4531903📥 Commits
Reviewing files that changed from the base of the PR and between
e317465d8aand 9e9c2de52faf6d422ea67d5690cfc163a4a088b9.📒 Files selected for processing (6)
CMakeLists.txtsrc/loader/runtimeLinker.cppsrc/loader/runtimeLinker.hsrc/loader/x64InstructionEmulator.cppsrc/loader/x64InstructionEmulator.htests/macosWideStoreTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
@ -795,0 +1171,4 @@return value != nullptr && std::strcmp(value, "all") == 0;}();if (trap && (move_all || std::binary_search(hot_sites.begin(), hot_sites.end(),reinterpret_cast<uint64_t>(code)))) {@wuboo on GitHub · 2026-10-02 20:54 UTC · original
Agreed. Fixed in e8f5158b: moving the following instructions is now limited to sites from the hot profile, and
KYTY_WIDE_STORE_MOVE=allopts into moving everywhere. Unprofiled short stores stay on the ud2 path.@ -0,0 +195,4 @@const auto live_result = Loader::X64InstructionEmulator::SplitWideStores(reinterpret_cast<uint64_t>(area.code), live.size(), &cursor, area.end);Check(live_result.via_cave == 0 && live_result.trapped == 1,"reachable nop not used as a cave");@wuboo on GitHub · 2026-10-02 20:54 UTC · original
Fixed by the same change (e8f5158b). With moving limited to profiled sites, the live-nop case traps as the test expects, and the test now runs.
@ -0,0 +301,4 @@TestShortStoreViaInt3Padding();TestShortStoreViaNopPadding();TestShortStoreViaLongStoreDeadBytes();TestRipRelativeStore();@wuboo on GitHub · 2026-10-02 20:54 UTC · original
Fixed in e8f5158b: both tests are called now and pass (
macos_wide_store_tests: all checks passed).@coderabbitai[bot] on GitHub · 2026-10-02 20:58 UTC · original · reviewed
🧹 Nitpick comments (2)
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
8b12013c-f15e-4413-bb9e-a77173c2a08f📥 Commits
Reviewing files that changed from the base of the PR and between 9e9c2de52faf6d422ea67d5690cfc163a4a088b9 and e8f5158bb6e581e13acb4a3118cbc41f876bae22.
📒 Files selected for processing (2)
src/loader/x64InstructionEmulator.cpptests/macosWideStoreTests.cpp🚧 Files skipped from review as they are similar to previous changes (2)
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.
@wuboo on GitHub · 2026-10-02 20:59 UTC · original
Addressed the outside-diff note in 8934d8d0:
TestUnpatchedAbortsUnderRosettanow assertsWIFSIGNALEDwhensysctl.proc_translatedreports Rosetta; native x86-64 still just exits.macos_wide_store_testspasses.@coderabbitai[bot] on GitHub · 2026-10-02 21:07 UTC · original · edited 2026-10-02 21:37 UTC
On
tests/macosWideStoreTests.cppline 106 (outdated):🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 2385
Require a normal zero exit on native x86-64.
When
IsRunningUnderRosetta()is false, the current condition passes regardless of the child’s wait status. This lets a native child terminated by a signal or nonzero exit pass the test. This is a test assertion gap, not a production behavior failure.🐛 Suggested fix
📝 Committable suggestion
✅ Addressed in commit 9d7ed9f
@coderabbitai[bot] on GitHub · 2026-10-02 21:07 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
4adcfcc0-c9c4-4088-9238-9ea7d3158594📥 Commits
Reviewing files that changed from the base of the PR and between e8f5158bb6e581e13acb4a3118cbc41f876bae22 and 8934d8d01a66cb4a76ecf8b765c608de5038743a.
📒 Files selected for processing (1)
tests/macosWideStoreTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.
@ -0,0 +195,4 @@const auto live_result = Loader::X64InstructionEmulator::SplitWideStores(reinterpret_cast<uint64_t>(area.code), live.size(), &cursor, area.end);Check(live_result.via_cave == 0 && live_result.trapped == 1,"reachable nop not used as a cave");@coderabbitai[bot] on GitHub · 2026-10-02 21:12 UTC · original
@wubooThanks for fixing this ine8f5158b. Limiting moving to profiled sites makes the live-nop case trap, as the test expects.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
@ -795,0 +1171,4 @@return value != nullptr && std::strcmp(value, "all") == 0;}();if (trap && (move_all || std::binary_search(hot_sites.begin(), hot_sites.end(),reinterpret_cast<uint64_t>(code)))) {@coderabbitai[bot] on GitHub · 2026-10-02 21:12 UTC · original
@wubooThanks for the fix. The supplied code now limits instruction relocation to hot-profile sites by default.KYTY_WIDE_STORE_MOVE=allexplicitly enables relocation at unprofiled sites. This addresses the finding.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
@wuboo on GitHub · 2026-10-02 21:29 UTC · original
On
tests/macosWideStoreTests.cppline 106 (outdated):Fixed in 9d7ed9fb. On native x86-64 the test now requires
WIFEXITED(status) && WEXITSTATUS(status) == 0, and under Rosetta it still requiresWIFSIGNALED. A child killed by a signal on real hardware no longer passes. I checked that the test fails when the native branch is forced.@coderabbitai[bot] on GitHub · 2026-10-02 21:37 UTC · original
On
tests/macosWideStoreTests.cppline 106 (outdated):@wubooThanks for the fix. The native assertion now rejects signal termination and nonzero exits. The Rosetta assertion remains unchanged.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
@wuboo on GitHub · 2026-10-03 17:50 UTC · original
Rebased on main (
19a9464b). Main now has a shared guest instruction trampoline area (#839 and the INSERTQ/RDPID/CLWB follow-up), so the split no longer reserves its own area on macOS. It usesinstruction_trampoline_*, andPatchReciprocalSquareRootsis gone because main moved that into the patcher.New commit
8863b3d7: the split starts its trampolines behind the bytes the patcher has already used. Without that, #1014 (patcher on macOS) plus this split would write over each other's trampolines when--amd-cpuis on. On this branch alone the patcher is still stubbed on macOS, so the offset is 0 and nothing changes.Full CPU ctest is identical to main: 35 pass including
macos_wide_store, and 16 GPU tests are skipped on this machine.@coderabbitai[bot] on GitHub · 2026-10-03 17:57 UTC · original · edited 2026-10-03 19:05 UTC
On
src/loader/x64InstructionEmulator.cppline 816 (outdated):🚀 Performance & Scalability | 🟠 Major | ⚡ Quick win
Exclude aligned stores (
VMOVAPS/VMOVAPD/VMOVDQA) from splitting.The header comment names one failure: a 256-bit store that crosses into a write-protected page. An aligned 256-bit store cannot cross a page:
VMOVAPS,VMOVAPDandVMOVDQAraise#GPwhen the address is not 32-byte aligned.The splitter still rewrites these stores, and that has a real cost:
instruction_trampoline_*area. That leaves less room for unaligned stores and for the patcher, and fills the area sooner (unsupported).ud2. Each execution then takes a SIGILL and runs the handler. Aligned stores are common in hot loops (vectorized memset/memcpy bodies, stack spills to aligned slots). Until the hot profile is rebuilt and the game restarted, such a loop takes one signal per iteration.vmovupsnever faults on misalignment.Only the unaligned forms can cross a page. Match only those forms here. The sweep at Line 1029-1034 can then skip the aligned forms as well.
♻️ Proposed fix
In the sweep:
✅ Addressed in commits
ad505e7toe225e7f@coderabbitai[bot] on GitHub · 2026-10-03 17:57 UTC · original · reviewed
Actionable comments posted: 1
🧹 Nitpick comments (1)
ℹ️ Review info
⚙️ Run configuration
d102773a-afd8-4be2-9c63-15457c2369ee📥 Commits
Reviewing files that changed from the base of the PR and between 9d7ed9fbd40ea1f4e9d369c006853dd3c4387842 and
8863b3d7de.📒 Files selected for processing (6)
src/loader/guestInstructionPatcher.cppsrc/loader/guestInstructionPatcher.hsrc/loader/runtimeLinker.cppsrc/loader/runtimeLinker.hsrc/loader/x64InstructionEmulator.cppsrc/loader/x64InstructionEmulator.hIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.
@wuboo on GitHub · 2026-10-03 19:05 UTC · original
On
src/loader/x64InstructionEmulator.cppline 816 (outdated):Agreed. A 32-byte aligned store can't cross a page, and the aligned forms must keep raising #GP on a misaligned address. Fixed in
ad505e77:IsPlainWideStoreand the candidate sweep now match only VMOVUPS/VMOVUPD/VMOVDQU. AddedTestAlignedStoresAreNotSplit(VMOVAPS/VMOVAPD/VMOVDQA ymm are left byte-for-byte unchanged and use no trampoline space); the existing crossing-store tests keep covering the unaligned VMOVUPS path.@wuboo on GitHub · 2026-10-03 19:05 UTC · original
On the review nitpick about the trampoline flush: Done in
e225e7f7: the trampoline flush now happens once after the segment loop, for[cursor_begin, cursor)only, and is skipped when nothing was written. The per-segment code flush stays, since each segment is patched separately.@coderabbitai[bot] on GitHub · 2026-10-03 19:06 UTC · original
On
src/loader/x64InstructionEmulator.cppline 816 (outdated):@wubooThanks for the fix. The supplied code excludes aligned stores from bothIsPlainWideStoreand the candidate sweep. This preserves their alignment faults and avoids unnecessary trampolines. The reportedTestAlignedStoresAreNotSplitcovers unchanged bytes and zero trampoline use.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.