fix(relinker): bound SCE relative relocation tables by PT_SCE_DYNLIBDATA #28881

Open
m4ce-w1ndu wants to merge 1 commit from upstream-pr/28881 into main
m4ce-w1ndu commented 2026-12-04 12:42:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by m4ce-w1ndu on 2026-12-04T12:42:20Z.


What

Bound SCE relative relocation tables (DT_OS_RELA and DT_OS_JMPREL) in BuildRelativeRelocationIndex to the PT_SCE_DYNLIBDATA segment extent (p_filesz) when PT_SCE_DYNLIBDATA is present.

Previously, BuildRelativeRelocationIndex validated PT_SCE_DYNLIBDATA against the file size and discarded its p_filesz, allowing DT_OS_RELA and DT_OS_JMPREL tables (empty or non-empty) to extend past the end of PT_SCE_DYNLIBDATA into trailing file bytes instead of rejecting them. Tables without PT_SCE_DYNLIBDATA keep their existing absolute file-offset behavior.

Added regression tests in core/relinker/relinker/tests/StrictNidFilterTests.cpp covering both exact-end acceptance and past-segment rejection for non-empty and empty DT_OS_RELA and DT_OS_JMPREL tables.

Tested

Linux x86-64 (GCC, C++20 -O2), relinker and strict_nid_filter_tests built from core/relinker/CMakeLists.txt:

  • Verified negative control: new regression tests fail against unpatched RelativeRelocationIndex.cpp (SCE RELA extending past PT_SCE_DYNLIBDATA into file bytes was accepted).
  • strict_nid_filter_tests passed.
  • Verified 15 relinker Python tests against the built relinker: test_jmprel_table_bounds.py, test_rela_size.py, test_relr_relocations.py, test_optional_rela.py, test_optional_plt.py, test_unused_filter_indirect.py, test_unused_filter_fallthrough_padding.py, test_init_array_roots.py, test_duplicate_relocations.py, test_overlapping_relocations.py, test_sce_code_extent.py, test_sce_code_gaps.py, test_null_symbol_relocations.py, test_none_relocations.py, and test_call_registry.py (15/15 passed).
  • python3 tools/check_conventions.py passed.
  • python3 tools/pr_overlap.py --branch fix/relinker-sce-reloc-index-bounds reported no overlap with open pull requests.
    Not tested with a real title or on Windows/macOS hosts.

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

