fix(relinker): mask embedded data tables in inter-FDE gaps #8076
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8076"
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 brainiac91 on 2026-10-19T20:09:58Z.
What
Resolves #7547 ([Relinker] Crash in CodeInstructionCollector during PRX relinking on ubiservices.prx).
Compilers place constant data tables, such as the OpenSSL Whirlpool table in
ubiservices.prx, in inter-FDE gaps after function code and reference them with a RIP-relativeLEA.FindRelativeJumpTablestreated thatLEAtarget as gap code, so the syscall scan decoded the table bytes andCodeInstructionCollectoradded them as CFG roots;0F 78is not a valid opcode, so conversion failed.LEAtarget are padding, no direct branch targets it, and its linear decoding fails; mask it with0x90in the scan buffer inRelinkerPipelineandGuestModuleBuilder, and keep it out of the CFG roots inCodeInstructionCollector.PT_GNU_EH_FRAMEas well asPT_SCE_DYNLIBDATAto locate those gaps, but keep the syscall and call-site scan rangesPT_SCE_DYNLIBDATA-only, so images without it stay strict.GapAnalysiscaches the per-byte padding, trailing-code and syscall state in one backward pass per gap, so the analysis is linear in gap size.Supersedes and replaces #8042.
Tested
main1f68b1f77; linear history, 8 commits.ctest --test-dir build-relinker: 301/301 passed.control_flow_graph,sce_code_extent,sce_module_code_extent,executable_segments: 4/4.CheckLargeZeroGaps(32 gaps of 64 KiB, 2 MiB) and the decoded-padding-suffix and stub-before-data regressions were verified to fail when their fixes are reverted. A 2 KiB table with00/cc/90runs andmov rax,rax; retchains after them masks whole, and fails against the earlier terminator rule.C0table (constants from Linuxcrypto/wp512.c, public domain) in a scratch harness on this head: with the reported0F 78 33 1Eat theLEAtarget the mask covers all 2048 table bytes and the syscall scan finds nothing in them, while amov eax,9; syscall; retstub behind the table is still seen and refused;C0on its own decodes linearly to +2046 with no0F 05/0F 34/0F 07/CD 80pair, so it is left unmasked.tools/check_conventions.py --base origin/mainandgit diff --check: clean.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 MarcZX10 on 2026-10-19T23:16:24Z.
I checked current head
17feca5cf4on Ubuntu Linux with GCC 12.2 in Release relinker-only mode. Build passed forrelinker,control_flow_graph_tests, andrelinker_conversion_tests. Focused CTest passed 4/4:control_flow_graph,sce_code_extent,sce_module_code_extent, andexecutable_segments. In particular,executable_segmentspasses here, including the truncatedrex-syscalltail that still fails on #8042. I have not run the full suite or the reported title binary, so this is focused Linux validation only.Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-19T23:17:53Z.
Verdict: defect. The mask runs past the data table into decodable code, so a forbidden syscall there is no longer refused.
For an embedded data table,
FindRelativeJumpTablesreturns{function, table start, inspectEnd}, whereinspectEndis the next unwind function. The syscall scan masks that whole range with0x90.HasUndecodableBytesonly requires one undecodable byte in the range, so decodable code that follows the data in the same gap is masked too. That contradicts the debt line ("gaps with ... valid instructions are preserved as code and undergo strict scanning").Probe image:
make_image("register", "unwind", body=c3)fromtest_tls_function_coverage.py, extended to three FDEs (0x1200, 0x1300 with size 8, 0x1400 with size 1):mainmain9127ea36d)Cannot decode instruction: Unsupported 0F 78 opcode without SSE4A prefix (offset 0x1310)b8 3b 00 00 00 0f 0590x6 (control)Forbidden syscall instruction at code offset 0x1325: syscall number 59Both results are the same for the Linux default and
--windows. Ending the masked range where the decodable code resumes, instead of atinspectEnd, would keep the refusal.Other results:
ctest212/212. Two load timeouts under-j4(performance_compare,guest_none_relocations), which also time out onmain, passed on a serial rerun. WSL Ubuntu 24.04, GCC 13.3: 216/217. The one failure,shader_cache_runtime_sources, fails intermittently onmaintoo.RelativeJumpTables.cppreverted tomain,control_flow_graphandsce_code_extentfail. With only theGuestModuleBuilder.cpp,RelinkerPipeline.cppandCodeInstructionCollector.cppchanges reverted, both still pass. No test covers thePT_GNU_EH_FRAMEunwind ranges, the guest-module masking, or thesceBase || !functions.empty()change.PT_GNU_EH_FRAMEand noPT_SCE_DYNLIBDATA, so the new unwind-range path is active for them. Relinked with--windows --to-intel,--macos --to-rosettaand Linux--to-intel, all 12 output sets are byte-identical tomain. I can't checkubiservices.prx(#7547) myself.executable_segmentsfails on Windows and Linux because of #8042's resync. The data probe without an LEA-referenced table then converts with a forbidden syscall left in place (see my comment on #8042). If this PR replaces #8042, closing #8042 avoids landing both.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T23:20:03Z.
Follow-up full Linux validation on unchanged head
17feca5cf4: Release relinker-only build completed successfully, thenctest --test-dir build-relinker --output-on-failurepassed 217/217. This includes the newcontrol_flow_graphtest and theexecutable_segmentstruncated-tail regression. Environment: Ubuntu Linux, GCC 12.2. I have not run the target title binary or native Windows/macOS builds.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-19T23:21:25Z.
I checked this against the current head
17feca5cf4:FindRelativeJumpTablescan return the data-table start withEnd=inspectEnd, and both scan paths fill the entire[Begin, End)interval with NOPs before callingSyscallScanner. Since the candidate is accepted when the range contains any undecodable bytes, valid code later in that same gap is hidden from the forbidden-syscall scan. Your probe demonstrates the consequence.Please limit masking to the confirmed table bytes and preserve scanning after the table, with a regression fixture containing an embedded undecodable table followed by decodable code with a forbidden syscall before the next FDE.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T01:22:10Z.
Adding one case to @wachi35676's finding, because ending the mask where decodable code resumes doesn't cover it.
On
main, any instruction the decoder can't handle inside scanned code is fatal (Cannot decode instruction, rc 2). With this PR, the classification itself rests on that failure: a gap region is treated as a data table when the preceding function takes its address with a RIP-relativelea, only padding precedes it, nothing branches into it directly, andHasUndecodableBytesfinds one undecodable byte anywhere before the next padding run.A callback routine without an FDE fits all four: its address is taken with
lea(that's how callbacks are passed), nothing calls it directly, it follows padding, and the decoder only needs to miss one instruction in it (any extension we don't decode yet). Today that relink fails loudly. With the PR it converts, and the whole routine, including anysyscall, is skipped bySyscallScanner, which is the only check that refuses forbidden syscalls.Could the mask cover only bytes that actually fail to decode from the table start, i.e. stop at the first offset where linear decoding succeeds through to padding or a decodable instruction run, and keep a region whose first bytes decode as code in the strict scan? A test with a
lea-referenced routine that contains asyscallplus one undecodable instruction after it would pin this down.Separately: the gate titles' executables (and the homebrew wachi35676 listed) have
PT_GNU_EH_FRAMEand noPT_SCE_DYNLIBDATA, so the new unwind-range path runs for every PS5 executable, including main's existing trailing-jump-table masking and thesceBase || !functions.empty()change. A test for that path, plus a relink comparison againstmainon the next head, would show it changes nothing beyond the tables.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T08:10:09Z.
Thanks, the bounded table end and the callback case look right, and the relinked output for our two test titles is byte-identical to
mainon both the Linux and Windows targets.One problem before merge: the relink takes much longer. For the PPSA21564 executable,
--to-intelgoes from 4.5 s onmainto 124 s with this PR (121 s of user time, same output, one run each, reproduced by a second run on another machine slot).--windowsgoes from 44 s to 372 s. A profile puts 85% of the samples inFindRelativeJumpTables(with the new helpers inlined) and about 5% in the decoder.The cause looks like
OnlyPadding(code, pos, ...), which walks the whole padding run (00/CC/90) frompos. It is now called at every position: inDecodesAsTrailingCodeBlock's per-instruction loop, in both decode loops ofFindEmbeddedDataTableEnd, and through the per-byte retry loops that callDecodesAsTrailingCodeBlockfrom every offset. A gap with long zero runs then costs at least quadratic time in the run length, and a title with a larger zero-filled gap could look hung.Could you compute the padding-run ends once per gap (one backward pass gives, for each offset, where the run of padding bytes starting there ends), so that
OnlyPaddingis a lookup, and limit the per-byte retry so the whole search stays linear in the gap size? A test with a large zero-filled gap after a table (for example 1 MiB) would keep this from coming back.Imported from boykopovar/AnyPS5. Originally posted by screamm on 2026-10-20T13:43:07Z.
Windows run of head
e4034102a, which is the head @oneandonlydean timed. It no longer merges intomaind50ec76da: besidesdocs/dev/debt/relinker.md,GuestModuleBuilder.cppandRelinkerPipeline.cppconflict with #7660 (SceUnwindRangesis now passed to the syscall scanner). I therefore built the head as-is and compared it with its merge-base585075cb7. That base relinks every title below in the same time asmaind50ec76daand to the same bytes, so the ratios also hold against currentmain.Host: Windows 11 Pro 26220, Ryzen 7 5700X, NVIDIA GeForce RTX 4070 driver 596.21, WinLibs MinGW-w64 GCC 15.2 r7, Release, relinker-only build (
-DANYPS5_RELINKER_ONLY=ON).control_flow_graph,executable_segments,sce_code_extentandsce_module_code_extenteach passed 3/3.eboot.elf, including PPSA99005 and PPSA99995 with its 16 Wine modules) for the default Linux target and for--windows. Every output file has the same sha256 as the base and asmain. PPSA99360--windowsfails the same way on all builds (Base relocation target crosses a page boundary).--windows, sum over 51 titles (wall)--windows, sum (user CPU)--windows--windows--windowsThe per-title median ratio is 3.06x (default) and 1.57x (
--windows), and no title got faster. The cost does not follow file size: PPSA99420 gains 55 s on the default target, while the larger PPSA99004 gains 9.5 s.--windows, 60 s each, RTX 4070): PPSA99005 and PPSA99995 relinked by the PR were still running at 60 s (158 and 34 frames;maingave 162 and 41 on the same loaded host), and neither printed a stop line.python tools/check_conventions.py --base 585075cb7passes, andgit diff --checkis clean.Not checked:
ubiservices.prx(#7547) and PPSA21564 (I don't have them), a profile, a rebase onto currentmain, macOS output, and the full relinker suite (a-j3run was cut short by a host problem after 87/212 had passed).Written with Claude Code assistance (validation by a subagent, reviewed in this session).