Skip to content

Ghidra is a software reverse engineering (SRE) framework

update python & headless tests to manage GhidraScriptUtil resource

Jason P. Leasure5f11c9cf7d76
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 50e0e8ebc0..c647d85194 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
@@ -49,8 +49,14 @@ public class GhidraScriptRunner implements GhidraLaunchable {
System.exit(0);
}
String logFile = null; //TODO get from arguments?
- initialize(layout, logFile, true);
- runScript(args[0]);
+ GhidraScriptUtil.initialize(new BundleHost(), scriptPaths);
+ try {
+ initialize(layout, logFile, true);
+ runScript(args[0]);
+ }
+ finally {
+ GhidraScriptUtil.dispose();
+ }
}
private void runScript(String string) throws Exception {
@@ -188,8 +194,6 @@ public class GhidraScriptRunner implements GhidraLaunchable {
* Gather paths where scripts may be found.
*/
private void initializeScriptPaths() {
- GhidraScriptUtil.initialize(new BundleHost(), scriptPaths);
-
StringBuffer buf = new StringBuffer("HEADLESS Script Paths:");
for (ResourceFile dir : GhidraScriptUtil.getScriptSourceDirectories()) {
buf.append("\n ");
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 25b2e11248..bfded77431 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
@@ -293,56 +293,62 @@ public class HeadlessAnalyzer {
}
}
- initializeScriptPaths();
- compileScripts();
+ GhidraScriptUtil.initialize(new BundleHost(), options.scriptPaths);
+ try {
+ initializeScriptPaths();
+ compileScripts();
- Msg.info(HeadlessAnalyzer.class, "HEADLESS: execution starts");
+ Msg.info(HeadlessAnalyzer.class, "HEADLESS: execution starts");
- GhidraURLConnection c = (GhidraURLConnection) ghidraURL.openConnection();
- c.setReadOnly(options.readOnly); // writable repository connection
+ GhidraURLConnection c = (GhidraURLConnection) ghidraURL.openConnection();
+ c.setReadOnly(options.readOnly); // writable repository connection
- if (c.getRepositoryName() == null) {
- throw new MalformedURLException("Unsupported repository URL: " + ghidraURL);
- }
+ if (c.getRepositoryName() == null) {
+ throw new MalformedURLException("Unsupported repository URL: " + ghidraURL);
+ }
- Msg.info(this, "Opening ghidra repository project: " + ghidraURL);
- Object obj = c.getContent();
- if (!(obj instanceof GhidraURLWrappedContent)) {
- throw new IOException(
- "Connect to repository folder failed. Response code: " + c.getResponseCode());
- }
- GhidraURLWrappedContent wrappedContent = (GhidraURLWrappedContent) obj;
- Object content = null;
- try {
- content = wrappedContent.getContent(this);
- if (!(content instanceof DomainFolder)) {
- throw new IOException("Connect to repository folder failed");
+ Msg.info(this, "Opening ghidra repository project: " + ghidraURL);
+ Object obj = c.getContent();
+ if (!(obj instanceof GhidraURLWrappedContent)) {
+ throw new IOException(
+ "Connect to repository folder failed. Response code: " + c.getResponseCode());
}
+ GhidraURLWrappedContent wrappedContent = (GhidraURLWrappedContent) obj;
+ Object content = null;
+ try {
+ content = wrappedContent.getContent(this);
+ if (!(content instanceof DomainFolder)) {
+ throw new IOException("Connect to repository folder failed");
+ }
- DomainFolder folder = (DomainFolder) content;
- project = new HeadlessProject(getProjectManager(), c);
+ DomainFolder folder = (DomainFolder) content;
+ project = new HeadlessProject(getProjectManager(), c);
- if (!checkUpdateOptions()) {
- return; // TODO: Should an exception be thrown?
- }
+ if (!checkUpdateOptions()) {
+ return; // TODO: Should an exception be thrown?
+ }
- if (options.runScriptsNoImport) {
- processNoImport(folder.getPathname());
+ if (options.runScriptsNoImport) {
+ processNoImport(folder.getPathname());
+ }
+ else {
+ processWithImport(folder.getPathname(), filesToImport);
+ }
}
- else {
- processWithImport(folder.getPathname(), filesToImport);
+ catch (NotFoundException e) {
+ throw new IOException("Connect to repository folder failed");
+ }
+ finally {
+ if (content != null) {
+ wrappedContent.release(content, this);
+ }
+ if (project != null) {
+ project.close();
+ }
}
- }
- catch (NotFoundException e) {
- throw new IOException("Connect to repository folder failed");
}
finally {
- if (content != null) {
- wrappedContent.release(content, this);
- }
- if (project != null) {
- project.close();
- }
+ GhidraScriptUtil.dispose();
}
}
@@ -394,53 +400,59 @@ public class HeadlessAnalyzer {
}
}
- initializeScriptPaths();
- compileScripts();
+ GhidraScriptUtil.initialize(new BundleHost(), options.scriptPaths);
+ try {
+ initializeScriptPaths();
+ compileScripts();
- Msg.info(HeadlessAnalyzer.class, "HEADLESS: execution starts");
+ Msg.info(HeadlessAnalyzer.class, "HEADLESS: execution starts");
- File dir = new File(projectLocation);
- ProjectLocator locator = new ProjectLocator(dir.getAbsolutePath(), projectName);
+ File dir = new File(projectLocation);
+ ProjectLocator locator = new ProjectLocator(dir.getAbsolutePath(), projectName);
- if (locator.getProjectDir().exists()) {
- project = openProject(locator);
- }
- else {
- if (options.runScriptsNoImport) {
- Msg.error(this, "Could not find project: " + locator +
- " -- should already exist in -process mode.");
- throw new IOException("Could not find project: " + locator);
+ if (locator.getProjectDir().exists()) {
+ project = openProject(locator);
}
+ else {
+ if (options.runScriptsNoImport) {
+ Msg.error(this, "Could not find project: " + locator +
+ " -- should already exist in -process mode.");
+ throw new IOException("Could not find project: " + locator);
+ }
- if (!options.runScriptsNoImport && options.readOnly) {
- // assume temporary when importing with readOnly option
- options.deleteProject = true;
- }
+ if (!options.runScriptsNoImport && options.readOnly) {
+ // assume temporary when importing with readOnly option
+ options.deleteProject = true;
+ }
- Msg.info(this,
- "Creating " + (options.deleteProject ? "temporary " : "") + "project: " + locator);
- project = getProjectManager().createProject(locator, null, false);
- }
+ Msg.info(this, "Creating " + (options.deleteProject ? "temporary " : "") +
+ "project: " + locator);
+ project = getProjectManager().createProject(locator, null, false);
+ }
- try {
+ try {
- if (!checkUpdateOptions()) {
- return; // TODO: Should an exception be thrown?
- }
+ if (!checkUpdateOptions()) {
+ return; // TODO: Should an exception be thrown?
+ }
- if (options.runScriptsNoImport) {
- processNoImport(rootFolderPath);
+ if (options.runScriptsNoImport) {
+ processNoImport(rootFolderPath);
+ }
+ else {
+ processWithImport(rootFolderPath, filesToImport);
+ }
}
- else {
- processWithImport(rootFolderPath, filesToImport);
+ finally {
+ project.close();
+ if (!options.runScriptsNoImport && options.deleteProject) {
+ FileUtilities.deleteDir(locator.getProjectDir());
+ locator.getMarkerFile().delete();
+ }
}
}
finally {
- project.close();
- if (!options.runScriptsNoImport && options.deleteProject) {
- FileUtilities.deleteDir(locator.getProjectDir());
- locator.getMarkerFile().delete();
- }
+ GhidraScriptUtil.dispose();
}
}
@@ -660,8 +672,6 @@ public class HeadlessAnalyzer {
* Gather paths where scripts may be found.
*/
private void initializeScriptPaths() {
- GhidraScriptUtil.initialize(new BundleHost(), options.scriptPaths);
-
StringBuffer buf = new StringBuffer("HEADLESS Script Paths:");
for (ResourceFile dir : GhidraScriptUtil.getScriptSourceDirectories()) {
buf.append("\n ");
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/AbstractGhidraScriptMgrPluginTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/AbstractGhidraScriptMgrPluginTest.java
index c3a3b7ea11..c3bbbbaacf 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/AbstractGhidraScriptMgrPluginTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/AbstractGhidraScriptMgrPluginTest.java
@@ -87,8 +87,6 @@ public abstract class AbstractGhidraScriptMgrPluginTest
@Before
public void setUp() throws Exception {
- System.err.printf("===== Starting %s ======\n", testName.getMethodName());
-
setErrorGUIEnabled(false);
// change the eclipse port so that Eclipse doesn't try to edit the script when
diff --git a/Ghidra/Features/Python/src/test.slow/java/ghidra/python/PythonInterpreterTest.java b/Ghidra/Features/Python/src/test.slow/java/ghidra/python/PythonInterpreterTest.java
index 6bcdf60dde..2e7e1bff47 100644
--- a/Ghidra/Features/Python/src/test.slow/java/ghidra/python/PythonInterpreterTest.java
+++ b/Ghidra/Features/Python/src/test.slow/java/ghidra/python/PythonInterpreterTest.java
@@ -22,6 +22,8 @@ import java.io.ByteArrayOutputStream;
import org.junit.*;
import generic.jar.ResourceFile;
+import ghidra.app.plugin.core.osgi.BundleHost;
+import ghidra.app.script.GhidraScriptUtil;
import ghidra.test.AbstractGhidraHeadedIntegrationTest;
/**
@@ -35,6 +37,7 @@ public class PythonInterpreterTest extends AbstractGhidraHeadedIntegrationTest {
@Before
public void setUp() throws Exception {
out = new ByteArrayOutputStream();
+ GhidraScriptUtil.initialize(new BundleHost(), null);
interpreter = GhidraPythonInterpreter.get();
interpreter.setOut(out);
interpreter.setErr(out);
@@ -44,6 +47,7 @@ public class PythonInterpreterTest extends AbstractGhidraHeadedIntegrationTest {
public void tearDown() throws Exception {
out.reset();
interpreter.cleanup();
+ GhidraScriptUtil.dispose();
}
/**
@@ -81,7 +85,7 @@ public class PythonInterpreterTest extends AbstractGhidraHeadedIntegrationTest {
@Test
public void testPythonCleanupInvalidation() {
interpreter.cleanup();
-
+
try {
interpreter.push("pass", null);
fail("Push still worked after interpreter cleanup.");
diff --git a/Ghidra/Features/Python/src/test.slow/java/ghidra/python/PythonPluginTest.java b/Ghidra/Features/Python/src/test.slow/java/ghidra/python/PythonPluginTest.java
index d12044fee9..c4e5544b6f 100644
--- a/Ghidra/Features/Python/src/test.slow/java/ghidra/python/PythonPluginTest.java
+++ b/Ghidra/Features/Python/src/test.slow/java/ghidra/python/PythonPluginTest.java
@@ -15,10 +15,12 @@
*/
package ghidra.python;
-import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.*;
import org.junit.*;
+import ghidra.app.plugin.core.osgi.BundleHost;
+import ghidra.app.script.GhidraScriptUtil;
import ghidra.framework.plugintool.PluginTool;
import ghidra.test.AbstractGhidraHeadedIntegrationTest;
import ghidra.test.TestEnv;
@@ -36,12 +38,14 @@ public class PythonPluginTest extends AbstractGhidraHeadedIntegrationTest {
public void setUp() throws Exception {
env = new TestEnv();
tool = env.getTool();
+ GhidraScriptUtil.initialize(new BundleHost(), null);
tool.addPlugin(PythonPlugin.class.getName());
plugin = env.getPlugin(PythonPlugin.class);
}
@After
public void tearDown() throws Exception {
+ GhidraScriptUtil.dispose();
env.dispose();
}
diff --git a/Ghidra/Features/Python/src/test.slow/java/ghidra/python/PythonScriptTest.java b/Ghidra/Features/Python/src/test.slow/java/ghidra/python/PythonScriptTest.java
index 76fb16d469..9c79e69e9f 100644
--- a/Ghidra/Features/Python/src/test.slow/java/ghidra/python/PythonScriptTest.java
+++ b/Ghidra/Features/Python/src/test.slow/java/ghidra/python/PythonScriptTest.java
@@ -24,6 +24,8 @@ import org.junit.*;
import generic.jar.ResourceFile;
import ghidra.app.plugin.core.console.ConsolePlugin;
+import ghidra.app.plugin.core.osgi.BundleHost;
+import ghidra.app.script.GhidraScriptUtil;
import ghidra.app.script.GhidraState;
import ghidra.app.services.ConsoleService;
import ghidra.framework.Application;
@@ -45,12 +47,14 @@ public class PythonScriptTest extends AbstractGhidraHeadedIntegrationTest {
public void setUp() throws Exception {
env = new TestEnv();
tool = env.getTool();
+ GhidraScriptUtil.initialize(new BundleHost(), null);
tool.addPlugin(ConsolePlugin.class.getName());
console = tool.getService(ConsoleService.class);
}
@After
public void tearDown() throws Exception {
+ GhidraScriptUtil.dispose();
env.dispose();
}