Summary by CodeRabbit

  • Bug Fixes
    • SCE relocation tables that extend beyond the dynamic-library data segment are now rejected, including tables located within the file but outside the segment.
    • Tables ending exactly at the segment boundary remain valid.
  • Tests
    • Added boundary checks for SCE RELA and JMPREL tables, including empty tables.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5646640500 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28881). Originally posted by [m4ce-w1ndu](https://github.com/m4ce-w1ndu) on 2026-12-04T12:42:20Z. --- ### What Bound SCE relative relocation tables (`DT_OS_RELA` and `DT_OS_JMPREL`) in `BuildRelativeRelocationIndex` to the `PT_SCE_DYNLIBDATA` segment extent (`p_filesz`) when `PT_SCE_DYNLIBDATA` is present. Previously, `BuildRelativeRelocationIndex` validated `PT_SCE_DYNLIBDATA` against the file size and discarded its `p_filesz`, allowing `DT_OS_RELA` and `DT_OS_JMPREL` tables (empty or non-empty) to extend past the end of `PT_SCE_DYNLIBDATA` into trailing file bytes instead of rejecting them. Tables without `PT_SCE_DYNLIBDATA` keep their existing absolute file-offset behavior. Added regression tests in `core/relinker/relinker/tests/StrictNidFilterTests.cpp` covering both exact-end acceptance and past-segment rejection for non-empty and empty `DT_OS_RELA` and `DT_OS_JMPREL` tables. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Linux x86-64 (GCC, C++20 `-O2`), relinker and `strict_nid_filter_tests` built from `core/relinker/CMakeLists.txt`: - Verified negative control: new regression tests fail against unpatched `RelativeRelocationIndex.cpp` (`SCE RELA extending past PT_SCE_DYNLIBDATA into file bytes was accepted`). - `strict_nid_filter_tests` passed. - Verified 15 relinker Python tests against the built `relinker`: `test_jmprel_table_bounds.py`, `test_rela_size.py`, `test_relr_relocations.py`, `test_optional_rela.py`, `test_optional_plt.py`, `test_unused_filter_indirect.py`, `test_unused_filter_fallthrough_padding.py`, `test_init_array_roots.py`, `test_duplicate_relocations.py`, `test_overlapping_relocations.py`, `test_sce_code_extent.py`, `test_sce_code_gaps.py`, `test_null_symbol_relocations.py`, `test_none_relocations.py`, and `test_call_registry.py` (15/15 passed). - `python3 tools/check_conventions.py` passed. - `python3 tools/pr_overlap.py --branch fix/relinker-sce-reloc-index-bounds` reported no overlap with open pull requests. Not tested with a real title or on Windows/macOS hosts. ### 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) 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 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * SCE relocation tables that extend beyond the dynamic-library data segment are now rejected, including tables located within the file but outside the segment. * Tables ending exactly at the segment boundary remain valid. * **Tests** * Added boundary checks for SCE RELA and JMPREL tables, including empty tables. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Plyxen commented 2026-12-04 12:50:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-12-04T12:50:34Z.

Review: APPROVED


Verified on Windows 11 with WinLibs GCC 15.2.0 r7 (Release). I used a tree at main 63b98e5bf8 (plus two unrelated commits of mine in libc and a LibreSSL test), with this PR applied together with #28881, #28810, #28806, #28776 and #28781, which touch separate files. I ran every strict_nid_filter, guest_audiodec*, guest_np_manager*, guest_np_reachability and HarfBuzz test (31): all pass before the change, and all pass in 3 of 3 runs with it.

Negative control, with RelativeRelocationIndex.cpp as on main and the new tests kept: strict_nid_filter fails with "SCE RELA extending past PT_SCE_DYNLIBDATA into file bytes was accepted".

The new bound is written without overflow: tableAddress > sceTableSize || tableSize > sceTableSize - tableAddress. It applies only when PT_SCE_DYNLIBDATA is present, so the absolute-offset path is unchanged.

Not run: the relinker's Python tests and a real title.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6189243249 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28881#pullrequestreview-6189243249). Originally posted by [Plyxen](https://github.com/Plyxen) on 2026-12-04T12:50:34Z. **Review:** APPROVED --- Verified on Windows 11 with WinLibs GCC 15.2.0 r7 (Release). I used a tree at `main` `63b98e5bf8` (plus two unrelated commits of mine in libc and a LibreSSL test), with this PR applied together with #28881, #28810, #28806, #28776 and #28781, which touch separate files. I ran every `strict_nid_filter`, `guest_audiodec*`, `guest_np_manager*`, `guest_np_reachability` and HarfBuzz test (31): all pass before the change, and all pass in 3 of 3 runs with it. Negative control, with `RelativeRelocationIndex.cpp` as on `main` and the new tests kept: `strict_nid_filter` fails with "SCE RELA extending past PT_SCE_DYNLIBDATA into file bytes was accepted". The new bound is written without overflow: `tableAddress > sceTableSize || tableSize > sceTableSize - tableAddress`. It applies only when `PT_SCE_DYNLIBDATA` is present, so the absolute-offset path is unchanged. Not run: the relinker's Python tests and a real title. AI-assisted: yes (Claude Code)
MarcZX10 commented 2026-12-04 13:06:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-04T13:06:05Z.

Review: APPROVED


Reviewed — looks good

The reviewed change bounds SCE relocation tables to the validated PT_SCE_DYNLIBDATA extent, and the added cases cover exact-end acceptance and past-segment rejection.

What I ran

Reviewed head bf869ac33b661c82fede9dafdb8345013ed6833b, the prepared diff and manifest, and both changed files. No tests or builds were run by me; the test results in the description are author-reported.

Results

Check Result Note
Diff and changed lines passed Both changed files reviewed
Tests and builds not run Author reports passing tests
Negative control not run Author reports it failed as expected

Negative control

Not run by me. The author reports that the new regression tests failed against the unpatched implementation.

Findings

Blocking

None.

Non-blocking

None.

Sources

Not verified

No tests or builds were run by me, and behavior on a real title or Windows/macOS hosts was not verified. Both changed files were read; files.json lists no excluded files.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6189490715 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28881#pullrequestreview-6189490715). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-12-04T13:06:05Z. **Review:** APPROVED --- # Reviewed — looks good The reviewed change bounds SCE relocation tables to the validated `PT_SCE_DYNLIBDATA` extent, and the added cases cover exact-end acceptance and past-segment rejection. ## What I ran Reviewed head `bf869ac33b661c82fede9dafdb8345013ed6833b`, the prepared diff and manifest, and both changed files. No tests or builds were run by me; the test results in the description are author-reported. ## Results | Check | Result | Note | |---|---|---| | Diff and changed lines | passed | Both changed files reviewed | | Tests and builds | not run | Author reports passing tests | | Negative control | not run | Author reports it failed as expected | ## Negative control Not run by me. The author reports that the new regression tests failed against the unpatched implementation. ## Findings ### Blocking None. ### Non-blocking None. ## Sources - [`RelativeRelocationIndex.cpp`, lines 77–85](https://github.com/boykopovar/AnyPS5/blob/bf869ac33b661c82fede9dafdb8345013ed6833b/core/relinker/relinker/src/analysis/UnusedNidFilter/RelativeRelocationIndex.cpp#L77-L85) — overflow and segment-extent checks precede file access. - [`StrictNidFilterTests.cpp`, lines 1152–1204](https://github.com/boykopovar/AnyPS5/blob/bf869ac33b661c82fede9dafdb8345013ed6833b/core/relinker/relinker/tests/StrictNidFilterTests.cpp#L1152-L1204) — exact-end and past-segment cases for RELA and JMPREL, including empty tables. ## Not verified No tests or builds were run by me, and behavior on a real title or Windows/macOS hosts was not verified. Both changed files were read; `files.json` lists no excluded files. ## 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/28881:upstream-pr/28881'
git switch 'upstream-pr/28881'

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