Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-746_emteere fix analyis time of large programs with code not contained within functions (exceptions)

emteere9dadf6e79960
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/ConstantPropagationAnalyzer.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/ConstantPropagationAnalyzer.java
index 64715c328c..f28b2f29e2 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/ConstantPropagationAnalyzer.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/ConstantPropagationAnalyzer.java
@@ -71,11 +71,11 @@ public class ConstantPropagationAnalyzer extends AbstractAnalyzer {
protected static final String MINSPECULATIVEREFADDRESS_OPTION_DESCRIPTION =
"Minimum speculative reference address for offsets and parameters";
protected static final int MINSPECULATIVEREFADDRESS_OPTION_DEFAULT_VALUE = 1024;
-
+
protected static final String MAXSPECULATIVEREFADDRESS_OPTION_NAME =
- "Speculative reference max";
+ "Speculative reference max";
protected static final String MAXSPECULATIVEREFADDRESS_OPTION_DESCRIPTION =
- "Maxmimum speculative reference address offset from the end of memory for offsets and parameters";
+ "Maxmimum speculative reference address offset from the end of memory for offsets and parameters";
protected static final int MAXSPECULATIVEREFADDRESS_OPTION_DEFAULT_VALUE = 256;
protected final static int NOTIFICATION_INTERVAL = 100;
@@ -305,36 +305,55 @@ public class ConstantPropagationAnalyzer extends AbstractAnalyzer {
return analyzedSet;
}
- public void analyzeSet(Program program, AddressSetView set, TaskMonitor monitor)
+ public void analyzeSet(Program program, AddressSetView setToAnalyze, TaskMonitor monitor)
throws CancelledException {
- long totalNumAddresses = set.getNumAddresses();
+ long totalNumAddresses = setToAnalyze.getNumAddresses();
monitor.initialize(totalNumAddresses);
// Iterate over all new instructions
// Evaluate each operand
+ Listing listing = program.getListing();
int count = 0;
- while (!set.isEmpty()) {
+ AddressSet todoSet = new AddressSet(setToAnalyze);
+ while (!todoSet.isEmpty()) {
monitor.checkCanceled();
if ((count++ % NOTIFICATION_INTERVAL) == 0) {
- monitor.setProgress(totalNumAddresses - set.getNumAddresses());
+ monitor.setProgress(totalNumAddresses - todoSet.getNumAddresses());
}
- InstructionIterator iterator = program.getListing().getInstructions(set, true);
- if (!iterator.hasNext()) {
- break;
+ // find the next instruction starting at the minimum addr in the set
+ Address nextAddr = todoSet.getMinAddress();
+ Instruction instr = listing.getInstructionAt(nextAddr);
+ if (instr == null) {
+ // no instr at min address, find the next after
+ instr = listing.getInstructionAfter(nextAddr);
+ if (instr == null) {
+ break; // no more to do
+ }
+
+ // the instruction after could land outside the set,
+ // but that will only happen once, then the
+ // search will start again inside the set
+ nextAddr = instr.getMinAddress();
+ if (!todoSet.contains(nextAddr)) {
+ todoSet.deleteFromMin(nextAddr);
+ continue;
+ }
}
- Instruction instr = iterator.next();
Address start = instr.getMinAddress();
- AddressSetView resultSet = analyzeLocation(program, start, set, monitor);
+ AddressSetView resultSet = analyzeLocation(program, start, todoSet, monitor);
if (resultSet != null) { // null sometimes when cancelled
- // set.getMinAddress() and start may not be in the same segment
- if (!start.equals(set.getMinAddress())) {
- set = AddressSetView.trimStart(set, start);
+ // if the first instruction found in todoSet, was past the beginning of
+ // the todoSet, there will be no instructions before start
+ if (!start.equals(todoSet.getMinAddress())) {
+ // delete all addresses up to the start from the todo set
+ todoSet.deleteFromMin(start);
}
- set = set.subtract(resultSet);
+ // now get rid of all the instructions that were analyzed
+ todoSet.delete(resultSet);
}
}
}
@@ -391,10 +410,10 @@ public class ConstantPropagationAnalyzer extends AbstractAnalyzer {
* @return the address set of instructions which were followed
* @throws CancelledException
*/
- public AddressSetView flowConstants(final Program program,
- Address flowStart, AddressSetView flowSet, final SymbolicPropogator symEval, final TaskMonitor monitor)
+ public AddressSetView flowConstants(final Program program, Address flowStart,
+ AddressSetView flowSet, final SymbolicPropogator symEval, final TaskMonitor monitor)
throws CancelledException {
-
+
ContextEvaluator eval = new ConstantPropagationContextEvaluator(trustWriteMemOption,
minStoreLoadRefAddress, minSpeculativeRefAddress, maxSpeculativeRefAddress);
@@ -469,7 +488,7 @@ public class ConstantPropagationAnalyzer extends AbstractAnalyzer {
minSpeculativeRefAddress = size * 16;
options.registerOption(MINSPECULATIVEREFADDRESS_OPTION_NAME, minSpeculativeRefAddress, null,
MINSPECULATIVEREFADDRESS_OPTION_DESCRIPTION);
-
+
maxSpeculativeRefAddress = size * 8;
options.registerOption(MAXSPECULATIVEREFADDRESS_OPTION_NAME, maxSpeculativeRefAddress, null,
MAXSPECULATIVEREFADDRESS_OPTION_DESCRIPTION);
@@ -489,7 +508,7 @@ public class ConstantPropagationAnalyzer extends AbstractAnalyzer {
minSpeculativeRefAddress =
options.getLong(MINSPECULATIVEREFADDRESS_OPTION_NAME, minSpeculativeRefAddress);
maxSpeculativeRefAddress =
- options.getLong(MAXSPECULATIVEREFADDRESS_OPTION_NAME, maxSpeculativeRefAddress);
+ options.getLong(MAXSPECULATIVEREFADDRESS_OPTION_NAME, maxSpeculativeRefAddress);
}
}
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/address/AddressSet.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/address/AddressSet.java
index b3d4ae237d..7b76a492fc 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/address/AddressSet.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/address/AddressSet.java
@@ -1375,4 +1375,41 @@ public class AddressSet implements AddressSetView {
}
return null;
}
+
+ /**
+ * Delete all addresses from the minimum address in the set up to and including toAddr.
+ * Addresses less-than-or-equal to specified
+ * address based upon {@link Address} comparison.
+ *
+ * @param toAddr only addresses greater than toAddr will be left in the set.
+ */
+ public void deleteFromMin(Address toAddr) {
+ if (isEmpty()) {
+ return;
+ }
+ // check if toAddr is already before the start of the set
+ if (toAddr.compareTo(getMinAddress()) < 0) {
+ return;
+ }
+ delete(getMinAddress(), toAddr);
+ }
+
+ /**
+ * Delete all addresses starting at the fromAddr to the maximum address in the set.
+ * Addresses greater-than-or-equal to specified
+ * address based upon {@link Address} comparison.
+ *
+ * @param fromAddr only addresses less than fromAddr will be left in the set.
+ */
+ public void deleteToMax(Address fromAddr) {
+ if (isEmpty()) {
+ return;
+ }
+
+ // check if endAddr is already past the end of the set
+ if (fromAddr.compareTo(getMaxAddress()) > 0) {
+ return;
+ }
+ delete(fromAddr, getMaxAddress());
+ }
}
diff --git a/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/model/address/AddressSetTest.java b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/model/address/AddressSetTest.java
index e2d890a892..263a2eab3b 100644
--- a/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/model/address/AddressSetTest.java
+++ b/Ghidra/Framework/SoftwareModeling/src/test/java/ghidra/program/model/address/AddressSetTest.java
@@ -826,6 +826,49 @@ public class AddressSetTest extends AbstractGenericTest {
assertEquals(expectedSet, trimSet);
}
+ @Test
+ public void testdeleteFromMin() {
+ AddressSet set = set(0x10, 0x20, 0x30, 0x40);
+ set.add(space2.getAddress(0x10), space2.getAddress(0x20));
+ set.add(space2.getAddress(0x30), space2.getAddress(0x40));
+
+ set.deleteFromMin(addr(0x15));
+
+ AddressSet expectedSet = set(0x16, 0x20, 0x30, 0x40);
+ expectedSet.add(space2.getAddress(0x10), space2.getAddress(0x20));
+ expectedSet.add(space2.getAddress(0x30), space2.getAddress(0x40));
+ assertEquals(expectedSet, set);
+
+ set = set(0x10, 0x20, 0x30, 0x40);
+ set.add(space2.getAddress(0x10), space2.getAddress(0x20));
+ set.add(space2.getAddress(0x30), space2.getAddress(0x40));
+
+ set.deleteFromMin(space2.getAddress(0x15));
+
+ expectedSet = new AddressSet(space2.getAddress(0x16), space2.getAddress(0x20));
+ expectedSet.add(space2.getAddress(0x30), space2.getAddress(0x40));
+ assertEquals(expectedSet, set);
+
+ set = set(0x10, 0x20, 0x30, 0x40);
+ set.add(space2.getAddress(0x10), space2.getAddress(0x20));
+ set.add(space2.getAddress(0x30), space2.getAddress(0x40));
+
+ set.deleteFromMin(space2.getAddress(0x50));
+ assertTrue(set.isEmpty());
+
+ set = set(0x10, 0x20, 0x30, 0x40);
+ set.add(space2.getAddress(0x10), space2.getAddress(0x20));
+ set.add(space2.getAddress(0x30), space2.getAddress(0x40));
+
+ set.deleteFromMin(space2.getAddress(0x40));
+ assertTrue(set.isEmpty());
+
+ // make sure handles empty set
+ set = new AddressSet();
+ set.deleteFromMin(addr(0x30));
+ assertTrue(set.isEmpty());
+ }
+
@Test
public void testTrimEnd() {
AddressSet set = set(0x10, 0x20, 0x30, 0x40);
@@ -844,6 +887,51 @@ public class AddressSetTest extends AbstractGenericTest {
assertEquals(expectedSet, trimSet);
}
+ @Test
+ public void testDeleteFrom() {
+ AddressSet set = set(0x10, 0x20, 0x30, 0x40);
+ set.add(space2.getAddress(0x10), space2.getAddress(0x20));
+ set.add(space2.getAddress(0x30), space2.getAddress(0x40));
+
+ AddressSet origSet = new AddressSet(set);
+ set.deleteToMax(space2.getAddress(0x50));
+ assertEquals(origSet, set);
+
+ set.deleteToMax(addr(0x15));
+
+ AddressSet expectedSet = set(0x10, 0x14);
+ assertEquals(expectedSet, set);
+
+ set = set(0x10, 0x20, 0x30, 0x40);
+ set.add(space2.getAddress(0x10), space2.getAddress(0x20));
+ set.add(space2.getAddress(0x30), space2.getAddress(0x40));
+
+ set.deleteToMax(space2.getAddress(0x15));
+
+ expectedSet = set(0x10, 0x20, 0x30, 0x40);
+ expectedSet.add(space2.getAddress(0x10), space2.getAddress(0x14));
+ assertEquals(expectedSet, set);
+
+ set = set(0x10, 0x20, 0x30, 0x40);
+ set.add(space2.getAddress(0x10), space2.getAddress(0x20));
+ set.add(space2.getAddress(0x30), space2.getAddress(0x40));
+
+ set.deleteToMax(addr(0x0));
+ assertTrue(set.isEmpty());
+
+ set = set(0x10, 0x20, 0x30, 0x40);
+ set.add(space2.getAddress(0x10), space2.getAddress(0x20));
+ set.add(space2.getAddress(0x30), space2.getAddress(0x40));
+
+ set.deleteToMax(addr(0x10));
+ assertTrue(set.isEmpty());
+
+ // make sure handles empty set
+ set = new AddressSet();
+ set.deleteToMax(addr(0x30));
+ assertTrue(set.isEmpty());
+ }
+
//
////
//// public void testSequentialInsertionSpeed() {