Ghidra is a software reverse engineering (SRE) framework
GP-0: Fixing StaticMappingServiceTest w/ some API improvements.
Dan56559fe62a24
diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/emulation/ReadsTargetMemoryPcodeExecutorState.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/emulation/ReadsTargetMemoryPcodeExecutorState.javaindex 2fdb45e6d9..808afb8bb8 100644--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/emulation/ReadsTargetMemoryPcodeExecutorState.java+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/emulation/ReadsTargetMemoryPcodeExecutorState.java@@ -17,9 +17,8 @@ package ghidra.app.plugin.core.debug.service.emulation;import java.util.Map.Entry;-import org.apache.commons.lang3.tuple.Pair;-import ghidra.app.services.DebuggerStaticMappingService;+import ghidra.app.services.DebuggerStaticMappingService.ShiftAndAddressSetView;import ghidra.app.services.TraceRecorder;import ghidra.framework.plugintool.PluginTool;import ghidra.program.model.address.*;@@ -72,17 +71,17 @@ public class ReadsTargetMemoryPcodeExecutorStateDebuggerStaticMappingService mappingService =tool.getService(DebuggerStaticMappingService.class);byte[] data = new byte[4096];- for (Entry<Program, Pair<Long, AddressSetView>> ent : mappingService+ for (Entry<Program, ShiftAndAddressSetView> ent : mappingService.getOpenMappedViews(trace, unknown, snap).entrySet()) {Program program = ent.getKey();- Pair<Long, AddressSetView> pair = ent.getValue();+ ShiftAndAddressSetView shifted = ent.getValue();Msg.warn(this,"Filling in unknown trace memory in emulator using mapped image: " +- program + ": " + pair.getRight());- long shift = pair.getLeft();+ program + ": " + shifted.getAddressSetView());+ long shift = shifted.getShift();Memory memory = program.getMemory();- for (AddressRange rng : pair.getRight()) {+ for (AddressRange rng : shifted.getAddressSetView()) {long lower = rng.getMinAddress().getOffset();long fullLen = rng.getLength();while (fullLen > 0) {diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/modules/DebuggerStaticMappingServicePlugin.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/modules/DebuggerStaticMappingServicePlugin.javaindex b0f89874de..e51bc4fc23 100644--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/modules/DebuggerStaticMappingServicePlugin.java+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/modules/DebuggerStaticMappingServicePlugin.java@@ -23,8 +23,6 @@ import java.util.Map.Entry;import java.util.stream.Collectors;import org.apache.commons.lang3.ArrayUtils;-import org.apache.commons.lang3.tuple.ImmutablePair;-import org.apache.commons.lang3.tuple.Pair;import com.google.common.collect.Range;@@ -286,7 +284,7 @@ public class DebuggerStaticMappingServicePlugin extends Pluginprivate Program program;private AddressRange staticRange;- private Long shift;+ private Long shift; // from static image to tracepublic MappingEntry(TraceStaticMapping mapping) {this.mapping = mapping;@@ -568,7 +566,7 @@ public class DebuggerStaticMappingServicePlugin extends Plugin}protected void collectOpenMappedPrograms(AddressRange rng, Range<Long> span,- Map<Program, Pair<Long, AddressSetView>> result) {+ Map<Program, ShiftAndAddressSetView> result) {TraceAddressSnapRange tatr = new ImmutableTraceAddressSnapRange(rng, span);for (Entry<TraceAddressSnapRange, MappingEntry> out : outbound.entrySet()) {MappingEntry me = out.getValue();@@ -579,15 +577,15 @@ public class DebuggerStaticMappingServicePlugin extends Plugincontinue;}- Pair<Long, AddressSetView> set = result.computeIfAbsent(me.program,- p -> new ImmutablePair<>(me.shift, new AddressSet()));- ((AddressSet) set.getRight()).add(me.mapTraceRangeToProgram(rng));+ ShiftAndAddressSetView set = result.computeIfAbsent(me.program,+ p -> new ShiftAndAddressSetView(-me.shift, new AddressSet()));+ ((AddressSet) set.getAddressSetView()).add(me.mapTraceRangeToProgram(rng));}}- public Map<Program, Pair<Long, AddressSetView>> getOpenMappedViews(AddressSetView set,+ public Map<Program, ShiftAndAddressSetView> getOpenMappedViews(AddressSetView set,Range<Long> span) {- Map<Program, Pair<Long, AddressSetView>> result = new HashMap<>();+ Map<Program, ShiftAndAddressSetView> result = new HashMap<>();for (AddressRange rng : set) {collectOpenMappedPrograms(rng, span, result);}@@ -718,7 +716,7 @@ public class DebuggerStaticMappingServicePlugin extends Plugin}protected void collectOpenMappedViews(AddressRange rng,- Map<TraceSnap, Pair<Long, AddressSetView>> result) {+ Map<TraceSnap, ShiftAndAddressSetView> result) {for (Entry<MappingEntry, Address> inPreceeding : inbound.headMapByValue(rng.getMaxAddress(), true).entrySet()) {Address start = inPreceeding.getValue();@@ -729,14 +727,14 @@ public class DebuggerStaticMappingServicePlugin extends Pluginif (!me.isInProgramRange(rng)) {continue;}- Pair<Long, AddressSetView> set = result.computeIfAbsent(me.getTraceSnap(),- p -> new ImmutablePair<>(me.shift, new AddressSet()));- ((AddressSet) set.getRight()).add(me.mapProgramRangeToTrace(rng));+ ShiftAndAddressSetView set = result.computeIfAbsent(me.getTraceSnap(),+ p -> new ShiftAndAddressSetView(me.shift, new AddressSet()));+ ((AddressSet) set.getAddressSetView()).add(me.mapProgramRangeToTrace(rng));}}- public Map<TraceSnap, Pair<Long, AddressSetView>> getOpenMappedViews(AddressSetView set) {- Map<TraceSnap, Pair<Long, AddressSetView>> result = new HashMap<>();+ public Map<TraceSnap, ShiftAndAddressSetView> getOpenMappedViews(AddressSetView set) {+ Map<TraceSnap, ShiftAndAddressSetView> result = new HashMap<>();for (AddressRange rng : set) {collectOpenMappedViews(rng, result);}@@ -1156,7 +1154,7 @@ public class DebuggerStaticMappingServicePlugin extends Plugin}@Override- public Map<Program, Pair<Long, AddressSetView>> getOpenMappedViews(Trace trace,+ public Map<Program, ShiftAndAddressSetView> getOpenMappedViews(Trace trace,AddressSetView set,long snap) {InfoPerTrace info = requireTrackedInfo(trace);@@ -1167,7 +1165,7 @@ public class DebuggerStaticMappingServicePlugin extends Plugin}@Override- public Map<TraceSnap, Pair<Long, AddressSetView>> getOpenMappedViews(Program program,+ public Map<TraceSnap, ShiftAndAddressSetView> getOpenMappedViews(Program program,AddressSetView set) {InfoPerProgram info = requireTrackedInfo(program);if (info == null) {diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/services/DebuggerStaticMappingService.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/services/DebuggerStaticMappingService.javaindex 3a30f33cc2..de13a97719 100644--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/services/DebuggerStaticMappingService.java+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/services/DebuggerStaticMappingService.java@@ -18,8 +18,6 @@ package ghidra.app.services;import java.util.*;import java.util.stream.Collectors;-import org.apache.commons.lang3.tuple.Pair;-import com.google.common.collect.Range;import ghidra.framework.model.DomainFile;@@ -488,6 +486,45 @@ public interface DebuggerStaticMappingService {}}+ /**+ * A {@code (shift,view)} pair for describing sets of mapped addresses+ */+ public class ShiftAndAddressSetView {+ private final long shift;+ private final AddressSetView view;++ public ShiftAndAddressSetView(long shift, AddressSetView view) {+ this.shift = shift;+ this.view = view;+ }++ /**+ * Get the shift from the source address set to this address set+ *+ * <p>+ * The meaning depends on what returned this view. If this view is the "static" set, then+ * this shift describes what was added to the offset of the "dynamic" address to get a+ * particular address in this set. Note that since not all addresses from the requested+ * source set may have been mapped, you cannot simply compare min addresses to obtain this+ * shift. To "map back" to the source address from a destination address in this set,+ * <em>subtract</em> this shift.+ *+ * @return the shift+ */+ public long getShift() {+ return shift;+ }++ /**+ * Get the destination address set view as mapped from the source address set+ *+ * @return the address set+ */+ public AddressSetView getAddressSetView() {+ return view;+ }+ }+/*** Add a static mapping (relocation) from the given trace to the given program*@@ -639,7 +676,7 @@ public interface DebuggerStaticMappingService {* @param snap the source snap* @return a map of destination programs to corresponding computed destination address sets*/- Map<Program, Pair<Long, AddressSetView>> getOpenMappedViews(Trace trace,+ Map<Program, ShiftAndAddressSetView> getOpenMappedViews(Trace trace,AddressSetView set, long snap);/**@@ -649,7 +686,7 @@ public interface DebuggerStaticMappingService {* @param set the destination address set, from which we are mapping back* @return a map of source traces to corresponding computed source address sets*/- Map<TraceSnap, Pair<Long, AddressSetView>> getOpenMappedViews(Program program,+ Map<TraceSnap, ShiftAndAddressSetView> getOpenMappedViews(Program program,AddressSetView set);/**diff --git a/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/service/modules/DebuggerStaticMappingServiceTest.java b/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/service/modules/DebuggerStaticMappingServiceTest.javaindex 600ba3c2ae..d2fe5d5554 100644--- a/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/service/modules/DebuggerStaticMappingServiceTest.java+++ b/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/service/modules/DebuggerStaticMappingServiceTest.java@@ -20,7 +20,6 @@ import static org.junit.Assert.*;import java.io.File;import java.util.*;-import org.apache.commons.lang3.tuple.Pair;import org.junit.Before;import org.junit.Test;@@ -28,6 +27,7 @@ import com.google.common.collect.Range;import ghidra.app.plugin.core.debug.gui.AbstractGhidraHeadedDebuggerGUITest;import ghidra.app.services.DebuggerStaticMappingService;+import ghidra.app.services.DebuggerStaticMappingService.ShiftAndAddressSetView;import ghidra.framework.model.DomainFile;import ghidra.program.model.address.*;import ghidra.program.model.listing.Program;@@ -339,7 +339,7 @@ public class DebuggerStaticMappingServiceTest extends AbstractGhidraHeadedDebuggpublic void testAddMappingThenTranslateTraceViewToStaticEmpty() throws Exception {addMapping();- Map<Program, Pair<Long, AddressSetView>> views =+ Map<Program, ShiftAndAddressSetView> views =mappingService.getOpenMappedViews(tb.trace, new AddressSet(), 0);assertTrue(views.isEmpty());}@@ -360,12 +360,12 @@ public class DebuggerStaticMappingServiceTest extends AbstractGhidraHeadedDebugg// Afterset.add(dynSpace.getAddress(0xbadbadbadL), dynSpace.getAddress(0xbadbadbadL + 0xff));- Map<Program, Pair<Long, AddressSetView>> views =+ Map<Program, ShiftAndAddressSetView> views =mappingService.getOpenMappedViews(tb.trace, set, 0);assertEquals(1, views.size());- Pair<Long, AddressSetView> pair = views.get(program);- assertEquals(0x100000, pair.getLeft().longValue());- AddressSetView inStatic = pair.getRight();+ ShiftAndAddressSetView shifted = views.get(program);+ assertEquals(0x100000, shifted.getShift());+ AddressSetView inStatic = shifted.getAddressSetView();assertEquals(3, inStatic.getNumAddressRanges());AddressSet expected = new AddressSet();expected.add(stSpace.getAddress(0x00200000), stSpace.getAddress(0x002000ff));@@ -380,7 +380,7 @@ public class DebuggerStaticMappingServiceTest extends AbstractGhidraHeadedDebuggcopyTrace();add2ndMapping();- Map<TraceSnap, Pair<Long, AddressSetView>> views =+ Map<TraceSnap, ShiftAndAddressSetView> views =mappingService.getOpenMappedViews(program, new AddressSet());assertTrue(views.isEmpty());}@@ -403,13 +403,15 @@ public class DebuggerStaticMappingServiceTest extends AbstractGhidraHeadedDebugg// Afterset.add(stSpace.getAddress(0xbadbadbadL), stSpace.getAddress(0xbadbadbadL + 0xff));- Map<TraceSnap, Pair<Long, AddressSetView>> views =+ Map<TraceSnap, ShiftAndAddressSetView> views =mappingService.getOpenMappedViews(program, set);Msg.info(this, views);assertEquals(2, views.size());- AddressSetView in1st = views.get(new DefaultTraceSnap(tb.trace, 0)).getRight();+ ShiftAndAddressSetView shifted1 = views.get(new DefaultTraceSnap(tb.trace, 0));+ assertEquals(-0x100000, shifted1.getShift());+ AddressSetView in1st = shifted1.getAddressSetView();assertEquals(5, in1st.getNumAddressRanges());- AddressSetView in2nd = views.get(new DefaultTraceSnap(copy, 0)).getRight();+ AddressSetView in2nd = views.get(new DefaultTraceSnap(copy, 0)).getAddressSetView();assertEquals(3, in2nd.getNumAddressRanges());AddressSet expectedIn1st = new AddressSet();