Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Switch variable detection] No switch variable candidate found #339

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Module for finding variable relevant to switch"""
import pysnooper
from typing import Optional

from decompiler.pipeline.stage import PipelineStage
Expand All @@ -8,6 +9,7 @@
from decompiler.structures.pseudo.instructions import Assignment, Branch, IndirectBranch, Instruction
from decompiler.structures.pseudo.operations import Condition, OperationType, UnaryOperation
from decompiler.task import DecompilerTask
from decompiler.util.decoration import DecoratedCFG


def is_dereference(expression: Expression) -> bool:
Expand Down Expand Up @@ -72,6 +74,7 @@ def __init__(self):
self._def_map: DefMap
self._use_map: UseMap
self._dereferences_used_in_branches: set
self._undefined_variables: set

def run(self, task: DecompilerTask):
"""
Expand All @@ -87,6 +90,8 @@ def run(self, task: DecompilerTask):
Overcomes issues with dummy heuristic.
"""
self._init_map(task.graph)
self._undefined_variables = task.graph.get_undefined_variables()
# DecoratedCFG.from_cfg(task.graph).export_plot("switch.png")
for switch_block in {edge.source for edge in task.graph.edges if isinstance(edge, SwitchCase)}:
self._handle_switch_block(switch_block)

Expand All @@ -106,6 +111,7 @@ def _handle_switch_block(self, basic_block: BasicBlock):
switch_expression = self.find_switch_expression(switch_instruction)
switch_instruction.substitute(switch_instruction.expression, switch_expression)

@pysnooper.snoop()
def find_switch_expression(self, switch_instruction: Instruction):
"""Try to deduce the variable utilized for the switch instruction."""
traced_variable = (
Expand Down Expand Up @@ -153,6 +159,7 @@ def _is_copy_assigned(self, value: Variable) -> bool:
return isinstance(definition.value, Variable)
return False


def _is_bounds_checked(self, value: Variable) -> bool:
"""
Check if variable can be used in switch expression.
Expand All @@ -163,6 +170,7 @@ def _is_bounds_checked(self, value: Variable) -> bool:
self._is_used_in_condition_assignment(value),
self._is_used_in_branch(value),
self._is_predecessor_dereferenced_in_branch(value),
value in self._undefined_variables,
]
)

Expand Down