Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-1669 cleanup AddressSpace.addNoWrap, add tests

dev7473689e2696584400
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/address/AbstractAddressSpace.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/address/AbstractAddressSpace.java
index 245ba48932..786390d9d7 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/address/AbstractAddressSpace.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/address/AbstractAddressSpace.java
@@ -322,25 +322,21 @@ abstract class AbstractAddressSpace implements AddressSpace {
"Address Overflow in subtract: " + addr + " - 0x" + Long.toHexString(displacement));
}
long addrOff = addr.getOffset();
- long maxOff = maxAddress.getOffset();
- long minOff = minAddress.getOffset();
long result = addrOff - displacement;
- if (maxOff < 0) { // 64 bit unsigned
- if (addrOff >= 0 && result < 0) {
- throw new AddressOverflowException("Address Overflow in subtract: " + addr +
- " - 0x" + Long.toHexString(displacement));
- }
- else if ((addrOff < 0 && result < 0) || (addrOff >= 0 && result >= 0)) {
- if (result > addrOff) {
- throw new AddressOverflowException("Address Overflow in subtract: " + addr +
- " - 0x" + Long.toHexString(displacement));
- }
+
+ // An integer underflow occurred if:
+ // 1) the result is less than the address space's min address, or
+ // 2) if the result is greater than the starting addr
+ if (signed) {
+ if (result < minAddress.getOffset() || result > addrOff) {
+ throw new AddressOverflowException(
+ String.format("Address Overflow in subtract: %s - 0x%x", addr, displacement));
}
}
else {
- if (result > addrOff || result < minOff) {
- throw new AddressOverflowException("Address Overflow in subtract: " + addr +
- " - 0x" + Long.toHexString(displacement));
+ if (Long.compareUnsigned(addrOff, result) < 0) {
+ throw new AddressOverflowException(
+ String.format("Address Overflow in subtract: %s - 0x%x", addr, displacement));
}
}
return getUncheckedAddress(result);
@@ -383,27 +379,25 @@ abstract class AbstractAddressSpace implements AddressSpace {
"Address Overflow in add: " + addr + " + 0x" + Long.toHexString(displacement));
}
long addrOff = addr.getOffset();
- long maxOff = maxAddress.getOffset();
long result = addrOff + displacement;
- if (maxOff < 0) { // 64 bit unsigned
- if (addrOff < 0 && result >= 0) {
+
+ // An integer overflow occurred if:
+ // 1) the result is larger than the address space's max address (a no-op when unsigned && maxaddr=MAX_UINT64)
+ // or
+ // 2) if the result is less than the starting addr (because result exceeded MAX_UINT64)
+ if (signed) {
+ if (result < addrOff || result > maxAddress.getOffset()) {
throw new AddressOverflowException(
- "Address Overflow in add: " + addr + " + 0x" + Long.toHexString(displacement));
- }
- else if ((addrOff < 0 && result < 0) || (addrOff >= 0 && result >= 0)) {
- if (result < addrOff) {
- throw new AddressOverflowException("Address Overflow in add: " + addr +
- " + 0x" + Long.toHexString(displacement));
- }
+ String.format("Address Overflow in add: %s 0x%x", addr, displacement));
}
}
else {
- if (result < addrOff || result > maxOff) {
+ if (Long.compareUnsigned(maxAddress.getOffset(), result) < 0 ||
+ Long.compareUnsigned(result, addrOff) < 0) {
throw new AddressOverflowException(
- "Address Overflow in add: " + addr + " + 0x" + Long.toHexString(displacement));
+ String.format("Address Overflow in add: %s 0x%x", addr, displacement));
}
}
-
return getUncheckedAddress(result);
}
diff --git a/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/model/address/GenericAddressSpaceTest.java b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/model/address/GenericAddressSpaceTest.java
index 0dff6b5558..8fca4f7349 100644
--- a/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/model/address/GenericAddressSpaceTest.java
+++ b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/model/address/GenericAddressSpaceTest.java
@@ -269,6 +269,246 @@ public class GenericAddressSpaceTest extends AbstractGenericTest {
Assert.assertEquals(-0x80, sp2.truncateOffset(-0x180));
}
+ @Test
+ public void testAddNoWrapWith64bitAddrSpace() throws AddressOverflowException {
+ // Exercise the address space wrapping detection code's unsigned vs signed limits.
+ // Try max signed (63 bits) values for starting address and the displacement.
+
+ AddressSpace sp1 = new GenericAddressSpace("AnotherSpace", 64, AddressSpace.TYPE_CODE, 1);
+
+ try {
+ sp1.addNoWrap(sp1.getMaxAddress(), 1);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+ try {
+ sp1.addNoWrap(sp1.getMaxAddress(), Long.MAX_VALUE);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+ try {
+ sp1.addNoWrap(sp1.getAddress(Long.MAX_VALUE).add(2), Long.MAX_VALUE);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+
+ assertEquals(sp1.getMaxAddress().subtract(1).getOffset(),
+ sp1.addNoWrap(sp1.getAddress(Long.MAX_VALUE), Long.MAX_VALUE).getOffset());
+ assertEquals(sp1.getMaxAddress().getOffset(),
+ sp1.addNoWrap(sp1.getAddress(Long.MAX_VALUE).add(1), Long.MAX_VALUE).getOffset());
+ assertEquals(sp1.getMaxAddress().getOffset(),
+ sp1.addNoWrap(sp1.getAddress(Long.MAX_VALUE).add(2), Long.MAX_VALUE - 1).getOffset());
+ }
+
+ @Test
+ public void testAddNoWrapWithSigned64bitAddrSpace() throws AddressOverflowException {
+ AddressSpace sp1 =
+ new GenericAddressSpace("AnotherSpace", 64, AddressSpace.TYPE_CONSTANT, 1);
+
+ assertEquals(Long.MAX_VALUE, sp1.getMaxAddress().getOffset());
+ try {
+ sp1.addNoWrap(sp1.getMaxAddress(), 1);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+ try {
+ sp1.addNoWrap(sp1.getMaxAddress(), Long.MAX_VALUE);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+
+ assertEquals(Long.MIN_VALUE, sp1.getMinAddress().getOffset());
+ assertEquals(-1, sp1.addNoWrap(sp1.getMinAddress(), Long.MAX_VALUE).getOffset());
+ assertEquals(Long.MAX_VALUE, sp1.addNoWrap(sp1.getAddress(0), Long.MAX_VALUE).getOffset());
+ assertEquals(Long.MAX_VALUE - 1,
+ sp1.addNoWrap(sp1.getAddress(-1), Long.MAX_VALUE).getOffset());
+ }
+
+ @Test
+ public void testSubtractNoWrapWithSigned64bitAddrSpace() throws AddressOverflowException {
+ AddressSpace sp1 =
+ new GenericAddressSpace("AnotherSpace", 64, AddressSpace.TYPE_CONSTANT, 1);
+
+ try {
+ sp1.subtractNoWrap(sp1.getMinAddress(), 1);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+ try {
+ sp1.subtractNoWrap(sp1.getMinAddress(), Long.MAX_VALUE);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+ assertEquals(0, sp1.subtractNoWrap(sp1.getMaxAddress(), Long.MAX_VALUE).getOffset());
+ assertEquals(-1,
+ sp1.subtractNoWrap(sp1.getMaxAddress().subtract(1), Long.MAX_VALUE).getOffset());
+ assertEquals(Long.MIN_VALUE + 1,
+ sp1.subtractNoWrap(sp1.getAddress(0), Long.MAX_VALUE).getOffset());
+ assertEquals(Long.MIN_VALUE,
+ sp1.subtractNoWrap(sp1.getAddress(-1), Long.MAX_VALUE).getOffset());
+ }
+
+ @Test
+ public void testSubtractNoWrapWith64bitAddrSpace() throws AddressOverflowException {
+ // Exercise the address space wrapping detection code's unsigned vs signed limits.
+ // Try max signed (63 bits) & max unsigned values for starting address and the displacement
+ // and combine with the other
+
+ AddressSpace sp1 = new GenericAddressSpace("AnotherSpace", 64, AddressSpace.TYPE_CODE, 1);
+
+ try {
+ sp1.subtractNoWrap(sp1.getMinAddress(), 1);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+ try {
+ sp1.subtractNoWrap(sp1.getMinAddress(), Long.MAX_VALUE);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+ assertEquals(0x8000000000000000L,
+ sp1.subtractNoWrap(sp1.getMaxAddress(), Long.MAX_VALUE).getOffset());
+ assertEquals(Long.MAX_VALUE,
+ sp1.subtractNoWrap(sp1.getMaxAddress().subtract(1), Long.MAX_VALUE).getOffset());
+ }
+
+ @Test
+ public void testAddNoWrapWithSmallAddrSpace() throws AddressOverflowException {
+ try {
+ space.addNoWrap(space.getMaxAddress(), 1);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+ try {
+ space.addNoWrap(space.getMaxAddress(), Long.MAX_VALUE);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+ assertEquals(Byte.MAX_VALUE,
+ space.addNoWrap(space.getMinAddress(), Byte.MAX_VALUE).getOffset());
+
+ assertEquals(0xff, space.addNoWrap(space.getMinAddress(), 0xff).getOffset());
+ try {
+ space.addNoWrap(space.getMinAddress(), 0xff + 1);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+ }
+
+ @Test
+ public void testAddNoWrapWithSmallSignedAddrSpace() throws AddressOverflowException {
+ space = new GenericAddressSpace("Test", 8, AddressSpace.TYPE_CONSTANT, 0);
+
+ try {
+ space.addNoWrap(space.getMaxAddress(), 1);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+ try {
+ space.addNoWrap(space.getMaxAddress(), Long.MAX_VALUE);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+ assertEquals(Byte.MAX_VALUE,
+ space.addNoWrap(space.getAddress(0), Byte.MAX_VALUE).getOffset());
+
+ assertEquals(Byte.MAX_VALUE, space.addNoWrap(space.getMinAddress(), 0xff).getOffset());
+ try {
+ space.addNoWrap(space.getMinAddress(), 0xff + 1);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+ }
+
+ @Test
+ public void testSubtractNoWrapWithSmallAddrSpace() throws AddressOverflowException {
+ try {
+ space.subtractNoWrap(space.getMinAddress(), 1);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+ try {
+ space.subtractNoWrap(space.getMinAddress(), Long.MAX_VALUE);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+ assertEquals(Byte.MAX_VALUE + 1,
+ space.subtractNoWrap(space.getMaxAddress(), Byte.MAX_VALUE).getOffset());
+
+ assertEquals(0, space.subtractNoWrap(space.getMaxAddress(), 0xff).getOffset());
+ try {
+ space.subtractNoWrap(space.getMaxAddress(), 0xff + 1);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+ }
+
+ @Test
+ public void testSubtractNoWrapWithSmallSignedAddrSpace() throws AddressOverflowException {
+ space = new GenericAddressSpace("Test", 8, AddressSpace.TYPE_CONSTANT, 0);
+
+ try {
+ space.subtractNoWrap(space.getMinAddress(), 1);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+ try {
+ space.subtractNoWrap(space.getMinAddress(), Long.MAX_VALUE);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+ assertEquals(0, space.subtractNoWrap(space.getMaxAddress(), Byte.MAX_VALUE).getOffset());
+
+ assertEquals(Byte.MIN_VALUE, space.subtractNoWrap(space.getMaxAddress(), 0xff).getOffset());
+ try {
+ space.subtractNoWrap(space.getMaxAddress(), 0xff + 1);
+ fail("Should have generated AddressOverflowException");
+ }
+ catch (AddressOverflowException e) {
+ // good
+ }
+ }
+
@Test
public void testGetAddressableWordOffset() {
AddressSpace sp1 = new GenericAddressSpace("AnotherSpace", 64, AddressSpace.TYPE_CODE, 1);