Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-4157: Injected "this" object into python script state so users of askProgram() can release the consumer

Ryan Kurtz8d27aa3f708a
diff --git a/Ghidra/Features/Python/ghidra_scripts/AskScriptPy.py b/Ghidra/Features/Python/ghidra_scripts/AskScriptPy.py
index b3a237b220..61f79eb33f 100644
--- a/Ghidra/Features/Python/ghidra_scripts/AskScriptPy.py
+++ b/Ghidra/Features/Python/ghidra_scripts/AskScriptPy.py
@@ -31,6 +31,7 @@ from ghidra.program.model.listing import Program
from ghidra.util import Msg
from java.lang import IllegalArgumentException
+from ghidra.util.exception import CancelledException
# The presence of the AskScript.properties file in the same location (as AskScript.java)
# allows for the following behavior:
@@ -67,8 +68,12 @@ try:
#for b in bytes:
# print "b = " + str(b & 0xff)
- prog = askProgram("Please choose a program to open.")
- print "Program picked: " + prog.getName()
+ try:
+ prog = askProgram("Please choose a program to open.")
+ print "Program picked: " + prog.getName()
+ finally:
+ if prog is not None:
+ prog.release(this)
domFile = askDomainFile("Which domain file would you like?")
print "Domain file: " + domFile.getName()
@@ -102,4 +107,6 @@ try:
except IllegalArgumentException as error:
Msg.warn(self, "Error during headless processing: " + error.toString())
+except CancelledException:
+ print "Cancelled"
diff --git a/Ghidra/Features/Python/src/main/java/ghidra/python/GhidraPythonInterpreter.java b/Ghidra/Features/Python/src/main/java/ghidra/python/GhidraPythonInterpreter.java
index f9377810f2..bc54a00a72 100644
--- a/Ghidra/Features/Python/src/main/java/ghidra/python/GhidraPythonInterpreter.java
+++ b/Ghidra/Features/Python/src/main/java/ghidra/python/GhidraPythonInterpreter.java
@@ -338,6 +338,9 @@ public class GhidraPythonInterpreter extends InteractiveInterpreter {
return;
}
+ // Inject 'this'
+ setVariable("this", script);
+
// Loop though the script class hierarchy
for (Class<?> scriptClass = script.getClass(); scriptClass != Object.class; scriptClass =
scriptClass.getSuperclass()) {