fix(relinker): collect --to-intel branch targets without signed overflow #10680

Merged
dkdhanda8 merged 1 commit from upstream-pr/10680 into main 2026-10-27 16:56:34 +00:00
dkdhanda8 commented 2026-10-24 17:21:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by dkdhanda8 on 2026-10-24T17:21:59Z.


What

_collectBranchTargets added the branch displacement to the next instruction address as std::int64_t. For an executable segment mapped at or above 2^63 the sum overflows and is undefined behaviour; the fuzzer repro attached to #10653 reports signed integer overflow at Amd64OnlyConverter.cpp:282. The addresses now add as unsigned values: a target that wraps past 2^64 is dropped, and every other target — including ones at or above 2^63, which the old target >= 0 check always dropped — goes through the same past-prefix and AMD-only checks as at lower addresses. The LEA jump-table path adds its displacement unsigned too; that is bit-identical to before without the overflow.

The consequence noted in the issue is also gone: a branch into an AMD-only instruction in a segment mapped across 2^63 was silently accepted before, and is now refused as at lower addresses.

Changed: core/relinker/codegen/src/Amd64OnlyConverter.cpp. The regression case in Amd64OnlyConverterTests.cpp maps segmentFixture at 0x8000000000000000 and asserts that the plain fixture still converts, a branch into the INSERTQ site is refused, and a backward branch whose sum stays below 2^64 does not change the conversion.

Method: the overflow-repro.elf attached to #10653 (SHA-256 3636a37f7422a0eaf3b247a22a65fdb730101666cdea934bda996daeda1cc2c4, verified) run under a relinker built with -fsanitize=undefined -fsanitize-undefined-trap-on-error: it traps on main and converts without a trap with this change. The new test case fails on main (the branch was accepted) and passes with the change, also under the trap-on-error build.

Fixes: #10653

Branch: fix/relinker-to-intel-branch-target-overflow, based on main 137a8fab8.

Tested

  • amd64_only_converter_tests passes with the change and fails without it on the new case; the whole converter suite also passes under -fsanitize=undefined -fsanitize-undefined-trap-on-error.
  • relinker --skip-sce-module --to-intel overflow-repro.elf out produces identical output on main and with the change (exit 2, FAIL: ET_NONE inputs do not specify an ELF object type).
  • ctest on the relinker-only build (Windows 11, MinGW-w64 GCC 15.2.0 x86_64-ucrt-posix-seh r7, Ninja, Release): the failure set is identical to main and is caused by this machine's Application Control policy refusing to execute freshly relinked outputs (WinError 4551), not by the change; the converter test binaries additionally pass under the trap-on-error UBSan build.
  • tools/check_conventions.py --base origin/main passes.

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); no silent stubs added
  • 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 #10653
  • Unblocks: none
  • GPUs tested: n/a (relinker only)
  • AI-assisted: yes

