Search in sources :

Example 71 with IAddress

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

the class CReilViewCreator method create.

/**
 * Creates a REIL view from a view.
 *
 * @param container The container in which the new REIL view is created.
 * @param view The view to be translated to REIL code.
 *
 * @return The created REIL code view.
 *
 * @throws InternalTranslationException Thrown if the view could not be translated to REIL code.
 * @throws CouldntLoadDataException
 */
public static INaviView create(final INaviModule container, final INaviView view) throws InternalTranslationException, CouldntLoadDataException {
    Preconditions.checkNotNull(container, "IE01768: Container argument can not be null");
    Preconditions.checkNotNull(view, "IE01769: View argument can not be null");
    final Map<IAddress, String> textMap = new HashMap<IAddress, String>();
    for (final CCodeNode node : view.getBasicBlocks()) {
        for (final INaviInstruction instruction : node.getInstructions()) {
            textMap.put(instruction.getAddress(), instruction.toString());
        }
    }
    final INaviView reilView = CReilViewCreator.create(container, view.getContent().getReilCode().getGraph());
    for (final CCodeNode node : reilView.getBasicBlocks()) {
        for (final INaviInstruction reilInstruction : node.getInstructions()) {
            if ((reilInstruction.getAddress().toLong() & 0xFF) == 0) {
                try {
                    node.getComments().appendLocalInstructionComment(reilInstruction, textMap.get(ReilHelpers.toNativeAddress(reilInstruction.getAddress())));
                } catch (final CouldntSaveDataException e) {
                    // Not possible for unsaved views.
                    CUtilityFunctions.logException(e);
                }
            }
        }
    }
    return reilView;
}
Also used : INaviView(com.google.security.zynamics.binnavi.disassembly.views.INaviView) HashMap(java.util.HashMap) CCodeNode(com.google.security.zynamics.binnavi.disassembly.CCodeNode) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction)

Example 72 with IAddress

use of com.google.security.zynamics.zylib.disassembly.IAddress 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 73 with IAddress

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

the class PostgreSQLAddressSpaceFunctions method loadModules.

/**
 * Loads the modules of an address space and their respective image bases inside the address
 * space.
 *
 * The address space must be stored in the database connected to by the provider argument.
 *
 * @param provider The SQL provider that provides the database connection.
 * @param addressSpace The address space whose modules are loaded.
 *
 * @return A list of the modules of the address space and their image base.
 *
 * @throws CouldntLoadDataException Thrown if the modules that belong to the address space could
 *         not be loaded.
 */
public static List<Pair<IAddress, INaviModule>> loadModules(final AbstractSQLProvider provider, final CAddressSpace addressSpace) throws CouldntLoadDataException {
    checkArguments(provider, addressSpace);
    final CConnection connection = provider.getConnection();
    final List<Pair<IAddress, INaviModule>> modules = new ArrayList<Pair<IAddress, INaviModule>>();
    final String query = "SELECT id, name, md5, sha1, description, import_time, " + CTableNames.SPACE_MODULES_TABLE + ".image_base " + " FROM " + CTableNames.SPACE_MODULES_TABLE + " JOIN " + CTableNames.MODULES_TABLE + " ON id = module_id WHERE address_space_id = " + addressSpace.getConfiguration().getId();
    try (ResultSet resultSet = connection.executeQuery(query, true)) {
        while (resultSet.next()) {
            final IAddress imageBase = PostgreSQLHelpers.loadAddress(resultSet, "image_base");
            final INaviModule module = provider.findModule(resultSet.getInt("id"));
            modules.add(new Pair<IAddress, INaviModule>(imageBase, module));
        }
        return modules;
    } catch (final SQLException e) {
        throw new CouldntLoadDataException(e);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) Pair(com.google.security.zynamics.zylib.general.Pair)

Example 74 with IAddress

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

the class PostgreSQLEdgeFunctions method appendGlobalEdgeComment.

/**
 * Appends a global edge comment to the list of global edge comments associated with this edge.
 *
 * @param provider The provider to access the database with.
 * @param edge The Edge where to comment is associated with.
 * @param commentText The text of the comment which will be appended.
 * @param userId The user id of the currently active user.
 *
 * @throws CouldntSaveDataException If the data could not be stored in the database.
 */
public static Integer appendGlobalEdgeComment(final AbstractSQLProvider provider, final INaviEdge edge, final String commentText, final Integer userId) throws CouldntSaveDataException {
    Preconditions.checkNotNull(provider, "IE00482: provider argument can not be null");
    Preconditions.checkNotNull(edge, "IE00483: edge argument can not be null");
    Preconditions.checkNotNull(commentText, "IE00484: commentText argument can not be null");
    Preconditions.checkNotNull(userId, "IE00485: userId argument can not be null");
    final Connection connection = provider.getConnection().getConnection();
    final String function = "{ ? = call append_global_edge_comment(?, ?, ?, ?, ?, ?) }";
    try {
        final int sourceModuleId = getModuleId(edge.getSource());
        final int destinationModuleId = getModuleId(edge.getTarget());
        final IAddress sourceAddress = CViewNodeHelpers.getAddress(edge.getSource());
        final IAddress destinationAddress = CViewNodeHelpers.getAddress(edge.getTarget());
        try {
            final CallableStatement appendCommentFunction = connection.prepareCall(function);
            try {
                appendCommentFunction.registerOutParameter(1, Types.INTEGER);
                appendCommentFunction.setInt(2, sourceModuleId);
                appendCommentFunction.setInt(3, destinationModuleId);
                appendCommentFunction.setObject(4, sourceAddress.toBigInteger(), Types.BIGINT);
                appendCommentFunction.setObject(5, destinationAddress.toBigInteger(), Types.BIGINT);
                appendCommentFunction.setInt(6, userId);
                appendCommentFunction.setString(7, commentText);
                appendCommentFunction.execute();
                final int commentId = appendCommentFunction.getInt(1);
                if (appendCommentFunction.wasNull()) {
                    throw new CouldntSaveDataException("Error: Got an comment id of null from the database");
                }
                return commentId;
            } finally {
                appendCommentFunction.close();
            }
        } catch (final SQLException exception) {
            throw new CouldntSaveDataException(exception);
        }
    } catch (final MaybeNullException exception) {
        throw new CouldntSaveDataException(exception);
    }
}
Also used : MaybeNullException(com.google.security.zynamics.binnavi.Exceptions.MaybeNullException) SQLException(java.sql.SQLException) CallableStatement(java.sql.CallableStatement) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) Connection(java.sql.Connection) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress)

