Search in sources :

Example 6 with COperandTree

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

the class PostgreSQLProviderTest method testInstructionFunctionsAddReference1.

@Test
public void testInstructionFunctionsAddReference1() throws CouldntSaveDataException, CouldntLoadDataException, LoadCancelledException {
    final INaviModule module = getProvider().loadModules().get(1);
    module.load();
    final INaviFunction function = module.getContent().getFunctionContainer().getFunctions().get(1800);
    function.load();
    final IBlockNode basicBlock = function.getBasicBlocks().get(0);
    final INaviInstruction instruction = Iterables.get(basicBlock.getInstructions(), 1);
    final COperandTree tree = instruction.getOperands().get(0);
    final INaviOperandTreeNode node = tree.getRootNode();
    final IAddress address = instruction.getAddress();
    final ReferenceType type = ReferenceType.DATA;
    final int references = node.getReferences().size();
    PostgreSQLInstructionFunctions.addReference(getProvider(), node, address, type);
    final INaviModule module2 = getProvider().loadModules().get(1);
    module2.load();
    final INaviFunction function2 = module2.getContent().getFunctionContainer().getFunctions().get(1800);
    function2.load();
    final IBlockNode basicBlock2 = function2.getBasicBlocks().get(0);
    final INaviInstruction instruction2 = Iterables.get(basicBlock2.getInstructions(), 1);
    final COperandTree tree2 = instruction2.getOperands().get(0);
    final INaviOperandTreeNode node2 = tree2.getRootNode();
    assertEquals(references + 1, node2.getReferences().size());
}
Also used : INaviOperandTreeNode(com.google.security.zynamics.binnavi.disassembly.INaviOperandTreeNode) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) IBlockNode(com.google.security.zynamics.binnavi.disassembly.IBlockNode) COperandTree(com.google.security.zynamics.binnavi.disassembly.COperandTree) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) ReferenceType(com.google.security.zynamics.zylib.disassembly.ReferenceType) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction) ExpensiveBaseTest(com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest) Test(org.junit.Test)

Example 7 with COperandTree

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

the class PostgreSQLProviderTest method testSave.

@Test
public void testSave() throws CouldntSaveDataException, CouldntLoadDataException, CouldntDeleteException, CPartialLoadException, InternalTranslationException, LoadCancelledException, MaybeNullException {
    final CTagManager tagManager = getProvider().loadTagManager(TagType.NODE_TAG);
    tagManager.addTag(tagManager.getRootTag(), "Node Tag I");
    tagManager.addTag(tagManager.getRootTag(), "Node Tag II");
    final ITreeNode<CTag> tag1 = tagManager.getRootTag().getChildren().get(0);
    final ITreeNode<CTag> tag2 = tagManager.getRootTag().getChildren().get(1);
    final INaviModule module = getProvider().loadModules().get(0);
    module.load();
    final CView view = module.getContent().getViewContainer().createView("Save View", "Save View Description");
    final INaviFunction function = module.getContent().getFunctionContainer().getFunction("sub_1002B87");
    function.load();
    final List<COperandTree> operands = new ArrayList<COperandTree>();
    final COperandTreeNode root1 = module.createOperandExpression("dword", ExpressionType.SIZE_PREFIX);
    final COperandTreeNode child1 = module.createOperandExpression("eax", ExpressionType.REGISTER);
    COperandTreeNode.link(root1, child1);
    final COperandTreeNode root2 = module.createOperandExpression("dword", ExpressionType.SIZE_PREFIX);
    final COperandTreeNode child2 = module.createOperandExpression("16", ExpressionType.IMMEDIATE_INTEGER);
    COperandTreeNode.link(root2, child2);
    final COperandTree operand1 = module.createOperand(root1);
    final COperandTree operand2 = module.createOperand(root2);
    operands.add(operand1);
    operands.add(operand2);
    final Iterable<INaviInstruction> instructions = function.getBasicBlocks().get(0).getInstructions();
    final Iterable<INaviInstruction> instructions2 = function.getBasicBlocks().get(1).getInstructions();
    final CCodeNode codeNode = view.getContent().createCodeNode(function, Lists.newArrayList(instructions));
    codeNode.tagNode(tag1.getObject());
    codeNode.getComments().appendLocalCodeNodeComment("XXX");
    codeNode.getComments().appendLocalInstructionComment(Iterables.getLast(codeNode.getInstructions()), "YYY");
    Iterables.getLast(codeNode.getInstructions()).appendGlobalComment(" GLOBAL INSTRUCTION COMMENT ");
    @SuppressWarnings("unused") final CCodeNode codeNode2 = view.getContent().createCodeNode(null, Lists.newArrayList(instructions2));
    final CFunctionNode functionNode = view.getContent().createFunctionNode(function);
    functionNode.tagNode(tag2.getObject());
    functionNode.appendLocalFunctionComment("ZZZ");
    @SuppressWarnings("unused") final CNaviViewEdge edge = view.getContent().createEdge(codeNode, functionNode, EdgeType.JUMP_UNCONDITIONAL);
    view.save();
    view.close();
    view.load();
    assertEquals(3, view.getGraph().getNodes().size());
    assertEquals(1, view.getGraph().getEdges().size());
    assertTrue(view.getGraph().getNodes().get(0).isTagged(tag1.getObject()));
    assertTrue(view.getGraph().getNodes().get(2).isTagged(tag2.getObject()));
    final CCodeNode loadedCodeNode = (CCodeNode) view.getGraph().getNodes().get(0);
    final CCodeNode loadedCodeNode2 = (CCodeNode) view.getGraph().getNodes().get(1);
    assertEquals("XXX", loadedCodeNode.getComments().getLocalCodeNodeComment().get(0).getComment());
    final INaviInstruction customInstruction = Iterables.getLast(loadedCodeNode.getInstructions());
    assertEquals(" GLOBAL INSTRUCTION COMMENT ", customInstruction.getGlobalComment().get(0).getComment());
    assertEquals("YYY", loadedCodeNode.getComments().getLocalInstructionComment(customInstruction).get(0).getComment());
    final ReilTranslator<INaviInstruction> translator = new ReilTranslator<INaviInstruction>();
    translator.translate(new StandardEnvironment(), loadedCodeNode);
    translator.translate(new StandardEnvironment(), loadedCodeNode2);
    final CFunctionNode loadedFunctionNode = (CFunctionNode) view.getGraph().getNodes().get(2);
    assertEquals("ZZZ", loadedFunctionNode.getLocalFunctionComment().get(0).getComment());
    tagManager.deleteTag(tag1);
    tagManager.deleteTag(tag2);
}
Also used : CFunctionNode(com.google.security.zynamics.binnavi.disassembly.CFunctionNode) COperandTreeNode(com.google.security.zynamics.binnavi.disassembly.COperandTreeNode) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) ArrayList(java.util.ArrayList) CTagManager(com.google.security.zynamics.binnavi.Tagging.CTagManager) CView(com.google.security.zynamics.binnavi.disassembly.views.CView) CNaviViewEdge(com.google.security.zynamics.binnavi.disassembly.CNaviViewEdge) ReilTranslator(com.google.security.zynamics.reil.translators.ReilTranslator) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) CCodeNode(com.google.security.zynamics.binnavi.disassembly.CCodeNode) COperandTree(com.google.security.zynamics.binnavi.disassembly.COperandTree) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction) StandardEnvironment(com.google.security.zynamics.reil.translators.StandardEnvironment) ExpensiveBaseTest(com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest) Test(org.junit.Test)

