Skip to content

Commit

Permalink
Remove redundant decompile methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rihi committed Feb 28, 2024
1 parent 5d8e632 commit b7b9950
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 24 deletions.
5 changes: 3 additions & 2 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
)


Expand Down
13 changes: 1 addition & 12 deletions decompile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions decompiler/util/bugfinder/bugfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
8 changes: 4 additions & 4 deletions decompiler/util/commandline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion decompiler/util/serialization/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions decompiler/util/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion test_threadsafety.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit b7b9950

Please sign in to comment.