Merge remote-tracking branch 'origin/GT-3182_FillStructureFail' into Ghidra_9.1

This commit is contained in:
Ryan Kurtz 2019-10-16 08:32:12 -04:00
commit 1a36d16dd5
4 changed files with 43 additions and 28 deletions

View file

@ -56,6 +56,34 @@ public abstract class CreateStructureVariableAction extends DockingAction {
return false;
}
/**
* Check if a variable has a data-type that is suitable for being extended.
* If so return the structure data-type, otherwise return null.
* Modulo typedefs, the data-type of the variable must be exactly a
* "pointer to a structure". Not a "structure" itself, or a
* "pointer to a pointer to ... a structure".
* @param dt is the data-type of the variable to test
* @return the extendable structure data-type or null
*/
public static Structure getStructureForExtending(DataType dt) {
if (dt instanceof TypeDef) {
dt = ((TypeDef) dt).getBaseDataType();
}
if (dt instanceof Pointer) {
dt = ((Pointer) dt).getDataType();
}
else {
return null;
}
if (dt instanceof TypeDef) {
dt = ((TypeDef) dt).getBaseDataType();
}
if (dt instanceof Structure) {
return (Structure) dt;
}
return null;
}
@Override
public abstract boolean isEnabledForContext(ActionContext context);
@ -67,13 +95,9 @@ public abstract class CreateStructureVariableAction extends DockingAction {
*/
protected void adjustCreateStructureMenuText(DataType dt, boolean isThisParam) {
// rip off any multi-level pointers
while (dt instanceof Pointer) {
dt = ((Pointer) dt).getDataType();
}
dt = getStructureForExtending(dt);
String menuString = "Auto Create Structure";
if (dt instanceof Structure) {
if (dt != null) {
if (isThisParam) {
menuString = "Auto Fill in Class Structure";
}

View file

@ -45,7 +45,7 @@ public class DecompilerStructureVariableAction extends CreateStructureVariableAc
return decompilerContext.checkActionEnablement(() -> {
Function function = controller.getFunction();
if (function instanceof UndefinedFunction) {
if (function == null || function instanceof UndefinedFunction) {
return false;
}
@ -58,13 +58,14 @@ public class DecompilerStructureVariableAction extends CreateStructureVariableAc
if (tokenAtCursor == null) {
return false;
}
int maxPointerSize = controller.getProgram().getDefaultPointerSize();
HighVariable var = tokenAtCursor.getHighVariable();
if (var != null && !(var instanceof HighConstant)) {
dt = var.getDataType();
isThisParam = testForAutoParameterThis(var, function);
}
if (dt == null) {
if (dt == null || dt.getLength() > maxPointerSize) {
return false;
}

View file

@ -370,21 +370,8 @@ public class FillOutStructureCmd extends BackgroundCommand {
*/
private Structure createStructure(HighVariable var, Function f, boolean isThisParam) {
Structure structDT = null;
DataType varDT = var.getDataType();
if (varDT instanceof Structure) {
structDT = (StructureDataType) varDT;
}
else if (varDT instanceof Pointer) {
DataType dt = ((Pointer) varDT).getDataType();
while (dt instanceof Pointer) {
dt = ((Pointer) dt).getDataType();
}
if (dt instanceof Structure) {
structDT = (Structure) dt;
}
}
Structure structDT =
CreateStructureVariableAction.getStructureForExtending(var.getDataType());
if (structDT == null) {
structDT = createNewStruct(var, (int) maxOffset, f, isThisParam);

View file

@ -15,6 +15,8 @@
*/
package ghidra.app.plugin.core.decompile.actions;
import docking.ActionContext;
import docking.action.MenuData;
import ghidra.app.context.ListingActionContext;
import ghidra.app.decompiler.component.DecompilerController;
import ghidra.app.plugin.core.function.FunctionPlugin;
@ -22,8 +24,6 @@ import ghidra.framework.plugintool.PluginTool;
import ghidra.program.model.data.DataType;
import ghidra.program.model.listing.*;
import ghidra.program.util.*;
import docking.ActionContext;
import docking.action.MenuData;
public class ListingStructureVariableAction extends CreateStructureVariableAction {
@ -55,16 +55,18 @@ public class ListingStructureVariableAction extends CreateStructureVariableActio
VariableLocation varLoc = (VariableLocation) location;
Variable variable = varLoc.getVariable();
if (variable instanceof Parameter) {
if (((Parameter) variable).getAutoParameterType() == AutoParameterType.THIS)
if (((Parameter) variable).getAutoParameterType() == AutoParameterType.THIS) {
isThisParam = true;
}
}
dt = variable.getDataType();
}
else if (location instanceof FunctionParameterFieldLocation) {
FunctionParameterFieldLocation funcPFL = (FunctionParameterFieldLocation) location;
Parameter parameter = funcPFL.getParameter();
if (parameter.getAutoParameterType() == AutoParameterType.THIS)
if (parameter.getAutoParameterType() == AutoParameterType.THIS) {
isThisParam = true;
}
dt = parameter.getDataType();
}
else if (location instanceof FunctionReturnTypeFieldLocation) {
@ -74,7 +76,8 @@ public class ListingStructureVariableAction extends CreateStructureVariableActio
dt = func.getReturnType();
}
if (dt == null) {
int maxPointerSize = currentProgram.getDefaultPointerSize();
if (dt == null || dt.getLength() > maxPointerSize) {
return false;
}