Search in sources :

Example 6 with CModule

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

the class CModuleContainerNodeTest method testUnloaded.

@Test
public void testUnloaded() throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException {
    final CRawModule rawModule = new CRawModule(1, "", 0, false, m_provider);
    final CModule module1 = new CModule(123, "Module 1", "Comment", new Date(), new Date(), "12345678123456781234567812345678", "1234567812345678123456781234567812345678", 55, 66, new CAddress(0x555), new CAddress(0x666), new DebuggerTemplate(1, "Mock Debugger", "localhaus", 88, m_provider), rawModule, Integer.MAX_VALUE, false, m_provider);
    final CModule module2 = new CModule(123, "Module 2", "Comment", new Date(), new Date(), "12345678123456781234567812345678", "1234567812345678123456781234567812345678", 55, 66, new CAddress(0x555), new CAddress(0x666), new DebuggerTemplate(1, "Mock Debugger", "localhaus", 88, m_provider), rawModule, Integer.MAX_VALUE, false, m_provider);
    final CModuleContainerNode node = new CModuleContainerNode(m_tree, m_database);
    assertEquals("Modules (0)", node.toString());
    assertEquals(0, node.getChildCount());
    m_database.getContent().addModule(module1);
    m_database.getContent().addModule(module2);
    assertEquals("Modules (2)", node.toString());
    assertEquals(2, node.getChildCount());
    assertEquals("Module 1 (55/66)", node.getChildAt(0).toString());
    assertEquals("Module 2 (55/66)", node.getChildAt(1).toString());
    m_database.getContent().delete(module1);
    assertEquals("Modules (1)", node.toString());
    assertEquals(1, node.getChildCount());
    assertEquals("Module 2 (55/66)", node.getChildAt(0).toString());
    node.dispose();
    assertTrue(((Collection<?>) ReflectionHelpers.getField(ReflectionHelpers.getField(m_database, "listeners"), "m_listeners")).isEmpty());
    assertTrue(((Collection<?>) ReflectionHelpers.getField(ReflectionHelpers.getField(m_database.getContent().getDebuggerTemplateManager(), "listeners"), "m_listeners")) == null);
}
Also used : DebuggerTemplate(com.google.security.zynamics.binnavi.debug.debugger.DebuggerTemplate) CRawModule(com.google.security.zynamics.binnavi.disassembly.CRawModule) CModule(com.google.security.zynamics.binnavi.disassembly.Modules.CModule) Date(java.util.Date) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress) Test(org.junit.Test)

Example 7 with CModule

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

the class ZyGraphFactory method generateTestGraph.

