fix(relinker): find relative jump tables across 2^63 without signed overflow #23003

Open
descambiado wants to merge 2 commits from upstream-pr/23003 into main
descambiado commented 2026-11-18 16:38:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by descambiado on 2026-11-18T16:38:10Z.


What

FindRelativeJumpTables forms a branch or lea target as static_cast<std::int64_t>(address) + displacement. For a segment mapped at or above 2^63 and a negative displacement the sum overflows. UBSan reports it on main: RelativeJumpTables.cpp:335:28: runtime error: signed integer overflow: -9223372036854775806 + -16 cannot be represented in type 'long int'.

It is the same class as #10653 and #11198, in four sites of this file: IsValid64BitInstruction, the function walk, the gap walk and the lea candidate search. The signed comparisons also misjudge a segment that straddles 2^63. FindRelativeJumpTables runs for every executable segment in RelinkerPipeline.cpp, not only with --to-intel.

The four sites now add as unsigned and reject a wrapped target through RelativeTarget, as #10680 did in Amd64OnlyConverter. The two offset-only sites in the file (TableEntries and the load scan) work on code offsets, not virtual addresses, and are unchanged.

Tested

Ubuntu 24.04 (WSL2), GCC 13.3, relinker-only build with -fsanitize=undefined -fno-sanitize-recover=undefined, head 2058bcd on main 01c8c4c.

  • control_flow_graph_tests with the new CheckRelativeJumpTablesAtHighBases and without the fix aborts at RelativeJumpTables.cpp:335:28. With the fix it passes.
  • In a plain Release build without sanitizers the new test also passes without the fix, because the overflow wraps silently there. It guards the change in the ASan and UBSan job, not in the regular builds.
  • 276 of 277 relinker-related ctest tests pass in one parallel run. The remaining one, conventions_unknown_nids, timed out at 20 s under the load of 14 parallel UBSan jobs and passes alone in 2.4 s.
  • tools/check_conventions.py --base upstream/main passes.
  • Two non-relinker test executables (shader_ir_use_rewrite_tests, request_serializer_tests) do not link with this plain -fsanitize=undefined flag set. They do not use the changed code.

