feat(libc): emulate the AMD-only CLZERO instruction on Windows #27532
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/27532"
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?
Imported from boykopovar/AnyPS5. Originally posted by akkadbakkad89 on 2026-11-26T16:30:05Z.
What
Emulates the AMD-only CLZERO instruction in the Windows crash reporter's vectored exception handler, the same pattern the SSE4a EXTRQ/INSERTQ emulation already uses.
CLZERO (
0F 01 FC) zeroes the cache line (64 bytes, aligned down) containing the linear address inrax. The PS5's Zen 2 CPU has it and titles use it in bulk-zero loops, but Intel hosts raiseSTATUS_ILLEGAL_INSTRUCTIONand the run dies in the crash reporter. The static conversion cannot always help:--to-intelrewrites fail when a branch target enters the instruction window a trampoline would move — observed on a large title where conversion aborts withBranch enters an AMD-only instruction(offset 0x22e111), so no--to-intelbinary can be produced at all.The emulation decodes the faulting bytes in a header-only helper (
ClzeroEmulation.hpp, mirroringSse4aEmulation.hppso a host test can drive it with crafted contexts), zeroes the line the context'sraxpoints at, advancesrippast the instruction and resumes. Zeroing memory is CLZERO's complete memory-visible effect, so the fill is exact.Cache-flush instructions (CLFLUSH/CLFLUSHOPT/CLWB) are deliberately out of scope: they are pure hints being handled statically in #26606, while CLZERO has memory-visible semantics and can never be NOP'd.
Tested
Windows 11, Intel Core i7-10700 (no CLZERO), NVIDIA RTX 3070 driver 617.42.
Title PPSA38500 (Game bring-up discussion #27523) executes
clzero; add rax, 0x40loops during initialization. Before this change the run died deterministically in the crash reporter withCLZERO is an AMD-only instruction this CPU lacksat the first CLZERO. With this change the fault is emulated and the run proceeds minutes deeper — through asset streaming, Vulkan device init, swapchain creation and PM4 command submission — to unrelated later failures in memory-management emulation (tracked in #27525).Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Summary by CodeRabbit
Design notes (moved out of the code per the conventions check — no functional change in the rebase):
The PS5's Zen 2 CPU has CLZERO and titles use it in bulk-zero loops, but Intel hosts raise STATUS_ILLEGAL_INSTRUCTION. The static conversion cannot always help:
--to-intelrewrites fail when a branch target enters the instruction window a trampoline would move, and code generated at run time is out of scope for conversion entirely. The crash reporter's vectored handler decodes the faulting bytes with this header, performs the operation on the CONTEXT and resumes. The header is kept header-only and free of process state so a host test can drive it with crafted CONTEXTs.CLZERO (AMD64 Architecture Programmer's Manual) is encoded 0F 01 FC and zeroes the cache line (64 bytes, aligned down) containing the linear address in rax. Zeroing memory is its complete memory-visible effect, so a plain fill of the line is the exact emulation. A 64-byte aligned cache line never straddles a page boundary, so one probe covers the line. When the bytes are not CLZERO or the line is not writable the handler returns false and leaves the context untouched: real hardware faults on such a target, so the correct outcome is the existing exception path rather than a nested fault inside the vectored handler. The known-gap form of this is in the debt fragment
libc-clzero-emulation.md.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T16:52:55Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: validate the target cache line before zeroing it so an invalid RAX address cannot cause a nested access violation in the exception handler.
What I ran
Reviewed head
d8680926155add78f5f9b0903ba03f8be1c1c17cusingrepo_read.pyreader tools. No commands or tests were run.Results
Findings
Blocking
ClzeroEmulation.hpp:33:std::memsetwrites 64 bytes to the aligned address fromcontext.Raxwithout checking that the entire range is writable. If the guest address is invalid or crosses into an inaccessible page, this faults inside the vectored exception handler while handling the original illegal instruction; it can prevent the existing crash and exception handling path from running. Validate/probe the full cache-line range before writing, and let an invalid target continue through the existing exception path.Non-blocking
None.
Not verified
No tests were run. The description's reported Windows 11 and Intel Core i7-10700 execution was not independently verified. Read
ClzeroEmulation.hppand the relevant ranges ofCrashReport.cpp;Sse4aEmulation.hppwas read for comparison. Both changed files listed in the manifest were included and read.To get this to a good state
Validate that the full 64-byte destination range is writable before zeroing it, then run focused tests for valid aligned and unaligned addresses, invalid addresses, and addresses at a page boundary.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T18:05:15Z.
Review: COMMENTED
Reviewed — comments
The CLZERO handler follows the existing exception emulation pattern, but its writable-memory check does not reject every non-writable Windows page protection.
What I ran
Reviewed head
e01d609d2fa18d79bbfbc67b027cf2fe0a96ff16. No commands or tests were run; this review used the prepared diff and the pinned reader.Results
Findings
Blocking
None.
Non-blocking
ClzeroEmulation.hpp:35checks whether any writable protection bit is present, but Windows protection constants are not independent flags. For example,PAGE_EXECUTE(0x10) combined withPAGE_READWRITE(0x04) passes this bitmask even though the page is execute-only.Emulatethen callsmemseton that page, which can fault inside the exception handler instead of leaving the context untouched as intended. Compare the masked base protection (info.Protect & 0xff) against the writable protection values, asCrashReport.cppdoes when checking executable pages.Not verified
No tests were run. The change adds no tests for the new helper. I read the complete changed
ClzeroEmulation.hppand the changed portions ofCrashReport.cpp, plus the related SSE4a helper and theReadCodeimplementation.review/files.jsonwas not available through the reader; the manifest lists both changed files as included. No other changed files were excluded in the manifest.To get this to a good state
Nothing is required. Consider tightening the protection check and adding tests for readable/writable page protections and rejected targets.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T18:57:37Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the point below is optional.
What I ran
Reviewed head
6c837fe7fd282571e08ff1fdf16cf974f03f7c47from the prepared diff and source context. No commands or tests were run.Results
Findings
Blocking
None.
Non-blocking
Not verified
The PR description reports a Windows 11 run on an Intel Core i7-10700, but that run was not independently verified. No tests were run. Both files listed as reviewed in the manifest were read; no files were excluded by the prepared manifest.
To get this to a good state
Nothing is required. Consider adding the host test coverage described above.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-29T18:46:01Z.
Review: COMMENTED
Actionable comments posted: 1
df9f8038-e957-4217-98ad-8704b98be1e2Reviewing files that changed from the base of the PR and between
6c837fe7fdand8c0bba4561.core/libs/prx/libc/src/specifics/windows/CrashReport.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-01T09:50:11Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: handle the valid address-size-prefixed CLZERO form with its correct address semantics and instruction length.
What I ran
Reviewed head
37c7760a911775d093c8fb4976c9cd8c2a8bae6dagainst the prepared diff; no merged-preview check was run. Read all changed files listed as reviewed infiles.json; no files were excluded. No tests or builds were run. The reader returned source context, but its audit logging failed because the run folder is read-only.Results
Negative control
Not run; no test was run to confirm that an unsupported encoding remains unhandled.
Findings
Blocking
core/libs/prx/libc/src/specifics/windows/ClzeroEmulation.hpp:14recognizes only the three-byte unprefixed opcode, and the emulation always usesRAXand advancesRIPby three bytes. A valid67 0F 01 FCform therefore remains an illegal-instruction failure on Intel hosts instead of emulating CLZERO with the address-size override. Decode the supported prefix form, use its effective address, and advance over the full instruction.Non-blocking
None.
Sources
rAX; the architecture manual describes address-size override prefixes.Not verified
No tests, builds, or physical-console behavior were run. The files read were
core/libs/prx/libc/src/specifics/windows/ClzeroEmulation.hpp,core/libs/prx/libc/src/specifics/windows/CrashReport.cpp, anddocs/dev/debt/changes/libc-clzero-emulation.md;files.jsonlists no excluded files.To get this to a good state
Support and cover the address-size-prefixed form, including its effective address and full instruction length. Then rerun the relevant host-side emulation checks on the updated head.
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-12-01T09:52:42Z.
Review: COMMENTED
🏛️ Architecture findings (1)
Advisory architecture findings (1)
💬 Code review
Actionable comments posted (1)
7b6bd2e6-f7d8-4b93-95ed-f448c1fc1f60Reviewing files that changed from the base of the PR and between
8c0bba4561and37c7760a91.core/libs/prx/libc/src/specifics/windows/ClzeroEmulation.hppdocs/dev/debt/changes/libc-clzero-emulation.mdIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-01T10:08:35Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; no follow-up is required.
What I ran
Reviewed head
f0139a5df2728451866f6bd7be0519ffc16f49a5. Read the prepared description, commits, manifest, diff, all three included changed files, and targeted source excerpts using the pinnedrepo_read.pyreader. No tests or builds were run.Results
Negative control
Not run; no test was added or executed to confirm that non-CLZERO bytes are rejected.
Findings
Blocking
None.
Non-blocking
None.
Sources
Not verified
No tests or builds were run. The reported Windows 11 runtime behavior and console behavior were not independently verified. Files reviewed:
ClzeroEmulation.hpp,CrashReport.cpp, andlibc-clzero-emulation.md;files.jsonlists no excluded files.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-01T13:13:47Z.
Review: APPROVED
Reviewed — looks good
The CLZERO emulation path validates the target line before clearing it and advances the saved instruction pointer only after successful emulation.
What I ran
Reviewed head
3488708a7ad3b4aa2952ba2fc200c57952d87695usingrepo_read.pyand the prepared diff. No build or test was run.Results
Negative control
Not run; no new test was run or modified to confirm it fails when the emulation is disabled.
Findings
Blocking
None.
Non-blocking
None.
Sources
Not verified
The reported Windows 11 hardware run and console behavior were not independently verified. Reviewed changed files:
core/libs/prx/libc/src/specifics/windows/ClzeroEmulation.hpp,core/libs/prx/libc/src/specifics/windows/CrashReport.cpp, anddocs/dev/debt/changes/libc-clzero-emulation.md. No files were excluded.To get this to a good state
Nothing is required.