From cc44b6d120bcd61d2bbdc9b75d967939b557611f Mon Sep 17 00:00:00 2001 From: rihi <19492038+rihi@users.noreply.github.com> Date: Thu, 12 Oct 2023 18:07:36 +0200 Subject: [PATCH] Catch exception caused by simplification rules if in debug --- .../expression_simplification/stages.py | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/decompiler/pipeline/controlflowanalysis/expression_simplification/stages.py b/decompiler/pipeline/controlflowanalysis/expression_simplification/stages.py index cc05a59ba..495db01fb 100644 --- a/decompiler/pipeline/controlflowanalysis/expression_simplification/stages.py +++ b/decompiler/pipeline/controlflowanalysis/expression_simplification/stages.py @@ -25,30 +25,42 @@ from decompiler.task import DecompilerTask +class _SimplificationException(Exception): + pass + + class _ExpressionSimplificationBase(PipelineStage, ABC): def run(self, task: DecompilerTask): max_iterations = task.options.getint("expression-simplification.max_iterations") - self._simplify_instructions(self._get_instructions(task), max_iterations) + debug = task.options.getboolean("pipeline.debug", fallback=False) + + self._simplify_instructions(self._get_instructions(task), max_iterations, debug) @abstractmethod def _get_instructions(self, task: DecompilerTask) -> list[Instruction]: pass @classmethod - def _simplify_instructions(cls, instructions: list[Instruction], max_iterations: int): + def _simplify_instructions(cls, instructions: list[Instruction], max_iterations: int, debug: bool): rule_sets = [ ("pre-rules", _pre_rules), ("rules", _rules), ("post-rules", _post_rules) ] - for rule_name, rule_set in rule_sets: - # max_iterations is counted per rule_set - iteration_count = cls._simplify_instructions_with_rule_set(instructions, rule_set, max_iterations) - if iteration_count <= max_iterations: - logging.info(f"Expression simplification took {iteration_count} iterations for {rule_name}") + try: + for rule_name, rule_set in rule_sets: + # max_iterations is counted per rule_set + iteration_count = cls._simplify_instructions_with_rule_set(instructions, rule_set, max_iterations) + if iteration_count <= max_iterations: + logging.info(f"Expression simplification took {iteration_count} iterations for {rule_name}") + else: + logging.warning(f"Exceeded max iteration count for {rule_name}") + except _SimplificationException as e: + if debug: + raise # re-raises the exception else: - logging.warning(f"Exceeded max iteration count for {rule_name}") + logging.exception(f"An unexpected error occurred while simplifying: {e}") @classmethod def _simplify_instructions_with_rule_set( @@ -91,7 +103,11 @@ def _simplify_instruction_with_rule( if not isinstance(expression, Operation): break - substitutions = rule.apply(expression) + try: + substitutions = rule.apply(expression) + except Exception as e: + raise _SimplificationException(e) + if not substitutions: break