fix(relinker): agree on the syscall number between the two constant-load paths #10439
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10439"
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 KSWORK2001 on 2026-10-24T00:24:23Z.
What
The syscall scan names the FreeBSD syscall number in its refusal message through two paths:
ConstantSyscallNumberwalks the decoded instructions before the pattern throughConstantRaxLoad, andConstantSyscallNumberInBytes/ConstantRaxSetEndingAtscans the raw bytes back from the pattern outside the unwind functions. The two disagreed (#10422): the decoded walk reportedmov rax, imm32(48 C7 C0 imm32) without the sign-extension the register receives, and recognized neithermov rax, imm64(48 B8 imm64) norxor eax, eax-style zeroing (31/33 C0), while the byte window recognized all of these with sign-extension. The same load therefore named a different number depending on where it sat, andmov rax, -1; syscallreported 4294967295 where rax holds0xFFFFFFFFFFFFFFFF.ConstantRaxLoadnow recognizes the byte window's forms with the same extension:48 C7 C0 imm32sign-extended,48 B8 imm64,B8 imm32zero-extended, and31/33 C0zeroing. The REX bits that redirect the written register exclude a form (REX.Bmoves theB8register to r8-r15,REX.Rextends the xor source,REX.Bthe xor destination), whileREX.Xextends no register and is accepted (42 31 C0isxor eax, eax); an operand-size prefix also excludes the xor forms, since66 31 C0clears only AX. The backward walk now steps over a register-to-registermov/xorthat consists only of a REX prefix, an opcode and a ModRM byte and writes a register other than rax —MovesOtherRegisterThanRaxis applied to the slice that starts at the REX byte, and only when the whole instruction is that slice, sof2 49 89 casteps becauseREX.Bmakes it write r10 while8b 44 8b f8(a memory load into eax) or05 31 c9 00 00(an immediate add to eax) stops the walk.ConstantRaxLoadandConstantSyscallNumberreturnstd::optional<std::uint64_t>, so the sign-extended value no longer truncates throughstd::optional's converting constructor.Refuse and skip outcomes are unchanged inside the unwind functions and in images without unwind data, where both states end in a refusal either way. Outside the unwind functions the change is stricter: a pattern there is skipped when no number is established, and the byte window anchors at the syscall's opcode byte, so an
F2/F3prefix on a preceding instruction displaces that window andmainestablished no number for a prefixed pattern. The decoded walk reads through prefixes, so such patterns are now refused with their number: in thetest_sce_code_gapsfixture,31 C0 F3 0F 05 C3and48 B8 3B 00 00 00 00 00 00 00 F2 49 89 CA 0F 05 C3are skipped onmainand refused here with numbers 0 and 59. Both cases are pinned asprefixed-zeroedandprefixed-mov-rax-imm64inLOADED_NUMBERS. Refusing is the conservative direction: a real syscall with an established number no longer slips through a gap.With this change the decoded walk answers the same as the prefix-aware byte window of #10537 for the encodings measured in review, including
42 31 C0(0),44 B8 imm32(the immediate),mov eax,imm32; 47 31 C0/45 31 C0/45 89 C0/4D 89 C1(the immediate), and the 20-byte tail of the SDK inlinemprotectstub (74);rex-x-zeroed,rex-r-loadandlow-word-ending-in-movepin three of those on both paths inLOADED_NUMBERS, and the rest are mirrored intest_syscall_number.py(which runs the decoded walk without unwind data), with the over-acceptance controlsmov eax,imm32; 8b 44 8b f8andmov eax,imm32; 05 imm32reporting no number. Three numbers remain that only the byte window reports: the partial rax writes it folds (b0,66 b8, aslow-word-after-zeroingandlow-word-ending-in-moveshow),66 48 31 C0, where the operand-size prefix still excludes the xor althoughREX.Wmakes itxor rax, rax, and a41 B0 imm8(a write to r8b) or any other r8-r15 byte write after a load, which the decoded walk conservatively stops at rather than recognizing as a write to another register. Onmain's unprefixed byte window,mov eax,imm32; 47/45 31 C0still answers 0 by reading only the last two bytes; #10537's prefix-aware recognizer corrects that side.Tested
Relinker-only build on Apple silicon (macOS 27.0.1, AppleClang 21),
main999e612599against this branch. The new cases inSyscallScannerTests.cppandtest_syscall_number.pyfail onmain(mov rax, -1reports 4294967295,mov rax, imm64andxor eax, eaxreport no number,42 31 C0reports no number) and pass on the branch, including the negative controls (66 31 C0,66 48 31 C0,45 31 C0,44 31 C0,41 B8 imm32,mov eax,imm32; 41 B0 imm8, and the over-acceptance controlsmov eax,imm32; 8b 44 8b f8,mov eax,imm32; 05 imm32report no number). The two prefixed gap patterns above were run againstmain's relinker (skipped, exit 0) and the branch's (refused with 0 and 59);mov eax,59; 47 31 C0was reproduced reporting 0 on the previous head and reports 59 now; decoded42 31 C0reports 0; the over-acceptance casesmov eax,59; 8b 44 8b f8andmov eax,59; 05 imm32were reproduced reporting 59 on the previous head and report no number now, and the gapmov eax,59; 66 b8 89 c1was reproduced reporting 59 on the previous head and reports 49545 now.SyscallScannerTests.cppruns on the Linux CI host; it was also compiled and run directly here against the branch and againstmain's relinker to record the failure. Full suite: 231/231 on the native arm64 build and 231/231 on the x86-64 Rosetta build (CMAKE_OSX_ARCHITECTURES=x86_64), before the change onmainand with it.check_conventions.py --base origin/mainpasses;pr_overlap.pyreports one shared file with #8042, whoseSyscallScanner.cppchange is a single unrelated line (an instruction-scanner construction flag).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)Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-24T11:36:28Z.
Review: COMMENTED
Reviewed head e8fd90f merged onto
upstream/mainaeeaefad9(2026-10-24); the merge is clean, and so are merges onto the currentmain50aa1798eand together with #10479 and #10537.Environment: Windows 11 Home 10.0.26200, WinLibs GCC 15.2.0 UCRT, CMake 4.3.1, Ninja 1.13.2, Python 3.13.9,
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON, Release. My machine was under heavy unrelated CPU load, so I report targeted tests only; the relinker matrix is green on the PR.Results
syscall_number,sce_code_gaps,guest_syscall_trap,sdk_mman_syscallpass. Red/green: withSyscallScanner.cppreverted and the PR's tests kept,syscall_numberfails with('sign-extended', 'did not report syscall number 18446744073709551615'); restored, it passes. With #10479 and #10537 merged on top,syscall_numberandsce_code_gaps(including #10537's new cases) still pass. The C++syscall_scanneris Linux-only in CI and was not run here; the Python test carries the same byte sequences.objdump -M intel:48 c7 c0 ff ff ff ff=mov rax, 0xffffffffffffffff(sign-extended),48 31 c0=xor rax, rax,66 31 c0=xor ax, ax,45 31 c0=xor r8d, r8d,44 31 c0=xor eax, r8d. Every form the PR accepts does set rax to the reported constant; I found no over-acceptance.check_conventions.pypasses;pr_overlap.pylists only #8042 as not compared (one unrelated line, and #8042 no longer merges againstmain).Should fix (text)
SyscallScanner.cpp:302-313), and the decoded walk steps over F2/F3 prefixes that the byte window (MovesOtherRegisterThanRax,ConstantRaxSetEndingAt) cannot. Withtest_sce_code_gaps.executableand--skip-sce-module --registry:48 B8 09 00 00 00 00 00 00 00 F2 49 89 CA 0F 05 C3exits 0 onmain("Skipped 1 syscall byte pattern(s) ... 0x21e") and exits 2 on this branch ("Forbidden syscall instruction at code offset 0x21e: syscall number 9");31 C0 F3 0F 05 C3likewise flips from skipped (0x212) to refused with number 0; the unprefixed48 B8 imm64; 49 89 CA; 0F 05and the48 C7 C0forms are refused on both. The change is stricter, not looser, which is fine, but the body should say that gap patterns with an F2/F3-prefixedmov r10, rcxor a prefixedsyscallafter a48 B8 imm64or31/33 C0load are now refused wheremainskipped them.Nits
std::optional's converting constructor" misdescribesmain:ConstantRaxLoadreturned astd::uint32_tread without sign-extension (SyscallScanner.cpp:131-139onmain), and the only optional conversion (number = ConstantSyscallNumber(...)intooptional<uint64_t>, line 304) widened. The widened return type is what lets the sign-extended value through; nothing truncated before.kExtendedSourceZeroedStub/ "stub that zeroes eax from r8d" (SyscallScannerTests.cpp:124and:207, andextended-source-zeroedin the Python test):44 31 C0isxor eax, r8d(31 /r isXOR r/m32, r32; REX.R extends the source), eax becomeseax ^ r8d; the expected "no number" is right, the name is not.33 C0arm is accepted but never tested:48 33 C0(0) and a negative control such as44 33 C0=xor r8d, eax(no number) would pin it.Notes
40 31 C0(REX with no bits),66 48 31 C0(REX.W overrides 66),4C C7 C0 imm32and44 B8 imm32(REX.R is ignored for these); consequence is only a missing number in the message, and compilers do not emit them.66 31 C0arm this body says is left alone, and it reports number 0 for44 31 C0and41 33 C0, where this PR's decoded path (correctly) reports none; after both merge the two paths disagree on those bytes. I have left a note on #10537. Whichever of the two lands second should restate the resulting byte-window behaviour in its body.Not checked: the macOS counts (231/231); the C++
syscall_scanner; a full suite; two consecutive REX bytes (48 48 31 C0is reported as 0 by the PR; I did not verify the hardware semantics).I found no blocking issue: the encodings are right, the counter-check holds, and the fix does what #10422 asks. The one thing to settle before merge is the body's claim about unchanged refuse/skip outcomes. For disclosure: I have open relinker PRs (#10564, #10402, #10401, #10403, #10390, #10391); none touches
SyscallScanner.cpp.Written with AI assistance (Claude Code). I ran the builds, tests and fixture checks above on my Windows 11 machine.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T13:59:42Z.
Review: CHANGES_REQUESTED
Follow-up on exact head
e8fd90f4e0533d6b3a760336f9957644b700842a, considering the current #10537 head1f6c936b36446eeadc0c1e0f8bd37a4de8c65aa0. #10537 expands the byte-window recognizer, so the two paths now disagree on additional valid register encodings relevant to #10422:42 31 C0isxor eax,eax(REX.X does not extend either register field). The byte window now reports 0, butConstantRaxLoadrejects every REX value except0x48, so the decoded path reports no syscall number.47 31 C0isxor r8d,r8d. The byte window skips this write and can continue to a precedingmov eax,imm32; the decoded backward walk only skips49 89 CA, so it stops at this unrelated write and reports no number instead of the loaded value.This reintroduces the same path-dependent diagnostic the issue is meant to remove. Please align the decoded path with the prefix-aware byte recognizer (and add differential cases covering both unwind-backed and gap/fallback paths), or coordinate an alternative that makes both paths report the same value. My review of #10537's current build ran
ctest --test-dir build -R '^(sce_code_gaps|syscall_number)$' --output-on-failure: 2/2 passed, but those tests do not compare the same encodings across both paths.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T15:16:50Z.
Review: DISMISSED
Re-reviewed exact head
ad6fa74e4ac99bafe2e33605360d51ee75ba12bc. The head delta frome8fd90f4only renames an XOR test; the path disagreement relevant to #10422 remains. I built this exact head with GCC 12.2 in a relinker-only Release configuration.ctest -R '^(sce_code_gaps|syscall_number|syscall_scanner)$' --output-on-failurepassed 3/3, but two extra exact-head fixtures reproduced the uncovered bug: decoded42 31 C0has no number while the SCE-gap scan reports 0; a gap fixture withmov eax,59; 47 31 C0reports 0 though the XOR only writes R8D and should preserve 59.git diff --check upstream/main...HEADand the exact-head conventions check pass. Hosted relinker matrix and ASan pass; product jobs are skipped. Please resolve the decoded/byte-path mismatch and add differential coverage before merge.Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T16:45:50Z.
Review: COMMENTED
Reviewed head
0832fe91bmerged ontomain0119103fa(clean; still clean on79f061ba5, which touches none of these files), and that merge plus #105371f6c936b. Linux: WSL2 Ubuntu 24.04, GCC 13.3, relinker-only Release.What I ran
ctest -R '^(sce_code_gaps|syscall_number|syscall_scanner)$': 3/3 pass on the merge and on the merge with #10537. Withmain'sSyscallScanner.cppand your tests kept, all 3 fail (mov rax, -1gives 4294967295,prefixed-zeroedis skipped). So the C++syscall_scannernow has a Linux red/green too.42 31 c0gives 0, and the gapmov eax,59; 47 31 c0gives 59.66 48 31 c0andmov eax,59; 41 b0 05. Those are the gap-only cases the body names.Should fix: the new step-over accepts instructions that write eax
ConstantSyscallNumbernow callsMovesOtherRegisterThanRax(bytes, match.Length)on decoded instructions of any length (SyscallScanner.cpp:194). That helper was written for 2- or 3-byte raw windows. For any other length it setsrexto 0 and takes the last two bytes as opcode and ModRM (:161-167). So any 4+ byte instruction whose tail looks like89/8B/31/33+ a mod-11 byte gets stepped over, even when it writes eax.Example:
b8 3b 00 00 00 8b 44 8b f8 0f 05 c3. objdump givesmov eax,0x3b; mov eax,DWORD PTR [rbx+rcx*4-0x8]; syscall. The tail8b f8reads asmov edi, eax.main: no number in the decoded fixture and inside the function; the gap is skipped.Same with
8b 44 33 f8(mov eax,[rbx+rsi-8]),05 00 00 31 c9(add eax,0xc9310000) and66 b8 89 c1(mov ax,0xc189). The last one also creates a new disagreement in a gap. Theremain's byte window gives the right 49545, but the decoded walk now runs first and names 59. #10537 doesn't change any of these.Here's what I tried locally: step over only when the instruction is just (REX) + opcode + ModRM, and pass the helper the slice that starts at the REX byte:
With that, the 3 tests still pass and the four sequences above get
main's answers back (no number, skipped, 49545 in the gap). Every roboterspinne row gives the same answer as on your head.f2 49 89 castill steps over, but now because REX.B makes it r10. Today it passes only because the dropped REX leaves rm 2 (rdx). A no-number case likeb8 3b 00 00 00 8b 44 8b f8 0f 05 c3would pin this.Nits
kExtendedLoadStubis41 B8, which ismov r8d. Its description says "loads r9d".mov/xorwhose REX, opcode and ModRM bytesMovesOtherRegisterThanRaxaccepts" holds only for 2- and 3-byte instructions right now (see above).Checked and fine: every
ConstantRaxLoadform against the SDM encodings.48 C7 C0sign-extends.B8with W=0 and B=0 zero-extends.31/33 C0with R=0 and B=0, without66, zeroes rax. A REX before a legacy prefix is ignored. I found no over-acceptance there.Not checked: Windows and macOS, the full suite, real binaries.
Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-26T08:36:01Z.
Review: APPROVED
@KSWORK2001 Thanks, checked
0f45390a9merged ontomain204c6747d(2026-10-26); the merge is clean. Windows 11, GCC 15.2, relinker-only Release.ctest: 306 of 307 pass;patch_advisor_unitfails the same way on plainmain, andlong_pathsis skipped.syscall_number,sce_code_gaps,sdk_mman_syscall,guest_syscall_trapandinput_magicpass.SyscallScanner.cppfailssyscall_numberatload-then-memory-loadandsce_code_gapsatlow-word-ending-in-move; both report 59.main's fails atsign-extendedandprefixed-zeroed.main's answers back: no number, or a skipped gap, and 49545 for66 b8 89 c1in a gap. Aftermov eax, 59,89 c1,f2 49 89 ca,f2 49 89 c8and66 89 c1report 59, and the mprotect tail reports 74.test_syscall_number.py, where both add constants afterPREFIXED_CLOBBERED; whichever lands second keeps both blocks.That settles the points from my review and my follow-up; I found nothing else to change.
Written with AI assistance (Claude Code). I ran the checks above on my Windows 11 machine.