Search in sources :

Example 6 with EdgeType

use of com.google.security.zynamics.zylib.gui.zygraph.edges.EdgeType in project binnavi by google.

the class CInliningHelper method inlineCodeNode.

/**
 * Inlines the basic blocks of a function into a code node.
 *
 * @param view The view where the inlining operation takes place.
 * @param originalNode The node where the inlining operation takes place.
 * @param inlineInstruction The function call instruction after which the function is inlined.
 * @param functionToInline The function to be inlined.
 *
 * @return Contains information about the inlining result.
 */
public static CInliningResult inlineCodeNode(final INaviView view, final INaviCodeNode originalNode, final INaviInstruction inlineInstruction, final INaviFunction functionToInline) {
    Preconditions.checkNotNull(view, "IE00108: View argument can not be null");
    Preconditions.checkNotNull(originalNode, "IE00109: Node argument can not be null");
    Preconditions.checkNotNull(inlineInstruction, "IE00110: Instruction argument can not be null");
    Preconditions.checkArgument(originalNode.hasInstruction(inlineInstruction), "IE00111: Instruction is not part of the code node");
    Preconditions.checkNotNull(functionToInline, "IE00112: Function argument can not be null");
    Preconditions.checkArgument(view.isLoaded(), "IE00113: View must be loaded before it can be inlined");
    Preconditions.checkArgument(view.getGraph().getNodes().contains(originalNode), "IE00114: Code node does not belong to the view");
    Preconditions.checkArgument(functionToInline.isLoaded(), "IE00115: Function must be loaded before it can be inlined");
    Preconditions.checkArgument(functionToInline.getBasicBlockCount() != 0, "IE00116: Functions with 0 blocks can not be inlined");
    final INaviGroupNode parentGroup = originalNode.getParentGroup();
    GroupHelpers.expandParents(originalNode);
    final List<INaviEdge> oldIncomingEdges = originalNode.getIncomingEdges();
    final List<INaviEdge> oldOutgoingEdges = originalNode.getOutgoingEdges();
    // At first we find out which instructions will be part of the new first block
    // and which instructions will be part of the new second block.
    final List<INaviInstruction> upperInstructions = new ArrayList<INaviInstruction>();
    final List<INaviInstruction> lowerInstructions = new ArrayList<INaviInstruction>();
    List<INaviInstruction> currentBlock = upperInstructions;
    for (final INaviInstruction currentInstruction : originalNode.getInstructions()) {
        currentBlock.add(currentInstruction);
        if (currentInstruction == inlineInstruction) {
            currentBlock = lowerInstructions;
        }
    }
    // Now we create the new nodes from the instructions blocks
    INaviCodeNode firstNode;
    final List<INaviViewNode> continueNodes = new ArrayList<INaviViewNode>();
    final boolean keepOriginalBlock = lowerInstructions.isEmpty();
    CCodeNode returnNode = null;
    if (keepOriginalBlock) {
        // There are no instructions in the second block => therefore the call instruction
        // is the last instruction of the block => therefore no splitting is necessary =>
        // therefore we can just reuse the original block.
        firstNode = originalNode;
        for (final INaviEdge edge : originalNode.getOutgoingEdges()) {
            continueNodes.add(edge.getTarget());
            view.getContent().deleteEdge(edge);
        }
    } else {
        // The second block is not empty => the call instruction is somewhere in the middle =>
        // the block must be split => the original block becomes useless and must be replaced by
        // two new blocks.
        final boolean recolor = (originalNode.getIncomingEdges().size() == 1) && (originalNode.getIncomingEdges().get(0).getType() == EdgeType.ENTER_INLINED_FUNCTION) && (originalNode.getOutgoingEdges().size() == 1) && (originalNode.getOutgoingEdges().get(0).getType() == EdgeType.LEAVE_INLINED_FUNCTION);
        view.getContent().deleteNode(originalNode);
        try {
            firstNode = view.getContent().createCodeNode(originalNode.getParentFunction(), upperInstructions);
        } catch (final MaybeNullException exception) {
            firstNode = view.getContent().createCodeNode(null, upperInstructions);
        }
        firstNode.setColor(originalNode.getColor());
        firstNode.setBorderColor(originalNode.getBorderColor());
        try {
            returnNode = view.getContent().createCodeNode(originalNode.getParentFunction(), lowerInstructions);
        } catch (final MaybeNullException e1) {
            returnNode = view.getContent().createCodeNode(null, lowerInstructions);
        }
        returnNode.setColor(originalNode.getColor());
        if (recolor) {
            firstNode.setBorderColor(new Color(-16736256));
            returnNode.setBorderColor(new Color(-6291456));
        }
        if (parentGroup != null) {
            parentGroup.addElement(firstNode);
            parentGroup.addElement(returnNode);
        }
        // Copy the tags of the original node too
        final Iterator<CTag> it = originalNode.getTagsIterator();
        while (it.hasNext()) {
            final CTag tag = it.next();
            try {
                firstNode.tagNode(tag);
                returnNode.tagNode(tag);
            } catch (final CouldntSaveDataException e) {
                CUtilityFunctions.logException(e);
            }
        }
        continueNodes.add(returnNode);
    }
    // Insert the nodes and edges from the loaded function
    final Triple<CCodeNode, List<CCodeNode>, ArrayList<CCodeNode>> nodes = insertNodes(view, functionToInline, parentGroup);
    final INaviCodeNode entryNode = nodes.first();
    final List<CCodeNode> exitNodes = nodes.second();
    if (!keepOriginalBlock) {
        for (final INaviEdge incomingEdge : oldIncomingEdges) {
            if (incomingEdge.getSource() == originalNode) {
                final EdgeType edgeType = incomingEdge.getType();
                view.getContent().createEdge(returnNode, firstNode, edgeType);
            } else {
                final EdgeType edgeType = incomingEdge.getType();
                view.getContent().createEdge(incomingEdge.getSource(), firstNode, edgeType);
            }
        }
    }
    // Create an edge from the upper part of the split block to the entry node
    // of the inlined function.
    view.getContent().createEdge(firstNode, entryNode, EdgeType.ENTER_INLINED_FUNCTION);
    // of the original function where control flow continues.
    for (final INaviCodeNode exitNode : exitNodes) {
        for (final INaviViewNode continueNode : continueNodes) {
            view.getContent().createEdge(exitNode, continueNode, EdgeType.LEAVE_INLINED_FUNCTION);
        }
    }
    if (!keepOriginalBlock) {
        for (final INaviEdge oldChild : oldOutgoingEdges) {
            for (final INaviViewNode continueNode : continueNodes) {
                if (oldChild.getTarget() != originalNode) {
                    view.getContent().createEdge(continueNode, oldChild.getTarget(), oldChild.getType());
                }
            }
        }
    }
    return new CInliningResult(firstNode, returnNode);
}
Also used : MaybeNullException(com.google.security.zynamics.binnavi.Exceptions.MaybeNullException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) Color(java.awt.Color) ArrayList(java.util.ArrayList) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) EdgeType(com.google.security.zynamics.zylib.gui.zygraph.edges.EdgeType) ArrayList(java.util.ArrayList) List(java.util.List) CInliningResult(com.google.security.zynamics.binnavi.disassembly.algorithms.CInliningResult)

