fix(relinker): reject large non-ELF inputs before allocation #8689

Merged
MarcZX10 merged 4 commits from upstream-pr/8689 into main 2026-10-21 19:12:49 +00:00
MarcZX10 commented 2026-10-20 21:07:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T21:07:01Z.


What

Preflight the input with the existing ELF header parser using a bounded 64-byte read before FileReader::Read allocates a vector for the full file. Unsupported large files now fail with the existing format-specific diagnostic instead of std::bad_alloc; short ELF inputs and SELF/bad-magic diagnostics continue through the same parser. The full read and all later ELF handling are unchanged.

Fixes #8685
Unblocks: none
GPUs tested: n/a

Tested

Debian GNU/Linux 13, x86-64, GCC 12.2.0, Ninja, Release, ANYPS5_RELINKER_ONLY=ON, BUILD_TESTING=ON.

  • Built the relinker and all relinker-only test targets.
  • Full relinker-only CTest suite: 43/43 passed.
  • The input_magic regression creates an 8 GiB sparse non-ELF file and limits the child relinker to 512 MiB of address space. A pre-fix local binary from b5d55bd804 fails with FAIL: std::bad_alloc; this branch exits 2 with Invalid ELF magic number: 7f 46 49 48 and creates no output.
  • python3 tools/check_conventions.py --base origin/main, git diff --check origin/main...HEAD, and python3 tools/pr_overlap.py --branch codex/reject-large-non-elf-early pass; the overlap check reports no open-PR overlap.
  • No title was run; the issue concerns rejection of unsupported package inputs, not package support.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4942506532 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8689). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-20T21:07:01Z. --- ### What Preflight the input with the existing ELF header parser using a bounded 64-byte read before `FileReader::Read` allocates a vector for the full file. Unsupported large files now fail with the existing format-specific diagnostic instead of `std::bad_alloc`; short ELF inputs and SELF/bad-magic diagnostics continue through the same parser. The full read and all later ELF handling are unchanged. Fixes #8685 Unblocks: none GPUs tested: n/a ### Tested Debian GNU/Linux 13, x86-64, GCC 12.2.0, Ninja, Release, `ANYPS5_RELINKER_ONLY=ON`, `BUILD_TESTING=ON`. - Built the relinker and all relinker-only test targets. - Full relinker-only CTest suite: 43/43 passed. - The `input_magic` regression creates an 8 GiB sparse non-ELF file and limits the child relinker to 512 MiB of address space. A pre-fix local binary from `b5d55bd804` fails with `FAIL: std::bad_alloc`; this branch exits 2 with `Invalid ELF magic number: 7f 46 49 48` and creates no output. - `python3 tools/check_conventions.py --base origin/main`, `git diff --check origin/main...HEAD`, and `python3 tools/pr_overlap.py --branch codex/reject-large-non-elf-early` pass; the overlap check reports no open-PR overlap. - No title was run; the issue concerns rejection of unsupported package inputs, not package support. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
Filip722 commented 2026-10-21 09:14:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Filip722 on 2026-10-21T09:14:01Z.


Confirmed the allocation and early-rejection fix for my report #8685 on 11adccfe15258e21e9ba6e90b947f674320ad9a8. I rebuilt this revision on Debian 13 x86-64 (GCC 15.2, Release, relinker-only) and compared it with the original reported binary at a6a4101e1.

  • With the same 512 MiB address-space cap, the 8 GiB sparse non-ELF reproduction changes from FAIL: std::bad_alloc to exit 2 with FAIL: Invalid ELF magic number: 7f 46 49 48.
  • Retested the original 7,762,206,580-byte base package and 1,189,769-byte upgrade package: both now produce that format diagnostic.
  • Repeated all cases with and without --skip-sce-module. Invalid input is rejected before the missing-module diagnostic on the default CLI path, and no output file is created.
  • All 235 tests registered by the relinker-only configuration passed, including input_magic and elf_header.

This confirms the reported allocation/validation-order bug is fixed. The error text still only names the invalid magic; explicit guidance to supply an extracted clean ELF and its bundled modules, as requested in the issue, would remain a useful diagnostic improvement. No title execution was tested.

