fix(relinker): place 256-bit store islands across 2^63 without signed overflow #11232
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11232"
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-25T22:18:43Z.
What
--to-rosettalowers a 256-bit store that has no room for a stub jump through an island: the instructions after the store move into a stub, ajmp rel32to the lowered store is written in their place, and ajmp rel8over the store reaches that island._islandAtand_buildIslandincore/relinker/codegen/src/Amd64OnlyConverter.cppcompute four distances by casting both addresses tostd::int64_tand subtracting:reach, from the end of the short jump to the islandSeekback to the storeJumpToafter the storerel8displacement written over the storeWhen a code segment crosses 2^63, the store sits just below it and the island just above, so the two signed operands have opposite signs and the subtraction overflows, which is undefined behaviour. #11198 reports it with UBSan (
Amd64OnlyConverter.cpp:169:66: runtime error: signed integer overflow). The wrapped result happens to be right in practice, so no wrong output has been seen.A
_distance(to, from)helper now subtracts the addresses asstd::uint64_tand converts the result tostd::int64_t, which C++20 defines modulo 2^64. That gives the exact signed distance for any two addresses less than 2^63 apart, and the island window is a few bytes. The range checks after it (reachagainstint8, the stub builder's own limits) are unchanged. There is one commit per site group, then the test:converterWideStoreIslandAcrossInt64Max.The test converts #11198's island fixture with the segment mapped at
0x1000and at0x8000000000000000 - 0x10. At the high base it requires the same segment bytes, trampoline offsets, lengths, bodies and relocations as at0x1000, andEB 0Dfor the short jump. Without sanitizers it guards the result; under-fsanitize=undefinedit also catches the overflow.relinker-asanruns ASan only, while the nightly ASan and UBSan workflow in #11062 and any UBSan build would report it.#10680 and #11033 fix the same kind of overflow in
_collectBranchTargets(#10653). They change other lines and do not touch the island path.Fixes: #11198
Tested
ubuntu-latestfork runner, relinker-only Debug builds:amd64_only_converterpasses without sanitizers and with-fsanitize=undefined -fno-sanitize-recover=undefined. WithAmd64OnlyConverter.cpprestored frommainand the new test kept, the UBSan build fails withAmd64OnlyConverter.cpp:169:126: runtime error: signed integer overflow.-fsanitize=undefined -fno-sanitize-recover=undefined. Onmainit stops atAmd64OnlyConverter.cpp:203:21: runtime error: signed integer overflow. With this branch it prints2 trampolines, short jump EB 0Dfor both bases. The fullamd64_only_convertertest cannot run on this host: it executes instructions the CPU lacks, also onmain.python3 tools/check_conventions.py --base upstream/mainandgit diff --check upstream/mainpass.tools/pr_overlap.py --branchlists only #8042 and #8236, which also add tests toAmd64OnlyConverterTests.cpp, with no conflicting lines. Windows and macOS were not run.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 roboterspinne on 2026-10-26T18:05:01Z.
Review: APPROVED
Reviewed
9da4efccemerged ontomain570089a80(2026-10-26); the merge is clean. Windows 11, i9-12900H, WinLibs GCC 15.2, relinker-only Release.Results
ctest: 308 of 309 pass;patch_advisor_unitfails the same way on plainmain.amd64_only_converterpasses.main'sAmd64OnlyConverter.cppand your test (MinGW has no UBSan runtime, so I used trapping builds):mainthe wrapped result is right.-fsanitize=signed-integer-overflow -fsanitize-undefined-trap-on-errorit traps inconverterWideStoreIslandAcrossInt64Max, in_islandAtat main's line 169.-ftrapvit aborts in_buildIslandat main's line 203.-ftrapv._distanceis exact here.CMAKE_CXX_STANDARDis 20, so the conversion toint64_tis defined, and every island distance stays inside the 0x100 search window. The int8 reach check and the stub's rel32 checks are unchanged.Nit: nothing places an island at the rel8 limit. Dropping
+ kShortJumpSizefrom line 170 passes both your suite andmain's. With that mutant, a backward island at reach -129 or -130 is accepted and line 214 writes rel8 +127/+126. A fixture accepted at -128 and 127 and refused at -129 and 128 would pin it. The gap is older than this PR, but the PR rewrites the line.Notes, nothing to change here
_collectBranchTargetsdrops those targets (line 284,target >= 0). With your fixture plus a jump to offset 0x10, which is 2^63 at the high base, the island window covers that target onmainand on this PR. With #10680 merged, the high base gives the same placement as the low base. So "do not touch the island path" holds for the lines, but a correct island across 2^63 needs both PRs._collectBranchTargets. It changesCodeInstructionCollector::Collectinstead; the conclusion that it doesn't overlap stays true.main()registration lines inAmd64OnlyConverterTests.cpp. Keeping both calls passes, plain and under the trap.Amd64OnlyConverter.cppauto-merges.relinker-asanruns only thetls_function_coveragelabel, soamd64_only_converterruns under no sanitizer inmain's CI today.Not checked: a real UBSan build, Linux and macOS, #11198's driver, and running converted code above 2^63.
I found nothing that needs to change before merge. For disclosure: none of my open PRs touches these files.
Written with AI assistance (Claude Code). I ran the builds, the counter-checks and the mutants above on my Windows 11 machine.