Search in sources :

Example 26 with InternalTranslationException

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

the class RcrTranslator method translate.

// TODO(timkornau): Check this code again
/**
   * Translates a RCR instruction to REIL code.
   *
   * @param environment A valid translation environment.
   * @param instruction The RCR instruction to translate.
   * @param instructions The generated REIL code will be added to this list
   *
   * @throws InternalTranslationException if any of the arguments are null the passed instruction is
   *         not an RCR instruction
   */
@Override
public void translate(final ITranslationEnvironment environment, final IInstruction instruction, final List<ReilInstruction> instructions) throws InternalTranslationException {
    TranslationHelpers.checkTranslationArguments(environment, instruction, instructions, "rcr");
    if (instruction.getOperands().size() != 2) {
        throw new InternalTranslationException("Error: Argument instruction is not a rcr instruction (invalid number of operands)");
    }
    final long baseOffset = instruction.getAddress().toLong() * 0x100;
    long offset = baseOffset;
    final List<? extends IOperandTree> operands = instruction.getOperands();
    final IOperandTree targetOperand = operands.get(0);
    final IOperandTree sourceOperand = operands.get(1);
    // Load source operand.
    final TranslationResult sourceResult = Helpers.translateOperand(environment, offset, sourceOperand, true);
    instructions.addAll(sourceResult.getInstructions());
    // Adjust the offset of the next REIL instruction.
    offset = baseOffset + instructions.size();
    // Load target operand.
    final TranslationResult targetResult = Helpers.translateOperand(environment, offset, targetOperand, true);
    instructions.addAll(targetResult.getInstructions());
    // Adjust the offset of the next REIL instruction.
    offset = baseOffset + instructions.size();
    final OperandSize sourceSize = sourceResult.getSize();
    final OperandSize targetSize = targetResult.getSize();
    final OperandSize resultSize = TranslationHelpers.getNextSize(sourceSize);
    final String sourceRegister = sourceResult.getRegister();
    final String targetRegister = targetResult.getRegister();
    final String rotateMask = environment.getNextVariableString();
    final String rotateMaskZero = environment.getNextVariableString();
    final String rotateMaskLessOne = environment.getNextVariableString();
    final String rotateMaskOne = environment.getNextVariableString();
    final String shiftedOp1 = environment.getNextVariableString();
    final String realOp1 = environment.getNextVariableString();
    final String shrValue = environment.getNextVariableString();
    final String shredResult = environment.getNextVariableString();
    final String shlValue = environment.getNextVariableString();
    final String shledResult = environment.getNextVariableString();
    final String result = environment.getNextVariableString();
    final String shiftedResult = environment.getNextVariableString();
    final String truncatedResult = environment.getNextVariableString();
    final String tempOf = environment.getNextVariableString();
    final String tempOfLsb = environment.getNextVariableString();
    final String msbMask = String.valueOf(TranslationHelpers.getMsbMask(sourceSize));
    final String maskSize = String.valueOf(TranslationHelpers.getAllBitsMask(sourceSize));
    final String modVal = String.valueOf(sourceSize.getBitSize());
    final String shiftMsbLsb = String.valueOf(TranslationHelpers.getShiftMsbLsbMask(sourceSize));
    // Make sure to rotate less than the size of the register
    instructions.add(ReilHelpers.createMod(offset, targetSize, targetRegister, targetSize, modVal, targetSize, rotateMask));
    // Find out if the rotate mask is 0 and negate the result
    instructions.add(ReilHelpers.createBisz(offset + 1, targetSize, rotateMask, OperandSize.BYTE, rotateMaskZero));
    // Find out if the rotate mask is 1
    instructions.add(ReilHelpers.createSub(offset + 2, targetSize, rotateMask, targetSize, "1", targetSize, rotateMaskLessOne));
    instructions.add(ReilHelpers.createBisz(offset + 3, targetSize, rotateMaskLessOne, OperandSize.BYTE, rotateMaskOne));
    // Rotating through the carry flag is like rotating through a 33 bit register
    // For rotating rightwards, the CF must be added at the LSB of the 32 register
    instructions.add(ReilHelpers.createBsh(offset + 4, sourceSize, sourceRegister, OperandSize.BYTE, "1", resultSize, shiftedOp1));
    instructions.add(ReilHelpers.createOr(offset + 5, resultSize, shiftedOp1, OperandSize.BYTE, Helpers.CARRY_FLAG, resultSize, realOp1));
    // Negate the rotate-mask => ROT to the right
    instructions.add(ReilHelpers.createSub(offset + 6, OperandSize.BYTE, "0", OperandSize.BYTE, rotateMask, OperandSize.BYTE, shrValue));
    // Perform the rotate
    instructions.add(ReilHelpers.createBsh(offset + 7, sourceSize, realOp1, OperandSize.BYTE, shrValue, sourceSize, shredResult));
    instructions.add(ReilHelpers.createSub(offset + 8, OperandSize.BYTE, modVal, OperandSize.BYTE, rotateMask, OperandSize.BYTE, shlValue));
    instructions.add(ReilHelpers.createBsh(offset + 9, sourceSize, realOp1, OperandSize.BYTE, shlValue, sourceSize, shledResult));
    instructions.add(ReilHelpers.createOr(offset + 10, sourceSize, shredResult, sourceSize, shledResult, sourceSize, result));
    // Truncate the result (get rid of the CF in the LSB)
    instructions.add(ReilHelpers.createBsh(offset + 11, resultSize, result, OperandSize.BYTE, "-1", resultSize, shiftedResult));
    instructions.add(ReilHelpers.createAnd(offset + 12, resultSize, shiftedResult, sourceSize, maskSize, sourceSize, truncatedResult));
    // Don't change the flags if the rotate value was zero
    final String jmpGoal = "666";
    instructions.add(ReilHelpers.createJcc(offset + 13, OperandSize.BYTE, rotateMaskZero, OperandSize.ADDRESS, jmpGoal));
    // Properly update OF if the rotate value == 1
    final String jmpGoal2 = "666";
    instructions.add(ReilHelpers.createJcc(offset + 14, OperandSize.BYTE, rotateMaskZero, OperandSize.ADDRESS, jmpGoal2));
    // Set the OF to undefined if the rotate-mask was positive but not 1
    instructions.add(ReilHelpers.createUndef(offset + 15, OperandSize.BYTE, Helpers.OVERFLOW_FLAG));
    // Update the CF now
    final String jmpGoal3 = "666";
    instructions.add(ReilHelpers.createJcc(offset + 15, OperandSize.BYTE, rotateMaskZero, OperandSize.ADDRESS, jmpGoal3));
    instructions.add(ReilHelpers.createAnd(offset + 16, sourceSize, sourceRegister, sourceSize, msbMask, sourceSize, tempOf));
    instructions.add(ReilHelpers.createBsh(offset + 17, sourceSize, tempOf, sourceSize, shiftMsbLsb, OperandSize.BYTE, tempOfLsb));
    instructions.add(ReilHelpers.createXor(offset + 18, OperandSize.BYTE, tempOfLsb, OperandSize.BYTE, Helpers.CARRY_FLAG, OperandSize.BYTE, Helpers.OVERFLOW_FLAG));
    // Set the CF to the LSB of the untruncated result
    instructions.add(ReilHelpers.createAnd(offset + 19, resultSize, result, OperandSize.BYTE, "1", OperandSize.BYTE, Helpers.CARRY_FLAG));
    Helpers.writeBack(environment, offset + 20, targetOperand, result, targetResult.getSize(), targetResult.getAddress(), targetResult.getType(), instructions);
}
Also used : IOperandTree(com.google.security.zynamics.zylib.disassembly.IOperandTree) InternalTranslationException(com.google.security.zynamics.reil.translators.InternalTranslationException) TranslationResult(com.google.security.zynamics.reil.translators.TranslationResult) OperandSize(com.google.security.zynamics.reil.OperandSize)

