Search in sources :

Example 1 with InstructionGraphNode

use of com.google.security.zynamics.binnavi.API.reil.mono.InstructionGraphNode in project binnavi by google.

the class GraphAlgorithmsTest method testDominatorTree.

@Test
public void testDominatorTree() throws MalformedGraphException {
    final InstructionGraphNode node1 = new InstructionGraphNode(new ReilInstruction(new Address(0x100), "nop", new ReilOperand(OperandSize.OPERAND_SIZE_EMPTY, ""), new ReilOperand(OperandSize.OPERAND_SIZE_EMPTY, ""), new ReilOperand(OperandSize.OPERAND_SIZE_EMPTY, "")));
    final InstructionGraphNode node2 = new InstructionGraphNode(new ReilInstruction(new Address(0x100), "nop", new ReilOperand(OperandSize.OPERAND_SIZE_EMPTY, ""), new ReilOperand(OperandSize.OPERAND_SIZE_EMPTY, ""), new ReilOperand(OperandSize.OPERAND_SIZE_EMPTY, "")));
    final InstructionGraphNode node3 = new InstructionGraphNode(new ReilInstruction(new Address(0x100), "nop", new ReilOperand(OperandSize.OPERAND_SIZE_EMPTY, ""), new ReilOperand(OperandSize.OPERAND_SIZE_EMPTY, ""), new ReilOperand(OperandSize.OPERAND_SIZE_EMPTY, "")));
    final InstructionGraphNode node4 = new InstructionGraphNode(new ReilInstruction(new Address(0x100), "nop", new ReilOperand(OperandSize.OPERAND_SIZE_EMPTY, ""), new ReilOperand(OperandSize.OPERAND_SIZE_EMPTY, ""), new ReilOperand(OperandSize.OPERAND_SIZE_EMPTY, "")));
    final InstructionGraphNode node5 = new InstructionGraphNode(new ReilInstruction(new Address(0x100), "nop", new ReilOperand(OperandSize.OPERAND_SIZE_EMPTY, ""), new ReilOperand(OperandSize.OPERAND_SIZE_EMPTY, ""), new ReilOperand(OperandSize.OPERAND_SIZE_EMPTY, "")));
    final List<InstructionGraphNode> nodes = Lists.newArrayList(node1, node2, node3, node4, node5);
    final InstructionGraphEdge edge1 = new InstructionGraphEdge(node1, node2, EdgeType.JumpUnconditional);
    final InstructionGraphEdge edge2 = new InstructionGraphEdge(node1, node3, EdgeType.JumpUnconditional);
    final InstructionGraphEdge edge3 = new InstructionGraphEdge(node2, node4, EdgeType.JumpUnconditional);
    final InstructionGraphEdge edge4 = new InstructionGraphEdge(node3, node4, EdgeType.JumpUnconditional);
    final InstructionGraphEdge edge5 = new InstructionGraphEdge(node4, node5, EdgeType.JumpUnconditional);
    InstructionGraphNode.link(node1, node2, edge1);
    InstructionGraphNode.link(node1, node3, edge2);
    InstructionGraphNode.link(node2, node4, edge3);
    InstructionGraphNode.link(node3, node4, edge4);
    InstructionGraphNode.link(node4, node5, edge5);
    final List<InstructionGraphEdge> edges = Lists.newArrayList(edge1, edge2, edge3, edge4, edge5);
    final InstructionGraph graph = new InstructionGraph(nodes, edges);
    final Tree<InstructionGraphNode> tree = GraphAlgorithms.getDominatorTree(graph, node1, null);
    assertEquals(node1, tree.getRootNode().getObject());
    assertEquals(3, tree.getRootNode().getChildren().size());
    final TreeNode<InstructionGraphNode> firstChild = tree.getRootNode().getChildren().get(0);
    final TreeNode<InstructionGraphNode> secondChild = tree.getRootNode().getChildren().get(1);
    final TreeNode<InstructionGraphNode> thirdChild = tree.getRootNode().getChildren().get(2);
    // All children are different
    assertTrue(firstChild.getObject() != secondChild.getObject());
    assertTrue(firstChild.getObject() != thirdChild.getObject());
    assertTrue(secondChild.getObject() != thirdChild.getObject());
    assertTrue((firstChild.getObject() == node4) || (firstChild.getChildren().size() == 0));
    assertTrue((firstChild.getObject() == node2) || (firstChild.getObject() == node3) || ((firstChild.getChildren().size() == 1) && (firstChild.getChildren().get(0).getObject() == node5)));
    assertTrue((secondChild.getObject() == node4) || (secondChild.getChildren().size() == 0));
    assertTrue((secondChild.getObject() == node2) || (secondChild.getObject() == node3) || ((secondChild.getChildren().size() == 1) && (secondChild.getChildren().get(0).getObject() == node5)));
    assertTrue((thirdChild.getObject() == node4) || (thirdChild.getChildren().size() == 0));
    assertTrue((thirdChild.getObject() == node2) || (thirdChild.getObject() == node3) || ((thirdChild.getChildren().size() == 1) && (thirdChild.getChildren().get(0).getObject() == node5)));
}
Also used : ReilInstruction(com.google.security.zynamics.binnavi.API.reil.ReilInstruction) Address(com.google.security.zynamics.binnavi.API.disassembly.Address) InstructionGraphEdge(com.google.security.zynamics.binnavi.API.reil.mono.InstructionGraphEdge) ReilOperand(com.google.security.zynamics.binnavi.API.reil.ReilOperand) InstructionGraph(com.google.security.zynamics.binnavi.API.reil.mono.InstructionGraph) InstructionGraphNode(com.google.security.zynamics.binnavi.API.reil.mono.InstructionGraphNode) Test(org.junit.Test)

