From e8f73a5531520443c60ccfe8c240eed3697b46d6 Mon Sep 17 00:00:00 2001 From: Oguzhan Ozdemir <48760055+oguzhanozfe@users.noreply.github.com> Date: Wed, 7 Oct 2026 19:00:37 +0300 Subject: [PATCH] fix(relinker): decode the 64-bit address of MOV moffs instructions A0-A3 (MOV AL/eAX/rAX to and from moffs) have no ModRM byte and carry an 8-byte absolute address in 64-bit mode, or 4 bytes with a 0x67 prefix. The decoder added nothing for the address, so the linear scanner decoded the address bytes as code. Co-Authored-By: Claude Opus 5.5 --- .../codegen/include/codegen/x86/X64OpcodeConstants.hpp | 2 ++ core/relinker/codegen/src/x86/X64InstructionDecoder.cpp | 9 ++++++++- core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/core/relinker/codegen/include/codegen/x86/X64OpcodeConstants.hpp b/core/relinker/codegen/include/codegen/x86/X64OpcodeConstants.hpp index 022721a23..265725367 100644 --- a/core/relinker/codegen/include/codegen/x86/X64OpcodeConstants.hpp +++ b/core/relinker/codegen/include/codegen/x86/X64OpcodeConstants.hpp @@ -110,6 +110,8 @@ inline constexpr std::uint8_t OneByteInt = 0xCD; inline constexpr std::uint8_t OneByteRetImm16 = 0xC2; inline constexpr std::uint8_t OneByteRetFarImm16 = 0xCA; inline constexpr std::uint8_t OneByteEnter = 0xC8; +inline constexpr std::uint8_t OneByteMovMoffsMin = 0xA0; +inline constexpr std::uint8_t OneByteMovMoffsMax = 0xA3; inline constexpr std::uint8_t TwoByteJccRel32Min = 0x80; inline constexpr std::uint8_t TwoByteJccRel32Max = 0x8F; diff --git a/core/relinker/codegen/src/x86/X64InstructionDecoder.cpp b/core/relinker/codegen/src/x86/X64InstructionDecoder.cpp index 065c7f9dd..c87130389 100644 --- a/core/relinker/codegen/src/x86/X64InstructionDecoder.cpp +++ b/core/relinker/codegen/src/x86/X64InstructionDecoder.cpp @@ -12,6 +12,7 @@ std::size_t X64InstructionDecoder::Decode(const std::uint8_t* data, std::size_t bool rexPresent = false; std::uint8_t rex = 0; bool operandSizeOverride = false; + bool addressSizeOverride = false; bool repnePrefix = false; while (pos < available) { @@ -28,7 +29,9 @@ std::size_t X64InstructionDecoder::Decode(const std::uint8_t* data, std::size_t repnePrefix = true; } else if (b == PrefixOperandSize) { operandSizeOverride = true; - } else if (b != PrefixLock && b != PrefixRep && b != PrefixAddressSize && + } else if (b == PrefixAddressSize) { + addressSizeOverride = true; + } else if (b != PrefixLock && b != PrefixRep && b != PrefixSegCs && b != PrefixSegSs && b != PrefixSegDs && b != PrefixSegEs && b != PrefixSegFs && b != PrefixSegGs) { break; @@ -203,6 +206,10 @@ std::size_t X64InstructionDecoder::Decode(const std::uint8_t* data, std::size_t if (opcode == OneByteEnter) { immediateSize = ImmSizeEnter; } + + if (opcode >= OneByteMovMoffsMin && opcode <= OneByteMovMoffsMax) { + immediateSize = addressSizeOverride ? ImmSize32 : ImmSize64; + } } else { if (opcode == TwoByteExtrqInsertqImm8Imm8) { if (!operandSizeOverride && !repnePrefix) { diff --git a/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp b/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp index c4fab89f4..9bee4fd46 100644 --- a/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp +++ b/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp @@ -124,6 +124,8 @@ void decoderLengths() { {0x66, 0x0F, 0xC4, 0xC0, 0x01}, {0xC2, 0x08, 0x00}, {0xC8, 0x10, 0x00, 0x00}, {0xF3, 0x0F, 0x2B, 0x07}, {0xF2, 0x44, 0x0F, 0x2B, 0x4C, 0x24, 0x10}, {0x0F, 0x01, 0xFA}, {0x0F, 0xB9, 0x00}, {0x41, 0x0F, 0xBB, 0xF7}, {0x0F, 0xBB, 0x47, 0x08}, + {0xA0, 1, 2, 3, 4, 5, 6, 7, 8}, {0x48, 0xA1, 1, 2, 3, 4, 5, 6, 7, 8}, {0xA2, 1, 2, 3, 4, 5, 6, 7, 8}, + {0x64, 0x48, 0xA3, 1, 2, 3, 4, 5, 6, 7, 8}, {0x67, 0xA1, 1, 2, 3, 4}, {0x48, 0x67, 0xA3, 1, 2, 3, 4}, {0x0F, 0x38, 0xCB, 0xCA}, {0x45, 0x0F, 0x38, 0xCC, 0xE1}, {0x0F, 0x38, 0xCD, 0x08}, {0x0F, 0x38, 0xCB, 0x0D, 0x10, 0x00, 0x00, 0x00}, {0x0F, 0x3A, 0xCC, 0xD5, 0x03}, {0x45, 0x0F, 0x3A, 0xCC, 0xE1, 0x00}, {0x0F, 0x3A, 0xCC, 0x08, 0x01}, {0x0F, 0x38, 0xC8, 0xCA}, {0x0F, 0x38, 0xC9, 0xCA}, {0x0F, 0x38, 0xCA, 0x08}, {0x48, 0x66, 0xB8, 0x34, 0x12}, {0x66, 0x48, 0xB8, 1, 2, 3, 4, 5, 6, 7, 8}, {0x41, 0x48, 0xB8, 1, 2, 3, 4, 5, 6, 7, 8},