Example 75 with IAddress

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

the class PostgreSQLFunctionFunctions method setName.

/**
 * Changes the name of a function.
 *
 * The function must be stored in the database connected to by the provider argument.
 *
 * @param provider The SQL provider that provides the connection.
 * @param function The function whose name is changed.
 * @param name The new name of the function.
 *
 * @throws CouldntSaveDataException Thrown if storing the new name to the database failed.
 */
public static void setName(final AbstractSQLProvider provider, final INaviFunction function, final String name) throws CouldntSaveDataException {
    Preconditions.checkNotNull(provider, "IE00456: Provider argument can not be null");
    Preconditions.checkNotNull(function, "IE00457: Function argument can not be null");
    Preconditions.checkNotNull(name, "IE00458: Name argument can not be null");
    Preconditions.checkArgument(function.inSameDatabase(provider), "IE00459: Function is not part of this database");
    final CConnection connection = provider.getConnection();
    final int module = function.getModule().getConfiguration().getId();
    final IAddress address = function.getAddress();
    final String query = "UPDATE " + CTableNames.FUNCTIONS_TABLE + " SET name = ? WHERE module_id = ? and address = ?";
    try (PreparedStatement statement = connection.getConnection().prepareStatement(query)) {
        statement.setString(1, name);
        statement.setInt(2, module);
        statement.setObject(3, address.toBigInteger(), java.sql.Types.BIGINT);
        statement.executeUpdate();
    } catch (final SQLException exception) {
        throw new CouldntSaveDataException(exception);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) PreparedStatement(java.sql.PreparedStatement) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress)

Aggregations

IAddress (com.google.security.zynamics.zylib.disassembly.IAddress)82 INaviModule (com.google.security.zynamics.binnavi.disassembly.INaviModule)28 ArrayList (java.util.ArrayList)23 CAddress (com.google.security.zynamics.zylib.disassembly.CAddress)19 INaviFunction (com.google.security.zynamics.binnavi.disassembly.INaviFunction)16 INaviInstruction (com.google.security.zynamics.binnavi.disassembly.INaviInstruction)15 Test (org.junit.Test)14 SQLException (java.sql.SQLException)12 CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)11 ResultSet (java.sql.ResultSet)11 BigInteger (java.math.BigInteger)10 HashMap (java.util.HashMap)10 INaviCodeNode (com.google.security.zynamics.binnavi.disassembly.INaviCodeNode)9 COperandTree (com.google.security.zynamics.binnavi.disassembly.COperandTree)7 INaviOperandTreeNode (com.google.security.zynamics.binnavi.disassembly.INaviOperandTreeNode)7 INaviView (com.google.security.zynamics.binnavi.disassembly.views.INaviView)7 CConnection (com.google.security.zynamics.binnavi.Database.CConnection)6 CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)6 ReilFunction (com.google.security.zynamics.reil.ReilFunction)6 List (java.util.List)6