fix(libc): return 0 from zero-sized fread and fwrite before reading the stream #16170

Merged
BunnyFl merged 3 commits from upstream-pr/16170 into main 2026-11-12 02:11:26 +00:00
BunnyFl commented 2026-11-05 19:09:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-05T19:09:08Z.


What

fread and fwrite now return 0 for a zero-sized transfer before they look at the stream, in the order firmware 13.42 checks.

  • fread: size == 0 || count == 0 returns 0 first, then the size * count overflow sets EINVAL and returns 0, then the stream checks. Before this change a null stream threw (FileStream: null stream) even when nothing was to be read.
  • fwrite: the wrapped product size * count == 0 returns 0 first, then the stream checks. Before, a null stream threw, and a product that wraps to 0 (SIZE_MAX / 2 + 1 times 2) threw fwrite: buffer size overflow.
  • core/libs/tests/FileStreams.cpp: the new CheckZeroSizedTransfersOnNullStream covers both on a null stream (result 0, buffer and errno unchanged). The existing fwrite(buffer, SIZE_MAX / 2 + 1, 2, stream) expectation changes from a throw to 0 with the error indicator clear: that product wraps to 0, and the firmware returns 0 for it at 0x61ab (below).
  • docs/dev/debt/libc.md: a new entry for the gap left open, an fwrite whose product overflows to a nonzero value, which still throws here.

Source. system/common/lib/libSceLibcInternal.sprx from firmware 13.42 (sha256 a01795ab49df639dd40abd6194956ada062244826824ce486c9acab227bf5285), read from disassembly to each ret, not run, not measured on a console:

fread (lbB+UlZqVG0, 0x604f0): the zero check comes before anything touches the stream, then the overflow check stores 0x16 through __error (the stub at 0x124330 jumps through the GOT slot of __error), then 0x3830 is called with the stream and its fields are read.

604fe:	test   %rsi,%rsi
60504:	sete   %al
60507:	test   %rdx,%rdx
6050a:	sete   %cl
6050d:	or     %al,%cl
6050f:	je     0x60518
60511:	xor    %eax,%eax
60513:	jmp    0x6061a
6051e:	mul    %rdx
60521:	jno    0x60535
60523:	call   0x124330
60528:	movl   $0x16,(%rax)
60543:	call   0x3830
6054d:	movzwl (%r14),%eax

fwrite (MpxhMh8QFro, 0x6190): the product is an imul with no overflow check, and a zero product returns 0 before the stream's field at +0x4 is read. The write loop then takes the product as the number of bytes left (0x61e4), which is the gap recorded in the debt entry.

61a1:	mov    %rdx,%r14
61a4:	imul   %rsi,%r14
61a8:	test   %r14,%r14
61ab:	je     0x62f4
61bd:	cmpl   $0x0,0x4(%rcx)
61e4:	mov    %r14,%r12
62f4:	xor    %eax,%eax
62f6:	jmp    0x637c

What the module does with a null stream once the size is nonzero was not read past 0x3830; the call and the field reads after it take the stream as is, and this PR keeps throwing there.

Tested

Windows 11 Pro 10.0.26300, WinLibs GCC 15.2, Release, head e0523220f3 (main adad8fdfa0 merged in):

  • The merge conflicted in docs/dev/debt/libc.md only: main added three entries at the place where this branch adds the fwrite entry. Both sides are kept, this branch's entry first; against main the file now differs only by that entry.
  • file_streams, file_position, guest_stream_buffer_host_write and public_headers pass (4 of 4, none Not Run), with their executables and libc rebuilt.
  • tools/check_conventions.py --base upstream/main on the committed head: all checks pass. No APS5_VABI declaration changes.
  • tools/pr_overlap.py --branch fix/libc-zero-size-null-stream-transfers --jobs 8 against 1,991 open pull requests: none of the 64 it could compare shares a line or an export. It failed closed on 40 that conflict with main and share docs/dev/debt/libc.md, eight of them also FileAndHeap.cpp and one FileStreams.cpp. Their diffs, searched for fread and fwrite: #7447, #8150, #12682, #12688, #12700 and #12965 only call them in tests, and #18122 adds _nid_disambig1 wrappers that call them; none changes either function. #5021's diff is over GitHub's 300-file limit and was not read. The negative control below was not repeated, since the branch's code and test lines are unchanged.

