Search in sources :

Example 31 with IAddress

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

the class ReilGraphGenerator method createGraphElements.

/**
 * Creates REIL basic blocks and edges from a list of REIL instructions.
 *
 * @param instructionList A list of REIL instructions.
 * @param nativeJumpTargets Additional jump targets for the algorithm to consider.
 *
 * @return A pair containing the blocks and edges created from the REIL instructions.
 */
public static Pair<List<ReilBlock>, List<ReilEdge>> createGraphElements(final Collection<List<ReilInstruction>> instructionList, final Collection<IAddress> nativeJumpTargets) {
    final BasicBlockGenerator generator = new BasicBlockGenerator(instructionList, nativeJumpTargets);
    final List<ReilBlock> blocks = generator.getBlocks();
    final ArrayList<Triple<ReilBlock, IAddress, EdgeType>> edgepairs = generator.getEdges();
    final List<ReilEdge> edges = new ArrayList<ReilEdge>();
    for (final Triple<ReilBlock, IAddress, EdgeType> p : edgepairs) {
        final ReilBlock source = p.first();
        final IAddress target = p.second();
        final EdgeType edgeType = p.third();
        if (target != null) {
            for (final ReilBlock block : blocks) {
                for (final ReilInstruction instruction : block.getInstructions()) {
                    if (target.equals(instruction.getAddress())) {
                        final ReilEdge edge = new ReilEdge(source, block, edgeType);
                        edges.add(edge);
                        ReilBlock.link(source, block, edge);
                    }
                }
            }
        } else {
            // Unknown target address
            final int index = blocks.indexOf(source);
            if (blocks.size() > (index + 1)) {
                final ReilEdge edge = new ReilEdge(source, blocks.get(index + 1), edgeType);
                edges.add(edge);
                ReilBlock.link(source, blocks.get(index + 1), edge);
            }
        }
    }
    return new Pair<List<ReilBlock>, List<ReilEdge>>(blocks, edges);
}
Also used : ReilInstruction(com.google.security.zynamics.reil.ReilInstruction) ReilEdge(com.google.security.zynamics.reil.ReilEdge) ReilBlock(com.google.security.zynamics.reil.ReilBlock) ArrayList(java.util.ArrayList) EdgeType(com.google.security.zynamics.zylib.gui.zygraph.edges.EdgeType) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) Triple(com.google.security.zynamics.zylib.general.Triple) Pair(com.google.security.zynamics.zylib.general.Pair)

Example 32 with IAddress

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

the class StateCombiner method combine.

private static ValueTrackerElement combine(final ValueTrackerElement state1, final ValueTrackerElement state2) {
    final Map<IAloc, IValueElement> values1 = state1.getStates();
    final Map<IAloc, IValueElement> values2 = state2.getStates();
    final Map<IAloc, IValueElement> combinedState = new HashMap<IAloc, IValueElement>();
    final Set<ReilInstruction> combinedInfluences = state1.getInfluences();
    combinedInfluences.addAll(state2.getInfluences());
    final Map<String, Set<IAddress>> combinedWritten = new HashMap<String, Set<IAddress>>();
    combinedWritten.putAll(state1.getLastWritten());
    for (final Map.Entry<String, Set<IAddress>> lastWritten : state2.getLastWritten().entrySet()) {
        if (combinedWritten.containsKey(lastWritten.getKey())) {
            combinedWritten.get(lastWritten.getKey()).addAll(lastWritten.getValue());
        } else {
            combinedWritten.put(lastWritten.getKey(), new HashSet<IAddress>(lastWritten.getValue()));
        }
    }
    for (final Map.Entry<IAloc, IValueElement> entry : values1.entrySet()) {
        final IAloc aloc = entry.getKey();
        if (values2.containsKey(aloc)) {
            final IValueElement lhs = entry.getValue();
            final IValueElement rhs = values2.get(aloc);
            combinedState.put(aloc, combine(lhs, rhs));
        } else {
            combinedState.put(aloc, new Undefined());
        }
    }
    for (final Map.Entry<IAloc, IValueElement> entry : values2.entrySet()) {
        final IAloc aloc = entry.getKey();
        if (!values1.containsKey(aloc)) {
            combinedState.put(aloc, new Undefined());
        }
    }
    if ((combinedState.size() < state1.getStates().size()) || (combinedState.size() < state2.getStates().size())) {
        throw new IllegalStateException();
    }
    return new ValueTrackerElement(combinedInfluences, combinedState, combinedWritten);
}
Also used : Undefined(com.google.security.zynamics.reil.algorithms.mono.valuetracking.elements.Undefined) ReilInstruction(com.google.security.zynamics.reil.ReilInstruction) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ValueTrackerElement(com.google.security.zynamics.reil.algorithms.mono.valuetracking.ValueTrackerElement) IAloc(com.google.security.zynamics.reil.algorithms.mono.valuetracking.elements.IAloc) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress) IValueElement(com.google.security.zynamics.reil.algorithms.mono.valuetracking.elements.IValueElement) HashMap(java.util.HashMap) Map(java.util.Map)

