Search in sources :

Example 11 with INaviModule

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

the class CViewInserterTest method test.

@Test
public void test() throws CouldntLoadDataException, CPartialLoadException, LoadCancelledException, FileReadException, CouldntSaveDataException {
    ConfigManager.instance().read();
    final INaviModule mockModule = new MockModule();
    final MockSqlProvider mockProvider = new MockSqlProvider();
    final CUserManager userManager = CUserManager.get(mockProvider);
    final IUser user = userManager.addUser(" VIEW INSERTER USER ");
    userManager.setCurrentActiveUser(user);
    final CModuleViewGenerator generator = new CModuleViewGenerator(mockProvider, mockModule);
    final CView view = generator.generate(1, "", "", ViewType.NonNative, GraphType.MIXED_GRAPH, new Date(), new Date(), 0, 0, new HashSet<CTag>(), new HashSet<CTag>(), false);
    view.load();
    final MockFunction mockFunction = new MockFunction(mockProvider);
    final CFunctionNode fnode1 = view.getContent().createFunctionNode(mockFunction);
    final CFunctionNode fnode2 = view.getContent().createFunctionNode(mockFunction);
    @SuppressWarnings("unused") final CNaviViewEdge edge1 = view.getContent().createEdge(fnode1, fnode2, EdgeType.JUMP_UNCONDITIONAL);
    final MockInstruction instruction1 = new MockInstruction();
    final CCodeNode cnode1 = view.getContent().createCodeNode(mockFunction, Lists.newArrayList(instruction1));
    final CCodeNode cnode2 = view.getContent().createCodeNode(mockFunction, Lists.newArrayList(instruction1));
    @SuppressWarnings("unused") final CNaviViewEdge edge2 = view.getContent().createEdge(cnode1, cnode2, EdgeType.JUMP_UNCONDITIONAL);
    final ArrayList<IComment> comments = Lists.<IComment>newArrayList(new CComment(null, CommonTestObjects.TEST_USER_1, null, "Foo"));
    final CTextNode tnode1 = view.getContent().createTextNode(comments);
    @SuppressWarnings("unused") final CNaviViewEdge edge3 = view.getContent().createEdge(cnode1, tnode1, EdgeType.JUMP_UNCONDITIONAL);
    final CGroupNode gnode1 = view.getContent().createGroupNode(Lists.newArrayList((INaviViewNode) fnode1, (INaviViewNode) fnode2));
    gnode1.appendComment("TEST GROUP NODE COMMENT 1");
    final CView view2 = generator.generate(2, "", "", ViewType.NonNative, GraphType.MIXED_GRAPH, new Date(), new Date(), 0, 0, new HashSet<CTag>(), new HashSet<CTag>(), false);
    view2.load();
    CViewInserter.insertView(view, view2);
    final List<INaviViewNode> nodes = view2.getGraph().getNodes();
    assertEquals(view2.getNodeCount(), 6);
    assertEquals(mockFunction, ((INaviFunctionNode) nodes.get(0)).getFunction());
    assertEquals(nodes.get(5), ((INaviFunctionNode) nodes.get(0)).getParentGroup());
}
Also used : MockFunction(com.google.security.zynamics.binnavi.disassembly.MockFunction) CFunctionNode(com.google.security.zynamics.binnavi.disassembly.CFunctionNode) CModuleViewGenerator(com.google.security.zynamics.binnavi.Database.CModuleViewGenerator) IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) CUserManager(com.google.security.zynamics.binnavi.Gui.Users.CUserManager) Date(java.util.Date) CView(com.google.security.zynamics.binnavi.disassembly.views.CView) CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) CNaviViewEdge(com.google.security.zynamics.binnavi.disassembly.CNaviViewEdge) MockInstruction(com.google.security.zynamics.binnavi.disassembly.MockInstruction) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) MockModule(com.google.security.zynamics.binnavi.disassembly.Modules.MockModule) MockSqlProvider(com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider) CCodeNode(com.google.security.zynamics.binnavi.disassembly.CCodeNode) IUser(com.google.security.zynamics.binnavi.Gui.Users.Interfaces.IUser) INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode) CTextNode(com.google.security.zynamics.binnavi.disassembly.CTextNode) CGroupNode(com.google.security.zynamics.binnavi.disassembly.CGroupNode) Test(org.junit.Test)

Example 12 with INaviModule

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

the class CPostgreSQLZyGraphTest2 method testSave.

