fix(relinker): resync the syscall scan past undecodable data regions #8042

Closed
plokmklo wants to merge 1 commit from upstream-pr/8042 into main
plokmklo commented 2026-10-19 18:39:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by plokmklo on 2026-10-19T18:39:54Z.


What

The syscall scan walks each executable segment linearly and any single undecodable byte failed the whole conversion with Cannot decode instruction. Real PRXs carry data inside executable segments — jump tables, constants, handler tables — so ubiservices.prx of The Rogue Prince of Persia (PPSA28155) stops dead at bare 0F 78 (offset 0xF72B9B), which is data, not an instruction: bare 0F 78 has no SSE4A meaning (EXTRQ needs 66, INSERTQ needs F2) and AMD64 has no VMREAD.

CodeInstructionCollector's padded scan already treats such regions as data; the discovery walk now does the same behind an explicit opt-in: MakeInstructionScanner(progress, resyncUndecodable). Resync prefers the next 16-byte boundary preceded by two 0xCC bytes (a genuine instruction boundary after function padding) and advances one byte when none remains. SyscallScanner opts in — it only discovers and refuses, it never rewrites.

The default stays STRICT: Amd64OnlyConverter's rewrite walk must keep failing loudly on an undecodable byte, because silently leaving one unconverted would corrupt the output binary. converterFailureOffsets passes unchanged.

Tested

