Search in sources :

Example 1 with CConnection

use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.

the class PostgreSQLAddressSpaceLoader method loadAddressSpaces.

/**
   * Loads the address spaces of a project.
   * 
   * The project, the debugger manager, and all modules in the module list must be stored in the
   * database connected to by the provider argument.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param project The parent project of the address spaces to load.
   * @param debuggerManager Debugger manager of the database.
   * @param list A list of all modules that belong to the database.
   * 
   * @return A list that contains the address spaces of the project.
   * 
   * @throws CouldntLoadDataException Thrown if the address spaces could not be loaded.
   */
public static List<CAddressSpace> loadAddressSpaces(final AbstractSQLProvider provider, final INaviProject project, final DebuggerTemplateManager debuggerManager, final List<INaviModule> list) throws CouldntLoadDataException {
    checkArguments(provider, project);
    Preconditions.checkNotNull(debuggerManager, "IE01543: Debugger provider argument can not be null");
    Preconditions.checkNotNull(list, "IE01545: Modules argument can not be null");
    NaviLogger.info("Loading address spaces of project %s", project.getConfiguration().getName());
    final CConnection connection = provider.getConnection();
    final List<CAddressSpace> addressSpaces = new ArrayList<CAddressSpace>();
    final String query = "SELECT id, name, description, creation_date, modification_date, debugger_id " + " FROM " + CTableNames.ADDRESS_SPACES_TABLE + " WHERE project_id = " + project.getConfiguration().getId();
    try {
        final ResultSet resultSet = connection.executeQuery(query, true);
        try {
            while (resultSet.next()) {
                final int addressSpaceId = resultSet.getInt("id");
                final Map<INaviModule, IAddress> imageBases = loadImageBases(connection, addressSpaceId, list);
                final String name = PostgreSQLHelpers.readString(resultSet, "name");
                final String description = PostgreSQLHelpers.readString(resultSet, "description");
                final Timestamp creationDate = resultSet.getTimestamp("creation_date");
                final Timestamp modificationDate = resultSet.getTimestamp("modification_date");
                final DebuggerTemplate debuggerDescription = debuggerManager.findDebugger(resultSet.getInt("debugger_id"));
                addressSpaces.add(new CAddressSpace(addressSpaceId, name, description, creationDate, modificationDate, imageBases, debuggerDescription, provider, project));
            }
            return addressSpaces;
        } finally {
            resultSet.close();
        }
    } catch (final SQLException e) {
        throw new CouldntLoadDataException(e);
    }
}
Also used : DebuggerTemplate(com.google.security.zynamics.binnavi.debug.debugger.DebuggerTemplate) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ArrayList(java.util.ArrayList) Timestamp(java.sql.Timestamp) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) CConnection(com.google.security.zynamics.binnavi.Database.CConnection) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) ResultSet(java.sql.ResultSet) CAddressSpace(com.google.security.zynamics.binnavi.disassembly.AddressSpaces.CAddressSpace)

Example 2 with CConnection

use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.

the class PostgreSQLCallgraphLoader method loadCallgraph.

/**
   * Loads the call graph of a module.
   * 
   * The module, the call graph ID, and all functions of the function list must refer to objects
   * stored in the database connected to by the provider argument.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param module The module whose call graph is loaded.
   * @param callgraphId The view ID of the call graph to load.
   * @param functions A list that contains all functions of the module.
   * 
   * @return The loaded call graph.
   * 
   * @throws CouldntLoadDataException Thrown if the call graph could not be loaded.
   */