Example 27 with InternalTranslationException

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

the class RetnTranslator method translate.

/**
   * Translates a RETN instruction to REIL code.
   *
   * @param environment A valid translation environment.
   * @param instruction The RETN instruction to translate.
   * @param instructions The generated REIL code will be added to this list
   *
   * @throws InternalTranslationException if any of the arguments are null the passed instruction is
   *         not an RETN instruction
   */
@Override
public void translate(final ITranslationEnvironment environment, final IInstruction instruction, final List<ReilInstruction> instructions) throws InternalTranslationException {
    TranslationHelpers.checkTranslationArguments(environment, instruction, instructions, "retn");
    if (instruction.getOperands().size() > 1) {
        throw new InternalTranslationException("Error: Argument instruction is not a retn instruction (invalid number of operands)");
    }
    final long baseOffset = instruction.getAddress().toLong() * 0x100;
    final long offset = baseOffset;
    final OperandSize archSize = environment.getArchitectureSize();
    final OperandSize nextSize = TranslationHelpers.getNextSize(archSize);
    final String truncateMask = String.valueOf(TranslationHelpers.getAllBitsMask(archSize));
    final String returnAddress = environment.getNextVariableString();
    final String adjustedStack = environment.getNextVariableString();
    // Load the return address from the stack
    instructions.add(ReilHelpers.createLdm(offset, archSize, "esp", archSize, returnAddress));
    final List<? extends IOperandTree> operands = instruction.getOperands();
    // Find out how much the stack must be moved
    final String stackMovement = operands.size() == 0 ? String.valueOf(archSize.getByteSize()) : String.valueOf(archSize.getByteSize() + Long.valueOf(Helpers.getLeafValue(operands.get(0).getRootNode())));
    // Adjust the stack and truncate overflows
    instructions.add(ReilHelpers.createAdd(offset + 1, archSize, "esp", archSize, stackMovement, nextSize, adjustedStack));
    instructions.add(ReilHelpers.createAnd(offset + 2, nextSize, adjustedStack, nextSize, truncateMask, archSize, "esp"));
    // Return from the function.
    instructions.add(ReilHelpers.createJcc(offset + 3, archSize, "1", archSize, returnAddress));
}
Also used : InternalTranslationException(com.google.security.zynamics.reil.translators.InternalTranslationException) OperandSize(com.google.security.zynamics.reil.OperandSize)

