fix(libkernel): fail open of a file named with a trailing slash on Windows #5266

Closed
DavidSamir wants to merge 2 commits from upstream-pr/5266 into main
DavidSamir commented 2026-10-13 16:00:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-13T16:00:35Z.


What

On Windows, sceKernelOpen, open and creat accept a regular file named with a trailing slash. open("file/", O_WRONLY | O_TRUNC) and creat("file/") empty the file, and open("missing/", O_CREAT) creates a file called missing. On FreeBSD a name with a trailing slash must be a directory: lookup fails with ENOTDIR when the path ends in a slash and the result is not a directory, and when the name does not exist and is being created it fails with ENOENT ("trailing slash only allowed for directories"). The Linux build fails in all of these cases too and leaves the file alone. The same happens for file//, file/. and file/./.

Cause. OpenNative passes the resolved path, which keeps the trailing separator, to CreateFileW, and the Win32 open accepts a separator after a file's name: on this machine CreateFileW(L"c/", ...) on a regular file c opens it. file/. is the same path after the resolver normalizes it. This is the open-side twin of #5164 / #5165, which is about unlink.

Observed on main a252fcd75, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release. file is a regular file of ten bytes and missing does not exist:

main on Windows this PR Linux build
sceKernelOpen("file/", O_RDONLY), also O_WRONLY, O_RDWR, O_APPEND descriptor 0x80020014 (ENOTDIR) 0x80020014
sceKernelOpen("file/", O_WRONLY|O_TRUNC) descriptor, file emptied 0x80020014, file kept 0x80020014, file kept
sceKernelOpen("file/", O_CREAT|O_TRUNC), creat("file/") descriptor, file emptied ENOTDIR, file kept EISDIR, file kept
sceKernelOpen("file/", O_CREAT|O_EXCL) 0x80020011 (EEXIST) 0x80020014 0x80020015 (EISDIR)
sceKernelOpen("missing/", O_WRONLY|O_CREAT), creat("missing/") descriptor, missing created ENOENT, nothing created EISDIR, nothing created
sceKernelOpen("dir/", O_RDONLY) descriptor descriptor descriptor

Where FreeBSD and Linux give different errors (the O_CREAT rows), this follows the FreeBSD lookup cited above.

I have not found a title that stops on this. It came out of running one probe of the path functions on the Windows build and on the Linux build and comparing the two outputs.

Two commits:

  1. fix(libkernel): fail open of a file named with a trailing slash on Windows: Open.cpp.
  2. test(libkernel): open names that end in a slash: guest_filesystem.

Windows only, in OpenNative, which sceKernelOpen, open, creat and openat reach. When the resolved path ends in a separator, the name without it is looked up before the host open: if it exists and is not a directory the call returns ENOTDIR; if it does not exist and O_CREAT is set it returns ENOENT. Nothing is opened, truncated or created in those cases. The check runs only for paths that end in a separator, so an ordinary open makes the same host calls as before, and a directory named with a trailing slash opens as it does on main.