Not tested: Windows, macOS, any title, and no end-to-end run of the CLI on a crafted ELF. I reached the function by calling FindRelativeJumpTables directly; its caller is RelinkerPipeline.cpp:508.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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:5380061295 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23003). Originally posted by [descambiado](https://github.com/descambiado) on 2026-11-18T16:38:10Z. --- ### What `FindRelativeJumpTables` forms a branch or lea target as `static_cast<std::int64_t>(address) + displacement`. For a segment mapped at or above 2^63 and a negative displacement the sum overflows. UBSan reports it on `main`: `RelativeJumpTables.cpp:335:28: runtime error: signed integer overflow: -9223372036854775806 + -16 cannot be represented in type 'long int'`. It is the same class as #10653 and #11198, in four sites of this file: `IsValid64BitInstruction`, the function walk, the gap walk and the lea candidate search. The signed comparisons also misjudge a segment that straddles 2^63. `FindRelativeJumpTables` runs for every executable segment in `RelinkerPipeline.cpp`, not only with `--to-intel`. The four sites now add as unsigned and reject a wrapped target through `RelativeTarget`, as #10680 did in `Amd64OnlyConverter`. The two offset-only sites in the file (`TableEntries` and the load scan) work on code offsets, not virtual addresses, and are unchanged. ### Tested Ubuntu 24.04 (WSL2), GCC 13.3, relinker-only build with `-fsanitize=undefined -fno-sanitize-recover=undefined`, head 2058bcd on `main` 01c8c4c. - `control_flow_graph_tests` with the new `CheckRelativeJumpTablesAtHighBases` and without the fix aborts at `RelativeJumpTables.cpp:335:28`. With the fix it passes. - In a plain Release build without sanitizers the new test also passes without the fix, because the overflow wraps silently there. It guards the change in the ASan and UBSan job, not in the regular builds. - 276 of 277 relinker-related ctest tests pass in one parallel run. The remaining one, `conventions_unknown_nids`, timed out at 20 s under the load of 14 parallel UBSan jobs and passes alone in 2.4 s. - `tools/check_conventions.py --base upstream/main` passes. - Two non-relinker test executables (`shader_ir_use_rewrite_tests`, `request_serializer_tests`) do not link with this plain `-fsanitize=undefined` flag set. They do not use the changed code. Not tested: Windows, macOS, any title, and no end-to-end run of the CLI on a crafted ELF. I reached the function by calling `FindRelativeJumpTables` directly; its caller is `RelinkerPipeline.cpp:508`. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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
PrivacyOnion commented 2026-11-18 16:39:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-18T16:39:30Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 2058bcd7db:

  • Conventions: tools/check_conventions.py passed.
  • Build: control_flow_graph_tests built cleanly.
  • CTest (control_flow_graph):
    1/1 Test #6132: control_flow_graph ...............   Passed    0.05 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/relinker/relinker/src/analysis/RelativeJumpTables.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #6132: control_flow_graph ...............   Passed    0.05 sec
    100% tests passed, 0 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Audited RelativeTarget helper in RelativeJumpTables.cpp:110-115 for overflow handling, unsigned wrap detection on next + static_cast(displacement) across [0, 2^64-1], and std::nullopt return on displacement wrapping.
    • Verified all four call sites of RelativeTarget in RelativeJumpTables.cpp (IsValid64BitInstruction at line 121, FindRelativeJumpTables function loop at line 342, gap walk at line 381, and lea table candidate search at line 419) against segment bounds [segmentVAddr, segmentEnd) and inspectEnd.
    • Checked for signed integer overflow, UB, and comparison signedness across VirtualAddress (uint64_t) and std::int64_t displacements, confirming no remaining signed arithmetic on address values.
    • Audited ControlFlowGraphTests.cpp:595-623 (CheckRelativeJumpTablesAtHighBases) to ensure high base addresses (0x7ffffffffffffff0, 0x8000000000000000, 0xffffffffffffff00) and backward branches properly exercise the fix without test pollution.
    • Checked AGENTS.md rules: no prohibited comments added, clean conventions, and conventional commit adherence.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5960971497 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23003#pullrequestreview-5960971497). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-18T16:39:30Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `2058bcd7db`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `control_flow_graph_tests` built cleanly. - **CTest** (`control_flow_graph`): ``` 1/1 Test #6132: control_flow_graph ............... Passed 0.05 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/relinker/relinker/src/analysis/RelativeJumpTables.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #6132: control_flow_graph ............... Passed 0.05 sec 100% tests passed, 0 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Audited RelativeTarget helper in RelativeJumpTables.cpp:110-115 for overflow handling, unsigned wrap detection on next + static_cast<VirtualAddress>(displacement) across [0, 2^64-1], and std::nullopt return on displacement wrapping. - Verified all four call sites of RelativeTarget in RelativeJumpTables.cpp (IsValid64BitInstruction at line 121, FindRelativeJumpTables function loop at line 342, gap walk at line 381, and lea table candidate search at line 419) against segment bounds [segmentVAddr, segmentEnd) and inspectEnd. - Checked for signed integer overflow, UB, and comparison signedness across VirtualAddress (uint64_t) and std::int64_t displacements, confirming no remaining signed arithmetic on address values. - Audited ControlFlowGraphTests.cpp:595-623 (CheckRelativeJumpTablesAtHighBases) to ensure high base addresses (0x7ffffffffffffff0, 0x8000000000000000, 0xffffffffffffff00) and backward branches properly exercise the fix without test pollution. - Checked AGENTS.md rules: no prohibited comments added, clean conventions, and conventional commit adherence. Looks Good To Me
MarcZX10 commented 2026-11-19 13:17:24 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the high-address target arithmetic and its regression coverage look sound based on the files reviewed.

What I ran

Reviewed head 2058bcd7db77a135fc480d1e57d5a6881bae85e0 using the prepared description, commit list, manifest, diff, and repo_read.py. No commands or tests were run.

Results

Check Result Note
Source review passed Two changed files read
Automated checks not run No execution environment available

Findings

Blocking

None.

Non-blocking

None.

Not verified

No tests were run. The author reports UBSan and relinker test results; these were not independently verified. No CLI run, Windows or macOS run, or physical-console behavior was verified. The complete contents of both changed files were read; files.json was not available in the prepared bundle.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5975726693 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23003#pullrequestreview-5975726693). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-19T13:17:24Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the high-address target arithmetic and its regression coverage look sound based on the files reviewed. ## What I ran Reviewed head `2058bcd7db77a135fc480d1e57d5a6881bae85e0` using the prepared description, commit list, manifest, diff, and `repo_read.py`. No commands or tests were run. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | Two changed files read | | Automated checks | not run | No execution environment available | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No tests were run. The author reports UBSan and relinker test results; these were not independently verified. No CLI run, Windows or macOS run, or physical-console behavior was verified. The complete contents of both changed files were read; `files.json` was not available in the prepared bundle. ## To get this to a good state Nothing is required.
edglz commented 2026-11-20 22:40:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by edglz on 2026-11-20T22:40:12Z.

Review: APPROVED


Summary

static_cast<std::int64_t>(address) + displacement overflows for segments at or above 2^63, and the signed comparisons misjudge segments straddling 2^63. RelativeTarget adds in unsigned arithmetic and detects wrap by comparing against the next-instruction address, which is the same approach as #10680 in Amd64OnlyConverter, applied to all four sites.

Verification

Environment: macOS 26, Apple M4 (arm64), Apple clang 21.0.0, CMake relinker-only Release build (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON), PR diff applied on current main.

Check Result
Full ctest suite with this PR (Release) 947/947 pass
control_flow_graph_tests with UBSan (-fsanitize=undefined -fno-sanitize-recover=undefined) and RelativeJumpTables.cpp reverted Aborts: RelativeJumpTables.cpp:335: signed integer overflow: -9223372036854775806 + -16
Same, with this PR Pass

Notes

As the description says, the new test only guards the change in sanitizer builds; in Release the overflow wraps silently.

Verdict

Approved.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6000333499 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23003#pullrequestreview-6000333499). Originally posted by [edglz](https://github.com/edglz) on 2026-11-20T22:40:12Z. **Review:** APPROVED --- ## Summary `static_cast<std::int64_t>(address) + displacement` overflows for segments at or above 2^63, and the signed comparisons misjudge segments straddling 2^63. `RelativeTarget` adds in unsigned arithmetic and detects wrap by comparing against the next-instruction address, which is the same approach as #10680 in `Amd64OnlyConverter`, applied to all four sites. ## Verification **Environment:** macOS 26, Apple M4 (arm64), Apple clang 21.0.0, CMake relinker-only Release build (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`), PR diff applied on current `main`. | Check | Result | |---|---| | Full ctest suite with this PR (Release) | 947/947 pass | | `control_flow_graph_tests` with UBSan (`-fsanitize=undefined -fno-sanitize-recover=undefined`) and `RelativeJumpTables.cpp` reverted | Aborts: `RelativeJumpTables.cpp:335: signed integer overflow: -9223372036854775806 + -16` | | Same, with this PR | Pass | ## Notes As the description says, the new test only guards the change in sanitizer builds; in Release the overflow wraps silently. ## Verdict Approved.
MarcZX10 commented 2026-11-26 01:59:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T01:59:44Z.

Review: APPROVED


Reviewed — looks good

The changed address calculations use unsigned wrap detection, and the added tests cover high virtual address bases; the review found no verified blocking defects.

What I ran

Reviewed head d61121a16f1c674e20b22d486af14a5edc0b5142. I inspected the prepared diff and both changed files, plus the VirtualAddress alias. No commands or tests were run.

Results

Check Result Note
Diff and source review passed Both changed files read
Tests not run Author reported sanitizer and relinker test runs

Findings

Blocking

None.

Non-blocking

None.

Not verified

Builds, tests, Windows and macOS behavior, and end-to-end CLI behavior were not independently verified. files.json was unavailable; the manifest lists both changed files as included, and both were read. No files were excluded in the manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6067551790 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23003#pullrequestreview-6067551790). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-26T01:59:44Z. **Review:** APPROVED --- # Reviewed — looks good The changed address calculations use unsigned wrap detection, and the added tests cover high virtual address bases; the review found no verified blocking defects. ## What I ran Reviewed head `d61121a16f1c674e20b22d486af14a5edc0b5142`. I inspected the prepared diff and both changed files, plus the `VirtualAddress` alias. No commands or tests were run. ## Results | Check | Result | Note | |---|---|---| | Diff and source review | passed | Both changed files read | | Tests | not run | Author reported sanitizer and relinker test runs | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Builds, tests, Windows and macOS behavior, and end-to-end CLI behavior were not independently verified. `files.json` was unavailable; the manifest lists both changed files as included, and both were read. No files were excluded in the manifest. ## To get this to a good state Nothing is required.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/23003:upstream-pr/23003'
git switch 'upstream-pr/23003'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/23003'
git switch 'upstream-pr/23003'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/23003'
git switch 'upstream-pr/23003'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/23003'
git switch 'main'
git merge --squash 'upstream-pr/23003'
git switch 'main'
git merge --ff-only 'upstream-pr/23003'
git switch 'main'
git merge 'upstream-pr/23003'
git push origin 'main'
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!23003
No description provided.