use of com.google.security.zynamics.reil.ReilInstruction in project binnavi by google.
the class TranslatorARM method translate.
/**
* Translates an ARM or THUMB instruction to REIL code
*
* @param environment A valid translation environment
* @param instruction The ARM or THUMB instruction to translate
*
* @return The list of REIL instruction the ARM instruction was translated to
*
* @throws InternalTranslationException An internal translation error occurred
* @throws IllegalArgumentException Any of the arguments passed to the function are invalid
*
*/
@Override
public List<ReilInstruction> translate(final ITranslationEnvironment environment, final InstructionType instruction, final List<ITranslationExtension<InstructionType>> extensions) throws InternalTranslationException {
Preconditions.checkNotNull(environment, "Error: Argument environment can't be null");
Preconditions.checkNotNull(instruction, "Error: Argument instruction can't be null");
final String mnemonic = instruction.getMnemonic();
final long instLength = instruction.getLength();
// TODO: >= 4 is a workaround because IDA merges multiple instructions into 1
final String normalizedMnemonic = instLength >= 4 ? mnemonic : "THUMB" + mnemonic;
final IInstructionTranslator translator = translators.get(normalizedMnemonic);
if (translators.containsKey(normalizedMnemonic)) {
final ArrayList<ReilInstruction> instructions = new ArrayList<ReilInstruction>();
translator.translate(environment, instruction, instructions);
return instructions;
} else if (mnemonic == null) {
return new ArrayList<ReilInstruction>();
} else {
return Lists.newArrayList(ReilHelpers.createUnknown(ReilHelpers.toReilAddress(instruction.getAddress()).toLong()));
}
}
use of com.google.security.zynamics.reil.ReilInstruction in project binnavi by google.
the class CDataflowViewCreator method create.
/**
* Creates a new dataflow view.
*
* @param container The container in which the dataflow view is created.
* @param view The normal view that provides the control-flow information.
*
* @return The created dataflow view.
*
* @throws InternalTranslationException Thrown if the input view could not be translated to REIL.
*/
public static INaviView create(final IViewContainer container, final INaviView view) throws InternalTranslationException {
Preconditions.checkNotNull(container, "IE00411: Module argument can not be null");
Preconditions.checkNotNull(view, "IE00414: View argument can not be null");
final Map<IAddress, INaviInstruction> instructions = new HashMap<IAddress, INaviInstruction>();
for (final CCodeNode codeNode : view.getBasicBlocks()) {
for (final INaviInstruction instruction : codeNode.getInstructions()) {
instructions.put(instruction.getAddress(), instruction);
}
}
final ReilFunction function = view.getContent().getReilCode();
final OperandGraph operandGraph = OperandGraph.create(function.getGraph());
final INaviView dfView = container.createView(String.format("Data flow view of '%s'", view.getName()), "");
final Map<OperandGraphNode, INaviCodeNode> nodeMap = new HashMap<OperandGraphNode, INaviCodeNode>();
final Map<INaviInstruction, CCodeNode> instructionMap = new HashMap<INaviInstruction, CCodeNode>();
for (final OperandGraphNode operandGraphNode : operandGraph) {
final ReilInstruction reilInstruction = operandGraphNode.getInstruction();
final INaviInstruction instruction = instructions.get(ReilHelpers.toNativeAddress(reilInstruction.getAddress()));
if (instructionMap.containsKey(instruction)) {
nodeMap.put(operandGraphNode, instructionMap.get(instruction));
continue;
}
final CCodeNode codeNode = dfView.getContent().createCodeNode(null, Lists.newArrayList(instruction));
codeNode.setColor(ConfigManager.instance().getColorSettings().getBasicBlocksColor());
nodeMap.put(operandGraphNode, codeNode);
instructionMap.put(instruction, codeNode);
}
for (final OperandGraphEdge edge : operandGraph.getEdges()) {
final INaviCodeNode source = nodeMap.get(edge.getSource());
final INaviCodeNode target = nodeMap.get(edge.getTarget());
if (source.equals(target)) {
continue;
}
dfView.getContent().createEdge(source, target, EdgeType.JUMP_UNCONDITIONAL);
}
return dfView;
}
use of com.google.security.zynamics.reil.ReilInstruction in project binnavi by google.
the class TranslatorMIPS method translate.
/**
* Translates a MIPS instruction to REIL code
*
* @param environment A valid translation environment
* @param instruction The MIPS instruction to translate
*
* @return The list of REIL instruction the MIPS instruction was translated to
*
* @throws InternalTranslationException An internal translation error occurred
* @throws IllegalArgumentException Any of the arguments passed to the function are invalid
*
*/
@Override
public List<ReilInstruction> translate(final ITranslationEnvironment environment, final InstructionType instruction, final List<ITranslationExtension<InstructionType>> extensions) throws InternalTranslationException {
Preconditions.checkNotNull(environment, "Error: Argument environment can't be null");
Preconditions.checkNotNull(instruction, "Error: Argument instruction can't be null");
final String mnemonic = instruction.getMnemonic();
// final long instLength = instruction.getLength();
if (mnemonic == null) {
return new ArrayList<ReilInstruction>();
}
final IInstructionTranslator translator = translators.get(mnemonic.toLowerCase());
if (translators.containsKey(mnemonic.toLowerCase())) {
final ArrayList<ReilInstruction> instructions = new ArrayList<ReilInstruction>();
translator.translate(environment, instruction, instructions);
for (final ITranslationExtension<InstructionType> extension : extensions) {
extension.postProcess(environment, instruction, instructions);
}
return instructions;
} else {
return Lists.newArrayList(ReilHelpers.createUnknown(ReilHelpers.toReilAddress(instruction.getAddress()).toLong()));
}
}
use of com.google.security.zynamics.reil.ReilInstruction 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);
}
use of com.google.security.zynamics.reil.ReilInstruction in project binnavi by google.
the class CReilViewCreator method create.
/**
* Creates a REIL view object from a REIL graph.
*
* @param container The container in which the new REIL view is created.
* @param graph The graph that contains the REIL code to be shown in the view.
*
* @return The created REIL code view.
*/
public static INaviView create(final INaviModule container, final ReilGraph graph) {
Preconditions.checkNotNull(container, "IE01809: Container argument can not be null");
Preconditions.checkNotNull(graph, "IE01815: Graph argument can not be null");
final INaviView view = container.getContent().getViewContainer().createView("REIL View", "");
final Map<ReilBlock, CCodeNode> nodeMap = new HashMap<ReilBlock, CCodeNode>();
for (final ReilBlock block : graph) {
final List<INaviInstruction> instructions = new ArrayList<INaviInstruction>();
for (final ReilInstruction reilInstruction : block) {
final List<COperandTree> operands = new ArrayList<COperandTree>();
if (reilInstruction.getFirstOperand().getType() == OperandType.EMPTY) {
operands.add(getEmptyOperand(container));
} else {
operands.add(convert(container, reilInstruction.getFirstOperand()));
}
if (reilInstruction.getSecondOperand().getType() == OperandType.EMPTY) {
operands.add(getEmptyOperand(container));
} else {
operands.add(convert(container, reilInstruction.getSecondOperand()));
}
if (reilInstruction.getThirdOperand().getType() == OperandType.EMPTY) {
operands.add(getEmptyOperand(container));
} else {
operands.add(convert(container, reilInstruction.getThirdOperand()));
}
final INaviInstruction convertedInstruction = container.createInstruction(reilInstruction.getAddress(), reilInstruction.getMnemonic(), operands, new byte[0], "REIL");
instructions.add(convertedInstruction);
}
final CCodeNode node = view.getContent().createCodeNode(null, instructions);
node.setColor(ConfigManager.instance().getColorSettings().getBasicBlocksColor());
nodeMap.put(block, node);
}
for (final ReilEdge edge : graph.getEdges()) {
final CNaviViewEdge reilEdge = view.getContent().createEdge(nodeMap.get(edge.getSource()), nodeMap.get(edge.getTarget()), edge.getType());
EdgeInitializer.adjustColor(reilEdge);
}
return view;
}
Aggregations