fix(libkernel): serialize Windows ftruncate with file transfers #6302
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6302"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-15T10:22:36Z.
What
Concurrent positioned reads and
ftruncatecan permanently overwrite existing file data on Windows. The resize path calls_chsize_soutside the position lock used by the read's save/transfer/restore sequence, although both operate on the same native file pointer. In the new regression, every resize requests either 8 MiB or 8 MiB + 4096, yet an independent host read after both descriptors are closed finds 8,355,051 changed bytes inside the protected original 8 MiB.Hold the existing
File::LockPositionedTransferwhile resizing an ordinary Windows file. The lock is already shared across duplicated descriptors and used by positioned transfers, scalar transfers and seeks. The shared-memory resize path returns before this lock is acquired, preserving its existing locking order.guest_truncate_raceexercises kernel APIs on one descriptor and POSIX APIs through adupalias. Each case performs four rounds, with 32 full-prefix reads and 256 alternating resizes per round. It checks transfer results, read contents, both aliases' cursor, the following ordinary read, final file size and the persisted prefix throughstd::ifstreamafter close. Descriptor lifetime stays outside the race. The test has a 20-second timeout.Tested
I tested on Windows 11 Pro 10.0.26300, WinLibs MinGW-w64 GCC 15.2.0 r7, Release/Ninja, with
BUILD_TESTING=ON, outside the sandbox.Hardware: AMD Ryzen 9 7900X, 32 GB RAM, NVIDIA GeForce RTX 4070 Ti SUPER.
The following are my captured runs of the same registered regression against main
a08de6827f217115078660b9f47201edae59668band this fix. I subsequently rebased onto maine49ec591dc93c9e4dfaad8b6dbc0b995dd7830a0; all libkernel/libc and regression Git blobs are identical to the measured fixed tree. Final head:b062366e5022798c256ae02377588e5b82807557.Each cell lists kernel/direct; POSIX/dup. Both runs attempt 128 reads and 1,024 resizes per case. These are correctness observations, not performance measurements.
I checked the complete file inventories of 576 open PRs on the final refresh, including pinned-head diffs for all 29 sharing
Stdio.cpp. The 19 older AOT diffs are identical in this file and change other platform paths. I compared the implementations, tests and discussions of #5980 (invalid descriptors), #5938 (close lifetime), #5833 (whole-vector serialization), #5831 (kernel append), #6287 (stream append), #6228 (directory identity) and #6234 (GPU write coherence). None adds ordinary-file resize serialization. #5835's archive rejection is in the public wrapper and does not implement this fix.The official overlap check flags one adjacent insertion with my #6228. This change inserts the position lock after the shared-memory return, while #6228 inserts the directory-descriptor rejection there. Both lines must remain when the second PR is rebased: keep the position lock before native handle access and retain the directory check. The fixes are independent; neither needs the other to function. The checker reports no other overlap.
Limits: the existing
guest_kernel_syssymbolic-link success branches reported that links were unavailable and were not covered. No retail title, Linux runtime, console or GPU execution was tested for this change. Full hosted Windows/Linux builds require collaborator dispatch underCONTRIBUTING.md; skipped jobs are not validation passes.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repositoryImported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:18:21Z.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
core/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/File/src/Stdio.cppcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/CMakeLists.txtcore/libs/prx/libkernel/File/src/Stdio.cppImported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-17T12:07:30Z.
Windows run of head
b062366e5, merged ontomain673f7406ctogether with #6311 (no shared file; both merges clean).libspass, with no warnings inStdio.cpporGuestTruncateRace.cpp.--repeat until-fail:3:guest_truncate_raceandguest_truncatepass 3/3. I also ran the test executable directly 10 times (0.7-1.3 s each) and it passed 10/10.Stdio.cppfrommainand the test kept,guest_truncate_racefails 3 of 3. The first run printedkernel-direct ... pread_count_failures=32 pread_data_failures=96 cursor_failures=3 read_failures=4 ... wrong_bytes=8355051andposix-dup ... pread_count_failures=31 pread_data_failures=96 cursor_failures=2 read_failures=4 wrong_bytes=8355051.wrong_bytes=8355051matches the number in the description. With the source restored it passes again.ctest -j8 --timeout 120 -E '^agc_driver_|^video_out_flip'): 865 of 878 pass and 2 are skipped. Eleven tests failed in the first pass (sixconventions_function_bodies*,hardware_oracle_compare,guest_none_relocations,tls_function_coverage_register_address_loadsandwindows_diagnostic_fixture_isolationtimed out, andguest_selectaborted with 0xc0000409 while the host was loaded). All eleven pass on serial reruns. I did not run the suite on plainmain.tools/check_conventions.py --base 673f7406candgit diff --checkpass. There are no new exports.Titles. In the relinked titles I have,
ftruncateis imported by 28 (sceKernelFtruncateby PPSA28319). I ran six of them for 45 s with a local diagnosticlibkernelthat logs the Windowsftruncatecalls: PPSA99169, PPSA85300, PPSA99109, PPSA99997, PPSA99039 and PPSA99920 made no call at all, so I cannot show a title reaching the changed path. PPSA99169 RetroArch for 60 s, three runs onmainand two on this stack: no stop line on any run. Four of the five were killed at 60 s with 3311, 4596 (main) and 5444, 3135 (this PR) frames. The fifth, amainrun, ended at 34 s after the picker loggedVkResult is ERROR_DEVICE_LOSTwhile the host was busy with other work. Frame counts vary too much between runs of the same build to compare.Notes on the diff, none blocking:
NativeFtruncatelines and conflicts with this PR inStdio.cpp(I checked withgit merge-tree); both lines must stay when the second one is rebased.NativeTruncate(the path-basedtruncate) now takes the lock of its private descriptor, which does not serialize against guest descriptors and does no harm.Not checked: Linux, macOS, and any title that resizes a file while reading it.
Written with Claude Code assistance (validation by a subagent, reviewed in this session).