mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2025-10-03 09:49:23 +02:00
Merge remote-tracking branch 'origin/GP-5981_ryanmkurtz_jython' into Ghidra_12.0
This commit is contained in:
commit
350817be2d
2 changed files with 48 additions and 19 deletions
|
@ -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)) {
|
||||||
|
|
|
@ -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();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue