libkernel: Windows getdirentries stores 0 in basep instead of the read offset (FreeBSD stores it only on success); fix branch ready #5237

Closed
opened 2026-10-13 15:23:30 +00:00 by husseinkad · 2 comments
husseinkad commented 2026-10-13 15:23:30 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-13T15:23:30Z.


The Windows getdirentries / sceKernelGetdirentries stores 0 in *basep on every call. FreeBSD 11.4 kern_getdirentries stores the descriptor offset the read started at, and only when the call succeeds. A guest that uses basep to remember its position in a directory (to resume or seek later) always sees 0 here, and a failed call overwrites it.

Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libkernel-getdirentries-basep (one commit on current main: File/src/Stdio.cpp, tests/GuestDirectoryBuffer.cpp with a new CheckBase, and one sentence in docs/dev/debt/libkernel.md line 106 that would otherwise be false). On Windows basep now holds the entry index before the read, the same value lseek returns and accepts; the Linux path stored it before the host read and now stores it only on success.

Checks:

  • CheckBase fails on main (abort) and passes with the change; guest_directory, guest_directory_buffer and guest_fdopendir pass.
  • Probed through both sceKernelGetdirentries and getdirentries_nid_postfix against main: directories with 0, 1, 5, 50 and 500 entries and 12 buffer sizes from 12 bytes to 64 KiB. Before every call *basep after the call equals the entries consumed so far and lseek(fd, 0, SEEK_CUR) agrees (main had 39 mismatches, the branch none). A NULL basep is safe; basep is untouched on a too-small buffer (EINVAL), a null buffer (EFAULT), a closed fd and a regular file (main wrote 0). After lseek to 0 or 3 basep is 0 or 3; at the end of the directory it reports the total entry count. Two descriptors on one directory each start at 0, and 8 threads on one descriptor read the right total with no crash.
  • Two independent reviews found nothing wrong. check_conventions.py is clean and pr_overlap.py reports only #4999 on the one debt-doc line (a one-line text conflict: #4999's EBADF path leaves basep untouched, which matches this branch, so merge #4999 first or drop the doc hunk).

Not covered: the cursor read and the listing read take separate locks, so two threads reading one descriptor can see a stale basep (noted in the debt entry; FreeBSD serialises with foffset_lock). The Linux change was read, not built or run. An invalid fd returns ENOTDIR rather than EBADF, the same as main (#4999 covers it). Only Windows was built and run.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5833011117 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5237). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-13T15:23:30Z. --- The Windows `getdirentries` / `sceKernelGetdirentries` stores 0 in `*basep` on every call. FreeBSD 11.4 `kern_getdirentries` stores the descriptor offset the read started at, and only when the call succeeds. A guest that uses `basep` to remember its position in a directory (to resume or seek later) always sees 0 here, and a failed call overwrites it. Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libkernel-getdirentries-basep (one commit on current main: `File/src/Stdio.cpp`, `tests/GuestDirectoryBuffer.cpp` with a new `CheckBase`, and one sentence in `docs/dev/debt/libkernel.md` line 106 that would otherwise be false). On Windows `basep` now holds the entry index before the read, the same value `lseek` returns and accepts; the Linux path stored it before the host read and now stores it only on success. Checks: - `CheckBase` fails on main (abort) and passes with the change; `guest_directory`, `guest_directory_buffer` and `guest_fdopendir` pass. - Probed through both `sceKernelGetdirentries` and `getdirentries_nid_postfix` against main: directories with 0, 1, 5, 50 and 500 entries and 12 buffer sizes from 12 bytes to 64 KiB. Before every call `*basep` after the call equals the entries consumed so far and `lseek(fd, 0, SEEK_CUR)` agrees (main had 39 mismatches, the branch none). A NULL `basep` is safe; `basep` is untouched on a too-small buffer (`EINVAL`), a null buffer (`EFAULT`), a closed fd and a regular file (main wrote 0). After `lseek` to 0 or 3 `basep` is 0 or 3; at the end of the directory it reports the total entry count. Two descriptors on one directory each start at 0, and 8 threads on one descriptor read the right total with no crash. - Two independent reviews found nothing wrong. `check_conventions.py` is clean and `pr_overlap.py` reports only #4999 on the one debt-doc line (a one-line text conflict: #4999's `EBADF` path leaves `basep` untouched, which matches this branch, so merge #4999 first or drop the doc hunk). Not covered: the cursor read and the listing read take separate locks, so two threads reading one descriptor can see a stale `basep` (noted in the debt entry; FreeBSD serialises with `foffset_lock`). The Linux change was read, not built or run. An invalid fd returns `ENOTDIR` rather than `EBADF`, the same as main (#4999 covers it). Only Windows was built and run. AI-assisted: yes
MarcZX10 commented 2026-10-13 15:41:05 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T15:41:05Z.


I opened PR #5253, carrying Hussein Kadhim's basep fix with his original commit author preserved. I added a Linux regression check proving a too-small-buffer failure leaves basep untouched; that assertion fails on unchanged main and passes with the fix.

Linux validation: guest_directory_entries passed (1/1), guest_directory and guest_fdopendir passed (2/2), and the convention check passed. The successful-offset regression is Windows-only; the issue report records it passing on Windows. I could not cross-configure the Windows target locally because the MinGW libgcc_s_seh-1.dll runtime is missing. This PR depends on #4999 only for the shared debt-file edit's merge order.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6149323167 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5237#issuecomment-6149323167). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-13T15:41:05Z. --- I opened [PR #5253](https://github.com/boykopovar/AnyPS5/pull/5253), carrying Hussein Kadhim's `basep` fix with his original commit author preserved. I added a Linux regression check proving a too-small-buffer failure leaves `basep` untouched; that assertion fails on unchanged `main` and passes with the fix. Linux validation: `guest_directory_entries` passed (1/1), `guest_directory` and `guest_fdopendir` passed (2/2), and the convention check passed. The successful-offset regression is Windows-only; the issue report records it passing on Windows. I could not cross-configure the Windows target locally because the MinGW `libgcc_s_seh-1.dll` runtime is missing. This PR depends on #4999 only for the shared debt-file edit's merge order.
oneandonlydean commented 2026-10-14 06:54:18 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T06:54:18Z.


Closing: no title is known to reach this, and functions are only changed when a title needs them. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162710567 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5237#issuecomment-6162710567). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T06:54:18Z. --- Closing: no title is known to reach this, and functions are only changed when a title needs them. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.
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#5237
No description provided.