GP-5981: Giving Jython scripts invoked via runScript() a fresh set of

locals
This commit is contained in:
Ryan Kurtz 2025-09-24 08:42:55 -04:00
parent 6c1a1b4600
commit 03ee787325
2 changed files with 48 additions and 19 deletions

View file

@ -50,8 +50,8 @@ public class GhidraJythonInterpreter extends InteractiveInterpreter {
private PyModule introspectModule; private PyModule introspectModule;
private PyModule builtinModule; private PyModule builtinModule;
private PyObject interrupt; private PyObject interrupt;
private boolean scriptMethodsInjected;
private boolean cleanedUp; private boolean cleanedUp;
private Stack<PyObject> localStack = new Stack<>();
/** /**
* Gets a new GhidraPythonInterpreter instance. * Gets a new GhidraPythonInterpreter instance.
@ -126,6 +126,7 @@ public class GhidraJythonInterpreter extends InteractiveInterpreter {
/** /**
* Initializes/resets the python path to include all known Ghidra script paths. * Initializes/resets the python path to include all known Ghidra script paths.
*/ */
@SuppressWarnings("deprecation")
private void initializePythonPath() { private void initializePythonPath() {
// Restore the python path back to default. // Restore the python path back to default.
@ -359,13 +360,12 @@ public class GhidraJythonInterpreter extends InteractiveInterpreter {
} }
} }
// Add public methods (only once). Ignore inner classes. // Add public methods. Ignore inner classes.
// //
// NOTE: We currently do not have a way to safely add protected methods. Disabling // NOTE: We currently do not have a way to safely add protected methods. Disabling
// python.security.respectJavaAccessibility and adding in protected methods in the below // python.security.respectJavaAccessibility and adding in protected methods in the below
// loop caused an InaccessibleObjectException for some users (relating to core Java // loop caused an InaccessibleObjectException for some users (relating to core Java
// modules, not the GhidraScript class hierarchy). // modules, not the GhidraScript class hierarchy).
if (!scriptMethodsInjected) {
for (Method method : scriptClass.getDeclaredMethods()) { for (Method method : scriptClass.getDeclaredMethods()) {
if (!method.getName().contains("$") && if (!method.getName().contains("$") &&
Modifier.isPublic(method.getModifiers())) { Modifier.isPublic(method.getModifiers())) {
@ -376,9 +376,6 @@ public class GhidraJythonInterpreter extends InteractiveInterpreter {
} }
} }
scriptMethodsInjected = true;
}
/** /**
* Safely sets a variable in the interpreter's namespace. This first checks to * Safely sets a variable in the interpreter's namespace. This first checks to
* make sure that we are not overriding a builtin Python symbol. * make sure that we are not overriding a builtin Python symbol.
@ -395,6 +392,29 @@ public class GhidraJythonInterpreter extends InteractiveInterpreter {
return false; return false;
} }
/**
* Saves the interpreter's locals to a stack. These locals can be restored with the
* {@link #restoreLocals()} method.
*/
public void saveLocals() {
localStack.push(getLocals());
}
/**
* Clears the interpreter's locals
*/
public void clearLocals() {
setLocals(new PyStringMap());
}
/**
* Restores the locals that were last saved with the {@link #saveLocals()} method, and removes
* them from the stack.
*/
public void restoreLocals() {
setLocals(localStack.pop());
}
/** /**
* Sets a bound (callback/function pointer) method as a local variable in the interpreter. * Sets a bound (callback/function pointer) method as a local variable in the interpreter.
* *
@ -427,6 +447,8 @@ public class GhidraJythonInterpreter extends InteractiveInterpreter {
Msg.error(this, Msg.error(this,
"Method " + methodName + " of " + obj + " attempting to shadow method " + "Method " + methodName + " of " + obj + " attempting to shadow method " +
pyMethod.__func__ + " of " + pyMethod.__self__); pyMethod.__func__ + " of " + pyMethod.__self__);
set(methodName, new PyMethod(new PyReflectedFunction(method), Py.java2py(obj),
Py.java2py(obj.getClass())));
return false; return false;
} }
if (!(pyMethod.__func__ instanceof PyReflectedFunction)) { if (!(pyMethod.__func__ instanceof PyReflectedFunction)) {

View file

@ -71,6 +71,9 @@ public class JythonScript extends GhidraScript {
updateStateFromVariables(); updateStateFromVariables();
} }
try {
interpreter.saveLocals();
interpreter.clearLocals();
if (ghidraScript instanceof JythonScript) { if (ghidraScript instanceof JythonScript) {
ghidraScript.set(scriptState); ghidraScript.set(scriptState);
JythonScript jythonScript = (JythonScript) ghidraScript; JythonScript jythonScript = (JythonScript) ghidraScript;
@ -79,6 +82,10 @@ public class JythonScript extends GhidraScript {
else { else {
ghidraScript.execute(scriptState, getControls()); ghidraScript.execute(scriptState, getControls());
} }
}
finally {
interpreter.restoreLocals();
}
if (scriptState == state) { if (scriptState == state) {
loadVariablesFromState(); loadVariablesFromState();