GP-6654: Fix state.get/setVar(size=0)

This commit is contained in:
Dan
2026-04-21 15:03:26 +00:00
parent 9c066df19d
commit 16f720139f
6 changed files with 110 additions and 0 deletions

View File

@@ -761,6 +761,9 @@ public enum TraceEmulationIntegration {
value)) {
return;
}
if (length == 0) {
return;
}
Address end = address.addWrap(length - 1);
if (address.compareTo(end) <= 0) {
written.add(address, end);

View File

@@ -32,6 +32,7 @@ import ghidra.pcode.emu.PcodeEmulator;
import ghidra.pcode.emu.PcodeThread;
import ghidra.pcode.exec.*;
import ghidra.pcode.exec.PcodeExecutorStatePiece.Reason;
import ghidra.pcode.exec.trace.TraceEmulationIntegration.TraceWriter;
import ghidra.pcode.exec.trace.TraceEmulationIntegration.Writer;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressRangeImpl;
@@ -55,6 +56,74 @@ public class BytesTracePcodeEmulatorTest extends AbstractTracePcodeEmulatorTest
return new PcodeEmulator(platform.getLanguage(), writer.callbacks());
}
@Test
public void testSetVarSize0() throws Throwable {
try (ToyDBTraceBuilder tb = new ToyDBTraceBuilder("Test", "x86:LE:64:default")) {
initTrace(tb, """
RIP = 0x00400000;
""",
List.of());
Writer writer = createWriter(tb.host, 0);
PcodeEmulator emu = createEmulator(tb.host, writer);
emu.getSharedState().setVar(tb.addr(0x00400000), 0, false, tb.arr());
TraceWriter tw = (TraceWriter) writer;
assertEquals(tb.set(), tw.memWritten);
}
}
@Test
public void testGetVarSize0Uninit() throws Throwable {
try (ToyDBTraceBuilder tb = new ToyDBTraceBuilder("Test", "x86:LE:64:default")) {
initTrace(tb, """
RIP = 0x00400000;
""",
List.of());
Writer writer = createWriter(tb.host, 0);
PcodeEmulator emu = createEmulator(tb.host, writer);
PcodeExecutorState<byte[]> state = emu.getSharedState();
Address addr = tb.addr(0x00400000);
byte[] result = state.getVar(addr, 0, false, Reason.EXECUTE_READ);
assertArrayEquals(new byte[0], result);
assertNull(state.getNextEntryInternal(addr.getAddressSpace(), 0));
}
}
@Test
public void testGetVarSize0Init() throws Throwable {
try (ToyDBTraceBuilder tb = new ToyDBTraceBuilder("Test", "x86:LE:64:default")) {
// Put some known data in a place likely to show in erroneous "uninitialized" set
initTrace(tb, """
RIP = 0x00400000;
*:8 RIP = 0xdeadbeefcafeface;
""",
List.of());
Writer writer = createWriter(tb.host, 0);
PcodeEmulator emu = createEmulator(tb.host, writer);
PcodeExecutorState<byte[]> state = emu.getSharedState();
Address addr = tb.addr(0x00400000);
state.setVar(addr, 4, false, tb.arr(1, 2, 3, 4));
var entry = state.getNextEntryInternal(addr.getAddressSpace(), 0);
assertEquals(0x00400000L, entry.getKey().longValue());
assertArrayEquals(tb.arr(1, 2, 3, 4), entry.getValue());
assertNull(state.getNextEntryInternal(addr.getAddressSpace(), 0x00400004));
byte[] result = state.getVar(addr, 0, false, Reason.EXECUTE_READ);
assertArrayEquals(tb.arr(), result);
entry = state.getNextEntryInternal(addr.getAddressSpace(), 0);
assertEquals(0x00400000L, entry.getKey().longValue());
assertArrayEquals(tb.arr(1, 2, 3, 4), entry.getValue());
assertNull(state.getNextEntryInternal(addr.getAddressSpace(), 0x00400004));
}
}
/**
* Test a single instruction
*