Example 7 with EdgeType

use of com.google.security.zynamics.zylib.gui.zygraph.edges.EdgeType in project binnavi by google.

the class PostgreSQLEdgeLoader method loadEdges.

/**
 * Loads the edges of a view.
 *
 * @param provider The connection to the database.
 * @param view The view whose edges are loaded.
 * @param nodeLookup Maps between node IDs and their corresponding node objects.
 * @param edgeToGlobalCommentMap Maps between edge IDs and their associated comments.
 *
 * @return The loaded edges.
 *
 * @throws CouldntLoadDataException
 */
private static List<INaviEdge> loadEdges(final AbstractSQLProvider provider, final INaviView view, final Map<Integer, INaviViewNode> nodeLookup, final Map<Integer, ArrayList<IComment>> edgeToGlobalCommentMap) throws CouldntLoadDataException {
    final String query = "SELECT * FROM load_view_edges(" + view.getConfiguration().getId() + ")";
    List<CBend> currentPaths = new ArrayList<>();
    final Map<Integer, INaviEdge> commentIdToEdge = new HashMap<Integer, INaviEdge>();
    final Map<Integer, INaviEdge> edgeIdToEdge = new HashMap<Integer, INaviEdge>();
    try {
        final CConnection connection = provider.getConnection();
        final PreparedStatement statement = connection.getConnection().prepareStatement(query);
        final ResultSet resultSet = statement.executeQuery();
        try {
            while (resultSet.next()) {
                final int edgeId = resultSet.getInt("id");
                if (edgeIdToEdge.containsKey(edgeId)) {
                    final INaviEdge edge = edgeIdToEdge.get(edgeId);
                    final double pathX = resultSet.getDouble("x");
                    final double pathY = resultSet.getDouble("y");
                    if (!resultSet.wasNull()) {
                        edge.addBend(pathX, pathY);
                    }
                    continue;
                }
                final int sourceNode = resultSet.getInt("source_node_id");
                final int targetNode = resultSet.getInt("target_node_id");
                Integer localCommentId = resultSet.getInt("comment_id");
                if (resultSet.wasNull()) {
                    localCommentId = null;
                }
                final double x1 = resultSet.getDouble("x1");
                final double y1 = resultSet.getDouble("y1");
                final double x2 = resultSet.getDouble("x2");
                final double y2 = resultSet.getDouble("y2");
                final EdgeType type = EdgeType.valueOf(resultSet.getString("type").toUpperCase());
                final Color color = new Color(resultSet.getInt("color"));
                final boolean visible = resultSet.getBoolean("visible");
                final boolean selected = resultSet.getBoolean("selected");
                final INaviViewNode source = nodeLookup.get(sourceNode);
                final INaviViewNode target = nodeLookup.get(targetNode);
                final double pathX = resultSet.getDouble("x");
                final double pathY = resultSet.getDouble("y");
                if (!resultSet.wasNull()) {
                    currentPaths.add(new CBend(pathX, pathY));
                }
                final CNaviViewEdge edge = new CNaviViewEdge(edgeId, source, target, type, x1, y1, x2, y2, color, selected, visible, null, currentPaths, provider);
                if (localCommentId != null) {
                    commentIdToEdge.put(localCommentId, edge);
                }
                final ArrayList<IComment> globalComments = edgeToGlobalCommentMap.containsKey(edgeId) ? edgeToGlobalCommentMap.get(edgeId) : null;
                if ((globalComments != null) && (globalComments.size() != 0)) {
                    initializeGlobalComment(edge, globalComments, provider);
                }
                source.addOutgoingEdge(edge);
                target.addIncomingEdge(edge);
                edgeIdToEdge.put(edge.getId(), edge);
                currentPaths = new ArrayList<CBend>();
            }
            if (!commentIdToEdge.isEmpty()) {
                final HashMap<Integer, ArrayList<IComment>> commentIdToComments = PostgreSQLCommentFunctions.loadMultipleCommentsById(provider, commentIdToEdge.keySet());
                for (final Entry<Integer, ArrayList<IComment>> commentIdToComment : commentIdToComments.entrySet()) {
                    commentIdToEdge.get(commentIdToComment.getKey()).initializeLocalComment(commentIdToComment.getValue());
                }
            }
        } finally {
            resultSet.close();
        }
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException("Error: Loading of view edges failed");
    }
    return Lists.newArrayList(edgeIdToEdge.values());
}
Also used : IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ArrayList(java.util.ArrayList) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge) CBend(com.google.security.zynamics.zylib.gui.zygraph.edges.CBend) ResultSet(java.sql.ResultSet) INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode) Color(java.awt.Color) PreparedStatement(java.sql.PreparedStatement) EdgeType(com.google.security.zynamics.zylib.gui.zygraph.edges.EdgeType) CConnection(com.google.security.zynamics.binnavi.Database.CConnection) CNaviViewEdge(com.google.security.zynamics.binnavi.disassembly.CNaviViewEdge)

Aggregations

EdgeType (com.google.security.zynamics.zylib.gui.zygraph.edges.EdgeType)7 ArrayList (java.util.ArrayList)5 ReilBlock (com.google.security.zynamics.reil.ReilBlock)4 ReilEdge (com.google.security.zynamics.reil.ReilEdge)3 ReilInstruction (com.google.security.zynamics.reil.ReilInstruction)3 IAddress (com.google.security.zynamics.zylib.disassembly.IAddress)2 Triple (com.google.security.zynamics.zylib.general.Triple)2 Color (java.awt.Color)2 HashMap (java.util.HashMap)2 List (java.util.List)2 CConnection (com.google.security.zynamics.binnavi.Database.CConnection)1 CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)1 CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)1 MaybeNullException (com.google.security.zynamics.binnavi.Exceptions.MaybeNullException)1 IComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment)1 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)1 CNaviViewEdge (com.google.security.zynamics.binnavi.disassembly.CNaviViewEdge)1 INaviEdge (com.google.security.zynamics.binnavi.disassembly.INaviEdge)1 INaviViewNode (com.google.security.zynamics.binnavi.disassembly.INaviViewNode)1 CInliningResult (com.google.security.zynamics.binnavi.disassembly.algorithms.CInliningResult)1