Search in sources :

Example 16 with CFunctionNode

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

the class View method createNode.

// ! Clones an existing node.
/**
 * Creates a new view node by cloning an existing view node.
 *
 * @param node The node to clone.
 *
 * @return The cloned node.
 */
public ViewNode createNode(final ViewNode node) {
    Preconditions.checkNotNull(node, "Error: Node argument can not be null");
    if (node instanceof CodeNode) {
        final List<INaviInstruction> instructionsList = new ArrayList<INaviInstruction>();
        for (final Instruction instruction : ((CodeNode) node).getInstructions()) {
            Preconditions.checkNotNull(instruction, "Error: Instruction list contains a null-element");
            instructionsList.add(instruction.getNative());
        }
        CCodeNode newNode;
        try {
            newNode = naviView.getContent().createCodeNode(((INaviCodeNode) node.getNative()).getParentFunction(), instructionsList);
        } catch (final MaybeNullException e) {
            newNode = naviView.getContent().createCodeNode(null, instructionsList);
        }
        adjustAttributes(node, newNode);
        return cachedNodes.get(newNode);
    } else if (node instanceof FunctionNode) {
        final CFunctionNode newNode = naviView.getContent().createFunctionNode(((INaviFunctionNode) node.getNative()).getFunction());
        adjustAttributes(node, newNode);
        return cachedNodes.get(newNode);
    } else if (node instanceof TextNode) {
        final CTextNode newNode = naviView.getContent().createTextNode(((TextNode) node).getComments());
        adjustAttributes(node, newNode);
        return cachedNodes.get(newNode);
    } else if (node instanceof GroupNode) {
        throw new IllegalStateException("Group nodes can not be cloned");
    } else {
        throw new IllegalStateException("Error: Unknown node type");
    }
}
Also used : CFunctionNode(com.google.security.zynamics.binnavi.disassembly.CFunctionNode) MaybeNullException(com.google.security.zynamics.binnavi.Exceptions.MaybeNullException) ArrayList(java.util.ArrayList) CFunctionNode(com.google.security.zynamics.binnavi.disassembly.CFunctionNode) INaviFunctionNode(com.google.security.zynamics.binnavi.disassembly.INaviFunctionNode) INaviTextNode(com.google.security.zynamics.binnavi.disassembly.INaviTextNode) CTextNode(com.google.security.zynamics.binnavi.disassembly.CTextNode) CGroupNode(com.google.security.zynamics.binnavi.disassembly.CGroupNode) INaviGroupNode(com.google.security.zynamics.binnavi.disassembly.INaviGroupNode) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction) INaviCodeNode(com.google.security.zynamics.binnavi.disassembly.INaviCodeNode) CCodeNode(com.google.security.zynamics.binnavi.disassembly.CCodeNode) INaviCodeNode(com.google.security.zynamics.binnavi.disassembly.INaviCodeNode) CCodeNode(com.google.security.zynamics.binnavi.disassembly.CCodeNode) INaviFunctionNode(com.google.security.zynamics.binnavi.disassembly.INaviFunctionNode) CTextNode(com.google.security.zynamics.binnavi.disassembly.CTextNode) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction)

Example 17 with CFunctionNode

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

the class PostgreSQLFunctionNodeLoader method load.

/**
 * Loads the function nodes of a view.
 *
 * @param provider The connection to the database.
 * @param view The view whose function nodes are loaded.
 * @param nodes The loaded nodes are stored here.
 *
 * @throws CPartialLoadException Thrown if loading the nodes failed because a necessary module was
 *         not loaded.
 * @throws CouldntLoadDataException
 */
