Ejemplo n.º 1
0
def test_that_root_node_is_connected_to_entry_points(simple_bytecode: str):
  builder = graph_builder.ControlAndDataFlowGraphBuilder()
  graph = builder.Build(simple_bytecode)
  assert "root" in graph
  assert graph.in_degree("root") == 0
  assert len(graph.out_edges("root")) == 2
  assert set(graph.neighbors("root")) == {"A_0", "B_0"}
Ejemplo n.º 2
0
def test_ComposeGraphs_undefined():
  """Test that function graph is inserted for call to undefined function."""
  builder = graph_builder.ControlAndDataFlowGraphBuilder()

  A = nx.MultiDiGraph(name="A")
  A.entry_block = "A_entry"
  A.exit_block = "A_exit"

  A.add_node(A.entry_block, type="statement", function="A", text="")
  A.add_node(A.exit_block, type="statement", function="A", text="")
  A.add_node("call", type="statement", function="A", text="call i32 @B(i32 1)")

  A.add_edge(A.entry_block, "call", flow="control", function="A")
  A.add_edge("call", A.exit_block, flow="control", function="A")

  call_graph = nx.MultiDiGraph()
  call_graph.add_edge("external node", "A")
  call_graph.add_edge("external node", "B")
  call_graph.add_edge("A", "B")

  g = builder.ComposeGraphs([A], call_graph)

  assert "root" in g
  assert "B_entry" in g
  assert "B_exit" in g

  assert g.edges("call", "B_entry")
  assert g.edges("B_exit", "call")

  assert g.number_of_nodes() == 6

  assert g.edges("root", "A")
  assert g.edges("root", "B")
Ejemplo n.º 3
0
def test_every_node_has_x(simple_bytecode: str):
  """Test that every edge has a position encoding."""
  builder = graph_builder.ControlAndDataFlowGraphBuilder()
  graph = builder.Build(simple_bytecode)
  for node, x in graph.nodes(data="x"):
    assert isinstance(
      x, int
    ), f'No x for node {graph.nodes[node]["original_text"]}'
Ejemplo n.º 4
0
def test_every_edge_has_position(simple_bytecode: str):
  """Test that every edge has a position encoding."""
  builder = graph_builder.ControlAndDataFlowGraphBuilder()
  graph = builder.Build(simple_bytecode)
  for src, dst, position in graph.edges(data="position"):
    assert isinstance(
      position, int
    ), f'No position for edge {graph.nodes[src]["original_text"]} -> {graph.nodes[dst]["original_text"]}'
Ejemplo n.º 5
0
def test_every_statement_has_a_predecessor(simple_bytecode: str):
  """Test that every statement (except entry blocks) have control preds."""
  builder = graph_builder.ControlAndDataFlowGraphBuilder()
  graph = builder.Build(simple_bytecode)
  entry_blocks = set([node for node, _ in nx_utils.EntryBlockIterator(graph)])
  for node, _ in nx_utils.StatementNodeIterator(graph):
    if node in entry_blocks:
      continue
    for edge in graph.in_edges(node):
      if graph.edges[edge[0], edge[1], 0]["flow"] == "control":
        break
    else:
      assert False, f"{node} has no control flow predecessor."