From a4cea1f25b8c4cf9e05e43e5b1799fdbefaa1d07 Mon Sep 17 00:00:00 2001 From: Ryongen Date: Wed, 25 Nov 2026 21:26:28 +0100 Subject: [PATCH] fix(relinker): keep fs and gs accesses out of red-zone stubs The Windows TLS rewrite patches fs: accesses in place before the trampolines are written, so a framed entry that moved one failed the byte check and its stub copy was never rewritten. --- core/relinker/codegen/src/x86/RedZoneFramer.cpp | 10 ++++++++-- .../relinker/codegen/tests/RedZoneFramerTests.cpp | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/core/relinker/codegen/src/x86/RedZoneFramer.cpp b/core/relinker/codegen/src/x86/RedZoneFramer.cpp index f2e588bf61c..86ad070271b 100644 --- a/core/relinker/codegen/src/x86/RedZoneFramer.cpp +++ b/core/relinker/codegen/src/x86/RedZoneFramer.cpp @@ -41,6 +41,10 @@ struct Insn { DecodedInstructionInfo Info; }; +bool segmentAccess(const DecodedInstructionInfo& info) { + return info.HasMemoryOperand && (info.SegmentPrefix == 0x64 || info.SegmentPrefix == 0x65); +} + std::optional 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(code[0] - 0x50); if (insn.Length == 2 && code[0] == 0x41 && code[1] >= 0x50 && code[1] <= 0x57) return static_cast(code[1] - 0x50 + 8); @@ -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; @@ -682,6 +685,7 @@ void frameSegment( constexpr std::int64_t shift = kRelocatedShift; if (bodyEnd - static_cast(start) < static_cast(kJumpBytes)) return "leaf shorter than a jump"; if (overlapsExisting(start, static_cast(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 { if (target < static_cast(start) || target >= bodyEnd) return std::nullopt; @@ -1118,6 +1122,7 @@ void frameSegment( std::vector siteEdits; const auto copy = [&](const std::size_t k) -> std::optional { 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; @@ -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(p), [](const Insn& insn) { return segmentAccess(insn.Info); })) reason = "segment-relative access in the prologue"; auto entryEnd = prologueEnd; auto entryLast = p; Copied entryTail; diff --git a/core/relinker/codegen/tests/RedZoneFramerTests.cpp b/core/relinker/codegen/tests/RedZoneFramerTests.cpp index e8d7e21b506..1175c0a8b6b 100644 --- a/core/relinker/codegen/tests/RedZoneFramerTests.cpp +++ b/core/relinker/codegen/tests/RedZoneFramerTests.cpp @@ -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 loadFsZero{0x64, 0x48, 0x8b, 0x04, 0x25, 0x00, 0x00, 0x00, 0x00}; + std::vector body = loadFsZero; + append(body, StoreBelowRsp); + append(body, LoadBelowRsp); + const auto input = image({framelessLeaf({}, body, Ret)}); + Codegen::RedZoneFrameReport report; + std::vector 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 body = StoreBelowRsp; append(body, LoadBelowRsp); @@ -1286,6 +1300,7 @@ int main() { widensShortProloguesAndExits(); widensAShortFramePointerPrologue(); framesLeavesWithoutPushes(); + leavesSegmentRelativeAccessesInPlace(); skipsFragmentsThatPopWithoutPushing(); framesLeavesEnteredByATailJump(); framesLeavesReadingAh();