From 82c1899c470bbad2281b35293b28882d3db8693e Mon Sep 17 00:00:00 2001 From: rihi <19492038+rihi@users.noreply.github.com> Date: Thu, 22 Feb 2024 12:23:01 +0100 Subject: [PATCH] Remove redundant decompile methods --- __init__.py | 5 +++-- decompile.py | 13 +------------ decompiler/util/bugfinder/bugfinder.py | 4 ++-- decompiler/util/commandline.py | 8 ++++---- decompiler/util/serialization/demo.py | 3 ++- decompiler/util/widget.py | 4 ++-- test_threadsafety.py | 2 +- 7 files changed, 15 insertions(+), 24 deletions(-) diff --git a/__init__.py b/__init__.py index 7c43afacd..bdeebda7e 100644 --- a/__init__.py +++ b/__init__.py @@ -22,9 +22,10 @@ def decompile(bv: BinaryView, function: Function): """Decompile the target mlil_function.""" decompiler = Decompiler.from_raw(bv) options = Options.from_gui() - task = decompiler.decompile(function, options) + result = decompiler.decompile([function], task_options=options) show_html_report( - f"decompile {task.name}", DecoratedCode.generate_html_from_code(task.code, task.options.getstring("code-generator.style_plugin")) + f"decompile {result.tasks[0].name}", + DecoratedCode.generate_html_from_code(result.code, options.getstring("code-generator.style_plugin")), ) diff --git a/decompile.py b/decompile.py index 9d373893e..d18c8b312 100755 --- a/decompile.py +++ b/decompile.py @@ -41,7 +41,7 @@ def from_raw(cls, data, frontend: Frontend = BinaryninjaFrontend) -> Decompiler: """Create a decompiler instance from existing frontend instance (e.g. a binaryninja view).""" return cls(frontend.from_raw(data)) - def _decompile(self, function_ids: Collection[object] | None = None, task_options: Options | None = None) -> Result: + def decompile(self, function_ids: Collection[object] | None = None, task_options: Options | None = None) -> Result: if function_ids is None: # decompile all functions when none are specified function_ids = self._frontend.get_all_function_names() if task_options is None: @@ -67,17 +67,6 @@ def _decompile(self, function_ids: Collection[object] | None = None, task_option code ) - def decompile(self, function_id: object, task_options: Options | None = None) -> DecompilerTask: - """Decompile the target function.""" - output = self._decompile([function_id], task_options) - output.tasks[0].code = output.code # because bad api design... - return output.tasks[0] - - def decompile_all(self, task_options: Optional[Options] = None) -> str: - """Decompile all functions in the binary""" - function_ids = self._frontend.get_all_function_names() - return self._decompile(function_ids, task_options).code - @dataclass class Result: tasks: list[DecompilerTask] diff --git a/decompiler/util/bugfinder/bugfinder.py b/decompiler/util/bugfinder/bugfinder.py index 22b7ec9d6..3bc0d72fa 100644 --- a/decompiler/util/bugfinder/bugfinder.py +++ b/decompiler/util/bugfinder/bugfinder.py @@ -198,9 +198,9 @@ def iter_function_reports(self, sample) -> Iterator[dict]: function_info = DBConnector.get_function_info(function) try: time1 = time.time() - task_result = self.decompile(function, options) + result = self.decompile([function], task_options=options) time2 = time.time() - decompilation_info = DBConnector.get_successful_info(task_result.code, int(time2 - time1)) + decompilation_info = DBConnector.get_successful_info(result.code, int(time2 - time1)) except Exception as e: decompilation_info = DBConnector.get_error_info(e) yield {**dewolf_info, **sample_info, **function_info, **decompilation_info} diff --git a/decompiler/util/commandline.py b/decompiler/util/commandline.py index 56449281d..d4f467259 100644 --- a/decompiler/util/commandline.py +++ b/decompiler/util/commandline.py @@ -72,15 +72,15 @@ def main(interface: "Decompiler"): try: if args.all or not args.function: # decompile all functions. - undecorated_code = decompiler.decompile_all(options) + result = decompiler.decompile(task_options=options) DecoratedCode.print_code( - undecorated_code, output_stream, color, style=options.getstring("code-generator.style_cmd", fallback="paraiso-dark") + result.code, output_stream, color, style=options.getstring("code-generator.style_cmd", fallback="paraiso-dark") ) else: for function_name in args.function: - task = decompiler.decompile(function_name, options) + result = decompiler.decompile([function_name], task_options=options) DecoratedCode.print_code( - task.code, output_stream, color, style=task.options.getstring("code-generator.style_cmd", fallback="paraiso-dark") + result.code, output_stream, color, style=options.getstring("code-generator.style_cmd", fallback="paraiso-dark") ) finally: if output_stream is not None: diff --git a/decompiler/util/serialization/demo.py b/decompiler/util/serialization/demo.py index 9083ca249..1efa1eb7d 100644 --- a/decompiler/util/serialization/demo.py +++ b/decompiler/util/serialization/demo.py @@ -5,7 +5,8 @@ from decompile import Decompiler decompiler = Decompiler.from_path("tests/samples/bin/systemtests/32/0/test_loop") -task = decompiler.decompile("test7") +result = decompiler.decompile(["test7"]) +task = result.tasks[0] # Serialize an AST from decompiler.util.serialization.ast_serializer import AstSerializer diff --git a/decompiler/util/widget.py b/decompiler/util/widget.py index eb400c5bf..bd84c7b2e 100644 --- a/decompiler/util/widget.py +++ b/decompiler/util/widget.py @@ -64,8 +64,8 @@ def decompile_for_widget(binary_view: BinaryView, function: Function): configure_logging() # reload settings decompiler = Decompiler.from_raw(binary_view) options = Options.from_gui() - task = decompiler.decompile(function, options) - return DecoratedCode.formatted_plain(task.code) + result = decompiler.decompile([function], task_options=options) + return DecoratedCode.formatted_plain(result.code) class CodeDisplay(QPlainTextEdit): diff --git a/test_threadsafety.py b/test_threadsafety.py index 5c5673831..c8bed470d 100644 --- a/test_threadsafety.py +++ b/test_threadsafety.py @@ -13,7 +13,7 @@ with ThreadPoolExecutor(max_workers=2) as executor: for function in decompiler._frontend.get_all_function_names(): - results.append(executor.submit(decompiler.decompile, function)) + results.append(executor.submit(decompiler.decompile, [function])) for future in results: future.exception()