Skip to content

fix(relinker): preserve patch inputs across all outputs - #8251

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
imshah-id:fix/relinker-patch-inputs
Nov 4, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
imshah-id:fix/relinker-patch-inputs

Conversation

@imshah-id

@imshah-id imshah-id commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

What

Passing an existing patch configuration as an executable, registry, module, icon or other generated output lets the relinker overwrite that configuration. Only the patch-manifest output previously checked for this collision.

Move patch-file protection into the shared output/input validation, before any output is written. Reuse SameOutputFile so direct paths, hard links and symbolic links are covered. Protect empty configurations too; --no-patches continues to allow overwriting an ignored configuration.

Add a regression covering generated output paths for Linux and Windows formats, verifying that rejection preserves every existing output and input. Include distinct-path success, empty-configuration rejection and disabled-patch controls.

The regression now checks each output kind once per format, distributes direct-path, hard-link and symlink cases across outputs, and retains all three aliases for the primary executable (including the Windows .exe symlink). This reduces relinker invocations from 91 to 37 while retaining all 29 output-kind/format pairs, unchanged-input/output assertions, empty-config rejection and --no-patches controls. Windows hosts without symlink privilege fall back to a direct-path check for that output, and report the missing symlink coverage. Linux-format, Windows-format and control cases have separate CTests with the existing 20-second timeout.

Tested

macOS, Apple Silicon, AppleClang 21.0.0, Release relinker-only build. Rebased onto f2d874c1fdccf781c0fa5d86619bb5dd8894b958; final head 062bee70effe50cada7a80505bfc6721c5c9bad7.

  • Built with -DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON; all 549 CTests passed (87.87 seconds). After the final symlink-privilege fallback adjustment, all three focused CTests passed again.
  • On this Mac, the original regression took 1.14 seconds. Final focused runs: Linux-format 0.22 seconds, Windows-format 0.23 seconds, controls 0.07 seconds (0.54 seconds total including harness overhead). These are macOS measurements, not Windows timings.
  • Injected Windows error 1314 into symlink creation and ran the complete Python regression with the real relinker; all preservation checks passed through the direct-path fallback. This is a simulated error-path check, not Windows execution.
  • python3 tools/check_conventions.py --base upstream/main and git diff --check passed. Graph output remains untracked.
  • The original overwrite regression was reproduced before the production fix. Current upstream's handle-based file identity implementation is included through the rebase.
  • Still unverified: final-head cold Windows CTest timings and actual Windows symlink privileges. A collaborator can run the upstream relinker matrix with gh workflow run build.yml --repo boykopovar/AnyPS5 --ref main -f pr=8251 -f full=false, then report first-run timings for patch_input_preservation_* and any symlink privilege fallback. Fork Actions are enabled, but the existing manual workflow requires an open PR in the same repository and cannot directly validate upstream PR 8251 from the fork.
  • No game, full runtime or GPU testing; this change is limited to the relinker.

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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

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.

Pull request Shared files not compared
PR 5021 core/relinker/CMakeLists.txt
PR 7531 core/relinker/CMakeLists.txt
PR 10861 core/relinker/CMakeLists.txt
PR 12678 core/relinker/CMakeLists.txt
core/relinker/main.cpp

@fogydes fogydes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the diff, CI, and test logic.

Production code (main.cpp): The change consolidates patch-file collision detection into the shared checkOutputInputs lambda. Before this PR, only the manifest output checked for collision with the patch file (via std::filesystem::equivalent). Now, a single check — if (!args.noPatches && std::filesystem::is_regular_file(patchFile) && SameOutputFile(patchFile, path)) — runs for every generated output path (executables, registry, guest modules, desktop entry, manifest). The old 4-line equivalent() check is removed since SameOutputFile already handles direct paths, hard links, and symlinks.

The !args.noPatches guard correctly allows overwriting when patches are disabled (--no-patches), since in that case the file is treated as an ordinary input, not a patch configuration.

Test (test_patch_input_preservation.py): Thorough — tests:

  • Control case: existing patch config is preserved after a successful run (no collision).
  • Every output type × three alias forms (direct path, hard link, symlink): verifies exit code 2, "would overwrite the patch file" in stderr, patch config preserved, all inputs and previous outputs preserved, no .anyps5-output-identity-* temp files left behind.
  • Symlink fallback on Windows when the privilege isn't available.
  • Empty configuration rejection.
  • --no-patches bypass (file is overwritten with ELF output, confirming the guard works).

