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

Improved PUSH handlers by moving reg field check to CanHandle and adding proper boundary checking

This commit is contained in:
bird_egop 2025-04-14 00:33:39 +03:00
parent 53696a9f1c
commit e134452eda
3 changed files with 39 additions and 17 deletions

View File

@ -37,6 +37,11 @@ public class PushImm32Handler : InstructionHandler
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "push"; instruction.Mnemonic = "push";
if(!Decoder.CanReadUInt())
{
return false;
}
// Read the immediate value // Read the immediate value
uint imm32 = Decoder.ReadUInt32(); uint imm32 = Decoder.ReadUInt32();

View File

@ -37,6 +37,11 @@ public class PushImm8Handler : InstructionHandler
// Set the mnemonic // Set the mnemonic
instruction.Mnemonic = "push"; instruction.Mnemonic = "push";
if(!Decoder.CanReadByte())
{
return false;
}
// Read the immediate value // Read the immediate value
byte imm8 = Decoder.ReadByte(); byte imm8 = Decoder.ReadByte();

View File

@ -23,7 +23,26 @@ public class PushRm32Handler : InstructionHandler
/// <returns>True if this handler can decode the opcode</returns> /// <returns>True if this handler can decode the opcode</returns>
public override bool CanHandle(byte opcode) public override bool CanHandle(byte opcode)
{ {
return opcode == 0xFF; // PUSH r/m32 is encoded as FF /6
if (opcode != 0xFF)
{
return false;
}
// Check if we have enough bytes to read the ModR/M byte
if (!Decoder.CanReadByte())
{
return false;
}
// Peek at the ModR/M byte without advancing the position
byte modRM = CodeBuffer[Decoder.GetPosition()];
// Extract the reg field (bits 3-5)
byte reg = (byte)((modRM & 0x38) >> 3);
// PUSH r/m32 is encoded as FF /6 (reg field = 6)
return reg == 6;
} }
/// <summary> /// <summary>
@ -34,9 +53,11 @@ public class PushRm32Handler : InstructionHandler
/// <returns>True if the instruction was successfully decoded</returns> /// <returns>True if the instruction was successfully decoded</returns>
public override bool Decode(byte opcode, Instruction instruction) public override bool Decode(byte opcode, Instruction instruction)
{ {
int position = Decoder.GetPosition(); // Set the mnemonic
instruction.Mnemonic = "push";
if (position >= Length) // Check if we have enough bytes for the ModR/M byte
if (!Decoder.CanReadByte())
{ {
return false; return false;
} }
@ -44,15 +65,6 @@ public class PushRm32Handler : InstructionHandler
// Read the ModR/M byte // Read the ModR/M byte
var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM(); var (mod, reg, rm, destOperand) = ModRMDecoder.ReadModRM();
// PUSH r/m32 is encoded as FF /6
if (reg != RegisterIndex.Sp)
{
return false;
}
// Set the mnemonic
instruction.Mnemonic = "push";
// For memory operands, set the operand // For memory operands, set the operand
if (mod != 3) // Memory operand if (mod != 3) // Memory operand
{ {