Skip to content

fix(io): avoid allocating the entire file prefix limit - #10545

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Bram910:fix/file-prefix-allocation
Oct 26, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Bram910:fix/file-prefix-allocation

Conversation

@Bram910

@Bram910 Bram910 commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

What

  • Read file prefixes in bounded chunks, growing the result only as bytes are read instead of allocating the entire requested limit upfront. A large valid prefix limit now returns the available bytes from a short or empty file instead of failing with std::bad_alloc.
  • Keep the existing regular-file checks, stream-size limit, binary data handling, and read-error diagnostics. Do not rely on file_size, since regular proc files can report zero while still containing readable data.
  • Add a portable CTest for empty/short files, exact and partial chunk boundaries, a maximum valid stream-size limit, UTF-8 filenames, missing files, directories, and invalid limits. Linux additionally covers /proc/self/cmdline with its zero reported file size.

Tested

  • Red/green on Windows x64, MinGW GCC 14.1: the new regression compiled against the unchanged reader fails with std::bad_alloc for an empty file and the maximum valid prefix limit. It passes with this fix.
  • Windows x64, MinGW GCC 14.1, Python 3.13: full relinker-only build succeeds and 239/239 CTests pass (ctest --test-dir build-relinker --output-on-failure --parallel 4).
  • Windows focused CTests file_reader_prefix, input_magic, input_file, and input_nonascii: five consecutive passes each (--repeat until-fail:5).
  • Ubuntu 22.04 under WSL, GCC 11.4: focused Release and ASan/UBSan builds with -Wall -Wextra -Werror, five successful runs each. Sanitizers use detect_leaks=1:halt_on_error=1 and halt_on_error=1:print_stacktrace=1.
  • Linux relinker and CMake test target build successfully; file_reader_prefix passes five consecutive CTest runs. The test_input_magic.py, test_input_file.py, and test_input_nonascii.py CLI integration scripts also pass, using native Linux temporary storage.
  • python tools/check_conventions.py --base origin/main and git diff --check pass.
  • python tools/pr_overlap.py --repo boykopovar/AnyPS5 --branch fix/file-prefix-allocation --jobs 4 found no competing prefix-reader implementation. It could not automatically compare the shared CMake file against already-conflicted PRs feat(aot): consolidate native ARM64 compiler, runtime and consumers #5021, test: organize library tests and label CTest suites #6044, and feat(relinker): trace function imports on macOS #9408. Manually reviewed their CMake patches: they add macOS fixtures, migrate existing registrations to component tests, and add macOS trace-import tests, respectively. None changes ReadPrefix or adds the new reader test target.
  • No full Linux suite, macOS, GPU, or title-runtime claim; the Linux checks above are focused tests for this I/O change.

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

@github-actions

github-actions Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/relinker/CMakeLists.txt
PR 6713 core/relinker/CMakeLists.txt
PR 10861 core/relinker/CMakeLists.txt

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the bounded 4 KiB read loop and test coverage for empty/short files, chunk boundaries, maximum valid stream size, UTF-8 paths, missing files, directories, invalid limits, and Linux proc files with zero reported size. The loop stops on short reads without depending on file size and preserves the existing bad-stream error. I built and ran the exact PR-head test directly with GCC 12 (-std=c++20 -Wall -Wextra -Werror): File prefix-read tests passed. Hosted eligibility, conventions, intake, labels, Linux/Windows/macOS relinker builds, and ASAN pass; unrelated platform build jobs were skipped by eligibility.

@oneandonlydean oneandonlydean self-assigned this Oct 25, 2026

@roboterspinne roboterspinne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed head 4866d9e merged onto upstream/main 63a863d (2026-10-25); the merge is clean.

Environment: Windows 11 Home 10.0.26200, Intel i9-12900H, WinLibs GCC 15.2.0 UCRT, CMake 4.3.1, Ninja 1.13.2, -DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON, Release.

Results

  • Full relinker-only suite on the merged tree: 262 tests, 260 pass, long_paths skipped. The one failure is patch_advisor_unit. It fails the same way on plain main on this machine, because the temp path contains a space (#10667), and it is unrelated. file_reader_prefix passes.
  • Counter-check: with main's FileReader.cpp under this test, file_reader_tests exits 1 with std::bad_alloc. With this branch it prints File prefix-read tests passed.
    • Only the maximum limit is red on main. With that one limit removed, the test passes on main, so the other cases are regression coverage for the new loop.
    • Those cases catch real mistakes in the loop. Always stopping after the first chunk fails at limit 4097. Requesting a whole chunk regardless of the limit fails at limit 1, and so does inserting requested instead of count. Opening the stream without std::ios::binary fails at 4095, because the fill pattern covers all 256 byte values, 0x1A included.
  • The new loop returns the same bytes as the old single read. In GCC 15.2's libstdc++, basic_filebuf::xsgetn keeps reading until end of file or an error (bits/fstream.tcc:717-731). So count < requested only happens at EOF or with badbit set, and an error after a partial chunk still throws Cannot read file: <path>. For every limit up to 4096, it is the same single read call as before.
  • ReadPrefix has one production caller on main, main.cpp:44. It reads 0x40 bytes for the ELF magic check and makes the same single read as before. So nothing changes for users today; the PR removes a latent bad_alloc from the API.
  • check_conventions.py --base upstream/main passes with this title and body. Hosted relinker jobs (ubuntu, windows, macOS) are green on 4866d9e.

Notes, nothing to change here

  • The overlap paragraph is slightly out of date. #6044 has been closed since. Two open PRs on the same files are not named: your #10303, which adds native_path_tests to the same CMakeLists.txt, and #9711, which changes the input_magic registration that covers the only caller. Both merge cleanly with this PR.
  • The Linux /proc/self/cmdline case uses file_size == 0 as a hard precondition, so it would fail rather than skip if a sandboxed /proc ever behaved differently.

Not checked: Linux and macOS locally (the /proc case is compiled out on Windows; it ran in the hosted ubuntu job).

I found nothing to change: the chunked loop keeps the old results and diagnostics for every limit, and the test pins the fix and the loop's edges. For disclosure: I have open relinker PRs, and three of them (#10732, #10646, #10402) also touch core/relinker/CMakeLists.txt; each merges cleanly with this one.

Written with AI assistance (Claude Code). I ran the build, the tests and the counter-check above on my Windows 11 machine.

@oneandonlydean
oneandonlydean merged commit 3fa7e3f into boykopovar:main Oct 26, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants