Search in sources :

Example 51 with TranslationResult

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

the class NegTranslator method translate.

/**
   * Translates an NEG instruction to REIL code.
   *
   * @param environment A valid translation environment.
   * @param instruction The NEG 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 NEG instruction
   */
@Override
public void translate(final ITranslationEnvironment environment, final IInstruction instruction, final List<ReilInstruction> instructions) throws InternalTranslationException {
    TranslationHelpers.checkTranslationArguments(environment, instruction, instructions, "neg");
    if (instruction.getOperands().size() != 1) {
        throw new InternalTranslationException("Error: Argument instruction is not an neg instruction (invalid number of operands)");
    }
    final long baseOffset = instruction.getAddress().toLong() * 0x100;
    long offset = baseOffset;
    // NEG instructions have exactly one operand.
    final IOperandTree operand = instruction.getOperands().get(0);
    // Load the operand.
    final TranslationResult result = Helpers.translateOperand(environment, offset, operand, true);
    instructions.addAll(result.getInstructions());
    // Adjust the offset of the next REIL instruction.
    offset = baseOffset + instructions.size();
    final String operandRegister = result.getRegister();
    final OperandSize size = result.getSize();
    final OperandSize resultSize = TranslationHelpers.getNextSize(size);
    final String msbMask = String.valueOf(TranslationHelpers.getMsbMask(size));
    final String truncateMask = String.valueOf(TranslationHelpers.getAllBitsMask(size));
    final String shiftValue = String.valueOf(TranslationHelpers.getShiftMsbLsbMask(size));
    final String targetIsZero = environment.getNextVariableString();
    final String msbTarget = environment.getNextVariableString();
    final String negResult = environment.getNextVariableString();
    final String msbResult = environment.getNextVariableString();
    final String tempOf = environment.getNextVariableString();
    final String truncatedResult = environment.getNextVariableString();
    // CF = ( original value == 0 ? 0 : 1 )
    instructions.add(ReilHelpers.createBisz(offset, size, operandRegister, OperandSize.BYTE, targetIsZero));
    instructions.add(ReilHelpers.createBisz(offset + 1, OperandSize.BYTE, targetIsZero, OperandSize.BYTE, Helpers.CARRY_FLAG));
    // Isolate the MSB of the original value
    instructions.add(ReilHelpers.createAnd(offset + 2, size, operandRegister, size, msbMask, size, msbTarget));
    // Negate the value
    instructions.add(ReilHelpers.createSub(offset + 3, size, "0", size, operandRegister, resultSize, negResult));
    // Isolate the MSB of the result and write it into SF
    instructions.add(ReilHelpers.createAnd(offset + 4, resultSize, negResult, size, msbMask, size, msbResult));
    instructions.add(ReilHelpers.createBsh(offset + 5, size, msbResult, size, shiftValue, OperandSize.BYTE, Helpers.SIGN_FLAG));
    // The OF is set is the original value was the lowest negative value of the target
    // Example: EAX => OF is set if value was 0x80000000
    // If that happens, the MSB of the operand and the result must both be set.
    instructions.add(ReilHelpers.createAnd(offset + 6, size, msbTarget, size, msbResult, size, tempOf));
    instructions.add(ReilHelpers.createBsh(offset + 7, size, tempOf, size, shiftValue, OperandSize.BYTE, Helpers.OVERFLOW_FLAG));
    // Make sure the result does not overflow
    instructions.add(ReilHelpers.createAnd(offset + 8, resultSize, negResult, size, truncateMask, size, truncatedResult));
    // Set the ZF according to the result
    instructions.add(ReilHelpers.createBisz(offset + 9, size, truncatedResult, OperandSize.BYTE, Helpers.ZERO_FLAG));
    // Write the truncated result back to the operand
    Helpers.writeBack(environment, offset + 10, operand, truncatedResult, size, result.getAddress(), result.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 52 with TranslationResult

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

the class OrTranslator method translate.

/**
   * Translates a OR instruction to REIL code.
   * 
   * @param environment A valid translation environment.
   * @param instruction The OR 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 OR instruction
   */
@Override
public void translate(final ITranslationEnvironment environment, final IInstruction instruction, final List<ReilInstruction> instructions) throws InternalTranslationException {
    TranslationHelpers.checkTranslationArguments(environment, instruction, instructions, "or");
    if (instruction.getOperands().size() != 2) {
        throw new InternalTranslationException("Error: Argument instruction is not a or 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 destination 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 size = targetResult.getSize();
    final String sourceRegister = sourceResult.getRegister();
    final String targetRegister = targetResult.getRegister();
    final String orResult = environment.getNextVariableString();
    // Do the OR operation
    instructions.add(ReilHelpers.createOr(offset, size, sourceRegister, size, targetRegister, size, orResult));
    // Set the flags according to the result of the OR operation
    Helpers.generateBinaryOperationFlags(environment, offset + 1, orResult, size, instructions);
    offset = baseOffset + instructions.size();
    // Write the result of the OR operation back into the target operand
    Helpers.writeBack(environment, offset, targetOperand, orResult, size, 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 53 with TranslationResult

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

the class PopTranslator method translate.

/**
   * Translates a POP instruction to REIL code.
   * 
   * @param environment A valid translation environment.
   * @param instruction The PUSH 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 POP instruction
   */
@Override
public void translate(final ITranslationEnvironment environment, final IInstruction instruction, final List<ReilInstruction> instructions) throws InternalTranslationException {
    if (instruction.getOperands().size() != 1) {
        throw new InternalTranslationException("Error: Argument instruction is not a pop instruction (invalid number of operands)");
    }
    final long baseOffset = instruction.getAddress().toLong() * 0x100;
    long offset = baseOffset;
    // POP instructions have exactly one operand
    final IOperandTree operand = instruction.getOperands().get(0);
    // Load the operand
    final TranslationResult result = Helpers.translateOperand(environment, offset, operand, false);
    final TranslationResultType resultType = result.getType();
    final OperandSize resultSize = result.getSize();
    instructions.addAll(result.getInstructions());
    // Adjust the offset of the next REIL instruction
    offset = baseOffset + instructions.size();
    // Load the value from the stack
    final String popResult = Helpers.generatePop(environment, offset, resultSize, null, instructions);
    // Adjust the offset of the next REIL instruction
    offset = baseOffset + instructions.size();
    // Write the loaded value into the target register
    Helpers.writeBack(environment, offset, operand, popResult, resultSize, result.getAddress(), resultType, instructions);
}
Also used : IOperandTree(com.google.security.zynamics.zylib.disassembly.IOperandTree) TranslationResultType(com.google.security.zynamics.reil.translators.TranslationResultType) InternalTranslationException(com.google.security.zynamics.reil.translators.InternalTranslationException) TranslationResult(com.google.security.zynamics.reil.translators.TranslationResult) OperandSize(com.google.security.zynamics.reil.OperandSize)

Example 54 with TranslationResult

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

the class MovzxTranslator method translate.

/**
   * Translates a MOVZX instruction to REIL code.
   * 
   * @param environment A valid translation environment.
   * @param instruction The MOVZX 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 MOVZX instruction
   */
@Override
public void translate(final ITranslationEnvironment environment, final IInstruction instruction, final List<ReilInstruction> instructions) throws InternalTranslationException {
    TranslationHelpers.checkTranslationArguments(environment, instruction, instructions, "movzx");
    if (instruction.getOperands().size() != 2) {
        throw new InternalTranslationException("Error: Argument instruction is not a movzx instruction (invalid number of operand)");
    }
    final long baseOffset = instruction.getAddress().toLong() * 0x100;
    long offset = baseOffset;
    final List<? extends IOperandTree> operands = instruction.getOperands();
    final IOperandTree destOperand = 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();
    final String sourceRegister = sourceResult.getRegister();
    // Load destination operand (must be a register).
    final String destRegister = Helpers.getLeafValue(destOperand.getRootNode());
    final OperandSize destSize = Helpers.getRegisterSize(destRegister);
    final OperandSize sourceSize = sourceResult.getSize();
    if (destSize == environment.getArchitectureSize()) {
        instructions.add(ReilHelpers.createOr(offset, destSize, "0", sourceSize, sourceRegister, destSize, destRegister));
    } else {
        Helpers.moveAndMask(environment, offset, sourceSize, sourceRegister, destRegister, 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 55 with TranslationResult

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

the class MovsxTranslator method translate.

/**
   * Translates a MOVSX instruction to REIL code.
   * 
   * @param environment A valid translation environment.
   * @param instruction The MOVSX 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 MOVSX instruction
   */
@Override
public void translate(final ITranslationEnvironment environment, final IInstruction instruction, final List<ReilInstruction> instructions) throws InternalTranslationException {
    TranslationHelpers.checkTranslationArguments(environment, instruction, instructions, "movsx");
    if (instruction.getOperands().size() != 2) {
        throw new InternalTranslationException("Error: Argument instruction is not a movsx instruction (invalid number of operands)");
    }
    final long baseOffset = instruction.getAddress().toLong() * 0x100;
    long offset = baseOffset;
    final List<? extends IOperandTree> operands = instruction.getOperands();
    // Load source operand.
    final TranslationResult sourceResult = Helpers.translateOperand(environment, offset, operands.get(1), true);
    instructions.addAll(sourceResult.getInstructions());
    // Adjust the offset of the next REIL instruction.
    offset = baseOffset + instructions.size();
    final String sourceOperand = sourceResult.getRegister();
    // Load destination operand (must be a register).
    final String destinationOperand = Helpers.getLeafValue(operands.get(0).getRootNode());
    final OperandSize destSize = Helpers.getRegisterSize(destinationOperand);
    final OperandSize sourceSize = sourceResult.getSize();
    final TranslationResult extendedSign = Helpers.extendSign(environment, offset, sourceOperand, sourceSize, destSize);
    instructions.addAll(extendedSign.getInstructions());
    // Adjust the offset of the next REIL instruction.
    offset = baseOffset + instructions.size();
    if (destSize == environment.getArchitectureSize()) {
        instructions.add(ReilHelpers.createStr(offset, destSize, extendedSign.getRegister(), destSize, destinationOperand));
    } else {
        Helpers.moveAndMask(environment, offset, extendedSign.getSize(), extendedSign.getRegister(), destinationOperand, instructions);
    }
}
Also used : InternalTranslationException(com.google.security.zynamics.reil.translators.InternalTranslationException) TranslationResult(com.google.security.zynamics.reil.translators.TranslationResult) OperandSize(com.google.security.zynamics.reil.OperandSize)

Aggregations

TranslationResult (com.google.security.zynamics.reil.translators.TranslationResult)55 OperandSize (com.google.security.zynamics.reil.OperandSize)45 InternalTranslationException (com.google.security.zynamics.reil.translators.InternalTranslationException)42 IOperandTree (com.google.security.zynamics.zylib.disassembly.IOperandTree)39 ReilInstruction (com.google.security.zynamics.reil.ReilInstruction)9 ArrayList (java.util.ArrayList)9 TranslationResultType (com.google.security.zynamics.reil.translators.TranslationResultType)3 OperandType (com.google.security.zynamics.reil.OperandType)2 ReilOperand (com.google.security.zynamics.reil.ReilOperand)1 ReilOperandNode (com.google.security.zynamics.reil.ReilOperandNode)1 IOperandTreeNode (com.google.security.zynamics.zylib.disassembly.IOperandTreeNode)1