Example 2 with InstructionGraphNode

use of com.google.security.zynamics.binnavi.API.reil.mono.InstructionGraphNode in project binnavi by google.

the class InstructionGraphTest method testRemoveEdge.

@Test
public void testRemoveEdge() {
    final List<InstructionGraphNode> nodes = new ArrayList<InstructionGraphNode>();
    final List<InstructionGraphEdge> edges = new ArrayList<InstructionGraphEdge>();
    final InstructionGraphNode node1 = new InstructionGraphNode(new ReilInstruction(new Address(0), "nop", ReilOperand.EMPTY_OPERAND, ReilOperand.EMPTY_OPERAND, ReilOperand.EMPTY_OPERAND));
    final InstructionGraphNode node2 = new InstructionGraphNode(new ReilInstruction(new Address(1), "nop", ReilOperand.EMPTY_OPERAND, ReilOperand.EMPTY_OPERAND, ReilOperand.EMPTY_OPERAND));
    final InstructionGraphEdge edge1 = new InstructionGraphEdge(node1, node2, EdgeType.JumpUnconditional);
    InstructionGraphNode.link(node1, node2, edge1);
    nodes.add(node1);
    nodes.add(node2);
    edges.add(edge1);
    final InstructionGraph graph = new InstructionGraph(nodes, edges);
    assertEquals(graph.edgeCount(), 1);
    assertEquals(1, node2.getParents().size());
    assertEquals(1, node1.getChildren().size());
    graph.removeEdge(edge1);
    InstructionGraphNode.unlink(node1, node2, edge1);
    assertEquals(0, node2.getParents().size());
    assertEquals(0, node1.getChildren().size());
}
Also used : ReilInstruction(com.google.security.zynamics.binnavi.API.reil.ReilInstruction) Address(com.google.security.zynamics.binnavi.API.disassembly.Address) InstructionGraphEdge(com.google.security.zynamics.binnavi.API.reil.mono.InstructionGraphEdge) ArrayList(java.util.ArrayList) InstructionGraph(com.google.security.zynamics.binnavi.API.reil.mono.InstructionGraph) InstructionGraphNode(com.google.security.zynamics.binnavi.API.reil.mono.InstructionGraphNode) Test(org.junit.Test)

Example 3 with InstructionGraphNode

use of com.google.security.zynamics.binnavi.API.reil.mono.InstructionGraphNode in project binnavi by google.

the class CInstructionGraphConverter method convert.

/**
 * Converts an internal InstructionGraph object into an API InstructionGraph object.
 *
 * @param graph The internal graph object that is converted.
 *
 * @return The converted API graph object.
 */
public static InstructionGraph convert(final com.google.security.zynamics.reil.algorithms.mono.InstructionGraph graph) {
    final List<InstructionGraphNode> nodes = new ArrayList<InstructionGraphNode>();
    final List<InstructionGraphEdge> edges = new ArrayList<InstructionGraphEdge>();
    final Map<com.google.security.zynamics.reil.algorithms.mono.InstructionGraphNode, InstructionGraphNode> nodeMap = new HashMap<com.google.security.zynamics.reil.algorithms.mono.InstructionGraphNode, InstructionGraphNode>();
    for (final com.google.security.zynamics.reil.algorithms.mono.InstructionGraphNode node : graph) {
        final InstructionGraphNode convertedNode = new InstructionGraphNode(new ReilInstruction(node.getInstruction()));
        nodeMap.put(node, convertedNode);
        nodes.add(convertedNode);
    }
    for (final com.google.security.zynamics.reil.algorithms.mono.InstructionGraphEdge edge : graph.getEdges()) {
        final InstructionGraphEdge convertedEdge = new InstructionGraphEdge(nodeMap.get(edge.getSource()), nodeMap.get(edge.getTarget()), EdgeType.convert(edge.getType()));
        edges.add(convertedEdge);
        InstructionGraphNode.link(nodeMap.get(edge.getSource()), nodeMap.get(edge.getTarget()), convertedEdge);
    }
    return new InstructionGraph(nodes, edges);
}
Also used : ReilInstruction(com.google.security.zynamics.binnavi.API.reil.ReilInstruction) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InstructionGraph(com.google.security.zynamics.binnavi.API.reil.mono.InstructionGraph) InstructionGraphNode(com.google.security.zynamics.binnavi.API.reil.mono.InstructionGraphNode) InstructionGraphEdge(com.google.security.zynamics.binnavi.API.reil.mono.InstructionGraphEdge)

Aggregations

ReilInstruction (com.google.security.zynamics.binnavi.API.reil.ReilInstruction)3 InstructionGraph (com.google.security.zynamics.binnavi.API.reil.mono.InstructionGraph)3 InstructionGraphEdge (com.google.security.zynamics.binnavi.API.reil.mono.InstructionGraphEdge)3 InstructionGraphNode (com.google.security.zynamics.binnavi.API.reil.mono.InstructionGraphNode)3 Address (com.google.security.zynamics.binnavi.API.disassembly.Address)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ReilOperand (com.google.security.zynamics.binnavi.API.reil.ReilOperand)1 HashMap (java.util.HashMap)1