fix(relinker): reject an executable PT_LOAD alignment that is not a power of two #10518

Merged
lulebo merged 2 commits from upstream-pr/10518 into main 2026-12-04 00:07:41 +00:00
lulebo commented 2026-10-24 07:35:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by lulebo on 2026-10-24T07:35:52Z.


What

ElfReader::ReadProgramHeaders accepts any p_align on an executable's PT_LOAD, and on the uncovered PT_SCE_RELRO it turns into one. The gABI says p_align should be 0, 1 or a positive integral power of 2. Linux and glibc skip any other value when they align the image, FreeBSD refuses it with ENOEXEC since 13.1, and FreeBSD 11.4's loader does not read p_align. This reader already refuses such a value for PT_TLS (#7098).

On main, the Linux conversion at the default base refuses such a value only because the base is not a multiple of it:

FAIL: Fixed Linux base 0x400000 is not a multiple of the PT_LOAD alignment 0x3

At a base that is, such as --linux-fixed-base 0x30000, with --lazy-binding, and to Windows or macOS, the executable converts. The last PT_LOAD of the input below has p_align 3 and starts on a page that the first segment maps with other bytes, so the Linux patcher copies it to the end of the output with min(p_align, 0x1000) as its p_align (#11487), and the Linux output declares p_align 3 as well.

Both segment kinds are now refused when read, next to the file size and range checks #5741 added for the same pair:

FAIL: PT_LOAD alignment is not a power of two (offset 0x800)

GuestImageReader takes its headers from the same function (GuestImageReader.cpp:36), so the power-of-two half of its Invalid load segment alignment check can no longer fail and is dropped. A guest module with such an alignment now fails with the message above, with the module as the input; the offset congruence half of that check stays. #13818's guest_load_ranges test gives an empty PT_LOAD an alignment of 3, so that case now expects the message above; its congruence case still reaches the GuestImageReader check.

The input
Type Offset Addresses File size p_align
PT_LOAD 0x0 [0x0, 0x1100) 0x800 0x1000
PT_DYNAMIC 0x400 [0x400, 0x480) 0x80 0x8
PT_LOAD 0x200 [0x1200, 0x1220) 0x20 0x1000
PT_LOAD 0x800 [0x1800, 0x1802) 0x2 0x3
base64 -d > input.elf.gz <<'EOF'
H4sIAAAAAAACA6t39XFjYmRkgAFmBjsGEE9ACMJ3YMAEDgwWDCxQGZBaNgY8gANKC0JpAQjFBNPHAhVHoxsYUGmYMSD7WGEGgADUnQoM2GmYfXB9MIMkUGmYcUzwcKApkGYYJOC/6q8P//8j+C9/szKMgpED4LENzcRcSPkMSZhBAcrgRss+7AyoDPTsxYnGHwWjYDABAWbUdAur90YKmHB4NA2MglEwCgYRAADPdMqZlQwAAA==
EOF
gunzip input.elf.gz

SHA-256 bcde1d6fd2e2b89cf54a51805e0779a27a2a790fd842d947da6469b2f69baf94. On main, relinker --skip-sce-module input.elf output.elf fails with the base message above, and relinker --lazy-binding --skip-sce-module input.elf output.elf writes a 10,672-byte output whose readelf -lW lists the moved segment as:

  LOAD           0x002800 0x0000000000001800 0x0000000000001800 0x000002 0x000002 R E 0x3

With this change, both fail at once:

FAIL: PT_LOAD alignment is not a power of two (offset 0x800)

Tested

WSL 2 Ubuntu 26.04 x86-64, GCC 15.2.0, CMake 4.2.3, Ninja 1.13.2, Python 3.14.4. Relinker-only builds of main (9ab6a281ea) and of this branch:

  • elf_reader_bounds: the new rejection case fails on main (Invalid program header was accepted) and passes with this change. 3, 0x7fffffff and 0xfffffffffffffffb are refused and 0, 1, 2, 8 and 0x4000 kept, for PT_LOAD and for an uncovered PT_SCE_RELRO, which also gets the PT_GNU_RELRO that #10662 adds.
  • guest_load_ranges: with this change and the test as on main, empty-first-alignment and empty-last-alignment fail (Expected Invalid load segment alignment, got PT_LOAD alignment is not a power of two); with the new expectation, it passes.
  • The input above, Release build, --skip-sce-module: on main the base message at the default base, a 14,768-byte output at --linux-fixed-base 0x30000 and a 10,672-byte one with --lazy-binding, both declaring p_align 3 for the moved segment, and a 247,296-byte --windows and a 77,888-byte --macos output. With this change, all five fail at once with the message above. With its last segment as PT_SCE_RELRO, main behaves the same at the default base and with --lazy-binding, and this change fails with PT_SCE_RELRO alignment is not a power of two (offset 0x800). With p_align 0xfffffffffffffffb, the value a fuzzer first found, main fails with the base message and converts with --lazy-binding, declaring 0x1000 for the moved segment; this change refuses both.
  • Release build: 686 of 686 tests pass.
  • ASan build configured as the relinker-asan job, and the same with -fsanitize=undefined -fno-sanitize-recover=undefined: 686 of 686 pass, with no ASan or UBSan report.
  • tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py --branch finds no shared lines with the open pull requests it can compare. It cannot compare eight that conflict with main, so each was trial-merged with this branch at its current head. #11628, #11661, #11689 and #11698 are built on an earlier head of this PR and list it in Depends on; they also conflict with it in ElfReaderBoundsTests.cpp, where this head updates its accepting case for #10662. #15345 moves the PT_SCE_RELRO size and range checks that this PR's check follows, and adds its ElfReaderBoundsTests.cpp cases where this PR adds its own, so whichever of the two merges second keeps both. #13486, #14001 and #15032 conflict with this branch only where they conflict with main.
  • Not tested: Windows and macOS hosts. The check is in the reader all three outputs share.

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)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4993535674 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10518). Originally posted by [lulebo](https://github.com/lulebo) on 2026-10-24T07:35:52Z. --- ### What `ElfReader::ReadProgramHeaders` accepts any `p_align` on an executable's `PT_LOAD`, and on the uncovered `PT_SCE_RELRO` it turns into one. The gABI says `p_align` should be 0, 1 or a positive integral power of 2. Linux and glibc skip any other value when they align the image, FreeBSD refuses it with ENOEXEC since 13.1, and FreeBSD 11.4's loader does not read `p_align`. This reader already refuses such a value for `PT_TLS` (#7098). On `main`, the Linux conversion at the default base refuses such a value only because the base is not a multiple of it: ``` FAIL: Fixed Linux base 0x400000 is not a multiple of the PT_LOAD alignment 0x3 ``` At a base that is, such as `--linux-fixed-base 0x30000`, with `--lazy-binding`, and to Windows or macOS, the executable converts. The last `PT_LOAD` of the input below has `p_align` 3 and starts on a page that the first segment maps with other bytes, so the Linux patcher copies it to the end of the output with `min(p_align, 0x1000)` as its `p_align` (#11487), and the Linux output declares `p_align` 3 as well. Both segment kinds are now refused when read, next to the file size and range checks #5741 added for the same pair: ``` FAIL: PT_LOAD alignment is not a power of two (offset 0x800) ``` `GuestImageReader` takes its headers from the same function (`GuestImageReader.cpp:36`), so the power-of-two half of its `Invalid load segment alignment` check can no longer fail and is dropped. A guest module with such an alignment now fails with the message above, with the module as the input; the offset congruence half of that check stays. #13818's `guest_load_ranges` test gives an empty `PT_LOAD` an alignment of 3, so that case now expects the message above; its congruence case still reaches the `GuestImageReader` check. <details> <summary>The input</summary> | Type | Offset | Addresses | File size | `p_align` | |---|---|---|---|---| | `PT_LOAD` | 0x0 | `[0x0, 0x1100)` | 0x800 | 0x1000 | | `PT_DYNAMIC` | 0x400 | `[0x400, 0x480)` | 0x80 | 0x8 | | `PT_LOAD` | 0x200 | `[0x1200, 0x1220)` | 0x20 | 0x1000 | | `PT_LOAD` | 0x800 | `[0x1800, 0x1802)` | 0x2 | 0x3 | ``` base64 -d > input.elf.gz <<'EOF' H4sIAAAAAAACA6t39XFjYmRkgAFmBjsGEE9ACMJ3YMAEDgwWDCxQGZBaNgY8gANKC0JpAQjFBNPHAhVHoxsYUGmYMSD7WGEGgADUnQoM2GmYfXB9MIMkUGmYcUzwcKApkGYYJOC/6q8P//8j+C9/szKMgpED4LENzcRcSPkMSZhBAcrgRss+7AyoDPTsxYnGHwWjYDABAWbUdAur90YKmHB4NA2MglEwCgYRAADPdMqZlQwAAA== EOF gunzip input.elf.gz ``` SHA-256 `bcde1d6fd2e2b89cf54a51805e0779a27a2a790fd842d947da6469b2f69baf94`. On `main`, `relinker --skip-sce-module input.elf output.elf` fails with the base message above, and `relinker --lazy-binding --skip-sce-module input.elf output.elf` writes a 10,672-byte output whose `readelf -lW` lists the moved segment as: ``` LOAD 0x002800 0x0000000000001800 0x0000000000001800 0x000002 0x000002 R E 0x3 ``` With this change, both fail at once: ``` FAIL: PT_LOAD alignment is not a power of two (offset 0x800) ``` </details> ### Tested WSL 2 Ubuntu 26.04 x86-64, GCC 15.2.0, CMake 4.2.3, Ninja 1.13.2, Python 3.14.4. Relinker-only builds of `main` (9ab6a281ea) and of this branch: - `elf_reader_bounds`: the new rejection case fails on `main` (`Invalid program header was accepted`) and passes with this change. 3, 0x7fffffff and 0xfffffffffffffffb are refused and 0, 1, 2, 8 and 0x4000 kept, for `PT_LOAD` and for an uncovered `PT_SCE_RELRO`, which also gets the `PT_GNU_RELRO` that #10662 adds. - `guest_load_ranges`: with this change and the test as on `main`, `empty-first-alignment` and `empty-last-alignment` fail (`Expected Invalid load segment alignment, got PT_LOAD alignment is not a power of two`); with the new expectation, it passes. - The input above, Release build, `--skip-sce-module`: on `main` the base message at the default base, a 14,768-byte output at `--linux-fixed-base 0x30000` and a 10,672-byte one with `--lazy-binding`, both declaring `p_align` 3 for the moved segment, and a 247,296-byte `--windows` and a 77,888-byte `--macos` output. With this change, all five fail at once with the message above. With its last segment as `PT_SCE_RELRO`, `main` behaves the same at the default base and with `--lazy-binding`, and this change fails with `PT_SCE_RELRO alignment is not a power of two (offset 0x800)`. With `p_align` 0xfffffffffffffffb, the value a fuzzer first found, `main` fails with the base message and converts with `--lazy-binding`, declaring 0x1000 for the moved segment; this change refuses both. - Release build: 686 of 686 tests pass. - ASan build configured as the `relinker-asan` job, and the same with `-fsanitize=undefined -fno-sanitize-recover=undefined`: 686 of 686 pass, with no ASan or UBSan report. - `tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py --branch` finds no shared lines with the open pull requests it can compare. It cannot compare eight that conflict with `main`, so each was trial-merged with this branch at its current head. #11628, #11661, #11689 and #11698 are built on an earlier head of this PR and list it in Depends on; they also conflict with it in `ElfReaderBoundsTests.cpp`, where this head updates its accepting case for #10662. #15345 moves the `PT_SCE_RELRO` size and range checks that this PR's check follows, and adds its `ElfReaderBoundsTests.cpp` cases where this PR adds its own, so whichever of the two merges second keeps both. #13486, #14001 and #15032 conflict with this branch only where they conflict with `main`. - Not tested: Windows and macOS hosts. The check is in the reader all three outputs share. ### 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
roboterspinne commented 2026-10-24 10:49:45 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Reviewed head ccdbdc068 merged onto upstream/main aeeaefad9 (2026-10-24); the merge is clean, and so is a merge onto the current main 60465afd3, which does not touch the two files.

Environment: Windows 11 Home 10.0.26200, WinLibs GCC 15.2.0 UCRT, CMake 4.3.1, Ninja 1.13.2, Python 3.13.9, -DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON, Release.

Results

  • Baseline main aeeaefad9 (idle machine): 238 tests, 0 failed, long_paths skipped. PR merged: elf_reader_bounds passes (0.2 s). I could not get a clean full-suite run today: both full runs happened under heavy unrelated CPU load on my machine and their failures were timeouts in unrelated tests (shader_cache_runtime_sources, nid_database_cache, select_tests, configure_seed*, two Python tests), every one of which passes when rerun alone; none touches this PR's files. CI's relinker matrix is green on all three OSes.
  • Red/green: with the ElfReader.cpp hunk reverted and the PR's test kept, elf_reader_bounds fails with load and uncovered SCE RELRO alignment is not a power of two: Invalid program header was accepted while the "zero, one or a power of two" case and the existing TLS case pass; restored, it passes.
  • CLI with the PR build on test_linux_load_alignment's fixture (one PT_LOAD at vaddr 0, offset 0x4000): p_align 3, 0x7fffffff and 0xfffffffffffffffb each exit 2 within about 50 ms with FAIL: PT_LOAD alignment is not a power of two (offset 0x4000); 0, 1, 2, 0x1000, 0x4000 and 0x10000 convert. An uncovered PT_SCE_RELRO (vaddr 0x9000, filesz 0, memsz 0x10) with p_align 3 exits 2 with FAIL: PT_SCE_RELRO alignment is not a power of two (offset 0x5000) and converts with 0x1000; a covered PT_SCE_RELRO with p_align 3 converts, which is right because its alignment is never consumed; a PT_LOAD with memsz 0 and p_align 3 is refused, consistent with GuestImageReader.
  • I deliberately did not run main on a bad-alignment input. The mechanism is visible in LinuxElfPatcher.cpp:139-141 (SeparateSharedLoadPages): with alignment = max(p_align, 0x1000) the loop while ((buf.size() + prefix) % alignment != load.MappedAddress % alignment) buf.push_back(0); cannot reach the target for a value near 2^64 and grows until allocation fails. The reader check stops such inputs before the patcher.
  • check_conventions.py --base upstream/main passes; pr_overlap.py reports no overlap.

Should fix

  • After this change a power-of-two p_align is still unbounded: std::has_single_bit admits 2^31 to 2^63, and nothing between the reader and SeparateSharedLoadPages (run at LinuxElfPatcher.cpp:556, before WriteLayout at :573) bounds it, so a segment on a shared page with differing bytes still pads the Linux output byte by byte to p_align and ends as a 2 GiB file or as the generic FAIL: std::bad_alloc from main.cpp:479-482 (exit 2), as the body says. CONTRIBUTING asks that known gaps be recorded in technical debt and docs/dev/debt/relinker.md has no entry for this. Please either add one line there (a power-of-two p_align above the page size pads the Linux output up to p_align - 1 bytes and fails only at std::bad_alloc) or bound p_align in the two new checks. For reference, FreeBSD refuses a PT_LOAD p_align that is not a power of two or exceeds maxsalign (PAGE_SIZE * 1024, raised to the largest superpage size: 1 GiB on amd64 with 1 GiB pages, otherwise 4 MiB) with Invalid segment alignment / ENOEXEC, which would also refuse the body's 0x80000000 example; but that check dates from December 2021 (714d6d09b57e, 36df8f540fa3, first in 13.1) and is absent from stable/11 and stable/12, so it says nothing about the PS5's own loader.

Nits

  • Body: "The gABI allows only 0, 1 or a power of two" overstates the spec. The gABI's p_align text says "Values 0 and 1 mean no alignment is required. Otherwise, p_align should be a positive, integral power of 2"; its only "must" is the page-size congruence of p_vaddr and p_offset. Linux (fs/binfmt_elf.c, maximum_alignment: "skip non-power of two alignments as invalid") and glibc (elf/dl-load.c, powerof2 filter on p_align_max) ignore such values; FreeBSD rejects them. Suggested: "The gABI says p_align should be 0, 1 or a power of two; FreeBSD's loader refuses anything else with ENOEXEC, while Linux and glibc ignore such a value."
  • GuestImageReader gets its headers from ElfReader(bytes).ReadProgramHeaders() (GuestImageReader.cpp:34), so a guest .prx with a non-power-of-two PT_LOAD p_align is now refused by the new check with PT_LOAD alignment is not a power of two (offset 0x...) before line 46 can say Invalid load segment alignment (checked with the PR build on a test_windows_import_modules.provider module patched to p_align 3; the offset-incongruence half of line 46 still fires for p_align 0x2000). The !std::has_single_bit half of line 46 is therefore unreachable, and the body's "already refuses other values" describes main, not this branch; no test references the guest message, which is why CI is unaffected. Either note the changed module diagnostic in the body or drop the dead half of line 46 here so one check owns the rule.
  • Test: RejectProgramHeaders accepts any RelinkerException without looking at the message (the file's convention, so fine); the kept list could include 2, and a covered PT_SCE_RELRO with a bad alignment staying accepted is deliberate but unpinned. Optional.
  • Body: the fuzzer input and the two FAIL logs are not attached, although the rules put notes and logs in the PR; the description omits the first segment's p_offset and the second segment's p_align, so the 3,221-byte input cannot be rebuilt exactly. The commit message says 48 s, the body 46 s.

Notes

  • Related gap, outside this diff: the executable reader does not require p_offset ≡ p_vaddr modulo the page (modules get that check in GuestImageReader.cpp:46). A PT_LOAD with vaddr 0x800 and offset 0x4000 converts on the PR build to an output whose header has off=0x4000 vaddr=0x400800; as far as I can tell from elf_map in fs/binfmt_elf.c the kernel cannot map that, but I have not run it on Linux. Follow-up material, not a defect here.
  • Overlap: #10561 edits ElfReader.cpp in ReadDynamicTags, far from these hunks, so either order merges. For disclosure, I have open PRs in this area: #10564 rejects overlapping PT_LOAD ranges in SeparateSharedLoadPages, before the padding loop; it would stop the body's fuzzer input at that check (its second segment overlaps the first) but not a disjoint layout (first segment ending mid-page, a later one starting on that page), so the two changes do not replace each other; and #10402 (Linux entry and relocation bounds). Neither touches this PR's files.

Not checked: main on any bad-alignment input (deliberately); the 46 s, 12.9 GiB and 2 GiB figures; ASan/UBSan builds; Linux or macOS hosts and execution of Linux outputs; the exact fuzzer input; a clean full-suite run on an idle machine.

I found no blocking issue: the two checks are placed and scoped correctly, the test is red without them and green with them, and the inputs behave as the body describes. The one thing I would like settled before merge is the residual power-of-two case, recorded or bounded.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5637975003 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10518#pullrequestreview-5637975003). Originally posted by [roboterspinne](https://github.com/roboterspinne) on 2026-10-24T10:49:45Z. **Review:** COMMENTED --- Reviewed head ccdbdc068 merged onto `upstream/main` aeeaefad9 (2026-10-24); the merge is clean, and so is a merge onto the current `main` 60465afd3, which does not touch the two files. **Environment:** Windows 11 Home 10.0.26200, WinLibs GCC 15.2.0 UCRT, CMake 4.3.1, Ninja 1.13.2, Python 3.13.9, `-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`, Release. **Results** - Baseline `main` aeeaefad9 (idle machine): 238 tests, 0 failed, `long_paths` skipped. PR merged: `elf_reader_bounds` passes (0.2 s). I could not get a clean full-suite run today: both full runs happened under heavy unrelated CPU load on my machine and their failures were timeouts in unrelated tests (`shader_cache_runtime_sources`, `nid_database_cache`, `select_tests`, `configure_seed*`, two Python tests), every one of which passes when rerun alone; none touches this PR's files. CI's relinker matrix is green on all three OSes. - Red/green: with the `ElfReader.cpp` hunk reverted and the PR's test kept, `elf_reader_bounds` fails with `load and uncovered SCE RELRO alignment is not a power of two: Invalid program header was accepted` while the "zero, one or a power of two" case and the existing TLS case pass; restored, it passes. - CLI with the PR build on `test_linux_load_alignment`'s fixture (one `PT_LOAD` at vaddr 0, offset 0x4000): `p_align` 3, 0x7fffffff and 0xfffffffffffffffb each exit 2 within about 50 ms with `FAIL: PT_LOAD alignment is not a power of two (offset 0x4000)`; 0, 1, 2, 0x1000, 0x4000 and 0x10000 convert. An uncovered `PT_SCE_RELRO` (vaddr 0x9000, filesz 0, memsz 0x10) with `p_align` 3 exits 2 with `FAIL: PT_SCE_RELRO alignment is not a power of two (offset 0x5000)` and converts with 0x1000; a covered `PT_SCE_RELRO` with `p_align` 3 converts, which is right because its alignment is never consumed; a `PT_LOAD` with memsz 0 and `p_align` 3 is refused, consistent with `GuestImageReader`. - I deliberately did not run `main` on a bad-alignment input. The mechanism is visible in `LinuxElfPatcher.cpp:139-141` (`SeparateSharedLoadPages`): with `alignment = max(p_align, 0x1000)` the loop `while ((buf.size() + prefix) % alignment != load.MappedAddress % alignment) buf.push_back(0);` cannot reach the target for a value near 2^64 and grows until allocation fails. The reader check stops such inputs before the patcher. - `check_conventions.py --base upstream/main` passes; `pr_overlap.py` reports no overlap. **Should fix** - After this change a power-of-two `p_align` is still unbounded: `std::has_single_bit` admits 2^31 to 2^63, and nothing between the reader and `SeparateSharedLoadPages` (run at `LinuxElfPatcher.cpp:556`, before `WriteLayout` at :573) bounds it, so a segment on a shared page with differing bytes still pads the Linux output byte by byte to `p_align` and ends as a 2 GiB file or as the generic `FAIL: std::bad_alloc` from `main.cpp:479-482` (exit 2), as the body says. CONTRIBUTING asks that known gaps be recorded in technical debt and `docs/dev/debt/relinker.md` has no entry for this. Please either add one line there (a power-of-two `p_align` above the page size pads the Linux output up to `p_align - 1` bytes and fails only at `std::bad_alloc`) or bound `p_align` in the two new checks. For reference, FreeBSD refuses a `PT_LOAD` `p_align` that is not a power of two or exceeds `maxsalign` (`PAGE_SIZE * 1024`, raised to the largest superpage size: 1 GiB on amd64 with 1 GiB pages, otherwise 4 MiB) with `Invalid segment alignment` / ENOEXEC, which would also refuse the body's 0x80000000 example; but that check dates from December 2021 (714d6d09b57e, 36df8f540fa3, first in 13.1) and is absent from stable/11 and stable/12, so it says nothing about the PS5's own loader. **Nits** - Body: "The gABI allows only 0, 1 or a power of two" overstates the spec. The gABI's `p_align` text says "Values 0 and 1 mean no alignment is required. Otherwise, p_align should be a positive, integral power of 2"; its only "must" is the page-size congruence of `p_vaddr` and `p_offset`. Linux (`fs/binfmt_elf.c`, `maximum_alignment`: "skip non-power of two alignments as invalid") and glibc (`elf/dl-load.c`, `powerof2` filter on `p_align_max`) ignore such values; FreeBSD rejects them. Suggested: "The gABI says p_align should be 0, 1 or a power of two; FreeBSD's loader refuses anything else with ENOEXEC, while Linux and glibc ignore such a value." - `GuestImageReader` gets its headers from `ElfReader(bytes).ReadProgramHeaders()` (`GuestImageReader.cpp:34`), so a guest `.prx` with a non-power-of-two `PT_LOAD` `p_align` is now refused by the new check with `PT_LOAD alignment is not a power of two (offset 0x...)` before line 46 can say `Invalid load segment alignment` (checked with the PR build on a `test_windows_import_modules.provider` module patched to `p_align` 3; the offset-incongruence half of line 46 still fires for `p_align` 0x2000). The `!std::has_single_bit` half of line 46 is therefore unreachable, and the body's "already refuses other values" describes `main`, not this branch; no test references the guest message, which is why CI is unaffected. Either note the changed module diagnostic in the body or drop the dead half of line 46 here so one check owns the rule. - Test: `RejectProgramHeaders` accepts any `RelinkerException` without looking at the message (the file's convention, so fine); the kept list could include 2, and a covered `PT_SCE_RELRO` with a bad alignment staying accepted is deliberate but unpinned. Optional. - Body: the fuzzer input and the two `FAIL` logs are not attached, although the rules put notes and logs in the PR; the description omits the first segment's `p_offset` and the second segment's `p_align`, so the 3,221-byte input cannot be rebuilt exactly. The commit message says 48 s, the body 46 s. **Notes** - Related gap, outside this diff: the executable reader does not require `p_offset ≡ p_vaddr` modulo the page (modules get that check in `GuestImageReader.cpp:46`). A `PT_LOAD` with vaddr 0x800 and offset 0x4000 converts on the PR build to an output whose header has `off=0x4000 vaddr=0x400800`; as far as I can tell from `elf_map` in `fs/binfmt_elf.c` the kernel cannot map that, but I have not run it on Linux. Follow-up material, not a defect here. - Overlap: #10561 edits `ElfReader.cpp` in `ReadDynamicTags`, far from these hunks, so either order merges. For disclosure, I have open PRs in this area: #10564 rejects overlapping `PT_LOAD` ranges in `SeparateSharedLoadPages`, before the padding loop; it would stop the body's fuzzer input at that check (its second segment overlaps the first) but not a disjoint layout (first segment ending mid-page, a later one starting on that page), so the two changes do not replace each other; and #10402 (Linux entry and relocation bounds). Neither touches this PR's files. **Not checked:** `main` on any bad-alignment input (deliberately); the 46 s, 12.9 GiB and 2 GiB figures; ASan/UBSan builds; Linux or macOS hosts and execution of Linux outputs; the exact fuzzer input; a clean full-suite run on an idle machine. I found no blocking issue: the two checks are placed and scoped correctly, the test is red without them and green with them, and the inputs behave as the body describes. The one thing I would like settled before merge is the residual power-of-two case, recorded or bounded. Written with AI assistance (Claude Code). I ran the builds, tests and CLI checks above on my Windows 11 machine.
MarcZX10 commented 2026-10-24 12:05:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T12:05:32Z.

Review: CHANGES_REQUESTED


The non-power-of-two checks address the malformed alignment that caused the allocation blow-up, and I agree the existing test covers both PT_LOAD and uncovered PT_SCE_RELRO. One dangerous case remains known in the PR description: a valid power-of-two alignment such as 0x80000000 can still make SeparateSharedLoadPages pad toward a multi-gigabyte output, and sufficiently large values fail later as std::bad_alloc. CONTRIBUTING requires known gaps to be recorded in technical debt. Please either document this remaining behavior in docs/dev/debt/relinker.md, or add an evidence-based bound at the parser/patcher boundary with a regression test. I have not independently reproduced the reported memory growth; this request is based on the code path and measurements documented in the existing review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638175090 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10518#pullrequestreview-5638175090). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T12:05:32Z. **Review:** CHANGES_REQUESTED --- The non-power-of-two checks address the malformed alignment that caused the allocation blow-up, and I agree the existing test covers both PT_LOAD and uncovered PT_SCE_RELRO. One dangerous case remains known in the PR description: a valid power-of-two alignment such as 0x80000000 can still make SeparateSharedLoadPages pad toward a multi-gigabyte output, and sufficiently large values fail later as std::bad_alloc. CONTRIBUTING requires known gaps to be recorded in technical debt. Please either document this remaining behavior in docs/dev/debt/relinker.md, or add an evidence-based bound at the parser/patcher boundary with a regression test. I have not independently reproduced the reported memory growth; this request is based on the code path and measurements documented in the existing review.
MarcZX10 commented 2026-10-24 21:54:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T21:54:40Z.

Review: APPROVED


Re-reviewed exact head e22e048dffca16949411221e6bdccf6cc8bb3874.

The current update resolves my prior request: the residual power-of-two padding behavior is now documented in docs/dev/debt/relinker.md, and the description explains the current-base behavior. The non-power-of-two checks cover both executable PT_LOAD and uncovered PT_SCE_RELRO; the test covers rejected non-powers and accepted zero, one, and power-of-two alignments. The earlier concern about the module reader's now-unreachable power-of-two check is also addressed in the current diff.

I built elf_reader_bounds_tests from this exact head using GCC 12.2 / CMake Release and ran ctest --test-dir build-review -R '^elf_reader_bounds$' --output-on-failure: 1/1 passed. check_conventions.py --base upstream/main --head upstream/pr-10518 and git diff --check passed. Hosted relinker jobs on Ubuntu, Windows, macOS arm64, macOS x86_64, ASan, eligibility, conventions, and labels pass; product jobs are skipped for this relinker-only change.

The documented large-output behavior for very large power-of-two alignments remains a known limitation rather than an unrecorded gap. No blocking findings on this head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640177051 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10518#pullrequestreview-5640177051). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T21:54:40Z. **Review:** APPROVED --- Re-reviewed exact head `e22e048dffca16949411221e6bdccf6cc8bb3874`. The current update resolves my prior request: the residual power-of-two padding behavior is now documented in `docs/dev/debt/relinker.md`, and the description explains the current-base behavior. The non-power-of-two checks cover both executable `PT_LOAD` and uncovered `PT_SCE_RELRO`; the test covers rejected non-powers and accepted zero, one, and power-of-two alignments. The earlier concern about the module reader's now-unreachable power-of-two check is also addressed in the current diff. I built `elf_reader_bounds_tests` from this exact head using GCC 12.2 / CMake Release and ran `ctest --test-dir build-review -R '^elf_reader_bounds$' --output-on-failure`: 1/1 passed. `check_conventions.py --base upstream/main --head upstream/pr-10518` and `git diff --check` passed. Hosted relinker jobs on Ubuntu, Windows, macOS arm64, macOS x86_64, ASan, eligibility, conventions, and labels pass; product jobs are skipped for this relinker-only change. The documented large-output behavior for very large power-of-two alignments remains a known limitation rather than an unrecorded gap. No blocking findings on this head.
MarcZX10 commented 2026-11-17 02:31:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T02:31:21Z.

Review: COMMENTED


Reviewed relinker PT_LOAD alignment check — no blocking findings

Nothing blocks this; the points below are optional.

What I ran

  • Head 9dcfb99b73b95f8524c03d53789336d259f0e5f9, base main. Diff read from the console's diff.patch; no merge preview.
  • Read-only inspection with repo_read.py search and cat at the head, plus the changed-file copies in the run folder.
  • No build and no test run, per the no-build review rules.
python3 repo_read.py search kPhdrAlignOffset core/relinker
python3 repo_read.py search PT_OS_RELRO core/relinker

Results

Check Result Note
Description claims vs. code passed Check and error text match the diff
p_align offset and PT_OS_RELRO value passed Offset 48 and 0x61000010 match the test byte offsets
GuestImageReader reads headers from ReadProgramHeaders passed The removed power-of-two check is unreachable for PT_LOAD
Build and unit tests not run No-build review
Hosted CI for this head not run Not queried

Findings

Blocking

None.

Non-blocking

  • core/relinker/relinker/src/parsing/ElfReader.cpp:181 — the new PT_SCE_RELRO check runs only in the uncovered branch. A covered PT_SCE_RELRO with a non-power-of-two p_align is not rejected, and no test covers that case. This matches the description's wording ("uncovered"), so it is a test gap rather than a defect.
  • core/relinker/relinker/src/guest/GuestImageReader.cpp:52 — the description cites GuestImageReader.cpp:36 for the ElfReader call. At the head that call is on line 37; line 36 sets OutputName. The claim itself holds.

Sources

Not verified

  • Windows and macOS outputs. The description says this was not tested either.
  • Hosted CI for head 9dcfb99b73b9. Approval needs it confirmed, so this review is a comment.
  • Build and runs of elf_reader_bounds and guest_load_ranges. The results in the description are the author's, not mine.
  • tools/check_conventions.py and tools/pr_overlap.py, and the open pull requests the description lists.
  • The Linux patcher's use of min(p_align, 0x1000), which I did not trace.
  • Full text of ElfReaderBoundsTests.cpp and GuestLoadRangesTests.cpp. I read the changed hunks, the Fixture and Reject helpers, and the surrounding code. I did not read the whole files, and I did not read RejectProgramHeaders to see whether it checks the message.
  • Files read: GuestImageReader.cpp, ElfReader.cpp, ElfReaderBoundsTests.cpp, GuestLoadRangesTests.cpp. Excluded by rule: none.

To get this to a good state

Nothing is required.

Optional:

  • Add a covered PT_SCE_RELRO case with alignment 3 to ElfReaderBoundsTests.cpp, or note in the description why it is out of scope.
  • Correct the GuestImageReader.cpp:36 reference in the description to line 37.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5931770799 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10518#pullrequestreview-5931770799). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-17T02:31:21Z. **Review:** COMMENTED --- # Reviewed relinker PT_LOAD alignment check — no blocking findings Nothing blocks this; the points below are optional. ## What I ran - Head `9dcfb99b73b95f8524c03d53789336d259f0e5f9`, base `main`. Diff read from the console's `diff.patch`; no merge preview. - Read-only inspection with `repo_read.py search` and `cat` at the head, plus the changed-file copies in the run folder. - No build and no test run, per the no-build review rules. ```sh python3 repo_read.py search kPhdrAlignOffset core/relinker python3 repo_read.py search PT_OS_RELRO core/relinker ``` ## Results | Check | Result | Note | |---|---|---| | Description claims vs. code | passed | Check and error text match the diff | | `p_align` offset and `PT_OS_RELRO` value | passed | Offset 48 and 0x61000010 match the test byte offsets | | `GuestImageReader` reads headers from `ReadProgramHeaders` | passed | The removed power-of-two check is unreachable for `PT_LOAD` | | Build and unit tests | not run | No-build review | | Hosted CI for this head | not run | Not queried | ## Findings ### Blocking None. ### Non-blocking - `core/relinker/relinker/src/parsing/ElfReader.cpp:181` — the new `PT_SCE_RELRO` check runs only in the uncovered branch. A covered `PT_SCE_RELRO` with a non-power-of-two `p_align` is not rejected, and no test covers that case. This matches the description's wording ("uncovered"), so it is a test gap rather than a defect. - `core/relinker/relinker/src/guest/GuestImageReader.cpp:52` — the description cites `GuestImageReader.cpp:36` for the `ElfReader` call. At the head that call is on line 37; line 36 sets `OutputName`. The claim itself holds. ## Sources - [`ElfReader.cpp` at the head, lines 157–158 and 181–182](https://github.com/boykopovar/AnyPS5/blob/9dcfb99b73b95f8524c03d53789336d259f0e5f9/core/relinker/relinker/src/parsing/ElfReader.cpp#L157-L182) - [`GuestImageReader.cpp` at the head, lines 37 and 52](https://github.com/boykopovar/AnyPS5/blob/9dcfb99b73b95f8524c03d53789336d259f0e5f9/core/relinker/relinker/src/guest/GuestImageReader.cpp#L37) ## Not verified - Windows and macOS outputs. The description says this was not tested either. - Hosted CI for head `9dcfb99b73b9`. Approval needs it confirmed, so this review is a comment. - Build and runs of `elf_reader_bounds` and `guest_load_ranges`. The results in the description are the author's, not mine. - `tools/check_conventions.py` and `tools/pr_overlap.py`, and the open pull requests the description lists. - The Linux patcher's use of `min(p_align, 0x1000)`, which I did not trace. - Full text of `ElfReaderBoundsTests.cpp` and `GuestLoadRangesTests.cpp`. I read the changed hunks, the `Fixture` and `Reject` helpers, and the surrounding code. I did not read the whole files, and I did not read `RejectProgramHeaders` to see whether it checks the message. - Files read: `GuestImageReader.cpp`, `ElfReader.cpp`, `ElfReaderBoundsTests.cpp`, `GuestLoadRangesTests.cpp`. Excluded by rule: none. ## To get this to a good state Nothing is required. Optional: - Add a covered `PT_SCE_RELRO` case with alignment 3 to `ElfReaderBoundsTests.cpp`, or note in the description why it is out of scope. - Correct the `GuestImageReader.cpp:36` reference in the description to line 37.
catminou4 commented 2026-11-25 05:48:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by catminou4 on 2026-11-25T05:48:50Z.

Review: COMMENTED


Reviewed merged into current main (61d110125e, clean merge) on Ubuntu 22.04, GCC 11.4, relinker-only build (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON).

  • ctest -R '^(elf_reader_bounds|guest_load_ranges)$': both pass.
  • Control: with GuestImageReader.cpp and ElfReader.cpp from main, both fail: elf_reader_bounds reports load and uncovered SCE RELRO alignment is not a power of two: Invalid program header was accepted, and guest_load_ranges gets Invalid load segment alignment instead of the new message (empty-first-alignment, empty-last-alignment). Restoring the PR sources passes again.
  • Full relinker-only ctest -j8: everything passes except tools_headers_manifest (test_hash_rejects_changes_that_preserve_file_size_and_mtime), which is a Python tools test unrelated to these files.
  • Moving the power-of-two check into ElfReader::ReadProgramHeaders makes every reader reject it, not only GuestImageReader; 0 and 1 still pass as the ELF spec allows.

Mergeable.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6052482506 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10518#pullrequestreview-6052482506). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T05:48:50Z. **Review:** COMMENTED --- Reviewed merged into current `main` (61d110125e, clean merge) on Ubuntu 22.04, GCC 11.4, relinker-only build (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`). - `ctest -R '^(elf_reader_bounds|guest_load_ranges)$'`: both pass. - Control: with `GuestImageReader.cpp` and `ElfReader.cpp` from `main`, both fail: `elf_reader_bounds` reports `load and uncovered SCE RELRO alignment is not a power of two: Invalid program header was accepted`, and `guest_load_ranges` gets `Invalid load segment alignment` instead of the new message (`empty-first-alignment`, `empty-last-alignment`). Restoring the PR sources passes again. - Full relinker-only `ctest -j8`: everything passes except `tools_headers_manifest` (`test_hash_rejects_changes_that_preserve_file_size_and_mtime`), which is a Python tools test unrelated to these files. - Moving the power-of-two check into `ElfReader::ReadProgramHeaders` makes every reader reject it, not only `GuestImageReader`; 0 and 1 still pass as the ELF spec allows. Mergeable.
ligmaSec commented 2026-12-03 11:58:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T11:58:22Z.

Review: COMMENTED


Built this head with ANYPS5_RELINKER_ONLY=ON and ran guest_load_ranges and elf_reader_bounds on Linux. Both passed, including the load-alignment regression.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6169308356 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10518#pullrequestreview-6169308356). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T11:58:22Z. **Review:** COMMENTED --- Built this head with `ANYPS5_RELINKER_ONLY=ON` and ran `guest_load_ranges` and `elf_reader_bounds` on Linux. Both passed, including the load-alignment regression. AI-assisted: yes.
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!10518
No description provided.