Skip to content

Commit

Permalink
Merge branch 'main' into go-rust-new
Browse files Browse the repository at this point in the history
  • Loading branch information
ebehner authored Nov 8, 2024
2 parents 1da337e + 101b889 commit 3990dff
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions decompiler/frontend/binaryninja/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class BinaryninjaFrontend(Frontend):
def __init__(self, bv: BinaryView):
"""Create a new binaryninja view with the given path."""
self._bv = bv if type(bv) == BinaryView else bv.getCurrentFunction().view
self._tagging = CompilerIdiomsTagging(self._bv)

@classmethod
def from_path(cls, path: str, options: Options):
Expand Down Expand Up @@ -75,8 +76,7 @@ def lift(self, task: DecompilerTask):
task.function_return_type = lifter.lift(function.return_type)
task.function_parameters = [lifter.lift(param_type) for param_type in function.type.parameters]

tagging = CompilerIdiomsTagging(self._bv, function.start, task.options)
tagging.run()
self._tagging.run(function, task.options)

task.cfg = parser.parse(function)
task.function_parameter_locations = self._parameter_locations(function)
Expand Down
21 changes: 13 additions & 8 deletions decompiler/frontend/binaryninja/tagging.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging

import binaryninja.function
from binaryninja import BinaryView
from compiler_idioms.disassembly.smda_disassembly import SMDADisassembly
from compiler_idioms.matcher import Matcher
from decompiler.util.options import Options

Expand All @@ -11,23 +13,26 @@ class CompilerIdiomsTagging:
TAG_SYMBOL = "⚙"
TAG_PREFIX = "compiler_idiom: "

def __init__(self, binary_view: BinaryView, start: int, options: Options):
def __init__(self, binary_view: BinaryView):
self._bv = binary_view
self._function_start = start
self._enabled = options.getboolean("compiler-idioms-tagging.enabled", fallback=True)
self._debug_submodules = options.getboolean("logging.debug-submodules")
self._disassembly = SMDADisassembly(self._bv.file.filename)
self._matcher = Matcher()

def run(self):
def run(self, function: binaryninja.function.Function, options: Options):
"""
Matches idioms in the function (disassembly) currently being decompiled.
For each found match creates a tag that contains info for original computation reconstruction.
"""
if not self._enabled:
enabled = options.getboolean("compiler-idioms-tagging.enabled", fallback=True)
debug_submodules = options.getboolean("logging.debug-submodules")

if not enabled:
return
try:
matches = Matcher().find_idioms_in_function(self._bv.file.filename, self._function_start)
instructions = self._disassembly.get_smda_function_at(function.start)
matches = self._matcher._match_single_function(instructions)
except Exception as e:
if self._debug_submodules:
if debug_submodules:
raise RuntimeError(e)
logging.warning("Compiler idioms matching failed, continue without compiler idioms.")
return
Expand Down

0 comments on commit 3990dff

Please sign in to comment.