public static CCallgraph loadCallgraph(final AbstractSQLProvider provider, final CModule module, final int callgraphId, final List<INaviFunction> functions) throws CouldntLoadDataException {
    checkArguments(provider, module, functions);
    final CConnection connection = provider.getConnection();
    try {
        final Pair<List<ICallgraphNode>, Map<Integer, CCallgraphNode>> nodeResult = loadNodes(connection, callgraphId, functions);
        final List<ICallgraphEdge> edges = loadEdges(connection, callgraphId, nodeResult.second());
        return new CCallgraph(nodeResult.first(), edges);
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) ICallgraphEdge(com.google.security.zynamics.binnavi.disassembly.ICallgraphEdge) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) CCallgraph(com.google.security.zynamics.binnavi.disassembly.CCallgraph)

Example 3 with CConnection

use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.

the class PostgreSQLModuleFlowgraphsLoader method loadModuleFlowgraphs.

/**
   * Loads the flow graphs of a module.
   * 
   * @param provider The connection to the database.
   * @param module The module whose flow graph views are loaded.
   * @param viewTagManager The tag manager responsible for tagging the views.
   * @param nodeTagManager The tag manager responsible for tagging view nodes.
   * @param viewType The type of the views to load.
   * 
   * @return The loaded views.
   * 
   * @throws CouldntLoadDataException Thrown if the views could not be loaded.
   */