AI-assisted (ZCode): the fix, the regression test and the local verification were prepared with AI assistance; every line was reviewed and the fuzzer repro and sanitizer runs were executed locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4997503335 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10680). Originally posted by [dkdhanda8](https://github.com/dkdhanda8) on 2026-10-24T17:21:59Z. --- ### What `_collectBranchTargets` added the branch displacement to the next instruction address as `std::int64_t`. For an executable segment mapped at or above 2^63 the sum overflows and is undefined behaviour; the fuzzer repro attached to #10653 reports `signed integer overflow` at `Amd64OnlyConverter.cpp:282`. The addresses now add as unsigned values: a target that wraps past 2^64 is dropped, and every other target — including ones at or above 2^63, which the old `target >= 0` check always dropped — goes through the same past-prefix and AMD-only checks as at lower addresses. The LEA jump-table path adds its displacement unsigned too; that is bit-identical to before without the overflow. The consequence noted in the issue is also gone: a branch into an AMD-only instruction in a segment mapped across 2^63 was silently accepted before, and is now refused as at lower addresses. Changed: `core/relinker/codegen/src/Amd64OnlyConverter.cpp`. The regression case in `Amd64OnlyConverterTests.cpp` maps `segmentFixture` at `0x8000000000000000` and asserts that the plain fixture still converts, a branch into the INSERTQ site is refused, and a backward branch whose sum stays below 2^64 does not change the conversion. Method: the `overflow-repro.elf` attached to #10653 (SHA-256 `3636a37f7422a0eaf3b247a22a65fdb730101666cdea934bda996daeda1cc2c4`, verified) run under a relinker built with `-fsanitize=undefined -fsanitize-undefined-trap-on-error`: it traps on `main` and converts without a trap with this change. The new test case fails on `main` (the branch was accepted) and passes with the change, also under the trap-on-error build. Fixes: #10653 Branch: `fix/relinker-to-intel-branch-target-overflow`, based on `main` `137a8fab8`. ### Tested - `amd64_only_converter_tests` passes with the change and fails without it on the new case; the whole converter suite also passes under `-fsanitize=undefined -fsanitize-undefined-trap-on-error`. - `relinker --skip-sce-module --to-intel overflow-repro.elf out` produces identical output on `main` and with the change (exit 2, `FAIL: ET_NONE inputs do not specify an ELF object type`). - `ctest` on the relinker-only build (Windows 11, MinGW-w64 GCC 15.2.0 `x86_64-ucrt-posix-seh` r7, Ninja, Release): the failure set is identical to `main` and is caused by this machine's Application Control policy refusing to execute freshly relinked outputs (`WinError 4551`), not by the change; the converter test binaries additionally pass under the trap-on-error UBSan build. - `tools/check_conventions.py --base origin/main` passes. ### 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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); no silent stubs added - [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 #10653 - [x] Unblocks: none - [x] GPUs tested: n/a (relinker only) - [x] AI-assisted: yes AI-assisted (ZCode): the fix, the regression test and the local verification were prepared with AI assistance; every line was reviewed and the fuzzer repro and sanitizer runs were executed locally.
MarcZX10 commented 2026-10-24 18:48:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T18:48:18Z.

Review: APPROVED


Reviewed head bec41e57aed60eb9eedfd8724d4098d4cde22be7 against current main (0ea0f25e1bfbf63e90bc28a3ca1bc4cd2a3972f6). The unsigned conversion and direction-specific wrap test avoid signed-overflow UB while preserving modulo address arithmetic; the backward-branch case crosses the signed boundary and the forward case still rejects a branch into the AMD-only instruction. I verified the exact-head relinker-only target builds. The full registered converter CTest executable aborts with SIGILL on this host; in the isolated worktree I narrowed its test entry point to the new converterSegmentMappedAtInt64Max regression, ran that through CTest (1/1 passed), and restored the source. The PR also reports the full converter suite and fuzzer reproduction under trap-on-error UBSan on Windows. git diff --check, the current-main conventions checker, and merge-tree pass; the current base has not changed either modified converter file. Automatic hosted relinker jobs are absent for this contributor, and I do not have repository admin rights to dispatch them. No blocking source issue found.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5639484187 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10680#pullrequestreview-5639484187). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T18:48:18Z. **Review:** APPROVED --- Reviewed head `bec41e57aed60eb9eedfd8724d4098d4cde22be7` against current `main` (`0ea0f25e1bfbf63e90bc28a3ca1bc4cd2a3972f6`). The unsigned conversion and direction-specific wrap test avoid signed-overflow UB while preserving modulo address arithmetic; the backward-branch case crosses the signed boundary and the forward case still rejects a branch into the AMD-only instruction. I verified the exact-head relinker-only target builds. The full registered converter CTest executable aborts with `SIGILL` on this host; in the isolated worktree I narrowed its test entry point to the new `converterSegmentMappedAtInt64Max` regression, ran that through CTest (1/1 passed), and restored the source. The PR also reports the full converter suite and fuzzer reproduction under trap-on-error UBSan on Windows. `git diff --check`, the current-main conventions checker, and merge-tree pass; the current base has not changed either modified converter file. Automatic hosted relinker jobs are absent for this contributor, and I do not have repository admin rights to dispatch them. No blocking source issue found.
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!10680
No description provided.