Example 28 with InternalTranslationException

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

the class LoopeTranslator method translate.

/**
   * Translates a LOOPE instruction to REIL code.
   * 
   * @param environment A valid translation environment.
   * @param instruction The LOOPE instruction to translate.
   * @param instructions The generated REIL code will be added to this list
   * 
   * @throws InternalTranslationException if any of the arguments are null the passed instruction is
   *         not a LOOPE instruction
   */
@Override
public void translate(final ITranslationEnvironment environment, final IInstruction instruction, final List<ReilInstruction> instructions) throws InternalTranslationException {
    TranslationHelpers.checkTranslationArguments(environment, instruction, instructions, "loope");
    if (instruction.getOperands().size() != 1) {
        throw new InternalTranslationException("Error: Argument instruction is not a loope instruction (invalid number of operands)");
    }
    final long baseOffset = instruction.getAddress().toLong() * 0x100;
    final List<? extends IOperandTree> operands = instruction.getOperands();
    final OperandSize archSize = environment.getArchitectureSize();
    final OperandSize resultSize = TranslationHelpers.getNextSize(archSize);
    final String truncateMask = String.valueOf(TranslationHelpers.getAllBitsMask(archSize));
    final String loopTarget = Helpers.getLeafValue(operands.get(0).getRootNode());
    final String tempEcx = environment.getNextVariableString();
    final String ecxZero = environment.getNextVariableString();
    final String ecxNotZero = environment.getNextVariableString();
    final String condition = environment.getNextVariableString();
    // Decrement ECX and truncate overflows
    instructions.add(ReilHelpers.createSub(baseOffset, archSize, "ecx", archSize, "1", archSize, tempEcx));
    instructions.add(ReilHelpers.createAnd(baseOffset + 1, resultSize, tempEcx, archSize, truncateMask, archSize, "ecx"));
    // Check if ECX == 0
    instructions.add(ReilHelpers.createBisz(baseOffset + 2, archSize, "ecx", OperandSize.BYTE, ecxZero));
    // Check if ECX != 0
    instructions.add(ReilHelpers.createBisz(baseOffset + 3, OperandSize.BYTE, ecxZero, OperandSize.BYTE, ecxNotZero));
    // Check if ECX != 0 && ZF == 1
    instructions.add(ReilHelpers.createAnd(baseOffset + 4, OperandSize.BYTE, ecxNotZero, OperandSize.BYTE, Helpers.ZERO_FLAG, OperandSize.BYTE, condition));
    // Jump if ECX != 0 && ZF == 1
    instructions.add(ReilHelpers.createJcc(baseOffset + 5, OperandSize.BYTE, condition, archSize, loopTarget));
}
Also used : InternalTranslationException(com.google.security.zynamics.reil.translators.InternalTranslationException) OperandSize(com.google.security.zynamics.reil.OperandSize)

Example 29 with InternalTranslationException

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

the class CGraphFunctions method showDataflowGraph.

/**
   * Creates a new view that shows the data flow graph of a view.
   * 
   * @param parent Window where the new view is shown.
   * @param container Container where the new view is created.
   * @param view The view whose data flow graph is created.
   */
