Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ebehner committed Apr 23, 2024
1 parent 70c6985 commit c4047da
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def find(cls, asforest: AbstractSyntaxForest, options: RestructuringOptions) ->
missing_case_finder._insert_case_node(
case_candidate_information.case_node, case_candidate_information.case_constants, case_candidate_information.switch_node
)

if case_candidate_information.in_sequence:
asforest.extract_switch_from_condition_sequence(case_candidate_information.switch_node, condition_node)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4932,107 +4932,188 @@ def test_extract_return(task):
assert branch.instructions == vertices[3].instructions


# fix in Issue 28
# def test_hash_eq_problem(task):
# """
# Hash and eq are not the same, therefore we have to be careful which one we want:
#
# - eq: Same condition node in sense of same condition
# - hash: same node in the graph
# """
# arg1 = Variable("arg1", Integer.int32_t(), ssa_name=Variable("arg1", Integer.int32_t(), 0))
# arg2 = Variable("arg2", Integer.int32_t(), ssa_name=Variable("arg2", Integer.int32_t(), 0))
# var_2 = Variable("var_2", Integer.int32_t(), None, True, Variable("rax_1", Integer.int32_t(), 1, True, None))
# var_5 = Variable("var_5", Integer.int32_t(), None, True, Variable("rax_2", Integer.int32_t(), 2, True, None))
# var_6 = Variable("var_6", Integer.int32_t(), None, True, Variable("rax_5", Integer.int32_t(), 30, True, None))
# var_7 = Variable("var_7", Integer.int32_t(), None, True, Variable("rax_3", Integer.int32_t(), 3, True, None))
# task.graph.add_nodes_from(
# vertices := [
# BasicBlock(0, instructions=[Branch(Condition(OperationType.equal, [arg1, Constant(1, Integer.int32_t())]))]),
# BasicBlock(
# 1,
# instructions=[
# Assignment(var_2, BinaryOperation(OperationType.plus, [var_2, Constant(1, Integer.int32_t())])),
# Branch(Condition(OperationType.not_equal, [var_2, Constant(0, Integer.int32_t())])),
# ],
# ),
# BasicBlock(
# 2,
# instructions=[
# Assignment(ListOperation([]), Call(imp_function_symbol("sub_140019288"), [arg2])),
# Branch(Condition(OperationType.equal, [arg1, Constant(0, Integer.int32_t())])),
# ],
# ),
# BasicBlock(
# 3,
# instructions=[
# Assignment(ListOperation([]), Call(imp_function_symbol("scanf"), [Constant(0x804B01F), var_5])),
# Branch(Condition(OperationType.not_equal, [var_5, Constant(0, Integer.int32_t())])),
# ],
# ),
# BasicBlock(
# 4, instructions=[Assignment(var_5, Constant(0, Integer.int32_t())), Assignment(var_7, Constant(-1, Integer.int32_t()))]
# ),
# BasicBlock(
# 5,
# instructions=[
# Assignment(var_5, Constant(0, Integer.int32_t())),
# Assignment(var_7, Constant(-1, Integer.int32_t())),
# Assignment(arg1, Constant(0, Integer.int32_t())),
# Assignment(var_2, Constant(0, Integer.int32_t())),
# ],
# ),
# BasicBlock(
# 6,
# instructions=[
# Assignment(var_5, Constant(0, Integer.int32_t())),
# Assignment(var_7, Constant(-1, Integer.int32_t())),
# Assignment(var_2, Constant(0, Integer.int32_t())),
# ],
# ),
# BasicBlock(7, instructions=[Assignment(ListOperation([]), Call(imp_function_symbol("sub_1400193a8"), []))]),
# BasicBlock(
# 8,
# instructions=[
# Assignment(ListOperation([]), Call(imp_function_symbol("scanf"), [Constant(0x804B01F), var_6])),
# Branch(Condition(OperationType.greater_us, [var_6, Constant(0, Integer.int32_t())])),
# ],
# ),
# BasicBlock(9, instructions=[Assignment(arg1, Constant(1, Integer.int32_t()))]),
# BasicBlock(10, instructions=[Return([arg1])]),
# ]
# )
# task.graph.add_edges_from(
# [
# TrueCase(vertices[0], vertices[1]),
# FalseCase(vertices[0], vertices[2]),
# TrueCase(vertices[1], vertices[3]),
# FalseCase(vertices[1], vertices[4]),
# TrueCase(vertices[2], vertices[5]),
# FalseCase(vertices[2], vertices[6]),
# TrueCase(vertices[3], vertices[7]),
# FalseCase(vertices[3], vertices[8]),
# UnconditionalEdge(vertices[4], vertices[7]),
# UnconditionalEdge(vertices[5], vertices[10]),
# UnconditionalEdge(vertices[6], vertices[9]),
# UnconditionalEdge(vertices[7], vertices[9]),
# TrueCase(vertices[8], vertices[9]),
# FalseCase(vertices[8], vertices[10]),
# UnconditionalEdge(vertices[9], vertices[10]),
# ]
# )
# PatternIndependentRestructuring().run(task)
# assert any(isinstance(node, SwitchNode) for node in task.syntax_tree)
# var_2_conditions = []
# for node in task.syntax_tree.get_condition_nodes_post_order():
# if (
# not node.condition.is_symbol
# and node.condition.is_literal
# and str(task.syntax_tree.condition_map[~node.condition]) in {"var_2 != 0x0"}
# ):
# node.switch_branches()
# if node.condition.is_symbol and str(task.syntax_tree.condition_map[node.condition]) in {"var_2 != 0x0"}:
# var_2_conditions.append(node)
# assert len(var_2_conditions) == 2
# assert var_2_conditions[0] == var_2_conditions[1]
# assert hash(var_2_conditions[0]) != hash(var_2_conditions[1])
def test_hash_eq_problem(task):
"""
Hash and eq are not the same, therefore we have to be careful which one we want:
- eq: Same condition node in sense of same condition
- hash: same node in the graph
"""
arg1 = Variable("arg1", Integer.int32_t(), ssa_name=Variable("arg1", Integer.int32_t(), 0))
arg2 = Variable("arg2", Integer.int32_t(), ssa_name=Variable("arg2", Integer.int32_t(), 0))
var_2 = Variable("var_2", Integer.int32_t(), None, True, Variable("rax_1", Integer.int32_t(), 1, True, None))
var_5 = Variable("var_5", Integer.int32_t(), None, True, Variable("rax_2", Integer.int32_t(), 2, True, None))
var_6 = Variable("var_6", Integer.int32_t(), None, True, Variable("rax_5", Integer.int32_t(), 30, True, None))
var_7 = Variable("var_7", Integer.int32_t(), None, True, Variable("rax_3", Integer.int32_t(), 3, True, None))
task.graph.add_nodes_from(
vertices := [
BasicBlock(0, instructions=[Branch(Condition(OperationType.equal, [arg1, Constant(1, Integer.int32_t())]))]),
BasicBlock(
1,
instructions=[
Assignment(var_2, BinaryOperation(OperationType.plus, [var_2, Constant(1, Integer.int32_t())])),
Branch(Condition(OperationType.not_equal, [var_2, Constant(0, Integer.int32_t())])),
],
),
BasicBlock(
2,
instructions=[
Assignment(ListOperation([]), Call(imp_function_symbol("sub_140019288"), [arg2])),
Branch(Condition(OperationType.equal, [arg1, Constant(0, Integer.int32_t())])),
],
),
BasicBlock(
3,
instructions=[
Assignment(ListOperation([]), Call(imp_function_symbol("scanf"), [Constant(0x804B01F), var_5])),
Branch(Condition(OperationType.not_equal, [var_5, Constant(0, Integer.int32_t())])),
],
),
BasicBlock(
4, instructions=[Assignment(var_5, Constant(0, Integer.int32_t())), Assignment(var_7, Constant(-1, Integer.int32_t()))]
),
BasicBlock(
5,
instructions=[
Assignment(var_5, Constant(0, Integer.int32_t())),
Assignment(var_7, Constant(-1, Integer.int32_t())),
Assignment(arg1, Constant(0, Integer.int32_t())),
Assignment(var_2, Constant(0, Integer.int32_t())),
],
),
BasicBlock(
6,
instructions=[
Assignment(var_5, Constant(0, Integer.int32_t())),
Assignment(var_7, Constant(-1, Integer.int32_t())),
Assignment(var_2, Constant(0, Integer.int32_t())),
],
),
BasicBlock(7, instructions=[Assignment(ListOperation([]), Call(imp_function_symbol("sub_1400193a8"), []))]),
BasicBlock(
8,
instructions=[
Assignment(ListOperation([]), Call(imp_function_symbol("scanf"), [Constant(0x804B01F), var_6])),
Branch(Condition(OperationType.greater_us, [var_6, Constant(0, Integer.int32_t())])),
],
),
BasicBlock(9, instructions=[Assignment(arg1, Constant(1, Integer.int32_t()))]),
BasicBlock(10, instructions=[Return([arg1])]),
]
)
task.graph.add_edges_from(
[
TrueCase(vertices[0], vertices[1]),
FalseCase(vertices[0], vertices[2]),
TrueCase(vertices[1], vertices[3]),
FalseCase(vertices[1], vertices[4]),
TrueCase(vertices[2], vertices[5]),
FalseCase(vertices[2], vertices[6]),
TrueCase(vertices[3], vertices[7]),
FalseCase(vertices[3], vertices[8]),
UnconditionalEdge(vertices[4], vertices[7]),
UnconditionalEdge(vertices[5], vertices[10]),
UnconditionalEdge(vertices[6], vertices[9]),
UnconditionalEdge(vertices[7], vertices[9]),
TrueCase(vertices[8], vertices[9]),
FalseCase(vertices[8], vertices[10]),
UnconditionalEdge(vertices[9], vertices[10]),
]
)
PatternIndependentRestructuring().run(task)
var_2_conditions = []
for node in task.syntax_tree.get_condition_nodes_post_order():
if (
not node.condition.is_symbol
and node.condition.is_literal
and str(task.syntax_tree.condition_map[~node.condition]) in {"arg1 == 0x0"}
):
node.switch_branches()
if node.condition.is_symbol and str(task.syntax_tree.condition_map[node.condition]) in {"arg1 == 0x0"}:
var_2_conditions.append(node)
assert len(var_2_conditions) == 2
assert var_2_conditions[0] == var_2_conditions[1]
assert hash(var_2_conditions[0]) != hash(var_2_conditions[1])


