Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-6288: PyGhidra now by default restores sys.modules each time a script is run, or the interactive interpreter is reset.

Ryan Kurtzd5b9582fd57b
diff --git a/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/internal/plugin/plugin.py b/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/internal/plugin/plugin.py
index b7629e021e..b19cc0f06d 100644
--- a/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/internal/plugin/plugin.py
+++ b/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/internal/plugin/plugin.py
@@ -30,6 +30,7 @@ from ghidra.pyghidra import PyGhidraScriptProvider, PyGhidraPlugin
from ghidra.pyghidra.interpreter import PyGhidraConsole
from ghidra.util.task import TaskMonitor
from java.io import BufferedReader, InputStreamReader # type:ignore @UnresolvedImport
+from java.lang import Boolean # type:ignore @UnresolvedImport
from java.lang import String # type:ignore @UnresolvedImport
from java.lang import Thread as JThread # type:ignore @UnresolvedImport
from java.util import Collections # type:ignore @UnresolvedImport
@@ -171,6 +172,7 @@ class PyConsole(InteractiveConsole):
self._script.set(state, ScriptControls(console, TaskMonitor.DUMMY))
self._state = ConsoleState.RESET
self._completer = PythonCodeCompleter(self)
+ self._orig_modules = sys.modules.copy()
def raw_input(self, prompt=''):
self._console.setPrompt(prompt)
@@ -238,6 +240,11 @@ class PyConsole(InteractiveConsole):
# this resets the locals, and gets a new code compiler
super().__init__(locals=PyGhidraScript(self._script))
+
+ # restore sys.modules to its original state
+ if not Boolean.getBoolean("pyghidra.sys.modules.restore.disable"):
+ for k in list(set(sys.modules) - set(self._orig_modules)):
+ sys.modules.pop(k, None)
@property
def name(self) -> str:
diff --git a/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/script.py b/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/script.py
index e272620277..200e4fdb4b 100644
--- a/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/script.py
+++ b/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/script.py
@@ -270,7 +270,7 @@ class PyGhidraScript(dict):
finally:
sys.argv = orig_argv
- if Boolean.getBoolean("pyghidra.sys.modules.restore"):
+ if not Boolean.getBoolean("pyghidra.sys.modules.restore.disable"):
for k in list(set(sys.modules) - set(orig_modules)):
sys.modules.pop(k, None)
diff --git a/Ghidra/RuntimeScripts/Common/support/launch.properties b/Ghidra/RuntimeScripts/Common/support/launch.properties
index aaf0472b08..47d8b5d529 100644
--- a/Ghidra/RuntimeScripts/Common/support/launch.properties
+++ b/Ghidra/RuntimeScripts/Common/support/launch.properties
@@ -159,7 +159,6 @@ VMARGS=--enable-native-access=ALL-UNNAMED
# Disables the loaders echoing their message log to the application.log file
#VMARGS=-Ddisable.loader.logging=true
-# Restores sys.modules to its prior state after a PyGhidra script is run so the next time the script
-# is run, it freshly reloads all of its imported modules again. This is experimental and should only
-# be enabled if necessary.
-#VMARGS=-Dpyghidra.sys.modules.restore=true
+# Prevents PyGhidra from restoring sys.modules to its original state after a PyGhidra script is run.
+# Used to help debug potential issues that might arise from mucking with sys.modules.
+#VMARGS=-Dpyghidra.sys.modules.restore.disable=true