public static ZyGraph generateTestGraph() throws FileReadException, CouldntLoadDataException, LoadCancelledException, CouldntSaveDataException {
    ConfigManager.instance().read();
    final MockDatabase database = new MockDatabase();
    final SQLProvider provider = new MockSqlProvider();
    final CModule module = new CModule(1, "", "", new Date(), new Date(), CommonTestObjects.MD5, CommonTestObjects.SHA1, 0, 0, new CAddress(0), new CAddress(0), null, null, Integer.MAX_VALUE, false, provider);
    final ZyGraphViewSettings settings = new ZyGraphViewSettings(new CallGraphSettingsConfigItem());
    settings.getLayoutSettings().setDefaultGraphLayout(LayoutStyle.CIRCULAR);
    database.getContent().addModule(module);
    final MockDatabaseManager manager = new MockDatabaseManager();
    manager.addDatabase(database);
    module.load();
    final CView m_view = module.getContent().getViewContainer().createView("name", "description");
    final LinkedHashMap<Node, NaviNode> nodeMap = new LinkedHashMap<Node, NaviNode>();
    final ZyGraph2DView g2dView = new ZyGraph2DView();
    final LinkedHashMap<Edge, NaviEdge> edgeMap = new LinkedHashMap<Edge, NaviEdge>();
    final Node node1 = g2dView.getGraph2D().createNode();
    final COperandTreeNode rootNode1 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_SIZE_PREFIX_ID, "b4", null, new ArrayList<IReference>(), provider, module.getTypeManager(), module.getContent().getTypeInstanceContainer());
    final COperandTreeNode childNode1 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_REGISTER_ID, "eax", null, new ArrayList<IReference>(), provider, module.getTypeManager(), module.getContent().getTypeInstanceContainer());
    COperandTreeNode.link(rootNode1, childNode1);
    final COperandTreeNode rootNode2 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_SIZE_PREFIX_ID, "b4", null, new ArrayList<IReference>(), provider, module.getTypeManager(), module.getContent().getTypeInstanceContainer());
    final COperandTreeNode childNode2 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_REGISTER_ID, "ebx", null, new ArrayList<IReference>(), provider, module.getTypeManager(), module.getContent().getTypeInstanceContainer());
    COperandTreeNode.link(rootNode2, childNode2);
    final COperandTree operand1 = new COperandTree(rootNode1, provider, module.getTypeManager(), module.getContent().getTypeInstanceContainer());
    final COperandTree operand2 = new COperandTree(rootNode2, provider, module.getTypeManager(), module.getContent().getTypeInstanceContainer());
    final ArrayList<COperandTree> operands1 = Lists.newArrayList(operand1, operand2);
    final List<INaviInstruction> instructions1 = Lists.newArrayList((INaviInstruction) new MockInstruction(new CAddress(0x123456), "mov", operands1, null, module));
    final INaviCodeNode rawNode1 = m_view.getContent().createCodeNode(null, instructions1);
    final ZyLabelContent content = ZyCodeNodeBuilder.buildContent(rawNode1, settings, null);
    nodeMap.put(node1, new NaviNode(node1, new ZyNormalNodeRealizer<NaviNode>(content), rawNode1));
    final Node node2 = g2dView.getGraph2D().createNode();
    final List<INaviInstruction> instructions2 = Lists.newArrayList((INaviInstruction) new MockInstruction());
    final INaviCodeNode rawNode2 = m_view.getContent().createCodeNode(null, instructions2);
    final ZyLabelContent content2 = ZyCodeNodeBuilder.buildContent(rawNode2, settings, null);
    nodeMap.put(node2, new NaviNode(node2, new ZyNormalNodeRealizer<NaviNode>(content2), rawNode2));
    rawNode2.setColor(Color.RED);
    rawNode2.setVisible(false);
    final CTag tag = new CTag(0, "Tag", "Description", TagType.NODE_TAG, new MockSqlProvider());
    rawNode2.tagNode(tag);
    final Node node3 = g2dView.getGraph2D().createNode();
    final String mnemonicName = "call";
    final List<COperandTree> operandTrees = new ArrayList<COperandTree>();
    final int type = ExpressionType.REGISTER.ordinal();
    final CReference reference = new CReference(new CAddress(0x123), ReferenceType.CALL_DIRECT);
    final List<IReference> referencea = new ArrayList<IReference>();
    referencea.add(reference);
    final COperandTreeNode root = new COperandTreeNode(0, type, "eax", null, referencea, provider, module.getTypeManager(), module.getContent().getTypeInstanceContainer());
    final COperandTree operandTree = new COperandTree(root, provider, module.getTypeManager(), module.getContent().getTypeInstanceContainer());
    operandTrees.add(0, operandTree);
    final List<INaviInstruction> instructions3 = Lists.newArrayList((INaviInstruction) new MockInstruction(mnemonicName, operandTrees, m_globalComment));
    final INaviCodeNode rawNode3 = m_view.getContent().createCodeNode(null, instructions3);
    final ZyLabelContent content3 = ZyCodeNodeBuilder.buildContent(rawNode3, settings, null);
    nodeMap.put(node3, new NaviNode(node3, new ZyNormalNodeRealizer<NaviNode>(content3), rawNode3));
    final Edge edge = g2dView.getGraph2D().createEdge(node1, node2);
    final INaviEdge rawEdge = m_view.getContent().createEdge(rawNode1, rawNode2, EdgeType.JUMP_UNCONDITIONAL);
    edgeMap.put(edge, new NaviEdge(nodeMap.get(node1), nodeMap.get(node2), edge, new ZyEdgeRealizer<NaviEdge>(new ZyLabelContent(null), null), rawEdge));
    final Edge edge2 = g2dView.getGraph2D().createEdge(node2, node3);
    final INaviEdge rawEdge2 = m_view.getContent().createEdge(rawNode2, rawNode3, EdgeType.JUMP_UNCONDITIONAL);
    edgeMap.put(edge2, new NaviEdge(nodeMap.get(node2), nodeMap.get(node3), edge, new ZyEdgeRealizer<NaviEdge>(new ZyLabelContent(null), null), rawEdge2));
    return new ZyGraph(m_view, nodeMap, edgeMap, settings, g2dView);
}
Also used : CallGraphSettingsConfigItem(com.google.security.zynamics.binnavi.config.CallGraphSettingsConfigItem) COperandTreeNode(com.google.security.zynamics.binnavi.disassembly.COperandTreeNode) Node(y.base.Node) NaviNode(com.google.security.zynamics.binnavi.yfileswrap.zygraph.NaviNode) INaviCodeNode(com.google.security.zynamics.binnavi.disassembly.INaviCodeNode) COperandTreeNode(com.google.security.zynamics.binnavi.disassembly.COperandTreeNode) NaviNode(com.google.security.zynamics.binnavi.yfileswrap.zygraph.NaviNode) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) ArrayList(java.util.ArrayList) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge) SQLProvider(com.google.security.zynamics.binnavi.Database.Interfaces.SQLProvider) CReference(com.google.security.zynamics.binnavi.disassembly.CReference) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress) LinkedHashMap(java.util.LinkedHashMap) CView(com.google.security.zynamics.binnavi.disassembly.views.CView) ZyEdgeRealizer(com.google.security.zynamics.zylib.yfileswrap.gui.zygraph.realizers.ZyEdgeRealizer) INaviCodeNode(com.google.security.zynamics.binnavi.disassembly.INaviCodeNode) MockSqlProvider(com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider) COperandTree(com.google.security.zynamics.binnavi.disassembly.COperandTree) MockDatabase(com.google.security.zynamics.binnavi.Database.MockClasses.MockDatabase) ZyGraph2DView(com.google.security.zynamics.zylib.yfileswrap.gui.zygraph.ZyGraph2DView) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction) ZyGraph(com.google.security.zynamics.binnavi.yfileswrap.zygraph.ZyGraph) Date(java.util.Date) MockInstruction(com.google.security.zynamics.binnavi.disassembly.MockInstruction) ZyLabelContent(com.google.security.zynamics.zylib.gui.zygraph.realizers.ZyLabelContent) ZyNormalNodeRealizer(com.google.security.zynamics.zylib.yfileswrap.gui.zygraph.realizers.ZyNormalNodeRealizer) MockDatabaseManager(com.google.security.zynamics.binnavi.Database.MockClasses.MockDatabaseManager) IReference(com.google.security.zynamics.zylib.disassembly.IReference) CModule(com.google.security.zynamics.binnavi.disassembly.Modules.CModule) NaviEdge(com.google.security.zynamics.binnavi.yfileswrap.zygraph.NaviEdge) Edge(y.base.Edge) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge) NaviEdge(com.google.security.zynamics.binnavi.yfileswrap.zygraph.NaviEdge) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge)

