Ghidra is a software reverse engineering (SRE) framework
renaming less datatype sensitive
caheckman1257f06c1793
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameVariableTask.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameVariableTask.javaindex 9b275bf744..2850141631 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameVariableTask.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameVariableTask.java@@ -33,7 +33,8 @@ public class RenameVariableTask extends RenameTask {private Program program;private Function function;private boolean commitRequired; // Set to true if all parameters are committed before renaming- private SourceType srctype;+ private SourceType srctype; // Desired source type for the variable being renamed+ private SourceType signatureSrcType; // Signature source type of the function (which will be preserved)public RenameVariableTask(PluginTool tool, String old, HighFunction hfunc, HighVariable v,Varnode ex, SourceType st) {@@ -44,13 +45,16 @@ public class RenameVariableTask extends RenameTask {function = hfunc.getFunction();program = function.getProgram();srctype = st;+ signatureSrcType = function.getSignatureSource();}@Overridepublic void commit() throws DuplicateNameException, InvalidInputException {if (commitRequired) {- HighFunctionDBUtil.commitParamsToDatabase(hfunction, true, srctype);- HighFunctionDBUtil.commitReturnToDatabase(hfunction, srctype);+ HighFunctionDBUtil.commitParamsToDatabase(hfunction, false, signatureSrcType);+ if (signatureSrcType != SourceType.DEFAULT) {+ HighFunctionDBUtil.commitReturnToDatabase(hfunction, signatureSrcType);+ }}HighFunctionDBUtil.updateDBVariable(var, newName, null, srctype);}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RetypeVariableAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RetypeVariableAction.javaindex 74f6ee2ef8..d3667d526b 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RetypeVariableAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RetypeVariableAction.java@@ -290,10 +290,6 @@ public class RetypeVariableAction extends AbstractDecompilerAction {return true;}- int skipslot = -1;- if (var != null) {- skipslot = ((HighParam) var).getSlot();- }for (int i = 0; i < numParams; i++) {HighParam param = localSymbolMap.getParam(i);if (param.getSlot() != i) {@@ -303,21 +299,6 @@ public class RetypeVariableAction extends AbstractDecompilerAction {if (!storage.equals(parameters[i].getVariableStorage())) {return true;}- if (skipslot != i) { // Compare datatypes unless it is the specific -var- we are skipping- if (!param.getDataType().isEquivalent(parameters[i].getDataType())) {- return true;- }- }- }-- if (var != null) { // A null var indicates we are changing the return type anyway, so we don't need to check it- DataType funcReturnType = function.getReturnType();- if (funcReturnType != DataType.DEFAULT) {- DataType hfuncReturnType = hfunction.getFunctionPrototype().getReturnType();- if (!funcReturnType.equals(hfuncReturnType)) {- return true;- }- }}return false;diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunctionDBUtil.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunctionDBUtil.javaindex abbffd6a0f..258e9a13f9 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunctionDBUtil.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunctionDBUtil.java@@ -72,37 +72,43 @@ public class HighFunctionDBUtil {/*** Commit all parameters associated with HighFunction to the underlying database.- * @param highFunction- * @param renameConflicts if true any name conflicts will be resolved- * by renaming the conflicting local variable/label- * @param source source type+ * @param highFunction is the associated HighFunction+ * @param useDataTypes is true if the HighFunction's parameter data-types should be committed+ * @param source is the signature source type to set* @throws DuplicateNameException if commit of parameters caused conflict with other- * local variable/label. Should not occur if renameConflicts is true.- * @throws InvalidInputException+ * local variable/label.+ * @throws InvalidInputException if specified storage is invalid*/- public static void commitParamsToDatabase(HighFunction highFunction, boolean renameConflicts,+ public static void commitParamsToDatabase(HighFunction highFunction, boolean useDataTypes,SourceType source) throws DuplicateNameException, InvalidInputException {Function function = highFunction.getFunction();- List<Parameter> params = getParameters(highFunction);+ List<Parameter> params = getParameters(highFunction, useDataTypes);commitParamsToDatabase(function, highFunction.getFunctionPrototype(), params,- highFunction.getFunctionPrototype().isVarArg(), renameConflicts, source);+ highFunction.getFunctionPrototype().isVarArg(), true, source);}- private static List<Parameter> getParameters(HighFunction highFunction)+ private static List<Parameter> getParameters(HighFunction highFunction, boolean useDataTypes)throws InvalidInputException {Function function = highFunction.getFunction();Program program = function.getProgram();+ DataTypeManager dtm = program.getDataTypeManager();LocalSymbolMap symbolMap = highFunction.getLocalSymbolMap();List<Parameter> params = new ArrayList<Parameter>();int paramCnt = symbolMap.getNumParams();for (int i = 0; i < paramCnt; ++i) {HighParam param = symbolMap.getParam(i);String name = param.getName();- boolean nameLocked = param.getSymbol().isNameLocked();- name = (nameLocked ? name : null);- params.add(new ParameterImpl(name, param.getDataType(), param.getStorage(), program));+ DataType dataType;+ if (useDataTypes) {+ dataType = param.getDataType();+ }+ else {+ dataType = Undefined.getUndefinedDataType(param.getSize());+ dataType = dataType.clone(dtm);+ }+ params.add(new ParameterImpl(name, dataType, param.getStorage(), program));}return params;}@@ -407,14 +413,17 @@ public class HighFunctionDBUtil {HighFunction highFunction = variable.getHighFunction();Function function = highFunction.getFunction();Program program = function.getProgram();- DataTypeManager dtm = program.getDataTypeManager();- dataType = dataType != null ? dataType.clone(dtm) : variable.getDataType();- if (dataType.getLength() <= 0) {- throw new InvalidInputException("Data type is not fixed-length: " + dataType.getName());- }+ boolean resized = false;+ if (dataType != null) {+ dataType = dataType.clone(program.getDataTypeManager());+ if (dataType.getLength() <= 0) {+ throw new InvalidInputException(+ "Data type is not fixed-length: " + dataType.getName());+ }- boolean resized = (dataType.getLength() != variable.getSize());+ resized = (dataType.getLength() != variable.getSize());+ }boolean isRename = name != null;@@ -422,13 +431,15 @@ public class HighFunctionDBUtil {HighParam param = (HighParam) variable;Parameter dbParam = getDatabaseParameter(param);VariableStorage storage = param.getStorage();- if (resized && function.hasCustomVariableStorage()) {- VariableStorage newStorage =- VariableUtilities.resizeStorage(storage, dataType, true, function);- dbParam.setDataType(dataType, newStorage, false, source);- }- else {- dbParam.setDataType(dataType, source);+ if (dataType != null) {+ if (resized && function.hasCustomVariableStorage()) {+ VariableStorage newStorage =+ VariableUtilities.resizeStorage(storage, dataType, true, function);+ dbParam.setDataType(dataType, newStorage, false, source);+ }+ else {+ dbParam.setDataType(dataType, source);+ }}if (name != null && !name.equals(dbParam.getName())) {dbParam.setName(name, source);@@ -440,6 +451,15 @@ public class HighFunctionDBUtil {boolean usesHashStorage = storage.isHashStorage();Variable var = clearConflictingLocalVariables(local);+ if (dataType == null) {+ if (var != null) {+ dataType = var.getDataType(); // Use preexisting datatype+ }+ else {+ dataType = Undefined.getUndefinedDataType(variable.getSize());+ dataType = dataType.clone(program.getDataTypeManager());+ }+ }if (resized) {if (usesHashStorage) {throw new InvalidInputException(@@ -495,7 +515,9 @@ public class HighFunctionDBUtil {}}- setGlobalDataType(global, dataType);+ if (dataType != null) {+ setGlobalDataType(global, dataType);+ }if (name != null) {try {