public static void load(final AbstractSQLProvider provider, final INaviView view, final List<INaviViewNode> nodes) throws CPartialLoadException, CouldntLoadDataException {
    Preconditions.checkNotNull(provider, "IE02510: provider argument can not be null");
    Preconditions.checkNotNull(view, "IE02511: view argument can not be null");
    Preconditions.checkNotNull(nodes, "IE02512: nodes argument can not be null");
    // TODO (timkornau): query needs to go into the database.
    final String query = "SELECT nodes.view_id, nodes.id, functions.module_id, " + " function, fnodes.comment_id as local_comment, x, y, width, height, " + " color, selected, visible FROM " + CTableNames.NODES_TABLE + " AS nodes JOIN " + CTableNames.FUNCTION_NODES_TABLE + " AS fnodes " + " ON nodes.id = fnodes.node_id JOIN " + CTableNames.FUNCTIONS_TABLE + " AS functions ON functions.address = fnodes.function " + " AND functions.module_id = fnodes.module_id  WHERE view_id = ?";
    final Map<Integer, INaviFunctionNode> commentIdToFunctionNode = new HashMap<Integer, INaviFunctionNode>();
    try {
        final PreparedStatement statement = provider.getConnection().getConnection().prepareStatement(query);
        statement.setInt(1, view.getConfiguration().getId());
        final ResultSet resultSet = statement.executeQuery();
        try {
            while (resultSet.next()) {
                final int moduleId = resultSet.getInt("module_id");
                final INaviModule module = provider.findModule(moduleId);
                if (!module.isLoaded()) {
                    try {
                        module.load();
                    } catch (final CouldntLoadDataException e) {
                        throw new CPartialLoadException("E00064: The view could not be loaded because not all modules that form the view are loaded", module);
                    } catch (final LoadCancelledException e) {
                        throw new CPartialLoadException("E00065: The view could not be loaded because not all modules that form the view are loaded", module);
                    }
                }
                final IAddress address = PostgreSQLHelpers.loadAddress(resultSet, "function");
                final INaviFunction function = module.getContent().getFunctionContainer().getFunction(address);
                final int nodeId = resultSet.getInt("id");
                Integer commentId = resultSet.getInt("local_comment");
                if (resultSet.wasNull()) {
                    commentId = null;
                }
                final double posX = resultSet.getDouble("x");
                final double posY = resultSet.getDouble("y");
                final double width = resultSet.getDouble("width");
                final double height = resultSet.getDouble("height");
                final Color color = new Color(resultSet.getInt("color"));
                final boolean selected = resultSet.getBoolean("selected");
                final boolean visible = resultSet.getBoolean("visible");
                final INaviFunctionNode functionNode = new CFunctionNode(nodeId, function, posX, posY, width, height, color, selected, visible, null, new HashSet<CTag>(), provider);
                nodes.add(functionNode);
                if (commentId != null) {
                    commentIdToFunctionNode.put(commentId, functionNode);
                }
            }
        } finally {
            resultSet.close();
        }
        if (!commentIdToFunctionNode.isEmpty()) {
            final HashMap<Integer, ArrayList<IComment>> commentIdsToComments = PostgreSQLCommentFunctions.loadMultipleCommentsById(provider, commentIdToFunctionNode.keySet());
            for (final Entry<Integer, ArrayList<IComment>> commentIdToComment : commentIdsToComments.entrySet()) {
                commentIdToFunctionNode.get(commentIdToComment.getKey()).initializeLocalFunctionComment(commentIdToComment.getValue());
            }
        }
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
}
Also used : CFunctionNode(com.google.security.zynamics.binnavi.disassembly.CFunctionNode) CPartialLoadException(com.google.security.zynamics.binnavi.Database.Exceptions.CPartialLoadException) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) Color(java.awt.Color) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) ResultSet(java.sql.ResultSet) INaviFunctionNode(com.google.security.zynamics.binnavi.disassembly.INaviFunctionNode) LoadCancelledException(com.google.security.zynamics.binnavi.Database.Exceptions.LoadCancelledException) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction)

Example 18 with CFunctionNode

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

the class CCallgraphCombiner method createCombinedCallgraph.

/**
 * Combines the call graphs of the modules of an address space.
 *
 * @param project The project where the combined view is created.
 * @param addressSpace Provides the modules whose call graphs are combined.
 *
 * @return The view that contains the combined call graph.
 */