Example 8 with CModule

use of com.google.security.zynamics.binnavi.disassembly.Modules.CModule 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 9 with CModule

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

the class PostgreSQLDatabaseFunctions method loadModules.

/**
   * Loads the modules of a database.
   * 
   * @param provider The SQL provider that provides the connection.
   * @param rawModules Previously loaded raw module objects.
   * @param debuggerManager Debugger manager object that belongs to the given database.
   * 
   * @return A list of modules that contains the modules stored in the database.
   * 
   * @throws CouldntLoadDataException Thrown if the modules could not be loaded from the database.
   */
public static List<INaviModule> loadModules(final AbstractSQLProvider provider, final List<INaviRawModule> rawModules, final DebuggerTemplateManager debuggerManager) throws CouldntLoadDataException {
    Preconditions.checkNotNull(rawModules, "IE02043: rawModules argument can not be null");
    PostgreSQLDatabaseFunctions.checkArguments(provider, debuggerManager);
    final List<CModule> modules = new ArrayList<>();
    final CConnection connection = provider.getConnection();
    if (!PostgreSQLHelpers.hasTable(connection, CTableNames.MODULES_TABLE)) {
        return new ArrayList<INaviModule>(modules);
    }
    final String query = "SELECT id, raw_module_id, " + CTableNames.MODULES_TABLE + ".name, " + " md5, sha1, description, import_time, modification_date, file_base, image_base, stared, " + " initialization_state, debugger_id, " + " (SELECT count(*) FROM " + CTableNames.FUNCTIONS_TABLE + " " + " WHERE id = " + CTableNames.FUNCTIONS_TABLE + ".module_id) " + " AS function_count, " + " (SELECT count(*) FROM " + CTableNames.MODULE_VIEWS_TABLE + " JOIN " + CTableNames.VIEWS_TABLE + " ON view_id = id " + " WHERE type = 'non-native' and module_id = " + CTableNames.MODULES_TABLE + ".id) " + " AS view_count FROM " + CTableNames.MODULES_TABLE + " " + " WHERE raw_module_id IS NOT NULL ORDER BY id";
    try (ResultSet resultSet = connection.executeQuery(query, true)) {
        while (resultSet.next()) {
            final int moduleId = resultSet.getInt("id");
            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 timestamp = resultSet.getTimestamp("import_time");
            final Timestamp modificationDate = resultSet.getTimestamp("modification_date");
            int functionCount = resultSet.getInt("function_count");
            final int viewCount = resultSet.getInt("view_count");
            final IAddress imageBase = PostgreSQLHelpers.loadAddress(resultSet, "image_base");
            final IAddress fileBase = PostgreSQLHelpers.loadAddress(resultSet, "file_base");
            final int debuggerId = resultSet.getInt("debugger_id");
            final boolean isStared = resultSet.getBoolean("stared");
            final int initializationState = resultSet.getInt("initialization_state");
            final DebuggerTemplate description = debuggerManager.findDebugger(debuggerId);
            final int rawModuleId = resultSet.getInt("raw_module_id");
            final INaviRawModule rawModule = PostgreSQLDatabaseFunctions.findRawModule(rawModuleId, rawModules);
            if ((functionCount == 0) && (rawModule != null)) {
                functionCount = rawModule.getFunctionCount();
            }
            modules.add(new CModule(moduleId, name, comment, timestamp, modificationDate, md5, sha1, functionCount, viewCount, fileBase, imageBase, description, rawModule, initializationState, isStared, provider));
        }
    } catch (final SQLException e) {
        throw new CouldntLoadDataException(e);
    }
    return new ArrayList<INaviModule>(modules);
}
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) INaviRawModule(com.google.security.zynamics.binnavi.disassembly.INaviRawModule) ResultSet(java.sql.ResultSet) CModule(com.google.security.zynamics.binnavi.disassembly.Modules.CModule)

