fix(relinker): honor prefixes in the syscall number byte walk #10537

Merged
nedra23 merged 2 commits from upstream-pr/10537 into main 2026-10-28 00:49:14 +00:00
nedra23 commented 2026-10-24 08:55:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nedra23 on 2026-10-24T08:55:23Z.


What

The refusal message names the FreeBSD syscall number that a constant load before the pattern establishes. When the syscall pattern sits outside the unwind functions, ConstantSyscallNumberInBytes computes that number from the raw bytes, and its recognizers matched byte patterns without looking at their prefixes:

  • 41 B8 imm32 is mov r8d, imm32, but the B8 arm reported the immediate as the syscall number.
  • 41 31 C0 is xor r8d, eax, but the xor arm reported 0.
  • 66 31 C0 and 66 33 C0 clear only AX, but the xor arm reported 0.
  • 41 B0 imm8 is mov r8b, imm8; the B0 arm consumed two of its three bytes as mov al, misaligning the walk so that a number a previous load established was not reported.

ActivePrefixesBefore now reads the prefixes of each recognized form: the active REX is only the byte immediately before the opcode, 66 anywhere earlier in the prefix run sets the operand size, and a REX before another prefix is a dead byte, so 41 66 B8 is mov ax, imm16 with a dead byte in front. Per form:

  • B8: a REX without W or B does not change the register (rex.R and rex.X are ignored for B8+rd), so the immediate is still accepted; W, B and an operand-size override are not.
  • 31 C0 / 33 C0: a 16-bit pair is rejected, and so is any REX bit that extends an operand away from EAX: with ModRM C0 the two operands are the same register only while neither REX.R nor REX.B is set, so both bits (0x05) reject the pattern. 41 31 C0 and 44 33 C0 write r8d, and 41 33 C0 and 44 31 C0 are xor eax, r8d, so all four leave the number unknown and the site is skipped. REX.W (48 31 C0) still zeroes RAX, and REX.X (42 31 C0) extends no register without a SIB byte, so both are accepted as zero.
  • B0: REX.B makes it a write to r8b-r11b, so the walk passes the three bytes without recording an AL write; without it the AL write is recorded with the prefix bytes in its length.
  • 66 B8: the immediate is recorded as a partial write of the low word of RAX, whose upper bits stay unchanged, so it never establishes a number on its own; a dead REX byte in front is kept in the walked span only, so the walk stays on instruction boundaries and a preceding full write is still found.

The encodings were verified against GNU objdump (x86-64) before the change. A number is reported only when the walked instructions genuinely establish the value in RAX; where they do not, the site is now counted as skipped, the scanner's existing outcome for a pattern without a number, instead of naming a wrong one.

test_sce_code_gaps.py gains seven reported-number cases, each run on the Linux, Windows and macOS targets: 48 31 C0 and 42 31 C0 as zero, 44 B8 as mov eax, B8 3B 00 00 00; 41 B0 05 and 31 C0; 41 B0 05 where the mov r8b no longer stops the walk, 31 C0; 41 66 B8 where the dead REX byte is walked and the partial AX write is merged over the zeroed RAX, and B8 3B 00 00 00; 47 31 C0 where xor r8d, r8d does not touch EAX and the loaded 59 is kept. Eight cases, on the Linux target like the existing skipped cases, assert that no number is reported for 41 B8, 41 31 C0, 44 33 C0, 41 33 C0, 44 31 C0, 66 31 C0 and 66 33 C0, and for B8 3B 00 00 00; 41 33 C0, where xor eax, r8d writes an unknown value into EAX so the earlier load no longer counts. The existing cases, including the plain forms and the low-byte and low-word zeroing chains, are unchanged and pass.

#10439 fixes the decoded path (ConstantRaxLoad, ConstantSyscallNumber), and its body leaves the byte window's 66 31 C0 alone, noting that excluding it there would flip patterns from refused to skipped. This PR takes exactly that up, for the whole prefix family rather than the 66 forms alone, and makes the flip explicit and tested: the byte window now rejects the same prefixed zeroing the decoded path rejects there and recognizes the same plain forms. The functions are disjoint, and tools/pr_overlap.py reports no overlap with open pull requests.