Windows 11 Pro 10.0.26300, WinLibs GCC, Release, branch 02e7389bd7 on main 04cdd52760 (clean merge):

  • file_streams, file_position, guest_stream_buffer_host_write and public_headers pass (4 of 4).
  • Negative control, core/libs/prx/libc/src/FileAndHeap.cpp taken from main with this PR's test kept: file_streams fails with FileStream: null stream.
  • python3 tools/check_conventions.py --base upstream/main passes on the committed head.
  • Linux and macOS were not run.
  • python3 tools/pr_overlap.py --branch fix/libc-zero-size-null-stream-transfers: the only line overlap is #14610, which also appends to the end of docs/dev/debt/libc.md (L335); whichever lands second resolves that one line. No open PR changes fread_nid_postfix or fwrite_nid_postfix: of the open PRs that touch FileAndHeap.cpp or FileStreams.cpp, #11831 and #8150 only call them in tests.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5177189174 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16170). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-05T19:09:08Z. --- ### What `fread` and `fwrite` now return 0 for a zero-sized transfer before they look at the stream, in the order firmware 13.42 checks. - `fread`: `size == 0 || count == 0` returns 0 first, then the `size * count` overflow sets `EINVAL` and returns 0, then the stream checks. Before this change a null stream threw (`FileStream: null stream`) even when nothing was to be read. - `fwrite`: the wrapped product `size * count == 0` returns 0 first, then the stream checks. Before, a null stream threw, and a product that wraps to 0 (`SIZE_MAX / 2 + 1` times 2) threw `fwrite: buffer size overflow`. - `core/libs/tests/FileStreams.cpp`: the new `CheckZeroSizedTransfersOnNullStream` covers both on a null stream (result 0, buffer and `errno` unchanged). The existing `fwrite(buffer, SIZE_MAX / 2 + 1, 2, stream)` expectation changes from a throw to 0 with the error indicator clear: that product wraps to 0, and the firmware returns 0 for it at `0x61ab` (below). - `docs/dev/debt/libc.md`: a new entry for the gap left open, an `fwrite` whose product overflows to a nonzero value, which still throws here. **Source.** `system/common/lib/libSceLibcInternal.sprx` from firmware 13.42 (sha256 `a01795ab49df639dd40abd6194956ada062244826824ce486c9acab227bf5285`), read from disassembly to each `ret`, not run, not measured on a console: `fread` (`lbB+UlZqVG0`, `0x604f0`): the zero check comes before anything touches the stream, then the overflow check stores 0x16 through `__error` (the stub at `0x124330` jumps through the GOT slot of `__error`), then `0x3830` is called with the stream and its fields are read. ``` 604fe: test %rsi,%rsi 60504: sete %al 60507: test %rdx,%rdx 6050a: sete %cl 6050d: or %al,%cl 6050f: je 0x60518 60511: xor %eax,%eax 60513: jmp 0x6061a 6051e: mul %rdx 60521: jno 0x60535 60523: call 0x124330 60528: movl $0x16,(%rax) 60543: call 0x3830 6054d: movzwl (%r14),%eax ``` `fwrite` (`MpxhMh8QFro`, `0x6190`): the product is an `imul` with no overflow check, and a zero product returns 0 before the stream's field at `+0x4` is read. The write loop then takes the product as the number of bytes left (`0x61e4`), which is the gap recorded in the debt entry. ``` 61a1: mov %rdx,%r14 61a4: imul %rsi,%r14 61a8: test %r14,%r14 61ab: je 0x62f4 61bd: cmpl $0x0,0x4(%rcx) 61e4: mov %r14,%r12 62f4: xor %eax,%eax 62f6: jmp 0x637c ``` What the module does with a null stream once the size is nonzero was not read past `0x3830`; the call and the field reads after it take the stream as is, and this PR keeps throwing there. ### 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. --> Windows 11 Pro 10.0.26300, WinLibs GCC 15.2, Release, head `e0523220f3` (`main` `adad8fdfa0` merged in): - The merge conflicted in `docs/dev/debt/libc.md` only: `main` added three entries at the place where this branch adds the `fwrite` entry. Both sides are kept, this branch's entry first; against `main` the file now differs only by that entry. - `file_streams`, `file_position`, `guest_stream_buffer_host_write` and `public_headers` pass (4 of 4, none Not Run), with their executables and `libc` rebuilt. - `tools/check_conventions.py --base upstream/main` on the committed head: all checks pass. No `APS5_VABI` declaration changes. - `tools/pr_overlap.py --branch fix/libc-zero-size-null-stream-transfers --jobs 8` against 1,991 open pull requests: none of the 64 it could compare shares a line or an export. It failed closed on 40 that conflict with `main` and share `docs/dev/debt/libc.md`, eight of them also `FileAndHeap.cpp` and one `FileStreams.cpp`. Their diffs, searched for `fread` and `fwrite`: #7447, #8150, #12682, #12688, #12700 and #12965 only call them in tests, and #18122 adds `_nid_disambig1` wrappers that call them; none changes either function. #5021's diff is over GitHub's 300-file limit and was not read. The negative control below was not repeated, since the branch's code and test lines are unchanged. Windows 11 Pro 10.0.26300, WinLibs GCC, Release, branch `02e7389bd7` on `main` `04cdd52760` (clean merge): - `file_streams`, `file_position`, `guest_stream_buffer_host_write` and `public_headers` pass (4 of 4). - Negative control, `core/libs/prx/libc/src/FileAndHeap.cpp` taken from `main` with this PR's test kept: `file_streams` fails with `FileStream: null stream`. - `python3 tools/check_conventions.py --base upstream/main` passes on the committed head. - Linux and macOS were not run. - `python3 tools/pr_overlap.py --branch fix/libc-zero-size-null-stream-transfers`: the only line overlap is #14610, which also appends to the end of `docs/dev/debt/libc.md` (L335); whichever lands second resolves that one line. No open PR changes `fread_nid_postfix` or `fwrite_nid_postfix`: of the open PRs that touch `FileAndHeap.cpp` or `FileStreams.cpp`, #11831 and #8150 only call them in tests. ### 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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
gabichuuuuu commented 2026-11-09 08:52:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-09T08:52:04Z.

Review: COMMENTED


Windows run of head ecd1a1bd2a, merged onto main 286b14d2f7 in one tree with 47 other small open PRs that touch different files. Setup: Windows 11, WinLibs GCC 15.2 r7 (x86_64-ucrt-posix-seh, the CI toolchain), Release, BUILD_TESTING=ON.

  • file_streams with --repeat until-fail:5: 5 / 5 pass.
  • Negative control, with main's FileAndHeap.cpp and this PR's test: 5 / 5 runs fail.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5823548302 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16170#pullrequestreview-5823548302). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-09T08:52:04Z. **Review:** COMMENTED --- Windows run of head `ecd1a1bd2a`, merged onto `main` `286b14d2f7` in one tree with 47 other small open PRs that touch different files. Setup: Windows 11, WinLibs GCC 15.2 r7 (`x86_64-ucrt-posix-seh`, the CI toolchain), Release, `BUILD_TESTING=ON`. - `file_streams` with `--repeat until-fail:5`: 5 / 5 pass. - **Negative control,** with `main`'s `FileAndHeap.cpp` and this PR's test: 5 / 5 runs fail.
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!16170
No description provided.