fix(relinker): reject overlapping PT_LOAD segments in Linux output #10564

Merged
roboterspinne merged 1 commit from upstream-pr/10564 into main 2026-10-25 22:08:08 +00:00
roboterspinne commented 2026-10-24 10:15:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-24T10:15:24Z.


What

SeparateSharedLoadPages in LinuxElfPatcher.cpp sorts the PT_LOAD segments by address but never checks that their memory ranges are disjoint, and ElfReader does not either. The Windows and macOS images reject overlapping loads (WindowsLoadImage.cpp:29-35, Overlapping PT_LOAD memory ranges) and so do guest modules (GuestImageReader.cpp:48). The Linux output kept both headers; the kernel maps the later segment over the earlier one, so a writable load over the code replaced it with zeros and the program crashed with SIGSEGV at start. After the sort, adjacent loads whose ranges overlap now fail with the Windows message, at the address of the second load. The subtraction cannot underflow because the loads are sorted, and loads with p_memsz 0 are already left out.

  • Inputs with disjoint loads are unchanged; every converted title already passes this check on Windows and macOS.
  • The check runs for the default output, --lazy-binding and --linux-fixed-base.
  • executable_segments now runs its overlap-next case on all five targets. overlap-past-end stays on Windows and macOS, because every Linux target rejects it before the overlap check: the default and --linux-fixed-base outputs because the address lies outside the user address space, --lazy-binding with Entry stub call exceeds rel32 range.

Tested

Ubuntu 24.04 x86_64, glibc 2.39, relinker-only Release build (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON); synthetic fixtures only, no title or homebrew.

  • ctest --test-dir build-relinker -j4: 241/241 passed. The changed executable_segments fails against the main binary at overlap-next on the Linux target, where the relink exits 0.
  • On main, the test_executable_tls_relocations.py fixture with its writable PT_LOAD moved to address 0, over the code segment, relinks with exit 0 in the default mode and with --lazy-binding, and both outputs exit with SIGSEGV. With this change both relinks exit 2 with Overlapping PT_LOAD memory ranges and leave no output.
  • python3 tools/check_conventions.py --base upstream/main with this title and description passed.

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 (Claude Code)**
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4994529503 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10564). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-24T10:15:24Z. --- ### What `SeparateSharedLoadPages` in `LinuxElfPatcher.cpp` sorts the `PT_LOAD` segments by address but never checks that their memory ranges are disjoint, and `ElfReader` does not either. The Windows and macOS images reject overlapping loads (`WindowsLoadImage.cpp:29-35`, `Overlapping PT_LOAD memory ranges`) and so do guest modules (`GuestImageReader.cpp:48`). The Linux output kept both headers; the kernel maps the later segment over the earlier one, so a writable load over the code replaced it with zeros and the program crashed with SIGSEGV at start. After the sort, adjacent loads whose ranges overlap now fail with the Windows message, at the address of the second load. The subtraction cannot underflow because the loads are sorted, and loads with `p_memsz` 0 are already left out. - Inputs with disjoint loads are unchanged; every converted title already passes this check on Windows and macOS. - The check runs for the default output, `--lazy-binding` and `--linux-fixed-base`. - `executable_segments` now runs its `overlap-next` case on all five targets. `overlap-past-end` stays on Windows and macOS, because every Linux target rejects it before the overlap check: the default and `--linux-fixed-base` outputs because the address lies outside the user address space, `--lazy-binding` with `Entry stub call exceeds rel32 range`. ### 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. --> Ubuntu 24.04 x86_64, glibc 2.39, relinker-only Release build (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`); synthetic fixtures only, no title or homebrew. - `ctest --test-dir build-relinker -j4`: 241/241 passed. The changed `executable_segments` fails against the `main` binary at `overlap-next` on the Linux target, where the relink exits 0. - On `main`, the `test_executable_tls_relocations.py` fixture with its writable `PT_LOAD` moved to address 0, over the code segment, relinks with exit 0 in the default mode and with `--lazy-binding`, and both outputs exit with SIGSEGV. With this change both relinks exit 2 with `Overlapping PT_LOAD memory ranges` and leave no output. - `python3 tools/check_conventions.py --base upstream/main` with this title and description passed. ### 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 - [x] Generic behaviour, not specific to one title - [x] Depends on: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes (Claude Code)**
MarcZX10 commented 2026-10-24 10:23:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T10:23:24Z.

Review: APPROVED


Reviewed the Linux load-range check and its regression coverage against the existing Windows and guest-image checks. Sorting by mapped address makes the subtraction safe from underflow; comparing the distance with the previous segment size accepts exactly adjacent ranges and rejects actual overlap. Zero-size loads are excluded before the check. The new test exercises default Linux output, lazy binding, fixed-base Linux, Windows and macOS, and the reported synthetic case reproduces the prior invalid output. Approved.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5637911722 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10564#pullrequestreview-5637911722). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T10:23:24Z. **Review:** APPROVED --- Reviewed the Linux load-range check and its regression coverage against the existing Windows and guest-image checks. Sorting by mapped address makes the subtraction safe from underflow; comparing the distance with the previous segment size accepts exactly adjacent ranges and rejects actual overlap. Zero-size loads are excluded before the check. The new test exercises default Linux output, lazy binding, fixed-base Linux, Windows and macOS, and the reported synthetic case reproduces the prior invalid output. Approved.
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!10564
No description provided.