Search in sources :

Example 1 with StandardEnvironment

use of com.google.security.zynamics.reil.translators.StandardEnvironment in project binnavi by google.

the class CViewContent method getReilCode.

@Override
public ReilFunction getReilCode() throws InternalTranslationException {
    if (m_reilFunction == null) {
        final StandardEnvironment env = new StandardEnvironment();
        m_reilFunction = m_translator.translate(env, view);
    }
    return m_reilFunction;
}
Also used : StandardEnvironment(com.google.security.zynamics.reil.translators.StandardEnvironment)

Example 2 with StandardEnvironment

use of com.google.security.zynamics.reil.translators.StandardEnvironment in project binnavi by google.

the class ReilTranslator method translateInstruction.

// ! Translates an instruction to REIL code.
/**
 * Translates a single instruction to REIL code.
 *
 * @param architecture The source architecture of the instruction.
 * @param instruction The instruction to translate.
 *
 * @return The generated REIL code for that instruction.
 *
 * @throws InternalTranslationException Thrown if something goes wrong during translation.
 */
public static List<ReilInstruction> translateInstruction(final NativeArchitecture architecture, final Instruction instruction) throws InternalTranslationException {
    Preconditions.checkNotNull(architecture, "Error: Architecture argument can not be null");
    Preconditions.checkNotNull(instruction, "Error: Instruction argument can't be null");
    final List<ReilInstruction> instructions = new ArrayList<ReilInstruction>();
    final StandardEnvironment environment = new StandardEnvironment();
    try {
        for (final com.google.security.zynamics.reil.ReilInstruction reilInstruction : getTranslator(architecture).translate(environment, instruction.getNative(), new ArrayList<ITranslationExtension<INaviInstruction>>())) {
            instructions.add(new ReilInstruction(reilInstruction));
        }
        return instructions;
    } catch (final com.google.security.zynamics.reil.translators.InternalTranslationException e) {
        throw new InternalTranslationException(e, instruction);
    }
}
Also used : ArrayList(java.util.ArrayList) ITranslationExtension(com.google.security.zynamics.reil.translators.ITranslationExtension) StandardEnvironment(com.google.security.zynamics.reil.translators.StandardEnvironment)

Example 3 with StandardEnvironment

use of com.google.security.zynamics.reil.translators.StandardEnvironment in project binnavi by google.

the class COperandsDeterminer method getRegisters.

/**
 * Returns the registers read and written by a native instruction.
 *
 * @param instruction The instruction whose accessed registers are returned.
 *
 * @return The read and written registers of the instruction.
 *
 * @throws InternalTranslationException Thrown if the instruction could not be translated to REIL.
 */
public static Pair<Set<String>, Set<String>> getRegisters(final INaviInstruction instruction) throws InternalTranslationException {
    final Set<String> inSet = new HashSet<String>();
    final Set<String> outSet = new HashSet<String>();
    final ReilTranslator<INaviInstruction> translator = new ReilTranslator<INaviInstruction>();
    final DirectedGraph<ReilBlock, ReilEdge> reilCode = translator.translate(new StandardEnvironment(), instruction);
    final boolean translatingReil = instruction.getArchitecture().equals("REIL");
    for (final ReilBlock reilBlock : reilCode) {
        for (final ReilInstruction reilInstruction : reilBlock) {
            if (writesThirdOperand(reilInstruction, translatingReil)) {
                outSet.add(reilInstruction.getThirdOperand().getValue());
            }
            if (!writesThirdOperand(reilInstruction, translatingReil) && isRegister(reilInstruction.getThirdOperand(), translatingReil)) {
                // JCC + STM
                inSet.add(reilInstruction.getThirdOperand().getValue());
            }
            if (isRegister(reilInstruction.getFirstOperand(), translatingReil)) {
                inSet.add(reilInstruction.getFirstOperand().getValue());
            }
            if (isRegister(reilInstruction.getSecondOperand(), translatingReil)) {
                inSet.add(reilInstruction.getSecondOperand().getValue());
            }
        }
    }
    return new Pair<Set<String>, Set<String>>(inSet, outSet);
}
Also used : ReilInstruction(com.google.security.zynamics.reil.ReilInstruction) ReilEdge(com.google.security.zynamics.reil.ReilEdge) ReilBlock(com.google.security.zynamics.reil.ReilBlock) ReilTranslator(com.google.security.zynamics.reil.translators.ReilTranslator) HashSet(java.util.HashSet) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction) StandardEnvironment(com.google.security.zynamics.reil.translators.StandardEnvironment) Pair(com.google.security.zynamics.zylib.general.Pair)

