Ghidra is a software reverse engineering (SRE) framework
GP-6961: Ensure bytes are read from static only if 'never known' in trace.
Dan9f0226c72bff
diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/emulation/DebuggerEmulationIntegration.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/emulation/DebuggerEmulationIntegration.javaindex a38f9d098e..a50b26e1a3 100644--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/emulation/DebuggerEmulationIntegration.java+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/emulation/DebuggerEmulationIntegration.java@@ -153,6 +153,7 @@ public enum DebuggerEmulationIntegration {return super.readUninitialized(acc, thread, piece, set);}}+ unknown = unknown.subtract(memAcc.intersectViewKnown(unknown, true));AddressSetView remains = memAcc.readFromStaticImages(piece, unknown);/*** In this case, readFromStaticImages has in fact modified the state piece, so we todiff --git a/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/service/emulation/DebuggerEmulationServiceTest.java b/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/service/emulation/DebuggerEmulationServiceTest.javaindex af0c8bf635..0b7f4cdf69 100644--- a/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/service/emulation/DebuggerEmulationServiceTest.java+++ b/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/service/emulation/DebuggerEmulationServiceTest.java@@ -38,6 +38,7 @@ import ghidra.app.plugin.core.debug.service.platform.DebuggerPlatformServicePlugimport ghidra.app.services.DebuggerEmulationService.EmulationResult;import ghidra.app.services.DebuggerStaticMappingService;import ghidra.app.services.DebuggerTraceManagerService.ActivationCause;+import ghidra.debug.api.emulation.PcodeDebuggerAccess;import ghidra.debug.api.platform.DebuggerPlatformMapper;import ghidra.debug.api.tracemgr.DebuggerCoordinates;import ghidra.pcode.emu.*;@@ -52,6 +53,7 @@ import ghidra.program.model.listing.ProgramContext;import ghidra.program.model.mem.Memory;import ghidra.program.model.mem.MemoryBlock;import ghidra.program.util.ProgramLocation;+import ghidra.trace.database.ToyDBTraceBuilder;import ghidra.trace.database.ToyDBTraceBuilder.ToySchemaBuilder;import ghidra.trace.model.*;import ghidra.trace.model.breakpoint.TraceBreakpointKind.CommonSet;@@ -1006,4 +1008,80 @@ public class DebuggerEmulationServiceTest extends AbstractGhidraHeadedDebuggerTePathFilter stackFilter = threadSchema.searchFor(TraceStack.class, false);assertNotNull("Non-unique Stack", stackFilter.getSingletonPath());}++ /**+ * Tests that debugger-integrated emulators will read through to the mapped program(s) when+ * appropriate.+ * <p>+ * In this test, the case where bytes are read through is tested by virtue of instruction+ * decode. The emulator will ask for bytes from the state, the state will examine the trace and+ * eventually ask to have those bytes copied in from the mapped program, resulting in+ * successfully decoded instructions as assembled into the program.+ * <p>+ * The case where bytes MUST NOT be read through is tested via the load instruction, where we've+ * placed bytes into the trace itself at snapshot 0. The access object is set to source bytes+ * from snapshot 1. Even though the bytes are only KNOWN in snapshot 0, that's still "more+ * recent" than the static images, so we should get the trace bytes, not the static image bytes.+ *+ * @throws Exception because+ */+ @Test+ public void testReadFromStatic() throws Exception {+ createProgram();+ intoProject(program);++ Assembler asm = Assemblers.getAssembler(program);+ Memory memory = program.getMemory();+ Address addrText = addr(program, 0x00400000);+ Address addrData = addr(program, 0x00000000);+ Register regR0 = program.getRegister("r0");+ try (Transaction tx = program.openTransaction("Initialize")) {+ MemoryBlock blockText = memory.createInitializedBlock(".text", addrText, 0x1000,+ (byte) 0, monitor, false);+ blockText.setExecute(true);+ asm.assemble(addrText,+ "imm r0, #0x100",+ "load r0, [r0]");+ MemoryBlock blockData = memory.createInitializedBlock(".data", addrData, 0x1000,+ (byte) 0, monitor, false);+ blockData.putBytes(addr(program, 0x0100), arr("deadbeefcafeface"));+ }++ programManager.openProgram(program);+ waitForSwing();++ Trace trace = null;+ try {+ trace = ProgramEmulationUtils.launchEmulationTrace(program, addrText, this);+ tb = new ToyDBTraceBuilder(trace);+ }+ finally {+ if (trace != null) {+ trace.release(this);+ }+ }++ traceManager.openTrace(trace);+ waitForSwing();++ try (Transaction tx = tb.startTransaction()) {+ TraceSnapshot snapshot = tb.trace.getTimeManager().getSnapshot(0, true);+ long snap = snapshot.getKey();++ tb.trace.getMemoryManager()+ .putBytes(snap, tb.addr(0x0100), ByteBuffer.wrap(arr("600dbeef12345678")));+ }++ // Set access at one snap later+ PcodeDebuggerAccess access = new DefaultPcodeDebuggerAccess(tool, null, tb.host, 1);+ Writer writer = DebuggerEmulationIntegration.bytesDelayedWriteTrace(access);+ PcodeEmulator emu = new PcodeEmulator(access.getLanguage(), writer.callbacks());+ TraceThread traceThread = Unique.assertOne(tb.trace.getThreadManager().getAllThreads());+ PcodeThread<byte[]> thread = emu.getThread(traceThread.getPath(), true);++ thread.stepInstruction(2);++ RegisterValue rv = thread.getState().inspectRegisterValue(regR0);+ assertEquals("600dbeef12345678", rv.getUnsignedValue().toString(16));+ }}