use of com.google.security.zynamics.reil.translators.TranslationResult in project binnavi by google.
the class AndTranslator method translate.
/**
* Translates an AND instruction to REIL code.
*
* @param environment A valid translation environment
* @param instruction The AND 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 AND instruction
*/
@Override
public void translate(final ITranslationEnvironment environment, final IInstruction instruction, final List<ReilInstruction> instructions) throws InternalTranslationException {
TranslationHelpers.checkTranslationArguments(environment, instruction, instructions, "and");
if (instruction.getOperands().size() != 2) {
throw new InternalTranslationException("Error: Argument instruction is not a and 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 andResult = Helpers.generateAnd(environment, offset, size, sourceRegister, targetRegister, instructions);
offset = baseOffset + instructions.size();
// Write the result of the ADD operation back into the target register
Helpers.writeBack(environment, offset, targetOperand, andResult, size, targetResult.getAddress(), targetResult.getType(), instructions);
Helpers.writeParityFlag(environment, baseOffset + instructions.size(), size, andResult, instructions);
}
use of com.google.security.zynamics.reil.translators.TranslationResult in project binnavi by google.
the class BtTranslator method translate.
/**
* Translates a BT instruction to REIL code.
*
* @param environment A valid translation environment
* @param instruction The BT 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 BT instruction
*/
@Override
public void translate(final ITranslationEnvironment environment, final IInstruction instruction, final List<ReilInstruction> instructions) throws InternalTranslationException {
TranslationHelpers.checkTranslationArguments(environment, instruction, instructions, "bt");
if (instruction.getOperands().size() != 2) {
throw new InternalTranslationException("Error: Argument instruction is not a bt instruction (invalid number of operands)");
}
final long baseOffset = instruction.getAddress().toLong() * 0x100;
long offset = baseOffset;
final IOperandTree targetOperand = instruction.getOperands().get(0);
final IOperandTree sourceOperand = instruction.getOperands().get(1);
// Load the target operand.
final TranslationResult targetResult = Helpers.translateOperand(environment, offset, targetOperand, true);
instructions.addAll(targetResult.getInstructions());
offset = baseOffset + instructions.size();
// Load the source operand.
final TranslationResult sourceResult = Helpers.translateOperand(environment, offset, sourceOperand, true);
instructions.addAll(sourceResult.getInstructions());
offset = baseOffset + instructions.size();
final String negatedIndex = environment.getNextVariableString();
// final String truncatedNegatedIndex = environment.getNextVariableString();
final String shiftedTarget = environment.getNextVariableString();
// TODO: Due to a bug in the REIL BSH specification we can not truncate the result
// of the subtraction here. See the tests for an example of what goes wrong.
instructions.add(ReilHelpers.createSub(offset++, OperandSize.BYTE, "0", sourceResult.getSize(), sourceResult.getRegister(), OperandSize.WORD, negatedIndex));
// instructions.add(ReilHelpers.createAnd(offset++, OperandSize.WORD, negatedIndex,
// OperandSize.BYTE, "255", OperandSize.BYTE, truncatedNegatedIndex));
instructions.add(ReilHelpers.createBsh(offset++, targetResult.getSize(), targetResult.getRegister(), OperandSize.WORD, negatedIndex, targetResult.getSize(), shiftedTarget));
instructions.add(ReilHelpers.createAnd(offset++, targetResult.getSize(), shiftedTarget, OperandSize.BYTE, "1", OperandSize.BYTE, Helpers.CARRY_FLAG));
}
use of com.google.security.zynamics.reil.translators.TranslationResult in project binnavi by google.
the class BtsTranslator method translate.
/**
* Translates a BTS instruction to REIL code.
*
* @param environment A valid translation environment
* @param instruction The BTR 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 BTS instruction
*/
@Override
public void translate(final ITranslationEnvironment environment, final IInstruction instruction, final List<ReilInstruction> instructions) throws InternalTranslationException {
TranslationHelpers.checkTranslationArguments(environment, instruction, instructions, "bts");
if (instruction.getOperands().size() != 2) {
throw new InternalTranslationException("Error: Argument instruction is not a bts instruction (invalid number of operands)");
}
final long baseOffset = instruction.getAddress().toLong() * 0x100;
long offset = baseOffset;
final IOperandTree targetOperand = instruction.getOperands().get(0);
final IOperandTree sourceOperand = instruction.getOperands().get(1);
// Load the target operand.
final TranslationResult targetResult = Helpers.translateOperand(environment, offset, targetOperand, true);
instructions.addAll(targetResult.getInstructions());
offset = baseOffset + instructions.size();
// Load the source operand.
final TranslationResult sourceResult = Helpers.translateOperand(environment, offset, sourceOperand, true);
instructions.addAll(sourceResult.getInstructions());
offset = baseOffset + instructions.size();
final String negatedIndex = environment.getNextVariableString();
// final String truncatedNegatedIndex = environment.getNextVariableString();
final String shiftedTarget = environment.getNextVariableString();
// TODO: Due to a bug in the REIL BSH specification we can not truncate the result
// of the subtraction here. See the tests for an example of what goes wrong.
instructions.add(ReilHelpers.createSub(offset++, OperandSize.BYTE, "0", sourceResult.getSize(), sourceResult.getRegister(), OperandSize.WORD, negatedIndex));
// instructions.add(ReilHelpers.createAnd(offset++, OperandSize.WORD, negatedIndex,
// OperandSize.BYTE, "255", OperandSize.BYTE, truncatedNegatedIndex));
instructions.add(ReilHelpers.createBsh(offset++, targetResult.getSize(), targetResult.getRegister(), OperandSize.BYTE, negatedIndex, targetResult.getSize(), shiftedTarget));
instructions.add(ReilHelpers.createAnd(offset++, targetResult.getSize(), shiftedTarget, OperandSize.BYTE, "1", OperandSize.BYTE, Helpers.CARRY_FLAG));
// Set the bit in the destination
final String shiftedIndex = environment.getNextVariableString();
final String andedResult = environment.getNextVariableString();
instructions.add(ReilHelpers.createBsh(offset++, OperandSize.BYTE, "1", sourceResult.getSize(), sourceResult.getRegister(), targetResult.getSize(), shiftedIndex));
instructions.add(ReilHelpers.createOr(offset++, targetResult.getSize(), targetResult.getRegister(), targetResult.getSize(), shiftedIndex, targetResult.getSize(), andedResult));
Helpers.writeBack(environment, offset++, targetOperand, andedResult, targetResult.getSize(), targetResult.getAddress(), targetResult.getType(), instructions);
}
use of com.google.security.zynamics.reil.translators.TranslationResult in project binnavi by google.
the class BsfBsrTranslatorCommon method translateBsfOrBsr.
public static void translateBsfOrBsr(final ITranslationEnvironment environment, final IInstruction instruction, final List<ReilInstruction> instructions, boolean translateBsf) throws InternalTranslationException {
if (instruction.getOperands().size() != 2) {
throw new InternalTranslationException("Error: Argument instruction is not a bsr/bsf instruction (invalid number of operands)");
}
final long baseOffset = instruction.getAddress().toLong() * 0x100;
long offset = baseOffset;
final IOperandTree targetOperand = instruction.getOperands().get(0);
final IOperandTree sourceOperand = instruction.getOperands().get(1);
// Load the source operand.
final TranslationResult sourceResult = Helpers.translateOperand(environment, offset, sourceOperand, true);
instructions.addAll(sourceResult.getInstructions());
offset = baseOffset + instructions.size();
final OperandSize sourceSize = sourceResult.getSize();
final String targetRegister = Helpers.getLeafValue(targetOperand.getRootNode());
final String labelNotZero = String.format("%d.%d", instruction.getAddress().toLong(), instructions.size() + 4);
final String labelLoopStart = String.format("%d.%d", instruction.getAddress().toLong(), instructions.size() + 7);
final String labelLoopEnd = String.format("%d.%d", instruction.getAddress().toLong(), instructions.size() + 12);
final String labelEnd = String.format("%d.%d", instruction.getAddress().toLong(), instructions.size() + 13);
instructions.add(ReilHelpers.createJcc(offset++, sourceSize, sourceResult.getRegister(), OperandSize.ADDRESS, labelNotZero));
// Input value is 0
instructions.add(ReilHelpers.createStr(offset++, OperandSize.BYTE, "1", OperandSize.BYTE, Helpers.ZERO_FLAG));
instructions.add(ReilHelpers.createUndef(offset++, environment.getArchitectureSize(), targetRegister));
instructions.add(ReilHelpers.createJcc(offset++, OperandSize.BYTE, "1", OperandSize.ADDRESS, labelEnd));
// Input value is not 0
final String counter = environment.getNextVariableString();
final String shiftedValue = environment.getNextVariableString();
final String isolatedMsb = environment.getNextVariableString();
instructions.add(ReilHelpers.createStr(offset++, OperandSize.BYTE, "0", OperandSize.BYTE, Helpers.ZERO_FLAG));
instructions.add(ReilHelpers.createStr(offset++, sourceSize, sourceResult.getRegister(), sourceSize, shiftedValue));
if (translateBsf) {
instructions.add(ReilHelpers.createStr(offset++, OperandSize.BYTE, "0", OperandSize.BYTE, counter));
instructions.add(ReilHelpers.createAnd(offset++, sourceSize, shiftedValue, sourceSize, "1", sourceSize, isolatedMsb));
} else {
instructions.add(ReilHelpers.createStr(offset++, OperandSize.BYTE, "31", OperandSize.BYTE, counter));
// Generate the instruction for a BSR, e.g. bitmask is 0x80000000.
instructions.add(ReilHelpers.createAnd(offset++, sourceSize, shiftedValue, sourceSize, String.valueOf(TranslationHelpers.getMsbMask(sourceSize)), sourceSize, isolatedMsb));
}
instructions.add(ReilHelpers.createJcc(offset++, sourceSize, isolatedMsb, OperandSize.ADDRESS, labelLoopEnd));
if (translateBsf) {
instructions.add(ReilHelpers.createAdd(offset++, OperandSize.BYTE, counter, OperandSize.BYTE, "1", OperandSize.BYTE, counter));
instructions.add(ReilHelpers.createBsh(offset++, sourceSize, shiftedValue, sourceSize, "-1", sourceSize, shiftedValue));
} else {
instructions.add(ReilHelpers.createSub(offset++, OperandSize.BYTE, counter, OperandSize.BYTE, "1", OperandSize.BYTE, counter));
instructions.add(ReilHelpers.createBsh(offset++, sourceSize, shiftedValue, sourceSize, "1", sourceSize, shiftedValue));
}
instructions.add(ReilHelpers.createJcc(offset++, OperandSize.BYTE, "1", OperandSize.ADDRESS, labelLoopStart));
instructions.add(ReilHelpers.createStr(offset++, OperandSize.DWORD, counter, OperandSize.DWORD, targetRegister));
instructions.add(ReilHelpers.createNop(offset++));
}
use of com.google.security.zynamics.reil.translators.TranslationResult in project binnavi by google.
the class BtcTranslator method translate.
/**
* Translates a BTC instruction to REIL code.
*
* @param environment A valid translation environment
* @param instruction The BTC 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 BTC instruction
*/
@Override
public void translate(final ITranslationEnvironment environment, final IInstruction instruction, final List<ReilInstruction> instructions) throws InternalTranslationException {
TranslationHelpers.checkTranslationArguments(environment, instruction, instructions, "btc");
if (instruction.getOperands().size() != 2) {
throw new InternalTranslationException("Error: Argument instruction is not a btc instruction (invalid number of operands)");
}
final long baseOffset = instruction.getAddress().toLong() * 0x100;
long offset = baseOffset;
final IOperandTree targetOperand = instruction.getOperands().get(0);
final IOperandTree sourceOperand = instruction.getOperands().get(1);
// Load the target operand.
final TranslationResult targetResult = Helpers.translateOperand(environment, offset, targetOperand, true);
instructions.addAll(targetResult.getInstructions());
offset = baseOffset + instructions.size();
// Load the source operand.
final TranslationResult sourceResult = Helpers.translateOperand(environment, offset, sourceOperand, true);
instructions.addAll(sourceResult.getInstructions());
offset = baseOffset + instructions.size();
final String negatedIndex = environment.getNextVariableString();
// final String truncatedNegatedIndex = environment.getNextVariableString();
final String shiftedTarget = environment.getNextVariableString();
// TODO: Due to a bug in the REIL BSH specification we can not truncate the result
// of the subtraction here. See the tests for an example of what goes wrong.
instructions.add(ReilHelpers.createSub(offset++, OperandSize.BYTE, "0", sourceResult.getSize(), sourceResult.getRegister(), OperandSize.WORD, negatedIndex));
// instructions.add(ReilHelpers.createAnd(offset++, OperandSize.WORD, negatedIndex,
// OperandSize.BYTE, "255", OperandSize.BYTE, truncatedNegatedIndex));
instructions.add(ReilHelpers.createBsh(offset++, targetResult.getSize(), targetResult.getRegister(), OperandSize.BYTE, negatedIndex, targetResult.getSize(), shiftedTarget));
instructions.add(ReilHelpers.createAnd(offset++, targetResult.getSize(), shiftedTarget, OperandSize.BYTE, "1", OperandSize.BYTE, Helpers.CARRY_FLAG));
// Set the bit in the destination
final String shiftedIndex = environment.getNextVariableString();
final String andedResult = environment.getNextVariableString();
instructions.add(ReilHelpers.createBsh(offset++, OperandSize.BYTE, "1", sourceResult.getSize(), sourceResult.getRegister(), targetResult.getSize(), shiftedIndex));
instructions.add(ReilHelpers.createXor(offset++, targetResult.getSize(), targetResult.getRegister(), targetResult.getSize(), shiftedIndex, targetResult.getSize(), andedResult));
Helpers.writeBack(environment, offset++, targetOperand, andedResult, targetResult.getSize(), targetResult.getAddress(), targetResult.getType(), instructions);
}
Aggregations