perf(relinker): keep three passes linear on crafted guest binaries #12203

Merged
MarcZX10 merged 3 commits from upstream-pr/12203 into main 2026-11-02 13:35:30 +00:00
MarcZX10 commented 2026-10-28 03:34:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T03:34:37Z.


What

Three passes of the relinker are quadratic in the size of a crafted guest binary. One commit each:

  • GuestImageReader.cpp readLifecycleArray scanned the whole RELA and PLT relocation tables for every 8-byte slot of DT_INIT_ARRAY / DT_FINI_ARRAY: slots times relocations, about 10^12 steps for a million of each (a 32 MB file). The relocations are now grouped by target once per array, in table order, so each slot visits only its own entries and the checks and their order are unchanged.
  • EhFrameReader.cpp parsed the FDE and its LSDA call-site table for every .eh_frame_hdr row, so a header repeating one row tens of thousands of times multiplied the work and the number of collected regions by the row count. A row that repeats an earlier (begin, fde) pair describes the same region and is skipped.
  • RelativeJumpTables.cpp: FindRelativeJumpTables walks every branch target found in a gap until a return, indirect jump or trap and only deduplicated start addresses, so a run of jmp next instructions was walked to the segment end once per instruction. A walk now stops at a position an earlier walk already decoded (the targets it would find were registered then). FindTrailingRelativeJumpTable scanned every recorded table load at each instruction after an indirect jump; the loads are also kept by target now, in the order they were seen.

No behaviour change is intended, so there is no new test: the existing relinker tests cover the results.

Tested

Linux x86-64, a throwaway archive build with the host CPU gate patched. These ctests pass: init_array_roots, guest_needed_modules, guest_needed_nested_modules, guest_module_args, guest_module_fini, guest_zero_initializer, duplicate_relocations, linux_eh_frame_header, sce_code_gaps, jmprel_table_bounds, entry_point_bounds, gnu_hash_analysis, guest_gnu_hash, unused_filter_indirect, unused_filter_fallthrough_padding, unwind_record_bounds, control_flow_graph and strict_nid_filter. The complexity claims come from reading the loops; I did not time the old and new binaries on a synthetic file. tools/check_conventions.py --base upstream/main and git diff --check pass.

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:5044786022 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12203). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T03:34:37Z. --- ### What Three passes of the relinker are quadratic in the size of a crafted guest binary. One commit each: - `GuestImageReader.cpp` `readLifecycleArray` scanned the whole RELA and PLT relocation tables for every 8-byte slot of `DT_INIT_ARRAY` / `DT_FINI_ARRAY`: slots times relocations, about 10^12 steps for a million of each (a 32 MB file). The relocations are now grouped by target once per array, in table order, so each slot visits only its own entries and the checks and their order are unchanged. - `EhFrameReader.cpp` parsed the FDE and its LSDA call-site table for every `.eh_frame_hdr` row, so a header repeating one row tens of thousands of times multiplied the work and the number of collected regions by the row count. A row that repeats an earlier `(begin, fde)` pair describes the same region and is skipped. - `RelativeJumpTables.cpp`: `FindRelativeJumpTables` walks every branch target found in a gap until a return, indirect jump or trap and only deduplicated start addresses, so a run of `jmp next` instructions was walked to the segment end once per instruction. A walk now stops at a position an earlier walk already decoded (the targets it would find were registered then). `FindTrailingRelativeJumpTable` scanned every recorded table load at each instruction after an indirect jump; the loads are also kept by target now, in the order they were seen. No behaviour change is intended, so there is no new test: the existing relinker tests cover the results. ### Tested Linux x86-64, a throwaway archive build with the host CPU gate patched. These ctests pass: `init_array_roots`, `guest_needed_modules`, `guest_needed_nested_modules`, `guest_module_args`, `guest_module_fini`, `guest_zero_initializer`, `duplicate_relocations`, `linux_eh_frame_header`, `sce_code_gaps`, `jmprel_table_bounds`, `entry_point_bounds`, `gnu_hash_analysis`, `guest_gnu_hash`, `unused_filter_indirect`, `unused_filter_fallthrough_padding`, `unwind_record_bounds`, `control_flow_graph` and `strict_nid_filter`. The complexity claims come from reading the loops; I did not time the old and new binaries on a synthetic file. `tools/check_conventions.py --base upstream/main` and `git diff --check` pass. ### 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`); 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
rooklz commented 2026-10-28 04:13:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-28T04:13:37Z.

Review: APPROVED


Reviewed 7849e7f69d on macOS arm64 (relinker-only Release build), merged onto main ed94f8443f: build 266/266, ctest -j 8 366/366, check_conventions --base upstream/main clean, pr_overlap --branch "No overlap with open pull requests" (1363 probed).

