feat(libc): implement unlocked character I/O #1765

Closed
MarcZX10 wants to merge 1 commit from upstream-pr/1765 into main
MarcZX10 commented 2026-10-08 18:42:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-08T18:42:01Z.


What

Implement the missing getc_unlocked and putc_unlocked imports. Use no-lock host CRT operations on Windows and POSIX unlocked operations elsewhere, while keeping the guest stream state in sync. Both imports appear in the audit in #1578.

Tested

Linux: built libc and guest_standard_stream_tests; ctest --test-dir build -R ^guest_standard_streams$ --output-on-failure passed. python3 tools/check_conventions.py --base upstream/main passed. Windows and PS5 hardware not tested.

Checklist

  • Based on current main; no other open PR implements these functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw; no stub added
  • No notes, investigation files or images added
  • No third-party code added
  • Generic behavior
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4791287568 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1765). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-08T18:42:01Z. --- ### What Implement the missing `getc_unlocked` and `putc_unlocked` imports. Use no-lock host CRT operations on Windows and POSIX unlocked operations elsewhere, while keeping the guest stream state in sync. Both imports appear in the audit in #1578. ### Tested Linux: built `libc` and `guest_standard_stream_tests`; `ctest --test-dir build -R ^guest_standard_streams$ --output-on-failure` passed. `python3 tools/check_conventions.py --base upstream/main` passed. Windows and PS5 hardware not tested. ### Checklist - [x] Based on current `main`; no other open PR implements these functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw; no stub added - [x] No notes, investigation files or images added - [x] No third-party code added - [x] Generic behavior - [x] Depends on: none - [x] AI-assisted: yes
oneandonlydean commented 2026-10-08 19:22:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-08T19:22:16Z.


No title reaches getc_unlocked/putc_unlocked on main yet: the homebrew audit lists the import, but those titles stop earlier at other missing imports. Closing for now; reopen when it's a title's next stop on main, with the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6067361054 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1765#issuecomment-6067361054). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-08T19:22:16Z. --- No title reaches `getc_unlocked`/`putc_unlocked` on main yet: the homebrew audit lists the import, but those titles stop earlier at other missing imports. Closing for now; reopen when it's a title's next stop on main, with the log line.
babyhuey commented 2026-10-11 07:39:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-11T07:39:01Z.


@MarcZX100 putc_unlocked is now a title's next stop on main b6f649d9. With your dirname (#3261) merged, Porpoise (PPSA99764) reports this on Linux (catalog build, sce_module/libc.prx unwrapped as #2605 requires):

app.elf: symbol lookup error: app.elf: undefined symbol: H-QeERgWuTM

This head (151814a3) merges cleanly onto b6f649d9, guest_standard_streams passes 5/5 (--repeat until-fail:5), and Porpoise then stops at pathconf (oc98+zOImus).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106758065 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1765#issuecomment-6106758065). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-11T07:39:01Z. --- @MarcZX100 `putc_unlocked` is now a title's next stop on `main` b6f649d9. With your `dirname` (#3261) merged, Porpoise (PPSA99764) reports this on Linux (catalog build, `sce_module/libc.prx` unwrapped as #2605 requires): `app.elf: symbol lookup error: app.elf: undefined symbol: H-QeERgWuTM` This head (151814a3) merges cleanly onto b6f649d9, `guest_standard_streams` passes 5/5 (`--repeat until-fail:5`), and Porpoise then stops at `pathconf` (`oc98+zOImus`).
Amoenus commented 2026-10-11 08:56:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Amoenus on 2026-10-11T08:56:51Z.


PPSA99902 validation and unlocked-status correction (2026-10-11, 2e12a9f4)

Validated the exact unmerged head 151814a3 on pinned main ccf9ff90. This adapts MarcZX100's existing work; no new PR has been opened. Proposed correction/tests are preserved in our fork commit 2e12a9f4. AI-assisted adaptation and investigation: Codex.

Confirmed Linux behavior to correct: both new functions call stream->SyncStatus() after the unlocked byte operation. SyncStatus uses std::feof and std::ferror; glibc can acquire the FILE lock there. See pinned feof.c and ferror.c. Actual production StandardStreams.cpp + FileStream.hpp were compiled into a native probe on Ubuntu 24.04 WSL, x86_64, glibc 2.39. With one thread holding flockfile but performing no I/O, an unlocked read/write from another thread did not finish within 1 second; the probe releases the lock and joins both threads before failing. 5/5 reads and 5/5 writes reproduced unlocked call waited for another thread's stream lock. The source link is glibc 2.42; the live probe separately establishes the behavior on installed 2.39.

