Search in sources :

Example 16 with IAddress

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

the class TypeManagerTest method testNotifySubstitutionCreated.

@Test
public void testNotifySubstitutionCreated() throws CouldntSaveDataException {
    final TypeSubstitutionChangedEventCollector events = new TypeSubstitutionChangedEventCollector();
    typeManager.addListener(events);
    final IAddress address = new CAddress(0x1000);
    final TypeSubstitution substitution = typeManager.createTypeSubstitution(new MockOperandTreeNode(), typeSystem.intType, 0, 0, address);
    final TypeSubstitutionChangedEventCollector expectedEvents = new TypeSubstitutionChangedEventCollector();
    expectedEvents.substitutionsAdded(Sets.newHashSet(substitution));
    Assert.assertEquals(expectedEvents, events);
}
Also used : MockOperandTreeNode(com.google.security.zynamics.binnavi.disassembly.MockOperandTreeNode) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress) Test(org.junit.Test)

Example 17 with IAddress

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

the class PostgreSQLCallgraphLoader method loadNodes.

/**
 * Loads the nodes of a call graph.
 *
 * @param connection Connection to the database.
 * @param callgraphId ID of the call graph view to load.
 * @param functions List of functions in the module whose call graph is loaded.
 *
 * @return <Call graph nodes, Call graph node IDs => Call graph nodes>
 *
 * @throws SQLException Thrown if loading the nodes failed.
 */
private static Pair<List<ICallgraphNode>, Map<Integer, CCallgraphNode>> loadNodes(final CConnection connection, final int callgraphId, final Collection<INaviFunction> functions) throws SQLException {
    // TODO: Simplify the return value of this method.
    // For performance reasons, we need a quick way to look up functions by their address.
    final Map<IAddress, INaviFunction> functionMap = getFunctionMap(functions);
    final List<ICallgraphNode> nodes = new ArrayList<ICallgraphNode>();
    final String nodeQuery = "SELECT nodes.id, function FROM " + CTableNames.NODES_TABLE + " AS nodes JOIN " + CTableNames.FUNCTION_NODES_TABLE + " AS function_nodes ON nodes.id = function_nodes.node_id WHERE nodes.view_id = " + callgraphId;
    final ResultSet nodeResult = connection.executeQuery(nodeQuery, true);
    final HashMap<Integer, CCallgraphNode> nodeMap = new HashMap<Integer, CCallgraphNode>();
    try {
        while (nodeResult.next()) {
            final int nodeId = nodeResult.getInt("id");
            final IAddress functionAddress = PostgreSQLHelpers.loadAddress(nodeResult, "function");
            final INaviFunction function = functionMap.get(functionAddress);
            final CCallgraphNode cgnode = new CCallgraphNode(function);
            nodeMap.put(nodeId, cgnode);
            nodes.add(cgnode);
        }
    } finally {
        nodeResult.close();
    }
    return new Pair<List<ICallgraphNode>, Map<Integer, CCallgraphNode>>(nodes, nodeMap);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CCallgraphNode(com.google.security.zynamics.binnavi.disassembly.CCallgraphNode) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) ICallgraphNode(com.google.security.zynamics.binnavi.disassembly.ICallgraphNode) ResultSet(java.sql.ResultSet) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction) Pair(com.google.security.zynamics.zylib.general.Pair)

Example 18 with IAddress

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

the class PostgreSQLInstructionFunctions method createInstructions.

/**
 * Saves an instruction to the database.
 *
 * @param provider The provider used to access the database.
 * @param instructions The instruction to save.
 *
 * @throws SQLException Thrown if the instruction could not be created.
 */
public static void createInstructions(final SQLProvider provider, final Iterable<INaviInstruction> instructions) throws SQLException {
    Preconditions.checkNotNull(provider, "IE01550: Provider argument can not be null");
    Preconditions.checkNotNull(instructions, "IE01554: Instruction argument can not be null");
    final String query = "INSERT INTO " + CTableNames.INSTRUCTIONS_TABLE + "(module_id, address, mnemonic, data, native, architecture, comment_id) " + "VALUES(?, ?, ?, ?, ?, ?, ?)";
    final PreparedStatement insertStatement = provider.getConnection().getConnection().prepareStatement(query);
    final ArrayList<INaviInstruction> instructionsWithUnsavedComments = new ArrayList<INaviInstruction>();
    final List<List<COperandTree>> operands = new ArrayList<List<COperandTree>>();
    for (final INaviInstruction instruction : instructions) {
        final String mnemonic = instruction.getMnemonic();
        final byte[] data = instruction.getData();
        operands.add(instruction.getOperands());
        final INaviModule module = instruction.getModule();
        final IAddress address = instruction.getAddress();
        final int moduleID = module.getConfiguration().getId();
        final List<IComment> comments = instruction.getGlobalComment();
        final Integer commentId = comments == null ? null : comments.size() == 0 ? null : Iterables.getLast(comments).getId();
        if ((comments != null) && (comments.size() != 0) && (commentId == null)) {
            instructionsWithUnsavedComments.add(instruction);
        }
        try {
            insertStatement.setInt(1, moduleID);
            insertStatement.setObject(2, address.toBigInteger(), Types.BIGINT);
            insertStatement.setString(3, mnemonic);
            insertStatement.setBytes(4, data);
            insertStatement.setBoolean(5, false);
            insertStatement.setObject(6, instruction.getArchitecture(), Types.OTHER);
            if (commentId == null) {
                insertStatement.setNull(7, Types.INTEGER);
            } else {
                insertStatement.setInt(7, commentId);
            }
            insertStatement.execute();
        } finally {
            insertStatement.close();
        }
    }
    // unsaved comments.
    for (final INaviInstruction instruction : instructionsWithUnsavedComments) {
        final ArrayList<IComment> instructionComments = new ArrayList<IComment>();
        for (final IComment comment : instruction.getGlobalComment()) {
            try {
                final Integer commentId = PostgreSQLInstructionFunctions.appendGlobalInstructionComment(provider, instruction, comment.getComment(), comment.getUser().getUserId());
                final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
                instructionComments.add(newComment);
            } catch (final CouldntSaveDataException exception) {
                CUtilityFunctions.logException(exception);
            }
        }
        instruction.initializeGlobalComment(instructionComments);
    }
    for (final List<COperandTree> operand : operands) {
        int position = 0;
        for (final COperandTree operandTree : operand) {
            createOperandTree(provider, operandTree, position);
            position++;
        }
    }
}
Also used : IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) BigInteger(java.math.BigInteger) CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) COperandTree(com.google.security.zynamics.binnavi.disassembly.COperandTree) ArrayList(java.util.ArrayList) List(java.util.List) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction)