Equivalence, read against main:

  • GuestImageReader readLifecycleArray: the index is built after the size == 0 return, from tables already checked to be % 24 == 0. Each slot's entries keep the old visit order (all RelaData hits, then RelaPltData, each in table order), so the checks and the first failure stay the same.
  • FindTrailingRelativeJumpTable: loadsAt[target] is filled in the same order as loads, so the inner loop visits exactly the old load.Target == position entries in the same order. TablesEnd still gets the full loads.
  • FindRelativeJumpTables: decoding depends only on pos, so a walk that reaches a position an earlier walk decoded would repeat the same instructions, push the same (already-inserted) targets and stop at the same break. branchTargets ends up as the same set. Positions decoded in the per-function pre-pass are not marked, so nothing gets skipped.
  • EhFrameReader: readFde depends only on (begin, fde) plus the CIE cache, so a repeated row gives the same region, or throws on its first occurrence anyway.

Mutation probe (each mutant built and run through the full ctest):

mutant result
M1 loadsAt never filled killed (control_flow_graph)
M2 lifecycle index skips RelaPltData survived
M3 EH dedup keeps only the first row killed (sce_code_extent, sce_module_code_extent)
M4 gap walk also marks pos + 1 decoded survived
M5 gap walk stops after the first walk survived

Non-blocking notes:

  1. M2/M4/M5 survive on main's tests too, so this is an existing coverage gap rather than a regression. Still, "the existing relinker tests cover the results" holds only for M1/M3. No test relocates an INIT_ARRAY/FINI_ARRAY slot through DT_JMPREL, and nothing checks which branch targets the gap walk finds. A small fixture for either would back up the no-behaviour-change claim.
  2. The EH dedup does change output for a header with repeated rows: ReadExceptionFunctions now returns one region where it used to return N copies. So RedZoneFrameReport::Functions and the red-zone framing pass (frameSegment iterates functions) see each function once. That is arguably the right behaviour, but it is a visible change, so it might be worth one line in the body.

Approving.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5677710454 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12203#pullrequestreview-5677710454). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-28T04:13:37Z. **Review:** APPROVED --- Reviewed 7849e7f69d on macOS arm64 (relinker-only Release build), merged onto main ed94f8443f: build 266/266, `ctest -j 8` 366/366, `check_conventions --base upstream/main` clean, `pr_overlap --branch` "No overlap with open pull requests" (1363 probed). Equivalence, read against main: - `GuestImageReader` `readLifecycleArray`: the index is built after the `size == 0` return, from tables already checked to be `% 24 == 0`. Each slot's entries keep the old visit order (all `RelaData` hits, then `RelaPltData`, each in table order), so the checks and the first failure stay the same. - `FindTrailingRelativeJumpTable`: `loadsAt[target]` is filled in the same order as `loads`, so the inner loop visits exactly the old `load.Target == position` entries in the same order. `TablesEnd` still gets the full `loads`. - `FindRelativeJumpTables`: decoding depends only on `pos`, so a walk that reaches a position an earlier walk decoded would repeat the same instructions, push the same (already-inserted) targets and stop at the same break. `branchTargets` ends up as the same set. Positions decoded in the per-function pre-pass are not marked, so nothing gets skipped. - `EhFrameReader`: `readFde` depends only on `(begin, fde)` plus the CIE cache, so a repeated row gives the same region, or throws on its first occurrence anyway. Mutation probe (each mutant built and run through the full ctest): | mutant | result | |---|---| | M1 `loadsAt` never filled | killed (`control_flow_graph`) | | M2 lifecycle index skips `RelaPltData` | **survived** | | M3 EH dedup keeps only the first row | killed (`sce_code_extent`, `sce_module_code_extent`) | | M4 gap walk also marks `pos + 1` decoded | **survived** | | M5 gap walk stops after the first walk | **survived** | Non-blocking notes: 1. M2/M4/M5 survive on main's tests too, so this is an existing coverage gap rather than a regression. Still, "the existing relinker tests cover the results" holds only for M1/M3. No test relocates an `INIT_ARRAY`/`FINI_ARRAY` slot through `DT_JMPREL`, and nothing checks which branch targets the gap walk finds. A small fixture for either would back up the no-behaviour-change claim. 2. The EH dedup does change output for a header with repeated rows: `ReadExceptionFunctions` now returns one region where it used to return N copies. So `RedZoneFrameReport::Functions` and the red-zone framing pass (`frameSegment` iterates `functions`) see each function once. That is arguably the right behaviour, but it is a visible change, so it might be worth one line in the body. Approving.
MarcZX10 commented 2026-11-02 04:58:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T04:58:00Z.

Review: COMMENTED


Reviewed — rooklz's notes checked, EH dedup change is visible and undocumented

The dedup in EhFrameReader.cpp changes what ReadExceptionFunctions returns for repeated rows, and the PR body says no behaviour change is intended.

What I ran

  • Live head: 7849e7f69dca310b03525682b0f2acbebc82c9d3 (OPEN, same as batch file).
  • gh api GET of issue comments, PR reviews and review comments for #12203; gh pr view --json statusCheckRollup; gh pr diff 12203 (saved to a scratch file).
  • git -C /marc/AnyPS5 show 7849e7f6… for EhFrameReader.cpp, GuestImageReader.cpp and RelativeJumpTables.cpp (read at the lines cited).
  • Nothing was built, run or mutated.