@Test
public void testSave() throws CouldntSaveDataException, CouldntLoadDriverException, CouldntConnectException, InvalidDatabaseException, CouldntInitializeDatabaseException, CouldntLoadDataException, InvalidExporterDatabaseFormatException, InvalidDatabaseVersionException, CPartialLoadException, LoadCancelledException {
    m_view.getGraph().getNodes().get(0).setSelected(true);
    m_view.getGraph().getNodes().get(1).setColor(new Color(123));
    m_view.getGraph().getNodes().get(2).setX(456);
    m_view.getGraph().getNodes().get(3).setY(789);
    final INaviView newView = m_graph.saveAs(new CModuleContainer(m_database, m_module), "New View", "New View Description");
    m_database2.connect();
    m_database2.load();
    final INaviModule module = m_database2.getContent().getModules().get(0);
    module.load();
    final Iterable<INaviView> views = CViewFilter.getFlowgraphViews(module.getContent().getViewContainer().getViews());
    final INaviView loadedNewView = Iterables.getLast(views);
    loadedNewView.load();
    assertEquals(loadedNewView.getNodeCount(), newView.getNodeCount());
    assertEquals(true, loadedNewView.getGraph().getNodes().get(0).isSelected());
    assertEquals(0xFF00007B, loadedNewView.getGraph().getNodes().get(1).getColor().getRGB());
    assertEquals(456, loadedNewView.getGraph().getNodes().get(2).getX(), 0);
    assertEquals(789, loadedNewView.getGraph().getNodes().get(3).getY(), 0);
    m_database2.close();
}
Also used : INaviView(com.google.security.zynamics.binnavi.disassembly.views.INaviView) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) Color(java.awt.Color) CModuleContainer(com.google.security.zynamics.binnavi.disassembly.Modules.CModuleContainer) Test(org.junit.Test)

Example 13 with INaviModule

use of com.google.security.zynamics.binnavi.disassembly.INaviModule 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 14 with INaviModule

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

the class PostgreSQLAddressSpaceLoader method loadImageBases.

/**
   * Loads the image bases of the given modules within the given address space.
   * 
   * The address space ID and the modules in the module list must all reference items that are
   * stored in the database connected to by connection argument.
   * 
   * @param connection Connection to the database.
   * @param addressSpaceId ID of the address space.
   * @param list Modules whose image bases are loaded.
   * 
   * @return A mapping of modules -> image bases for all modules in the address space.
   * 
   * @throws CouldntLoadDataException Thrown if the image bases could not be loaded.
   */
private static Map<INaviModule, IAddress> loadImageBases(final CConnection connection, final int addressSpaceId, final List<INaviModule> list) throws CouldntLoadDataException {
    final HashMap<INaviModule, IAddress> imageBases = new HashMap<INaviModule, IAddress>();
    final String query = "SELECT module_id, image_base FROM " + CTableNames.SPACE_MODULES_TABLE + " WHERE address_space_id = " + addressSpaceId;
    try {
        final ResultSet resultSet = connection.executeQuery(query, true);
        try {
            while (resultSet.next()) {
                try {
                    final INaviModule module = findModule(list, resultSet.getInt("module_id"));
                    imageBases.put(module, PostgreSQLHelpers.loadAddress(resultSet, "image_base"));
                } catch (final MaybeNullException exception) {
                    // I can not think of a scenario where this can happen.
                    CUtilityFunctions.logException(exception);
                }
            }
        } finally {
            resultSet.close();
        }
        return imageBases;
    } catch (final SQLException exception) {
        throw new CouldntLoadDataException(exception);
    }
}
Also used : INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) MaybeNullException(com.google.security.zynamics.binnavi.Exceptions.MaybeNullException) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) CouldntLoadDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException) ResultSet(java.sql.ResultSet) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress)

Example 15 with INaviModule

use of com.google.security.zynamics.binnavi.disassembly.INaviModule 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)

Aggregations

INaviModule (com.google.security.zynamics.binnavi.disassembly.INaviModule)180 Test (org.junit.Test)105 ExpensiveBaseTest (com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest)69 INaviFunction (com.google.security.zynamics.binnavi.disassembly.INaviFunction)39 CAddress (com.google.security.zynamics.zylib.disassembly.CAddress)39 INaviView (com.google.security.zynamics.binnavi.disassembly.views.INaviView)29 UnrelocatedAddress (com.google.security.zynamics.binnavi.disassembly.UnrelocatedAddress)28 IAddress (com.google.security.zynamics.zylib.disassembly.IAddress)28 MockModule (com.google.security.zynamics.binnavi.disassembly.Modules.MockModule)24 BreakpointAddress (com.google.security.zynamics.binnavi.debug.models.breakpoints.BreakpointAddress)22 ArrayList (java.util.ArrayList)19 INaviInstruction (com.google.security.zynamics.binnavi.disassembly.INaviInstruction)18 CView (com.google.security.zynamics.binnavi.disassembly.views.CView)13 BigInteger (java.math.BigInteger)13 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)12 CouldntSaveDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException)11 CTagManager (com.google.security.zynamics.binnavi.Tagging.CTagManager)11 CouldntLoadDataException (com.google.security.zynamics.binnavi.Database.Exceptions.CouldntLoadDataException)9 CAddressSpace (com.google.security.zynamics.binnavi.disassembly.AddressSpaces.CAddressSpace)9 COperandTree (com.google.security.zynamics.binnavi.disassembly.COperandTree)9