Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-24_emteere simplified to not include setting base offset position of buffer

emteered3a0a93f94c7
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/program/model/mem/WrappedMemoryBufferTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/program/model/mem/WrappedMemoryBufferTest.java
index b9cca75971..755d651573 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/program/model/mem/WrappedMemoryBufferTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/program/model/mem/WrappedMemoryBufferTest.java
@@ -167,52 +167,6 @@ public class WrappedMemoryBufferTest extends AbstractGhidraHeadedIntegrationTest
}
}
-
- @Test
- public void testSetPosition() throws Exception {
- loadProgram("notepad");
-
- Memory mem = program.getMemory();
- Address addr = program.getMinAddress();
- memBuf = new MemoryBufferImpl(mem, program.getMinAddress());
- WrappedMemBuffer wrapBuf = new WrappedMemBuffer(memBuf, 5, 0);
- for (int i = 0; i < 5000; i++) {
- assertEquals(mem.getByte(addr), wrapBuf.getByte(0));
- assertEquals(mem.getByte(addr.add(1)), wrapBuf.getByte(1));
- assertEquals(mem.getByte(addr.add(2)), wrapBuf.getByte(2));
- assertEquals(mem.getByte(addr.add(3)), wrapBuf.getByte(3));
- addr = addr.add(5);
- int offset = (int) addr.subtract(program.getMinAddress());
- wrapBuf.setBaseOffset(offset);
- }
-
- addr = program.getMinAddress();
- memBuf = new MemoryBufferImpl(mem, program.getMinAddress());
- wrapBuf = new WrappedMemBuffer(memBuf, 5, 0);
- for (int i = 0; i < 500; i++) {
- assertEquals(mem.getByte(addr), memBuf.getByte(0));
- assertEquals(mem.getByte(addr.add(1)), memBuf.getByte(1));
- assertEquals(mem.getByte(addr.add(2)), memBuf.getByte(2));
- assertEquals(mem.getByte(addr.add(3)), memBuf.getByte(3));
- addr = addr.add(50);
- int offset = (int) addr.subtract(program.getMinAddress());
- wrapBuf.setBaseOffset(offset);
- }
-
- addr = program.getMinAddress();
- memBuf = new MemoryBufferImpl(mem, program.getMinAddress());
- wrapBuf = new WrappedMemBuffer(memBuf, 5, 0);
- for (int i = 0; i < 10; i++) {
- assertEquals(mem.getByte(addr), wrapBuf.getByte(0));
- assertEquals(mem.getByte(addr.add(1)), wrapBuf.getByte(1));
- assertEquals(mem.getByte(addr.add(2)), wrapBuf.getByte(2));
- assertEquals(mem.getByte(addr.add(3)), wrapBuf.getByte(3));
- addr = addr.add(2000);
- int offset = (int) addr.subtract(program.getMinAddress());
- wrapBuf.setBaseOffset(offset);
- }
- }
-
@Test
public void testProgram20bit() throws Exception {
program =
@@ -298,10 +252,8 @@ public class WrappedMemoryBufferTest extends AbstractGhidraHeadedIntegrationTest
assertEquals(mem.getByte(addr), wrapBuf.getByte(0));
- wrapBuf.setBaseOffset(1);
-
try {
- wrapBuf.getByte(0);
+ wrapBuf.getByte(1);
Assert.fail("Should not have been able to get byte");
}
catch (MemoryAccessException e) {
@@ -312,10 +264,8 @@ public class WrappedMemoryBufferTest extends AbstractGhidraHeadedIntegrationTest
assertEquals(mem.getByte(addr), wrapBuf.getByte(0));
- wrapBuf.setBaseOffset(1);
-
try {
- wrapBuf.getByte(0);
+ wrapBuf.getByte(1);
Assert.fail("Should not have been able to get byte");
}
catch (MemoryAccessException e) {
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/mem/WrappedMemBuffer.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/mem/WrappedMemBuffer.java
index 7eab8170f6..4f4c10b490 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/mem/WrappedMemBuffer.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/mem/WrappedMemBuffer.java
@@ -21,6 +21,24 @@ import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressOutOfBoundsException;
import ghidra.util.GhidraDataConverter;
+/**
+ * WrappedMemBuffer implements a MemBuffer that provides a zero based index
+ * on top of an underlying membuffer with at a given address. It can buffer N bytes
+ * at time using the constructor that takes a cache size. However the default
+ * is to provide no buffering. Use of the buffer can
+ * reduce the overall number of calls to Memory, greatly reducing
+ * the overhead of various error checks. This implementation will not wrap
+ * if the end of the memory space is encountered.
+ *
+ * The {@link #getByte(int), #getBytes(byte[], int)} methods can cause the bytes in the
+ * buffer cache if the request is outside of the current cached bytes.
+ *
+ * WARNING: The underlying MemBuffer should not change its base address. Using a
+ * mutable MemBuffer can cause problematic behavior if not controlled carefully.
+ *
+ * WARNING: Not thread-safe.
+ */
+
public class WrappedMemBuffer implements MemBuffer {
private final GhidraDataConverter converter;
@@ -32,37 +50,36 @@ public class WrappedMemBuffer implements MemBuffer {
private static final int DEFAULT_BUFSIZE = 0; // default no buffer
private byte[] buffer;
- private int subBufferIndex = 0;
private int minOffset = 0;
private int maxOffset = -1;
- private int threshold = 0;
/**
* Construct a wrapped MemBuffer with an adjustable base offset
* @param buf memory buffer
- * @param offset base offset for this buffer relative to buf's address
+ * @param baseOffset offset relative to the underlying buffer's start address
+ * (addr + baseOffset) will be the 0 index into this buffer
* @throws AddressOutOfBoundsException
*/
- public WrappedMemBuffer(MemBuffer buf, int offset) throws AddressOutOfBoundsException {
- this(buf, DEFAULT_BUFSIZE, offset);
+ public WrappedMemBuffer(MemBuffer buf, int baseOffset) throws AddressOutOfBoundsException {
+ this(buf, DEFAULT_BUFSIZE, baseOffset);
}
/**
* Construct a wrapped MemBuffer with an adjustable base offset
* @param buf memory buffer
* @buffersize size of cache buffer - specify 0 for no buffering
- * @param offset base offset for this buffer relative to buf's address
+ * @param baseOffset offset relative to the underlying buffer's start address
+ * (addr + baseOffset) will be the 0 index into this buffer
* @throws AddressOutOfBoundsException
*/
- public WrappedMemBuffer(MemBuffer buf, int bufferSize, int offset)
+ public WrappedMemBuffer(MemBuffer buf, int bufferSize, int baseOffset)
throws AddressOutOfBoundsException {
this.memBuffer = buf;
this.converter = GhidraDataConverter.getInstance(buf.isBigEndian());
buffer = new byte[bufferSize];
- threshold = bufferSize / 100; // 1/100 of buffer size
- setBaseOffset(offset);
+ setBaseOffset(baseOffset);
}
@Override
@@ -75,24 +92,12 @@ public class WrappedMemBuffer implements MemBuffer {
* @param offset new base offset of this buffer
* @throws AddressOutOfBoundsException
*/
- public void setBaseOffset(int offset) throws AddressOutOfBoundsException {
+ private void setBaseOffset(int offset) throws AddressOutOfBoundsException {
this.address = memBuffer.getAddress().add(offset);
- // already set, changing position
- if (minOffset <= maxOffset) {
- long curOffset = offset - baseOffset; // convert the new offset into an offset based on current offset
- if (curOffset >= minOffset && curOffset < (maxOffset - threshold)) {
- baseOffset = offset;
- minOffset -= (int) curOffset;
- maxOffset -= (int) curOffset;
- subBufferIndex += curOffset;
- return;
- }
- }
this.baseOffset = offset;
if (buffer.length > 0) {
- subBufferIndex = 0;
minOffset = 0;
maxOffset = -1;
@@ -108,10 +113,10 @@ public class WrappedMemBuffer implements MemBuffer {
private int computeOffset(int offset) throws MemoryAccessException {
int bufOffset = baseOffset + offset;
if (offset > 0 && bufOffset < baseOffset) {
- throw new MemoryAccessException();
+ throw new MemoryAccessException("Invalid WrappedMemBuffer, offset would wrap underlying memory buffer");
}
if (offset < 0 && bufOffset > baseOffset) {
- throw new MemoryAccessException();
+ throw new MemoryAccessException("Invalid WrappedMemBuffer offset, offset would wrap underlying memory buffer");
}
return bufOffset;
}
@@ -119,13 +124,13 @@ public class WrappedMemBuffer implements MemBuffer {
@Override
public byte getByte(int offset) throws MemoryAccessException {
// no buffering, just get the byte
- if (buffer.length <= 0) {
+ if (buffer.length == 0) {
return memBuffer.getByte(computeOffset(offset));
}
// byte found in buffer
if ((offset >= minOffset) && (offset <= maxOffset)) {
- return buffer[subBufferIndex + offset];
+ return buffer[offset - minOffset];
}
fillBuffer(offset);
@@ -144,7 +149,7 @@ public class WrappedMemBuffer implements MemBuffer {
}
// bytes are contained in the buffer
if (offset >= minOffset && (b.length + offset - 1) <= maxOffset) {
- System.arraycopy(buffer, subBufferIndex + offset, b, 0, b.length);
+ System.arraycopy(buffer, offset - minOffset, b, 0, b.length);
return b.length;
}
}
@@ -162,10 +167,9 @@ public class WrappedMemBuffer implements MemBuffer {
int nRead = memBuffer.getBytes(buffer, computeOffset(offset));
if (nRead == 0) {
- throw new MemoryAccessException();
+ throw new MemoryAccessException("No bytes available in memory to cache");
}
- subBufferIndex = -offset;
minOffset = offset;
maxOffset = offset + nRead - 1;
}