Linux: sceKernelClose(0) closes the host's stdin, the next opened file gets descriptor 0, and a later close(0) closes it mid-read (PPSA13579); fixed by #3295 #4223

Closed
opened 2026-10-12 01:45:23 +00:00 by eliyarson · 2 comments
eliyarson commented 2026-10-12 01:45:23 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by eliyarson on 2026-10-12T01:45:23Z.


Symptom

Blasphemous 2 (PPSA13579) on Linux crashes during asset loading at a different point in each run. With a local fix for its PSNCore start (#4225), 7 runs under gdb ended like this:

  • 4 runs: SIGSEGV in Unity's string reserve/assign.
  • 1 run: "guest address space arena exhausted".
  • 2 runs: survived the 240 s run.

Cause

  • Guest file descriptors are host descriptors. In one traced run, the title called sceKernelClose(0) 102 times: after closing a real file it closes 0 as well (CLOSE fd=7; CLOSE fd=0).
  • The first such call closes the host process's stdin. After that, sceKernelOpen returns descriptor 0 for real files (ScriptingAssemblies.json, unity_builtin_extra, Script_monoscripts.bundle).
  • The next stray sceKernelClose(0) on one thread closes the file another thread is reading. That thread's pread then returns 0 bytes or fails with EBADF:
    PREAD fd=0 n=10055 off=4190027 -> 0
    PREAD fd=0 ... -> -1 errno=9
    
  • Unity parses the unfilled buffer, reads a garbage string length, and the allocation and copy fail. The garbage value differs from run to run, which is why the crash site varies.

Fix

#3295 (by @SynnZer0, closed automatically by the intake pause, not reviewed) makes close and sceKernelClose fail with EBADF for descriptors 0–2 that were open when libkernel loaded, and leaves them open. I cherry-picked it unchanged onto a11c8f7f7 (no conflicts; the diff is identical to the PR head).

Build Runs Result
a11c8f7f7 + PSNCore fix 7 (gdb) 5 crashed (4 SIGSEGV, 1 arena exhausted)
+ #3295 (four variants of the PSNCore fix) 16 × 240 s 0 crashes; all reached the 240 s timeout

Built on Linux (CachyOS, GCC, RTX 3080 Ti). The File test subset passes, including #3295's own guest_host_streams, and so do check_conventions and check_test_registration.

Could #3295 be reopened when intake resumes? The PR's own description names PPSA21703 for the same pattern.

  • #1222 (merged): sceKernelClose returns EBADF for a descriptor that is not open. #3295 extends that to the host's open standard streams.
  • #3840 (open): changes Open.cpp and Stdio.cpp (fcntl/ioctl), the same files #3295 changes, so one of them will need rebasing.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5807186316 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4223). Originally posted by [eliyarson](https://github.com/eliyarson) on 2026-10-12T01:45:23Z. --- ### Symptom Blasphemous 2 (PPSA13579) on Linux crashes during asset loading at a different point in each run. With a local fix for its `PSNCore` start (#4225), 7 runs under gdb ended like this: - 4 runs: SIGSEGV in Unity's string `reserve`/`assign`. - 1 run: "guest address space arena exhausted". - 2 runs: survived the 240 s run. ### Cause - Guest file descriptors are host descriptors. In one traced run, the title called `sceKernelClose(0)` 102 times: after closing a real file it closes 0 as well (`CLOSE fd=7; CLOSE fd=0`). - The first such call closes the host process's stdin. After that, `sceKernelOpen` returns descriptor 0 for real files (`ScriptingAssemblies.json`, `unity_builtin_extra`, `Script_monoscripts.bundle`). - The next stray `sceKernelClose(0)` on one thread closes the file another thread is reading. That thread's `pread` then returns 0 bytes or fails with EBADF: ``` PREAD fd=0 n=10055 off=4190027 -> 0 PREAD fd=0 ... -> -1 errno=9 ``` - Unity parses the unfilled buffer, reads a garbage string length, and the allocation and copy fail. The garbage value differs from run to run, which is why the crash site varies. ### Fix #3295 (by @SynnZer0, closed automatically by the intake pause, not reviewed) makes `close` and `sceKernelClose` fail with EBADF for descriptors 0–2 that were open when libkernel loaded, and leaves them open. I cherry-picked it unchanged onto `a11c8f7f7` (no conflicts; the diff is identical to the PR head). | Build | Runs | Result | |---|---|---| | `a11c8f7f7` + PSNCore fix | 7 (gdb) | 5 crashed (4 SIGSEGV, 1 arena exhausted) | | + #3295 (four variants of the PSNCore fix) | 16 × 240 s | 0 crashes; all reached the 240 s timeout | Built on Linux (CachyOS, GCC, RTX 3080 Ti). The File test subset passes, including #3295's own `guest_host_streams`, and so do `check_conventions` and `check_test_registration`. Could #3295 be reopened when intake resumes? The PR's own description names PPSA21703 for the same pattern. ### Related - #1222 (merged): `sceKernelClose` returns EBADF for a descriptor that is not open. #3295 extends that to the host's open standard streams. - #3840 (open): changes `Open.cpp` and `Stdio.cpp` (fcntl/ioctl), the same files #3295 changes, so one of them will need rebasing.
oneandonlydean commented 2026-10-12 02:47:41 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T02:47:41Z.


Reopened #3295; it still merges cleanly onto main. Your table (5 of 7 runs crash without it, 0 of 16 with it) is the evidence it was missing.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116839661 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4223#issuecomment-6116839661). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-12T02:47:41Z. --- Reopened #3295; it still merges cleanly onto main. Your table (5 of 7 runs crash without it, 0 of 16 with it) is the evidence it was missing.
oneandonlydean commented 2026-10-14 18:45:42 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T18:45:42Z.


Fixed by #3295 (merged): close and sceKernelClose no longer close the host's descriptors 0 to 2. Closing; please reopen if PPSA13579 still loses a descriptor on current main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6175748842 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4223#issuecomment-6175748842). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T18:45:42Z. --- Fixed by #3295 (merged): `close` and `sceKernelClose` no longer close the host's descriptors 0 to 2. Closing; please reopen if PPSA13579 still loses a descriptor on current main.
Sign in to join this conversation.
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#4223
No description provided.