Example 4 with StandardEnvironment

use of com.google.security.zynamics.reil.translators.StandardEnvironment in project binnavi by google.

the class CReilInstructionDialog method show.

/**
 * Shows an instruction dialog.
 *
 * @param parent Parent window used for dialogs.
 * @param instruction The instruction whose REIL code is shown.
 *
 * @throws InternalTranslationException Thrown if the instruction could not be converted to REIL
 *         code.
 */
public static void show(final Window parent, final INaviInstruction instruction) throws InternalTranslationException {
    final ReilTranslator<INaviInstruction> translator = new ReilTranslator<INaviInstruction>();
    final ReilGraph reilGraph = translator.translate(new StandardEnvironment(), instruction);
    final String text = reilGraphToText(reilGraph);
    final String title = String.format("REIL code of '%s'", instruction.toString());
    final CReilInstructionDialog dialog = new CReilInstructionDialog(parent, title, text);
    GuiHelper.centerChildToParent(parent, dialog, true);
    dialog.setVisible(true);
}
Also used : ReilGraph(com.google.security.zynamics.reil.ReilGraph) ReilTranslator(com.google.security.zynamics.reil.translators.ReilTranslator) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction) StandardEnvironment(com.google.security.zynamics.reil.translators.StandardEnvironment)

Example 5 with StandardEnvironment

use of com.google.security.zynamics.reil.translators.StandardEnvironment 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)

Aggregations

StandardEnvironment (com.google.security.zynamics.reil.translators.StandardEnvironment)16 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)7 INaviInstruction (com.google.security.zynamics.binnavi.disassembly.INaviInstruction)5 ReilTranslator (com.google.security.zynamics.reil.translators.ReilTranslator)5 INaviModule (com.google.security.zynamics.binnavi.disassembly.INaviModule)4 ReilGraph (com.google.security.zynamics.reil.ReilGraph)4 INaviFunction (com.google.security.zynamics.binnavi.disassembly.INaviFunction)3 ReilFunction (com.google.security.zynamics.reil.ReilFunction)3 ReilInstruction (com.google.security.zynamics.reil.ReilInstruction)3 MockCodeContainer (com.google.security.zynamics.zylib.disassembly.MockCodeContainer)3 MockInstruction (com.google.security.zynamics.zylib.disassembly.MockInstruction)3 HashSet (java.util.HashSet)3 CCodeNode (com.google.security.zynamics.binnavi.disassembly.CCodeNode)2 RegisterSetLatticeElement (com.google.security.zynamics.reil.algorithms.mono2.registertracking.RegisterSetLatticeElement)2 RegisterTrackingOptions (com.google.security.zynamics.reil.algorithms.mono2.registertracking.RegisterTrackingOptions)2 CAddress (com.google.security.zynamics.zylib.disassembly.CAddress)2 MockBlockContainer (com.google.security.zynamics.zylib.disassembly.MockBlockContainer)2 MockCodeNodeData (com.google.security.zynamics.binnavi.Database.MockClasses.MockCodeNodeData)1 MockCodeNodeProvider (com.google.security.zynamics.binnavi.Database.MockClasses.MockCodeNodeProvider)1