Skip to content

Commit

Permalink
Different approach
Browse files Browse the repository at this point in the history
  • Loading branch information
rihi committed Sep 5, 2024
1 parent 3199561 commit 34dfae0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 40 deletions.
4 changes: 2 additions & 2 deletions decompiler/frontend/binaryninja/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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 @@ -70,8 +71,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, task.options)
tagging.run()
self._tagging.run(function, task.options)

task.cfg = parser.parse(function)
task.complex_types = parser.complex_types
Expand Down
17 changes: 3 additions & 14 deletions decompiler/frontend/binaryninja/handlers/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,8 @@
from typing import Union

from binaryninja import DataVariable, SymbolType, Type, mediumlevelil
from decompiler.frontend.binaryninja.handlers.globals import addr_in_section
from decompiler.frontend.lifter import Handler
from decompiler.structures.pseudo import (
Constant,
CustomType,
GlobalVariable,
Integer,
NotUseableConstant,
OperationType,
Pointer,
Symbol,
UnaryOperation,
)
from decompiler.structures.pseudo import Constant, GlobalVariable, Integer, NotUseableConstant, OperationType, Symbol, UnaryOperation

BYTE_SIZE = 8

Expand All @@ -40,8 +29,8 @@ def lift_constant(self, constant: mediumlevelil.MediumLevelILConst, **kwargs):
"""Lift the given constant value."""
if constant.constant in [math.inf, -math.inf, math.nan]:
return NotUseableConstant(str(constant.constant))
if isinstance(constant.constant, int) and addr_in_section(constant.function.view, constant.constant):
return self.lift_constant_pointer(constant)
# if isinstance(constant.constant, int) and addr_in_section(constant.function.view, constant.constant):
# return self.lift_constant_pointer(constant)
return Constant(constant.constant, vartype=self._lifter.lift(constant.expr_type))

@staticmethod
Expand Down
35 changes: 11 additions & 24 deletions decompiler/frontend/binaryninja/tagging.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import logging

import binaryninja.function
from binaryninja import BinaryView, InstructionTextTokenType
from compiler_idioms.instruction import Instruction
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 @@ -13,38 +13,25 @@ class CompilerIdiomsTagging:
TAG_SYMBOL = "⚙"
TAG_PREFIX = "compiler_idiom: "

def __init__(self, binary_view: BinaryView, function: binaryninja.function.Function, options: Options):
def __init__(self, binary_view: BinaryView):
self._bv = binary_view
self._function = function
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)

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:
instructions: list[Instruction] = []
for tokens, address in self._function.instructions:
operands: list[str] = []
operand = ""
for token in tokens[2:]:
if token.type == InstructionTextTokenType.OperandSeparatorToken:
operands.append(operand)
operand = ""
else:
operand += token.text
if operand:
operands.append(operand)

instructions.append(Instruction(address, tokens[0].text, tuple(operands)))

instructions = self._disassembly.get_smda_function_at(function.start)
matches = 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 34dfae0

Please sign in to comment.