perf(relinker): keep three passes linear on crafted guest binaries #12203
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12203"
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 MarcZX10 on 2026-10-28T03:34:37Z.
What
Three passes of the relinker are quadratic in the size of a crafted guest binary. One commit each:
GuestImageReader.cppreadLifecycleArrayscanned the whole RELA and PLT relocation tables for every 8-byte slot ofDT_INIT_ARRAY/DT_FINI_ARRAY: slots times relocations, about 10^12 steps for a million of each (a 32 MB file). The relocations are now grouped by target once per array, in table order, so each slot visits only its own entries and the checks and their order are unchanged.EhFrameReader.cppparsed the FDE and its LSDA call-site table for every.eh_frame_hdrrow, so a header repeating one row tens of thousands of times multiplied the work and the number of collected regions by the row count. A row that repeats an earlier(begin, fde)pair describes the same region and is skipped.RelativeJumpTables.cpp:FindRelativeJumpTableswalks every branch target found in a gap until a return, indirect jump or trap and only deduplicated start addresses, so a run ofjmp nextinstructions was walked to the segment end once per instruction. A walk now stops at a position an earlier walk already decoded (the targets it would find were registered then).FindTrailingRelativeJumpTablescanned every recorded table load at each instruction after an indirect jump; the loads are also kept by target now, in the order they were seen.No behaviour change is intended, so there is no new test: the existing relinker tests cover the results.
Tested
Linux x86-64, a throwaway archive build with the host CPU gate patched. These ctests pass:
init_array_roots,guest_needed_modules,guest_needed_nested_modules,guest_module_args,guest_module_fini,guest_zero_initializer,duplicate_relocations,linux_eh_frame_header,sce_code_gaps,jmprel_table_bounds,entry_point_bounds,gnu_hash_analysis,guest_gnu_hash,unused_filter_indirect,unused_filter_fallthrough_padding,unwind_record_bounds,control_flow_graphandstrict_nid_filter. The complexity claims come from reading the loops; I did not time the old and new binaries on a synthetic file.tools/check_conventions.py --base upstream/mainandgit diff --checkpass.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)Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-28T04:13:37Z.
Review: APPROVED
Reviewed
7849e7f69don macOS arm64 (relinker-only Release build), merged onto mained94f8443f: build 266/266,ctest -j 8366/366,check_conventions --base upstream/mainclean,pr_overlap --branch"No overlap with open pull requests" (1363 probed).Equivalence, read against main:
GuestImageReaderreadLifecycleArray: the index is built after thesize == 0return, from tables already checked to be% 24 == 0. Each slot's entries keep the old visit order (allRelaDatahits, thenRelaPltData, each in table order), so the checks and the first failure stay the same.FindTrailingRelativeJumpTable:loadsAt[target]is filled in the same order asloads, so the inner loop visits exactly the oldload.Target == positionentries in the same order.TablesEndstill gets the fullloads.FindRelativeJumpTables: decoding depends only onpos, so a walk that reaches a position an earlier walk decoded would repeat the same instructions, push the same (already-inserted) targets and stop at the same break.branchTargetsends up as the same set. Positions decoded in the per-function pre-pass are not marked, so nothing gets skipped.EhFrameReader:readFdedepends only on(begin, fde)plus the CIE cache, so a repeated row gives the same region, or throws on its first occurrence anyway.Mutation probe (each mutant built and run through the full ctest):
loadsAtnever filledcontrol_flow_graph)RelaPltDatasce_code_extent,sce_module_code_extent)pos + 1decodedNon-blocking notes:
INIT_ARRAY/FINI_ARRAYslot throughDT_JMPREL, and nothing checks which branch targets the gap walk finds. A small fixture for either would back up the no-behaviour-change claim.ReadExceptionFunctionsnow returns one region where it used to return N copies. SoRedZoneFrameReport::Functionsand the red-zone framing pass (frameSegmentiteratesfunctions) see each function once. That is arguably the right behaviour, but it is a visible change, so it might be worth one line in the body.Approving.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T04:58:00Z.
Review: COMMENTED
Reviewed — rooklz's notes checked, EH dedup change is visible and undocumented
The dedup in
EhFrameReader.cppchanges whatReadExceptionFunctionsreturns for repeated rows, and the PR body says no behaviour change is intended.What I ran
7849e7f69dca310b03525682b0f2acbebc82c9d3(OPEN, same as batch file).gh apiGET of issue comments, PR reviews and review comments for #12203;gh pr view --json statusCheckRollup;gh pr diff 12203(saved to a scratch file).git -C /marc/AnyPS5 show 7849e7f6…forEhFrameReader.cpp,GuestImageReader.cppandRelativeJumpTables.cpp(read at the lines cited).Results
Findings
std::set listedatEhFrameReader.cpp:38andif (!listed.emplace(begin, fde).second) continue;at line 43, so a repeated(begin, fde)row is no longer read again. The result therefore has one region where it used to have N copies. The PR body's EH bullet describes the skip, but the paragraph before "Tested" says "No behaviour change is intended". Adding one line that names the visible effect onfunctionswould reconcile the two. Not verified: the effect on the red-zone framing pass (frameSegment), which I did not read.RelaPltDataatGuestImageReader.cpp:299, so a fixture that relocates anINIT_ARRAY/FINI_ARRAYslot throughDT_JMPRELis a real gap candidate. The mutant results and whether the existing tests cover them were not reproduced, because nothing was built. Whether to add fixtures is the author's call.Sources
github.com/boykopovar/AnyPS5@7849e7f69d/core/relinker/relinker/src/analysis/UnusedNidFilter/EhFrameReader.cpp (L38-L43)github.com/boykopovar/AnyPS5@7849e7f69d/core/relinker/relinker/src/guest/GuestImageReader.cpp (L289-L299)Not verified
frameSegment) effect of the EH change was not read.