Example 10 with CModule

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

the class BasicBlockTest method testConstructor.

@Test
public void testConstructor() throws InternalTranslationException {
    final MockSqlProvider provider = new MockSqlProvider();
    final CModule internalModule = new CModule(1, "", "", new Date(), new Date(), "00000000000000000000000000000000", "0000000000000000000000000000000000000000", 0, 0, new CAddress(0), new CAddress(0), null, null, Integer.MAX_VALUE, false, provider);
    final CFunction internalFunction = new CFunction(internalModule, new MockView(), new CAddress(0x123), "Mock Function", "Mock Function", "Mock Description", 0, 0, 0, 0, FunctionType.NORMAL, "", 0, null, null, null, provider);
    final List<INaviInstruction> instructions = new ArrayList<INaviInstruction>();
    instructions.add(new MockInstruction(0x123));
    final INaviBasicBlock bblock = new CBasicBlock(1, "Hannes", instructions);
    final IBlockNode node = new CBlockNode(bblock);
    final Function pFunction = new Function(ModuleFactory.get(), internalFunction);
    final BasicBlock block = new BasicBlock(node, pFunction);
    assertEquals(0x123, block.getAddress().toLong());
    assertEquals(pFunction, block.getParentFunction());
    assertEquals("Hannes", block.getComment());
    assertEquals(0x123, block.getInstructions().get(0).getAddress().toLong());
    assertNotNull(block.getReilCode());
    assertTrue(block.getChildren().isEmpty());
    assertTrue(block.getParents().isEmpty());
    assertEquals("123  nop \n", block.toString());
}
Also used : CBlockNode(com.google.security.zynamics.binnavi.disassembly.CBlockNode) MockView(com.google.security.zynamics.binnavi.disassembly.MockView) IBlockNode(com.google.security.zynamics.binnavi.disassembly.IBlockNode) ArrayList(java.util.ArrayList) INaviBasicBlock(com.google.security.zynamics.binnavi.disassembly.INaviBasicBlock) CBasicBlock(com.google.security.zynamics.binnavi.disassembly.CBasicBlock) CFunction(com.google.security.zynamics.binnavi.disassembly.CFunction) Date(java.util.Date) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress) CBasicBlock(com.google.security.zynamics.binnavi.disassembly.CBasicBlock) CFunction(com.google.security.zynamics.binnavi.disassembly.CFunction) MockInstruction(com.google.security.zynamics.binnavi.disassembly.MockInstruction) MockSqlProvider(com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider) CModule(com.google.security.zynamics.binnavi.disassembly.Modules.CModule) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction) INaviBasicBlock(com.google.security.zynamics.binnavi.disassembly.INaviBasicBlock) Test(org.junit.Test)

