Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-1417_emteere initial stab at lazy DataDB size

emteere56942bbc50d9
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CodeUnitDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CodeUnitDB.java
index b04b3de2d9..0bbf30633d 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CodeUnitDB.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/CodeUnitDB.java
@@ -284,7 +284,7 @@ abstract class CodeUnitDB extends DatabaseObject implements CodeUnit, ProcessorC
public Address getMaxAddress() {
refreshIfNeeded();
if (endAddr == null) {
- endAddr = length == 0 ? address : address.add(length - 1);
+ endAddr = getLength() == 0 ? address : address.add(getLength() - 1);
}
return endAddr;
}
@@ -669,9 +669,10 @@ abstract class CodeUnitDB extends DatabaseObject implements CodeUnit, ProcessorC
try {
checkIsValid();
populateByteArray();
- byte[] b = new byte[length];
- if (bytes.length < length) {
- if (program.getMemory().getBytes(address, b) != length) {
+ int len = getLength();
+ byte[] b = new byte[len];
+ if (bytes.length < len) {
+ if (program.getMemory().getBytes(address, b) != len) {
throw new MemoryAccessException("Couldn't get all bytes for CodeUnit");
}
}
@@ -829,7 +830,7 @@ abstract class CodeUnitDB extends DatabaseObject implements CodeUnit, ProcessorC
}
protected int getPreferredCacheLength() {
- return length;
+ return getLength();
}
private void validateOpIndex(int opIndex) {
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataDB.java
index ed11306b27..142d65e675 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataDB.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/code/DataDB.java
@@ -71,10 +71,7 @@ class DataDB extends CodeUnitDB implements Data {
baseDataType = getBaseDataType(dataType);
defaultSettings = dataType.getDefaultSettings();
- computeLength();
- if (length < 0) {
- Msg.error(this, " bad bad");
- }
+ length = -1; // lazy compute
}
protected static DataType getBaseDataType(DataType dataType) {
@@ -121,10 +118,24 @@ class DataDB extends CodeUnitDB implements Data {
dataType = dt;
baseDataType = getBaseDataType(dataType);
defaultSettings = dataType.getDefaultSettings();
- computeLength();
+ length = -1; // set to compute lazily later
return false;
}
+ @Override
+ public int getLength() {
+ if (length == -1) {
+ lock.acquire();
+ try {
+ computeLength();
+ }
+ finally {
+ lock.release();
+ }
+ }
+ return length;
+ }
+
private void computeLength() {
length = dataType.getLength();
if (length < 1) {
@@ -165,6 +176,17 @@ class DataDB extends CodeUnitDB implements Data {
}
}
+ bytes = null;
+
+ // if this is not a component where the size could change and
+ // the length restricted by the following instruction/data item, assume
+ // the createData method stopped fixed code units that won't fit from being added
+ if (!(baseDataType instanceof Composite || baseDataType instanceof ArrayDataType)) {
+ return;
+ }
+
+ // This is potentially expensive! So only do if necessary
+ // see if the datatype length is restricted by a following codeunit
Address nextAddr = codeMgr.getDefinedAddressAfter(address);
if ((nextAddr != null) && nextAddr.compareTo(endAddress) <= 0) {
length = (int) nextAddr.subtract(address);
@@ -269,7 +291,7 @@ class DataDB extends CodeUnitDB implements Data {
lock.acquire();
try {
checkIsValid();
- return dataType.getRepresentation(this, this, length);
+ return dataType.getRepresentation(this, this, getLength());
}
finally {
lock.release();
@@ -501,7 +523,7 @@ class DataDB extends CodeUnitDB implements Data {
lock.acquire();
try {
checkIsValid();
- if (offset < 0 || offset > length) {
+ if (offset < 0 || offset > getLength()) {
return null;
}
@@ -538,7 +560,7 @@ class DataDB extends CodeUnitDB implements Data {
lock.acquire();
try {
checkIsValid();
- if (offset < 0 || offset >= length) {
+ if (offset < 0 || offset >= getLength()) {
return null;
}
if (baseDataType instanceof Array) {
@@ -662,7 +684,7 @@ class DataDB extends CodeUnitDB implements Data {
lock.acquire();
try {
checkIsValid();
- if (length < dataType.getLength()) {
+ if (getLength() < dataType.getLength()) {
return -1;
}
if (baseDataType instanceof Composite) {
@@ -715,7 +737,7 @@ class DataDB extends CodeUnitDB implements Data {
lock.acquire();
try {
checkIsValid();
- if (offset < 0 || offset >= length) {
+ if (offset < 0 || offset >= getLength()) {
return null;
}
Data dc = getComponentContaining(offset);
@@ -744,7 +766,7 @@ class DataDB extends CodeUnitDB implements Data {
lock.acquire();
try {
checkIsValid();
- return baseDataType.getValue(this, this, length);
+ return baseDataType.getValue(this, this, getLength());
}
finally {
lock.release();
@@ -773,7 +795,7 @@ class DataDB extends CodeUnitDB implements Data {
if (options == null) {
options = DataTypeDisplayOptions.DEFAULT;
}
- return dataType.getDefaultLabelPrefix(this, this, length, options);
+ return dataType.getDefaultLabelPrefix(this, this, getLength(), options);
}
@Override