Repository navigation
fix(libkernel): zero-fill APR reads that reach the end of the file - #5255
Merged
oneandonlydean merged 1 commit intoOct 14, 2026
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
APR file reads threw when the file ended before the requested length (
_readFileincore/libs/prx/libkernel/Apr/src/Apr.cpp; every read command goes through it, gather and scatter reads included). That throw came with #232, which replaced accepting a short read with the rest of the destination left untouched. This change keeps the throw for a short read that stops before the file's current end and zero-fills one that reaches it, instead of going back to leaving the rest untouched:std::filesystem::file_size), not the size from the resolve.main. Whether a read of a file that is appended to while it runs throws or is zero-filled depends on timing.bad()) still throws "APR: read failed"._read, whose 32-bit count makes it 0, and takes the empty result as end-of-file. Such a read still throws "returned 0 of 4294967296 bytes", as onmain, unless it starts at or past the end of the file, where it is now zero-filled.std::filesystem::filesystem_errorinstead of the APR message, and a file replaced by a rename is judged by the new file's size. On Windows neither can happen while the reader's stream is open: removing the file or renaming another file over it fails with a sharing violation (checked with a stand-alone copy of the read and its condition).docs/dev/debt/libkernel.mdrecords the rule, and that the console's behaviour for a read past the end of a file is unverified (it may zero-fill, leave the destination untouched or fail the command).Ready or Not (PPSA18605) reads its settings file this way. Traced with temporary logging on my bring-up build (run 185):
/download0/readyornot/saved/ps5/gameusersettings.iniat 5480 bytes.sceKernelOpen, one 5479-bytesceKernelWriteandsceKernelClose. The only change in the file isInitialOptionsIsSet=FalsebecomingTrue, one byte shorter.main, it calls neithersceKernelAprGetFileSizenorsceKernelAprGetFileStatfor the file (only its Epic Online Services module imports them), and itssceKernelStatafter the save sees 5479 bytes.Without this change the read throws and the title terminates:
Tests added to
core/libs/tests/GuestAmpr.cpp(guest_ampr_tests, ctestguest_ampr_markers):TestReadPastEndOfFileZeroFillsresolves a 64-byte file in a new directory, rewrites it on the host as 63 different bytes, and submits into buffers filled with 0xA5: a 64-byte read at offset 0 (bytes 0-62 must be the new content and byte 63 zero), a 16-byte scatter after it (all zeros), an 8-byte gather at offset 0 after that (the file's first 8 bytes, behind the scatter's 16), and 16-byte reads at offset 63, the new end of the file, and at offset 4096 (both all zeros).TestReadAtUnseekableOffsetThrowswrites a read at offset 2^63 into the command buffer directly, since the recording API rejects offsets of 2^40 and above; the seek fails before the end of the file, and the submit must still throw "returned 0 of 4 bytes". It passes onmainas well._readFilecall (fix(libkernel): keep host standard streams open on guest close #3295's case) or a writer grows the file before the size query, which a test cannot arrange without racing the read, and on Windows for the 2^32-byte read above. No test is kept for that read, because it would require a valid read to fail, a limitmainshares, and would break once such reads are split; a probe checks it instead (below). A read error does not reach the size operand: under a Windows byte-range lock, libstdc++ setsbadbitand the read throws "APR: read failed" before the size check (below).Overlaps:
mainalready throws on a short read, so staging changed nothing a title sees, and for the second copy on every read and its retry loop (fix(libkernel): pread, read and APR reads into write-tracked guest memory through a staging buffer #182).sceKernelClose(0)closes the descriptor the APR reader'sstd::ifstreamholds, and the C runtime gives descriptor 0 to the next file opened. With this change those reads still abort (from the code; not run): a short read of a range inside the file on disk always throws, and fix(libkernel): keep host standard streams open on guest close #3295 found the bundles complete on disk. A read through the reused descriptor that gets enough bytes from the other file still returns them without an error, as onmain. Its "APR: read failed" errors are unchanged. fix(libkernel): keep host standard streams open on guest close #3295 is still needed.Apr.cpp,GuestAmpr.cppanddocs/dev/debt/libkernel.md; this branch is rebased over it. No open PR touchesApr.cpporGuestAmpr.cpp. The 53 open PRs that editdocs/dev/debt/libkernel.mdmerge with this branch exactly as they merge withmain(git merge-tree).Tested
Windows 11 Pro 10.0.22631, AMD Ryzen 7 7800X3D, AMD Radeon RX 7900 XTX, WinLibs GCC 15.2.0, Release,
BUILD_TESTING=ON:maine0d388f, this head:ctestfor libSceAmpr (4 of 4) and libkernel (125 of 125) passes, and the four libSceAmpr tests passed 10 runs in a row.TestResolveSymbolicLinkis skipped without Developer Mode, as onmain.tools/check_conventions.py --base e0d388fa2passes.main1268ba8, the base before, whose_readFileis the same, one change at a time, rebuildinglibkerneland runningguest_ampr_tests:memsetline,TestReadPastEndOfFileZeroFillsfails (whole[size - 1] == 0);main, with the resolved size in place offile_size, or with<=in place of<,TestReadPastEndOfFileZeroFillsaborts with "APR: read of ...\ampr_read_past_end\settings.ini at offset 0 returned 63 of 64 bytes";TestReadPastEndOfFileZeroFillsfails (the gather lands on the scatter's zeros);TestReadAtUnseekableOffsetThrowsfails;Ready or Not (PPSA18605) ran on
maind1fd937, this branch's first base;_readFileis the same there as on e0d388f. The runtime'slibsheld the 123 modules built from d1fd937, withlibkernelwithout this change (run 300) and with it (run 302);app.exe, the title relinked for Windows, is from my bring-up runtime and was not rebuilt. Both runs started from the settings file the title wrote on its first boot (InitialOptionsIsSet=False, 5480 bytes), withAPS5_TRACE_APR=1:main: "AGC graphics: color views of several array slices are unsupported" (State.cpp:1011; open feat(agc): render layered draws into 3D color targets #1077 adds layered 3D color targets).InitialOptionsIsSet=True.Linux: not run.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)