private static ImmutableList<IFlowgraphView> loadModuleFlowgraphs(final AbstractSQLProvider provider, final CModule module, final CTagManager viewTagManager, final CTagManager nodeTagManager, final ViewType viewType) throws CouldntLoadDataException {
    checkArguments(provider, module, viewTagManager);
    final String query = " SELECT * FROM load_module_flow_graphs(?, ?) ";
    final CConnection connection = provider.getConnection();
    try {
        final PreparedStatement statement = connection.getConnection().prepareStatement(query);
        statement.setInt(1, module.getConfiguration().getId());
        statement.setObject(2, viewType == ViewType.Native ? "native" : "non-native", Types.OTHER);
        final ResultSet resultSet = statement.executeQuery();
        final Map<Integer, Set<CTag>> tags = loadTags(connection, module, viewTagManager);
        return new ImmutableList.Builder<IFlowgraphView>().addAll(processQueryResults(resultSet, module, tags, nodeTagManager, provider, new ArrayList<CView>(), viewType, GraphType.FLOWGRAPH)).build();
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) CView(com.google.security.zynamics.binnavi.disassembly.views.CView) Set(java.util.Set) ResultSet(java.sql.ResultSet) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 4 with CConnection

use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.

the class PostgreSQLProjectCallgraphLoader method loadCallgraphViews.

/**
   * Loads the non-native call graph views of a project.
   * 
   * The project, the node tag manager, and the view tag manager must be stored in the database
   * connected to by the provider argument.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param project The project from where the views are loaded.
   * @param viewTagManager View tag manager that contains all view tags of the database.
   * @param nodeTagManager The tag manager responsible for tagging view nodes.
   * 
   * @return A list of non-native call graph views.
   * 
   * @throws CouldntLoadDataException Thrown if the views could not be loaded.
   */
public static List<ICallgraphView> loadCallgraphViews(final AbstractSQLProvider provider, final CProject project, final CTagManager viewTagManager, final CTagManager nodeTagManager) throws CouldntLoadDataException {
    checkArguments(provider, project, viewTagManager);
    final String query = " SELECT * FROM load_project_call_graphs(?, ?) ";
    try {
        final CConnection connection = provider.getConnection();
        final PreparedStatement statement = connection.getConnection().prepareStatement(query);
        statement.setInt(1, project.getConfiguration().getId());
        statement.setObject(2, "non-native", Types.OTHER);
        final ResultSet resultSet = statement.executeQuery();
        final Map<Integer, Set<CTag>> tags = loadTags(connection, project, viewTagManager);
        return new ArrayList<ICallgraphView>(processQueryResults(resultSet, project, tags, nodeTagManager, provider, new ArrayList<CView>(), ViewType.NonNative, GraphType.CALLGRAPH));
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) Set(java.util.Set) ResultSet(java.sql.ResultSet) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement)

Example 5 with CConnection

use of com.google.security.zynamics.binnavi.Database.CConnection in project binnavi by google.

the class PostgreSQLModuleFunctions method deleteModule.

/**
   * Deletes a module from the database.
   *
   * The module must be stored in the database connected to by the provider argument.
   *
   * @param provider The SQL provider that provides the connection.
   * @param module The module to be deleted.
   *
   * @throws CouldntDeleteException Thrown if the module could not be deleted.
   */
public static void deleteModule(final AbstractSQLProvider provider, final INaviModule module) throws CouldntDeleteException {
    PostgreSQLModuleFunctions.checkArguments(provider, module);
    NaviLogger.info("Deleting module %s", module.getConfiguration().getName());
    final CConnection connection = provider.getConnection();
    try {
        final String moduleViewQuery = "DELETE FROM " + CTableNames.VIEWS_TABLE + " " + " WHERE id IN (SELECT view_id FROM " + CTableNames.MODULE_VIEWS_TABLE + " WHERE module_id = " + module.getConfiguration().getId() + ")";
        connection.executeUpdate(moduleViewQuery, true);
        final String nodeQuery = "DELETE FROM " + CTableNames.NODES_TABLE + " " + " WHERE id IN " + " (SELECT view_id FROM " + CTableNames.MODULE_VIEWS_TABLE + " WHERE module_id = " + module.getConfiguration().getId() + ")";
        connection.executeUpdate(nodeQuery, true);
        final String instructionsQuery = String.format("DELETE FROM " + CTableNames.INSTRUCTIONS_TABLE + " WHERE module_id = %d", module.getConfiguration().getId());
        connection.executeUpdate(instructionsQuery, true);
        connection.executeUpdate(String.format("delete FROM " + CTableNames.EXPRESSION_TREE_TABLE + "_mapping where module_id = %d", module.getConfiguration().getId()), true);
        connection.executeUpdate(String.format("delete FROM " + CTableNames.EXPRESSION_TREE_TABLE + " where module_id = %d", module.getConfiguration().getId()), true);
        connection.executeUpdate(String.format("delete FROM " + CTableNames.EXPRESSION_TREE_TABLE + "_ids where module_id = %d", module.getConfiguration().getId()), true);
        connection.executeUpdate(String.format("delete FROM " + CTableNames.CODE_NODES_TABLE + " where module_id = %d", module.getConfiguration().getId()), true);
        connection.executeUpdate(String.format("delete from " + CTableNames.MODULES_TABLE + " where id = %d", module.getConfiguration().getId()), true);
    } catch (final SQLException e) {
        throw new CouldntDeleteException(e);
    }
}
Also used : CConnection(com.google.security.zynamics.binnavi.Database.CConnection) CouldntDeleteException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntDeleteException) SQLException(java.sql.SQLException)

Aggregations

CConnection (com.google.security.zynamics.binnavi.Database.CConnection)70 SQLException (java.sql.SQLException)59 CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)31 PreparedStatement (java.sql.PreparedStatement)25 ResultSet (java.sql.ResultSet)25 CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)20 ArrayList (java.util.ArrayList)15 CouldntDeleteException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntDeleteException)7 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)6 IAddress (com.google.security.zynamics.zylib.disassembly.IAddress)6 Timestamp (java.sql.Timestamp)6 HashMap (java.util.HashMap)6 Set (java.util.Set)6 DebuggerTemplate (com.google.security.zynamics.binnavi.debug.debugger.DebuggerTemplate)5 AbstractSQLProvider (com.google.security.zynamics.binnavi.Database.AbstractSQLProvider)4 INaviEdge (com.google.security.zynamics.binnavi.disassembly.INaviEdge)4 INaviViewNode (com.google.security.zynamics.binnavi.disassembly.INaviViewNode)4 ExpensiveBaseTest (com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest)4 CallableStatement (java.sql.CallableStatement)4 Test (org.junit.Test)4