Search in sources :

Example 1 with Instruction

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

the class PathFinder method connectFunctions.

/**
   * Connects the functions in the view using inlining edges.
   *
   * @param view The view where the edges are created.
   * @param startNode The start node of the path.
   * @param targetNode The target node of the path.
   * @param passedFunctions All functions that lie on the path.
   * @param entryNodes Keeps track of the entry nodes of all functions.
   * @param exitNodes Keeps track of the exit nodes of all functions.
   * @param functionMap Keeps track to what function a node belongs to.
   *
   * @return Node pair that contains the updated start node and target node.
   */
private static NodePair connectFunctions(final View view, final ViewNode startNode, final ViewNode targetNode, final Collection<FunctionBlock> passedFunctions, final Map<Function, ViewNode> entryNodes, final ArrayListMultimap<Function, ViewNode> exitNodes, final Map<ViewNode, Function> functionMap) {
    ViewNode realStartNode = startNode;
    ViewNode realTargetNode = targetNode;
    final Set<ViewNode> handled = new HashSet<ViewNode>();
    while (true) {
        boolean splitNode = false;
        start: for (final ViewNode node : view.getGraph().getNodes()) {
            if (handled.contains(node)) {
                continue;
            }
            if (!(node instanceof CodeNode)) {
                continue;
            }
            final CodeNode cnode = (CodeNode) node;
            for (final Instruction instruction : cnode.getInstructions()) {
                for (final FunctionBlock functionBlock : passedFunctions) {
                    final Function function = functionBlock.getFunction();
                    if (callsFunction(instruction, function)) {
                        // A function call to a function on the path was found.
                        // At this point we have to split the code node after
                        // the function call.
                        final NodePair result = splitBlock(view, functionMap.get(cnode), cnode, instruction);
                        if (realStartNode == cnode) {
                            // Of course it is possible that the start node was split,
                            // therefore we have to update the start node to the upper
                            // part of the new node.
                            realStartNode = result.getFirst();
                        }
                        if (realTargetNode == cnode) {
                            // Of course it is possible that the target node was split,
                            // therefore we have to update the target node to the upper
                            // part of the new node.
                            realTargetNode = result.getFirst();
                        }
                        // too.
                        for (final FunctionBlock functionBlock2 : passedFunctions) {
                            final Function function2 = functionBlock2.getFunction();
                            if (entryNodes.get(function2) == cnode) {
                                // Update the entry nodes
                                entryNodes.put(function2, result.getFirst());
                            }
                            if (exitNodes.get(function2).contains(cnode)) {
                                // Update the exit nodes
                                if (result.getSecond() != null) {
                                    exitNodes.remove(function2, cnode);
                                    exitNodes.put(function2, result.getSecond());
                                }
                            }
                        }
                        if (functionMap.containsKey(cnode)) {
                            final Function f = functionMap.get(cnode);
                            functionMap.remove(cnode);
                            functionMap.put(result.getFirst(), f);
                        }
                        handled.add(result.getFirst());
                        if (result.getSecond() == null) {
                            for (final ViewEdge edge : node.getOutgoingEdges()) {
                                for (final ViewNode currentExitNode : exitNodes.get(function)) {
                                    final ViewEdge leaveEdge = view.createEdge(currentExitNode, edge.getTarget(), EdgeType.LeaveInlinedFunction);
                                    leaveEdge.setColor(DEFAULT_INLINING_EDGE_COLOR);
                                }
                                view.deleteEdge(edge);
                            }
                            final ViewEdge enterEdge = view.createEdge(result.getFirst(), entryNodes.get(function), EdgeType.EnterInlinedFunction);
                            enterEdge.setColor(DEFAULT_INLINING_EDGE_COLOR);
                            handled.add(cnode);
                        } else {
                            // The node was split. We simply have to connect both split parts to the
                            // called function.
                            final ViewEdge enterEdge = view.createEdge(result.getFirst(), entryNodes.get(function), EdgeType.EnterInlinedFunction);
                            enterEdge.setColor(DEFAULT_INLINING_EDGE_COLOR);
                            for (final ViewNode currentExitNode : exitNodes.get(function)) {
                                final ViewEdge leaveEdge = view.createEdge(currentExitNode, result.getSecond(), EdgeType.LeaveInlinedFunction);
                                leaveEdge.setColor(DEFAULT_INLINING_EDGE_COLOR);
                            }
                        }
                        splitNode = true;
                        break start;
                    }
                }
            }
            handled.add(cnode);
        }
        if (!splitNode) {
            break;
        }
    }
    return new NodePair(realStartNode, realTargetNode);
}
Also used : Function(com.google.security.zynamics.binnavi.API.disassembly.Function) CodeNode(com.google.security.zynamics.binnavi.API.disassembly.CodeNode) ViewEdge(com.google.security.zynamics.binnavi.API.disassembly.ViewEdge) ViewNode(com.google.security.zynamics.binnavi.API.disassembly.ViewNode) Instruction(com.google.security.zynamics.binnavi.API.disassembly.Instruction) FunctionBlock(com.google.security.zynamics.binnavi.API.disassembly.FunctionBlock) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with Instruction

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

