diff --git a/core/relinker/codegen/src/x86/X64InstructionDecoder.cpp b/core/relinker/codegen/src/x86/X64InstructionDecoder.cpp index 314a1d99b..065c7f9dd 100644 --- a/core/relinker/codegen/src/x86/X64InstructionDecoder.cpp +++ b/core/relinker/codegen/src/x86/X64InstructionDecoder.cpp @@ -352,20 +352,34 @@ DecodedInstructionInfo X64InstructionDecoder::DecodeInstruction( if (pos >= info.Length) return info; + const auto readModRm = [&](const std::size_t modrmPos) { + const std::uint8_t modrm = data[modrmPos]; + info.HasModRm = true; + info.ModRmByte = modrm; + info.ModRmRegField = (modrm >> ModRmRegShift) & ModRmRegMask; + const std::uint8_t mod = (modrm >> ModRmModShift) & ModRmModMask; + const std::uint8_t rm = modrm & ModRmRmMask; + if (mod == ModRmModIndirect && rm == ModRmRmRipRelative) { + info.HasRipRelativeDisp = true; + info.RipRelativeDispOffset = modrmPos + 1; + } + }; + std::uint8_t op = data[pos++]; bool twoByteOpcode = false; - if (op == OneByteVex2 || op == OneByteVex3) { - info.FlowKind = ControlFlowKind::Sequential; - return info; - } - if (op == EvexPrefix) { + if (op == OneByteVex2 || op == OneByteVex3 || op == EvexPrefix) { + pos += (op == OneByteVex2 ? 1 : (op == OneByteVex3 ? 2 : EvexPrefixLength - 1)) + 1; + if (pos < info.Length) + readModRm(pos); info.FlowKind = ControlFlowKind::Sequential; return info; } if (op == TwoByteOpcodeEscape && pos < info.Length) { twoByteOpcode = true; op = data[pos++]; + if (op == ThreeByteEscape38 || op == ThreeByteEscape3A) + ++pos; } info.Opcode = op; @@ -506,6 +520,7 @@ DecodedInstructionInfo X64InstructionDecoder::DecodeInstruction( op == TwoByteGrp15 || op == TwoByteXadd || op == TwoByteGrp9 || op == TwoByteNopModRm || op == TwoByteEndbr || op == TwoByteMovImm8ModRm || + op == ThreeByteEscape38 || op == ThreeByteEscape3A || (op >= TwoByteShiftImm8Min && op <= TwoByteShiftImm8Max) || op == TwoByteShldImm8 || op == TwoByteShrdImm8 || op == TwoByteShufpsImm8 || op == TwoByteShufpdImm8 || @@ -519,18 +534,8 @@ DecodedInstructionInfo X64InstructionDecoder::DecodeInstruction( hasModRm = true; } - if (hasModRm && pos < info.Length) { - std::uint8_t modrm = data[pos]; - info.HasModRm = true; - info.ModRmByte = modrm; - info.ModRmRegField = (modrm >> ModRmRegShift) & ModRmRegMask; - std::uint8_t mod = (modrm >> ModRmModShift) & ModRmModMask; - std::uint8_t rm = modrm & ModRmRmMask; - if (mod == ModRmModIndirect && rm == ModRmRmRipRelative) { - info.HasRipRelativeDisp = true; - info.RipRelativeDispOffset = pos + 1; - } - } + if (hasModRm && pos < info.Length) + readModRm(pos); info.FlowKind = ControlFlowKind::Sequential; return info; diff --git a/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp b/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp index 82bad675a..08693f5ea 100644 --- a/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp +++ b/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp @@ -29,6 +29,7 @@ #include #include #include +#include namespace { @@ -138,6 +139,25 @@ void decoderLengths() { require(last.Length == 4 && last.OpcodeOffset == 2 && last.RexPrefix == 0x48, "The REX before the opcode was not the one kept"); } +const std::vector kRipRelativeVectorLoads = { + {0xC5, 0xF9, 0x6F, 0x05, 0x10, 0x00, 0x00, 0x00}, + {0xC4, 0xE2, 0x79, 0x00, 0x05, 0x10, 0x00, 0x00, 0x00}, + {0x62, 0xF1, 0xFD, 0x08, 0x6F, 0x05, 0x10, 0x00, 0x00, 0x00}, + {0x66, 0x0F, 0x38, 0x00, 0x05, 0x10, 0x00, 0x00, 0x00}, + {0x66, 0x0F, 0x3A, 0x0F, 0x05, 0x10, 0x00, 0x00, 0x00, 0x08}}; +const std::vector kRegisterVectorOperations = { + {0xC5, 0xF9, 0x6F, 0xC1}, {0x66, 0x0F, 0x38, 0x00, 0xC1}, {0xC5, 0xF8, 0x77}, {0xC4, 0xE2, 0x79, 0x00, 0x00}}; + +void decoderRipRelative() { + const Codegen::X64InstructionDecoder decoder; + for (const auto& instruction : kRipRelativeVectorLoads) { + const auto info = decoder.DecodeInstruction(instruction.data(), instruction.size()); + require(info.Length == instruction.size() && info.HasRipRelativeDisp && read(instruction, info.RipRelativeDispOffset) == 0x10, "VEX, EVEX or three-byte RIP-relative operand was not reported"); + } + for (const auto& instruction : kRegisterVectorOperations) + require(!decoder.DecodeInstruction(instruction.data(), instruction.size()).HasRipRelativeDisp, "Vector instruction without a RIP-relative operand was reported as RIP-relative"); +} + void sse4aOperands() { const auto check = [](const Bytes& site, const bool insertq, const int dst, const int src, const int length, const int index) { const auto operands = Codegen::DecodeSse4a(site.data(), site.size()); @@ -334,6 +354,27 @@ void converterSegment() { requireFailure([&] { (void)converter->Convert(file, {segmentHeader(0x200)}); }, "Segment exceeding the file was accepted"); } +void converterRipRelativeFollower() { + const auto converter = Codegen::MakeAmd64OnlyConverter(); + const auto withFollower = [](const Bytes& following) { + Bytes file(0x300, 0xCC); + Bytes text = {0x66, 0x0F, 0x79, 0xCA}; + text.insert(text.end(), following.begin(), following.end()); + text.push_back(0xC3); + std::copy(text.begin(), text.end(), file.begin() + 0x200); + return std::pair{file, segmentHeader(text.size())}; + }; + for (const auto& following : kRipRelativeVectorLoads) { + const auto [file, header] = withFollower(following); + require(failureOffset([&] { (void)converter->Convert(file, {header}); }, "RIP-relative vector load was moved into an EXTRQ stub") == 0x204, "RIP-relative follower failure does not carry its file offset"); + } + for (const auto& following : kRegisterVectorOperations) { + const auto [file, header] = withFollower(following); + const auto result = converter->Convert(file, {header}); + require(result.Trampolines.size() == 1 && result.Trampolines[0].Length == 4 + following.size(), "Vector instruction without a RIP-relative operand was not moved into the EXTRQ stub"); + } +} + void converterSha256() { const auto converter = Codegen::MakeAmd64OnlyConverter(); Bytes file(0x300, 0xCC); @@ -780,6 +821,7 @@ void scannerZeroTail() { int main() { try { decoderLengths(); + decoderRipRelative(); sse4aOperands(); sha256Operands(); clzeroOperands(); @@ -789,6 +831,7 @@ int main() { sha256Execution(); clzeroExecution(); converterSegment(); + converterRipRelativeFollower(); converterSha256(); converterMonitorWait(); converterClzero();