Search in sources :

Example 1 with ViewEdge

use of com.google.security.zynamics.binnavi.API.disassembly.ViewEdge in project binnavi by google.

the class PathFinder method createPath.

/**
   * Creates a view that shows all possible paths between two blocks of a module.
   *
   * @param module The module for which the view is created.
   * @param startBlock The basic block where the path begins (must be null if startFunction is not
   *        null).
   * @param targetBlock The basic block where the path ends (must be null if targetFunction is not
   *        null).
   * @param startFunction The function where the path starts (must be null if startBlock is not
   *        null).
   * @param targetFunction The function where the path ends (must be null if targetBlock is not
   *        null).
   *
   * @return The view that contains all possible paths between the start block and the target block.
   *
   * @throws CouldntLoadDataException
   * @throws PartialLoadException
   * @throws IllegalArgumentException
   */
public static View createPath(final Module module, final BasicBlock startBlock, final BasicBlock targetBlock, final Function startFunction, final Function targetFunction) throws CouldntLoadDataException, PartialLoadException {
    Preconditions.checkNotNull(module, "Error: Module argument can't be null");
    Preconditions.checkArgument(module.isLoaded(), "Error: Module is not loaded");
    if ((startBlock == null) && (startFunction == null)) {
        throw new IllegalArgumentException("Error: No valid start given");
    }
    if ((targetBlock == null) && (targetFunction == null)) {
        throw new IllegalArgumentException("Error: No valid target given");
    }
    if ((startFunction != null) && !startFunction.isLoaded()) {
        throw new IllegalArgumentException("Error: Start function is not loaded");
    }
    if ((targetFunction != null) && !targetFunction.isLoaded()) {
        throw new IllegalArgumentException("Error: Target function is not loaded");
    }
    // The algorithm works like this:
    //
    // 1. Find all functions that lie between the start function and the target function.
    // 2. Insert all of these functions into the new view.
    // 3. Connect the individual functions at function calls and split code nodes if necessary.
    // 4. Determine what nodes are actually on the path by taking the successors of the start
    // node and set-unioning those with the predecessors of the target node.
    // 5. Delete all the nodes which are not on the path.
    // At first we determine the function where the path starts and the function where the path
    // ends.
    final Function realStartFunction = startFunction != null ? startFunction : startBlock.getParentFunction();
    final Function realTargetFunction = targetFunction != null ? targetFunction : targetBlock.getParentFunction();
    if (realStartFunction.getGraph().nodeCount() == 0) {
        throw new IllegalArgumentException("Error: Functions with zero nodes can not be used for pathfinding");
    }
    // Determine the real start and end blocks of the path with the help of the function flow graphs
    final BasicBlock realStartBlock = startBlock != null ? startBlock : findEntryNode(realStartFunction);
    final BasicBlock realTargetBlock = targetBlock != null ? targetBlock : findEntryNode(realTargetFunction);
    // Find out what functions are called on the way between the first block and the second block.
    final LinkedHashSet<FunctionBlock> passedFunctions = findPassedFunctions(module.getCallgraph(), realStartFunction, realTargetFunction);
    // Create the view that represents the calculated path
    final String endAddress = realTargetBlock != null ? realTargetBlock.getAddress().toHexString() : realTargetFunction.getAddress().toHexString();
    final View view = module.createView("New Pathfinder View", String.format("%s -> %s", realStartBlock.getAddress().toHexString(), endAddress));
    view.load();
    // Maps basic blocks of functions to their corresponding node in the new view.
    final Map<BasicBlock, ViewNode> nodeMap = new HashMap<BasicBlock, ViewNode>();
    // Keeps track of the entry nodes for each function,
    final Map<Function, ViewNode> entryNodes = new HashMap<Function, ViewNode>();
    // Keeps track of the exit nodes for each function,
    final ArrayListMultimap<Function, ViewNode> exitNodes = ArrayListMultimap.create();
    // Keeps track of the function a view node belongs to.
    // TODO (timkornau): This should actually be accessible from the plug in API.
    final Map<ViewNode, Function> functionMap = new HashMap<ViewNode, Function>();
    // Create a code node for all basic blocks of the passed functions.
    createInitialBlocks(view, passedFunctions, nodeMap, functionMap);
    // Create view edges for all edges in the passed functions.
    createInitialEdges(view, passedFunctions, nodeMap);
    // Find the entry and exit nodes for all passed functions.
    findEntryExitNodes(passedFunctions, nodeMap, functionMap, entryNodes, exitNodes);
    ViewNode startNode = nodeMap.get(realStartBlock);
    ViewNode targetNode = realTargetBlock == null ? entryNodes.get(realTargetFunction) : nodeMap.get(realTargetBlock);
    startNode.setColor(Color.GREEN);
    targetNode.setColor(Color.YELLOW);
    // Connect the nodes of the different functions on function calls.
    final NodePair splitResult = connectFunctions(view, startNode, targetNode, passedFunctions, entryNodes, exitNodes, functionMap);
    startNode = splitResult.getFirst();
    targetNode = splitResult.getSecond();
    // all paths end here.
    for (final ViewEdge edge : targetNode.getOutgoingEdges()) {
        view.deleteEdge(edge);
    }
    // Delete all nodes that are not on the path.
    deleteNodesNotOnPath(view, startNode, targetNode);
    if (startNode.getOutgoingEdges().isEmpty()) {
        // no path exists between the two nodes
        return null;
    }
    try {
        view.save();
    } catch (final CouldntSaveDataException exception) {
        CUtilityFunctions.logException(exception);
    }
    return view;
}
Also used : HashMap(java.util.HashMap) CouldntSaveDataException(com.google.security.zynamics.binnavi.API.disassembly.CouldntSaveDataException) BasicBlock(com.google.security.zynamics.binnavi.API.disassembly.BasicBlock) View(com.google.security.zynamics.binnavi.API.disassembly.View) Function(com.google.security.zynamics.binnavi.API.disassembly.Function) ViewEdge(com.google.security.zynamics.binnavi.API.disassembly.ViewEdge) ViewNode(com.google.security.zynamics.binnavi.API.disassembly.ViewNode) FunctionBlock(com.google.security.zynamics.binnavi.API.disassembly.FunctionBlock)

