use of com.google.security.zynamics.binnavi.disassembly.CCodeNode in project binnavi by google.
the class CViewInserter method insertCodeNode.
/**
* Clones a source node and inserts the cloned node into the target view.
*
* @param target The target view where the node is inserted.
* @param node Node from the source view that is cloned.
*
* @return The cloned code node.
*/
private static INaviCodeNode insertCodeNode(final INaviView target, final INaviCodeNode node) {
// TODO: cloning the node is a bad solution since this just fixes the symptoms: instructions are
// closed two times
final INaviCodeNode sourceNode = (INaviCodeNode) node.cloneNode();
final Iterable<INaviInstruction> instructions = sourceNode.getInstructions();
final ArrayList<INaviInstruction> instructionList = Lists.newArrayList(instructions);
CCodeNode codeNode;
try {
codeNode = target.getContent().createCodeNode(sourceNode.getParentFunction(), instructionList);
} catch (final MaybeNullException e) {
codeNode = target.getContent().createCodeNode(null, instructionList);
}
if (sourceNode.getComments().getGlobalCodeNodeComment() != null) {
codeNode.getComments().initializeGlobalCodeNodeComment(sourceNode.getComments().getGlobalCodeNodeComment());
}
if (sourceNode.getComments().getLocalCodeNodeComment() != null) {
codeNode.getComments().initializeLocalCodeNodeComment(sourceNode.getComments().getLocalCodeNodeComment());
}
final Iterable<INaviInstruction> newInstructions = codeNode.getInstructions();
for (int i = 0; i < Iterables.size(instructions); i++) {
codeNode.getComments().initializeLocalInstructionComment(Iterables.get(newInstructions, i), sourceNode.getComments().getLocalInstructionComment(Iterables.get(instructions, i)));
}
return codeNode;
}
use of com.google.security.zynamics.binnavi.disassembly.CCodeNode in project binnavi by google.
the class PostgreSQLNodeSaver method saveCodeNodeInstructions.
/**
* Saves the mapping between code nodes and their instructions to the database.
*
* @param provider The provider used to access the database.
* @param nodes The nodes to save.
* @param firstNode The database index of the first node.
* @param codeNodeIndices Index into the nodes list that identifies the code nodes.
*
* @throws SQLException Thrown if saving the code node instructions failed.
*/
protected static ArrayList<Pair<INaviCodeNode, INaviInstruction>> saveCodeNodeInstructions(final SQLProvider provider, final List<INaviViewNode> nodes, final int firstNode, final List<Integer> codeNodeIndices) throws SQLException {
if (!nodes.isEmpty()) {
final Set<INaviInstruction> unsavedInstructions = new HashSet<INaviInstruction>();
for (final int index : codeNodeIndices) {
final CCodeNode node = (CCodeNode) nodes.get(index);
final Iterable<INaviInstruction> instructions = node.getInstructions();
for (final INaviInstruction instruction : instructions) {
if (!(instruction.isStored())) {
unsavedInstructions.add(instruction);
}
}
}
PostgreSQLInstructionFunctions.createInstructions(provider, unsavedInstructions);
final String query = "INSERT INTO " + CTableNames.CODENODE_INSTRUCTIONS_TABLE + " (module_id, node_id, position, address, comment_id) VALUES (?, ?, ?, ?, ?)";
final PreparedStatement preparedStatement = provider.getConnection().getConnection().prepareStatement(query);
final ArrayList<Pair<INaviCodeNode, INaviInstruction>> instructionsWithUnsavedLocalComments = new ArrayList<Pair<INaviCodeNode, INaviInstruction>>();
try {
for (final Integer index : codeNodeIndices) {
final INaviCodeNode codeNode = (INaviCodeNode) nodes.get(index);
int position = 0;
for (final INaviInstruction instruction : codeNode.getInstructions()) {
final List<IComment> comments = codeNode.getComments().getLocalInstructionComment(instruction);
final Integer commentId = comments == null ? null : comments.size() == 0 ? null : Iterables.getLast(comments).getId();
if ((comments != null) && (comments.size() != 0) && (commentId == null)) {
instructionsWithUnsavedLocalComments.add(new Pair<INaviCodeNode, INaviInstruction>(codeNode, instruction));
}
final int moduleId = instruction.getModule().getConfiguration().getId();
preparedStatement.setInt(1, moduleId);
preparedStatement.setInt(2, firstNode + index);
preparedStatement.setInt(3, position);
preparedStatement.setObject(4, instruction.getAddress().toBigInteger(), Types.BIGINT);
if (commentId == null) {
preparedStatement.setNull(5, Types.INTEGER);
} else {
preparedStatement.setInt(5, commentId);
}
position++;
preparedStatement.addBatch();
}
}
preparedStatement.executeBatch();
} finally {
preparedStatement.close();
}
return instructionsWithUnsavedLocalComments;
}
return null;
}
use of com.google.security.zynamics.binnavi.disassembly.CCodeNode in project binnavi by google.
the class PostgreSQLProviderTest method testSave.
@Test
public void testSave() throws CouldntSaveDataException, CouldntLoadDataException, CouldntDeleteException, CPartialLoadException, InternalTranslationException, LoadCancelledException, MaybeNullException {
final CTagManager tagManager = getProvider().loadTagManager(TagType.NODE_TAG);
tagManager.addTag(tagManager.getRootTag(), "Node Tag I");
tagManager.addTag(tagManager.getRootTag(), "Node Tag II");
final ITreeNode<CTag> tag1 = tagManager.getRootTag().getChildren().get(0);
final ITreeNode<CTag> tag2 = tagManager.getRootTag().getChildren().get(1);
final INaviModule module = getProvider().loadModules().get(0);
module.load();
final CView view = module.getContent().getViewContainer().createView("Save View", "Save View Description");
final INaviFunction function = module.getContent().getFunctionContainer().getFunction("sub_1002B87");
function.load();
final List<COperandTree> operands = new ArrayList<COperandTree>();
final COperandTreeNode root1 = module.createOperandExpression("dword", ExpressionType.SIZE_PREFIX);
final COperandTreeNode child1 = module.createOperandExpression("eax", ExpressionType.REGISTER);
COperandTreeNode.link(root1, child1);
final COperandTreeNode root2 = module.createOperandExpression("dword", ExpressionType.SIZE_PREFIX);
final COperandTreeNode child2 = module.createOperandExpression("16", ExpressionType.IMMEDIATE_INTEGER);
COperandTreeNode.link(root2, child2);
final COperandTree operand1 = module.createOperand(root1);
final COperandTree operand2 = module.createOperand(root2);
operands.add(operand1);
operands.add(operand2);
final Iterable<INaviInstruction> instructions = function.getBasicBlocks().get(0).getInstructions();
final Iterable<INaviInstruction> instructions2 = function.getBasicBlocks().get(1).getInstructions();
final CCodeNode codeNode = view.getContent().createCodeNode(function, Lists.newArrayList(instructions));
codeNode.tagNode(tag1.getObject());
codeNode.getComments().appendLocalCodeNodeComment("XXX");
codeNode.getComments().appendLocalInstructionComment(Iterables.getLast(codeNode.getInstructions()), "YYY");
Iterables.getLast(codeNode.getInstructions()).appendGlobalComment(" GLOBAL INSTRUCTION COMMENT ");
@SuppressWarnings("unused") final CCodeNode codeNode2 = view.getContent().createCodeNode(null, Lists.newArrayList(instructions2));
final CFunctionNode functionNode = view.getContent().createFunctionNode(function);
functionNode.tagNode(tag2.getObject());
functionNode.appendLocalFunctionComment("ZZZ");
@SuppressWarnings("unused") final CNaviViewEdge edge = view.getContent().createEdge(codeNode, functionNode, EdgeType.JUMP_UNCONDITIONAL);
view.save();
view.close();
view.load();
assertEquals(3, view.getGraph().getNodes().size());
assertEquals(1, view.getGraph().getEdges().size());
assertTrue(view.getGraph().getNodes().get(0).isTagged(tag1.getObject()));
assertTrue(view.getGraph().getNodes().get(2).isTagged(tag2.getObject()));
final CCodeNode loadedCodeNode = (CCodeNode) view.getGraph().getNodes().get(0);
final CCodeNode loadedCodeNode2 = (CCodeNode) view.getGraph().getNodes().get(1);
assertEquals("XXX", loadedCodeNode.getComments().getLocalCodeNodeComment().get(0).getComment());
final INaviInstruction customInstruction = Iterables.getLast(loadedCodeNode.getInstructions());
assertEquals(" GLOBAL INSTRUCTION COMMENT ", customInstruction.getGlobalComment().get(0).getComment());
assertEquals("YYY", loadedCodeNode.getComments().getLocalInstructionComment(customInstruction).get(0).getComment());
final ReilTranslator<INaviInstruction> translator = new ReilTranslator<INaviInstruction>();
translator.translate(new StandardEnvironment(), loadedCodeNode);
translator.translate(new StandardEnvironment(), loadedCodeNode2);
final CFunctionNode loadedFunctionNode = (CFunctionNode) view.getGraph().getNodes().get(2);
assertEquals("ZZZ", loadedFunctionNode.getLocalFunctionComment().get(0).getComment());
tagManager.deleteTag(tag1);
tagManager.deleteTag(tag2);
}
use of com.google.security.zynamics.binnavi.disassembly.CCodeNode in project binnavi by google.
the class CCodeNodeParser method createCurrentNode.
/**
* Creates a new code node.
*
* @param resultSet Provides the data for the code node.
*
* @return The created code node object.
*
* @throws ParserException Thrown if the node data could not be read.
* @throws CPartialLoadException Thrown if not all required modules are loaded.
*/
private CCodeNode createCurrentNode(final ICodeNodeProvider resultSet) throws ParserException, CPartialLoadException {
final int nodeId = resultSet.getNodeId();
final int moduleId = resultSet.getModule();
final IAddress parentFunction = resultSet.getParentFunction();
final INaviModule module = modules.get(moduleId);
if (module == null) {
throw new ParserException(String.format("Node with ID %d has unknown parent module with ID %d", nodeId, moduleId));
}
if (!module.isLoaded()) {
try {
module.load();
} catch (final CouldntLoadDataException e) {
throw new CPartialLoadException("E00066: The view could not be loaded because not all modules that form the view could be loaded", module);
} catch (final LoadCancelledException e) {
throw new CPartialLoadException("E00067: The view could not be loaded because it was cancelled", module);
}
}
final INaviFunction function = parentFunction == null ? null : module.getContent().getFunctionContainer().getFunction(parentFunction);
if ((parentFunction != null) && (function == null)) {
throw new ParserException(String.format("Node with ID %d has unknown parent function with address %s", nodeId, parentFunction.toHexString()));
}
final double x = resultSet.getX();
final double y = resultSet.getY();
final double width = resultSet.getWidth();
final double height = resultSet.getHeight();
final Color color = new Color(resultSet.getColor());
final Color bordercolor = new Color(resultSet.getBorderColor());
final boolean selected = resultSet.isSelected();
final boolean visible = resultSet.isVisible();
final Integer localCodeNodeCommentId = resultSet.getLocalNodeCommentId();
final Integer globalCodeNodeCommentId = resultSet.getGlobalNodeCommentId();
// TODO(timkornau): final new Set<CTag>! must replaced by a set which
// contains the loaded node
// tags from the DB
final CCodeNode codeNode = new CCodeNode(nodeId, x, y, width, height, color, bordercolor, selected, visible, null, function, new HashSet<CTag>(), sqlProvider);
if (localCodeNodeCommentId != null) {
localCommentIdToCodeNode.put(localCodeNodeCommentId, codeNode);
}
if (globalCodeNodeCommentId != null) {
globalCommentIdToCodeNode.put(globalCodeNodeCommentId, codeNode);
}
return codeNode;
}
use of com.google.security.zynamics.binnavi.disassembly.CCodeNode in project binnavi by google.
the class View method createCodeNode.
// ! Creates a new code node.
/**
* Creates a code node in the view.
*
* @param function The parent function of the code node. This argument is optional can can be
* null.
* @param instructions The instructions that should be displayed in the code node.
*
* @return The created code node.
*/
public CodeNode createCodeNode(final Function function, final List<Instruction> instructions) {
Preconditions.checkNotNull(instructions, "Error: Instructions argument can't be null");
final List<INaviInstruction> instructionsList = new ArrayList<INaviInstruction>();
for (final Instruction instruction : instructions) {
Preconditions.checkNotNull(instruction, "Error: Instruction list contains a null-element");
instructionsList.add(instruction.getNative());
}
assert !instructions.isEmpty();
final CCodeNode newCodenode = naviView.getContent().createCodeNode(function == null ? null : function.getNative(), instructionsList);
newCodenode.setColor(ConfigManager.instance().getColorSettings().getBasicBlocksColor());
return (CodeNode) cachedNodes.get(newCodenode);
}
Aggregations