Tested

Relinker-only build on Windows 10 Pro (build 19045), MSYS2 MinGW-w64 GCC 15.2.0 (mingw64, MSVCRT), CMake 4.2.3, Ninja, Python 3.14.3, main aeeaefad9 against this branch.

Focused ctest: syscall_number, sce_code_gaps (with the new cases), sdk_mman_syscall, guest_syscall_trap, input_magic - all pass. The three wrong numbers were reproduced before the change by running main's relinker on the same gap fixtures the new cases use (41 B8 names 5, 41 31 C0 and 66 31 C0 name 0); the new cases pass on the branch.

Full relinker suite: 236 of 238 pass. shader_cache_file_read and check_compile fail, reproduced identically on the unmodified base commit aeeaefad9 before this change: with this MSVCRT toolchain std::filesystem::create_directories refuses the \\?\-prefixed paths the cache test writes, and the clang found on PATH cannot start (exit 0xC0000139); the failing subtests invoke it. Both are environment-specific and untouched by this change.

syscall_scanner is registered only on Linux x86-64 (core/relinker/CMakeLists.txt), so it runs in the CI Linux relinker leg, not in this Windows run.

python3 tools/check_conventions.py --base origin/main - passes. python3 tools/pr_overlap.py --branch fix/relinker-syscall-number-rex-prefixes - no overlap with open pull requests.