The correction adds SyncUnlockedStatus, using feof_unlocked/ferror_unlocked on POSIX and the observed non-locking UCRT feof/ferror on Windows, and uses it from both functions. It preserves guest EOF/error bits, encoding-error state, other stream flags, and resets guest read/write macro counters. Native byte I/O still uses the original PR's getc_unlocked/putc_unlocked and _fgetc_nolock/_fputc_nolock.

Check Result
Original PR, Windows UCRT guest_standard_streams 5/5 pass
Original PR, Windows held-stream-lock native probes 5 reads + 5 writes pass
Corrected Windows extended guest_standard_streams 5/5 pass, max 0.27 s
Corrected native Linux full byte/status/refusal/lock probes 5/5 pass
Linux deliberately incorrect variants All 4 caught: locking status, locking byte operation, stale status/counters, false zero result
Current-main Windows full build + all configured system libraries Pass; 122 libraries
Current-main full Windows suite, ctest -j12, RTX 5080, cache disabled 795 pass, 5 documented display/no-Vulkan skips, 6 GPU failures; serial recheck reproduces all 6 failures, not a clean-suite claim
Conventions Pass

The full-suite failures are agc_driver_f32_compare_constant and flat-aperture parts 0, 2, 3, 4, 5; several record device loss or crashes. All six reproduce serially. Their relationship to current upstream GPU changes is unresolved; GPU observations and failing byte-read controls are recorded on #3291. New I/O tests in GuestStandardStreams.cpp cover unsigned-byte conversion (including high-bit bytes), EOF/reset, write failure without file modification, guest macro state, encoding-error retention, and both held-stream-lock operations. The Windows standalone exception harness was not accepted as null/closed-stream proof; those refusal probes passed on Linux. No Linux full runtime/GPU build, other CRT or PS5-console equivalence is claimed.

Title validation: PPSA99902, Sonic Unleashed Recompiled 01.003.000, original ELF SHA-256 c34f5b0848ef49e96a855076d8254254047e966e4fe56589d0e504fe13828479. Fresh default conversion uses relinker source exactly matching f60923bf and the zero-return fallback protection from merged #2705. Runtime is our previously validated experimental integration dff875a7, with pending/local prerequisites; this is not an unmodified-main startup claim.

Before: FAIL: unresolved ELF import H-QeERgWuTM from libSceLibcInternal.prx (putc_unlocked), 0.146 s.

After replacing only libc.prx (other 121 libraries, executable and guest module unchanged): five launches stop at FAIL: unresolved ELF import 0hlfW1O4Aa4 from libSceLibcInternal.prx (localeconv), 0.112–0.130 s. All exit 0xC0000139, no timeout, before guest entry. An audit of all 122 native libraries confirms libc.prx now provides both unlocked NIDs and no native provider provides localeconv. This establishes loader progress, not successful guest execution of either I/O function. Each run restored/hash-verified the exact 123-library baseline population, guest module and original ELF/module. Locale follow-up belongs on existing #3249 / #1578.

Fork integration preserved and revalidated (2026-10-11, 8f2b7210)

The correction is also carried into experimental integration 8f2b7210, branch codex/experimental-ppsa99902-unlocked-io, directly after dff875a7. Only the same three I/O source/test files and the debt entry change; all three I/O files exactly match the focused current-main commit. The integration retains its pinned f60923bf base and pending/local runtime prerequisites. Updating it to ccf9ff90 produced 11 graphics/debt conflicts; that merge was cleanly aborted and careful merge/validation remains pending. The standalone focused branch is based on ccf9ff90.

Windows 11 / RTX 5080, Vulkan required, shader cache disabled: full build and all 122 libraries pass; 823/828 full-suite tests pass, 5 documented display/no-Vulkan skips, 0 failures, max passed test 16.40 s (ctest -j4). Extended stream tests then pass 5/5 again (max 0.19 s). Five fresh-conversion title launches stop at localeconv / 0hlfW1O4Aa4, 0xC0000139, 0.121–0.147 s, no timeout, before guest entry. Every replay restores the exact 123-library baseline/guest module and verifies original hashes. This second profile uses the complete rebuilt experimental runtime; the earlier libc-only profile remains the controlled replacement experiment.