Aggregations

CModule (com.google.security.zynamics.binnavi.disassembly.Modules.CModule)57 CAddress (com.google.security.zynamics.zylib.disassembly.CAddress)47 Date (java.util.Date)46 MockSqlProvider (com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider)40 Test (org.junit.Test)36 MockDatabase (com.google.security.zynamics.binnavi.Database.MockClasses.MockDatabase)23 CFunction (com.google.security.zynamics.binnavi.disassembly.CFunction)23 MockView (com.google.security.zynamics.binnavi.disassembly.MockView)22 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)19 DebuggerTemplate (com.google.security.zynamics.binnavi.debug.debugger.DebuggerTemplate)16 MockTagManager (com.google.security.zynamics.binnavi.Tagging.MockTagManager)14 Before (org.junit.Before)14 ArrayList (java.util.ArrayList)12 CTagManager (com.google.security.zynamics.binnavi.Tagging.CTagManager)11 CInstruction (com.google.security.zynamics.binnavi.disassembly.CInstruction)9 CComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment)8 CView (com.google.security.zynamics.binnavi.disassembly.views.CView)8 SQLProvider (com.google.security.zynamics.binnavi.Database.Interfaces.SQLProvider)7 MockInstruction (com.google.security.zynamics.binnavi.disassembly.MockInstruction)7 COperandTree (com.google.security.zynamics.binnavi.disassembly.COperandTree)6