From 6bcb80d4c0781435d7fd1cc7f631e4db071c6ff6 Mon Sep 17 00:00:00 2001 From: Zach Shallbetter Date: Sat, 28 Nov 2026 23:23:35 -0800 Subject: [PATCH 1/2] fix(relinker): place short instruction stubs in post-return padding --- .../codegen/src/Amd64OnlyConverter.cpp | 42 ++++++++++++++++++- .../codegen/tests/Amd64OnlyConverterTests.cpp | 30 +++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/core/relinker/codegen/src/Amd64OnlyConverter.cpp b/core/relinker/codegen/src/Amd64OnlyConverter.cpp index 74e028131ee5..ee77fb09cfe6 100644 --- a/core/relinker/codegen/src/Amd64OnlyConverter.cpp +++ b/core/relinker/codegen/src/Amd64OnlyConverter.cpp @@ -89,6 +89,8 @@ class WindowPlacer { inReach = true; if (auto placement = _islandAt(index, start)) return placement; + if (auto placement = _returnPaddingAt(index, start)) + return placement; } if (!inReach) return std::nullopt; @@ -223,12 +225,43 @@ class WindowPlacer { return std::nullopt; } + [[nodiscard]] std::optional _returnPaddingAt(const std::size_t index, const std::size_t start) { + using Amd64OnlySubstitutionTable::kJmpRel32; + if (start == 0 || start + kJmpRel32.Size >= _matches.size() || + _info(start - 1).FlowKind != ControlFlowKind::Return || !_int3(start) || + _end(start - 1) != _address(start)) + return std::nullopt; + + const auto first = start + 1; + const auto island = _address(first); + const auto reach = _distance(island, _address(index) + kShortJumpSize); + if (reach < std::numeric_limits::min() || reach > std::numeric_limits::max()) + return std::nullopt; + + const auto target = _branchTargets.lower_bound(island); + if (target != _branchTargets.end() && *target < island + kJmpRel32.Size) + return std::nullopt; + + Placement placement; + for (std::size_t byte = 0; byte < kJmpRel32.Size; ++byte) { + const auto padding = first + byte; + if (!_int3(padding) || !_movable(padding, true) || _address(padding) != island + byte) + return std::nullopt; + placement.Claimed.push_back(padding); + } + try { + _appendIsland(placement, index, island); + } catch (const CodegenException&) { + return std::nullopt; + } + return placement; + } + [[nodiscard]] Placement _buildIsland(const std::size_t index, const std::size_t start, const std::size_t last, const std::size_t head) { using Amd64OnlySubstitutionTable::kJmpRel32; const auto windowStart = _address(start); const auto windowEnd = _end(last); const auto island = windowStart + head; - const auto store = _address(index); Placement placement; StubBodyBuilder moved; @@ -239,7 +272,13 @@ class WindowPlacer { if (!_blocksFollowing(last)) moved.JumpTo(static_cast(windowEnd - windowStart)); placement.Sites.push_back(_site(windowStart, head, moved.Finish())); + _appendIsland(placement, index, island); + return placement; + } + void _appendIsland(Placement& placement, const std::size_t index, const std::uint64_t island) { + using Amd64OnlySubstitutionTable::kJmpRel32; + const auto store = _address(index); StubBodyBuilder lowered; lowered.Seek(_distance(store, island)); _matcher.EmitOutOfLine(lowered, _bytes(index)); @@ -252,7 +291,6 @@ class WindowPlacer { std::fill_n(_seg.begin() + static_cast(offset), _matches[index].Length, Amd64OnlySubstitutionTable::kTrapFill); _seg[offset] = X64OpcodeConstants::OneByteJmpRel8; _seg[offset + 1] = static_cast(static_cast(_distance(island, store + kShortJumpSize))); - return placement; } }; diff --git a/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp b/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp index ed3b23a0c7e9..29154bb54a6a 100644 --- a/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp +++ b/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp @@ -1212,6 +1212,35 @@ void converterIslandInTrapPadding() { require(site.Offset + site.Length <= entry, "An island in the int3 padding after a jump ran into the next function"); } +void converterIslandAfterTargetedReturn() { + const auto converter = Codegen::MakeAmd64OnlyConverter(Codegen::Amd64OnlyTarget::Rosetta, {}, true); + const auto convert = [&](bool entryIntoPadding) { + Bytes text = { + 0x75, 0x14, + 0x74, 0x09, + 0xC5, 0xFC, 0x11, 0x47, 0x10, + 0xC5, 0xFC, 0x11, 0x07, + 0xC3 + }; + if (entryIntoPadding) + text[1] = 0x0D; + text.insert(text.end(), 8, 0xCC); + text.push_back(0xC3); + Bytes file(0x200 + text.size()); + std::copy(text.begin(), text.end(), file.begin() + 0x200); + return converter->Convert(file, {segmentHeader(text.size())}); + }; + const auto result = convert(false); + require(result.Trampolines.size() == 2 && result.KeptCount == 0, "A short store next to a targeted return did not use the trap padding"); + require(result.Bytes[0x209] == 0xEB && result.Bytes[0x20A] == 4, "The short jump did not land in the trap padding"); + require(result.Bytes[0x20D] == 0xC3, "The targeted return was changed"); + require(result.Bytes[0x20E] == 0xCC, "The first trap after the return was changed"); + const auto& island = result.Trampolines.back(); + require(island.Offset == 0x20F && island.Length == 5, "The island was not confined to the trap padding"); + require(!island.Relocations.empty() && island.Relocations.back().SiteTarget == -2, "The island did not return to the original return instruction"); + requireFailure([&] { (void)convert(true); }, "The island replaced a branch target in the trap padding"); +} + void converterClaimedFollower() { const auto converter = Codegen::MakeAmd64OnlyConverter(Codegen::Amd64OnlyTarget::Rosetta); for (const auto& [shortInstruction, name] : {std::pair{Bytes{0x0F, 0x53, 0xCA}, "RCPPS"}, std::pair{Bytes{0xF3, 0x0F, 0xC7, 0xF8}, "RDPID"}}) { @@ -2768,6 +2797,7 @@ int main() { converterWideStoreIslandAcrossInt64Max(); converterIslandIndirectEntries(); converterIslandInTrapPadding(); + converterIslandAfterTargetedReturn(); converterClaimedFollower(); converterJumpTableTargets(); converterReciprocal(); From f5cfff2ea6ed596a8747a370510be3f16ec7cc8e Mon Sep 17 00:00:00 2001 From: Zach Shallbetter Date: Tue, 1 Dec 2026 02:57:45 -0800 Subject: [PATCH 2/2] fix(relinker): prefer regular islands before return padding --- .../codegen/src/Amd64OnlyConverter.cpp | 32 ++++++++++--------- .../codegen/tests/Amd64OnlyConverterTests.cpp | 8 ++++- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/core/relinker/codegen/src/Amd64OnlyConverter.cpp b/core/relinker/codegen/src/Amd64OnlyConverter.cpp index ee77fb09cfe6..b86f29e5d5bc 100644 --- a/core/relinker/codegen/src/Amd64OnlyConverter.cpp +++ b/core/relinker/codegen/src/Amd64OnlyConverter.cpp @@ -78,23 +78,25 @@ class WindowPlacer { [[nodiscard]] std::optional Island(const std::size_t index) { const auto store = _address(index); - for (std::size_t distance = 1;; ++distance) { - bool inReach = false; - for (const bool before : {true, false}) { - if (before ? distance > index : index + distance >= _matches.size()) - continue; - const auto start = before ? index - distance : index + distance; - if (_address(start) + kIslandSearchBytes < store || _address(start) > store + kIslandSearchBytes) - continue; - inReach = true; - if (auto placement = _islandAt(index, start)) - return placement; - if (auto placement = _returnPaddingAt(index, start)) - return placement; + for (const bool useReturnPadding : {false, true}) { + for (std::size_t distance = 1;; ++distance) { + bool inReach = false; + for (const bool before : {true, false}) { + if (before ? distance > index : index + distance >= _matches.size()) + continue; + const auto start = before ? index - distance : index + distance; + if (_address(start) + kIslandSearchBytes < store || _address(start) > store + kIslandSearchBytes) + continue; + inReach = true; + auto placement = useReturnPadding ? _returnPaddingAt(index, start) : _islandAt(index, start); + if (placement) + return placement; + } + if (!inReach) + break; } - if (!inReach) - return std::nullopt; } + return std::nullopt; } private: diff --git a/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp b/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp index 29154bb54a6a..068b6db90958 100644 --- a/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp +++ b/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp @@ -1214,7 +1214,7 @@ void converterIslandInTrapPadding() { void converterIslandAfterTargetedReturn() { const auto converter = Codegen::MakeAmd64OnlyConverter(Codegen::Amd64OnlyTarget::Rosetta, {}, true); - const auto convert = [&](bool entryIntoPadding) { + const auto convert = [&](bool entryIntoPadding, bool regularIsland = false) { Bytes text = { 0x75, 0x14, 0x74, 0x09, @@ -1226,6 +1226,8 @@ void converterIslandAfterTargetedReturn() { text[1] = 0x0D; text.insert(text.end(), 8, 0xCC); text.push_back(0xC3); + if (regularIsland) + text.insert(text.end(), {0x55, 0x48, 0x89, 0xE5, 0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54, 0x53, 0xC3}); Bytes file(0x200 + text.size()); std::copy(text.begin(), text.end(), file.begin() + 0x200); return converter->Convert(file, {segmentHeader(text.size())}); @@ -1239,6 +1241,10 @@ void converterIslandAfterTargetedReturn() { require(island.Offset == 0x20F && island.Length == 5, "The island was not confined to the trap padding"); require(!island.Relocations.empty() && island.Relocations.back().SiteTarget == -2, "The island did not return to the original return instruction"); requireFailure([&] { (void)convert(true); }, "The island replaced a branch target in the trap padding"); + + const auto regular = convert(false, true); + require(regular.KeptCount == 0 && !regular.Trampolines.empty() && regular.Trampolines.back().Offset >= 0x217, "The return padding took precedence over a regular island"); + require(regular.Bytes[0x20F] == 0xCC, "The return padding was used when a regular island was available"); } void converterClaimedFollower() {