Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-7360: Adjustments to delay-slot tests: MIPS assembly.

Dan6d5318dddc08
diff --git a/Ghidra/Framework/Emulation/src/main/java/ghidra/pcode/emu/jit/decode/DecoderExecutor.java b/Ghidra/Framework/Emulation/src/main/java/ghidra/pcode/emu/jit/decode/DecoderExecutor.java
index 2fa3a5a722..ce2c78d2dc 100644
--- a/Ghidra/Framework/Emulation/src/main/java/ghidra/pcode/emu/jit/decode/DecoderExecutor.java
+++ b/Ghidra/Framework/Emulation/src/main/java/ghidra/pcode/emu/jit/decode/DecoderExecutor.java
@@ -84,10 +84,7 @@ class DecoderExecutor extends FoldingExecutor implements DisassemblerContextAdap
final AddrCtx at;
private PseudoInstruction instruction;
- /**
- * The length of {@link #instruction}, including any delay-slotted instructions
- */
- private int lengthWithDelays;
+ private int lengthWithDelays; // If no delays, this is the length of the one instruction
private final Map<PcodeFrame, NopPcodeOp> termNopsPerFrame = new HashMap<>();
private RegisterValue flow;
@@ -196,9 +193,7 @@ class DecoderExecutor extends FoldingExecutor implements DisassemblerContextAdap
public PseudoInstruction decodeInstruction() {
PseudoInstruction instruction = stride.decoder.decodeInstruction(at.address, at.rvCtx);
setInstruction(instruction);
- if (instruction.getDelaySlotDepth() > 0) {
- lengthWithDelays = stride.decoder.getLastLengthWithDelays();
- }
+ lengthWithDelays = stride.decoder.getLastLengthWithDelays();
return instruction;
}
@@ -556,7 +551,8 @@ class DecoderExecutor extends FoldingExecutor implements DisassemblerContextAdap
* Compute the fall-through address
* <p>
* This computes the "next" address whether or not the instruction actually has fall through.
- * The caller should check for fall through first.
+ * The caller should check for fall through first. If the instruction has a delay slots, this
+ * will compute the address after the <em>last</em> delay slot.
*
* @return the next address
* @implNote If no instruction was actually decoded during this step, and the decoder is asking
@@ -568,15 +564,7 @@ class DecoderExecutor extends FoldingExecutor implements DisassemblerContextAdap
*/
Address getAdvancedAddress() {
if (instruction != null) {
- /**
- * The p-code of an instruction with delay slots already includes that of its
- * delay-slotted instruction(s), so fall through must skip them, too. This mirrors
- * DefaultPcodeThread, which advances by InstructionDecoder#getLastLengthWithDelays().
- */
- if (lengthWithDelays > instruction.getLength()) {
- return instruction.getAddress().addWrap(lengthWithDelays);
- }
- return instruction.getMaxAddress().next();
+ return instruction.getAddress().addWrap(lengthWithDelays);
}
Msg.warn(this, "An inject may have forgotten control flow.");
return at.address;
diff --git a/Ghidra/Framework/Emulation/src/main/java/ghidra/pcode/emu/jit/decode/JitPassageDecoder.java b/Ghidra/Framework/Emulation/src/main/java/ghidra/pcode/emu/jit/decode/JitPassageDecoder.java
index 300a8159fd..344a1595bf 100644
--- a/Ghidra/Framework/Emulation/src/main/java/ghidra/pcode/emu/jit/decode/JitPassageDecoder.java
+++ b/Ghidra/Framework/Emulation/src/main/java/ghidra/pcode/emu/jit/decode/JitPassageDecoder.java
@@ -167,10 +167,9 @@ public class JitPassageDecoder {
}
/**
- * Get the length of the last instruction decoded by {@link #decodeInstruction(Address,
- * RegisterValue)}, including any delay-slotted instructions
+ * {@return the length of the last instruction decoded by
+ * {@link #decodeInstruction(Address, RegisterValue)}, including any delay-slotted instructions}
*
- * @return the length
* @see InstructionDecoder#getLastLengthWithDelays()
*/
public int getLastLengthWithDelays() {
diff --git a/Ghidra/Test/IntegrationTest/src/test.slow/java/ghidra/pcode/emu/AbstractPcodeEmulatorTest.java b/Ghidra/Test/IntegrationTest/src/test.slow/java/ghidra/pcode/emu/AbstractPcodeEmulatorTest.java
index c982eb7f6f..4bc2f82fd3 100644
--- a/Ghidra/Test/IntegrationTest/src/test.slow/java/ghidra/pcode/emu/AbstractPcodeEmulatorTest.java
+++ b/Ghidra/Test/IntegrationTest/src/test.slow/java/ghidra/pcode/emu/AbstractPcodeEmulatorTest.java
@@ -20,6 +20,7 @@ import static org.junit.Assert.fail;
import java.io.IOException;
import java.math.BigInteger;
+import java.util.List;
import org.junit.Before;
import org.junit.Test;
@@ -236,27 +237,30 @@ public abstract class AbstractPcodeEmulatorTest extends AbstractGTest {
arithmetic.toLong(thread.getState().getVar(rax, Reason.INSPECT), Purpose.INSPECT));
}
+ final static String BREAK_PREFIX = "<break>";
+
/**
- * Run raw MIPS code at 0x00400000 until a breakpoint at {@code brkOffset}, then return t0.
- *
- * <p>
- * The MIPS assembler cannot (yet) assemble delay-slotted instructions, so these tests take
- * encoded instructions.
+ * Run raw MIPS code at 0x00400000 until a breakpoint denoted by {@value #BREAK_PREFIX}, then
+ * return t0.
*/
- protected long runMipsToBreakpoint(int[] code, int brkOffset) throws Exception {
+ protected long runMipsToBreakpoint(List<String> lines) throws Exception {
PcodeEmulator emu = createEmulator(getLanguage(LANGID_MIPS_BE));
PcodeArithmetic<byte[]> arithmetic = emu.getArithmetic();
AddressSpace space = emu.getLanguage().getDefaultSpace();
Address entry = space.getAddress(0x00400000);
- Address brk = entry.add(brkOffset);
-
- byte[] bytes = new byte[code.length * 4];
- for (int i = 0; i < code.length; i++) {
- bytes[i * 4] = (byte) (code[i] >> 24);
- bytes[i * 4 + 1] = (byte) (code[i] >> 16);
- bytes[i * 4 + 2] = (byte) (code[i] >> 8);
- bytes[i * 4 + 3] = (byte) code[i];
+ Address brk = null;
+
+ AssemblyBuffer asm = new AssemblyBuffer(Assemblers.getAssembler(emu.getLanguage()), entry);
+ for (String l : lines) {
+ l = l.trim();
+ if (l.startsWith(BREAK_PREFIX)) {
+ brk = asm.getNext();
+ l = l.substring(BREAK_PREFIX.length()).trim();
+ }
+ asm.assemble(l);
}
+
+ byte[] bytes = asm.getBytes();
emu.getSharedState().setVar(entry, bytes.length, false, bytes);
PcodeThread<byte[]> thread = emu.newThread();
thread.overrideCounter(entry);
@@ -281,12 +285,12 @@ public abstract class AbstractPcodeEmulatorTest extends AbstractGTest {
*/
@Test
public void testDelaySlotBranchNotTaken() throws Exception {
- assertEquals(1, runMipsToBreakpoint(new int[] {
- 0x24080000, // addiu t0,zero,0x0
- 0x14000002, // bne zero,zero,0x00400010 (never taken)
- 0x25080001, // _addiu t0,t0,0x1 (delay slot)
- 0x24090000, // addiu t1,zero,0x0 <- breakpoint
- }, 0xc));
+ assertEquals(1, runMipsToBreakpoint(List.of(
+ " li t0,0x0",
+ " bne t0,zero,0x00400010", // never taken
+ " li t0,0x1", // delay slot
+ "<break> li t1,0x0" // TODO: nop
+ )));
}
/**
@@ -294,13 +298,13 @@ public abstract class AbstractPcodeEmulatorTest extends AbstractGTest {
*/
@Test
public void testDelaySlotBranchTaken() throws Exception {
- assertEquals(1, runMipsToBreakpoint(new int[] {
- 0x24080000, // addiu t0,zero,0x0
- 0x10000002, // beq zero,zero,0x00400010 (always taken)
- 0x25080001, // _addiu t0,t0,0x1 (delay slot)
- 0x25080010, // addiu t0,t0,0x10 (skipped)
- 0x24090000, // addiu t1,zero,0x0 <- breakpoint
- }, 0x10));
+ assertEquals(1, runMipsToBreakpoint(List.of(
+ " li t0,0x0",
+ " beq t0,zero,0x00400010", // always taken
+ " li t0,0x1", // delay slot, executed
+ " addiu t0,t0,0x10", // skipped
+ "<break> li t1,0x0" // TODO: nop
+ )));
}
@Test