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

@ -11,11 +11,11 @@ public class PushImm32Handler : InstructionHandler
/// <param name="codeBuffer">The buffer containing the code to decode</param> /// <param name="codeBuffer">The buffer containing the code to decode</param>
/// <param name="decoder">The instruction decoder that owns this handler</param> /// <param name="decoder">The instruction decoder that owns this handler</param>
/// <param name="length">The length of the buffer</param> /// <param name="length">The length of the buffer</param>
public PushImm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length) public PushImm32Handler(byte[] codeBuffer, InstructionDecoder decoder, int length)
: 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 PushImm32Handler : InstructionHandler
{ {
return opcode == 0x68; return opcode == 0x68;
} }
/// <summary> /// <summary>
/// Decodes a PUSH imm32 instruction /// Decodes a PUSH imm32 instruction
/// </summary> /// </summary>
@ -37,9 +37,14 @@ 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();
// Set the operands with 8-digit padding to match test expectations // Set the operands with 8-digit padding to match test expectations
instruction.Operands = $"0x{imm32:X8}"; instruction.Operands = $"0x{imm32:X8}";

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
{ {