AI-assisted verification.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6312391924 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8689#issuecomment-6312391924). Originally posted by [Filip722](https://github.com/Filip722) on 2026-10-21T09:14:01Z. --- Confirmed the allocation and early-rejection fix for my report #8685 on `11adccfe15258e21e9ba6e90b947f674320ad9a8`. I rebuilt this revision on Debian 13 x86-64 (GCC 15.2, Release, relinker-only) and compared it with the original reported binary at `a6a4101e1`. - With the same 512 MiB address-space cap, the 8 GiB sparse non-ELF reproduction changes from `FAIL: std::bad_alloc` to exit 2 with `FAIL: Invalid ELF magic number: 7f 46 49 48`. - Retested the original 7,762,206,580-byte base package and 1,189,769-byte upgrade package: both now produce that format diagnostic. - Repeated all cases with and without `--skip-sce-module`. Invalid input is rejected before the missing-module diagnostic on the default CLI path, and no output file is created. - All 235 tests registered by the relinker-only configuration passed, including `input_magic` and `elf_header`. This confirms the reported allocation/validation-order bug is fixed. The error text still only names the invalid magic; explicit guidance to supply an extracted clean ELF and its bundled modules, as requested in the issue, would remain a useful diagnostic improvement. No title execution was tested. AI-assisted verification.
cadenasynyster commented 2026-10-21 17:54:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by cadenasynyster on 2026-10-21T17:54:29Z.


Independent Windows run of this PR.

Environment: Windows 11 10.0.26300 (NTFS), Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7 UCRT, Release, BUILD_TESTING=ON, PR head.

Tests: elf_header and input_magic pass. The 8 GiB case in test_input_magic.py runs only on Linux (RLIMIT_AS), so it is skipped here.

Manual probe on Windows: I created a 4 GiB file whose first bytes are 7f 46 49 48 (set with FileStream.SetLength) and ran relinker --windows --skip-sce-module <file> <out>. I then kept the PR's tests and replaced only the five production files (FileReader.hpp/.cpp, main.cpp, ElfReader.hpp/.cpp) with their base versions:

  • Base: fails in 0.51 s with Cannot read file: <path>, and no output file is created.
  • PR head: fails in 0.15 s with Invalid ELF magic number: 7f 46 49 48, and no output file is created.

So on this host the base does not show std::bad_alloc; the user-visible problem is a misleading read error, and the PR replaces it with the format diagnostic.

Not run: Linux/macOS, a baseline run of the unit tests, peak memory, the bundled-module scan without --skip-sce-module, and the full repository CTest.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6322806202 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8689#issuecomment-6322806202). Originally posted by [cadenasynyster](https://github.com/cadenasynyster) on 2026-10-21T17:54:29Z. --- Independent Windows run of this PR. **Environment:** Windows 11 10.0.26300 (NTFS), Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7 UCRT, Release, BUILD_TESTING=ON, PR head. **Tests:** `elf_header` and `input_magic` pass. The 8 GiB case in `test_input_magic.py` runs only on Linux (`RLIMIT_AS`), so it is skipped here. **Manual probe on Windows:** I created a 4 GiB file whose first bytes are `7f 46 49 48` (set with `FileStream.SetLength`) and ran `relinker --windows --skip-sce-module <file> <out>`. I then kept the PR's tests and replaced only the five production files (`FileReader.hpp/.cpp`, `main.cpp`, `ElfReader.hpp/.cpp`) with their base versions: - Base: fails in 0.51 s with `Cannot read file: <path>`, and no output file is created. - PR head: fails in 0.15 s with `Invalid ELF magic number: 7f 46 49 48`, and no output file is created. So on this host the base does not show `std::bad_alloc`; the user-visible problem is a misleading read error, and the PR replaces it with the format diagnostic. **Not run:** Linux/macOS, a baseline run of the unit tests, peak memory, the bundled-module scan without `--skip-sce-module`, and the full repository CTest. AI-assisted: yes
adriadam10 commented 2026-10-21 19:13:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-21T19:13:43Z.