Example 19 with IAddress

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

the class PostgreSQLModuleFunctions method readModule.

public static CModule readModule(final CConnection connection, final int moduleId, final INaviRawModule rawModule, final SQLProvider provider) throws CouldntLoadDataException {
    Preconditions.checkNotNull(rawModule, "IE01797: Raw module argument can not be null");
    Preconditions.checkNotNull(provider, "IE01798: Provider argument can not be null");
    final String query = "SELECT id, " + CTableNames.MODULES_TABLE + ".name, md5, sha1, " + " description, import_time, modification_date, image_base, file_base, stared, " + " initialization_state " + " FROM " + CTableNames.MODULES_TABLE + " WHERE id = " + moduleId + " ORDER by id";
    try {
        final ResultSet resultSet = connection.executeQuery(query, true);
        try {
            while (resultSet.next()) {
                final String name = PostgreSQLHelpers.readString(resultSet, "name");
                final String md5 = PostgreSQLHelpers.readString(resultSet, "md5");
                final String sha1 = PostgreSQLHelpers.readString(resultSet, "sha1");
                final String comment = PostgreSQLHelpers.readString(resultSet, "description");
                final Timestamp importTime = resultSet.getTimestamp("import_time");
                final Timestamp modificationDate = resultSet.getTimestamp("modification_date");
                final int functionCount = rawModule.getFunctionCount();
                final int viewCount = 0;
                final IAddress imageBase = PostgreSQLHelpers.loadAddress(resultSet, "image_base");
                final IAddress fileBase = PostgreSQLHelpers.loadAddress(resultSet, "file_base");
                final boolean isStared = resultSet.getBoolean("stared");
                final int initializationState = resultSet.getInt("initialization_state");
                return new CModule(moduleId, name, comment, importTime, modificationDate, md5, sha1, functionCount, viewCount, fileBase, imageBase, null, rawModule, initializationState, isStared, provider);
            }
        } finally {
            resultSet.close();
        }
    } catch (final SQLException e) {
        throw new CouldntLoadDataException(e);
    }
    throw new CouldntLoadDataException("Error: No module with the given ID exists");
}
Also used : SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ResultSet(java.sql.ResultSet) Timestamp(java.sql.Timestamp) CModule(com.google.security.zynamics.binnavi.disassembly.Modules.CModule) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress)

Example 20 with IAddress

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

the class PostgreSQLSectionFunctions method loadSections.

/**
 * Loads all sections that are associated with the given module.
 *
 * @param provider The SQL provider that holds the database connection.
 * @param module The module whose sections should be loaded.
 * @return The list of sections loaded from the database.
 * @throws CouldntLoadDataException Thrown if the sections could not be loaded from the database.
 */
public static Map<Section, Integer> loadSections(final SQLProvider provider, final INaviModule module) throws CouldntLoadDataException {
    Preconditions.checkNotNull(provider, "Error: provider argument can not be null");
    Preconditions.checkNotNull(module, "Error: module argument can not be null");
    final HashMap<Section, Integer> sections = Maps.newHashMap();
    final String query = "SELECT * FROM get_sections(?)";
    try (PreparedStatement statement = provider.getConnection().getConnection().prepareStatement(query)) {
        statement.setInt(1, module.getConfiguration().getId());
        final ResultSet result = statement.executeQuery();
        while (result.next()) {
            final int id = result.getInt("id");
            final String name = result.getString("name");
            Integer commentId = result.getInt("comment_id");
            if (result.wasNull()) {
                commentId = null;
            }
            final IAddress startAddress = new CAddress(result.getLong("start_address"));
            final IAddress endAddress = new CAddress(result.getLong("end_address"));
            final SectionPermission permission = SectionPermission.valueOf(result.getString("permission"));
            final byte[] data = result.getBytes("data");
            sections.put(new Section(id, name, CommentManager.get(provider), module, startAddress, endAddress, permission, data), commentId);
        }
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
    return sections;
}
Also used : SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) PreparedStatement(java.sql.PreparedStatement) Section(com.google.security.zynamics.binnavi.disassembly.types.Section) SectionPermission(com.google.security.zynamics.binnavi.disassembly.types.SectionPermission) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress) BigInteger(java.math.BigInteger) ResultSet(java.sql.ResultSet)

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