avoid calling GhidraScriptUtils.getScriptSourceDirectories

- when called from the GUI, the bundle host manages paths
- when used to find a script by name, use findScriptByName
This commit is contained in:
Jason P. Leasure 2020-04-28 17:23:08 -04:00
parent 2b49816c6c
commit 7ee2467016
7 changed files with 135 additions and 156 deletions

View file

@ -33,7 +33,8 @@ import docking.actions.KeyBindingUtils;
import docking.tool.ToolConstants;
import docking.widgets.table.GTable;
import generic.jar.ResourceFile;
import ghidra.app.script.*;
import ghidra.app.script.GhidraScriptInfoManager;
import ghidra.app.script.ScriptInfo;
import ghidra.framework.Application;
import ghidra.framework.options.SaveState;
import ghidra.util.*;
@ -79,7 +80,7 @@ class GhidraScriptActionManager {
}
void restoreUserDefinedKeybindings(SaveState saveState) {
List<ResourceFile> dirs = GhidraScriptUtil.getScriptSourceDirectories();
Collection<ResourceFile> dirs = provider.getBundleHost().getBundlePaths();
String[] names = saveState.getNames();
for (String name : names) {

View file

@ -790,35 +790,32 @@ public abstract class GhidraScript extends FlatProgramAPI {
*/
public void runScript(String scriptName, String[] scriptArguments, GhidraState scriptState)
throws Exception {
List<ResourceFile> dirs = GhidraScriptUtil.getScriptSourceDirectories();
for (ResourceFile dir : dirs) {
ResourceFile scriptSource = new ResourceFile(dir, scriptName);
if (scriptSource.exists()) {
GhidraScriptProvider provider = GhidraScriptUtil.getProvider(scriptSource);
ResourceFile scriptSource = GhidraScriptUtil.findScriptByName(scriptName);
if (scriptSource != null) {
GhidraScriptProvider provider = GhidraScriptUtil.getProvider(scriptSource);
if (provider == null) {
throw new IOException("Attempting to run subscript '" + scriptName +
"': unable to run this script type.");
}
GhidraScript script = provider.getScriptInstance(scriptSource, writer);
script.setScriptArgs(scriptArguments);
if (potentialPropertiesFileLocs.size() > 0) {
script.setPotentialPropertiesFileLocations(potentialPropertiesFileLocs);
}
if (scriptState == state) {
updateStateFromVariables();
}
script.execute(scriptState, monitor, writer);
if (scriptState == state) {
loadVariablesFromState();
}
return;
if (provider == null) {
throw new IOException("Attempting to run subscript '" + scriptName +
"': unable to run this script type.");
}
GhidraScript script = provider.getScriptInstance(scriptSource, writer);
script.setScriptArgs(scriptArguments);
if (potentialPropertiesFileLocs.size() > 0) {
script.setPotentialPropertiesFileLocations(potentialPropertiesFileLocs);
}
if (scriptState == state) {
updateStateFromVariables();
}
script.execute(scriptState, monitor, writer);
if (scriptState == state) {
loadVariablesFromState();
}
return;
}
boolean shouldContinue = false;

View file

@ -129,13 +129,10 @@ public class GhidraScriptRunner implements GhidraLaunchable {
if (scriptSource.exists()) {
return scriptSource;
}
List<ResourceFile> dirs = GhidraScriptUtil.getScriptSourceDirectories();
for (ResourceFile dir : dirs) {
scriptSource = new ResourceFile(dir, scriptName);
if (scriptSource.exists()) {
return scriptSource;
}
scriptSource = GhidraScriptUtil.findScriptByName(scriptName);
if (scriptSource != null) {
return scriptSource;
}
throw new IllegalArgumentException("Script not found: " + scriptName);
}

View file

@ -676,12 +676,9 @@ public class HeadlessAnalyzer {
if (scriptSource.exists()) {
return scriptSource;
}
List<ResourceFile> dirs = GhidraScriptUtil.getScriptSourceDirectories();
for (ResourceFile dir : dirs) {
scriptSource = new ResourceFile(dir, scriptName);
if (scriptSource.exists()) {
return scriptSource;
}
scriptSource = GhidraScriptUtil.findScriptByName(scriptName);
if (scriptSource != null) {
return scriptSource;
}
throw new IllegalArgumentException("Script not found: " + scriptName);
}

View file

@ -16,7 +16,6 @@
package ghidra.app.util.headless;
import java.io.IOException;
import java.util.List;
import generic.jar.ResourceFile;
import ghidra.app.script.*;
@ -338,8 +337,8 @@ public abstract class HeadlessScript extends GhidraScript {
* @throws IOException if there are issues creating the folder
* @throws InvalidNameException if folder name is invalid
*/
public void setHeadlessImportDirectory(String importDir) throws ImproperUseException,
IOException, InvalidNameException {
public void setHeadlessImportDirectory(String importDir)
throws ImproperUseException, IOException, InvalidNameException {
checkHeadlessStatus();
// Do nothing if not importing -- we don't want to have arbitrary folders
@ -397,57 +396,52 @@ public abstract class HeadlessScript extends GhidraScript {
resolveContinuationOptionWith(scriptSetOption);
scriptSetOption = null;
}
ResourceFile scriptSource = GhidraScriptUtil.findScriptByName(scriptName);
if (scriptSource != null) {
GhidraScriptProvider provider = GhidraScriptUtil.getProvider(scriptSource);
List<ResourceFile> dirs = GhidraScriptUtil.getScriptSourceDirectories();
for (ResourceFile dir : dirs) {
ResourceFile scriptSource = new ResourceFile(dir, scriptName);
if (scriptSource.exists()) {
GhidraScriptProvider provider = GhidraScriptUtil.getProvider(scriptSource);
if (provider == null) {
throw new IOException("Attempting to run subscript '" + scriptName +
"': unable to run this script type.");
}
GhidraScript script = provider.getScriptInstance(scriptSource, writer);
isHeadlessScript = script instanceof HeadlessScript ? true : false;
if (potentialPropertiesFileLocs.size() > 0) {
script.setPotentialPropertiesFileLocations(potentialPropertiesFileLocs);
}
if (scriptState == state) {
updateStateFromVariables();
}
if (isHeadlessScript) {
((HeadlessScript) script).setHeadlessInstance(headless);
((HeadlessScript) script).setRunningInnerScript(true);
}
script.setScriptArgs(scriptArguments);
script.execute(scriptState, monitor, writer);
if (scriptState == state) {
loadVariablesFromState();
}
// Resolve continuations options, if they have changed
if (isHeadlessScript) {
HeadlessContinuationOption innerScriptOpt =
((HeadlessScript) script).getHeadlessContinuationOption();
if (innerScriptOpt != null) {
resolveContinuationOptionWith(innerScriptOpt);
}
((HeadlessScript) script).setRunningInnerScript(false);
}
return;
if (provider == null) {
throw new IOException("Attempting to run subscript '" + scriptName +
"': unable to run this script type.");
}
GhidraScript script = provider.getScriptInstance(scriptSource, writer);
isHeadlessScript = script instanceof HeadlessScript ? true : false;
if (potentialPropertiesFileLocs.size() > 0) {
script.setPotentialPropertiesFileLocations(potentialPropertiesFileLocs);
}
if (scriptState == state) {
updateStateFromVariables();
}
if (isHeadlessScript) {
((HeadlessScript) script).setHeadlessInstance(headless);
((HeadlessScript) script).setRunningInnerScript(true);
}
script.setScriptArgs(scriptArguments);
script.execute(scriptState, monitor, writer);
if (scriptState == state) {
loadVariablesFromState();
}
// Resolve continuations options, if they have changed
if (isHeadlessScript) {
HeadlessContinuationOption innerScriptOpt =
((HeadlessScript) script).getHeadlessContinuationOption();
if (innerScriptOpt != null) {
resolveContinuationOptionWith(innerScriptOpt);
}
((HeadlessScript) script).setRunningInnerScript(false);
}
return;
}
throw new IllegalArgumentException("Script does not exist: " + scriptName);

View file

@ -113,8 +113,8 @@ public class ShowConstantUse extends GhidraScript {
for (int i = 0; i < clangStmt.numChildren(); i++) {
ClangNode child = clangStmt.Child(i);
if (child.equals(clangVar)) {
constLocs =
backtrackParamToConstant(f, paramIndex, tableDialog);
constLocs = backtrackParamToConstant(f, paramIndex,
tableDialog);
break;
}
if (child instanceof ClangVariableToken) {
@ -254,9 +254,8 @@ public class ShowConstantUse extends GhidraScript {
@Override
public String getColumnValue(AddressableRowObject rowObject) {
ConstUseLocation entry = (ConstUseLocation) rowObject;
Function func =
entry.getProgram().getFunctionManager().getFunctionContaining(
entry.getAddress());
Function func = entry.getProgram().getFunctionManager().getFunctionContaining(
entry.getAddress());
if (func == null) {
return "";
}
@ -336,20 +335,15 @@ public class ShowConstantUse extends GhidraScript {
}
public void runScript(String name, Program prog, Address loc) {
GhidraState scriptState =
new GhidraState(state.getTool(), state.getProject(), prog, new ProgramLocation(
prog, loc), null, null);
GhidraState scriptState = new GhidraState(state.getTool(), state.getProject(), prog,
new ProgramLocation(prog, loc), null, null);
try {
List<ResourceFile> dirs = GhidraScriptUtil.getScriptSourceDirectories();
for (ResourceFile dir : dirs) {
ResourceFile scriptSource = new ResourceFile(dir, name);
if (scriptSource.exists()) {
GhidraScriptProvider provider =
GhidraScriptUtil.getProvider(scriptSource);
GhidraScript script = provider.getScriptInstance(scriptSource, writer);
script.execute(scriptState, monitor, writer);
return;
}
ResourceFile scriptSource = GhidraScriptUtil.findScriptByName(name);
if (scriptSource != null) {
GhidraScriptProvider provider = GhidraScriptUtil.getProvider(scriptSource);
GhidraScript script = provider.getScriptInstance(scriptSource, writer);
script.execute(scriptState, monitor, writer);
return;
}
}
catch (Exception exc) {
@ -615,7 +609,8 @@ public class ShowConstantUse extends GhidraScript {
Varnode pvnode = null;
Parameter parm = f.getParameter(paramIndex);
if (parm == null) {
this.popup("Please put the cursor on a function parameter variable\nIf the function has not had it's parameters identified\nplease do so and try again");
this.popup(
"Please put the cursor on a function parameter variable\nIf the function has not had it's parameters identified\nplease do so and try again");
return constUse;
}
@ -672,9 +667,8 @@ public class ShowConstantUse extends GhidraScript {
if (refFunc == null) {
localConstUse.put(refAddr, null);
String problem =
"*** No function at " + refAddr +
".\nCould not analyze constant use past this undefined function!";
String problem = "*** No function at " + refAddr +
".\nCould not analyze constant use past this undefined function!";
addConstantProblem(tableChooserDialog, refAddr, problem);
refFunc = UndefinedFunction.findFunction(currentProgram, refAddr, monitor);
}
@ -736,11 +730,10 @@ public class ShowConstantUse extends GhidraScript {
// call dest
if (parm == null) {
constUse.put(instr.getAddress(), null);
String problem =
" *** Warning, it appears that function '" + funcVarUse.getName() +
"' at " + funcVarUse.getAddress() +
" does not have it's parameters recovered!\n" +
" Use Commit Params/Return in the decompiler on this function.";
String problem = " *** Warning, it appears that function '" +
funcVarUse.getName() + "' at " + funcVarUse.getAddress() +
" does not have it's parameters recovered!\n" +
" Use Commit Params/Return in the decompiler on this function.";
addErrorNote(instr.getAddress(), problem);
break;
}
@ -805,7 +798,8 @@ public class ShowConstantUse extends GhidraScript {
value = value & pcodeOp.getInput(1).getOffset();
}
else {
throw new InvalidInputException(" Unhandled Pcode OP " + pcodeOp.toString());
throw new InvalidInputException(
" Unhandled Pcode OP " + pcodeOp.toString());
}
break;
default:
@ -855,7 +849,8 @@ public class ShowConstantUse extends GhidraScript {
long value = def.getInput(0).getOffset();
try {
value = applyDefUseList(value, defUseList);
constUse.put(remapAddress(funcEntry, def.getOutput().getPCAddress()), value);
constUse.put(remapAddress(funcEntry, def.getOutput().getPCAddress()),
value);
println(" " + function.getName() + " " +
def.getOutput().getPCAddress() + " : 0x" + Long.toHexString(value));
}
@ -954,7 +949,7 @@ public class ShowConstantUse extends GhidraScript {
break;
case PcodeOp.PTRSUB: // Pointer + some sub element access (usually a
// structure ref)
// structure ref)
Varnode offsetVal = def.getInput(1);
if (!offsetVal.isConstant()) {
break;
@ -965,7 +960,8 @@ public class ShowConstantUse extends GhidraScript {
long value = baseVal.getOffset() + offsetVal.getOffset();
try {
value = applyDefUseList(value, defUseList);
constUse.put(remapAddress(funcEntry, def.getOutput().getPCAddress()), value);
constUse.put(remapAddress(funcEntry, def.getOutput().getPCAddress()),
value);
println(" " + function.getName() + " " +
def.getOutput().getPCAddress() + " : 0x" + Long.toHexString(value));
}
@ -997,8 +993,9 @@ public class ShowConstantUse extends GhidraScript {
// println(" Lost IT! " + vnode.getPCAddress());
}
private void followThroughGlobal(HashMap<Address, Long> constUse,
ArrayList<PcodeOp> defUseList, HighVariable hvar, ArrayList<FunctionParamUse> funcList,
private void followThroughGlobal(HashMap<Address, Long> constUse, ArrayList<PcodeOp> defUseList,
HighVariable hvar,
ArrayList<FunctionParamUse> funcList,
HashSet<SequenceNumber> doneSet) {
Address loc = hvar.getRepresentative().getAddress();
PcodeOp def = hvar.getRepresentative().getDef();
@ -1068,9 +1065,8 @@ public class ShowConstantUse extends GhidraScript {
// don't decompile the function again if it was the same as the last one
//
if (!f.getEntryPoint().equals(lastDecompiledFuncAddr)) {
lastResults =
decompInterface.decompileFunction(f,
decompInterface.getOptions().getDefaultTimeout(), monitor);
lastResults = decompInterface.decompileFunction(f,
decompInterface.getOptions().getDefaultTimeout(), monitor);
}
hfunction = lastResults.getHighFunction();

View file

@ -59,34 +59,31 @@ public class PythonScript extends GhidraScript {
throw new AssertException("Could not get Ghidra Python interpreter!");
}
}
for (ResourceFile dir : GhidraScriptUtil.getScriptSourceDirectories()) {
ResourceFile scriptSource = new ResourceFile(dir, scriptName);
if (scriptSource.exists()) {
GhidraScriptProvider provider = GhidraScriptUtil.getProvider(scriptSource);
GhidraScript ghidraScript = provider.getScriptInstance(scriptSource, writer);
if (ghidraScript == null) {
throw new IllegalArgumentException("Script does not exist: " + scriptName);
}
if (scriptState == state) {
updateStateFromVariables();
}
if (ghidraScript instanceof PythonScript) {
ghidraScript.set(scriptState, monitor, writer);
PythonScript pythonScript = (PythonScript) ghidraScript;
interpreter.execFile(pythonScript.getSourceFile(), pythonScript);
}
else {
ghidraScript.execute(scriptState, monitor, writer);
}
if (scriptState == state) {
loadVariablesFromState();
}
return;
ResourceFile scriptSource = GhidraScriptUtil.findScriptByName(scriptName);
if (scriptSource != null) {
GhidraScriptProvider provider = GhidraScriptUtil.getProvider(scriptSource);
GhidraScript ghidraScript = provider.getScriptInstance(scriptSource, writer);
if (ghidraScript == null) {
throw new IllegalArgumentException("Script does not exist: " + scriptName);
}
if (scriptState == state) {
updateStateFromVariables();
}
if (ghidraScript instanceof PythonScript) {
ghidraScript.set(scriptState, monitor, writer);
PythonScript pythonScript = (PythonScript) ghidraScript;
interpreter.execFile(pythonScript.getSourceFile(), pythonScript);
}
else {
ghidraScript.execute(scriptState, monitor, writer);
}
if (scriptState == state) {
loadVariablesFromState();
}
return;
}
throw new IllegalArgumentException("Script does not exist: " + scriptName);
}