the class BreakpointHelpersTest method setUp.

@Before
public void setUp() throws DebugExceptionWrapper, CouldntLoadDataException, LoadCancelledException, FileReadException {
    ConfigManager.instance().read();
    final CDatabase database = new CDatabase("", "", "", "", "", "", "", false, false);
    final Database apiDatabase = new Database(database);
    final SQLProvider mockProvider = new MockSqlProvider();
    final ITreeNode<CTag> nodeRootNode = new TreeNode<CTag>(new CTag(0, "", "", TagType.NODE_TAG, mockProvider));
    final Tree<CTag> nodeTagTree = new Tree<CTag>(nodeRootNode);
    final TagManager nodeTagManager = new TagManager(new CTagManager(nodeTagTree, TagType.NODE_TAG, mockProvider));
    final ITreeNode<CTag> viewRootNode = new TreeNode<CTag>(new CTag(0, "", "", TagType.VIEW_TAG, mockProvider));
    final Tree<CTag> viewTagTree = new Tree<CTag>(viewRootNode);
    final TagManager viewTagManager = new TagManager(new CTagManager(viewTagTree, TagType.VIEW_TAG, mockProvider));
    m_module = new CModule(1, "", "", new Date(), new Date(), "00000000000000000000000000000000", "0000000000000000000000000000000000000000", 0, 0, new CAddress(0), new CAddress(0), null, null, Integer.MAX_VALUE, false, mockProvider);
    m_module.load();
    m_mockDebugger = new MockDebugger(m_moduleDebugSettings);
    m_mockDebugger.connect();
    m_debugger = new Debugger(m_mockDebugger);
    final INaviFunction parentFunction = m_module.getContent().getFunctionContainer().getFunctions().get(0);
    m_mockDebugger.setAddressTranslator(m_module, new CAddress(0), new CAddress(0x1000));
    final ViewContainer viewContainer = new Module(apiDatabase, m_module, nodeTagManager, viewTagManager);
    final INaviView naviView = new MockView(mockProvider);
    final Function apiFunction = new Function(ModuleFactory.get(), parentFunction);
    final COperandTreeNode rootNode1 = new COperandTreeNode(1, IOperandTree.NODE_TYPE_REGISTER_ID, "eax", null, new ArrayList<IReference>(), mockProvider, m_module.getTypeManager(), m_module.getContent().getTypeInstanceContainer());
    final COperandTreeNode rootNode2 = new COperandTreeNode(1, IOperandTree.NODE_TYPE_REGISTER_ID, "ebx", null, new ArrayList<IReference>(), mockProvider, m_module.getTypeManager(), m_module.getContent().getTypeInstanceContainer());
    final COperandTree operand1 = new COperandTree(rootNode1, mockProvider, m_module.getTypeManager(), m_module.getContent().getTypeInstanceContainer());
    final COperandTree operand2 = new COperandTree(rootNode2, mockProvider, m_module.getTypeManager(), m_module.getContent().getTypeInstanceContainer());
    final List<COperandTree> operands = Lists.newArrayList(operand1, operand2);
    final CInstruction internalInstruction = new CInstruction(true, m_module, new CAddress(0x1234), "mov", operands, new byte[] { 1, 2, 3 }, "x86-32", mockProvider);
    m_view = new View(viewContainer, naviView, nodeTagManager, viewTagManager);
    m_node = m_view.createCodeNode(apiFunction, Lists.newArrayList(new Instruction(internalInstruction)));
    setM_functionNode(m_view.createFunctionNode(apiFunction));
}
Also used : MockDebugger(com.google.security.zynamics.binnavi.Debug.Debugger.MockDebugger) COperandTreeNode(com.google.security.zynamics.binnavi.disassembly.COperandTreeNode) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) CInstruction(com.google.security.zynamics.binnavi.disassembly.CInstruction) Instruction(com.google.security.zynamics.binnavi.API.disassembly.Instruction) SQLProvider(com.google.security.zynamics.binnavi.Database.Interfaces.SQLProvider) ViewContainer(com.google.security.zynamics.binnavi.API.disassembly.ViewContainer) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction) Function(com.google.security.zynamics.binnavi.API.disassembly.Function) MockSqlProvider(com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider) ITreeNode(com.google.security.zynamics.zylib.types.trees.ITreeNode) COperandTreeNode(com.google.security.zynamics.binnavi.disassembly.COperandTreeNode) TreeNode(com.google.security.zynamics.zylib.types.trees.TreeNode) COperandTree(com.google.security.zynamics.binnavi.disassembly.COperandTree) CDatabase(com.google.security.zynamics.binnavi.Database.CDatabase) Database(com.google.security.zynamics.binnavi.API.disassembly.Database) IOperandTree(com.google.security.zynamics.zylib.disassembly.IOperandTree) COperandTree(com.google.security.zynamics.binnavi.disassembly.COperandTree) Tree(com.google.security.zynamics.zylib.types.trees.Tree) MockView(com.google.security.zynamics.binnavi.disassembly.MockView) CTagManager(com.google.security.zynamics.binnavi.Tagging.CTagManager) CInstruction(com.google.security.zynamics.binnavi.disassembly.CInstruction) INaviView(com.google.security.zynamics.binnavi.disassembly.views.INaviView) View(com.google.security.zynamics.binnavi.API.disassembly.View) MockView(com.google.security.zynamics.binnavi.disassembly.MockView) Date(java.util.Date) MockDebugger(com.google.security.zynamics.binnavi.Debug.Debugger.MockDebugger) CDatabase(com.google.security.zynamics.binnavi.Database.CDatabase) CTagManager(com.google.security.zynamics.binnavi.Tagging.CTagManager) TagManager(com.google.security.zynamics.binnavi.API.disassembly.TagManager) INaviView(com.google.security.zynamics.binnavi.disassembly.views.INaviView) Module(com.google.security.zynamics.binnavi.API.disassembly.Module) CModule(com.google.security.zynamics.binnavi.disassembly.Modules.CModule) IReference(com.google.security.zynamics.zylib.disassembly.IReference) CModule(com.google.security.zynamics.binnavi.disassembly.Modules.CModule) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction) Before(org.junit.Before)