Example 2 with ViewEdge

use of com.google.security.zynamics.binnavi.API.disassembly.ViewEdge in project binnavi by google.

the class PathFinder method createInitialEdges.

/**
   * Creates view edges for all edges in the passed functions.
   *
   * @param view The view where the edges are created.
   * @param passedFunctions All functions that lie on the path.
   * @param nodeMap Maps between the basic blocks of the functions and their corresponding code
   *        nodes.
   */
private static void createInitialEdges(final View view, final Collection<FunctionBlock> passedFunctions, final Map<BasicBlock, ViewNode> nodeMap) {
    for (final FunctionBlock functionBlock : passedFunctions) {
        final Function function = functionBlock.getFunction();
        for (final BlockEdge edge : function.getGraph().getEdges()) {
            final ViewEdge newEdge = view.createEdge(nodeMap.get(edge.getSource()), nodeMap.get(edge.getTarget()), edge.getType());
            newEdge.setColor(getEdgeColor(edge));
        }
    }
}
Also used : Function(com.google.security.zynamics.binnavi.API.disassembly.Function) BlockEdge(com.google.security.zynamics.binnavi.API.disassembly.BlockEdge) ViewEdge(com.google.security.zynamics.binnavi.API.disassembly.ViewEdge) FunctionBlock(com.google.security.zynamics.binnavi.API.disassembly.FunctionBlock)

Example 3 with ViewEdge

use of com.google.security.zynamics.binnavi.API.disassembly.ViewEdge in project binnavi by google.

the class PathFinder method connectFunctions.

/**
   * Connects the functions in the view using inlining edges.
   *
   * @param view The view where the edges are created.
   * @param startNode The start node of the path.
   * @param targetNode The target node of the path.
   * @param passedFunctions All functions that lie on the path.
   * @param entryNodes Keeps track of the entry nodes of all functions.
   * @param exitNodes Keeps track of the exit nodes of all functions.
   * @param functionMap Keeps track to what function a node belongs to.
   *
   * @return Node pair that contains the updated start node and target node.
   */