Both Linux ELF and Windows PE output formats are covered via the windows bool.

CI: conventions, intake, labels all pass. Build/relinker tests not triggered (no relinker build check visible, but the conventions are the gating CI for non-build PRs).

PR body: Complete template, overlap analysis done (6 conflicting PRs checked manually), honest about no Windows or runtime testing.

No blocking issues found. Well-structured consolidation that reduces the collision-check code while covering more output paths.

@wachi35676

Copy link
Copy Markdown
Contributor

Verdict: defect on Windows. patch_input_preservation fails on a Windows host that can create symbolic links, and the patch file is overwritten.

Tested at 029f86717, merged onto main 02fb1ce53. Windows 11 Home 10.0.26300, WinLibs GCC 15.2.0 r7, relinker-only Release. This account can create symlinks, so the test's symlink cases run instead of being skipped with 1314.

AssertionError: (True, 'executable', 'symlink', 'Patches: ...\True-executable-symlink\relinker-patches.toml
Converting input.elf ...

The relinker exits 0, and relinker-patches.toml now starts with MZ. It fails the same way on a serial rerun. Windows ctest 221/222, with that one failure; WSL Ubuntu 24.04 / GCC 13.3 passes 227/227.

The trigger is the .exe name, not --windows. Output path as a symlink to the patch file:

output path with this PR
output.exe with --windows rc 0, patch file overwritten
output.exe without --windows rc 0, patch file overwritten
output.bin with --windows rc 2 Executable output would overwrite the patch file
output.elf rc 2, same
output.exe hard link rc 2, same

The cause is in SameOutputFile, and it's already on main. With WinLibs' libstdc++, std::filesystem::equivalent also compares st_mode, and _wstat64 reports 0777 for any *.exe name. A minimal program built with the same compiler gives equivalent(a.toml, l.exe) = 0 (modes 100666 vs 100777) and equivalent(a.toml, l.bin) = 1. The _WIN32 fallback in SameOutputFile only handles hard links (hard_link_count > 1), so a symlink falls through. main has the same gap for the input: with --windows, output.exe as a symlink to input.elf converts with rc 0 and overwrites input.elf, while output.elf gets Executable output would overwrite the input executable. Comparing weakly_canonical paths, or file IDs from GetFileInformationByHandle, would catch both.

Other results:

  • Negative control: this PR's test_patch_input_preservation.py against main's relinker fails at the first collision case (False, 'executable', 'direct').
  • For the executable, registry and manifest outputs, as a hard link or a symlink, on both targets, everything except the .exe symlink is refused and the patch file stays intact. The secondary executables' Windows outputs (secondary-one.bin.exe) are also .exe names, so they hit the same gap.

AI-assisted: yes (Claude Code); results above were run on this machine.

@ycbuzi

ycbuzi commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Windows run of 029f867 merged onto main 1fd6718 (40bd9f644f, clean), the execution the description lists as not tested. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, BUILD_TESTING=ON, Python 3.14.

  • output_collisions and secondary_output_collisions pass under ctest.
  • patch_input_preservation passes when run directly (python test_patch_input_preservation.py <relinker.exe> with ctest's TMP settings prints Patch input preservation tests passed), but it takes 76 s on this PC, so under ctest it hits the test's TIMEOUT of 20 s and is reported as a timeout, not a failure. Something that lets it fit on Windows, a larger timeout for this one or fewer relink rounds, would make the Windows job clean once it runs these.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@imshah-id the change looks right to me on current main. The Windows symlink case @wachi35676 hit came from std::filesystem::equivalent comparing st_mode. main has since replaced it with a handle-based file identity (Io::SameFile, core/relinker/io/src/FileIdentity.cpp: volume serial and file index from GetFileInformationByHandle), and SameOutputFile now goes through that, so a symlinked output.exe resolves to the patch file's identity. The merge with current main is clean.

One thing left before CI: @ycbuzi measured patch_input_preservation at 76 s on Windows, and the test is registered with TIMEOUT 20, so the Windows job would report a timeout. Could you either cut the number of relink rounds (one relink per output kind and format is enough to prove the check fires before any write) or raise the timeout for this test? Most relinker script tests on main use 20-60 s, so fewer rounds would fit better.

@oneandonlydean oneandonlydean self-assigned this Oct 23, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

@imshah-id The Windows report for head 029f867 puts patch_input_preservation at 76 seconds, while core/relinker/CMakeLists.txt still gives it TIMEOUT 20. Please split or shrink the regression cases and report cold Windows timings on the final head. Current CONTRIBUTING.md requires each test to finish within 30 seconds on CI, including its first run, and asks to split or shrink rather than raise the timeout. Please keep direct-path, hard-link and symlink coverage, unchanged-input/output assertions, and the empty-config and --no-patches controls.

@BKlaasWerkman

Copy link
Copy Markdown
Contributor

A second Windows timing for the same head, as data for the timeout question. Head 029f867 merged into main 990f45e (clean merge). Windows 11 Pro 10.0.26300, Core Ultra 9 185H, WinLibs GCC 15.2.0 r7, relinker-only BUILD_TESTING=ON, Release.

The run was ctest --parallel 4, the first run after a fresh build of the merged tree, with a GPU test job running on the same machine. patch_input_preservation passes in 7.3 s against its TIMEOUT 20 (ctest: Test timeout computed to be: 20). The full suite passes 236 of 236, and none of the other 235 changes result against a run of that main on the same machine.

So the 76 s seems specific to the reporting machine, and this test doesn't need that long on Windows in general. That doesn't remove the margin concern for slower CI runners.

AI-assisted: yes (Claude Code)

@blawesom

Copy link
Copy Markdown
Contributor

Static review plus an independent Linux run (CachyOS, kernel 7.2.10, GCC 16.2.1, Ninja, Release, BUILD_TESTING=ON, Python 3.14). No findings.

What I checked (static)

  • Generalizes the patch-file collision guard checkOutputInputs previously applied only to guest artifacts and executables: now every output path (Executable, Patch manifest, Guest module) is checked against patchFile when patches are enabled (!args.noPatches && std::filesystem::is_regular_file(patchFile)). The former manifest-specific std::filesystem::equivalent(patchFile, manifestPath) special case is correctly deleted — subsumed by the general check.
  • SameOutputFile resolves by identity, so the catch is not limited to one spelling of the path (soft/hard/symbolic link collision forms are covered).
  • The Python matrix (test_patch_input_preservation.py) is genuinely two-dimensional (Windows False|True × output-slot × direct/hardlink/symlink alias) and traps partial-write risk: assert config.read_bytes() == text post-error (patch inputs untouched even when the run fails), assert not list(case.rglob(".anyps5-output-identity-*")) (no stale identity artifacts), assert not outputs["executable"].exists() for the empty-config case, and assert path.read_bytes() == data for every input. None of those can be satisfied by no-throw alone. Reuses prepare_secondary/patch_text fixtures from the neighbouring collision tests instead of duplicating setup.

Focused test run

tree result
this head 029f86717e 1/1 patch_input_preservation Passed (0.21s)

Not covered (honest)

This test is exercised on Linux here only. Windows/macOS alias variants (hardlink/symlink privilege handling on Windows, hardlink semantics on macOS) are in the matrix but were not run on those hosts in this triage environment; the Windows symlink case self-skips with a message when privilege is unavailable.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@imshah-id Thanks to @BKlaasWerkman for the fresh-build Windows timing: 7.3 seconds under the existing 20-second CTest timeout is useful evidence that the test does not time out on every Windows host. The earlier 76-second direct run on another Windows host is still unexplained, and the branch has not changed since the request to split or shrink the regression. Please reduce the repeated work while keeping direct-path, hard-link and symlink preservation checks, plus empty-config and --no-patches controls, then report the final-head cold Windows CTest time. A larger timeout would not address the current under-30-second test rule. The Linux pass is helpful correctness evidence, but does not settle Windows duration or symlink-privilege coverage.

@imshah-id
imshah-id force-pushed the fix/relinker-patch-inputs branch from 029f867 to 062bee7 Compare November 1, 2026 15:25
@oneandonlydean
oneandonlydean merged commit ba61f41 into boykopovar:main Nov 4, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants