fix(libkernel): flush only the paths the guest changed in sceKernelSync on Windows #1093

Merged
kjhq merged 1 commit from upstream-pr/1093 into main 2026-10-08 08:58:56 +00:00
kjhq commented 2026-10-07 10:39:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kjhq on 2026-10-07T10:39:48Z.


What

On Windows, sceKernelSync opened every writable volume (\\.\Volume{...}) with GENERIC_WRITE and called FlushFileBuffers. Opening a volume that way needs administrator rights, so in a normal user session CreateFileW failed with ERROR_ACCESS_DENIED and sceKernelSync threw. PPSA17221 (Minecraft) calls it and stopped there.

sceKernelSync no longer opens volumes on Windows. Instead:

  • libc keeps a registry of the host files and directories the guest changed (RecordWrittenPath_nid_no_patch). Entries come from:
    • sceKernelOpen with write access, O_CREAT or O_TRUNC
    • fopen/freopen with a w, a or + mode
    • rename (both paths), unlink, remove, mkdir/sceKernelMkdir, sceKernelRmdir, truncate
    • chmod/fchmod and utimes/futimes
    • the files and directories libSceSaveData and libSceAppContent write
  • On Windows, sceKernelSync (SyncWrittenPaths_nid_no_patch) does three things:
    • It flushes each recorded path that was recorded or modified (LastWriteTime) since the previous call.
    • It flushes that path's parent directories, walking up while a directory changed since that call.
    • Each flush opens the path with GENERIC_WRITE and FILE_FLAG_BACKUP_SEMANTICS and calls FlushFileBuffers.
  • A normal user can open those handles. A path that no longer exists is skipped and dropped from the registry.
  • A file that cannot be opened for writing is skipped: ERROR_ACCESS_DENIED (for example a read-only file) or ERROR_SHARING_VIOLATION (another handle denies write sharing). FlushFileBuffers needs a GENERIC_WRITE handle, so these files cannot be flushed. Skipping one only affects durability; the guest reads the same data.
  • Any other error throws.
  • Linux still calls sync() and clears the registry.

TechnicalDebt gets two entries:

  • Only recorded paths are flushed. A host write in a library that does not call RecordWrittenPath_nid_no_patch is not flushed by sceKernelSync on Windows.
  • Files that cannot be opened for writing are skipped.

An earlier revision of this PR skipped volumes that denied access and added a TechnicalDebt entry for it. That entry is removed again.

Test: guest_kernel_sync checks these things:

  • Write opens, fopen("a"), mkdir, rename and sceKernelChmod are recorded.
  • Read-only sceKernelOpen and fopen("r") are not.
  • sceKernelSync runs.
  • An unlinked file is no longer recorded afterwards.
  • sceKernelSync returns after sceKernelChmod(..., 0444) has made a recorded file read-only.
  • On Windows only, sceKernelSync returns while another handle holds a recorded file open with FILE_SHARE_READ.

Which job covers which claim:

  • Linux CI job: on Linux sceKernelSync is sync(), so this job covers only the registry: what is recorded, what is not, and that an unlinked path is dropped after a sync.
  • Windows CI job: covers the flush itself, including the read-only and sharing-violation rows.

Title

