fix(relinker): replace output files instead of rewriting them in place #5018
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5018"
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 SandstromPL on 2026-10-13T08:29:21Z.
What
On Linux, relinking while the previous build is still running fails with
Cannot open output file: …/game.elf(ETXTBSY), but only after the guest modules have been rewritten in place. The running build's loaded modules change under it, and a process that has one mapped gets SIGBUS when the module gets shorter. The output directory is left with new modules beside the previous executable and registries. Separately, a write that fails part way, for example on a full disk, leaves a truncated output.On Linux and macOS,
FileWriternow writes a regular output to a new file beside it and renames it over the output, assavedata::replace_filedoes. The new file gets a name that does not exist yet:<output>.plus six random characters, created withO_CREAT | O_EXCL. It gets the previous file's permission bits and is written through that descriptor. Open files keep their previous contents, and a failed write leaves the previous file whole. An output is still written in place, as before, in these cases:A relink killed between the write and the rename leaves its temporary file behind. That name is never used again, so later relinks are not affected. (The first revision used
<output>.tmp, and a file left there made every later relink fall back to the in-place write; thanks to CnRJay's review.)Behaviour changes for replaced outputs:
Windows is unchanged. It refuses to replace a running executable or a loaded library as well, so only the partial-write case would improve there, and that needs the rename handling #3660 added for Windows readers.
Tested
Ubuntu 24.04, GCC 13.3, relinker-only build with
BUILD_TESTING=ON: 145/145 tests pass on this branch, and 155/155 with it merged intomain016db923a.FileWriterTests.cpp(Linux) gains four checks, run for both overloads:sleepruns from the output path with a descriptor open on it. The write succeeds, and the path has the new contents. The write runs underumask 077, so the previous permission bits (0770) must be restored exactly. The descriptor still reads the previous contents, the process keeps running, and no temporary file is left.<output>.tmpby an earlier, killed relink. The output is still replaced, and that file is not touched.RLIMIT_FSIZEfails and keeps the previous file, with no temporary file left.<output>.tmpis left alone.With main's
FileWriter, the first two checks fail withCannot open output file, and the third leaves a truncated file. With this PR's first revision, the second check fails withCannot open output file. Without thefchmodthat restores the previous permission bits, the first check fails.End to end, with the test fixtures (a bundled module and
--registry): a copy ofsleepruns from the output path, and another process maps a longer previous guest module. Relinking again:Other output states give the same result as main, re-run with this revision. They are a fresh output (same contents and modes), an existing 0755 output, a symbolic-link output, a writable output in a read-only directory, an
<output>.tmpsymbolic link (its target is left alone), and a leftover<output>.tmpfile.Not covered:
FileWriteras well.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 repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T01:51:05Z.
Review: COMMENTED
Writing to a sibling file and renaming over the output is the right approach for
ETXTBSYand for partial writes. The fallbacks for symlinks and non-regular files are sensible.One case I'd change: the fixed
<output>.tmpname. If a relink is killed betweenwriteFileandmoveOver(Ctrl+C or SIGKILL;catch (...)only covers exceptions), the.tmpis left behind. Every later relink of that output then sees an existing.tmpand silently falls back to the in-place write. That brings back exactly the failure this PR fixes:Cannot open output fileafter the modules have already been rewritten, and SIGBUS for a running process. Nothing tells the user why. A unique name in the same directory avoids that and still never touches an unrelated file. For example,mkstempon<output>.XXXXXX, then writing through that descriptor, or opening a generated name withO_CREAT | O_EXCL. A unique temporary also has the right mode from the start if it is created with the previous permission bits, rather than created and then changed withpermissions().This also overlaps with #4802. Once outputs are replaced by rename on Linux and macOS, a hard link between two outputs no longer makes one overwrite the other: each rename replaces only its own name. That removes most of the reason for #4802's hard-link identity probing on those hosts. #4802's case-insensitive and same-path checks are still useful.
Imported from boykopovar/AnyPS5. Originally posted by SandstromPL on 2026-10-14T02:03:15Z.
Thanks, good catch. Fixed in
26c62cc63: the temporary file now gets a fresh name beside the output (<output>.plus six random characters), created withO_CREAT | O_EXCLand the previous permission bits, written through that descriptor and renamed over the output. A file left at<output>.tmp, or anywhere else, no longer forces the in-place write. A new test leaves<output>.tmpbehind and then replaces a running executable: it fails on the previous revision withCannot open output fileand passes now. The permission check also runs underumask 077now, so the previous bits must be restored exactly. A killed relink can still leave its own temporary file, but that name is never reused, so it doesn't affect later runs.On #4802: agreed. With the rename, two hard-linked outputs no longer write through each other on Linux and macOS. Windows still writes outputs in place, so the identity check still matters there.
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-14T15:46:17Z.
macOS data point.
file_writer_binary/file_writer_textare registered only on Linux (/dev/full,/proc/<pid>/exe), so the new POSIX path had no macOS coverage; I exercised it with a small harness instead.Apple M2 Max, macOS 27.2 (26B5091g), AppleClang, Ninja Release, relinker-only. Head
26c62cc63merged without conflicts intomain84b01f9cd.ctest --parallel 8 --timeout 30156/156 pass.-DCMAKE_OSX_ARCHITECTURES=x86_64, x86_64 Python): 150/156; the 6 failures (pr_overlap_encoding,progress_history,stubgen_stdout_newlines,release_package,linux_fixed_base,guest_module_args) fail the same way on unmodifiedmainon this machine (five are Python 3.9 incompatibilities of the only x86_64 Python here;guest_module_argsis the fixture #5362 fixes).Harness (APFS, links
FileWriter.cppfrommainand from this PR, same driver), output path running as an executable while it is rewritten with a different executable:mainETXTBSYon macOS), rewritten in place0750kept underumask 077RLIMIT_FSIZE= 1 byte, 64 KiB writeFailed to write file, output left truncated to 1 byteFailed to write file, previouspreviouscontents kept, no temporary leftSo on macOS
maindoes not fail like on Linux; it silently rewrites the running file in place, which this PR avoids.Hard links (same on any POSIX host), as listed in the description: an output that is a hard link is now replaced by a new inode, so the other name keeps the old contents (
main: both names see the new contents, link count 2; this PR: link count 1).Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by SandstromPL on 2026-10-14T15:51:47Z.
Thanks for the macOS run and the harness, especially the Rosetta and
RLIMIT_FSIZEcases. The hard-link change is intended; it's listed under "Behaviour changes for replaced outputs" ("An output hard-linked to another name no longer changes that name"). It's what replacing by rename gives: LLVM'sFileOutputBuffer, which LLVM's tools write their outputs through, does the same for regular files (FileOutputBuffer.cpp). Keeping the link would mean rewriting in place, which brings back the LinuxETXTBSYfailure and the in-place rewrite of a running executable you measured on macOS.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-14T16:02:58Z.
You're right, it is listed under "Behaviour changes for replaced outputs"; I missed it. I've corrected my comment above.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-14T17:29:03Z.
Checked the temp-file-and-rename path in
FileWriter, the in-place fallbacks, and the tests: relinker build and ctest are green (129/129 scoped), andfile_writer_binary/file_writer_textfail with the previousFileWriter. The unique temp name addresses CnRJay's review, and jotitadev's macOS run covers the POSIX path. Merging.