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)));
}
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());
}
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);
}
Aggregations