No title or homebrew run: the change affects conversion diagnostics only.

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4994022994 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10537). Originally posted by [nedra23](https://github.com/nedra23) on 2026-10-24T08:55:23Z. --- ### What The refusal message names the FreeBSD syscall number that a constant load before the pattern establishes. When the `syscall` pattern sits outside the unwind functions, `ConstantSyscallNumberInBytes` computes that number from the raw bytes, and its recognizers matched byte patterns without looking at their prefixes: - `41 B8 imm32` is `mov r8d, imm32`, but the `B8` arm reported the immediate as the syscall number. - `41 31 C0` is `xor r8d, eax`, but the xor arm reported 0. - `66 31 C0` and `66 33 C0` clear only AX, but the xor arm reported 0. - `41 B0 imm8` is `mov r8b, imm8`; the `B0` arm consumed two of its three bytes as `mov al`, misaligning the walk so that a number a previous load established was not reported. `ActivePrefixesBefore` now reads the prefixes of each recognized form: the active REX is only the byte immediately before the opcode, `66` anywhere earlier in the prefix run sets the operand size, and a REX before another prefix is a dead byte, so `41 66 B8` is `mov ax, imm16` with a dead byte in front. Per form: - `B8`: a REX without W or B does not change the register (`rex.R` and `rex.X` are ignored for `B8+rd`), so the immediate is still accepted; W, B and an operand-size override are not. - `31 C0` / `33 C0`: a 16-bit pair is rejected, and so is any REX bit that extends an operand away from EAX: with `ModRM C0` the two operands are the same register only while neither REX.R nor REX.B is set, so both bits (`0x05`) reject the pattern. `41 31 C0` and `44 33 C0` write `r8d`, and `41 33 C0` and `44 31 C0` are `xor eax, r8d`, so all four leave the number unknown and the site is skipped. REX.W (`48 31 C0`) still zeroes RAX, and REX.X (`42 31 C0`) extends no register without a SIB byte, so both are accepted as zero. - `B0`: REX.B makes it a write to `r8b`-`r11b`, so the walk passes the three bytes without recording an AL write; without it the AL write is recorded with the prefix bytes in its length. - `66 B8`: the immediate is recorded as a partial write of the low word of RAX, whose upper bits stay unchanged, so it never establishes a number on its own; a dead REX byte in front is kept in the walked span only, so the walk stays on instruction boundaries and a preceding full write is still found. The encodings were verified against GNU objdump (x86-64) before the change. A number is reported only when the walked instructions genuinely establish the value in RAX; where they do not, the site is now counted as skipped, the scanner's existing outcome for a pattern without a number, instead of naming a wrong one. `test_sce_code_gaps.py` gains seven reported-number cases, each run on the Linux, Windows and macOS targets: `48 31 C0` and `42 31 C0` as zero, `44 B8` as `mov eax`, `B8 3B 00 00 00; 41 B0 05` and `31 C0; 41 B0 05` where the `mov r8b` no longer stops the walk, `31 C0; 41 66 B8` where the dead REX byte is walked and the partial AX write is merged over the zeroed RAX, and `B8 3B 00 00 00; 47 31 C0` where `xor r8d, r8d` does not touch EAX and the loaded 59 is kept. Eight cases, on the Linux target like the existing skipped cases, assert that no number is reported for `41 B8`, `41 31 C0`, `44 33 C0`, `41 33 C0`, `44 31 C0`, `66 31 C0` and `66 33 C0`, and for `B8 3B 00 00 00; 41 33 C0`, where `xor eax, r8d` writes an unknown value into EAX so the earlier load no longer counts. The existing cases, including the plain forms and the low-byte and low-word zeroing chains, are unchanged and pass. #10439 fixes the decoded path (`ConstantRaxLoad`, `ConstantSyscallNumber`), and its body leaves the byte window's `66 31 C0` alone, noting that excluding it there would flip patterns from refused to skipped. This PR takes exactly that up, for the whole prefix family rather than the `66` forms alone, and makes the flip explicit and tested: the byte window now rejects the same prefixed zeroing the decoded path rejects there and recognizes the same plain forms. The functions are disjoint, and `tools/pr_overlap.py` reports no overlap with open pull requests. ### Tested Relinker-only build on Windows 10 Pro (build 19045), MSYS2 MinGW-w64 GCC 15.2.0 (mingw64, MSVCRT), CMake 4.2.3, Ninja, Python 3.14.3, `main` `aeeaefad9` against this branch. Focused `ctest`: `syscall_number`, `sce_code_gaps` (with the new cases), `sdk_mman_syscall`, `guest_syscall_trap`, `input_magic` - all pass. The three wrong numbers were reproduced before the change by running `main`'s relinker on the same gap fixtures the new cases use (`41 B8` names 5, `41 31 C0` and `66 31 C0` name 0); the new cases pass on the branch. Full relinker suite: 236 of 238 pass. `shader_cache_file_read` and `check_compile` fail, reproduced identically on the unmodified base commit `aeeaefad9` before this change: with this MSVCRT toolchain `std::filesystem::create_directories` refuses the `\\?\`-prefixed paths the cache test writes, and the `clang` found on `PATH` cannot start (exit `0xC0000139`); the failing subtests invoke it. Both are environment-specific and untouched by this change. `syscall_scanner` is registered only on Linux x86-64 (`core/relinker/CMakeLists.txt`), so it runs in the CI Linux relinker leg, not in this Windows run. `python3 tools/check_conventions.py --base origin/main` - passes. `python3 tools/pr_overlap.py --branch fix/relinker-syscall-number-rex-prefixes` - no overlap with open pull requests. No title or homebrew run: the change affects conversion diagnostics only. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-24 10:58:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T10:58:07Z.

Review: APPROVED


Reviewed the exact head 79fef78d01c7b5da0ae71e4f9bb45e8709915734. The prefix-aware backward walk correctly distinguishes the reported REX and operand-size cases, including REX.B byte writes and the dead REX before 66 B8. The relinker-only Release build completed with GCC 12.2 (cmake -S . -B /tmp/anyps5-review-10537/build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DANYPS5_RELINKER_ONLY=ON, then target relinker), and ctest -R sce_code_gaps passed (1/1). git diff --check upstream/main...HEAD is clean. Hosted conventions, intake, and label checks pass. I did not run the full project build or non-Linux suites locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5637999608 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10537#pullrequestreview-5637999608). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T10:58:07Z. **Review:** APPROVED --- Reviewed the exact head `79fef78d01c7b5da0ae71e4f9bb45e8709915734`. The prefix-aware backward walk correctly distinguishes the reported REX and operand-size cases, including REX.B byte writes and the dead REX before `66 B8`. The relinker-only Release build completed with GCC 12.2 (`cmake -S . -B /tmp/anyps5-review-10537/build -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DANYPS5_RELINKER_ONLY=ON`, then target `relinker`), and `ctest -R sce_code_gaps` passed (1/1). `git diff --check upstream/main...HEAD` is clean. Hosted conventions, intake, and label checks pass. I did not run the full project build or non-Linux suites locally.
MarcZX10 commented 2026-10-24 12:17:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T12:17:47Z.

Review: CHANGES_REQUESTED


I am revising my earlier approval after the new review discussion and an independent check of the REX/ModRM fields.

At the current head 79fef78d01c7b5da0ae71e4f9bb45e8709915734, the 31 C0 / 33 C0 branch still accepts 44 31 C0 and 41 33 C0 as zeroing RAX. Both encode xor eax, r8d: opcode 31 /r uses ModRM r/m as destination and reg as source; 33 /r uses reg as destination and r/m as source. REX.R extends the reg field and REX.B extends r/m, so these prefixes select R8D as the source, not EAX as both operands. The result depends on R8D and cannot be reported as syscall number 0. Intel's SDM lists these operand directions and REX field extensions.

Please reject these two cases in ConstantRaxSetEndingAt, move their fixtures from the reported-number group to the no-number/skipped group, and correct the PR body. #10439's decoded path already declines both encodings, so this also restores agreement between the two paths. The current tests explicitly expect the wrong number, making this a correctness issue rather than only a documentation discrepancy.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638209873 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10537#pullrequestreview-5638209873). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T12:17:47Z. **Review:** CHANGES_REQUESTED --- I am revising my earlier approval after the new review discussion and an independent check of the REX/ModRM fields. At the current head `79fef78d01c7b5da0ae71e4f9bb45e8709915734`, the `31 C0` / `33 C0` branch still accepts `44 31 C0` and `41 33 C0` as zeroing RAX. Both encode `xor eax, r8d`: opcode `31 /r` uses ModRM r/m as destination and reg as source; `33 /r` uses reg as destination and r/m as source. REX.R extends the reg field and REX.B extends r/m, so these prefixes select R8D as the source, not EAX as both operands. The result depends on R8D and cannot be reported as syscall number 0. Intel's SDM lists these operand directions and REX field extensions. Please reject these two cases in `ConstantRaxSetEndingAt`, move their fixtures from the reported-number group to the no-number/skipped group, and correct the PR body. #10439's decoded path already declines both encodings, so this also restores agreement between the two paths. The current tests explicitly expect the wrong number, making this a correctness issue rather than only a documentation discrepancy.
MarcZX10 commented 2026-10-24 13:17:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T13:17:47Z.

Review: APPROVED


Reviewed exact head 1f6c936b36446eeadc0c1e0f8bd37a4de8c65aa0. The extended-operand correction rejects both REX.R and REX.B forms, moves all four xor eax,r8d/xor r8d,eax encodings to the skipped fixtures, and covers the case that clobbers a preceding EAX load. On the exact head, a Release relinker-only build with GCC 12.2 completed and ctest -R sce_code_gaps passed (1/1); git diff --check upstream/main...HEAD and conventions passed. I manually checked the overlap warning for #8042: it only changes SyscallScanner to opt into undecodable-byte resynchronization; the constant-number helper changes are separate. The focused gap test passed with that scanner behavior included. No full project or Windows/macOS local build was run; hosted build jobs are not present on this current head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638385749 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10537#pullrequestreview-5638385749). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T13:17:47Z. **Review:** APPROVED --- Reviewed exact head `1f6c936b36446eeadc0c1e0f8bd37a4de8c65aa0`. The extended-operand correction rejects both REX.R and REX.B forms, moves all four `xor eax,r8d`/`xor r8d,eax` encodings to the skipped fixtures, and covers the case that clobbers a preceding EAX load. On the exact head, a Release relinker-only build with GCC 12.2 completed and `ctest -R sce_code_gaps` passed (1/1); `git diff --check upstream/main...HEAD` and conventions passed. I manually checked the overlap warning for #8042: it only changes SyscallScanner to opt into undecodable-byte resynchronization; the constant-number helper changes are separate. The focused gap test passed with that scanner behavior included. No full project or Windows/macOS local build was run; hosted build jobs are not present on this current head.
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!10537
No description provided.