From 56559fe62a2457a94ea0726a2b229f95ac660d41 Mon Sep 17 00:00:00 2001 From: Dan <46821332+nsadeveloper789@users.noreply.github.com> Date: Wed, 7 Apr 2021 13:14:21 -0400 Subject: [PATCH] GP-0: Fixing StaticMappingServiceTest w/ some API improvements. --- .../ReadsTargetMemoryPcodeExecutorState.java | 13 +++--- .../DebuggerStaticMappingServicePlugin.java | 32 +++++++------ .../DebuggerStaticMappingService.java | 45 +++++++++++++++++-- .../DebuggerStaticMappingServiceTest.java | 22 ++++----- 4 files changed, 74 insertions(+), 38 deletions(-) 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.java index 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 ReadsTargetMemoryPcodeExecutorState DebuggerStaticMappingService mappingService = tool.getService(DebuggerStaticMappingService.class); byte[] data = new byte[4096]; - for (Entry> ent : mappingService + for (Entry ent : mappingService .getOpenMappedViews(trace, unknown, snap) .entrySet()) { Program program = ent.getKey(); - Pair 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.java index 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 Plugin private Program program; private AddressRange staticRange; - private Long shift; + private Long shift; // from static image to trace public MappingEntry(TraceStaticMapping mapping) { this.mapping = mapping; @@ -568,7 +566,7 @@ public class DebuggerStaticMappingServicePlugin extends Plugin } protected void collectOpenMappedPrograms(AddressRange rng, Range span, - Map> result) { + Map result) { TraceAddressSnapRange tatr = new ImmutableTraceAddressSnapRange(rng, span); for (Entry out : outbound.entrySet()) { MappingEntry me = out.getValue(); @@ -579,15 +577,15 @@ public class DebuggerStaticMappingServicePlugin extends Plugin continue; } - Pair 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> getOpenMappedViews(AddressSetView set, + public Map getOpenMappedViews(AddressSetView set, Range span) { - Map> result = new HashMap<>(); + Map 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> result) { + Map result) { for (Entry inPreceeding : inbound.headMapByValue( rng.getMaxAddress(), true).entrySet()) { Address start = inPreceeding.getValue(); @@ -729,14 +727,14 @@ public class DebuggerStaticMappingServicePlugin extends Plugin if (!me.isInProgramRange(rng)) { continue; } - Pair 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> getOpenMappedViews(AddressSetView set) { - Map> result = new HashMap<>(); + public Map getOpenMappedViews(AddressSetView set) { + Map result = new HashMap<>(); for (AddressRange rng : set) { collectOpenMappedViews(rng, result); } @@ -1156,7 +1154,7 @@ public class DebuggerStaticMappingServicePlugin extends Plugin } @Override - public Map> getOpenMappedViews(Trace trace, + public Map getOpenMappedViews(Trace trace, AddressSetView set, long snap) { InfoPerTrace info = requireTrackedInfo(trace); @@ -1167,7 +1165,7 @@ public class DebuggerStaticMappingServicePlugin extends Plugin } @Override - public Map> getOpenMappedViews(Program program, + public Map 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.java index 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 + * + *

+ * 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, + * subtract 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> getOpenMappedViews(Trace trace, + Map 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> getOpenMappedViews(Program program, + Map 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.java index 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 AbstractGhidraHeadedDebugg public void testAddMappingThenTranslateTraceViewToStaticEmpty() throws Exception { addMapping(); - Map> views = + Map views = mappingService.getOpenMappedViews(tb.trace, new AddressSet(), 0); assertTrue(views.isEmpty()); } @@ -360,12 +360,12 @@ public class DebuggerStaticMappingServiceTest extends AbstractGhidraHeadedDebugg // After set.add(dynSpace.getAddress(0xbadbadbadL), dynSpace.getAddress(0xbadbadbadL + 0xff)); - Map> views = + Map views = mappingService.getOpenMappedViews(tb.trace, set, 0); assertEquals(1, views.size()); - Pair 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 AbstractGhidraHeadedDebugg copyTrace(); add2ndMapping(); - Map> views = + Map views = mappingService.getOpenMappedViews(program, new AddressSet()); assertTrue(views.isEmpty()); } @@ -403,13 +403,15 @@ public class DebuggerStaticMappingServiceTest extends AbstractGhidraHeadedDebugg // After set.add(stSpace.getAddress(0xbadbadbadL), stSpace.getAddress(0xbadbadbadL + 0xff)); - Map> views = + Map 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();