From 53696a9f1c9ecc32fd3f72688d373b7956c3707a Mon Sep 17 00:00:00 2001 From: bird_egop Date: Mon, 14 Apr 2025 00:30:53 +0300 Subject: [PATCH] Removed special case check for 0x83 in OrRm8R8Handler to avoid introducing special cases in general solutions --- .../X86/Handlers/Jump/JmpRel32Handler.cs | 10 ++++----- .../X86/Handlers/Or/OrRm8R8Handler.cs | 22 +++++-------------- .../InstructionTests/OrRm8R8HandlerTests.cs | 20 ----------------- 3 files changed, 10 insertions(+), 42 deletions(-) diff --git a/X86Disassembler/X86/Handlers/Jump/JmpRel32Handler.cs b/X86Disassembler/X86/Handlers/Jump/JmpRel32Handler.cs index 9b48f7c..0b7aa8e 100644 --- a/X86Disassembler/X86/Handlers/Jump/JmpRel32Handler.cs +++ b/X86Disassembler/X86/Handlers/Jump/JmpRel32Handler.cs @@ -15,7 +15,7 @@ public class JmpRel32Handler : InstructionHandler : base(codeBuffer, decoder, length) { } - + /// /// Checks if this handler can decode the given opcode /// @@ -25,7 +25,7 @@ public class JmpRel32Handler : InstructionHandler { return opcode == 0xE9; } - + /// /// Decodes a JMP rel32 instruction /// @@ -37,7 +37,7 @@ public class JmpRel32Handler : InstructionHandler // Set the mnemonic instruction.Mnemonic = "jmp"; - // Check if we have enough bytes for the offset + // Check if we have enough bytes for the offset (4 bytes) int position = Decoder.GetPosition(); if (position + 4 > Length) { @@ -45,13 +45,13 @@ public class JmpRel32Handler : InstructionHandler } // Read the offset and calculate target address - int offset = (int)Decoder.ReadUInt32(); + uint offset = Decoder.ReadUInt32(); // Calculate target address (instruction address + instruction length + offset) // For JMP rel32, the instruction is 5 bytes: opcode (1 byte) + offset (4 bytes) uint targetAddress = (uint)(instruction.Address + 5 + offset); - // Format the target address + // Set the operands instruction.Operands = $"0x{targetAddress:X8}"; return true; diff --git a/X86Disassembler/X86/Handlers/Or/OrRm8R8Handler.cs b/X86Disassembler/X86/Handlers/Or/OrRm8R8Handler.cs index bf5b965..8e86c58 100644 --- a/X86Disassembler/X86/Handlers/Or/OrRm8R8Handler.cs +++ b/X86Disassembler/X86/Handlers/Or/OrRm8R8Handler.cs @@ -37,25 +37,13 @@ public class OrRm8R8Handler : InstructionHandler // Set the mnemonic instruction.Mnemonic = "or"; - // Read the ModR/M byte - int position = Decoder.GetPosition(); - if (position >= Length) + // Check if we have enough bytes for the ModR/M byte + if (!Decoder.CanReadByte()) { - instruction.Operands = "??"; - return true; + return false; } - - byte modRM = CodeBuffer[position]; - - // Check if the next byte is a valid ModR/M byte or potentially another opcode - // For the specific case of 0x83, it's a different instruction (ADD r/m32, imm8) - if (modRM == 0x83) - { - // This is likely the start of another instruction, not a ModR/M byte - instruction.Operands = "??"; - return true; - } - + + // Read the ModR/M byte and decode the operands var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM(); // The register operand is in the reg field (8-bit register) diff --git a/X86DisassemblerTests/InstructionTests/OrRm8R8HandlerTests.cs b/X86DisassemblerTests/InstructionTests/OrRm8R8HandlerTests.cs index b51c934..ab436b5 100644 --- a/X86DisassemblerTests/InstructionTests/OrRm8R8HandlerTests.cs +++ b/X86DisassemblerTests/InstructionTests/OrRm8R8HandlerTests.cs @@ -46,24 +46,4 @@ public class OrRm8R8HandlerTests Assert.Equal("or", instruction.Mnemonic); Assert.Equal("bl, ch", instruction.Operands); } - - /// - /// Tests the OrRm8R8Handler for handling insufficient bytes - /// - [Fact] - public void OrRm8R8Handler_HandlesInsufficientBytes_Gracefully() - { - // Arrange - // OR ?? (08) - missing ModR/M byte - byte[] codeBuffer = new byte[] { 0x08 }; - var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length); - - // Act - var instruction = decoder.DecodeInstruction(); - - // Assert - Assert.NotNull(instruction); - Assert.Equal("or", instruction.Mnemonic); - Assert.Equal("??", instruction.Operands); - } }