Example 3 with Instruction

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

the class InstructionFinders method findInstruction.

/**
   * Searches for an instruction in a code node.
   *
   * @param codeNode The code node to search through.
   * @param searchInstruction The instruction to search for.
   *
   * @return The API instruction object that wraps the search instruction.
   */
public static Instruction findInstruction(final CodeNode codeNode, final IInstruction searchInstruction) {
    Preconditions.checkNotNull(codeNode, "IE02027: Code node argument can not be null");
    Preconditions.checkNotNull(searchInstruction, "IE02033: Instruction argument can not be null");
    for (final Instruction instruction : codeNode.getInstructions()) {
        if (instruction.getNative() == searchInstruction) {
            return instruction;
        }
    }
    throw new IllegalStateException("IE01273: Could not determine what instruction could not be translated");
}
Also used : Instruction(com.google.security.zynamics.binnavi.API.disassembly.Instruction) IInstruction(com.google.security.zynamics.zylib.disassembly.IInstruction)

Example 4 with Instruction

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

the class InstructionFinders method findInstruction.

/**
   * Searches for an instruction in a function.
   *
   * @param function The function to search through.
   * @param searchInstruction The instruction to search for.
   *
   * @return The API instruction object that wraps the search instruction.
   */
public static Instruction findInstruction(final Function function, final IInstruction searchInstruction) {
    Preconditions.checkNotNull(function, "IE02034: Function argument can not be null");
    Preconditions.checkNotNull(searchInstruction, "IE02052: Instruction argument can not be null");
    for (final BasicBlock block : function.getGraph().getNodes()) {
        for (final Instruction instruction : block.getInstructions()) {
            if (instruction.getNative() == searchInstruction) {
                return instruction;
            }
        }
    }
    throw new IllegalStateException("IE01274: Could not determine what instruction could not be translated");
}
Also used : BasicBlock(com.google.security.zynamics.binnavi.API.disassembly.BasicBlock) Instruction(com.google.security.zynamics.binnavi.API.disassembly.Instruction) IInstruction(com.google.security.zynamics.zylib.disassembly.IInstruction)