The integration's different base/runtime does not resolve or isolate the six failures on the newer-main focused build: all five corresponding aperture groups pass here, but newer main's agc_driver_f32_compare_constant is not registered in this older integration. Both fork refs and their four changed-file Git blobs are verified from GitHub. No PR opened, no gameplay/console equivalence claim. AI-assisted: Codex.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6107346500 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1765#issuecomment-6107346500). Originally posted by [Amoenus](https://github.com/Amoenus) on 2026-10-11T08:56:51Z. --- ### PPSA99902 validation and unlocked-status correction (2026-10-11, 2e12a9f4) Validated the exact unmerged head `151814a3` on pinned main `ccf9ff90`. This adapts MarcZX100's existing work; no new PR has been opened. Proposed correction/tests are preserved in [our fork commit `2e12a9f4`](https://github.com/Amoenus/AnyPS5/commit/2e12a9f478b1356353d463374c2e7bffb1984a3c). AI-assisted adaptation and investigation: Codex. **Confirmed Linux behavior to correct:** both new functions call `stream->SyncStatus()` after the unlocked byte operation. `SyncStatus` uses `std::feof` and `std::ferror`; glibc can acquire the FILE lock there. See pinned [feof.c](https://github.com/bminor/glibc/blob/d2097651cc57834dbfcaa102ddfacae0d86cfb66/libio/feof.c) and [ferror.c](https://github.com/bminor/glibc/blob/d2097651cc57834dbfcaa102ddfacae0d86cfb66/libio/ferror.c). Actual production `StandardStreams.cpp` + `FileStream.hpp` were compiled into a native probe on Ubuntu 24.04 WSL, x86_64, glibc 2.39. With one thread holding `flockfile` but performing no I/O, an unlocked read/write from another thread did not finish within 1 second; the probe releases the lock and joins both threads before failing. **5/5 reads and 5/5 writes reproduced** `unlocked call waited for another thread's stream lock`. The source link is glibc 2.42; the live probe separately establishes the behavior on installed 2.39. The correction adds `SyncUnlockedStatus`, using `feof_unlocked`/`ferror_unlocked` on POSIX and the observed non-locking UCRT `feof`/`ferror` on Windows, and uses it from both functions. It preserves guest EOF/error bits, encoding-error state, other stream flags, and resets guest read/write macro counters. Native byte I/O still uses the original PR's `getc_unlocked`/`putc_unlocked` and `_fgetc_nolock`/`_fputc_nolock`. | Check | Result | |---|---| | Original PR, Windows UCRT `guest_standard_streams` | 5/5 pass | | Original PR, Windows held-stream-lock native probes | 5 reads + 5 writes pass | | Corrected Windows extended `guest_standard_streams` | 5/5 pass, max 0.27 s | | Corrected native Linux full byte/status/refusal/lock probes | 5/5 pass | | Linux deliberately incorrect variants | All 4 caught: locking status, locking byte operation, stale status/counters, false zero result | | Current-main Windows full build + all configured system libraries | Pass; 122 libraries | | Current-main full Windows suite, `ctest -j12`, RTX 5080, cache disabled | 795 pass, 5 documented display/no-Vulkan skips, **6 GPU failures**; serial recheck reproduces all 6 failures, not a clean-suite claim | | Conventions | Pass | The full-suite failures are `agc_driver_f32_compare_constant` and flat-aperture parts 0, 2, 3, 4, 5; several record device loss or crashes. All six reproduce serially. Their relationship to current upstream GPU changes is unresolved; [GPU observations and failing byte-read controls are recorded on #3291](https://github.com/boykopovar/AnyPS5/issues/3291#issuecomment-6107405942). New I/O tests in [GuestStandardStreams.cpp](https://github.com/Amoenus/AnyPS5/blob/2e12a9f478b1356353d463374c2e7bffb1984a3c/core/libs/tests/GuestStandardStreams.cpp#L100) cover unsigned-byte conversion (including high-bit bytes), EOF/reset, write failure without file modification, guest macro state, encoding-error retention, and both held-stream-lock operations. The Windows standalone exception harness was not accepted as null/closed-stream proof; those refusal probes passed on Linux. No Linux full runtime/GPU build, other CRT or PS5-console equivalence is claimed. **Title validation:** PPSA99902, Sonic Unleashed Recompiled 01.003.000, original ELF SHA-256 `c34f5b0848ef49e96a855076d8254254047e966e4fe56589d0e504fe13828479`. Fresh default conversion uses relinker source exactly matching `f60923bf` and the zero-return fallback protection from merged #2705. Runtime is our previously validated experimental integration `dff875a7`, with pending/local prerequisites; this is not an unmodified-main startup claim. Before: `FAIL: unresolved ELF import H-QeERgWuTM from libSceLibcInternal.prx` (`putc_unlocked`), 0.146 s. After replacing **only `libc.prx`** (other 121 libraries, executable and guest module unchanged): five launches stop at `FAIL: unresolved ELF import 0hlfW1O4Aa4 from libSceLibcInternal.prx` (`localeconv`), 0.112–0.130 s. All exit `0xC0000139`, no timeout, **before guest entry**. An audit of all 122 native libraries confirms `libc.prx` now provides both unlocked NIDs and no native provider provides `localeconv`. This establishes loader progress, not successful guest execution of either I/O function. Each run restored/hash-verified the exact 123-library baseline population, guest module and original ELF/module. Locale follow-up belongs on existing #3249 / #1578. ### Fork integration preserved and revalidated (2026-10-11, 8f2b7210) The correction is also carried into [experimental integration `8f2b7210`](https://github.com/Amoenus/AnyPS5/commit/8f2b7210b1905aaf87751a51452062d135f0ed08), branch `codex/experimental-ppsa99902-unlocked-io`, directly after `dff875a7`. Only the same three I/O source/test files and the debt entry change; all three I/O files exactly match the focused current-main commit. The integration retains its pinned `f60923bf` base and pending/local runtime prerequisites. Updating it to `ccf9ff90` produced 11 graphics/debt conflicts; that merge was cleanly aborted and careful merge/validation remains pending. The standalone focused branch is based on `ccf9ff90`. Windows 11 / RTX 5080, Vulkan required, shader cache disabled: **full build and all 122 libraries pass; 823/828 full-suite tests pass, 5 documented display/no-Vulkan skips, 0 failures, max passed test 16.40 s** (`ctest -j4`). Extended stream tests then pass 5/5 again (max 0.19 s). Five fresh-conversion title launches stop at **`localeconv` / `0hlfW1O4Aa4`**, `0xC0000139`, 0.121–0.147 s, no timeout, before guest entry. Every replay restores the exact 123-library baseline/guest module and verifies original hashes. This second profile uses the complete rebuilt experimental runtime; the earlier libc-only profile remains the controlled replacement experiment. The integration's different base/runtime does **not** resolve or isolate the six failures on the newer-main focused build: all five corresponding aperture groups pass here, but newer main's `agc_driver_f32_compare_constant` is not registered in this older integration. Both fork refs and their four changed-file Git blobs are verified from GitHub. No PR opened, no gameplay/console equivalence claim. AI-assisted: Codex.
MarcZX10 commented 2026-10-11 09:25:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-11T09:25:09Z.


