Skip to content

Commit

Permalink
apply black
Browse files Browse the repository at this point in the history
  • Loading branch information
ebehner committed Oct 25, 2023
1 parent 331ee9d commit 083bba9
Show file tree
Hide file tree
Showing 59 changed files with 644 additions and 681 deletions.
2 changes: 1 addition & 1 deletion decompiler/backend/cexpressiongenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def _format_string_literal(constant: expressions.Constant) -> str:

@staticmethod
def format_variables_declaration(var_type: Type, var_names: list[str]) -> str:
""" Return a string representation of variable declarations."""
"""Return a string representation of variable declarations."""
match var_type:
case Pointer(type=FunctionTypeDef() as fun_type):
parameter_names = ", ".join(str(parameter) for parameter in fun_type.parameters)
Expand Down
7 changes: 3 additions & 4 deletions decompiler/backend/codegenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ def generate_function(self, task: DecompilerTask) -> str:
return self.TEMPLATE.substitute(
return_type=task.function_return_type,
name=task.name,
parameters=", ".join(map(
lambda param: CExpressionGenerator.format_variables_declaration(param.type, [param.name]),
task.function_parameters
)),
parameters=", ".join(
map(lambda param: CExpressionGenerator.format_variables_declaration(param.type, [param.name]), task.function_parameters)
),
local_declarations=LocalDeclarationGenerator.from_task(task) if not task.failed else "",
function_body=CodeVisitor(task).visit(task.syntax_tree.root) if not task.failed else task.failure_message,
)
7 changes: 2 additions & 5 deletions decompiler/backend/variabledeclarations.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ def generate(self, param_names: list[str] = []) -> Iterator[str]:
variable_type_mapping[variable.type].append(variable)
for variable_type, variables in sorted(variable_type_mapping.items(), key=lambda x: str(x)):
for chunked_variables in self._chunks(variables, self._vars_per_line):
yield CExpressionGenerator.format_variables_declaration(
variable_type,
[var.name for var in chunked_variables]
) + ";"
yield CExpressionGenerator.format_variables_declaration(variable_type, [var.name for var in chunked_variables]) + ";"

