Skip to content

Commit

Permalink
start with some updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ebehner committed Nov 7, 2024
1 parent af570df commit 97e79af
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 28 deletions.
9 changes: 6 additions & 3 deletions decompiler/frontend/binaryninja/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import logging
from typing import List

import binaryninja
from binaryninja import BinaryView
Expand Down Expand Up @@ -87,9 +88,11 @@ def lift(self, task: DecompilerTask):
if task.options.getboolean("pipeline.debug", fallback=False):
raise e

def _parameter_locations(self, function: binaryninja.function.Function) -> list[str | None]:
"""For a given Binary Ninja Function, this method returns a list of its parameters' locations in the correct order.
E.g. if the first parameter is stored in r14, the first entry in the returned list will be 'r14'."""
def _parameter_locations(self, function: binaryninja.function.Function) -> List[str | None]:
"""
For a given Binary Ninja Function, this method returns a list of its parameters' locations in the correct order.
E.g. if the first parameter is stored in r14, the first entry in the returned list will be 'r14'.
"""
raw_parameters = function.type.parameters
parameter_locations = []
for parameter in raw_parameters:
Expand Down
19 changes: 9 additions & 10 deletions decompiler/frontend/binaryninja/handlers/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
OperationType,
Pointer,
Symbol,
UnaryOperation,
UnaryOperation, FunctionSymbol,
)

BYTE_SIZE = 8
Expand Down Expand Up @@ -61,18 +61,17 @@ def lift_constant_pointer(self, pointer: mediumlevelil.MediumLevelILConstPtr, **
res = self._lifter.lift(variable, view=view, parent=pointer)

elif (symbol := view.get_symbol_at(pointer.constant)) and symbol.type != SymbolType.DataSymbol:
result = self._lifter.lift(symbol)
can_return = None
try:
can_return = view.get_function_at(pointer.constant).can_return.value
except Exception:
pass
result.can_return = can_return
if isinstance(result := self._lifter.lift(symbol), FunctionSymbol):
try:
result.can_return = view.get_function_at(pointer.constant).can_return.value
return result
except Exception:
pass
return result

elif function := view.get_function_at(pointer.constant):
result = self._lifter.lift(function.symbol)
result.can_return = function.can_return.value
if isinstance(result := self._lifter.lift(function.symbol), FunctionSymbol):
result.can_return = function.can_return.value
return result

else:
Expand Down
9 changes: 5 additions & 4 deletions decompiler/frontend/binaryninja/rust_string_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class RustStringDetection:

def __init__(self, binary_view: BinaryView, options: Options):
self._bv = binary_view
self._enabled = options.getboolean("rust-string-detection.enabled", fallback=True)
self._rust_binaries_only = options.getboolean("rust-string-detection.rust_binaries_only", fallback=True)
self._string_slicer_path = options.getstring("rust-string-detection.string_slicer_path")
self._debug_submodules = options.getboolean("logging.debug-submodules")
self._enabled = options.getboolean("rust-string-detection.enabled", fallback=False)
self._rust_binaries_only = options.getboolean("rust-string-detection.rust_binaries_only", fallback=False)
self._string_slicer_path = options.getstring("rust-string-detection.string_slicer_path", fallback="")
self._debug_submodules = options.getboolean("logging.debug-submodules", fallback=False)

def is_rust_binary(self):
"""
Expand All @@ -40,6 +40,7 @@ def run(self):
String Slicer's path will be added to Python's path before importing the module.
"""
if not self._enabled:
logging.info("Rust String Slicer not executed")
return

if self._rust_binaries_only and not self.is_rust_binary():
Expand Down
11 changes: 2 additions & 9 deletions decompiler/pipeline/preprocessing/remove_stack_canary.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ def run(self, task: DecompilerTask):
for fail_node in list(self._contains_stack_check_fail()):
self._patch_canary(fail_node)

def _get_called_functions(self, instructions):
"""
Yields all functions called by an instruction
"""
for instruction in instructions:
if isinstance(instruction, Assignment) and isinstance(instruction.value, Call):
yield instruction.value.function

def _contains_stack_check_fail(self) -> Iterator[BasicBlock]:
"""
Iterate leaf nodes of cfg, yield nodes containing canary check.
Expand All @@ -53,7 +45,8 @@ def _is_stack_chk_fail(self, node: BasicBlock) -> bool:
return any(self.STACK_FAIL_STR in str(inst) for inst in node.instructions) or self._reached_by_failed_canary_check(node)

def _reached_by_failed_canary_check(self, node: BasicBlock) -> bool:
"""Determine if the given `node` is reached by a failed stack canary check.
"""
Determine if the given `node` is reached by a failed stack canary check.
This function checks if any incoming edges to the `node` are conditional branches
that failed a stack canary check. It examines the predecessor nodes to see if the
Expand Down
2 changes: 1 addition & 1 deletion decompiler/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DecompilerTask:
ast: AbstractSyntaxTree | None = None
function_return_type: Type = Integer.int32_t()
function_parameters: List[Variable] = field(default_factory=list)
function_parameter_locations: List[str | None] = (field(default_factory=list),)
function_parameter_locations: List[str | None] = field(default_factory=list)
complex_types: ComplexTypeMap = field(default_factory=ComplexTypeMap)

_failure_origin: str | None = field(default=None, init=False)
Expand Down
2 changes: 1 addition & 1 deletion decompiler/util/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
},
{
"dest": "rust-string-detection.rust_binaries_only",
"default": true,
"default": false,
"title": "Restrict string slice detection to Rust binaries",
"type": "boolean",
"description": "string slices will only be detected for Rust binaries",
Expand Down

0 comments on commit 97e79af

Please sign in to comment.