From 0cc03c24797ac136e50eedbf9bfae34d06f277fd Mon Sep 17 00:00:00 2001 From: bird_egop Date: Sat, 12 Apr 2025 22:16:12 +0300 Subject: [PATCH] Added test for INT3 instruction handler --- .../X86/Handlers/InstructionHandlerFactory.cs | 2 + X86Disassembler/X86/Handlers/Int3Handler.cs | 45 +++++++++++++++++++ X86DisassemblerTests/Int3InstructionTests.cs | 30 +++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 X86Disassembler/X86/Handlers/Int3Handler.cs create mode 100644 X86DisassemblerTests/Int3InstructionTests.cs diff --git a/X86Disassembler/X86/Handlers/InstructionHandlerFactory.cs b/X86Disassembler/X86/Handlers/InstructionHandlerFactory.cs index bf3c60a..5dafc28 100644 --- a/X86Disassembler/X86/Handlers/InstructionHandlerFactory.cs +++ b/X86Disassembler/X86/Handlers/InstructionHandlerFactory.cs @@ -45,6 +45,8 @@ public class InstructionHandlerFactory RegisterGroup1Handlers(); // Register specific instruction handlers + _handlers.Add(new Int3Handler(_codeBuffer, _decoder, _length)); + _handlers.Add(new RetHandler(_codeBuffer, _decoder, _length)); _handlers.Add(new RetImmHandler(_codeBuffer, _decoder, _length)); _handlers.Add(new CallRel32Handler(_codeBuffer, _decoder, _length)); diff --git a/X86Disassembler/X86/Handlers/Int3Handler.cs b/X86Disassembler/X86/Handlers/Int3Handler.cs new file mode 100644 index 0000000..d104af1 --- /dev/null +++ b/X86Disassembler/X86/Handlers/Int3Handler.cs @@ -0,0 +1,45 @@ +namespace X86Disassembler.X86.Handlers; + +/// +/// Handler for CALL rel32 instruction (0xE8) +/// +public class Int3Handler : InstructionHandler +{ + /// + /// Initializes a new instance of the CallRel32Handler class + /// + /// The buffer containing the code to decode + /// The instruction decoder that owns this handler + /// The length of the buffer + public Int3Handler(byte[] codeBuffer, InstructionDecoder decoder, int length) + : base(codeBuffer, decoder, length) + { + } + + /// + /// Checks if this handler can decode the given opcode + /// + /// The opcode to check + /// True if this handler can decode the opcode + public override bool CanHandle(byte opcode) + { + return opcode == 0xCC; + } + + /// + /// Decodes a CALL rel32 instruction + /// + /// The opcode of the instruction + /// The instruction object to populate + /// True if the instruction was successfully decoded + public override bool Decode(byte opcode, Instruction instruction) + { + // Set the mnemonic + instruction.Mnemonic = "int3"; + + // Set the operands + instruction.Operands = ""; + + return true; + } +} diff --git a/X86DisassemblerTests/Int3InstructionTests.cs b/X86DisassemblerTests/Int3InstructionTests.cs new file mode 100644 index 0000000..9be7ce5 --- /dev/null +++ b/X86DisassemblerTests/Int3InstructionTests.cs @@ -0,0 +1,30 @@ +namespace X86DisassemblerTests; + +using Xunit; +using X86Disassembler.X86; + +/// +/// Tests for INT3 instruction handler +/// +public class Int3InstructionTests +{ + /// + /// Tests the Int3Handler for decoding INT3 instruction + /// + [Fact] + public void Int3Handler_DecodesInt3_Correctly() + { + // Arrange + // INT3 (CC) + byte[] codeBuffer = new byte[] { 0xCC }; + var decoder = new InstructionDecoder(codeBuffer, codeBuffer.Length); + + // Act + var instruction = decoder.DecodeInstruction(); + + // Assert + Assert.NotNull(instruction); + Assert.Equal("int3", instruction.Mnemonic); + Assert.Equal("", instruction.Operands); + } +}