Ghidra is a software reverse engineering (SRE) framework
oops GP-1 more changes related to GP_7119
ghidragon1fe24d271dae
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/navigation/GoToQuery.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/navigation/GoToQuery.javaindex 2c51ae9466..75c9055914 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/navigation/GoToQuery.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/navigation/GoToQuery.java@@ -21,7 +21,6 @@ import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;-import generic.expressions.ExpressionException;import ghidra.app.nav.Navigatable;import ghidra.app.plugin.core.gotoquery.GoToQueryResultsTableModel;import ghidra.app.plugin.core.navigation.NavigationOptions;@@ -36,6 +35,7 @@ import ghidra.framework.plugintool.Plugin;import ghidra.framework.plugintool.PluginTool;import ghidra.program.model.address.Address;import ghidra.program.model.address.AddressFormatException;+import ghidra.program.model.address.AddressOutOfBoundsException;import ghidra.program.model.address.AddressSpace;import ghidra.program.model.listing.Program;import ghidra.program.model.mem.Memory;@@ -86,7 +86,7 @@ public class GoToQuery {this.monitor = monitor;}- public boolean processQuery() throws ExpressionException {+ public boolean processQuery() {// Queries can be of several different types. Handle all the non-symbol types first since// they are faster to try, as they don't require searching through all the program's// symbols.@@ -95,7 +95,7 @@ public class GoToQuery {return true;}- if (processAddressExpression(false)) {+ if (processAddressExpression()) {return true;}@@ -103,14 +103,7 @@ public class GoToQuery {return true;}- if (processSymbols()) {- return true;- }-- // try processing the expression again, this time allowing it to possibly throw an- // exception if it looks like an expression, but doesn't evaluate properly.- return processAddressExpression(true);-+ return processSymbols();}private boolean processFileOffset() {@@ -134,7 +127,7 @@ public class GoToQuery {return false;}- private boolean processAddressExpression(boolean allowExceptions) throws ExpressionException {+ private boolean processAddressExpression() {String queryInput = queryData.getQueryString();if (!isAddressExpression(queryInput)) {return false;@@ -143,22 +136,14 @@ public class GoToQuery {// checking for leading "+" or "-", ignoring spaces.boolean relative = queryInput.matches("^\\s*[+-].*");Address baseAddr = relative ? fromAddress : null;- ExpressionException exception = null;+ AddressOutOfBoundsException exception = null;for (Program program : getSearchPrograms()) {-- Address evalAddr;- try {- evalAddr = AddressEvaluator.parseRelative(program, baseAddr, queryInput);+ Address evalAddr = AddressEvaluator.evaluate(program, queryInput, baseAddr);+ if (evalAddr != null) {return goTo(program, new ProgramLocation(program, evalAddr));}- catch (ExpressionException e) {- exception = e;- }}- if (allowExceptions && exception != null) {- throw exception;- }return false;}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/navigation/GoToServiceImpl.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/navigation/GoToServiceImpl.javaindex 5129cd3e0b..889c6091f6 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/navigation/GoToServiceImpl.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/navigation/GoToServiceImpl.java@@ -15,7 +15,6 @@*/package ghidra.app.util.navigation;-import generic.expressions.ExpressionException;import ghidra.app.nav.Navigatable;import ghidra.app.plugin.core.gotoquery.GoToHelper;import ghidra.app.services.GoToOverrideService;@@ -24,6 +23,7 @@ import ghidra.app.services.GoToServiceListener;import ghidra.app.services.QueryData;import ghidra.framework.plugintool.Plugin;import ghidra.program.model.address.Address;+import ghidra.program.model.address.AddressOutOfBoundsException;import ghidra.program.model.listing.Program;import ghidra.program.model.symbol.ExternalLocation;import ghidra.program.util.ProgramLocation;@@ -149,7 +149,7 @@ public class GoToServiceImpl implements GoToService {}return result;}- catch (ExpressionException e) {+ catch (AddressOutOfBoundsException e) {// The exception has more information about the failure. Pass that to the listener// if the user has supplied one.if (listener != null) {diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/program/util/AddressEvaluatorTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/program/util/AddressEvaluatorTest.javaindex b34a809e0a..f66e8ea363 100644--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/program/util/AddressEvaluatorTest.java+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/program/util/AddressEvaluatorTest.java@@ -22,10 +22,10 @@ import org.junit.After;import org.junit.Before;import org.junit.Test;-import generic.expressions.ExpressionException;import generic.test.AbstractGenericTest;import ghidra.program.database.ProgramBuilder;import ghidra.program.model.address.Address;+import ghidra.program.model.address.AddressOutOfBoundsException;import ghidra.program.model.listing.Program;import ghidra.program.model.symbol.Symbol;@@ -63,8 +63,7 @@ public class AddressEvaluatorTest extends AbstractGenericTest {assertEval(addr("0x3"), "0-5+8");assertEval(addr("0x3"), "-5+8");assertEval(addr("0x11"), "3+(5+(3*2)+(3))");- assertOutOfBounds("Address out of bounds. Expression evaluated to a negative value: -1",- "-1");+ assertOutOfBounds("-1");}@Test@@ -169,26 +168,26 @@ public class AddressEvaluatorTest extends AbstractGenericTest {}private void assertEvalRelative(Address expected, Address base, String input) {- assertEquals(expected, AddressEvaluator.evaluate(program, base, input));+ assertEquals(expected, AddressEvaluator.evaluate(program, input, base));}- private void assertOutOfBounds(String errMsg, String input) {+ private void assertOutOfBounds(String input) {try {- AddressEvaluator.parse(program, input);+ AddressEvaluator.evaluate(program, input);fail("Expected Expression exception");}- catch (ExpressionException e) {- assertEquals(errMsg, e.getMessage());+ catch (AddressOutOfBoundsException e) {+ // expected}}private void assertOutOfBoundsRelative(Address base, String input) {try {- AddressEvaluator.parseRelative(program, base, input);+ AddressEvaluator.evaluate(program, input, base);fail("Expected Expression exception");}- catch (ExpressionException e) {- assertEquals("Address out of bounds", e.getMessage());+ catch (AddressOutOfBoundsException e) {+ // expected}}diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/AddressEvaluator.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/AddressEvaluator.javaindex 8875801690..4344796789 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/AddressEvaluator.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/AddressEvaluator.java@@ -66,27 +66,24 @@ public class AddressEvaluator extends ExpressionEvaluator {* to a unique legitimate address.*/public static Address evaluate(Program p, String inputExpression) {- AddressEvaluator evaluator = new AddressEvaluator(p, true);- try {- return evaluator.parseAsAddress(inputExpression);- }- catch (ExpressionException e) {- return null;- }+ return evaluate(p, inputExpression, null);}/*** Gets a valid address for the specified program as indicated by the input expression or null if* the expression could not be evaluated for any reason.* @param p the program to use for determining the address.- * @param baseAddr the base address to use for relative addressing.* @param inputExpression string representation of the address desired.+ * @param baseAddr the base address to use for relative addressing(if null, then parse absolute)* @return the address. Otherwise, return null if the string fails to evaluate* to a unique legitimate address.*/- public static Address evaluate(Program p, Address baseAddr, String inputExpression) {- AddressEvaluator evaluator = new AddressEvaluator(p, true);+ public static Address evaluate(Program p, String inputExpression, Address baseAddr) {try {+ AddressEvaluator evaluator = new AddressEvaluator(p, true);+ if (baseAddr == null) {+ return evaluator.parseAsAddress(inputExpression);+ }return evaluator.parseAsRelativeAddress(inputExpression, baseAddr);}catch (ExpressionException e) {@@ -94,34 +91,6 @@ public class AddressEvaluator extends ExpressionEvaluator {}}- /**- * Gets a valid address for the specified program as indicated by the input expression.- * @param p the program to use for determining the address.- * @param baseAddr the base address to use for relative addressing- * @param inputExpression string representation of the address desired- * @return a valid address in the program that the expression evaluated to- * @throws ExpressionException if the expression could be evaluated to an address- */- public static Address parseRelative(Program p, Address baseAddr, String inputExpression)- throws ExpressionException {- AddressEvaluator evaluator = new AddressEvaluator(p, true);- return evaluator.parseAsRelativeAddress(inputExpression, baseAddr);- }-- /**- * Gets a valid address for the specified program as indicated by the input expression.- * @param p the program to use for determining the address.- * @param baseAddr the base address to use for relative addressing- * @param inputExpression string representation of the address desired- * @return a valid address in the program that the expression evaluated to- * @throws ExpressionException if the expression could be evaluated to an address- */- public static Address parse(Program p, String inputExpression)- throws ExpressionException {- AddressEvaluator evaluator = new AddressEvaluator(p, true);- return evaluator.parseAsAddress(inputExpression);- }-/*** Constructs an AddressEvalutor for the given program and in the specified hex/decimal mode.* @param program the program to use to evaluate expressions into valid addresses.diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/AddressExpressionValue.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/AddressExpressionValue.javaindex cec7694b04..d619ff18bf 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/AddressExpressionValue.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/AddressExpressionValue.java@@ -70,12 +70,12 @@ public class AddressExpressionValue implements ExpressionValue {private Address toAddress(AddressSpace space, BigInteger value) throws ExpressionException {if (value.signum() < 0) {// a negative value is less than than min address- throw new ExpressionException(+ throw new AddressOutOfBoundsException("Address out of bounds. Expression evaluated to a negative value: " + value);}if (isBiggerThanMaxAddress(space, value)) {- throw new ExpressionException(+ throw new AddressOutOfBoundsException("Address out of bounds. Expression evaluated to a value larger than max address: " +value);}@@ -134,47 +134,41 @@ public class AddressExpressionValue implements ExpressionValue {BigInteger myValue = new BigInteger(Long.toUnsignedString(value.getOffset()));int compareResult = myValue.compareTo(otherValue);- try {+ switch (operator) {+ case BITWISE_AND:+ return addressExpressionOf(myValue.and(otherValue));+ case BITWISE_OR:+ return addressExpressionOf(myValue.or(otherValue));+ case BITWISE_XOR:+ return addressExpressionOf(myValue.xor(otherValue));+ case DIVIDE:+ return addressExpressionOf(myValue.divide(otherValue));+ case SUBTRACT:+ return addressExpressionOf(value.subtract(otherValue.longValueExact()));+ case ADD:+ return addressExpressionOf(value.add(otherValue.longValueExact()));+ case MULTIPLY:+ return addressExpressionOf(myValue.multiply(otherValue));+ case SHIFT_LEFT:+ return addressExpressionOf(myValue.shiftLeft(otherValue.intValueExact()));+ case SHIFT_RIGHT:+ return addressExpressionOf(myValue.shiftRight(otherValue.intValueExact()));+ case EQUALS:+ return booleanExpression(compareResult == 0);+ case GREATER_THAN:+ return booleanExpression(compareResult > 0);+ case LESS_THAN:+ return booleanExpression(compareResult < 0);+ case GREATER_THAN_OR_EQUAL:+ return booleanExpression(compareResult >= 0);+ case LESS_THAN_OR_EQUAL:+ return booleanExpression(compareResult <= 0);++ default:+ throw new ExpressionException(+ "Binary Operator \"" + operator ++ "\" with Long operands not supported by Address values!");- switch (operator) {- case BITWISE_AND:- return addressExpressionOf(myValue.and(otherValue));- case BITWISE_OR:- return addressExpressionOf(myValue.or(otherValue));- case BITWISE_XOR:- return addressExpressionOf(myValue.xor(otherValue));- case DIVIDE:- return addressExpressionOf(myValue.divide(otherValue));- case SUBTRACT:- return addressExpressionOf(value.subtract(otherValue.longValueExact()));- case ADD:- return addressExpressionOf(value.add(otherValue.longValueExact()));- case MULTIPLY:- return addressExpressionOf(myValue.multiply(otherValue));- case SHIFT_LEFT:- return addressExpressionOf(myValue.shiftLeft(otherValue.intValueExact()));- case SHIFT_RIGHT:- return addressExpressionOf(myValue.shiftRight(otherValue.intValueExact()));- case EQUALS:- return booleanExpression(compareResult == 0);- case GREATER_THAN:- return booleanExpression(compareResult > 0);- case LESS_THAN:- return booleanExpression(compareResult < 0);- case GREATER_THAN_OR_EQUAL:- return booleanExpression(compareResult >= 0);- case LESS_THAN_OR_EQUAL:- return booleanExpression(compareResult <= 0);-- default:- throw new ExpressionException(- "Binary Operator \"" + operator +- "\" with Long operands not supported by Address values!");-- }- }- catch (Exception e) {- throw new ExpressionException("Address out of bounds");}}