fix(relinker): accept a relative jump table padded to 16 bytes before more code #8168
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8168"
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 savvaniss on 2026-10-20T01:09:34Z.
What
FindTrailingRelativeJumpTable(#6595) finds a switch's relative jump table placed after a function's last instruction. It accepts the table only when padding follows it up to the end of the inspected range, which reaches the next FDE.In the
SavedGames.prxplugin that Boggle (CUSA03312, a Unity title) ships inMedia/Plugins, a function's table ends 8 bytes past its FDE. Padding then runs to the next 16-byte boundary, and code that no FDE covers follows before the next FDE. The table was therefore not recognised, and its words were decoded as instructions. The Windows conversion then failed on a TLS access that does not exist (Unsupported Windows guest TLS instruction (bytes: 64 fe ff) (offset 0x741c)).The table is now also accepted when padding reaches the next 16-byte boundary after it:
main. When it holds, the table ends at the end of the range, so a long NOP that crosses the boundary is still accepted.main.Code before the boundary still rejects the table; the existing
nonPaddingcase covers that.FindTrailingRelativeJumpTabletakes the code's address, to find the boundary, and the end of the function's FDE;FindRelativeJumpTablespasses both.#6618 handles tables inside a function's unwind range with a separate pass, and conflicts with
main. This only widensmain's trailing-table rule.Overlap with #8076:
OnlyPaddingnow takes an end,(code, from, to, decoder). #8076 calls the three-argumentOnlyPadding(code, from, decoder)in five places, so merging the two needs those calls to passcode.size(), besides thetables.push_backline both edit.Tested
Windows 11 Pro 26300, WinLibs GCC 15.2.0, Release, relinker-only build (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON) offb050223d.Full
ctest: 213/213 pass.control_flow_graphcovers the trailing table with a code address that is not 16-aligned (through both functions), a table that ends on a 16-byte boundary at +8, padding to the next function at +48, a NOP that crosses a 31-byte range end, the 15+9-byte NOP layout (end 48, and rejected when code follows at +48), and a table cut short inside its FDE. Each check fails with the change it covers removed:4aadd1965rule (boundary checked first)Table followed by a NOP across the boundary was not foundunwindEnd0)Table followed by more of its function was acceptedTable boundary ignored the code addressFindRelativeJumpTablespasses 0 for the addressFunction address was not passed to the table search+ 16in place of+ 15Table ending on a 16-byte boundary was rejectedPadding to the end of the range was cut at the boundarystd::minclamp to the rangePadding check read past the inspected rangeOnlyPaddingdecodes past its endTable ending inside a NOP was acceptedFindRelativeJumpTableson the layouts from the review (code at 0xec00001000, FDEs{base, base+9}and the next function at the range end):maina569f47cb4aadd1965fb050223d90to 0x20,90to 0x40, 7-byte NOP inside +0x20Hasbro pack (CUSA03312): on the stack of
main, #6684, #7660, #8166 and the TLS relocation fix from #6977, with4aadd1965and withfb050223d, Boggle (--module-dir boggleMedia/Modules --module-dir boggleMedia/Plugins, 14 modules includingSavedGames.prx) and Trivial Pursuit (15 modules) convert for Windows and Linux, and so do Risk and Monopoly through--executable. The two outputs are byte-identical; the Linux.desktopfiles differ only in their output path. Earlier: without this change the same stack stops at the TLS error above in Boggle'sSavedGames.prx; with #6977's libraries Boggle then renders its first scene. Trivial Pursuit Live!'s ownSavedGames.prxconverts either way.tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branchlists only #8076, which conflicts withmain, soRelativeJumpTables.cppandControlFlowGraphTests.cppwere not compared automatically; see the overlap note above.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 github-actions[bot] on 2026-10-20T01:19:52Z.
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.
core/relinker/relinker/src/analysis/RelativeJumpTables.cppcore/relinker/relinker/tests/ControlFlowGraphTests.cppImported from boykopovar/AnyPS5. Originally posted by SandstromPL on 2026-10-20T11:14:22Z.
@savvaniss Review of
4aadd1965.Regression: a long NOP that crosses the 16-byte boundary now makes the table be rejected
At
RelativeJumpTables.cpp:87,OnlyPadding(code, end, aligned, decoder)runs first, andOnlyPaddingnow decodes with the limitto - position(:34-35). When a multi-byte NOP starts beforealignedand ends after it, that check fails and the loop takescontinue, so the range-end check on :88 is never reached. main did a singleOnlyPadding(code, end)call bounded bycode.size(), and it accepts these tables. That goes against the body's "When padding continues to the end of the range, the table ends at the end of the range, as before".Common prefix for every layout:
48 8d 05 09 00 00 00(lea rax → +0x10),ff e0(jmp rax),90×7, then the table at +0x10,f0 ff ff ff f7 ff ff ff({-16,-9}), which ends at +0x18. Code address 0xec00001000; functions{base, base+9}and{base+size, …}.a569f47cb4aadd196590×4, then0f 1f 80 00 00 00 00at +0x1c..+0x22, then90up to +0x3066 66 2e 0f 1f 84 00 00 00 00 00at +0x18..+0x22, then90up to +0x4066 66 66 66 66 66 2e 0f 1f 84 00 00 00 00 00at +0x18, then66 0f 1f 84 00 00 00 00 00at +0x27, next function at +0x3090only up to +0x20 or +0x40, or a 7-byte NOP at +0x18..+0x1e that does not crossWhen a table is rejected, it is no longer filled with 0x90 before the syscall scan (
RelinkerPipeline.cpp:392-393), andCodeInstructionCollector.cpp:261-264no longer cuts it out of the function. Its words are decoded as code again, which is the failure this PR fixes. It only happens when a NOP straddles the first 16-byte boundary after the table, which needs more than 16 bytes of padding (for example, a next function aligned to 32 or 64 bytes). Plain 16-byte function alignment never triggers it. We have not measured how common this layout is in shipped titles.How we reproduced it: a single-file probe that calls
FindTrailingRelativeJumpTableandFindRelativeJumpTables, compiled against each tree's own headers andlibrelinker_core.a. Suggested fix: check main's rule first, and fall back to the boundary rule only if it fails.What the fix does:
codeAftercase still gives [+0x10,+0x20).55 5d c3at +0x30, are still rejected, as on main.control_flow_graphpasses.Letting
OnlyPaddingdecode pasttodoes not work as a fix. The 15+9 layout then ends at +0x20, inside the 15-byte NOP, and the two-9-byte-NOP case is accepted with its end inside a NOP.Smaller: a table that ends exactly on a boundary has nothing after it checked
If
TablesEndstops on a 16-byte boundary, thenaligned == endand nothing after the table is checked. A table cut short by an entry that fails validation is therefore accepted.Example, 0x38 bytes with one FDE over all of it:
48 8d 05 09 00 00 00 ff e0 90×7 | f0 ff ff ff f9 ff ff ff fa ff ff ff fb ff ff ff | 20 00 00 00 20 00 00 00 cc×8 | 55 5d c3 cc×5test_sce_code_extent.pyfixture (with PT_TLS) we put64 48 8b 00 48 89 c1 48 89 c2 c3at that point. main--windowsrewrites the fs access, #8168 leaves it raw, and both exit 0.clang does not emit live code after a table inside the same FDE, so this is unlikely in practice. A bound that fixes it and leaves the Boggle layout alone: treat the bytes after the table that the function's own FDE still covers as padding that must be checked, as main does.
const auto checkedTo = std::max(aligned, std::min(code.size(), unwindEnd));where the code now usesaligned.FindRelativeJumpTablespassesstd::min(end, segmentEnd) - beginasunwindEnd.We checked this bound on its own on
4aadd1965:control_flow_graphpasses.We have not tested it together with the diff above.
Test gaps. Each mutant below passes
control_flow_graphunchanged, and no other test exercisesRelativeJumpTables.cppdirectly.codeAddressis never exercised, because every call uses an address that is 0 mod 16. Ignoring it, or havingFindRelativeJumpTablespass 0 instead ofbegin, both survive. Cases:FindTrailingRelativeJumpTable(nonPadding, TextAddress + 1, &e) == 16withe == 31, andFindRelativeJumpTables(nonPadding, TextAddress + 1, {{T+1, T+10}, {T+33, T+41}})returning one table [T+17, T+32).*tableEnd = alignedon :88 survives: the old 32-byte case has its boundary equal tocode.size(), andcodeAfterhas code after the boundary. Case:textpadded with0xccto 48 bytes, with the next function at T+48, must give End == T+48. To pin the regression above, use the 15+9 NOP layout: tableEnd must be 48, and the same padding followed by code at +48 must be rejected.std::minclamp (the read then runs past the span) survives, and so does+16in place of+15. Cases:FindTrailingRelativeJumpTable(std::span<const std::uint8_t>(nonPadding).first(31)) == 16, andFindTrailingRelativeJumpTable(nonPadding, TextAddress + 8) == 16.Lines for the end of
CheckRelativeJumpTables(); they pass on4aadd1965On the #8076 overlap: #8076 (e4034102a) calls the 3-argument
OnlyPadding(code, from, decoder)atRelativeJumpTables.cpp:121,:125,:151,:172and:353. This PR changes that function to(code, from, to, decoder), so a merge that resolves only thepush_backhunk will not build.Our runs (Linux GCC, relinker-only Release):
4aadd1965, full ctest: 218/218. linux_gnu_segments timed out once under load and passes when run alone.e9a5a0c84+ #8168: 225/225.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T11:38:56Z.
Verdict: confirm. Ending the table at the 16-byte boundary keeps code after the table in the syscall scan. In one layout it also closes a gap that exists on
main.Tested at
4aadd1965, merged ontomain02fb1ce53. Windows 11 Home 10.0.26300, Ryzen 5 9600X, WinLibs GCC 15.2.0 r7, relinker-only Release. WSL Ubuntu 24.04, GCC 13.3.RelinkerPipelinefills[table.Begin, table.End)with0x90before the syscall scan, so I checked what the mask covers when code follows the table. Probe: the PS4fixture()fromtest_sce_code_extent.py, rebuilt with two unwind functions, 0x200..0x210 and 0x230..0x240. The first islea rcx,[rip+9]; movsxd rax,[rcx+rdi*4]; add rax,rcx; jmp rax, and its table starts at 0x210, after its FDE. Both the Linux default and--windowsgive the same result:mainccx8,mov eax,59; syscall; retat 0x220Forbidden syscall ... 0x225: syscall number 59ccx8,xor eax,eax; syscall; retForbidden syscall ... 0x222mov eax,59; syscall; retForbidden syscall ... 0x225(no number)... 0x225: syscall number 59xor eax,eax; syscall; ret0f 05 c3Forbidden syscall ... 0x222ccx8,retOn
main, the table in rows 3 and 4 is rejected because code follows it. Its words are then decoded as instructions, and that decode runs into the code: in row 4 it swallows thesyscall. With this PR, the table is masked only up to 0x220, and the scan decodes the code from its first byte. Only entries thatTableEntriesaccepts (negative offsets back to instruction starts) and padding are masked. When padding runs to the end of the range,tableEndis the end of the range, as onmain. #8076 ate4034102agivesmain's results for all six rows, row 4 included.Other results:
ctest: Windows 221/221, WSL 226/226 (main: 221/221 and 226/226).OnlyPadding(code, end, aligned, ...)changed back to padding up to the end of the range,control_flow_graph_testsfails withTable padded to 16 bytes before more code was not found.PT_SCE_DYNLIBDATA, soFindRelativeJumpTablesdoesn't run for them. All 12 output sets (--windows --to-intel,--macos --to-rosetta, Linux--to-intel) are byte-identical tomain.syscall number 59), and rows 2 and 4 convert under #8166's rule for syscalls without a constant number (see my comment on #8166).AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by savvaniss on 2026-10-20T12:33:56Z.
@SandstromPL @wachi35676 Thank you both.
fb050223daddresses the review.Long NOP across the boundary. Applied your order: padding to the end of the range is checked first, as on
main, and the boundary rule is only the fallback.Table ending on a boundary. Applied your bound,
checkedTo = std::max(aligned, std::min(code.size(), unwindEnd)), in every place that usedaligned.FindRelativeJumpTablespassesstd::min(end, segmentEnd) - begin.FindTrailingRelativeJumpTabletakes it as a fourth argument that defaults to 0, so a call without an FDE end checks only up to the boundary.Tested the two together with
FindRelativeJumpTableson your layouts (code at 0xec00001000):maina569f47cb4aadd1965fb050223d90to 0x20,90to 0x40, 7-byte NOP inside +0x20Tests. Your cases are in
control_flow_graph, with one change and two additions:OnlyPadding(code, end, code.size())accepts them and gives 31 with or without the clamp. The clamp check now uses a 4-byte NOP at +28 that crosses the 31-byte range end; without the clamp, the boundary check reads past the span and accepts the table.Each new check fails with the change it covers removed (tests built against each variant of
RelativeJumpTables.cpp):4aadd1965rule (boundary checked first)Table followed by a NOP across the boundary was not foundunwindEnd0)Table followed by more of its function was acceptedTable boundary ignored the code addressFindRelativeJumpTablespasses 0 for the addressFunction address was not passed to the table search+ 16in place of+ 15Table ending on a 16-byte boundary was rejectedPadding to the end of the range was cut at the boundarystd::minclampPadding check read past the inspected rangeOnlyPaddingdecodes past its endTable ending inside a NOP was acceptedRuns (Windows 11 Pro 26300, WinLibs GCC 15.2.0, relinker-only Release):
ctestatfb050223d: 213/213.main, #6684, #7660, #8166, #6977's TLS relocation fix): Boggle (14 modules, includingSavedGames.prx) and Trivial Pursuit (15) convert for Windows and Linux with--module-dir <Media>/Modules --module-dir <Media>/Plugins, and Risk and Monopoly with--executable. The outputs of4aadd1965andfb050223dare byte-identical; the Linux.desktopfiles differ only in their output path.@wachi35676: in your six rows the FDE ends at 0x210, before the table, so
unwindEndis below the boundary and the boundary rule applies as it did at4aadd1965. The padding-to-range-end check gives the same answer as before for those rows too. I expect the same six results but have not rerun your fixture.The #8076 overlap is now in the PR body:
OnlyPaddingtakes(code, from, to, decoder), so #8076's five three-argument calls needcode.size()when the two are merged.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-20T13:13:39Z.
Verdict: confirm. Your expectation holds: my six rows give the same results at
fb050223das at4aadd1965, including the gap onmainthat this PR closes.I re-ran
fb050223dmerged ontomain44e3389c8(clean merge) on Windows 11 with WinLibs GCC 15.2.0 (relinker-only Release) and on Ubuntu 24.04 (WSL2) with GCC 13.3. I used the same fixture as before: the table at 0x210 after the function 0x200..0x210, and a second FDE at 0x230. Linux default and--windowsgive the same results.main4aadd1965fb050223dccx8,mov eax,59; syscallccx8,xor eax,eax; syscallmov eax,59; syscallxor eax,eax; syscall0f 05 c3ccx8,retOne more layout, which follows from the new FDE bound: the same six gaps, but the first FDE covers 0x200..0x230, so the code after the table is part of the table's function. Here
fb050223dgives exactlymain's results, including rc 0 with0f 05 c3still in the output for row 4.4aadd1965refused row 4. That is SandstromPL's rule working as designed (a table followed by more of its own function isn't masked), and it isn't a regression frommain. It does mean that the decode of an unmasked table can still swallow a followingsyscallwhen the FDE covers it. That belongs to the syscall scan, not to this PR.Other results:
ctest: Windows 223/223 and WSL 228/228, the same counts asmain44e3389c8on each OS. Neither build has warnings from this change.RelativeJumpTables.cpp/.hppset back to4aadd1965,control_flow_graphfails withTable followed by a NOP across the boundary was not found. With them set back tomain, the test doesn't compile (FindTrailingRelativeJumpTabletakes one argument there).--windows --to-intel,--macos --to-rosetta, Linux--to-intel) are byte-identical tomain44e3389c8.AI-assisted: yes (Claude Code); results above were run on this machine.