Example 5 with Instruction

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

the class BreakpointHelpers method getBreakpoints.

/**
   * Returns the addresses of a code node where breakpoints of a given type are set.
   *
   * @param debugger The debugger that set the breakpoints.
   * @param node The code node to check.
   * @param type The type of the breakpoints to search for.
   *
   * @return The addresses of the code node where breakpoints are set.
   */
private static List<Address> getBreakpoints(final Debugger debugger, final CodeNode node, final BreakpointType type) {
    Preconditions.checkNotNull(debugger, "Error: Debugger argument can not be null");
    Preconditions.checkNotNull(node, "Error: Node argument can not be null");
    final BreakpointManager manager = debugger.getBreakpointManager();
    final List<Address> breakpoints = new ArrayList<Address>();
    for (final Instruction instruction : node.getInstructions()) {
        final BreakpointAddress address = new BreakpointAddress(instruction.getNative().getModule(), new UnrelocatedAddress(new CAddress(instruction.getAddress().toLong())));
        if (manager.getNative().hasBreakpoint(type, address)) {
            breakpoints.add(new Address(address.getAddress().getAddress().toBigInteger()));
        }
    }
    return breakpoints;
}
Also used : CAddress(com.google.security.zynamics.zylib.disassembly.CAddress) BreakpointAddress(com.google.security.zynamics.binnavi.debug.models.breakpoints.BreakpointAddress) Address(com.google.security.zynamics.binnavi.API.disassembly.Address) UnrelocatedAddress(com.google.security.zynamics.binnavi.disassembly.UnrelocatedAddress) UnrelocatedAddress(com.google.security.zynamics.binnavi.disassembly.UnrelocatedAddress) ArrayList(java.util.ArrayList) Instruction(com.google.security.zynamics.binnavi.API.disassembly.Instruction) BreakpointAddress(com.google.security.zynamics.binnavi.debug.models.breakpoints.BreakpointAddress) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress)

Aggregations

Instruction (com.google.security.zynamics.binnavi.API.disassembly.Instruction)8 IInstruction (com.google.security.zynamics.zylib.disassembly.IInstruction)4 CodeNode (com.google.security.zynamics.binnavi.API.disassembly.CodeNode)3 Function (com.google.security.zynamics.binnavi.API.disassembly.Function)2 ViewEdge (com.google.security.zynamics.binnavi.API.disassembly.ViewEdge)2 ViewNode (com.google.security.zynamics.binnavi.API.disassembly.ViewNode)2 CAddress (com.google.security.zynamics.zylib.disassembly.CAddress)2 ArrayList (java.util.ArrayList)2 Address (com.google.security.zynamics.binnavi.API.disassembly.Address)1 BasicBlock (com.google.security.zynamics.binnavi.API.disassembly.BasicBlock)1 Database (com.google.security.zynamics.binnavi.API.disassembly.Database)1 FunctionBlock (com.google.security.zynamics.binnavi.API.disassembly.FunctionBlock)1 Module (com.google.security.zynamics.binnavi.API.disassembly.Module)1 TagManager (com.google.security.zynamics.binnavi.API.disassembly.TagManager)1 View (com.google.security.zynamics.binnavi.API.disassembly.View)1 ViewContainer (com.google.security.zynamics.binnavi.API.disassembly.ViewContainer)1 CDatabase (com.google.security.zynamics.binnavi.Database.CDatabase)1 SQLProvider (com.google.security.zynamics.binnavi.Database.Interfaces.SQLProvider)1 MockSqlProvider (com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider)1 MockDebugger (com.google.security.zynamics.binnavi.Debug.Debugger.MockDebugger)1