Skip to content

Commit

Permalink
Fix OOP model and handling of environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Flipout50 committed Jan 16, 2024
1 parent 0302d4e commit 24b24b3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
27 changes: 14 additions & 13 deletions libbs/api/decompiler_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def __init__(
supports_undo: bool = False,
# these will be changed often by public API use
headless: bool = False,
headless_binary_path: Optional[str] = None,
binary: Optional[str] = None,
decompiler_headless_binary_path: Optional[str] = None,
project_binary_path: Optional[str] = None,
init_plugin: bool = False,
plugin_name: str = f"generic_libbs_plugin",
# [category/name] = (action_string, callback_func)
Expand All @@ -82,15 +82,6 @@ def __init__(
self.qt_version = qt_version
self._error_on_artifact_duplicates = error_on_artifact_duplicates

headless_path = Path(headless_binary_path)
bin_path = Path(binary)
if not headless_path.exists():
raise FileNotFoundError("Path to headless binary not found")
if not bin_path.exists():
raise FileNotFoundError("Path to binary not found")
self.headless_binary_path = headless_path
self.binary = bin_path

# GUI things
self.headless = headless
self._init_plugin = init_plugin
Expand Down Expand Up @@ -120,13 +111,23 @@ def __init__(
args = ui_init_args or []
kwargs = ui_init_kwargs or {}
self._init_ui_components(*args, **kwargs)
else:
self._init_headless_components(decompiler_headless_binary_path, project_binary_path)

#
# Headless
#

def _init_headless_components(self, *args, **kwargs):
pass
def _init_headless_components(self, decompiler_headless_path, project_binary_path):
headless_path = Path(decompiler_headless_path)
bin_path = Path(project_binary_path)
if not headless_path.exists():
raise FileNotFoundError("Path to headless binary not found")
if not bin_path.exists():
raise FileNotFoundError("Path to binary not found")

self.decompiler_headless_binary_path = headless_path
self.project_binary_path = bin_path

#
# Decompiler GUI API
Expand Down
31 changes: 17 additions & 14 deletions libbs/decompilers/ghidra/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,33 @@ def __init__(self, loop_on_plugin=True, **kwargs):

self.loop_on_plugin = loop_on_plugin

# Startup headless ghidra binary
if self.headless:
script_path = PluginInstaller.find_pkg_files("libbs") / "decompiler_stubs" / "ghidra_libbs"
tmpdir = tempfile.TemporaryDirectory()
self.headless_project = tmpdir
print(self.binary)
print(script_path)
p = subprocess.Popen([str(self.headless_binary_path),
tmpdir.name, "headless",
"-import", str(self.binary),
"-scriptPath", str(script_path),
"-postScript", "ghidra_libbs_mainthread_server.py"],)
time.sleep(10)

# Connect to the remote bridge, assumes Ghidra is already running!
if not self.connect_ghidra_bridge():
raise Exception("Failed to connect to remote Ghidra Bridge. Did you start it first?")

#
# Headless
#

def _init_headless_components(self, decompiler_headless_path, project_binary_path):
super()._init_headless_components(decompiler_headless_path, project_binary_path)
script_path = PluginInstaller.find_pkg_files("libbs") / "decompiler_stubs" / "ghidra_libbs"
tmpdir = tempfile.TemporaryDirectory()
self.headless_project = tmpdir
p = subprocess.Popen([str(self.decompiler_headless_binary_path),
tmpdir.name, "headless",
"-import", str(self.project_binary_path),
"-scriptPath", str(script_path),
"-postScript", "ghidra_libbs_mainthread_server.py"], )
time.sleep(10)

def shutdown(self):
self.ghidra.bridge.remote_shutdown()
# need to wait a sec for the server to shutdown
time.sleep(5)
self.headless_project.cleanup()


#
# Controller API
#
Expand Down
8 changes: 5 additions & 3 deletions tests/tests.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import unittest
import pathlib
import os

from libbs.api import DecompilerInterface

GHIDRA_INSTALL_DIR = os.environ.get('GHIDRA_INSTALL_DIR')
HOME_DIR = os.environ.get('HOME')

class TestHeadlessInterfaces(unittest.TestCase):
def test_ghidra_interface(self):
ci_headless_binary_path = os.path.expandvars("$GHIDRA_INSTALL_DIR/support/analyzeHeadless")
#ci_headless_binary_path = os.path.expandvars("/home/flipout/.local/bin/ghidra_10.4_PUBLIC/support/analyzeHeadless")
fauxware_path = os.path.expandvars("$HOME/fauxware")
ci_headless_binary_path = GHIDRA_INSTALL_DIR + "/support/analyzeHeadless"
fauxware_path = HOME_DIR + "/fauxware"
deci = DecompilerInterface.discover(force_decompiler="ghidra",
headless=True,
headless_binary_path=ci_headless_binary_path,
Expand Down

0 comments on commit 24b24b3

Please sign in to comment.