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
Next Next commit
some tryouts
  • Loading branch information
ebehner committed Aug 12, 2022
commit 20517de1f80b77258ebf35b28a255ca64f9c0cc2
2 changes: 2 additions & 0 deletions decompiler/pipeline/controlflowanalysis/restructuring.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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
from decompiler.pipeline.controlflowanalysis.restructuring_commons.sied_effect_handler import SideEffectHandler
from decompiler.pipeline.stage import PipelineStage
from decompiler.structures.ast.syntaxforest import AbstractSyntaxForest
from decompiler.structures.ast.syntaxtree import AbstractSyntaxTree
Expand Down Expand Up @@ -48,6 +49,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)
task._cfg = None

def restructure_cfg(self) -> None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import Dict

from decompiler.structures.ast.ast_nodes import AbstractSyntaxTreeNode, CodeNode, ConditionNode, LoopNode, SwitchNode
from decompiler.structures.ast.syntaxtree import AbstractSyntaxTree
from decompiler.structures.graphs.basicblock import BasicBlock
from decompiler.structures.graphs.cfg import ControlFlowGraph


class SideEffectHandler:

def __init__(self, ast: AbstractSyntaxTree):
self._ast: AbstractSyntaxTree = ast
self._cfg = ControlFlowGraph()

@classmethod
def resolve(cls, ast: AbstractSyntaxTree):
side_effect_handler = cls(ast)
side_effect_handler._create_cfg_from_ast()

def _create_cfg_from_ast(self):
translation_dict: Dict[AbstractSyntaxTreeNode, int] = dict()
for idx, ast_node in enumerate(self._ast.nodes):
if isinstance(ast_node, CodeNode):
self._cfg.add_node(BasicBlock(idx, ast_node.instructions))
elif isinstance(ast_node, ConditionNode):
self._cfg.add_node(BasicBlock(idx, [ast_node.condition]))
elif isinstance(ast_node, SwitchNode):
self._cfg.add_node(BasicBlock(idx, [ast_node.expression]))
elif isinstance(ast_node, LoopNode):
self._cfg.add_node(BasicBlock(idx, []))
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from decompiler.pipeline.controlflowanalysis.restructuring_commons.sied_effect_handler import SideEffectHandler
from decompiler.structures.ast.ast_nodes import CodeNode
from decompiler.structures.ast.syntaxtree import AbstractSyntaxTree
from decompiler.structures.logic.logic_condition import LogicCondition


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._cfg) == 1
27 changes: 27 additions & 0 deletions tests/samples/src/systemtests/test_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,33 @@ int test11(int a, int b)
return 0;
}


void test_do_while(){
char c;
int choice,dummy;
do{
printf("\n1. Print Hello\n2. Print Javatpoint\n3. Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1 :
printf("Hello");
break;
case 2:
printf("Javatpoint");
break;
case 3:
exit(0);
break;
default:
printf("please enter valid choice");
}
printf("do you want to enter more?");
scanf("%d",&dummy);
scanf("%c",&c);
}while(c=='y');
}

int main()
{
test1();
Expand Down