Results

Check Result Note
conventions, eligibility, intake, labels SUCCESS hosted
relinker (macos clang++ x2, ubuntu g++, windows g++), relinker-asan SUCCESS hosted
linux, windows, register-context, write-watch SKIPPED not run by the hosted matrix here

Findings

  • rooklz note 2 (EH dedup changes output): confirmed. The PR adds std::set listed at EhFrameReader.cpp:38 and if (!listed.emplace(begin, fde).second) continue; at line 43, so a repeated (begin, fde) row is no longer read again. The result therefore has one region where it used to have N copies. The PR body's EH bullet describes the skip, but the paragraph before "Tested" says "No behaviour change is intended". Adding one line that names the visible effect on functions would reconcile the two. Not verified: the effect on the red-zone framing pass (frameSegment), which I did not read.
  • rooklz note 1 (mutants M2, M4, M5 survived; fixtures would help): the code shows the lifecycle index includes RelaPltData at GuestImageReader.cpp:299, so a fixture that relocates an INIT_ARRAY/FINI_ARRAY slot through DT_JMPREL is a real gap candidate. The mutant results and whether the existing tests cover them were not reproduced, because nothing was built. Whether to add fixtures is the author's call.
  • Other reports (JJenkx timings on 2026-10-28 05:50Z and 2026-10-31 06:55Z): measurements, not checkable in the code here; no reply required from the code side.

Sources

Not verified

  • Mutants M2, M4 and M5 were not built or run; the survival results come from rooklz's report.
  • The red-zone framing pass (frameSegment) effect of the EH change was not read.
  • The timing tables from JJenkx were not reproduced.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5732252248 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12203#pullrequestreview-5732252248). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-02T04:58:00Z. **Review:** COMMENTED --- # Reviewed — rooklz's notes checked, EH dedup change is visible and undocumented The dedup in `EhFrameReader.cpp` changes what `ReadExceptionFunctions` returns for repeated rows, and the PR body says no behaviour change is intended. ## What I ran - Live head: `7849e7f69dca310b03525682b0f2acbebc82c9d3` (OPEN, same as batch file). - `gh api` GET of issue comments, PR reviews and review comments for #12203; `gh pr view --json statusCheckRollup`; `gh pr diff 12203` (saved to a scratch file). - `git -C /marc/AnyPS5 show 7849e7f6…` for `EhFrameReader.cpp`, `GuestImageReader.cpp` and `RelativeJumpTables.cpp` (read at the lines cited). - Nothing was built, run or mutated. ## Results | Check | Result | Note | |---|---|---| | conventions, eligibility, intake, labels | SUCCESS | hosted | | relinker (macos clang++ x2, ubuntu g++, windows g++), relinker-asan | SUCCESS | hosted | | linux, windows, register-context, write-watch | SKIPPED | not run by the hosted matrix here | ## Findings - **rooklz note 2 (EH dedup changes output)**: confirmed. The PR adds `std::set listed` at `EhFrameReader.cpp:38` and `if (!listed.emplace(begin, fde).second) continue;` at line 43, so a repeated `(begin, fde)` row is no longer read again. The result therefore has one region where it used to have N copies. The PR body's EH bullet describes the skip, but the paragraph before "Tested" says "No behaviour change is intended". Adding one line that names the visible effect on `functions` would reconcile the two. Not verified: the effect on the red-zone framing pass (`frameSegment`), which I did not read. - **rooklz note 1 (mutants M2, M4, M5 survived; fixtures would help)**: the code shows the lifecycle index includes `RelaPltData` at `GuestImageReader.cpp:299`, so a fixture that relocates an `INIT_ARRAY`/`FINI_ARRAY` slot through `DT_JMPREL` is a real gap candidate. The mutant results and whether the existing tests cover them were not reproduced, because nothing was built. Whether to add fixtures is the author's call. - **Other reports** (JJenkx timings on 2026-10-28 05:50Z and 2026-10-31 06:55Z): measurements, not checkable in the code here; no reply required from the code side. ## Sources - https://github.com/boykopovar/AnyPS5/blob/7849e7f69dca310b03525682b0f2acbebc82c9d3/core/relinker/relinker/src/analysis/UnusedNidFilter/EhFrameReader.cpp#L38-L43 - https://github.com/boykopovar/AnyPS5/blob/7849e7f69dca310b03525682b0f2acbebc82c9d3/core/relinker/relinker/src/guest/GuestImageReader.cpp#L289-L299 ## Not verified - Mutants M2, M4 and M5 were not built or run; the survival results come from rooklz's report. - The red-zone framing pass (`frameSegment`) effect of the EH change was not read. - The timing tables from JJenkx were not reproduced.
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!12203
No description provided.