Thanks @babyhuey for the Porpoise import trace, and @Amoenus for the controlled held-lock probes. The probes exposed that the original wrappers could still block: after the unlocked byte operation, SyncStatus() called feof/ferror, which can reacquire the stream lock.

I preserved the original implementation commit and carried the lock-free status correction as a separate credited commit in replacement PR #3375. That PR is based on current main (5ad2cb8) and adds guest coverage for byte values, EOF/error state, and reads/writes while another thread holds the stream lock.

Validation Result
Linux GCC 12.2 Release, current main guest_standard_streams passed 5/5; conventions and diff checks passed
Windows 11 / UCRT, same correction on earlier base ccf9ff9 guest_standard_streams passed 5/5; five held-lock reads and writes passed (reported by @Amoenus)
Porpoise (PPSA99764) Its startup log after #3261 was app.elf: symbol lookup error: app.elf: undefined symbol: H-QeERgWuTM. The report used original head 151814a3 and then stopped at pathconf; it does not validate the corrected head
Windows on current head / PS5 hardware Not tested

Amoenus's separate Sonic Unleashed run reports advancing from putc_unlocked to localeconv after replacing libc.prx; it used a local experimental integration, not an unmodified-main startup.

I tried to reopen #1765 after the new title evidence met the original request, but GitHub keeps its closed head frozen. The original description has been restored, and #3375 is the reviewable current-head replacement.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6107564072 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1765#issuecomment-6107564072). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-11T09:25:09Z. --- Thanks @babyhuey for the Porpoise import trace, and @Amoenus for the controlled held-lock probes. The probes exposed that the original wrappers could still block: after the unlocked byte operation, `SyncStatus()` called `feof`/`ferror`, which can reacquire the stream lock. I preserved the original implementation commit and carried the lock-free status correction as a separate credited commit in replacement PR [#3375](https://github.com/boykopovar/AnyPS5/pull/3375). That PR is based on current `main` (`5ad2cb8`) and adds guest coverage for byte values, EOF/error state, and reads/writes while another thread holds the stream lock. | Validation | Result | | --- | --- | | Linux GCC 12.2 Release, current `main` | `guest_standard_streams` passed 5/5; conventions and diff checks passed | | Windows 11 / UCRT, same correction on earlier base `ccf9ff9` | `guest_standard_streams` passed 5/5; five held-lock reads and writes passed (reported by @Amoenus) | | Porpoise (PPSA99764) | Its startup log after #3261 was `app.elf: symbol lookup error: app.elf: undefined symbol: H-QeERgWuTM`. The report used original head `151814a3` and then stopped at `pathconf`; it does not validate the corrected head | | Windows on current head / PS5 hardware | Not tested | Amoenus's separate Sonic Unleashed run reports advancing from `putc_unlocked` to `localeconv` after replacing `libc.prx`; it used a local experimental integration, not an unmodified-main startup. I tried to reopen #1765 after the new title evidence met the original request, but GitHub keeps its closed head frozen. The original description has been restored, and #3375 is the reviewable current-head replacement.

Pull request closed

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!1765
No description provided.