[CPU] Guest SSE4a EXTRQ raises SIGILL on any host without SSE4a (Dead Cells PPSA15552)
Emulator Version / Commit
0.0.2-beta.2 / 24b82a7 (main). Reproduced with a self-contained osx-x64 publish built exactly as CI does.
Platform: Apple M1 Max, macOS 26.5.2 (Darwin 25.5.0), osx-x64 build under Rosetta 2
Game: Dead Cells [PPSA15552]
Note: an earlier revision of this issue claimed Rosetta 2 also lacks AVX and AVX2, and concluded that recovering
extrqwas pointless because the following AVX block would fault anyway. That was wrong and is corrected below — see the comments. AVX/AVX2 execute correctly under Rosetta 2; SSE4a is the only extension that actually faults.
Bug Description
The emulator boots, selects the GPU, presents a first frame, and then dies deterministically with exit 132 (SIGILL) while executing guest code. Same faulting address on every run:
[LOADER][TRACE] posix-signal#1: sig=4 rip=0x000000080030C6ED fault=0x0 access=0 rsp=0x00006FFFF01FF280
[LOADER][INFO] NATIVE EXCEPTION CAUGHT!
[LOADER][INFO] Code: 0xC000001D
[LOADER][INFO] Exception Address: 0x000000080030C6ED
[LOADER][INFO] Host thread: managed=4 name='SharpEmu Emulation'
[LOADER][TRACE] posix-signal#1: recovered=False new_rip=0x000000080030C6ED
Root cause: the guest instruction at that address is AMD-only SSE4a, which the host does not implement.
Resolving the guest address through the SELF container (program header 0 maps to physical file offset 0xc070 via the SELF segment table — not the naive ELF offset) and disassembling at the exact faulting RIP:
0x80030C6ED: 66 0f 78 c1 28 00 extrq xmm1, 0x28, 0 groups: ['sse4a']
0x80030C6F3: c4 e3 79 02 c1 02 vpblendd xmm0, xmm0, xmm1, 2 groups: ['avx2']
0x80030C6F9: c5 fa e6 c0 vcvtdq2pd xmm0, xmm0 groups: ['avx']
extrq is SSE4a. SSE4a is an AMD-only extension — Intel has never implemented it. This is therefore not a macOS or Rosetta-specific bug: the PS5's Zen 2 cores have SSE4a and Sony's toolchain emits it, so the same guest code aborts on any Intel host, Windows and Linux included, while running fine on an AMD desktop. Rosetta 2 is simply another host without it.
Only the extrq faults. The AVX2/AVX instructions that follow it execute normally (verified by executing them directly — see comments).
Existing handling
DirectExecutionBackend.TryPatchSse4aExtrqBlend already rewrites this at load time, replacing the extrq+vpblendd pair with equivalent SSE4.1 pextrb/pinsrd. Its comment already records that Rosetta does not implement EXTRQ.
It does not fire here because it matches a fixed 12-byte sequence with xmm2 hard-coded:
66 0F 78 C2 28 00 extrq xmm2, 0x28, 0
C4 E3 79 02 C2 02 vpblendd xmm0, xmm0, xmm2, 2
PPSA15552 emits the identical sequence against xmm1 (...C1... rather than ...C2...), so the pattern misses by one byte per instruction and the opcode reaches the CPU. The startup log shows Patched ... 0 SSE4a EXTRQ blends.
Expected: guest SSE4a executes, or is recovered, on hosts that lack the extension. Actual: SIGILL; the signal bridge catches it, cannot recover, and the process dies with 132.
Relationship to #194: likely unrelated. #194 is a Rosetta thread-resume failure (ThreadContext::resume failed 37) with no SharpEmu-side exception. This is a clean caught #UD at an identifiable instruction.
Steps to reproduce
dotnet publish src/SharpEmu.CLI/SharpEmu.CLI.csproj -c Release -r osx-x64 --self-contained true -p:PublishDir=<dir>scripts/fetch-macos-moltenvk.sh <dir>./SharpEmu /path/to/PPSA15552/eboot.bin- Process exits 132 after the first presented frame.
An Intel host should reproduce the same fault without Rosetta involved, though I have no Intel machine to confirm that.
Log File
[LOADER][INFO] Vulkan device: Apple M1 Max (IntegratedGpu)
[LOADER][INFO] Vulkan VideoOut presented first frame: 3840x2160
[LOADER][INFO] Patched 0 TLS loads, 164 TLS stores, 0 stack-canary accesses, 0 SSE4a EXTRQ blends
...
[LOADER][TRACE] posix-signal#1: sig=4 rip=0x000000080030C6ED fault=0x0000000000000000 access=0 rsp=0x00006FFFF01FF280
[LOADER][INFO] NATIVE EXCEPTION CAUGHT!
[LOADER][INFO] Code: 0xC000001D
[LOADER][INFO] Exception Address: 0x000000080030C6ED
[LOADER][INFO] RIP: 0x000000080030C6ED
[LOADER][INFO] Host thread: managed=4 name='SharpEmu Emulation'
[LOADER][TRACE] posix-signal#1: recovered=False new_rip=0x000000080030C6ED
exit code: 132 (SIGILL), reproduced identically across runs with no external signal sent.
Correcting an error in my original report.
I wrote that "there is no interpreter or recompiler path to fall back to" on the basis that CpuExecutionEngine only exposes NativeOnly. That was wrong, and I've fixed the body.
DirectExecutionBackend.TryRecoverIllegalInstruction (in DirectExecutionBackend.IllegalInstruction.cs) already does per-instruction recovery from #UD: decode with Iced, evaluate against the trapped register/memory state, write the result into the CONTEXT, step RIP past the instruction, resume. It was added for precisely this situation — a host CPU missing an extension the guest uses — and Rosetta is one of those hosts, since it reports hw.optional.bmi1: 0 and hw.optional.bmi2: 0.
So the mechanism exists and is wired in at DirectExecutionBackend.Exceptions.cs:131. What's missing is coverage: it handles only the register-only BMI1/BMI2/ABM forms, which live in the CONTEXT's GPR slots. extrq (SSE4a) and the following AVX/AVX2 block operate on XMM/YMM state that the fallback doesn't model, so TryRecoverIllegalInstruction returns false and the process falls through to the diagnostics and dies.
That makes this a scope question rather than the structural dead-end I framed it as. I've reworded the issue accordingly. Apologies for the noise.
Measured the actual scope, since the open question here was whether extending TryRecoverIllegalInstruction to cover the vector ISA is worth doing. Short answer: not via that mechanism.
Disassembled program header 0 of the PPSA15552 eboot (physical offset 0xc070, 0x16bd5fc bytes, resolved through the SELF segment table) and counted every instruction whose CPUID group Rosetta 2 does not implement:
| Extension | Instructions | Distinct mnemonics | Status |
|---|---|---|---|
| AVX | 626,980 | 180 | traps |
| AVX2 | 37,569 | 63 | traps |
| BMI + BMI2 | 7,217 | 10 | already emulated by the existing fallback |
| SSE4a | 238 | 1 (extrq) |
traps |
Top offenders are the tell:
avx vmovsd 202974
avx vmulsd 69236
avx vaddsd 61088
avx vmovups 51242
avx vcvtsi2sd 42520
avx vxorpd 27824
avx vucomisd 25508
These are ordinary scalar double-precision operations in VEX encoding, not exotic vector work. The title was compiled with AVX codegen throughout, so effectively every floating-point operation in the binary is an instruction Rosetta refuses. AVX is ~11% of all decoded instructions.
That explains why the existing #UD fallback is fine for BMI and cannot extend here. BMI is ~7k static sites hit occasionally, so a signal round-trip per occurrence is affordable. Trapping on every float operation means a SIGILL, handler entry, Iced decode and emulated write-back for each one — microseconds in place of nanoseconds, and 180 mnemonics of implementation to get there. It would be correct and unusable.
extrq being 238 sites of one mnemonic makes it tempting as a first step. It isn't one: emulating it advances RIP six bytes into vpblendd (AVX2), and past that into vcvtdq2pd / vaddpd.
Caveat: this is a linear sweep over the full 23 MB segment, so data regions decode as garbage and inflate the absolute counts. The ratio is the point — 626,980 vs 238 survives any plausible garbage rate.
So the realistic options are a VEX→SSE rewrite at load time (most of these are 128-bit forms with exact SSE equivalents, though 3-operand VEX doesn't always map cleanly onto 2-operand SSE without a scratch register) or a genuine x86→arm64 recompiler. Both are architectural decisions rather than fixes, and I'm not proposing either — just closing off the "extend the fallback" path with numbers.
Worth noting the implication beyond this one game: if Sony's toolchain targets Zen 2 with AVX by default, this is not a Dead Cells quirk. Any title built the same way hits it on the first float. #330 adds the README caveat and makes the failure name itself; that may be as far as this can reasonably go on Apple Silicon.
Correcting this issue again, and this time the error was the central premise rather than a detail.
I claimed Rosetta 2 does not implement AVX or AVX2. That is false. I took it from sysctl hw.optional.avx1_0 / avx2_0 reporting 0, and never tested it. CPUID agrees with sysctl — and both are wrong about what the CPU will actually execute.
Emitting the instructions into an executable page and running them, as an osx-x64 process under Rosetta 2 on an M1 Max:
--- what CPUID claims ---
CPUID.01h:ECX[28] AVX = False
CPUID.07h:EBX[5] AVX2 = False
Avx.IsSupported (.NET) = False
Avx2.IsSupported (.NET) = False
--- what actually executes ---
vpblendd xmm1,xmm1,xmm1,2 (AVX2) -> EXECUTED
vaddpd xmm1,xmm1,xmm1 (AVX) -> EXECUTED
vcvtdq2pd xmm1,xmm1 (AVX) -> EXECUTED
extrq xmm1,0x28,0 (SSE4a) -> SIGILL, process died (132)
Rosetta 2 executes AVX and AVX2 while advertising neither. Only SSE4a genuinely faults.
Three conclusions I posted here are therefore withdrawn:
- "Rosetta 2 exposes nothing above SSE4.2." Wrong. It executes AVX/AVX2; it just does not advertise them.
- "Special-casing
extrqwould move the fault six bytes forward intovpblendd." Wrong.vpblenddruns. - The scope analysis (626,980 AVX instructions, ~11% of the binary, "correct and unusable"). Wrong, and wrong in the direction that matters: those instructions were never the problem. The AVX counts are real, but they are not blockers.
What that means practically. SSE4a is the only extension in the way, and in PPSA15552 it is a single mnemonic at 238 sites. With EXTRQ recovered from #UD, the title runs on Apple Silicon with no unrecovered fault at all — it reaches its first frames and then stalls in an Ngs2 import loop (uu94irFOGpA, ~4k calls), which is ordinary HLE-completeness work and looks related to #259. Not playable, but the instruction-set problem is gone rather than merely deferred.
One caveat worth stating plainly: recovering EXTRQ trades a fast, self-describing abort for an indefinite hang in that Ngs2 loop, which needs killing by hand. That is arguably a worse experience to triage than the crash, and argues for the two landing together.
On the existing patcher. TryPatchSse4aExtrqBlend already solves this for the xmm2 encoding and misses PPSA15552's xmm1 encoding by one byte per instruction. Recovering the opcode from #UD instead covers every register and both encodings, and needs no per-title pattern — which also sits better with CONTRIBUTING's preference for generic implementations over game-specific handling. Whether to replace the patcher or keep both is a maintainer call.
Apologies for the noise. The lesson I should have applied earlier: a CPU feature flag describes what the CPU advertises, not what it will execute, and under a translation layer those are different questions.
Status check, since the fix this issue converged on (recovering EXTRQ generically from #UD) already exists: DirectExecutionBackend.Amd64Compat.cs (TryRecoverSse4aExtractInsert, from #449/#482) does exactly this - decodes the immediate-form EXTRQ/INSERTQ, evaluates it against the trapped register state, writes the result back, steps RIP. It's register/encoding-agnostic (not tied to xmm2 like the older load-time patch), so it would cover PPSA15552's xmm1 case.
But it's deliberately gated off on Darwin. DirectExecutionBackend.PosixSignals.cs bridges XMM state into the fake CONTEXT buffer this recovery reads/writes by copying from the live FXSAVE image on Linux (TryHandlePosixFault, using LinuxGregsFpstateOffset/FxsaveXmmOffset, with a symmetric write-back after the handler runs so the result actually reaches the guest). On Darwin there's no equivalent bridge yet, so _posixXmmContextBridged stays false there by design - the comment on that flag spells out why: without it, the recovery would compute a result from a zeroed scratch buffer and silently discard whatever it "wrote". So on macOS/Rosetta this issue's exact crash still reproduces, correctly falling through to the diagnostic death rather than corrupting state.
What's missing is specifically: the byte offset of __fpu_xmm0 within Darwin's mcontext64/x86_float_state64_t (analogous to LinuxGregsFpstateOffset/FxsaveXmmOffset), wired into both directions (read before recovery, write-back before resuming) the same way the Linux branch does it.
I can't attempt this myself - I'm on Windows with no macOS/Rosetta environment to verify a raw mcontext offset against, and getting that kind of low-level ABI detail wrong is a memory-corruption risk, not just a non-functional patch. Flagging the precise shape of the remaining gap in case someone with Apple Silicon access wants to pick it up - the Linux branch in TryHandlePosixFault is the exact template to mirror.
Picking this up on Apple Silicon, following @zambasaurus's pointer to the exact gap.
I've wired up the Darwin XMM bridge. The generic EXTRQ/INSERTQ recovery (TryRecoverSse4aExtractInsert) already existed but was gated off on Darwin because _posixXmmContextBridged stays false there — the POSIX signal bridge only copied XMM state into/out of the fake CONTEXT on Linux, where it hangs off the kernel fpstate pointer. Darwin embeds x86_float_state64 inline in __darwin_mcontext64, so there was no bridge and the recovery correctly declined rather than compute from a zeroed scratch buffer.
The change resolves the inline XMM block directly for macOS (Linux keeps its fpstate-pointer path), copies it into the CONTEXT before recovery and back before sigreturn, and flips _posixXmmContextBridged true. I also enlarged the warm-up fake mcontext so the inline read doesn't over-run the stack buffer.
Confirmed the offset against the macOS SDK headers via offsetof (cross-compiled x86_64, run under Rosetta on an M1 Max):
__es(16) + __ss(168) => x86_float_state64 at +184
__fpu_xmm0 = 352 __fpu_xmm1 = 368 sizeof(__darwin_mcontext64) = 712
So mcontext + 352 for the xmm0 slot. Builds clean.
Still to do — runtime verification. I haven't yet confirmed the end-to-end round-trip on PPSA15552 (SIGILL → recovered=True → software EXTRQ, no exit 132). Per this thread the expectation is that it then advances to the Ngs2 import stall (#259) rather than crashing, i.e. this trades the fast abort for a hang, so it likely wants to land alongside that. I'll follow up with results before opening a PR.
Following up with the run results — and they change the conclusion, so I'm withdrawing the bridge I described above.
I ran PPSA15552 on a current-main build (off 6db095e) on the M1 Max under Rosetta 2. The exit-132 crash no longer reproduces. The title boots, presents its first frame, and then runs for as long as I let it (killed manually at 120s, ~14.1M imports) — it does not SIGILL.
The reason is not the #UD recovery path this issue was about. The startup log shows the load-time patcher doing the work:
[LOADER][INFO] ... 208 SSE4a EXTRQ blends
[LOADER][INFO] Vulkan VideoOut presented first frame: 3840x2160
There are zero runtime SSE4a #UD faults in the whole log — no sig=4 at 0x80030C6ED, no "emulating SSE4a in software", nothing recovered. All 208 extrq blends were rewritten before execution.
That's because Sse4aExtrqBlendPatch has been generalized since this issue was filed. It didn't exist at the commit I reported against (24b82a7); on current main it reads the source register from the ModRM r/m field instead of hard-coding xmm2, so it now matches PPSA15552's xmm1 encoding — exactly the one-byte miss described in the original report. That is the fix this issue converged on, already landed at load time.
Consequences:
- The SSE4a/ISA blocker in this issue is resolved on current
mainfor Dead Cells. What's left is downstream: the run gets stuck in sustainedlibSceAgccommand-buffer activity (register-patch / resource-registration calls, no further presented frames), which is a graphics-path problem unrelated to the CPU extension. - My Darwin XMM-context bridge is never exercised here — the patcher pre-empts every
extrq, so_posixXmmContextBridgedis never read. It would only matter for anextrq/insertqform the blend-patcher doesn't match, and I don't have a case that produces one. So I'm closing that PR rather than land a change with no observable effect. If a title later shows a non-blendextrq/insertqfaulting on Apple Silicon, the bridge is the missing piece and I'll revisit it then.
Net: on current main this specific crash appears fixed by the patcher. Happy to leave it open if maintainers want to track the remaining libSceAgc stall separately, or it can close.
Reporting back: this is fixed on main, and the fix removes the exact fragility described here.
TryPatchSse4aExtrqBlend no longer exists. The ahead-of-time byte-pattern patcher was replaced by a fault-time recovery that decodes with Iced rather than matching a fixed-length sequence:
DirectExecutionBackend.Amd64Compat.cs—TryRecoverSse4aExtractInsert, reached fromTryRecoverAmd64CompatInstructionon a#UD.- It matches on
instruction.Mnemonic == Mnemonic.Extrq/Insertqand resolves the destination withTryGetXmmOffset(instruction.GetOpRegister(0), ...).
The register is taken from the decoded instruction, so the failure mode in this report — the patcher matching a fixed 12-byte sequence with xmm2 hard-coded and missing by one byte per instruction when a title emits xmm1 — cannot occur. There is no fixed pattern left to miss.
Regression coverage is tests/SharpEmu.Libs.Tests/Cpu/Sse4aWindowsExceptionRecoveryTests.cs, whose theory runs destination registers 0, 1, 7, 8, 15 — register 1 being exactly the xmm1 case from this report — plus Sse4aPosixSignalRecoveryTests.cs for the Rosetta path and Sse4aBitFieldEmulatorTests.cs for the bit-field arithmetic.
Landed in 8ef5a54 "cpu: emulate AMD-only Zen 2 instructions in software (#449)".
No code change needed, so I have not opened a PR. Closing.
For reference, the general point still stands from this report — and a follow-up now exists: an instruction the ahead-of-time pass misses has no second chance. A fault-time fallback for unpatched fs:/gs: accesses is proposed in #1048, which is the same shape as these SSE4a and BMI/ABM recoveries.
Already fixed on main by 8ef5a54 (#449); verified covered by the existing SSE4a recovery tests. No change needed.