Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 55 additions & 15 deletions core/relinker/codegen/src/Amd64OnlyConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,25 @@ class WindowPlacer {

[[nodiscard]] std::optional<Placement> 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;
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:
Expand Down Expand Up @@ -223,12 +227,43 @@ class WindowPlacer {
return std::nullopt;
}

[[nodiscard]] std::optional<Placement> _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<std::int8_t>::min() || reach > std::numeric_limits<std::int8_t>::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;
Expand All @@ -239,7 +274,13 @@ class WindowPlacer {
if (!_blocksFollowing(last))
moved.JumpTo(static_cast<std::int64_t>(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));
Expand All @@ -252,7 +293,6 @@ class WindowPlacer {
std::fill_n(_seg.begin() + static_cast<std::ptrdiff_t>(offset), _matches[index].Length, Amd64OnlySubstitutionTable::kTrapFill);
_seg[offset] = X64OpcodeConstants::OneByteJmpRel8;
_seg[offset + 1] = static_cast<std::uint8_t>(static_cast<std::int8_t>(_distance(island, store + kShortJumpSize)));
return placement;
}
};

Expand Down
36 changes: 36 additions & 0 deletions core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,41 @@ 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, bool regularIsland = false) {
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);
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())});
};
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");

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() {
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"}}) {
Expand Down Expand Up @@ -2768,6 +2803,7 @@ int main() {
converterWideStoreIslandAcrossInt64Max();
converterIslandIndirectEntries();
converterIslandInTrapPadding();
converterIslandAfterTargetedReturn();
converterClaimedFollower();
converterJumpTableTargets();
converterReciprocal();
Expand Down
Loading