fix(relinker): place 256-bit store islands across 2^63 without signed overflow #11232

Merged
MarcZX10 merged 4 commits from upstream-pr/11232 into main 2026-10-26 21:39:59 +00:00
MarcZX10 commented 2026-10-25 22:18:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T22:18:43Z.


What

--to-rosetta lowers a 256-bit store that has no room for a stub jump through an island: the instructions after the store move into a stub, a jmp rel32 to the lowered store is written in their place, and a jmp rel8 over the store reaches that island. _islandAt and _buildIsland in core/relinker/codegen/src/Amd64OnlyConverter.cpp compute four distances by casting both addresses to std::int64_t and subtracting:

Line Distance
169 reach, from the end of the short jump to the island
203 the stub's Seek back to the store
205 its JumpTo after the store
213 the rel8 displacement written over the store

When a code segment crosses 2^63, the store sits just below it and the island just above, so the two signed operands have opposite signs and the subtraction overflows, which is undefined behaviour. #11198 reports it with UBSan (Amd64OnlyConverter.cpp:169:66: runtime error: signed integer overflow). The wrapped result happens to be right in practice, so no wrong output has been seen.

A _distance(to, from) helper now subtracts the addresses as std::uint64_t and converts the result to std::int64_t, which C++20 defines modulo 2^64. That gives the exact signed distance for any two addresses less than 2^63 apart, and the island window is a few bytes. The range checks after it (reach against int8, the stub builder's own limits) are unchanged. There is one commit per site group, then the test:

  1. the island's reach (line 169), which adds the helper;
  2. the stub's seek and return (lines 203 and 205);
  3. the short jump written over the store (line 213);
  4. converterWideStoreIslandAcrossInt64Max.

The test converts #11198's island fixture with the segment mapped at 0x1000 and at 0x8000000000000000 - 0x10. At the high base it requires the same segment bytes, trampoline offsets, lengths, bodies and relocations as at 0x1000, and EB 0D for the short jump. Without sanitizers it guards the result; under -fsanitize=undefined it also catches the overflow. relinker-asan runs ASan only, while the nightly ASan and UBSan workflow in #11062 and any UBSan build would report it.

#10680 and #11033 fix the same kind of overflow in _collectBranchTargets (#10653). They change other lines and do not touch the island path.

Fixes: #11198

Tested

  • On an ubuntu-latest fork runner, relinker-only Debug builds: amd64_only_converter passes without sanitizers and with -fsanitize=undefined -fno-sanitize-recover=undefined. With Amd64OnlyConverter.cpp restored from main and the new test kept, the UBSan build fails with Amd64OnlyConverter.cpp:169:126: runtime error: signed integer overflow.
  • Linux x86-64, GCC 12.2, with #11198's own driver built against the converter sources with -fsanitize=undefined -fno-sanitize-recover=undefined. On main it stops at Amd64OnlyConverter.cpp:203:21: runtime error: signed integer overflow. With this branch it prints 2 trampolines, short jump EB 0D for both bases. The full amd64_only_converter test cannot run on this host: it executes instructions the CPU lacks, also on main.
  • python3 tools/check_conventions.py --base upstream/main and git diff --check upstream/main pass. tools/pr_overlap.py --branch lists only #8042 and #8236, which also add tests to Amd64OnlyConverterTests.cpp, with no conflicting lines. Windows and macOS were not run.

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 (not applicable; no third-party code added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: #11198
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5009281660 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11232). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T22:18:43Z. --- ### What `--to-rosetta` lowers a 256-bit store that has no room for a stub jump through an island: the instructions after the store move into a stub, a `jmp rel32` to the lowered store is written in their place, and a `jmp rel8` over the store reaches that island. `_islandAt` and `_buildIsland` in `core/relinker/codegen/src/Amd64OnlyConverter.cpp` compute four distances by casting both addresses to `std::int64_t` and subtracting: | Line | Distance | |---|---| | 169 | `reach`, from the end of the short jump to the island | | 203 | the stub's `Seek` back to the store | | 205 | its `JumpTo` after the store | | 213 | the `rel8` displacement written over the store | When a code segment crosses 2^63, the store sits just below it and the island just above, so the two signed operands have opposite signs and the subtraction overflows, which is undefined behaviour. #11198 reports it with UBSan (`Amd64OnlyConverter.cpp:169:66: runtime error: signed integer overflow`). The wrapped result happens to be right in practice, so no wrong output has been seen. A `_distance(to, from)` helper now subtracts the addresses as `std::uint64_t` and converts the result to `std::int64_t`, which C++20 defines modulo 2^64. That gives the exact signed distance for any two addresses less than 2^63 apart, and the island window is a few bytes. The range checks after it (`reach` against `int8`, the stub builder's own limits) are unchanged. There is one commit per site group, then the test: 1. the island's reach (line 169), which adds the helper; 2. the stub's seek and return (lines 203 and 205); 3. the short jump written over the store (line 213); 4. `converterWideStoreIslandAcrossInt64Max`. The test converts #11198's island fixture with the segment mapped at `0x1000` and at `0x8000000000000000 - 0x10`. At the high base it requires the same segment bytes, trampoline offsets, lengths, bodies and relocations as at `0x1000`, and `EB 0D` for the short jump. Without sanitizers it guards the result; under `-fsanitize=undefined` it also catches the overflow. `relinker-asan` runs ASan only, while the nightly ASan and UBSan workflow in #11062 and any UBSan build would report it. #10680 and #11033 fix the same kind of overflow in `_collectBranchTargets` (#10653). They change other lines and do not touch the island path. Fixes: #11198 ### Tested - On an `ubuntu-latest` fork runner, relinker-only Debug builds: `amd64_only_converter` passes without sanitizers and with `-fsanitize=undefined -fno-sanitize-recover=undefined`. With `Amd64OnlyConverter.cpp` restored from `main` and the new test kept, the UBSan build fails with `Amd64OnlyConverter.cpp:169:126: runtime error: signed integer overflow`. - Linux x86-64, GCC 12.2, with #11198's own driver built against the converter sources with `-fsanitize=undefined -fno-sanitize-recover=undefined`. On `main` it stops at `Amd64OnlyConverter.cpp:203:21: runtime error: signed integer overflow`. With this branch it prints `2 trampolines, short jump EB 0D` for both bases. The full `amd64_only_converter` test cannot run on this host: it executes instructions the CPU lacks, also on `main`. - `python3 tools/check_conventions.py --base upstream/main` and `git diff --check upstream/main` pass. `tools/pr_overlap.py --branch` lists only #8042 and #8236, which also add tests to `Amd64OnlyConverterTests.cpp`, with no conflicting lines. Windows and macOS were not run. ### 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 (not applicable; no third-party code added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: #11198 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
roboterspinne commented 2026-10-26 18:05:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-26T18:05:01Z.

Review: APPROVED


Reviewed 9da4efcce merged onto main 570089a80 (2026-10-26); the merge is clean. Windows 11, i9-12900H, WinLibs GCC 15.2, relinker-only Release.

Results

  • Full ctest: 308 of 309 pass; patch_advisor_unit fails the same way on plain main. amd64_only_converter passes.
  • Counter-check with main's Amd64OnlyConverter.cpp and your test (MinGW has no UBSan runtime, so I used trapping builds):
    • Without a sanitizer it passes, as you say: on main the wrapped result is right.
    • With -fsanitize=signed-integer-overflow -fsanitize-undefined-trap-on-error it traps in converterWideStoreIslandAcrossInt64Max, in _islandAt at main's line 169.
    • With -ftrapv it aborts in _buildIsland at main's line 203.
    • Your file passes plain, with the trap and with -ftrapv.
  • Putting the signed subtraction back at one site at a time traps at lines 170, 204, 206 and 214 in turn, each inside the new test. So it reaches all four sites.
  • A mutant that is wrong only when the two addresses straddle 2^63 fails only the new test ("An island across 2^63 changed the segment bytes"), so the test does guard the result without a sanitizer.
  • _distance is exact here. CMAKE_CXX_STANDARD is 20, so the conversion to int64_t is defined, and every island distance stays inside the 0x100 search window. The int8 reach check and the stub's rel32 checks are unchanged.

Nit: nothing places an island at the rel8 limit. Dropping + kShortJumpSize from line 170 passes both your suite and main's. With that mutant, a backward island at reach -129 or -130 is accepted and line 214 writes rel8 +127/+126. A fixture accepted at -128 and 127 and refused at -129 and 128 would pin it. The gap is older than this PR, but the PR rewrites the line.

Notes, nothing to change here

  • Islands across 2^63 can still be placed over a branch target at or above 2^63 until #10680 lands. _collectBranchTargets drops those targets (line 284, target >= 0). With your fixture plus a jump to offset 0x10, which is 2^63 at the high base, the island window covers that target on main and on this PR. With #10680 merged, the high base gives the same placement as the low base. So "do not touch the island path" holds for the lines, but a correct island across 2^63 needs both PRs.
  • The description says #11033 fixes _collectBranchTargets. It changes CodeInstructionCollector::Collect instead; the conclusion that it doesn't overlap stays true.
  • The pr_overlap line in Tested is stale: #8042 is closed and #8236 has merged. Today the overlap is #11413, which conflicts only on the adjacent main() registration lines in Amd64OnlyConverterTests.cpp. Keeping both calls passes, plain and under the trap. Amd64OnlyConverter.cpp auto-merges.
  • relinker-asan runs only the tls_function_coverage label, so amd64_only_converter runs under no sanitizer in main's CI today.

Not checked: a real UBSan build, Linux and macOS, #11198's driver, and running converted code above 2^63.

I found nothing that needs to change before merge. For disclosure: none of my open PRs touches these files.

Written with AI assistance (Claude Code). I ran the builds, the counter-checks and the mutants above on my Windows 11 machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5656735727 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11232#pullrequestreview-5656735727). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-26T18:05:01Z. **Review:** APPROVED --- Reviewed 9da4efcce merged onto `main` 570089a80 (2026-10-26); the merge is clean. Windows 11, i9-12900H, WinLibs GCC 15.2, relinker-only Release. **Results** - Full `ctest`: 308 of 309 pass; `patch_advisor_unit` fails the same way on plain `main`. `amd64_only_converter` passes. - Counter-check with `main`'s `Amd64OnlyConverter.cpp` and your test (MinGW has no UBSan runtime, so I used trapping builds): - Without a sanitizer it passes, as you say: on `main` the wrapped result is right. - With `-fsanitize=signed-integer-overflow -fsanitize-undefined-trap-on-error` it traps in `converterWideStoreIslandAcrossInt64Max`, in `_islandAt` at main's line 169. - With `-ftrapv` it aborts in `_buildIsland` at main's line 203. - Your file passes plain, with the trap and with `-ftrapv`. - Putting the signed subtraction back at one site at a time traps at lines 170, 204, 206 and 214 in turn, each inside the new test. So it reaches all four sites. - A mutant that is wrong only when the two addresses straddle 2^63 fails only the new test ("An island across 2^63 changed the segment bytes"), so the test does guard the result without a sanitizer. - `_distance` is exact here. `CMAKE_CXX_STANDARD` is 20, so the conversion to `int64_t` is defined, and every island distance stays inside the 0x100 search window. The int8 reach check and the stub's rel32 checks are unchanged. **Nit:** nothing places an island at the rel8 limit. Dropping `+ kShortJumpSize` from line 170 passes both your suite and `main`'s. With that mutant, a backward island at reach -129 or -130 is accepted and line 214 writes rel8 +127/+126. A fixture accepted at -128 and 127 and refused at -129 and 128 would pin it. The gap is older than this PR, but the PR rewrites the line. **Notes, nothing to change here** - Islands across 2^63 can still be placed over a branch target at or above 2^63 until #10680 lands. `_collectBranchTargets` drops those targets (line 284, `target >= 0`). With your fixture plus a jump to offset 0x10, which is 2^63 at the high base, the island window covers that target on `main` and on this PR. With #10680 merged, the high base gives the same placement as the low base. So "do not touch the island path" holds for the lines, but a correct island across 2^63 needs both PRs. - The description says #11033 fixes `_collectBranchTargets`. It changes `CodeInstructionCollector::Collect` instead; the conclusion that it doesn't overlap stays true. - The pr_overlap line in Tested is stale: #8042 is closed and #8236 has merged. Today the overlap is #11413, which conflicts only on the adjacent `main()` registration lines in `Amd64OnlyConverterTests.cpp`. Keeping both calls passes, plain and under the trap. `Amd64OnlyConverter.cpp` auto-merges. - `relinker-asan` runs only the `tls_function_coverage` label, so `amd64_only_converter` runs under no sanitizer in `main`'s CI today. **Not checked:** a real UBSan build, Linux and macOS, #11198's driver, and running converted code above 2^63. I found nothing that needs to change before merge. For disclosure: none of my open PRs touches these files. Written with AI assistance (Claude Code). I ran the builds, the counter-checks and the mutants above on my Windows 11 machine.
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!11232
No description provided.