def test_condition_based_refined_considers_conditions(task):
"""Test condition test 16"""
var_5_0 = Variable("var_5", Integer.int32_t(), None, True, Variable("var_10", Integer.int32_t(), 0, True, None))
var_5_2 = Variable("var_5", Integer.int32_t(), None, True, Variable("var_10", Integer.int32_t(), 2, True, None))
var_5_3 = Variable("var_5", Integer.int32_t(), None, True, Variable("var_10", Integer.int32_t(), 3, True, None))
var_5_4 = Variable("var_5", Integer.int32_t(), None, True, Variable("var_10", Integer.int32_t(), 4, True, None))
var_5_6 = Variable("var_5", Integer.int32_t(), None, True, Variable("var_10", Integer.int32_t(), 6, True, None))
var_6_0 = Variable("var_6", Integer.int32_t(), None, True, Variable("var_14", Integer.int32_t(), 0, True, None))
var_6_2 = Variable("var_6", Integer.int32_t(), None, True, Variable("var_14", Integer.int32_t(), 2, True, None))
var_6_5 = Variable("var_6", Integer.int32_t(), None, True, Variable("var_14", Integer.int32_t(), 5, True, None))
var_6_6 = Variable("var_6", Integer.int32_t(), None, True, Variable("var_14", Integer.int32_t(), 6, True, None))
var_7 = Variable("var_7", Integer.int32_t(), None, True, Variable("c0", Integer.int32_t(), 0, True, None))
task.graph.add_nodes_from(
vertices := [
BasicBlock(
0,
instructions=[
Assignment(
ListOperation([]),
Call(
imp_function_symbol("sub_140019288"),
[
UnaryOperation(OperationType.address, [var_5_0], Pointer(Integer.int32_t(), 32)),
UnaryOperation(OperationType.address, [var_6_0], Pointer(Integer.int32_t(), 32)),
],
),
),
Branch(Condition(OperationType.greater, [var_5_2, Constant(4, Integer.int32_t())])),
],
),
BasicBlock(
1, instructions=[Assignment(var_5_3, BinaryOperation(OperationType.minus, [var_5_2, Constant(5, Integer.int32_t())]))]
),
BasicBlock(
2,
instructions=[
Assignment(var_7, BinaryOperation(OperationType.plus, [var_5_2, Constant(5, Integer.int32_t())])),
Assignment(var_5_4, var_7),
Branch(Condition(OperationType.greater, [var_6_0, Constant(4, Integer.int32_t())])),
],
),
BasicBlock(
3,
instructions=[
Assignment(ListOperation([]), Call(imp_function_symbol("printf"), [Constant(0x804B01F), var_5_6, var_6_6])),
Return([Constant(0, Integer.int32_t())]),
],
),
BasicBlock(
4,
instructions=[
Assignment(var_6_5, BinaryOperation(OperationType.plus, [var_7, var_6_2])),
],
),
]
)
task.graph.add_edges_from(
[
TrueCase(vertices[0], vertices[1]),
FalseCase(vertices[0], vertices[2]),
UnconditionalEdge(vertices[1], vertices[3]),
TrueCase(vertices[2], vertices[3]),
FalseCase(vertices[2], vertices[4]),
UnconditionalEdge(vertices[4], vertices[3]),
]
)
PatternIndependentRestructuring().run(task)
assert isinstance(root := task.ast.root, SeqNode) and len(children := root.children) == 3
assert isinstance(children[0], CodeNode) and children[0].instructions == vertices[0].instructions[:-1]
assert isinstance(cn := children[1], ConditionNode)
assert isinstance(children[2], CodeNode) and children[2].instructions == vertices[3].instructions

if cn.condition.is_literal and not cn.condition.is_symbol:
cn.switch_branches()
assert task.ast.condition_map[cn.condition] == vertices[0].instructions[-1].condition
assert isinstance(tb := cn.true_branch_child, CodeNode) and tb.instructions == vertices[1].instructions
assert isinstance(fb := cn.false_branch_child, SeqNode) and len(fb_children := fb.children) == 2
assert isinstance(code_node := fb_children[0], CodeNode) and code_node.instructions == vertices[2].instructions[:-1]
assert isinstance(nested_if := fb_children[1], ConditionNode) and nested_if.condition.is_literal and not nested_if.condition.is_symbol
assert nested_if.false_branch is None and isinstance(nested_code := nested_if.true_branch_child, CodeNode)
assert nested_code.instructions == vertices[4].instructions

0 comments on commit c4047da

Please sign in to comment.