From 1e818adb0534860f3169dfec41cdcd2d19c3181c Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 28 Sep 2023 10:31:13 +0200 Subject: [PATCH] [Parser] Lift undetermined jumps as comment (#336) * Create draft PR for #335 * If we encounter a basic-block ending in a jump instruction and having no outgoing edges, insert a comment. Note: We do not resolve jump variable --------- Co-authored-by: mm4rks Co-authored-by: Marvin Marks Co-authored-by: Niklas Bergmann <97505753+0x6e62@users.noreply.github.com> --- decompiler/frontend/binaryninja/parser.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/decompiler/frontend/binaryninja/parser.py b/decompiler/frontend/binaryninja/parser.py index 43ccc684d..9c24b97dc 100644 --- a/decompiler/frontend/binaryninja/parser.py +++ b/decompiler/frontend/binaryninja/parser.py @@ -9,6 +9,7 @@ MediumLevelILBasicBlock, MediumLevelILConstPtr, MediumLevelILInstruction, + MediumLevelILJump, MediumLevelILJumpTo, MediumLevelILTailcallSsa, RegisterValueType, @@ -18,6 +19,7 @@ from decompiler.structures.graphs.cfg import BasicBlock, ControlFlowGraph, FalseCase, IndirectEdge, SwitchCase, TrueCase, UnconditionalEdge from decompiler.structures.pseudo import Constant, Instruction from decompiler.structures.pseudo.complextypes import ComplexTypeMap +from decompiler.structures.pseudo.instructions import Comment class BinaryninjaParser(Parser): @@ -135,6 +137,10 @@ def _get_lookup_table(self, block: MediumLevelILBasicBlock) -> Dict[int, List[Co lookup[target] += [Constant(value)] return lookup + def _has_undetermined_jump(self, basic_block: MediumLevelILBasicBlock) -> bool: + """Return True if basic-block is ending in a jump and has no outgoing edges""" + return bool(len(basic_block) and isinstance(basic_block[-1], MediumLevelILJump) and not basic_block.outgoing_edges) + def _lift_instructions(self, basic_block: MediumLevelILBasicBlock) -> Iterator[Instruction]: """Yield the lifted versions of all instructions in the given basic block.""" for instruction in basic_block: @@ -144,6 +150,8 @@ def _lift_instructions(self, basic_block: MediumLevelILBasicBlock) -> Iterator[I self._unlifted_instructions.append(instruction) continue yield lifted_instruction + if self._has_undetermined_jump(basic_block): + yield Comment("jump -> undetermined") def _report_lifter_errors(self): """Report instructions which could not be lifted and reset their counter."""