Example 8 with COperandTree

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

the class PostgreSQLProviderTest method testInstructionFunctionsAddReference6.

@Test(expected = CouldntSaveDataException.class)
public void testInstructionFunctionsAddReference6() throws CouldntSaveDataException, CouldntLoadDataException, LoadCancelledException {
    final INaviModule module = getProvider().loadModules().get(1);
    module.load();
    final INaviFunction function = module.getContent().getFunctionContainer().getFunctions().get(1800);
    function.load();
    final IBlockNode basicBlock = function.getBasicBlocks().get(0);
    final INaviInstruction instruction = Iterables.get(basicBlock.getInstructions(), 1);
    final COperandTree tree = instruction.getOperands().get(0);
    final INaviOperandTreeNode node = tree.getRootNode();
    final IAddress address = instruction.getAddress();
    final ReferenceType type = ReferenceType.DATA;
    PostgreSQLInstructionFunctions.addReference(getProvider(), node, address, type);
}
Also used : INaviOperandTreeNode(com.google.security.zynamics.binnavi.disassembly.INaviOperandTreeNode) INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) IBlockNode(com.google.security.zynamics.binnavi.disassembly.IBlockNode) COperandTree(com.google.security.zynamics.binnavi.disassembly.COperandTree) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) ReferenceType(com.google.security.zynamics.zylib.disassembly.ReferenceType) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction) ExpensiveBaseTest(com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest) Test(org.junit.Test)

Example 9 with COperandTree

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

the class PostgreSQLProviderTest method testInstructionFunctionsCreateInstruction1.

@Test
public void testInstructionFunctionsCreateInstruction1() throws SQLException, CouldntLoadDataException, LoadCancelledException {
    final INaviModule module = getProvider().loadModules().get(0);
    module.load();
    final String mnemonic = "burzel";
    final IAddress iAddress = new CAddress(0x1234);
    final COperandTreeNode rootNode = module.createOperandExpression("eax", ExpressionType.REGISTER);
    final COperandTree tree = new COperandTree(rootNode, getProvider(), module.getTypeManager(), module.getContent().getTypeInstanceContainer());
    final List<COperandTree> operands = new ArrayList<COperandTree>();
    operands.add(tree);
    final byte[] data = { 0xF };
    final String architecture = "x86-32";
    final INaviInstruction instruction = module.createInstruction(iAddress, mnemonic, operands, data, architecture);
    PostgreSQLInstructionFunctions.createInstructions(getProvider(), Lists.newArrayList(instruction));
}
Also used : INaviModule(com.google.security.zynamics.binnavi.disassembly.INaviModule) COperandTreeNode(com.google.security.zynamics.binnavi.disassembly.COperandTreeNode) COperandTree(com.google.security.zynamics.binnavi.disassembly.COperandTree) ArrayList(java.util.ArrayList) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction) ExpensiveBaseTest(com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest) Test(org.junit.Test)