Ubuntu 24.04: amd64_only_converter (new scannerEmbeddedDataResync case: embedded data no longer fails the resyncing scan, surrounding instructions still reported, truncated non-zero tail resyncs, strict default still refuses both), relinker_conversion, sdk_mman_syscall, call_site_resolver, conversion_diagnostics all pass. Cannot reproduce the PPSA28155 module itself (binary not available); the failing pattern is covered byte-for-byte in the new test.

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: #7547
  • Unblocks: PPSA28155 (The Rogue Prince of Persia) conversion — #7547
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4925661520 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8042). Originally posted by [plokmklo](https://github.com/plokmklo) on 2026-10-19T18:39:54Z. --- ### What The syscall scan walks each executable segment linearly and any single undecodable byte failed the whole conversion with `Cannot decode instruction`. Real PRXs carry data inside executable segments — jump tables, constants, handler tables — so `ubiservices.prx` of The Rogue Prince of Persia (PPSA28155) stops dead at bare `0F 78` (offset 0xF72B9B), which is data, not an instruction: bare `0F 78` has no SSE4A meaning (EXTRQ needs 66, INSERTQ needs F2) and AMD64 has no VMREAD. `CodeInstructionCollector`'s padded scan already treats such regions as data; the discovery walk now does the same behind an explicit opt-in: `MakeInstructionScanner(progress, resyncUndecodable)`. Resync prefers the next 16-byte boundary preceded by two `0xCC` bytes (a genuine instruction boundary after function padding) and advances one byte when none remains. `SyscallScanner` opts in — it only discovers and refuses, it never rewrites. The default stays STRICT: `Amd64OnlyConverter`'s rewrite walk must keep failing loudly on an undecodable byte, because silently leaving one unconverted would corrupt the output binary. `converterFailureOffsets` passes unchanged. ### Tested Ubuntu 24.04: `amd64_only_converter` (new `scannerEmbeddedDataResync` case: embedded data no longer fails the resyncing scan, surrounding instructions still reported, truncated non-zero tail resyncs, strict default still refuses both), `relinker_conversion`, `sdk_mman_syscall`, `call_site_resolver`, `conversion_diagnostics` all pass. Cannot reproduce the PPSA28155 module itself (binary not available); the failing pattern is covered byte-for-byte in the new test. ### Checklist - [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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical 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 - [ ] Depends on: none - [x] Fixes: #7547 - [ ] Unblocks: PPSA28155 (The Rogue Prince of Persia) conversion — #7547 - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-19 20:08:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T20:08:00Z.


I reproduced the Linux CI test failure on this head: the relinker-only build succeeds, but ctest --test-dir build-relinker --output-on-failure is 215/216, with executable_segments failing its rex-syscall case. That fixture expects a truncated nonzero instruction tail to fail with exit code 2 and produce no output; because SyscallScanner opts into resync for every segment, conversion now succeeds. The new scanner test checks that the default MakeInstructionScanner() stays strict, but it does not cover the production syscall scanner's opt-in path. Please preserve the rejection of a truncated nonzero tail while allowing the embedded-data case, and cover both through SyscallScanner.

The conventions check also fails because the Depends on and Unblocks checklist boxes are left unchecked. Please mark the dependency as none and fill the Unblocks item in the required format.

Validation here: Ubuntu Linux, GCC 12.2, Release; cmake --build build-relinker --parallel 4 passes. Full CTest: 215/216, only executable_segments fails as described. I have not run the target module because its binary was not supplied.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6276225644 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8042#issuecomment-6276225644). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-19T20:08:00Z. --- I reproduced the Linux CI test failure on this head: the relinker-only build succeeds, but `ctest --test-dir build-relinker --output-on-failure` is 215/216, with `executable_segments` failing its `rex-syscall` case. That fixture expects a truncated nonzero instruction tail to fail with exit code 2 and produce no output; because `SyscallScanner` opts into resync for every segment, conversion now succeeds. The new scanner test checks that the default `MakeInstructionScanner()` stays strict, but it does not cover the production syscall scanner's opt-in path. Please preserve the rejection of a truncated nonzero tail while allowing the embedded-data case, and cover both through `SyscallScanner`. The conventions check also fails because the `Depends on` and `Unblocks` checklist boxes are left unchecked. Please mark the dependency as none and fill the Unblocks item in the required format. Validation here: Ubuntu Linux, GCC 12.2, Release; `cmake --build build-relinker --parallel 4` passes. Full CTest: 215/216, only `executable_segments` fails as described. I have not run the target module because its binary was not supplied.
wachi35676 commented 2026-10-19 23:17:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-19T23:17:49Z.


Verdict: defect. With resync on, a forbidden syscall that follows undecodable bytes is skipped and the conversion succeeds.

SyscallScanner is the only check that refuses forbidden syscall instructions, and with this change it resyncs to the next 16-byte boundary preceded by cc cc. Any code between the undecodable byte and that boundary is never decoded. Probe image: make_image("register", "unwind", body=c3) from test_tls_function_coverage.py, with these bytes at file/vaddr 0x1600 outside the unwind function:

0f 78 11 22 33 44   0f 05   c3   90 90 90 90 90   cc cc   (next byte at 0x1610)
image main this PR (merged onto main 9127ea36d)
bytes above rc 2 Cannot decode instruction: Unsupported 0F 78 opcode without SSE4A prefix (offset 0x1600) rc 0; the output still contains 0f 05
same, with the 6 data bytes replaced by 90 (control) rc 2 Forbidden syscall instruction at code offset 0x1606 rc 2, same message

Both results are the same for the Linux default and --windows. A refusal therefore turns into an output that runs the raw syscall. Only a scan with no cc cc boundary before the end of the segment falls back to +1 stepping.

Also reproduced on Windows: executable_segments fails on the rex-syscall case, as @MarcZX10 reported for Linux. The truncated nonzero tail now converts (assert result.returncode == 2 and not output.exists(), case rex-syscall). Results:

  • Windows 11 Home 10.0.26300, Ryzen 5 9600X, WinLibs GCC 15.2.0 r7, relinker-only Release: ctest 211/212, with only executable_segments failing.
  • WSL Ubuntu 24.04, GCC 13.3: 215/217, with executable_segments and shader_cache_runtime_sources failing. The second also fails on main there.
  • Merged together with #8076 (which says it supersedes this PR), executable_segments still fails on Linux.

On the four homebrew titles I have (PPSA99005, PPSA47402, PPSA99581, and PPSA99995 with its Wine modules), relinked for Windows, macOS/Rosetta and Linux, the outputs are byte-identical to main. None of them hit an undecodable byte, so they don't exercise the change.

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6279477011 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8042#issuecomment-6279477011). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-19T23:17:49Z. --- Verdict: defect. With resync on, a forbidden syscall that follows undecodable bytes is skipped and the conversion succeeds. `SyscallScanner` is the only check that refuses forbidden `syscall` instructions, and with this change it resyncs to the next 16-byte boundary preceded by `cc cc`. Any code between the undecodable byte and that boundary is never decoded. Probe image: `make_image("register", "unwind", body=c3)` from `test_tls_function_coverage.py`, with these bytes at file/vaddr 0x1600 outside the unwind function: ``` 0f 78 11 22 33 44 0f 05 c3 90 90 90 90 90 cc cc (next byte at 0x1610) ``` | image | `main` | this PR (merged onto `main` 9127ea36d) | |---|---|---| | bytes above | rc 2 `Cannot decode instruction: Unsupported 0F 78 opcode without SSE4A prefix (offset 0x1600)` | rc 0; the output still contains `0f 05` | | same, with the 6 data bytes replaced by `90` (control) | rc 2 `Forbidden syscall instruction at code offset 0x1606` | rc 2, same message | Both results are the same for the Linux default and `--windows`. A refusal therefore turns into an output that runs the raw `syscall`. Only a scan with no `cc cc` boundary before the end of the segment falls back to `+1` stepping. Also reproduced on Windows: `executable_segments` fails on the `rex-syscall` case, as @MarcZX10 reported for Linux. The truncated nonzero tail now converts (`assert result.returncode == 2 and not output.exists()`, case `rex-syscall`). Results: - Windows 11 Home 10.0.26300, Ryzen 5 9600X, WinLibs GCC 15.2.0 r7, relinker-only Release: `ctest` 211/212, with only `executable_segments` failing. - WSL Ubuntu 24.04, GCC 13.3: 215/217, with `executable_segments` and `shader_cache_runtime_sources` failing. The second also fails on `main` there. - Merged together with #8076 (which says it supersedes this PR), `executable_segments` still fails on Linux. On the four homebrew titles I have (PPSA99005, PPSA47402, PPSA99581, and PPSA99995 with its Wine modules), relinked for Windows, macOS/Rosetta and Linux, the outputs are byte-identical to `main`. None of them hit an undecodable byte, so they don't exercise the change. AI-assisted: yes (Claude Code); results above were run on this machine.
MarcZX10 commented 2026-10-19 23:21:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T23:21:19Z.


I reproduced the Linux CI test failure on this head: the relinker-only build succeeds, but ctest --test-dir build-relinker --output-on-failure is 215/216, with executable_segments failing its rex-syscall case. That fixture expects a truncated nonzero instruction tail to fail with exit code 2 and produce no output; because SyscallScanner opts into resync for every segment, conversion now succeeds. The new scanner test checks that the default MakeInstructionScanner() stays strict, but it does not cover the production syscall scanner's opt-in path. Please preserve the rejection of a truncated nonzero tail while allowing the embedded-data case, and cover both through SyscallScanner.

The conventions check also fails because the Depends on and Unblocks checklist boxes are left unchecked. Please mark the dependency as none and fill the Unblocks item in the required format.

Validation here: Ubuntu Linux, GCC 12.2, Release; cmake --build build-relinker --parallel 4 passes. Full CTest: 215/216, only executable_segments fails as described. I have not run the target module because its binary was not supplied.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6279524361 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8042#issuecomment-6279524361). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-19T23:21:19Z. --- I reproduced the Linux CI test failure on this head: the relinker-only build succeeds, but `ctest --test-dir build-relinker --output-on-failure` is 215/216, with `executable_segments` failing its `rex-syscall` case. That fixture expects a truncated nonzero instruction tail to fail with exit code 2 and produce no output; because `SyscallScanner` opts into resync for every segment, conversion now succeeds. The new scanner test checks that the default `MakeInstructionScanner()` stays strict, but it does not cover the production syscall scanner's opt-in path. Please preserve the rejection of a truncated nonzero tail while allowing the embedded-data case, and cover both through `SyscallScanner`. The conventions check also fails because the `Depends on` and `Unblocks` checklist boxes are left unchecked. Please mark the dependency as none and fill the Unblocks item in the required format. Validation here: Ubuntu Linux, GCC 12.2, Release; `cmake --build build-relinker --parallel 4` passes. Full CTest: 215/216, only `executable_segments` fails as described. I have not run the target module because its binary was not supplied.
oneandonlydean commented 2026-10-20 01:43:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T01:43:42Z.


Thanks for this. #8076 targets the same crash, and we're going to carry the fix there: its remaining issue is narrower, and both PRs can't land together because they break executable_segments. Your review on #8076 is very welcome. We'll close this once #8076 merges.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6281361585 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8042#issuecomment-6281361585). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T01:43:42Z. --- Thanks for this. #8076 targets the same crash, and we're going to carry the fix there: its remaining issue is narrower, and both PRs can't land together because they break executable_segments. Your review on #8076 is very welcome. We'll close this once #8076 merges.

Pull request closed

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!8042
No description provided.