@staticmethod
def _chunks(lst: List, n: int) -> Iterator[List]:
Expand Down Expand Up @@ -134,7 +131,7 @@ def get_initial_value(variable: GlobalVariable) -> str:
return str(variable.initial_value.value)
if isinstance(variable.initial_value, bytes):
return str(convert_bytes(variable.initial_value, variable.type))
if isinstance(operation:=variable.initial_value, Operation):
if isinstance(operation := variable.initial_value, Operation):
for requirement in operation.requirements:
if isinstance(requirement, GlobalVariable):
requirement.unsubscript()
Expand Down
11 changes: 7 additions & 4 deletions decompiler/frontend/binaryninja/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,16 @@ def create_task(self, function_identifier: Union[str, Function], options: Option
try:
cfg, complex_types = self._extract_cfg(function.function, options)
task = DecompilerTask(
function.name, cfg, function_return_type=function.return_type, function_parameters=function.params,
options=options, complex_types=complex_types
function.name,
cfg,
function_return_type=function.return_type,
function_parameters=function.params,
options=options,
complex_types=complex_types,
)
except Exception as e:
task = DecompilerTask(
function.name, None, function_return_type=function.return_type, function_parameters=function.params,
options=options
function.name, None, function_return_type=function.return_type, function_parameters=function.params, options=options
)
task.fail(origin="CFG creation")
logging.error(f"Failed to decompile {task.name}, error during CFG creation: {e}")
Expand Down
9 changes: 3 additions & 6 deletions decompiler/frontend/binaryninja/handlers/assignments.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def lift_set_field(self, assignment: mediumlevelil.MediumLevelILSetVarField, is_
# case 1 (struct), avoid set field of named integers:
dest_type = self._lifter.lift(assignment.dest.type)
if isinstance(assignment.dest.type, binaryninja.NamedTypeReferenceType) and not (
isinstance(dest_type, Pointer) and isinstance(dest_type.type, Integer)
isinstance(dest_type, Pointer) and isinstance(dest_type.type, Integer)
):
struct_variable = self._lifter.lift(assignment.dest, is_aliased=True, parent=assignment)
destination = MemberAccess(
Expand Down Expand Up @@ -105,11 +105,8 @@ def lift_get_field(self, instruction: mediumlevelil.MediumLevelILVarField, is_al
if instruction.offset:
return UnaryOperation(
OperationType.cast,
[BinaryOperation(
OperationType.right_shift_us,
[source, Constant(instruction.offset, Integer.int32_t())]
)],
cast_type
[BinaryOperation(OperationType.right_shift_us, [source, Constant(instruction.offset, Integer.int32_t())])],
cast_type,
)
return UnaryOperation(OperationType.cast, [source], vartype=cast_type, contraction=True)

Expand Down
13 changes: 8 additions & 5 deletions decompiler/frontend/binaryninja/handlers/calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def lift_syscall(self, call: mediumlevelil.MediumLevelILSyscall, ssa: bool = Fal
[self._lifter.lift(parameter, parent=call) for parameter in call.params],
vartype=dest.type.copy(),
writes_memory=call.output_dest_memory if ssa else None,
meta_data={"param_names": self._lift_syscall_parameter_names(call)}
meta_data={"param_names": self._lift_syscall_parameter_names(call)},
),
)

Expand All @@ -74,12 +74,15 @@ def lift_intrinsic(self, call: mediumlevelil.MediumLevelILIntrinsic, ssa: bool =
@staticmethod
def _lift_call_parameter_names(instruction: mediumlevelil.MediumLevelILCall) -> List[str]:
"""Lift parameter names of call by iterating over the function parameters where the call is pointing to (if available)"""
if instruction.dest.expr_type is None or not isinstance(instruction.dest.expr_type, PointerType) or \
not isinstance(instruction.dest.expr_type.target, FunctionType):
if (
instruction.dest.expr_type is None
or not isinstance(instruction.dest.expr_type, PointerType)
or not isinstance(instruction.dest.expr_type.target, FunctionType)
):
return []
return [param.name for param in instruction.dest.expr_type.target.parameters]

@staticmethod
@staticmethod
def _lift_syscall_parameter_names(instruction: mediumlevelil.MediumLevelILSyscall) -> List[str]:
"""Lift syscall identifier (e.G. sys_open) from a syscall instruction"""
return [str(instruction).split("syscall(")[1].split(' ')[0]]
return [str(instruction).split("syscall(")[1].split(" ")[0]]
6 changes: 3 additions & 3 deletions decompiler/frontend/binaryninja/handlers/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

BYTE_SIZE = 8


class ConstantHandler(Handler):
def register(self):
"""Register the handler at its parent lifter."""
Expand All @@ -24,7 +25,7 @@ def register(self):

def lift_constant(self, constant: mediumlevelil.MediumLevelILConst, **kwargs) -> Constant:
"""Lift the given constant value."""
if(constant.constant in [math.inf, -math.inf, math.nan]):
if constant.constant in [math.inf, -math.inf, math.nan]:
return NotUseableConstant(str(constant.constant))
return Constant(constant.constant, vartype=self._lifter.lift(constant.expr_type))

Expand All @@ -33,7 +34,6 @@ def lift_integer_literal(value: int, **kwargs) -> Constant:
"""Lift the given literal, which is most likely an artefact from shift operations and the like."""
return Constant(value, vartype=Integer.int32_t())


def lift_constant_data(self, pointer: mediumlevelil.MediumLevelILConstData, **kwargs) -> Constant:
"""Lift const data as a non mute able constant string"""
return StringSymbol(str(pointer), pointer.address)
Expand Down Expand Up @@ -67,4 +67,4 @@ def _in_read_only_section(self, addr: int, view: BinaryView) -> bool:
for _, section in view.sections.items():
if addr >= section.start and addr <= section.end and section.semantics == SectionSemantics.ReadOnlyDataSectionSemantics:
return True
return False
return False
Loading

0 comments on commit 083bba9

Please sign in to comment.