Example 10 with COperandTree

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

the class InstructionConverter method generateTree.

/**
   * Converts a raw operand tree into a proper operand tree.
   *
   * @param rawTree The raw operand tree.
   * @param provider The connection to the database.
   * @param module
   *
   * @return The proper operand tree.
   */
private static COperandTree generateTree(final OperandTree rawTree, final SQLProvider provider, final INaviModule module) {
    final ArrayList<COperandTreeNode> realNodes = new ArrayList<COperandTreeNode>();
    final HashMap<COperandTreeNode, OperandTreeNode> realToRawMapping = new HashMap<COperandTreeNode, OperandTreeNode>();
    final HashMap<Integer, COperandTreeNode> idToRealMapping = new HashMap<Integer, COperandTreeNode>();
    COperandTreeNode root = null;
    final TypeManager typeManager = module.getTypeManager();
    final TypeInstanceContainer instanceContainer = module.getContent().getTypeInstanceContainer();
    for (final OperandTreeNode rawNode : rawTree.getNodes()) {
        final COperandTreeNode node = new COperandTreeNode(rawNode.getId(), rawNode.getType(), rawNode.getValue(), rawNode.getReplacement(), rawNode.getReferences(), provider, typeManager, instanceContainer);
        if (rawNode.getTypeSubstitution() != null) {
            typeManager.initializeTypeSubstitution(node, rawNode.getTypeSubstitution());
        }
        if (rawNode.getTypeInstanceId() != null) {
            instanceContainer.initializeTypeInstanceReference(rawNode.getAddress(), rawNode.getPosition(), rawNode.getId(), node);
        }
        realToRawMapping.put(node, rawNode);
        idToRealMapping.put(rawNode.getId(), node);
        if (rawNode.getParentId() == null) {
            root = node;
        }
        realNodes.add(node);
    }
    for (final COperandTreeNode realNode : realNodes) {
        // Link the real nodes here.
        // To link two real nodes, it is necessary to know
        // which node is the parent and which node is the
        // child.
        final OperandTreeNode rawNode = realToRawMapping.get(realNode);
        final Integer parentId = rawNode.getParentId();
        if (parentId == null) {
            continue;
        }
        final COperandTreeNode realParent = idToRealMapping.get(parentId);
        COperandTreeNode.link(realParent, realNode);
    }
    return new COperandTree(root, provider, typeManager, instanceContainer);
}
Also used : HashMap(java.util.HashMap) COperandTreeNode(com.google.security.zynamics.binnavi.disassembly.COperandTreeNode) COperandTree(com.google.security.zynamics.binnavi.disassembly.COperandTree) ArrayList(java.util.ArrayList) TypeManager(com.google.security.zynamics.binnavi.disassembly.types.TypeManager) COperandTreeNode(com.google.security.zynamics.binnavi.disassembly.COperandTreeNode) TypeInstanceContainer(com.google.security.zynamics.binnavi.disassembly.types.TypeInstanceContainer)

Aggregations

COperandTree (com.google.security.zynamics.binnavi.disassembly.COperandTree)23 INaviInstruction (com.google.security.zynamics.binnavi.disassembly.INaviInstruction)15 Test (org.junit.Test)14 COperandTreeNode (com.google.security.zynamics.binnavi.disassembly.COperandTreeNode)11 CAddress (com.google.security.zynamics.zylib.disassembly.CAddress)10 MockSqlProvider (com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider)9 INaviModule (com.google.security.zynamics.binnavi.disassembly.INaviModule)9 CInstruction (com.google.security.zynamics.binnavi.disassembly.CInstruction)8 INaviFunction (com.google.security.zynamics.binnavi.disassembly.INaviFunction)8 IReference (com.google.security.zynamics.zylib.disassembly.IReference)8 ArrayList (java.util.ArrayList)8 ExpensiveBaseTest (com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest)7 IAddress (com.google.security.zynamics.zylib.disassembly.IAddress)7 INaviOperandTreeNode (com.google.security.zynamics.binnavi.disassembly.INaviOperandTreeNode)6 CModule (com.google.security.zynamics.binnavi.disassembly.Modules.CModule)6 Date (java.util.Date)6 SQLProvider (com.google.security.zynamics.binnavi.Database.Interfaces.SQLProvider)5 CTag (com.google.security.zynamics.binnavi.Tagging.CTag)5 IBlockNode (com.google.security.zynamics.binnavi.disassembly.IBlockNode)5 INaviView (com.google.security.zynamics.binnavi.disassembly.views.INaviView)5