private static NodePair connectFunctions(final View view, final ViewNode startNode, final ViewNode targetNode, final Collection<FunctionBlock> passedFunctions, final Map<Function, ViewNode> entryNodes, final ArrayListMultimap<Function, ViewNode> exitNodes, final Map<ViewNode, Function> functionMap) {
    ViewNode realStartNode = startNode;
    ViewNode realTargetNode = targetNode;
    final Set<ViewNode> handled = new HashSet<ViewNode>();
    while (true) {
        boolean splitNode = false;
        start: for (final ViewNode node : view.getGraph().getNodes()) {
            if (handled.contains(node)) {
                continue;
            }
            if (!(node instanceof CodeNode)) {
                continue;
            }
            final CodeNode cnode = (CodeNode) node;
            for (final Instruction instruction : cnode.getInstructions()) {
                for (final FunctionBlock functionBlock : passedFunctions) {
                    final Function function = functionBlock.getFunction();
                    if (callsFunction(instruction, function)) {
                        // A function call to a function on the path was found.
                        // At this point we have to split the code node after
                        // the function call.
                        final NodePair result = splitBlock(view, functionMap.get(cnode), cnode, instruction);
                        if (realStartNode == cnode) {
                            // Of course it is possible that the start node was split,
                            // therefore we have to update the start node to the upper
                            // part of the new node.
                            realStartNode = result.getFirst();
                        }
                        if (realTargetNode == cnode) {
                            // Of course it is possible that the target node was split,
                            // therefore we have to update the target node to the upper
                            // part of the new node.
                            realTargetNode = result.getFirst();
                        }
                        // too.
                        for (final FunctionBlock functionBlock2 : passedFunctions) {
                            final Function function2 = functionBlock2.getFunction();
                            if (entryNodes.get(function2) == cnode) {
                                // Update the entry nodes
                                entryNodes.put(function2, result.getFirst());
                            }
                            if (exitNodes.get(function2).contains(cnode)) {
                                // Update the exit nodes
                                if (result.getSecond() != null) {
                                    exitNodes.remove(function2, cnode);
                                    exitNodes.put(function2, result.getSecond());
                                }
                            }
                        }
                        if (functionMap.containsKey(cnode)) {
                            final Function f = functionMap.get(cnode);
                            functionMap.remove(cnode);
                            functionMap.put(result.getFirst(), f);
                        }
                        handled.add(result.getFirst());
                        if (result.getSecond() == null) {
                            for (final ViewEdge edge : node.getOutgoingEdges()) {
                                for (final ViewNode currentExitNode : exitNodes.get(function)) {
                                    final ViewEdge leaveEdge = view.createEdge(currentExitNode, edge.getTarget(), EdgeType.LeaveInlinedFunction);
                                    leaveEdge.setColor(DEFAULT_INLINING_EDGE_COLOR);
                                }
                                view.deleteEdge(edge);
                            }
                            final ViewEdge enterEdge = view.createEdge(result.getFirst(), entryNodes.get(function), EdgeType.EnterInlinedFunction);
                            enterEdge.setColor(DEFAULT_INLINING_EDGE_COLOR);
                            handled.add(cnode);
                        } else {
                            // The node was split. We simply have to connect both split parts to the
                            // called function.
                            final ViewEdge enterEdge = view.createEdge(result.getFirst(), entryNodes.get(function), EdgeType.EnterInlinedFunction);
                            enterEdge.setColor(DEFAULT_INLINING_EDGE_COLOR);
                            for (final ViewNode currentExitNode : exitNodes.get(function)) {
                                final ViewEdge leaveEdge = view.createEdge(currentExitNode, result.getSecond(), EdgeType.LeaveInlinedFunction);
                                leaveEdge.setColor(DEFAULT_INLINING_EDGE_COLOR);
                            }
                        }
                        splitNode = true;
                        break start;
                    }
                }
            }
            handled.add(cnode);
        }
        if (!splitNode) {
            break;
        }
    }
    return new NodePair(realStartNode, realTargetNode);
}
Also used : Function(com.google.security.zynamics.binnavi.API.disassembly.Function) CodeNode(com.google.security.zynamics.binnavi.API.disassembly.CodeNode) ViewEdge(com.google.security.zynamics.binnavi.API.disassembly.ViewEdge) ViewNode(com.google.security.zynamics.binnavi.API.disassembly.ViewNode) Instruction(com.google.security.zynamics.binnavi.API.disassembly.Instruction) FunctionBlock(com.google.security.zynamics.binnavi.API.disassembly.FunctionBlock) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with ViewEdge

use of com.google.security.zynamics.binnavi.API.disassembly.ViewEdge in project binnavi by google.

the class OutputGraphGenerator method createCompleteView.

/**
   * Creates a view that shows all nodes and edges from the original call graph in addition to the
   * newly resolved functions.
   * 
   * @param target The target whose indirect modules were resolved.
   * @param indirectCallAddresses The addresses of the indirect call objects from the target.
   * @param resolvedAddresses The resolved function addresses.
   * 
   * @return The generated view.
   */