public static INaviView createCombinedCallgraph(final INaviProject project, final INaviAddressSpace addressSpace) {
    final INaviView view = project.getContent().createView("Combined Callgraph", "");
    final Map<INaviFunction, CFunctionNode> nodeMap = new HashMap<INaviFunction, CFunctionNode>();
    final Map<INaviFunction, INaviFunction> resolvedMap = new HashMap<INaviFunction, INaviFunction>();
    final List<INaviModule> modules = addressSpace.getContent().getModules();
    // functions that are not forwarded.
    for (final INaviModule module : modules) {
        final CCallgraph callgraph = module.getContent().getNativeCallgraph();
        for (final ICallgraphNode callgraphNode : callgraph) {
            final INaviFunction function = callgraphNode.getFunction();
            final INaviFunction resolvedFunction = getResolvedFunction(function, modules);
            if (resolvedFunction == null) {
                final CFunctionNode node = view.getContent().createFunctionNode(function);
                node.setColor(CFunctionNodeColorizer.getFunctionColor(function.getType()));
                nodeMap.put(function, node);
            } else {
                resolvedMap.put(function, resolvedFunction);
            }
        }
    }
    // for the forwarded functions.
    for (final INaviModule module : modules) {
        final CCallgraph callgraph = module.getContent().getNativeCallgraph();
        for (final ICallgraphEdge callgraphEdge : callgraph.getEdges()) {
            final INaviFunction source = resolvedMap.containsKey(callgraphEdge.getSource().getFunction()) ? resolvedMap.get(callgraphEdge.getSource().getFunction()) : callgraphEdge.getSource().getFunction();
            final INaviFunction target = resolvedMap.containsKey(callgraphEdge.getTarget().getFunction()) ? resolvedMap.get(callgraphEdge.getTarget().getFunction()) : callgraphEdge.getTarget().getFunction();
            view.getContent().createEdge(nodeMap.get(source), nodeMap.get(target), EdgeType.JUMP_UNCONDITIONAL);
        }
    }
    return view;
}
Also used : ICallgraphEdge(com.google.security.zynamics.binnavi.disassembly.ICallgraphEdge) CFunctionNode(com.google.security.zynamics.binnavi.disassembly.CFunctionNode) ICallgraphNode(com.google.security.zynamics.binnavi.disassembly.ICallgraphNode) INaviView(com.google.security.zynamics.binnavi.disassembly.views.INaviView) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) HashMap(java.util.HashMap) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction) CCallgraph(com.google.security.zynamics.binnavi.disassembly.CCallgraph)

Aggregations

CFunctionNode (com.google.security.zynamics.binnavi.disassembly.CFunctionNode)18 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)10 INaviFunction (com.google.security.zynamics.binnavi.disassembly.INaviFunction)9 Test (org.junit.Test)8 MockView (com.google.security.zynamics.binnavi.disassembly.MockView)7 CCodeNode (com.google.security.zynamics.binnavi.disassembly.CCodeNode)6 CFunction (com.google.security.zynamics.binnavi.disassembly.CFunction)6 CView (com.google.security.zynamics.binnavi.disassembly.views.CView)6 CAddress (com.google.security.zynamics.zylib.disassembly.CAddress)6 ArrayList (java.util.ArrayList)6 MockSqlProvider (com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider)5 MockTagManager (com.google.security.zynamics.binnavi.Tagging.MockTagManager)5 INaviFunctionNode (com.google.security.zynamics.binnavi.disassembly.INaviFunctionNode)5 INaviViewNode (com.google.security.zynamics.binnavi.disassembly.INaviViewNode)5 Date (java.util.Date)5 MockDatabase (com.google.security.zynamics.binnavi.Database.MockClasses.MockDatabase)4 CComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment)4 CTagManager (com.google.security.zynamics.binnavi.Tagging.CTagManager)4 CTextNode (com.google.security.zynamics.binnavi.disassembly.CTextNode)4 INaviInstruction (com.google.security.zynamics.binnavi.disassembly.INaviInstruction)4