feat(relinker): lower CLZERO for --to-intel

Lower CLZERO out of line instead of failing the relink: the 64-byte line
containing rAX (EAX with 67h) is cleared with four MOVNTDQ stores of
zero, the same non-temporal, weakly ordered store. The line address is
masked in xmm0 so RFLAGS stays untouched; xmm0 and rcx are spilled below
the red zone. Short sites share stubs through MatchSequence. CLZERO with
LOCK, an FS/GS override, a 66/F2/F3 prefix or longer than 15 bytes fails
the relink.

The decoder splits a REX followed by another prefix into a separate
instruction, which would hide a prefix that changes what CLZERO or the
next instruction does. The converter now fails the relink when such a
prefix-only fragment directly precedes an AMD-only instruction or would
be moved into a stub as a trailing instruction.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ljkx
2026-10-01 10:49:27 +02:00
parent c7fb2575a3
commit 09cb86b076
10 changed files with 447 additions and 13 deletions
@@ -4,6 +4,7 @@
#include <codegen/x86/Amd64OnlySubstitutionTable.hpp>
#include <codegen/x86/X64InstructionDecoder.hpp>
#include <codegen/x86/X64InstructionRewriter.hpp>
#include <codegen/x86/X64OpcodeConstants.hpp>
#include <codegen/x86/IAmd64OnlyInstructionMatcher.hpp>
#include <algorithm>
#include <memory>
@@ -16,6 +17,15 @@ namespace Codegen {
namespace {
bool _isPrefixRun(const std::uint8_t* data, const std::size_t length) {
using namespace X64OpcodeConstants;
return std::all_of(data, data + length, [](const std::uint8_t b) {
return (b >= RexMin && b <= RexMax) || b == PrefixLock || b == PrefixRepne || b == PrefixRep ||
b == PrefixSegCs || b == PrefixSegSs || b == PrefixSegDs || b == PrefixSegEs ||
b == PrefixSegFs || b == PrefixSegGs || b == PrefixOperandSize || b == PrefixAddressSize;
});
}
template<typename TOperation>
auto _atFileOffset(const Domain::FileByteOffset base, const TOperation& operation) {
try {
@@ -110,6 +120,11 @@ void Amd64OnlyConverter::_convertSegment(
for (const auto& item : pending) {
if (consumed.contains(item.Index))
continue;
if (item.Index > 0) {
const auto& previous = matches[item.Index - 1];
if (previous.Offset + previous.Length == item.Instruction.Offset && _isPrefixRun(seg.data() + previous.Offset, previous.Length))
throw CodegenException("AMD-only instruction follows prefixes the decoder split from it", ph.Offset + previous.Offset);
}
const auto& match = item.Instruction;
const auto& substitution = item.Substitution;
const auto fileOffset = static_cast<Domain::FileByteOffset>(ph.Offset + match.Offset);
@@ -142,7 +157,7 @@ void Amd64OnlyConverter::_convertSegment(
if (amdOnly && trailingBytes == 0) {
sequence.push_back(bytes);
consumed.insert(next);
} else if (amdOnly || info.FlowKind != ControlFlowKind::Sequential || info.HasRipRelativeDisp || info.HasBranchTarget) {
} else if (amdOnly || info.FlowKind != ControlFlowKind::Sequential || info.HasRipRelativeDisp || info.HasBranchTarget || _isPrefixRun(bytes.data(), bytes.size())) {
throw CodegenException("AMD-only instruction too short for a jump is followed by an instruction that cannot move", ph.Offset + following.Offset);
} else {
trailingBytes += following.Length;