Skip to content

Ghidra is a software reverse engineering (SRE) framework

avoid calling GhidraScriptUtils.getScriptSourceDirectories

Jason P. Leasure7ee24670161c
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptActionManager.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptActionManager.java
index 99ee86620d..c8bd052563 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptActionManager.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptActionManager.java
@@ -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) {
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScript.java b/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScript.java
index d1a295363d..9aad8c20b8 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScript.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScript.java
@@ -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);
-
- if (provider == null) {
- throw new IOException("Attempting to run subscript '" + scriptName +
- "': unable to run this script type.");
- }
+ ResourceFile scriptSource = GhidraScriptUtil.findScriptByName(scriptName);
+ if (scriptSource != null) {
+ GhidraScriptProvider provider = GhidraScriptUtil.getProvider(scriptSource);
- GhidraScript script = provider.getScriptInstance(scriptSource, writer);
- script.setScriptArgs(scriptArguments);
+ if (provider == null) {
+ throw new IOException("Attempting to run subscript '" + scriptName +
+ "': unable to run this script type.");
+ }
- if (potentialPropertiesFileLocs.size() > 0) {
- script.setPotentialPropertiesFileLocations(potentialPropertiesFileLocs);
- }
+ GhidraScript script = provider.getScriptInstance(scriptSource, writer);
+ script.setScriptArgs(scriptArguments);
- if (scriptState == state) {
- updateStateFromVariables();
- }
+ if (potentialPropertiesFileLocs.size() > 0) {
+ script.setPotentialPropertiesFileLocations(potentialPropertiesFileLocs);
+ }
- script.execute(scriptState, monitor, writer);
+ if (scriptState == state) {
+ updateStateFromVariables();
+ }
- if (scriptState == state) {
- loadVariablesFromState();
- }
- return;
+ script.execute(scriptState, monitor, writer);
+
+ if (scriptState == state) {
+ loadVariablesFromState();
}
+ return;
}
boolean shouldContinue = false;
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/GhidraScriptRunner.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/GhidraScriptRunner.java
index 3c3b90455d..50e0e8ebc0 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/GhidraScriptRunner.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/GhidraScriptRunner.java
@@ -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);
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/HeadlessAnalyzer.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/HeadlessAnalyzer.java
index c43828a4f6..25b2e11248 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/HeadlessAnalyzer.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/HeadlessAnalyzer.java
@@ -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);
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/HeadlessScript.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/HeadlessScript.java
index 66c742d80f..b991d6212f 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/HeadlessScript.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/headless/HeadlessScript.java
@@ -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 (provider == null) {
+ throw new IOException("Attempting to run subscript '" + scriptName +
+ "': unable to run this script type.");
+ }
- if (potentialPropertiesFileLocs.size() > 0) {
- script.setPotentialPropertiesFileLocations(potentialPropertiesFileLocs);
- }
+ GhidraScript script = provider.getScriptInstance(scriptSource, writer);
+ isHeadlessScript = script instanceof HeadlessScript ? true : false;
- if (scriptState == state) {
- updateStateFromVariables();
- }
+ if (potentialPropertiesFileLocs.size() > 0) {
+ script.setPotentialPropertiesFileLocations(potentialPropertiesFileLocs);
+ }
- if (isHeadlessScript) {
- ((HeadlessScript) script).setHeadlessInstance(headless);
- ((HeadlessScript) script).setRunningInnerScript(true);
- }
+ if (scriptState == state) {
+ updateStateFromVariables();
+ }
- script.setScriptArgs(scriptArguments);
+ if (isHeadlessScript) {
+ ((HeadlessScript) script).setHeadlessInstance(headless);
+ ((HeadlessScript) script).setRunningInnerScript(true);
+ }
- script.execute(scriptState, monitor, writer);
+ script.setScriptArgs(scriptArguments);
- if (scriptState == state) {
- loadVariablesFromState();
- }
+ script.execute(scriptState, monitor, writer);
- // Resolve continuations options, if they have changed
- if (isHeadlessScript) {
- HeadlessContinuationOption innerScriptOpt =
- ((HeadlessScript) script).getHeadlessContinuationOption();
+ if (scriptState == state) {
+ loadVariablesFromState();
+ }
- if (innerScriptOpt != null) {
- resolveContinuationOptionWith(innerScriptOpt);
- }
+ // Resolve continuations options, if they have changed
+ if (isHeadlessScript) {
+ HeadlessContinuationOption innerScriptOpt =
+ ((HeadlessScript) script).getHeadlessContinuationOption();
- ((HeadlessScript) script).setRunningInnerScript(false);
+ if (innerScriptOpt != null) {
+ resolveContinuationOptionWith(innerScriptOpt);
}
- return;
+ ((HeadlessScript) script).setRunningInnerScript(false);
}
+
+ return;
}
throw new IllegalArgumentException("Script does not exist: " + scriptName);
diff --git a/Ghidra/Features/Decompiler/ghidra_scripts/ShowConstantUse.java b/Ghidra/Features/Decompiler/ghidra_scripts/ShowConstantUse.java
index f009692475..0192e7d58e 100644
--- a/Ghidra/Features/Decompiler/ghidra_scripts/ShowConstantUse.java
+++ b/Ghidra/Features/Decompiler/ghidra_scripts/ShowConstantUse.java
@@ -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();
diff --git a/Ghidra/Features/Python/src/main/java/ghidra/python/PythonScript.java b/Ghidra/Features/Python/src/main/java/ghidra/python/PythonScript.java
index b21ce8ee49..2ae177a8b0 100644
--- a/Ghidra/Features/Python/src/main/java/ghidra/python/PythonScript.java
+++ b/Ghidra/Features/Python/src/main/java/ghidra/python/PythonScript.java
@@ -59,34 +59,31 @@ public class PythonScript extends GhidraScript {
throw new AssertException("Could not get Ghidra Python interpreter!");
}
}
+ 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);
+ }
- 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;
+ if (scriptState == state) {
+ loadVariablesFromState();
}
+ return;
}
throw new IllegalArgumentException("Script does not exist: " + scriptName);
}