public static void showDataflowGraph(final CGraphWindow parent, final IViewContainer container, final INaviView view) {
    try {
        final INaviView dataflowView = CDataflowViewCreator.create(container, view);
        CViewOpener.showView(parent, container, dataflowView, parent);
    } catch (final InternalTranslationException e) {
        CUtilityFunctions.logException(e);
        final String innerMessage = "E00110: " + "Could not create dataflow graph";
        final String innerDescription = CUtilityFunctions.createDescription(String.format("BinNavi could not create the data flow graph of view '%s'.", view.getName()), new String[] { "An error occurred in the REIL translator code." }, new String[] { "This is an internal error which you can not fix yourself. " + "Please report the bug to the zynamics support team." });
        NaviErrorDialog.show(parent, innerMessage, innerDescription);
    }
}
Also used : INaviView(com.google.security.zynamics.binnavi.disassembly.views.INaviView) InternalTranslationException(com.google.security.zynamics.reil.translators.InternalTranslationException)

Example 30 with InternalTranslationException

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

the class AddressingModeFourGenerator method generate.

public static String generate(final long baseOffset, final ITranslationEnvironment environment, final IInstruction instruction, final List<ReilInstruction> instructions, final String typeValue, final String registerNodeValue, final String wBit, final IOperandTreeNode rootNodeOfRegisterList) throws InternalTranslationException {
    Preconditions.checkNotNull(environment, "Error: Argument environment can't be null");
    Preconditions.checkNotNull(instruction, "Error: Argument instruction can't be null");
    Preconditions.checkNotNull(instructions, "Error: Argument instructions can't be null");
    if (typeValue.equals("DA") || (typeValue.equals("FA") && instruction.getMnemonic().startsWith(matchLDM)) || (typeValue.equals("ED") && instruction.getMnemonic().startsWith(matchSTM))) {
        return generateDA(baseOffset, environment, instructions, registerNodeValue, wBit, rootNodeOfRegisterList);
    } else if (typeValue.equals("DB") || (typeValue.equals("EA") && instruction.getMnemonic().startsWith(matchLDM)) || (typeValue.equals("FD") && instruction.getMnemonic().startsWith(matchSTM))) {
        return generateDB(baseOffset, environment, instructions, registerNodeValue, wBit, rootNodeOfRegisterList);
    } else if (typeValue.equals("IA") || (typeValue.equals("FD") && instruction.getMnemonic().startsWith(matchLDM)) || (typeValue.equals("EA") && instruction.getMnemonic().startsWith(matchSTM))) {
        return generateIA(baseOffset, environment, instructions, registerNodeValue, wBit, rootNodeOfRegisterList);
    } else if (typeValue.equals("IB") || (typeValue.equals("ED") && instruction.getMnemonic().startsWith(matchLDM)) || (typeValue.equals("FA") && instruction.getMnemonic().startsWith(matchSTM))) {
        return generateIB(baseOffset, environment, instructions, registerNodeValue, wBit, rootNodeOfRegisterList);
    } else {
        throw new InternalTranslationException("ERROR: there is no such AddressingMode :" + typeValue);
    }
}
Also used : InternalTranslationException(com.google.security.zynamics.reil.translators.InternalTranslationException)

Aggregations

InternalTranslationException (com.google.security.zynamics.reil.translators.InternalTranslationException)62 OperandSize (com.google.security.zynamics.reil.OperandSize)46 TranslationResult (com.google.security.zynamics.reil.translators.TranslationResult)42 IOperandTree (com.google.security.zynamics.zylib.disassembly.IOperandTree)35 ReilInstruction (com.google.security.zynamics.reil.ReilInstruction)6 ArrayList (java.util.ArrayList)5 TranslationResultType (com.google.security.zynamics.reil.translators.TranslationResultType)3 INaviInstruction (com.google.security.zynamics.binnavi.disassembly.INaviInstruction)2 OperandType (com.google.security.zynamics.reil.OperandType)2 IOperandTreeNode (com.google.security.zynamics.zylib.disassembly.IOperandTreeNode)2 BigInteger (java.math.BigInteger)2 MockCodeNodeData (com.google.security.zynamics.binnavi.Database.MockClasses.MockCodeNodeData)1 MockCodeNodeProvider (com.google.security.zynamics.binnavi.Database.MockClasses.MockCodeNodeProvider)1 MockSqlProvider (com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider)1 CCodeNodeParser (com.google.security.zynamics.binnavi.Database.NodeParser.CCodeNodeParser)1 MaybeNullException (com.google.security.zynamics.binnavi.Exceptions.MaybeNullException)1 CCodeNode (com.google.security.zynamics.binnavi.disassembly.CCodeNode)1 INaviCodeNode (com.google.security.zynamics.binnavi.disassembly.INaviCodeNode)1 MockFunction (com.google.security.zynamics.binnavi.disassembly.MockFunction)1 MockModule (com.google.security.zynamics.binnavi.disassembly.Modules.MockModule)1