From cd40985bccd265d2a19bea1ddc40b5da1bdcb42c Mon Sep 17 00:00:00 2001 From: Dan <46821332+nsadeveloper789@users.noreply.github.com> Date: Mon, 16 May 2022 16:24:35 -0400 Subject: [PATCH] GP-2011: Fix AddressOutOfBounds issue in mapper --- .../DebuggerStaticMappingServicePlugin.java | 13 +++++------ .../DebuggerStaticMappingServiceTest.java | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) 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 c3c66b0388..585ba142c4 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 @@ -95,13 +95,12 @@ public class DebuggerStaticMappingServicePlugin extends Plugin } public Address addOrMax(Address start, long length) { - try { - return start.addNoWrap(length); - } - catch (AddressOverflowException e) { - Msg.warn(this, "Mapping entry cause overflow in static address space"); + Address result = start.addWrapSpace(length); + if (result.compareTo(start) < 0) { + Msg.warn(this, "Mapping entry caused overflow in static address space"); return start.getAddressSpace().getMaxAddress(); } + return result; } public boolean programOpened(Program opened) { @@ -176,7 +175,7 @@ public class DebuggerStaticMappingServicePlugin extends Plugin protected Address mapTraceAddressToProgram(Address address) { assert isInTraceRange(address, null); long offset = address.subtract(mapping.getMinTraceAddress()); - return staticRange.getMinAddress().add(offset); + return staticRange.getMinAddress().addWrapSpace(offset); } public ProgramLocation mapTraceAddressToProgramLocation(Address address) { @@ -197,7 +196,7 @@ public class DebuggerStaticMappingServicePlugin extends Plugin protected Address mapProgramAddressToTrace(Address address) { assert isInProgramRange(address); long offset = address.subtract(staticRange.getMinAddress()); - return mapping.getMinTraceAddress().add(offset); + return mapping.getMinTraceAddress().addWrapSpace(offset); } protected TraceLocation mapProgramAddressToTraceLocation(Address address) { 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 7b2d1a4049..ab0f71a982 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 @@ -611,4 +611,27 @@ public class DebuggerStaticMappingServiceTest extends AbstractGhidraHeadedDebugg DebuggerStaticMappingProposals.groupRegionsByLikelyModule(mm.getAllRegions()); assertEquals(Set.of(Set.of(echoText, echoData), Set.of(libText, libData)), actual); } + + protected void assertMapsTwoWay(long stOff, long dynOff) { + TraceLocation dynLoc = + new DefaultTraceLocation(tb.trace, null, Range.atLeast(0L), tb.addr(dynOff)); + ProgramLocation stLoc = new ProgramLocation(program, stSpace.getAddress(stOff)); + assertEquals(stLoc, mappingService.getOpenMappedLocation(dynLoc)); + assertEquals(dynLoc, mappingService.getOpenMappedLocation(tb.trace, stLoc, 0)); + } + + @Test + public void testMapFullSpace() throws Exception { + try (UndoableTransaction tid = tb.startTransaction()) { + TraceLocation traceLoc = + new DefaultTraceLocation(tb.trace, null, Range.atLeast(0L), tb.addr(0)); + ProgramLocation progLoc = new ProgramLocation(program, stSpace.getAddress(0)); + // NB. 0 indicates 1 << 64 + mappingService.addMapping(traceLoc, progLoc, 0, true); + } + waitForPass(() -> assertMapsTwoWay(0L, 0L)); + assertMapsTwoWay(-1L, -1L); + assertMapsTwoWay(Long.MAX_VALUE, Long.MAX_VALUE); + assertMapsTwoWay(Long.MIN_VALUE, Long.MIN_VALUE); + } }