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

[Restructuring] Wrong Value in Condition #93

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
side effects get task object
  • Loading branch information
ebehner committed Aug 12, 2022
commit 711b1609f1b5cc7cfa6683ab29932ef6cf6e82f6
6 changes: 5 additions & 1 deletion decompiler/frontend/binaryninja/handlers/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ def lift_constant_pointer(self, pointer: mediumlevelil.MediumLevelILConstPtr, **
if symbol is not None and symbol.type in (SymbolType.ImportedFunctionSymbol, SymbolType.ExternalSymbol, SymbolType.FunctionSymbol):
return self._lift_symbol_pointer(address, symbol)

if not isinstance(pointer, mediumlevelil.MediumLevelILImport) and (symbol is None or symbol.type != SymbolType.DataSymbol) and (string := bv.get_string_at(address, partial=True) or bv.get_ascii_string_at(address, min_length=2)):
if (
not isinstance(pointer, mediumlevelil.MediumLevelILImport)
and (symbol is None or symbol.type != SymbolType.DataSymbol)
and (string := bv.get_string_at(address, partial=True) or bv.get_ascii_string_at(address, min_length=2))
):
return Constant(address, Pointer(Integer.char()), Constant(string.value, Integer.char()))

if (variable := bv.get_data_var_at(address)) is not None:
Expand Down
4 changes: 3 additions & 1 deletion decompiler/pipeline/controlflowanalysis/restructuring.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
from typing import List, Optional

from decompiler.pipeline.commons.reaching_definitions import ReachingDefinitions
from decompiler.pipeline.controlflowanalysis.restructuring_commons.acyclic_restructuring import AcyclicRegionRestructurer
from decompiler.pipeline.controlflowanalysis.restructuring_commons.cyclic_restructuring import CyclicRegionStructurer
from decompiler.pipeline.controlflowanalysis.restructuring_commons.empty_basic_block_remover import EmptyBasicBlockRemover
Expand Down Expand Up @@ -34,6 +35,7 @@ def __init__(self, tcfg: Optional[TransitionCFG] = None, asforest: Optional[Abst
"""
self.t_cfg: TransitionCFG = tcfg
self.asforest: AbstractSyntaxForest = asforest
self._reaching_definitions: Optional[ReachingDefinitions] = None

def run(self, task: DecompilerTask):
"""
Expand All @@ -49,7 +51,7 @@ def run(self, task: DecompilerTask):
self.asforest.set_current_root(self.t_cfg.root.ast)
assert (roots := len(self.asforest.get_roots)) == 1, f"After the restructuring the forest should have one root, but it has {roots}!"
task._ast = AbstractSyntaxTree.from_asforest(self.asforest, self.asforest.current_root)
SideEffectHandler.resolve(task.syntax_tree)
SideEffectHandler.resolve(task)
task._cfg = None

def restructure_cfg(self) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,18 @@

from typing import Dict, Iterable, Optional, Type, Union

from networkx import MultiDiGraph

from decompiler.pipeline.controlflowanalysis.restructuring_commons.side_effect_handling.data_graph_visitor import (
ASTDataGraphVisitor,
SubtreeProperty,
)
from decompiler.structures.ast.ast_nodes import (
AbstractSyntaxTreeNode,
CaseNode,
CodeNode,
ConditionNode,
LoopNode,
SwitchNode,
TrueNode,
)
from decompiler.structures.ast.ast_nodes import AbstractSyntaxTreeNode, CaseNode, CodeNode, ConditionNode, LoopNode, SwitchNode, TrueNode
from decompiler.structures.ast.syntaxtree import AbstractSyntaxTree
from decompiler.structures.graphs.basicblock import BasicBlock
from decompiler.structures.graphs.branches import BasicBlockEdge, FalseCase, SwitchCase, TrueCase, UnconditionalEdge
from decompiler.structures.graphs.cfg import ControlFlowGraph
from decompiler.structures.logic.logic_condition import LogicCondition
from decompiler.structures.pseudo import Assignment, Branch, Call, Condition, Constant, ListOperation, Variable
from networkx import MultiDiGraph


class DataNode(BasicBlock):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
from __future__ import annotations
from typing import Dict, List, Optional

from typing import Optional

from decompiler.pipeline.commons.reaching_definitions import ReachingDefinitions
from decompiler.pipeline.controlflowanalysis.restructuring_commons.side_effect_handling.data_graph import DataGraph
from decompiler.structures.ast.syntaxtree import AbstractSyntaxTree
from decompiler.structures.pseudo import Instruction, Variable
from decompiler.structures.graphs.cfg import ControlFlowGraph
from decompiler.task import DecompilerTask


class SideEffectHandler:
def __init__(self, ast: AbstractSyntaxTree, cfg: Optional[DataGraph] = None):
def __init__(self, ast: AbstractSyntaxTree, cfg: ControlFlowGraph, data_graph: DataGraph):
self._ast: AbstractSyntaxTree = ast
self._data_graph: DataGraph = cfg
self._cfg: ControlFlowGraph = cfg
self._data_graph: DataGraph = data_graph

@classmethod
def resolve(cls, ast: AbstractSyntaxTree) -> None:
def resolve(cls, task: DecompilerTask) -> None:
# return
cfg = DataGraph.generate_from_ast(ast)
side_effect_handler = cls(ast, cfg)
from decompiler.util.decoration import DecoratedAST

DecoratedAST.from_ast(ast).export_plot("/home/eva/Projects/dewolf-decompiler/AST/ast.png")
from decompiler.util.decoration import DecoratedCFG
data_graph = DataGraph.generate_from_ast(task.syntax_tree)
side_effect_handler = cls(task.syntax_tree, task.graph, data_graph)
# DecoratedAST.from_ast(task.syntax_tree).export_plot("/home/eva/Projects/dewolf-decompiler/AST/ast.png")
from decompiler.util.decoration import DecoratedAST, DecoratedCFG

DecoratedCFG.from_cfg(cfg).export_plot("/home/eva/Projects/dewolf-decompiler/AST/cfg.png")
# DecoratedCFG.from_cfg(data_graph).export_plot("/home/eva/Projects/dewolf-decompiler/AST/cfg.png")
side_effect_handler.apply()

def apply(self):
Expand All @@ -44,5 +45,3 @@ def apply(self):
# definition = [def_var for def_var in def_instruction.definitions if def_var == used_variable][0]
# if used_variable.ssa_name != definition.ssa_name:
# raise "We have to handle side effects!"


13 changes: 5 additions & 8 deletions decompiler/util/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ def register_widget(cls, parent: QWidget):
return code_display



class DewolfNotifications(UIContextNotification):
"""Class handling notifications to the dewolf widget."""

Expand All @@ -143,6 +142,7 @@ def OnViewChange(self, context, frame, type):
def OnAddressChange(self, context, frame, view, location):
self.widget.updateState()


class DewolfWidget(QWidget, UIContextNotification):
"""Class for docking widget, displaying decompiled code"""

Expand Down Expand Up @@ -315,7 +315,7 @@ def start_worker(self, binary_view: BinaryView, function: Function) -> None:
self.threadpool.start(worker)

def updateState(self):
""" Update the current UI state (frame, view, data, function) """
"""Update the current UI state (frame, view, data, function)"""

self._current_frame = UIContext.currentViewFrameForWidget(self)

Expand All @@ -329,19 +329,18 @@ def updateState(self):

@staticmethod
def createPane(context):
""" Create a WidgetPane """
"""Create a WidgetPane"""
if context.context and context.binaryView:
widget = DewolfWidget(context.binaryView)
pane = WidgetPane(widget, "dewolf decompiler")
context.context.openPane(pane)

@staticmethod
def canCreatePane(context):
""" Determine if we can create a WidgetPane """
"""Determine if we can create a WidgetPane"""
return context.context and context.binaryView



class Highlighter(QSyntaxHighlighter):
"""Highlighter class for syntax highlighting in CodeView"""

Expand Down Expand Up @@ -447,7 +446,5 @@ def _highlight_tokens(self, text: str, tokens: List[str], color: ThemeColor, is_
def add_dewolf_widget():
"""Add widget to GUI"""
UIAction.registerAction("dewolf decompiler")
UIActionHandler.globalActions().bindAction(
"dewolf decompiler", UIAction(DewolfWidget.createPane, DewolfWidget.canCreatePane)
)
UIActionHandler.globalActions().bindAction("dewolf decompiler", UIAction(DewolfWidget.createPane, DewolfWidget.canCreatePane))
Menu.mainMenu("Tools").addAction("dewolf decompiler", "dewolf decompiler")
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from decompiler.pipeline.controlflowanalysis.restructuring_commons.side_effect_handling.data_graph import DataGraph
from decompiler.pipeline.controlflowanalysis.restructuring_commons.side_effect_handling.side_effect_handler import SideEffectHandler
from decompiler.structures.ast.ast_nodes import CodeNode
from decompiler.structures.ast.syntaxtree import AbstractSyntaxTree
Expand All @@ -8,6 +9,5 @@ def test_create_ast_from_code_node():
ast = AbstractSyntaxTree(
root=CodeNode([], reaching_condition=LogicCondition.initialize_true(LogicCondition.generate_new_context())), condition_map=dict()
)
side_effect_handler = SideEffectHandler(ast)
side_effect_handler._create_cfg_from_ast()
assert len(side_effect_handler._data_graph) == 1
data_graph = DataGraph.generate_from_ast(ast)
assert len(data_graph) == 1