Skip to content
Closed
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
10 changes: 8 additions & 2 deletions core/relinker/codegen/src/x86/RedZoneFramer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ struct Insn {
DecodedInstructionInfo Info;
};

bool segmentAccess(const DecodedInstructionInfo& info) {
return info.HasMemoryOperand && (info.SegmentPrefix == 0x64 || info.SegmentPrefix == 0x65);
}

std::optional<std::uint8_t> pushedRegister(const std::uint8_t* code, const Insn& insn) {
if (insn.Length == 1 && code[0] >= 0x50 && code[0] <= 0x57 && code[0] != 0x50 + kRegRsp) return static_cast<std::uint8_t>(code[0] - 0x50);
if (insn.Length == 2 && code[0] == 0x41 && code[1] >= 0x50 && code[1] <= 0x57) return static_cast<std::uint8_t>(code[1] - 0x50 + 8);
Expand Down Expand Up @@ -624,8 +628,7 @@ void frameSegment(
info.FlowKind == ControlFlowKind::IndirectJump ||
info.FlowKind == ControlFlowKind::Return ||
info.FlowKind == ControlFlowKind::Trap;
const bool tls = info.HasMemoryOperand && (info.SegmentPrefix == 0x64 || info.SegmentPrefix == 0x65);
if (!terminator || tls || address + match.Length > nextLimit) {
if (!terminator || segmentAccess(info) || address + match.Length > nextLimit) {
skip("instruction crosses the range end");
invalid = true;
break;
Expand Down Expand Up @@ -682,6 +685,7 @@ void frameSegment(
constexpr std::int64_t shift = kRelocatedShift;
if (bodyEnd - static_cast<std::int64_t>(start) < static_cast<std::int64_t>(kJumpBytes)) return "leaf shorter than a jump";
if (overlapsExisting(start, static_cast<std::size_t>(bodyEnd))) return "overlaps another stub";
if (std::any_of(body.begin(), body.end(), [](const Insn& insn) { return segmentAccess(insn.Info); })) return "segment-relative access in a relocated leaf";
if (enteredFromOutside(start + 1, start + kJumpBytes)) return "branch from outside into the start of a relocated leaf";
const auto inside = [&](const std::int64_t target) -> std::optional<std::size_t> {
if (target < static_cast<std::int64_t>(start) || target >= bodyEnd) return std::nullopt;
Expand Down Expand Up @@ -1118,6 +1122,7 @@ void frameSegment(
std::vector<RedZoneSiteEdit> siteEdits;
const auto copy = [&](const std::size_t k) -> std::optional<Copied> {
const auto& insn = body[k];
if (segmentAccess(insn.Info)) return std::nullopt;
Copied out;
if (insn.Info.FlowKind == ControlFlowKind::ConditionalBranch) {
if (!insn.Info.HasBranchTarget || insn.Info.HasRipRelativeDisp || insn.Info.OpcodeOffset != 0) return std::nullopt;
Expand Down Expand Up @@ -1398,6 +1403,7 @@ void frameSegment(
}
const auto prologueEnd = p > 0 ? body[p - 1].Offset + body[p - 1].Length : start;
if (reason == nullptr && !thunk && std::any_of(stackMoves.begin(), stackMoves.end(), [&](const std::size_t k) { return std::none_of(exits.begin(), exits.end(), [&](const ExitSite& exit) { return exit.Begin <= body[k].Offset && body[k].Offset < exit.End; }); })) reason = "push or pop inside the body";
if (reason == nullptr && std::any_of(body.begin(), body.begin() + static_cast<std::ptrdiff_t>(p), [](const Insn& insn) { return segmentAccess(insn.Info); })) reason = "segment-relative access in the prologue";
auto entryEnd = prologueEnd;
auto entryLast = p;
Copied entryTail;
Expand Down
15 changes: 15 additions & 0 deletions core/relinker/codegen/tests/RedZoneFramerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,20 @@ void framesLeavesWithoutPushes() {
require(sites[1].OriginalBytes.size() == LoadBelowRsp.size() + Ret.size() && sites[1].Body == stubBody(exitBody), "the exit stub does not run the load moved by the shift, raise rsp and return");
}

void leavesSegmentRelativeAccessesInPlace() {
const std::vector<std::uint8_t> loadFsZero{0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00};
std::vector<std::uint8_t> body = loadFsZero;
append(body, StoreBelowRsp);
append(body, LoadBelowRsp);
const auto input = image({framelessLeaf({}, body, Ret)});
Codegen::RedZoneFrameReport report;
std::vector<Codegen::RedZoneDisplacement> displacements;
const auto sites = Codegen::FrameRedZoneFunctions(input.bytes, input.segments, input.functions, {}, report, displacements);
const auto access = Base + 0x40;
require(std::none_of(sites.begin(), sites.end(), [&](const Codegen::TrampolineSite& site) { return site.Address < access + loadFsZero.size() && access < site.Address + site.Length; }), "an fs-relative load was moved into a stub, where the Windows TLS rewrite does not reach it");
require(report.Framed == 0 && !report.Skipped.empty(), "a leaf whose only entry site would move an fs-relative load was not reported as skipped");
}

void skipsFragmentsThatPopWithoutPushing() {
std::vector<std::uint8_t> body = StoreBelowRsp;
append(body, LoadBelowRsp);
Expand Down Expand Up @@ -1286,6 +1300,7 @@ int main() {
widensShortProloguesAndExits();
widensAShortFramePointerPrologue();
framesLeavesWithoutPushes();
leavesSegmentRelativeAccessesInPlace();
skipsFragmentsThatPopWithoutPushing();
framesLeavesEnteredByATailJump();
framesLeavesReadingAh();
Expand Down
Loading