public static View createCompleteView(final ICallResolverTarget target, final List<IndirectCall> indirectCallAddresses, final Map<BigInteger, Set<ResolvedFunction>> resolvedAddresses) {
    final View view = target.createView();
    final Map<Function, FunctionNode> nodes = new HashMap<Function, FunctionNode>();
    for (final Module module : target.getModules()) {
        for (final Function function : module.getFunctions()) {
            final FunctionNode node = view.createFunctionNode(function);
            nodes.put(function, node);
        }
        final Callgraph callgraph = module.getCallgraph();
        for (final FunctionEdge edge : callgraph.getEdges()) {
            final FunctionNode sourceNode = nodes.get(edge.getSource().getFunction());
            final FunctionNode targetNode = nodes.get(edge.getTarget().getFunction());
            view.createEdge(sourceNode, targetNode, EdgeType.JumpUnconditional);
        }
    }
    for (final Entry<BigInteger, Set<ResolvedFunction>> element : resolvedAddresses.entrySet()) {
        final BigInteger start = element.getKey();
        final Set<ResolvedFunction> targets = element.getValue();
        final IndirectCall call = IndirectCallResolver.findIndirectCall(target.getDebugger(), indirectCallAddresses, start);
        final FunctionNode sourceNode = nodes.get(call.getFunction());
        if (sourceNode != null) {
            for (final ResolvedFunction targetFunction : targets) {
                final Function function = targetFunction.getFunction();
                if (function != null) {
                    final FunctionNode targetNode = nodes.get(function);
                    final ViewEdge edge = view.createEdge(sourceNode, targetNode, EdgeType.JumpUnconditional);
                    edge.setColor(Color.RED);
                }
            }
        }
    }
    return view;
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) FunctionNode(com.google.security.zynamics.binnavi.API.disassembly.FunctionNode) View(com.google.security.zynamics.binnavi.API.disassembly.View) Function(com.google.security.zynamics.binnavi.API.disassembly.Function) Callgraph(com.google.security.zynamics.binnavi.API.disassembly.Callgraph) FunctionEdge(com.google.security.zynamics.binnavi.API.disassembly.FunctionEdge) ViewEdge(com.google.security.zynamics.binnavi.API.disassembly.ViewEdge) BigInteger(java.math.BigInteger) Module(com.google.security.zynamics.binnavi.API.disassembly.Module) MemoryModule(com.google.security.zynamics.binnavi.API.debug.MemoryModule)

Example 5 with ViewEdge

use of com.google.security.zynamics.binnavi.API.disassembly.ViewEdge in project binnavi by google.

the class PathFinderTest method testInsideFunctionPartial.

@Test
public void testInsideFunctionPartial() throws CouldntLoadDataException, PartialLoadException {
    // Tests path finding somewhere inside a function
    final Function startFunction = findFunction(m_notepad, 0x01002452);
    final BasicBlock startBlock = findBlock(startFunction, 0x10024C2);
    final BasicBlock endBlock = findBlock(startFunction, 0x10026FB);
    final View view = PathFinder.createPath(m_notepad, startBlock, endBlock, null, null);
    assertEquals(9, view.getGraph().nodeCount());
    assertEquals(11, view.getGraph().edgeCount());
    final List<ViewEdge> edges = view.getGraph().getEdges();
    final List<ViewNode> nodes = view.getGraph().getNodes();
    assertEquals(EdgeType.JumpConditionalFalse, findEdge(edges, 0x10024C2, 0x1002523).getType());
    assertEquals(EdgeType.JumpConditionalTrue, findEdge(edges, 0x10024C2, 0x1002539).getType());
    assertEquals(EdgeType.JumpUnconditional, findEdge(edges, 0x100253F, 0x10026F9).getType());
    assertEquals(Color.GREEN, findNode(nodes, 0x10024C2).getColor());
    assertEquals(Color.YELLOW, findNode(nodes, 0x10026FB).getColor());
}
Also used : Function(com.google.security.zynamics.binnavi.API.disassembly.Function) BasicBlock(com.google.security.zynamics.binnavi.API.disassembly.BasicBlock) ViewEdge(com.google.security.zynamics.binnavi.API.disassembly.ViewEdge) ViewNode(com.google.security.zynamics.binnavi.API.disassembly.ViewNode) View(com.google.security.zynamics.binnavi.API.disassembly.View) Test(org.junit.Test)

Aggregations

ViewEdge (com.google.security.zynamics.binnavi.API.disassembly.ViewEdge)6 Function (com.google.security.zynamics.binnavi.API.disassembly.Function)5 FunctionBlock (com.google.security.zynamics.binnavi.API.disassembly.FunctionBlock)3 View (com.google.security.zynamics.binnavi.API.disassembly.View)3 ViewNode (com.google.security.zynamics.binnavi.API.disassembly.ViewNode)3 BasicBlock (com.google.security.zynamics.binnavi.API.disassembly.BasicBlock)2 CodeNode (com.google.security.zynamics.binnavi.API.disassembly.CodeNode)2 Instruction (com.google.security.zynamics.binnavi.API.disassembly.Instruction)2 HashMap (java.util.HashMap)2 MemoryModule (com.google.security.zynamics.binnavi.API.debug.MemoryModule)1 BlockEdge (com.google.security.zynamics.binnavi.API.disassembly.BlockEdge)1 Callgraph (com.google.security.zynamics.binnavi.API.disassembly.Callgraph)1 CouldntSaveDataException (com.google.security.zynamics.binnavi.API.disassembly.CouldntSaveDataException)1 FunctionEdge (com.google.security.zynamics.binnavi.API.disassembly.FunctionEdge)1 FunctionNode (com.google.security.zynamics.binnavi.API.disassembly.FunctionNode)1 Module (com.google.security.zynamics.binnavi.API.disassembly.Module)1 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1