PPSA17221 (Minecraft) needs #716 to load the modules in its app root. Every run below is current main + #716 + the PRs of this set (#1088, #1091–#1096), plus local changes outside it:

  • dialog, NpAuth and TextToSpeech2 stubs that open PRs cover (#560, #925, #301)
  • a GPU end-of-pipe change that is not submitted

With all of them the title loads a world. The line below is from a run with only this PR left out, Windows 11, WinLibs GCC, normal user session.

Without it the title terminates about 5 s after start:

terminate called after throwing an instance of 'std::system_error'
  what():  Opening volume for sync: Access is denied

Tested

Windows 11, WinLibs GCC, normal (non-elevated) user session:

  • guest_kernel_sync without the ERROR_ACCESS_DENIED/ERROR_SHARING_VIOLATION skip fails with Opening ...\kernel_sync_probe\read_only.bin for sync: Access is denied. With it the test passed in 3 of 3 runs.
  • Earlier revision of this PR: PPSA17221 with this PR and the rest of the set loads a world and runs for 160 s with no sync error.
  • Earlier revision of this PR: ctest -R "guest_kernel_sync|guest_filesystem|guest_appcontent|savedata_core_write_replacement|savedata_replace_file_failure": 5/5 passed.

Debian 13 (WSL2), GCC 14.2.0, rebased on main e705d430: full ctest, 1 of 426 failed. The failure is tls_function_coverage (alu-exec-xor-3 exits with SIGSEGV), and main e705d430 fails it the same way. guest_kernel_sync passes.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4771431913 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1093). Originally posted by [kjhq](https://github.com/kjhq) on 2026-10-07T10:39:48Z. --- ### What On Windows, `sceKernelSync` opened every writable volume (`\\.\Volume{...}`) with `GENERIC_WRITE` and called `FlushFileBuffers`. Opening a volume that way needs administrator rights, so in a normal user session `CreateFileW` failed with `ERROR_ACCESS_DENIED` and `sceKernelSync` threw. PPSA17221 (Minecraft) calls it and stopped there. `sceKernelSync` no longer opens volumes on Windows. Instead: - libc keeps a registry of the host files and directories the guest changed (`RecordWrittenPath_nid_no_patch`). Entries come from: - `sceKernelOpen` with write access, `O_CREAT` or `O_TRUNC` - `fopen`/`freopen` with a `w`, `a` or `+` mode - rename (both paths), unlink, remove, `mkdir`/`sceKernelMkdir`, `sceKernelRmdir`, truncate - `chmod`/`fchmod` and `utimes`/`futimes` - the files and directories libSceSaveData and libSceAppContent write - On Windows, `sceKernelSync` (`SyncWrittenPaths_nid_no_patch`) does three things: - It flushes each recorded path that was recorded or modified (`LastWriteTime`) since the previous call. - It flushes that path's parent directories, walking up while a directory changed since that call. - Each flush opens the path with `GENERIC_WRITE` and `FILE_FLAG_BACKUP_SEMANTICS` and calls `FlushFileBuffers`. - A normal user can open those handles. A path that no longer exists is skipped and dropped from the registry. - A file that cannot be opened for writing is skipped: `ERROR_ACCESS_DENIED` (for example a read-only file) or `ERROR_SHARING_VIOLATION` (another handle denies write sharing). `FlushFileBuffers` needs a `GENERIC_WRITE` handle, so these files cannot be flushed. Skipping one only affects durability; the guest reads the same data. - Any other error throws. - Linux still calls `sync()` and clears the registry. TechnicalDebt gets two entries: - Only recorded paths are flushed. A host write in a library that does not call `RecordWrittenPath_nid_no_patch` is not flushed by `sceKernelSync` on Windows. - Files that cannot be opened for writing are skipped. An earlier revision of this PR skipped volumes that denied access and added a TechnicalDebt entry for it. That entry is removed again. Test: `guest_kernel_sync` checks these things: - Write opens, `fopen("a")`, `mkdir`, rename and `sceKernelChmod` are recorded. - Read-only `sceKernelOpen` and `fopen("r")` are not. - `sceKernelSync` runs. - An unlinked file is no longer recorded afterwards. - `sceKernelSync` returns after `sceKernelChmod(..., 0444)` has made a recorded file read-only. - On Windows only, `sceKernelSync` returns while another handle holds a recorded file open with `FILE_SHARE_READ`. Which job covers which claim: - **Linux CI job:** on Linux `sceKernelSync` is `sync()`, so this job covers only the registry: what is recorded, what is not, and that an unlinked path is dropped after a sync. - **Windows CI job:** covers the flush itself, including the read-only and sharing-violation rows. ### Title PPSA17221 (Minecraft) needs #716 to load the modules in its app root. Every run below is current `main` + #716 + the PRs of this set (#1088, #1091–#1096), plus local changes outside it: - dialog, NpAuth and TextToSpeech2 stubs that open PRs cover (#560, #925, #301) - a GPU end-of-pipe change that is not submitted With all of them the title loads a world. The line below is from a run with only this PR left out, Windows 11, WinLibs GCC, normal user session. Without it the title terminates about 5 s after start: ``` terminate called after throwing an instance of 'std::system_error' what(): Opening volume for sync: Access is denied ``` ### Tested Windows 11, WinLibs GCC, normal (non-elevated) user session: - `guest_kernel_sync` without the `ERROR_ACCESS_DENIED`/`ERROR_SHARING_VIOLATION` skip fails with `Opening ...\kernel_sync_probe\read_only.bin for sync: Access is denied`. With it the test passed in 3 of 3 runs. - Earlier revision of this PR: PPSA17221 with this PR and the rest of the set loads a world and runs for 160 s with no sync error. - Earlier revision of this PR: `ctest -R "guest_kernel_sync|guest_filesystem|guest_appcontent|savedata_core_write_replacement|savedata_replace_file_failure"`: 5/5 passed. Debian 13 (WSL2), GCC 14.2.0, rebased on main e705d430: full `ctest`, 1 of 426 failed. The failure is `tls_function_coverage` (`alu-exec-xor-3` exits with SIGSEGV), and main e705d430 fails it the same way. `guest_kernel_sync` passes. ### 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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [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] AI-assisted: yes
github-actions[bot] commented 2026-10-07 12:09:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-07T12:09:51Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#1079 core/libs/prx/libc/src/General.cpp L9, L70-71
#1108 core/libs/prx/libkernel/File/src/Open.cpp L118
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6037600359 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1093#issuecomment-6037600359). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-07T12:09:51Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #1079 | `core/libs/prx/libc/src/General.cpp` [L9](https://github.com/boykopovar/AnyPS5/blob/df16c4c256be3c44e03eb9149a6ce5f8e8a038b2/core/libs/prx/libc/src/General.cpp#L9), [L70-71](https://github.com/boykopovar/AnyPS5/blob/df16c4c256be3c44e03eb9149a6ce5f8e8a038b2/core/libs/prx/libc/src/General.cpp#L70-L71) | | | | #1108 | `core/libs/prx/libkernel/File/src/Open.cpp` [L118](https://github.com/boykopovar/AnyPS5/blob/df16c4c256be3c44e03eb9149a6ce5f8e8a038b2/core/libs/prx/libkernel/File/src/Open.cpp#L118) | | |
adriadam10 commented 2026-10-08 06:19:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T06:19:19Z.

Review: CHANGES_REQUESTED


Thanks, the approach (no volume handles, flush what the guest touched) looks right, and Linux/Windows CI and the full ctest on current main are green. A few things to fix:

  • core/libs/prx/libc/src/General.cpp:109 (FlushPath): opening with GENERIC_WRITE fails with ERROR_ACCESS_DENIED on a read-only file, and with a sharing violation if another handle denies write sharing, and both throw out of sceKernelSync. chmod/fchmod are recorded (Stdio.cpp:43), so a guest that makes a file read-only and then syncs hits the same kind of failure this PR fixes. FlushFileBuffers requires GENERIC_WRITE, so a read-only handle is no alternative: skip files that can't be opened for writing (read-only, sharing violation), record that in TechnicalDebt (the flush only affects durability, the guest sees the same data), and add a test row for a read-only file.
  • Semantics change not recorded: only paths that went through RecordWrittenPath_nid_no_patch are flushed now; writers that don't call it (any host-side write in other libs that skips the registry) are no longer covered by sceKernelSync on Windows. Please add a line to docs/dev/TechnicalDebt.md saying so (not at the end of its section).
  • core/libs/tests/GuestKernelSync.cpp: on Linux it only exercises the registry; the flush itself is covered only by the Windows CI job. Fine, but the PR body should say which job covers which claim, and the test should include the read-only case above so Windows pins it.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5452392892 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1093#pullrequestreview-5452392892). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T06:19:19Z. **Review:** CHANGES_REQUESTED --- Thanks, the approach (no volume handles, flush what the guest touched) looks right, and Linux/Windows CI and the full ctest on current main are green. A few things to fix: - `core/libs/prx/libc/src/General.cpp:109` (`FlushPath`): opening with `GENERIC_WRITE` fails with `ERROR_ACCESS_DENIED` on a read-only file, and with a sharing violation if another handle denies write sharing, and both throw out of `sceKernelSync`. `chmod`/`fchmod` are recorded (`Stdio.cpp:43`), so a guest that makes a file read-only and then syncs hits the same kind of failure this PR fixes. `FlushFileBuffers` requires `GENERIC_WRITE`, so a read-only handle is no alternative: skip files that can't be opened for writing (read-only, sharing violation), record that in TechnicalDebt (the flush only affects durability, the guest sees the same data), and add a test row for a read-only file. - Semantics change not recorded: only paths that went through `RecordWrittenPath_nid_no_patch` are flushed now; writers that don't call it (any host-side write in other libs that skips the registry) are no longer covered by `sceKernelSync` on Windows. Please add a line to `docs/dev/TechnicalDebt.md` saying so (not at the end of its section). - `core/libs/tests/GuestKernelSync.cpp`: on Linux it only exercises the registry; the flush itself is covered only by the Windows CI job. Fine, but the PR body should say which job covers which claim, and the test should include the read-only case above so Windows pins it.
kjhq commented 2026-10-08 08:30:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kjhq on 2026-10-08T08:30:47Z.


Thanks for the review. Changes:

  • FlushPath now skips a file whose open for writing fails with ERROR_ACCESS_DENIED (read-only) or ERROR_SHARING_VIOLATION. Other errors still throw.
  • TechnicalDebt has two new lines in Functional, after the sceKernelPread entry. One says only paths recorded through RecordWrittenPath_nid_no_patch are flushed. The other says files that cannot be opened for writing are skipped.
  • guest_kernel_sync makes a recorded file read-only with sceKernelChmod(..., 0444) and syncs. On Windows it also syncs while another handle holds the file open with FILE_SHARE_READ.
  • The PR body now says which job covers which claim. Linux covers only the registry; Windows covers the flush.

Without the skip, the new test fails on Windows in a non-elevated session with Opening ...\\kernel_sync_probe\\read_only.bin for sync: Access is denied. With the skip it passed in 3 of 3 runs. The branch is rebased on main e705d430 and squashed into one commit.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6055940128 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1093#issuecomment-6055940128). Originally posted by [kjhq](https://github.com/kjhq) on 2026-10-08T08:30:47Z. --- Thanks for the review. Changes: - `FlushPath` now skips a file whose open for writing fails with `ERROR_ACCESS_DENIED` (read-only) or `ERROR_SHARING_VIOLATION`. Other errors still throw. - TechnicalDebt has two new lines in Functional, after the `sceKernelPread` entry. One says only paths recorded through `RecordWrittenPath_nid_no_patch` are flushed. The other says files that cannot be opened for writing are skipped. - `guest_kernel_sync` makes a recorded file read-only with `sceKernelChmod(..., 0444)` and syncs. On Windows it also syncs while another handle holds the file open with `FILE_SHARE_READ`. - The PR body now says which job covers which claim. Linux covers only the registry; Windows covers the flush. Without the skip, the new test fails on Windows in a non-elevated session with `Opening ...\\kernel_sync_probe\\read_only.bin for sync: Access is denied`. With the skip it passed in 3 of 3 runs. The branch is rebased on main e705d430 and squashed into one commit.
adriadam10 commented 2026-10-08 08:58:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T08:58:52Z.

Review: APPROVED


Checked: build + full ctest on current main + this PR (427/427), guest_kernel_sync fails to build without the change, and the three points from my review are addressed (skip on ERROR_ACCESS_DENIED/ERROR_SHARING_VIOLATION, the two TechnicalDebt lines, read-only and sharing-violation rows in the test). The Windows flush itself is covered only by the Windows CI job. Merging once CI is green.

Note: #1079 also edits General.cpp and conflicts with this one, so whichever lands second rebases.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5454161536 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1093#pullrequestreview-5454161536). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T08:58:52Z. **Review:** APPROVED --- Checked: build + full ctest on current main + this PR (427/427), `guest_kernel_sync` fails to build without the change, and the three points from my review are addressed (skip on `ERROR_ACCESS_DENIED`/`ERROR_SHARING_VIOLATION`, the two TechnicalDebt lines, read-only and sharing-violation rows in the test). The Windows flush itself is covered only by the Windows CI job. Merging once CI is green. Note: #1079 also edits `General.cpp` and conflicts with this one, so whichever lands second rebases.
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!1093
No description provided.