Example 33 with IAddress

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

the class BasicBlockGenerator method addInstruction.

private void addInstruction(final ReilInstruction reilInstruction, final HashSet<IAddress> jumpTargets, final ReilInstruction lastInstruction) {
    if (jumpTargets.contains(reilInstruction.getAddress()) && (currentBlock.size() != 0)) {
        final ReilBlock reilBlock = new ReilBlock(currentBlock);
        // final IAddress blockAddress = reilBlock.getAddress();
        blocks.add(reilBlock);
        // if ((reilBlock.getAddress().toLong() & 0xFFFFFFFFFFFFFF00L) ==
        // (reilInstruction.getAddress().toLong() & 0xFFFFFFFFFFFFFF00L))
        {
            edgepairs.add(new Triple<ReilBlock, IAddress, EdgeType>(reilBlock, reilInstruction.getAddress(), EdgeType.JUMP_UNCONDITIONAL));
        }
        currentBlock = new ArrayList<ReilInstruction>();
    }
    currentBlock.add(reilInstruction);
    if (reilInstruction.getMnemonic().equals(ReilHelpers.OPCODE_JCC) && (ReilHelpers.isDelayedBranch(reilInstruction) || (reilInstruction != lastInstruction))) {
        // Every JCC instruction finishes a block. We skip the last instruction of a block
        // because those edges already exist in the native edge set.
        // 
        // Delayed branches also finish a block, at least as far as edge creation goes.
        final ReilBlock reilBlock = new ReilBlock(currentBlock);
        blocks.add(reilBlock);
        currentBlock = new ArrayList<ReilInstruction>();
        final String jumpTarget = reilInstruction.getThirdOperand().getValue();
        if (ReilHelpers.isConditionalJump(reilInstruction)) {
            // If we have a conditional jump we have to add two edges.
            edgepairs.add(new Triple<ReilBlock, IAddress, EdgeType>(reilBlock, null, EdgeType.JUMP_CONDITIONAL_FALSE));
            if (Convert.isDecString(jumpTarget)) {
                edgepairs.add(new Triple<ReilBlock, IAddress, EdgeType>(reilBlock, toReilAddress(jumpTarget), EdgeType.JUMP_CONDITIONAL_TRUE));
            } else if (reilInstruction.getThirdOperand().getType() == OperandType.SUB_ADDRESS) {
                final String[] parts = jumpTarget.split("\\.");
                edgepairs.add(new Triple<ReilBlock, IAddress, EdgeType>(reilBlock, toReilAddress(parts), EdgeType.JUMP_CONDITIONAL_TRUE));
            }
        } else if (ReilHelpers.isFunctionCall(reilInstruction)) {
            edgepairs.add(new Triple<ReilBlock, IAddress, EdgeType>(reilBlock, null, EdgeType.JUMP_UNCONDITIONAL));
        } else if (Convert.isDecString(jumpTarget)) {
            edgepairs.add(new Triple<ReilBlock, IAddress, EdgeType>(reilBlock, toReilAddress(jumpTarget), EdgeType.JUMP_UNCONDITIONAL));
        } else if (reilInstruction.getThirdOperand().getType() == OperandType.SUB_ADDRESS) {
            final String[] parts = jumpTarget.split("\\.");
            edgepairs.add(new Triple<ReilBlock, IAddress, EdgeType>(reilBlock, toReilAddress(parts), EdgeType.JUMP_UNCONDITIONAL));
        }
    }
}
Also used : Triple(com.google.security.zynamics.zylib.general.Triple) ReilInstruction(com.google.security.zynamics.reil.ReilInstruction) ReilBlock(com.google.security.zynamics.reil.ReilBlock) EdgeType(com.google.security.zynamics.zylib.gui.zygraph.edges.EdgeType) IAddress(com.google.security.zynamics.zylib.disassembly.IAddress)

Example 34 with IAddress

use of com.google.security.zynamics.zylib.disassembly.IAddress 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 35 with IAddress

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

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