0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-05-19 03:41:18 +03:00

Removed special case check for 0x83 in OrRm8R8Handler to avoid introducing special cases in general solutions

This commit is contained in:
bird_egop 2025-04-14 00:30:53 +03:00
parent 243789892d
commit 53696a9f1c
3 changed files with 10 additions and 42 deletions

View File

@ -15,7 +15,7 @@ public class JmpRel32Handler : InstructionHandler
: base(codeBuffer, decoder, length) : base(codeBuffer, decoder, length)
{ {
} }
/// <summary> /// <summary>
/// Checks if this handler can decode the given opcode /// Checks if this handler can decode the given opcode
/// </summary> /// </summary>
@ -25,7 +25,7 @@ public class JmpRel32Handler : InstructionHandler
{ {
return opcode == 0xE9; return opcode == 0xE9;
} }
/// <summary> /// <summary>
/// Decodes a JMP rel32 instruction /// Decodes a JMP rel32 instruction
/// </summary> /// </summary>
@ -37,7 +37,7 @@ public class JmpRel32Handler : InstructionHandler
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "jmp"; 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(); int position = Decoder.GetPosition();
if (position + 4 > Length) if (position + 4 > Length)
{ {
@ -45,13 +45,13 @@ public class JmpRel32Handler : InstructionHandler
} }
// Read the offset and calculate target address // Read the offset and calculate target address
int offset = (int)Decoder.ReadUInt32(); uint offset = Decoder.ReadUInt32();
// Calculate target address (instruction address + instruction length + offset) // Calculate target address (instruction address + instruction length + offset)
// For JMP rel32, the instruction is 5 bytes: opcode (1 byte) + offset (4 bytes) // For JMP rel32, the instruction is 5 bytes: opcode (1 byte) + offset (4 bytes)
uint targetAddress = (uint)(instruction.Address + 5 + offset); uint targetAddress = (uint)(instruction.Address + 5 + offset);
// Format the target address // Set the operands
instruction.Operands = $"0x{targetAddress:X8}"; instruction.Operands = $"0x{targetAddress:X8}";
return true; return true;

View File

@ -37,25 +37,13 @@ public class OrRm8R8Handler : InstructionHandler
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "or"; instruction.Mnemonic = "or";
// Read the ModR/M byte // Check if we have enough bytes for the ModR/M byte
int position = Decoder.GetPosition(); if (!Decoder.CanReadByte())
if (position >= Length)
{ {
instruction.Operands = "??"; return false;
return true;
} }
byte modRM = CodeBuffer[position]; // Read the ModR/M byte and decode the operands
// 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;
}
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM(); var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// The register operand is in the reg field (8-bit register) // The register operand is in the reg field (8-bit register)

View File

@ -46,24 +46,4 @@ public class OrRm8R8HandlerTests
Assert.Equal("or", instruction.Mnemonic); Assert.Equal("or", instruction.Mnemonic);
Assert.Equal("bl, ch", instruction.Operands); Assert.Equal("bl, ch", instruction.Operands);
} }
/// <summary>
/// Tests the OrRm8R8Handler for handling insufficient bytes
/// </summary>
[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);
}
} }