Not changed: fopen opens its file itself in FileAndHeap.cpp and still accepts file/ (#4822 is changing its error handling); rename, truncate, utimes and lstat also accept file/; and a path that runs through a regular file (file/x) still gives ENOENT where FreeBSD gives ENOTDIR. The check acts on the path the resolver returns; #4681 is about whether paths under a mount alias keep their trailing slash there.

guest_filesystem gains checks next to the existing creat ones: for a file named with /, //, /. and /./, sceKernelOpen with O_RDONLY and with O_WRONLY|O_TRUNC must return 0x80020014, open must fail with errno 20, sceKernelOpen with O_CREAT|O_TRUNC and creat must fail, and the file must keep its size; sceKernelOpen with O_CREAT and creat on a missing name with a slash must fail and create nothing; and a directory with a trailing slash must open.

Tested

Windows (b5abbc1f0 on main a252fcd75, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds):

  • With Open.cpp as on main and the test kept, guest_filesystem fails (exit 0xc0000409, Filesystem check failed at line 717). With the fix it passes 5/5 (--repeat until-fail:5), and guest_kernel_fcntl, guest_truncate, guest_dup, guest_file_vector, file_streams, guest_standard_streams, guest_posix_filesystem, guest_fdopendir, guest_kernel_rename and guest_path_case pass 3/3.
  • The probe behind the table, 156 lines, with main's libraries, the branch's and on the Linux build: twelve opens (sceKernelOpen with O_RDONLY, O_WRONLY, O_RDWR, O_WRONLY|O_TRUNC, O_WRONLY|O_APPEND, O_WRONLY|O_CREAT, O_CREAT|O_TRUNC, O_CREAT|O_EXCL and O_DIRECTORY; open with O_RDONLY and with O_WRONLY|O_CREAT|O_TRUNC; creat) on each of 13 names (a file, a directory and a missing name, each plain, with / and with /., plus a file in a subdirectory and a file reached through ..), printing the result, the file's contents and whether the missing name now exists. With main's libraries 30 of the opens on a slash-ended name empty the file or create the missing one; with the branch's none do. The 60 lines for plain names and for directories are identical with both sets. Against the Linux build the branch still differs in two ways: sceKernelOpen returns its error without setting errno, as on main, and the O_CREAT rows of the table.
  • Probes of 122 lines over the descriptor functions, 866 over streams, 188 over the file, directory, vector and mapping functions, 431 over time, number, character and string functions, 145 over threads, 95 and 54 over flock, 44 over pipe streams, 15 over fcntl status flags, 127 over freopen, 130 over directory descriptors, 117 over streams used the other way, 144 over truncating opens, 164 over unlink, 313 over fdopen and 198 over paths that run through a regular file, run with both sets of libraries: the only lines that differ are two in the first probe and twelve in the last that open a file named with / or /., and a file size in the fdopen probe that changes from run to run with either set.
  • Timing of open and close, 20,000 rounds per pass, three alternating runs: a file 80 to 93 µs with main's libraries and 77 to 78 µs with the branch's; a directory 81 to 98 and 77 to 85; a directory named with a trailing slash, the one case that makes the extra lookup, 85 to 99 and 86 to 96; a missing name 339 to 348 and 279 to 307. No slowdown I can separate from the spread.
  • Titles from the homebrew catalog, 35 s each with both sets of libraries, windows kept off-screen: ccleste (PPSA59322), RommPS (PPSA76677), SwanStationPS5 (PPSA98510), ProsperoTV (PPSA99003), DOOM (PPSA99666), ScreenTester5 (PPSA69420), Prospero Vibrate (PPSA99920), SymphonyStation5 (PPSA17641), ProsperoStore (PPSA99000), OpenNOW (PPSA99082), ProsperoRadio (PPSA99001) and PSXS5 (PPSA97510) are all still running at 35 s with both, with no what() in either log and the same last kind of log line; SwanStationPS5 and PSXS5, whose logs are the longest, were run four times with each set (50 or 51 and 74 to 79 stderr lines, the spread being periodic statistics lines).
  • Full ctest --parallel 4: 1074 of 1074 pass, as with main a252fcd75 built without this change.
  • Commit 1 alone (b30931231): full build, and guest_filesystem, guest_kernel_fcntl, guest_truncate, guest_posix_filesystem and file_streams pass 3/3.
  • tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py --branch reports no overlap with open pull requests.

Linux (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release): libs builds, and guest_filesystem and guest_kernel_fcntl pass 3/3, so the new checks hold on the Linux build as it is. The changed code is Windows only.

Not checked on a console or with a commercial title.

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
  • Unblocks: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4845963482 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5266). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-13T16:00:35Z. --- ### What On Windows, `sceKernelOpen`, `open` and `creat` accept a regular file named with a trailing slash. `open("file/", O_WRONLY | O_TRUNC)` and `creat("file/")` empty the file, and `open("missing/", O_CREAT)` creates a file called `missing`. On FreeBSD a name with a trailing slash must be a directory: [`lookup`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/kern/vfs_lookup.c#L1068-L1075) fails with `ENOTDIR` when the path ends in a slash and the result is not a directory, and when the name does not exist and is being created it fails with [`ENOENT`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/kern/vfs_lookup.c#L947-L952) ("trailing slash only allowed for directories"). The Linux build fails in all of these cases too and leaves the file alone. The same happens for `file//`, `file/.` and `file/./`. **Cause.** [`OpenNative`](https://github.com/boykopovar/AnyPS5/blob/a252fcd75/core/libs/prx/libkernel/File/src/Open.cpp#L164-L200) passes the resolved path, which keeps the trailing separator, to `CreateFileW`, and the Win32 open accepts a separator after a file's name: on this machine `CreateFileW(L"c/", ...)` on a regular file `c` opens it. `file/.` is the same path after the resolver normalizes it. This is the open-side twin of #5164 / #5165, which is about `unlink`. Observed on `main` a252fcd75, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release. `file` is a regular file of ten bytes and `missing` does not exist: | | `main` on Windows | this PR | Linux build | |---|---|---|---| | `sceKernelOpen("file/", O_RDONLY)`, also `O_WRONLY`, `O_RDWR`, `O_APPEND` | descriptor | `0x80020014` (ENOTDIR) | `0x80020014` | | `sceKernelOpen("file/", O_WRONLY\|O_TRUNC)` | descriptor, file emptied | `0x80020014`, file kept | `0x80020014`, file kept | | `sceKernelOpen("file/", O_CREAT\|O_TRUNC)`, `creat("file/")` | descriptor, file emptied | ENOTDIR, file kept | EISDIR, file kept | | `sceKernelOpen("file/", O_CREAT\|O_EXCL)` | `0x80020011` (EEXIST) | `0x80020014` | `0x80020015` (EISDIR) | | `sceKernelOpen("missing/", O_WRONLY\|O_CREAT)`, `creat("missing/")` | descriptor, `missing` created | ENOENT, nothing created | EISDIR, nothing created | | `sceKernelOpen("dir/", O_RDONLY)` | descriptor | descriptor | descriptor | Where FreeBSD and Linux give different errors (the `O_CREAT` rows), this follows the FreeBSD lookup cited above. I have not found a title that stops on this. It came out of running one probe of the path functions on the Windows build and on the Linux build and comparing the two outputs. Two commits: 1. `fix(libkernel): fail open of a file named with a trailing slash on Windows`: `Open.cpp`. 2. `test(libkernel): open names that end in a slash`: `guest_filesystem`. Windows only, in `OpenNative`, which `sceKernelOpen`, `open`, `creat` and `openat` reach. When the resolved path ends in a separator, the name without it is looked up before the host open: if it exists and is not a directory the call returns `ENOTDIR`; if it does not exist and `O_CREAT` is set it returns `ENOENT`. Nothing is opened, truncated or created in those cases. The check runs only for paths that end in a separator, so an ordinary open makes the same host calls as before, and a directory named with a trailing slash opens as it does on `main`. Not changed: `fopen` opens its file itself in `FileAndHeap.cpp` and still accepts `file/` (#4822 is changing its error handling); `rename`, `truncate`, `utimes` and `lstat` also accept `file/`; and a path that runs through a regular file (`file/x`) still gives `ENOENT` where FreeBSD gives `ENOTDIR`. The check acts on the path the resolver returns; #4681 is about whether paths under a mount alias keep their trailing slash there. `guest_filesystem` gains checks next to the existing `creat` ones: for a file named with `/`, `//`, `/.` and `/./`, `sceKernelOpen` with `O_RDONLY` and with `O_WRONLY|O_TRUNC` must return `0x80020014`, `open` must fail with errno 20, `sceKernelOpen` with `O_CREAT|O_TRUNC` and `creat` must fail, and the file must keep its size; `sceKernelOpen` with `O_CREAT` and `creat` on a missing name with a slash must fail and create nothing; and a directory with a trailing slash must open. ### Tested Windows (b5abbc1f0 on `main` a252fcd75, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds): - With `Open.cpp` as on `main` and the test kept, `guest_filesystem` fails (exit 0xc0000409, `Filesystem check failed at line 717`). With the fix it passes 5/5 (`--repeat until-fail:5`), and `guest_kernel_fcntl`, `guest_truncate`, `guest_dup`, `guest_file_vector`, `file_streams`, `guest_standard_streams`, `guest_posix_filesystem`, `guest_fdopendir`, `guest_kernel_rename` and `guest_path_case` pass 3/3. - The probe behind the table, 156 lines, with `main`'s libraries, the branch's and on the Linux build: twelve opens (`sceKernelOpen` with `O_RDONLY`, `O_WRONLY`, `O_RDWR`, `O_WRONLY|O_TRUNC`, `O_WRONLY|O_APPEND`, `O_WRONLY|O_CREAT`, `O_CREAT|O_TRUNC`, `O_CREAT|O_EXCL` and `O_DIRECTORY`; `open` with `O_RDONLY` and with `O_WRONLY|O_CREAT|O_TRUNC`; `creat`) on each of 13 names (a file, a directory and a missing name, each plain, with `/` and with `/.`, plus a file in a subdirectory and a file reached through `..`), printing the result, the file's contents and whether the missing name now exists. With `main`'s libraries 30 of the opens on a slash-ended name empty the file or create the missing one; with the branch's none do. The 60 lines for plain names and for directories are identical with both sets. Against the Linux build the branch still differs in two ways: `sceKernelOpen` returns its error without setting errno, as on `main`, and the `O_CREAT` rows of the table. - Probes of 122 lines over the descriptor functions, 866 over streams, 188 over the file, directory, vector and mapping functions, 431 over time, number, character and string functions, 145 over threads, 95 and 54 over `flock`, 44 over pipe streams, 15 over `fcntl` status flags, 127 over `freopen`, 130 over directory descriptors, 117 over streams used the other way, 144 over truncating opens, 164 over `unlink`, 313 over `fdopen` and 198 over paths that run through a regular file, run with both sets of libraries: the only lines that differ are two in the first probe and twelve in the last that open a file named with `/` or `/.`, and a file size in the `fdopen` probe that changes from run to run with either set. - Timing of open and close, 20,000 rounds per pass, three alternating runs: a file 80 to 93 µs with `main`'s libraries and 77 to 78 µs with the branch's; a directory 81 to 98 and 77 to 85; a directory named with a trailing slash, the one case that makes the extra lookup, 85 to 99 and 86 to 96; a missing name 339 to 348 and 279 to 307. No slowdown I can separate from the spread. - Titles from the homebrew catalog, 35 s each with both sets of libraries, windows kept off-screen: ccleste (PPSA59322), RommPS (PPSA76677), SwanStationPS5 (PPSA98510), ProsperoTV (PPSA99003), DOOM (PPSA99666), ScreenTester5 (PPSA69420), Prospero Vibrate (PPSA99920), SymphonyStation5 (PPSA17641), ProsperoStore (PPSA99000), OpenNOW (PPSA99082), ProsperoRadio (PPSA99001) and PSXS5 (PPSA97510) are all still running at 35 s with both, with no `what()` in either log and the same last kind of log line; SwanStationPS5 and PSXS5, whose logs are the longest, were run four times with each set (50 or 51 and 74 to 79 stderr lines, the spread being periodic statistics lines). - Full `ctest --parallel 4`: 1074 of 1074 pass, as with `main` a252fcd75 built without this change. - Commit 1 alone (b30931231): full build, and `guest_filesystem`, `guest_kernel_fcntl`, `guest_truncate`, `guest_posix_filesystem` and `file_streams` pass 3/3. - `tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py --branch` reports no overlap with open pull requests. Linux (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release): `libs` builds, and `guest_filesystem` and `guest_kernel_fcntl` pass 3/3, so the new checks hold on the Linux build as it is. The changed code is Windows only. Not checked on a console or with a commercial title. ### Checklist - [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] Unblocks: none - [x] AI-assisted: yes
oneandonlydean commented 2026-10-14 04:42:02 +00:00 (Migrated from github.com)

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


Closing: no title is known to reach this. With the queue at 480+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6161056661 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5266#issuecomment-6161056661). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T04:42:02Z. --- Closing: no title is known to reach this. With the queue at 480+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

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