Checked the diff against the description: ValidateMagic is the existing header check moved out of ReadHeader with the same messages, and Convert now runs it on a 64-byte prefix before the full read. The 8 GiB sparse-file test under a 512 MiB address-space cap fails with std::bad_alloc on main and passes here. Relinker jobs (Linux, Windows, macOS, ASan) are green, and Filip722's and cadenasynyster's runs confirm it on Debian and Windows; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6324373683 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8689#issuecomment-6324373683). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-21T19:13:43Z. --- Checked the diff against the description: `ValidateMagic` is the existing header check moved out of `ReadHeader` with the same messages, and `Convert` now runs it on a 64-byte prefix before the full read. The 8 GiB sparse-file test under a 512 MiB address-space cap fails with `std::bad_alloc` on `main` and passes here. Relinker jobs (Linux, Windows, macOS, ASan) are green, and Filip722's and cadenasynyster's runs confirm it on Debian and Windows; merged.
Iluminatti312 commented 2026-10-21 19:16:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Iluminatti312 on 2026-10-21T19:16:04Z.


Independent Windows validation — PASS

I independently built and tested this PR on Windows.

Environment

  • OS: Windows 11 Pro, build 26200
  • Compiler: WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh)
  • CMake: 4.4.4
  • Ninja: 1.13.2
  • Python: 3.13.14
  • PR commit: 11adccfe1
  • Build: Release, ANYPS5_RELINKER_ONLY=ON, BUILD_TESTING=ON

Build and test results

  • CMake configuration: PASS
  • Ninja build: PASS (309/309)
  • Focused input_magic test: PASS
  • Full relinker-only CTest suite: 229 passed, 0 failed, 1 skipped (230 registered tests)
  • Skipped test: long_paths, because Windows LongPathsEnabled is not enabled.

Additional Windows reproduction

I created a 256 MiB NTFS sparse file whose first four bytes were 7f 46 49 48 (invalid ELF magic), then executed:

.\build-review-8689\core\relinker\relinker.exe --windows --skip-sce-module <input-file> <output-file>

Observed:

FAIL: Invalid ELF magic number: 7f 46 49 48
ExitCode=2
OutputExists=False

The invalid large input was rejected with the expected ELF diagnostic, and no output executable was created.

Scope of validation: This confirms compilation, regression tests, and early rejection of a 256 MiB invalid input on Windows. I did not measure peak memory usage or reproduce the original 8 GiB / 512 MiB address-space-limit scenario.

AI-assisted investigation and test preparation; all reported commands and results were executed locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6324425591 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8689#issuecomment-6324425591). Originally posted by [Iluminatti312](https://github.com/Iluminatti312) on 2026-10-21T19:16:04Z. --- ### Independent Windows validation — PASS I independently built and tested this PR on Windows. **Environment** - OS: Windows 11 Pro, build 26200 - Compiler: WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh) - CMake: 4.4.4 - Ninja: 1.13.2 - Python: 3.13.14 - PR commit: `11adccfe1` - Build: Release, `ANYPS5_RELINKER_ONLY=ON`, `BUILD_TESTING=ON` **Build and test results** - CMake configuration: PASS - Ninja build: PASS (309/309) - Focused `input_magic` test: PASS - Full relinker-only CTest suite: 229 passed, 0 failed, 1 skipped (230 registered tests) - Skipped test: `long_paths`, because Windows `LongPathsEnabled` is not enabled. **Additional Windows reproduction** I created a 256 MiB NTFS sparse file whose first four bytes were `7f 46 49 48` (invalid ELF magic), then executed: ```powershell .\build-review-8689\core\relinker\relinker.exe --windows --skip-sce-module <input-file> <output-file> ``` Observed: ```text FAIL: Invalid ELF magic number: 7f 46 49 48 ExitCode=2 OutputExists=False ``` The invalid large input was rejected with the expected ELF diagnostic, and no output executable was created. **Scope of validation:** This confirms compilation, regression tests, and early rejection of a 256 MiB invalid input on Windows. I did not measure peak memory usage or reproduce the original 8 GiB / 512 MiB address-space-limit scenario. AI-assisted investigation and test preparation; all reported commands and results were executed locally.
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!8689
No description provided.