Skip to content

fix(relinker): write registries after the executable - #4346

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
SandstromPL:fix/relinker-registry-after-patch
Oct 12, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
SandstromPL:fix/relinker-registry-after-patch

Conversation

@SandstromPL

@SandstromPL SandstromPL commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

What

With --registry, main.cpp writes <stem>.registry.json, and one <stem>.<module>.registry.json per converted bundled module, before Patch. Patch can still fail. With --windows, two inputs make it fail:

  • an sce_sys/icon0.png that fails the Windows icon check (Invalid Windows icon PNG);
  • a run path with a variable Windows does not support, such as --rpath '${ORIGIN}/libs'.

The relink exits with an error, but by then the registries describe the new input. The previous executable and app0/ are still beside them, and a module registry exists for a module that was never converted. These are the files tools/import_audit.py reads (USAGE.md, "pass all of them"), so the audit then reports on a binary that is not there.

The registry paths and their collision checks stay where they were, before Patch. Collisions therefore still fail before any output is written (USAGE.md:38). The files themselves are now written after the executable, and before the desktop entry. This follows the rule of ca330dfef: a malformed icon0.png leaves no output behind.

Registry contents do not change. They come from RegistryEntries and the modules' imports, which Patch does not receive.

One side effect: --registry into an output directory that does not exist yet now works when there are guest modules, the same as without --registry. The guest module writes create the directories. Without modules it still fails, but the error now names the executable instead of the registry.

Tested

Ubuntu 24.04, GCC 13.3, relinker-only build: 127/127 tests pass.

Not covered: a failure while writing a registry itself, after the new executable is written. Any write order without temporary files leaves that case.

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 (Claude Code); I reviewed and tested every line

Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@Dextroid17 Dextroid17 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.

Verified on the PR head, with the implementation isolated. Head 70f03dc5e, base 2ca6198e9 — its parent (70f03dc5e^) and merge-base. upstream/main has since moved to 57dc13602, which changes a different region of core/relinker/main.cpp (c763a78b6, the --to-intel substitution block around line 74); git merge-tree --write-tree 57dc13602 70f03dc5e exits 0 with no conflict, and the test file is untouched upstream.

The suite on the head. Relinker-only build (GCC 16.2.1, Release): 100% tests passed out of 127, input_preservation among them.

The added check bites. With core/relinker/main.cpp at 2ca6198e9 and the PR's tests kept:

File "…/test_input_preservation.py", line 75, in check_registry_after_failed_patch
    assert all(registry.read_bytes() == previous_registry for registry in registries)
AssertionError
exit=1

The move shows up by line position, not by presence: fileWriter.Write(Io::Utf8Path(registryPaths.front()), …WriteCallRegistry(…)) sits at line 140 in the base, before the executable write at 176, and at line 174 in the head, after the executable write at 171. Restoring the head file reproduces the PR blob byte-for-byte (d557614c) and the test passes again (exit 0). On the reverted arm the suite reads 99% tests passed, 1 tests failed out of 127 — input_preservation only, so nothing else in the tree asserts where the registries are written.

End to end, both triggers the body names. Fixture from the PR's own test helpers (main_fixture(), guest_fixture(PLAIN_SITE)), with a previous output.exe and previous registries planted:

invalid sce_sys/icon0.png, --windows --registry
  base  exit 2  "Invalid Windows icon PNG …"   output.exe unchanged   registry -> "[\n]\n"
  PR    exit 2  same error                     output.exe unchanged   registry -> "previous registry"

--rpath '${ORIGIN}/libs' (icon valid, so the run-path check is what fires)
  base  exit 2  "Unsupported variable in Windows run path"            registry -> "[\n]\n"
  PR    exit 2  same error                                            registry -> "previous registry"

The body's third clause holds too: with only the call registry planted and the module registry absent, the base creates output.sample.prx.guest.prx.registry.json — a registry for a module that was never converted — and the head does not.

Two behaviour changes, both real. With a valid icon, --registry, and an output path inside a directory that does not exist yet:

base  exit 2  "Cannot open output file: …/newdir/out.registry.json"     nothing created
PR    exit 0  stderr empty   newdir/ -> out.exe, out.registry.json, out.sample.prx.guest.prx.registry.json,
                                       app0/sce_module/sample.prx.guest.prx

The guest module lands under newdir/app0/sce_module/, and this reproduces on a plain Linux run, so it is not a --windows effect: the guest-module writes' create_directories now runs before the first writer that touches the output directory (the base's registry write came first, and FileWriter does not create directories). With --skip-sce-module into a missing directory, the failure line now names the executable instead of the registry: base Cannot open output file: …/newdir/out.registry.json, head …/newdir/out.elf — exit 2 and nothing created in both.

One window the move opens, which I did not find in the body. Registries written after the executable means a registry write that fails now leaves the executable already replaced — the opposite direction to the state this PR removes. Demonstrated on the head, with the executable and registries present and output.registry.json made read-only (0444), and again with the registry path occupied by a directory:

head  exit 2  "Cannot open output file: …/output.registry.json"
      output.elf is now the new ELF (7f 45 4c 46), app0/sce_module/sample.prx.guest.prx written,
      while the registries are still the previous ones
base  exit 2  same message, but output.elf is still the previous bytes and no app0/ exists

So exit 2 no longer implies "the executable was left alone" for a post-Patch failure. The Patch-failure case this PR fixes is much the likelier one, which is presumably why the order was chosen, but the trade-off is worth a line in the body — and the added test covers Patch failures only, not a registry write failing after one. An order that closes both would write the registries after Patch and before the executable, leaving a fresh-registry/old-executable window only if the executable write itself fails.

The collision checks still precede the write. registryPaths is built at the same place with the same expressions under the same args.writeRegistry guard, the checkOutputInputs(path, "Registry") loop is still before Patch (main.cpp:137 vs :161), and the paths are unchanged — so docs/user/USAGE.md:38's "These collisions fail before any output is written" still holds.

What I could not verify. A Windows host: every row above is a PE image produced by a Linux build, never executed on Windows. And the body's consequence claim that tools/import_audit.py "reports on a binary that is not there" — import_audit takes only registry JSONs plus --libs/--modules and no executable argument, so a stale registry is a stale input describing an older build; I could not run it here either (the relinker-only tree has no built core/libs).

No merge rights on my side — this is a comment, not an approval.

Written with AI assistance.

Hermes Agent 🤖 at your service

@oneandonlydean oneandonlydean self-assigned this Oct 12, 2026
@oneandonlydean
oneandonlydean merged commit d3187df into boykopovar:main Oct 12, 2026
15 checks passed
@SandstromPL

Copy link
Copy Markdown
Contributor Author

Thanks for the review, and for checking that the new test fails without the change.

The registry-write window is in the body, at the end of Tested: "Not covered: a failure while writing a registry itself, after the new executable is written." FileWriter writes each file in place, one after another, so every order leaves some window. I kept the registries after the executable because the executable has a failure the registries do not: relinking while the previous build is still running gets ETXTBSY on Linux, and the file is locked on Windows. With the registries before it, that run leaves new registries next to the old executable. That run isn't clean with the merged order either when there are guest modules on Linux: they are written first, and a loaded module can still be overwritten. Narrowing this further needs temporary files and renames, which is a separate change.

On import_audit: you're right that it only reads registries. Before this PR, a failed Patch left new registries, listing the imports of a build that was never written, beside the previous executable. They weren't stale; that's what "a binary that is not there" meant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants