use of com.google.security.zynamics.zylib.disassembly.IAddress in project binnavi by google.
the class ReilGraphGenerator method createGraphElements.
/**
* Creates REIL basic blocks and edges from a list of REIL instructions.
*
* @param instructionList A list of REIL instructions.
* @param nativeJumpTargets Additional jump targets for the algorithm to consider.
*
* @return A pair containing the blocks and edges created from the REIL instructions.
*/
public static Pair<List<ReilBlock>, List<ReilEdge>> createGraphElements(final Collection<List<ReilInstruction>> instructionList, final Collection<IAddress> nativeJumpTargets) {
final BasicBlockGenerator generator = new BasicBlockGenerator(instructionList, nativeJumpTargets);
final List<ReilBlock> blocks = generator.getBlocks();
final ArrayList<Triple<ReilBlock, IAddress, EdgeType>> edgepairs = generator.getEdges();
final List<ReilEdge> edges = new ArrayList<ReilEdge>();
for (final Triple<ReilBlock, IAddress, EdgeType> p : edgepairs) {
final ReilBlock source = p.first();
final IAddress target = p.second();
final EdgeType edgeType = p.third();
if (target != null) {
for (final ReilBlock block : blocks) {
for (final ReilInstruction instruction : block.getInstructions()) {
if (target.equals(instruction.getAddress())) {
final ReilEdge edge = new ReilEdge(source, block, edgeType);
edges.add(edge);
ReilBlock.link(source, block, edge);
}
}
}
} else {
// Unknown target address
final int index = blocks.indexOf(source);
if (blocks.size() > (index + 1)) {
final ReilEdge edge = new ReilEdge(source, blocks.get(index + 1), edgeType);
edges.add(edge);
ReilBlock.link(source, blocks.get(index + 1), edge);
}
}
}
return new Pair<List<ReilBlock>, List<ReilEdge>>(blocks, edges);
}
use of com.google.security.zynamics.zylib.disassembly.IAddress in project binnavi by google.
the class StateCombiner method combine.
private static ValueTrackerElement combine(final ValueTrackerElement state1, final ValueTrackerElement state2) {
final Map<IAloc, IValueElement> values1 = state1.getStates();
final Map<IAloc, IValueElement> values2 = state2.getStates();
final Map<IAloc, IValueElement> combinedState = new HashMap<IAloc, IValueElement>();
final Set<ReilInstruction> combinedInfluences = state1.getInfluences();
combinedInfluences.addAll(state2.getInfluences());
final Map<String, Set<IAddress>> combinedWritten = new HashMap<String, Set<IAddress>>();
combinedWritten.putAll(state1.getLastWritten());
for (final Map.Entry<String, Set<IAddress>> lastWritten : state2.getLastWritten().entrySet()) {
if (combinedWritten.containsKey(lastWritten.getKey())) {
combinedWritten.get(lastWritten.getKey()).addAll(lastWritten.getValue());
} else {
combinedWritten.put(lastWritten.getKey(), new HashSet<IAddress>(lastWritten.getValue()));
}
}
for (final Map.Entry<IAloc, IValueElement> entry : values1.entrySet()) {
final IAloc aloc = entry.getKey();
if (values2.containsKey(aloc)) {
final IValueElement lhs = entry.getValue();
final IValueElement rhs = values2.get(aloc);
combinedState.put(aloc, combine(lhs, rhs));
} else {
combinedState.put(aloc, new Undefined());
}
}
for (final Map.Entry<IAloc, IValueElement> entry : values2.entrySet()) {
final IAloc aloc = entry.getKey();
if (!values1.containsKey(aloc)) {
combinedState.put(aloc, new Undefined());
}
}
if ((combinedState.size() < state1.getStates().size()) || (combinedState.size() < state2.getStates().size())) {
throw new IllegalStateException();
}
return new ValueTrackerElement(combinedInfluences, combinedState, combinedWritten);
}
use of com.google.security.zynamics.zylib.disassembly.IAddress in project binnavi by google.
the class BasicBlockGenerator method addInstruction.
private void addInstruction(final ReilInstruction reilInstruction, final HashSet<IAddress> jumpTargets, final ReilInstruction lastInstruction) {
if (jumpTargets.contains(reilInstruction.getAddress()) && (currentBlock.size() != 0)) {
final ReilBlock reilBlock = new ReilBlock(currentBlock);
// final IAddress blockAddress = reilBlock.getAddress();
blocks.add(reilBlock);
// if ((reilBlock.getAddress().toLong() & 0xFFFFFFFFFFFFFF00L) ==
// (reilInstruction.getAddress().toLong() & 0xFFFFFFFFFFFFFF00L))
{
edgepairs.add(new Triple<ReilBlock, IAddress, EdgeType>(reilBlock, reilInstruction.getAddress(), EdgeType.JUMP_UNCONDITIONAL));
}
currentBlock = new ArrayList<ReilInstruction>();
}
currentBlock.add(reilInstruction);
if (reilInstruction.getMnemonic().equals(ReilHelpers.OPCODE_JCC) && (ReilHelpers.isDelayedBranch(reilInstruction) || (reilInstruction != lastInstruction))) {
// Every JCC instruction finishes a block. We skip the last instruction of a block
// because those edges already exist in the native edge set.
//
// Delayed branches also finish a block, at least as far as edge creation goes.
final ReilBlock reilBlock = new ReilBlock(currentBlock);
blocks.add(reilBlock);
currentBlock = new ArrayList<ReilInstruction>();
final String jumpTarget = reilInstruction.getThirdOperand().getValue();
if (ReilHelpers.isConditionalJump(reilInstruction)) {
// If we have a conditional jump we have to add two edges.
edgepairs.add(new Triple<ReilBlock, IAddress, EdgeType>(reilBlock, null, EdgeType.JUMP_CONDITIONAL_FALSE));
if (Convert.isDecString(jumpTarget)) {
edgepairs.add(new Triple<ReilBlock, IAddress, EdgeType>(reilBlock, toReilAddress(jumpTarget), EdgeType.JUMP_CONDITIONAL_TRUE));
} else if (reilInstruction.getThirdOperand().getType() == OperandType.SUB_ADDRESS) {
final String[] parts = jumpTarget.split("\\.");
edgepairs.add(new Triple<ReilBlock, IAddress, EdgeType>(reilBlock, toReilAddress(parts), EdgeType.JUMP_CONDITIONAL_TRUE));
}
} else if (ReilHelpers.isFunctionCall(reilInstruction)) {
edgepairs.add(new Triple<ReilBlock, IAddress, EdgeType>(reilBlock, null, EdgeType.JUMP_UNCONDITIONAL));
} else if (Convert.isDecString(jumpTarget)) {
edgepairs.add(new Triple<ReilBlock, IAddress, EdgeType>(reilBlock, toReilAddress(jumpTarget), EdgeType.JUMP_UNCONDITIONAL));
} else if (reilInstruction.getThirdOperand().getType() == OperandType.SUB_ADDRESS) {
final String[] parts = jumpTarget.split("\\.");
edgepairs.add(new Triple<ReilBlock, IAddress, EdgeType>(reilBlock, toReilAddress(parts), EdgeType.JUMP_UNCONDITIONAL));
}
}
}
use of com.google.security.zynamics.zylib.disassembly.IAddress in project binnavi by google.
the class PostgreSQLProviderTest method testInstructionFunctionsCreateInstruction1.
@Test
public void testInstructionFunctionsCreateInstruction1() throws SQLException, CouldntLoadDataException, LoadCancelledException {
final INaviModule module = getProvider().loadModules().get(0);
module.load();
final String mnemonic = "burzel";
final IAddress iAddress = new CAddress(0x1234);
final COperandTreeNode rootNode = module.createOperandExpression("eax", ExpressionType.REGISTER);
final COperandTree tree = new COperandTree(rootNode, getProvider(), module.getTypeManager(), module.getContent().getTypeInstanceContainer());
final List<COperandTree> operands = new ArrayList<COperandTree>();
operands.add(tree);
final byte[] data = { 0xF };
final String architecture = "x86-32";
final INaviInstruction instruction = module.createInstruction(iAddress, mnemonic, operands, data, architecture);
PostgreSQLInstructionFunctions.createInstructions(getProvider(), Lists.newArrayList(instruction));
}
use of com.google.security.zynamics.zylib.disassembly.IAddress in project binnavi by google.
the class PostgreSQLProviderTest method testInstructionFunctionsAddReference1.
@Test
public void testInstructionFunctionsAddReference1() throws CouldntSaveDataException, CouldntLoadDataException, LoadCancelledException {
final INaviModule module = getProvider().loadModules().get(1);
module.load();
final INaviFunction function = module.getContent().getFunctionContainer().getFunctions().get(1800);
function.load();
final IBlockNode basicBlock = function.getBasicBlocks().get(0);
final INaviInstruction instruction = Iterables.get(basicBlock.getInstructions(), 1);
final COperandTree tree = instruction.getOperands().get(0);
final INaviOperandTreeNode node = tree.getRootNode();
final IAddress address = instruction.getAddress();
final ReferenceType type = ReferenceType.DATA;
final int references = node.getReferences().size();
PostgreSQLInstructionFunctions.addReference(getProvider(), node, address, type);
final INaviModule module2 = getProvider().loadModules().get(1);
module2.load();
final INaviFunction function2 = module2.getContent().getFunctionContainer().getFunctions().get(1800);
function2.load();
final IBlockNode basicBlock2 = function2.getBasicBlocks().get(0);
final INaviInstruction instruction2 = Iterables.get(basicBlock2.getInstructions(), 1);
final COperandTree tree2 = instruction2.getOperands().get(0);
final INaviOperandTreeNode node2 = tree2.getRootNode();
assertEquals(references + 1, node2.getReferences().size());
}
Aggregations