use of com.google.security.zynamics.binnavi.disassembly.INaviFunction in project binnavi by google.
the class PostgreSQLFunctionNodeLoader method load.
/**
* Loads the function nodes of a view.
*
* @param provider The connection to the database.
* @param view The view whose function nodes are loaded.
* @param nodes The loaded nodes are stored here.
*
* @throws CPartialLoadException Thrown if loading the nodes failed because a necessary module was
* not loaded.
* @throws CouldntLoadDataException
*/
public static void load(final AbstractSQLProvider provider, final INaviView view, final List<INaviViewNode> nodes) throws CPartialLoadException, CouldntLoadDataException {
Preconditions.checkNotNull(provider, "IE02510: provider argument can not be null");
Preconditions.checkNotNull(view, "IE02511: view argument can not be null");
Preconditions.checkNotNull(nodes, "IE02512: nodes argument can not be null");
// TODO (timkornau): query needs to go into the database.
final String query = "SELECT nodes.view_id, nodes.id, functions.module_id, " + " function, fnodes.comment_id as local_comment, x, y, width, height, " + " color, selected, visible FROM " + CTableNames.NODES_TABLE + " AS nodes JOIN " + CTableNames.FUNCTION_NODES_TABLE + " AS fnodes " + " ON nodes.id = fnodes.node_id JOIN " + CTableNames.FUNCTIONS_TABLE + " AS functions ON functions.address = fnodes.function " + " AND functions.module_id = fnodes.module_id WHERE view_id = ?";
final Map<Integer, INaviFunctionNode> commentIdToFunctionNode = new HashMap<Integer, INaviFunctionNode>();
try {
final PreparedStatement statement = provider.getConnection().getConnection().prepareStatement(query);
statement.setInt(1, view.getConfiguration().getId());
final ResultSet resultSet = statement.executeQuery();
try {
while (resultSet.next()) {
final int moduleId = resultSet.getInt("module_id");
final INaviModule module = provider.findModule(moduleId);
if (!module.isLoaded()) {
try {
module.load();
} catch (final CouldntLoadDataException e) {
throw new CPartialLoadException("E00064: The view could not be loaded because not all modules that form the view are loaded", module);
} catch (final LoadCancelledException e) {
throw new CPartialLoadException("E00065: The view could not be loaded because not all modules that form the view are loaded", module);
}
}
final IAddress address = PostgreSQLHelpers.loadAddress(resultSet, "function");
final INaviFunction function = module.getContent().getFunctionContainer().getFunction(address);
final int nodeId = resultSet.getInt("id");
Integer commentId = resultSet.getInt("local_comment");
if (resultSet.wasNull()) {
commentId = null;
}
final double posX = resultSet.getDouble("x");
final double posY = resultSet.getDouble("y");
final double width = resultSet.getDouble("width");
final double height = resultSet.getDouble("height");
final Color color = new Color(resultSet.getInt("color"));
final boolean selected = resultSet.getBoolean("selected");
final boolean visible = resultSet.getBoolean("visible");
final INaviFunctionNode functionNode = new CFunctionNode(nodeId, function, posX, posY, width, height, color, selected, visible, null, new HashSet<CTag>(), provider);
nodes.add(functionNode);
if (commentId != null) {
commentIdToFunctionNode.put(commentId, functionNode);
}
}
} finally {
resultSet.close();
}
if (!commentIdToFunctionNode.isEmpty()) {
final HashMap<Integer, ArrayList<IComment>> commentIdsToComments = PostgreSQLCommentFunctions.loadMultipleCommentsById(provider, commentIdToFunctionNode.keySet());
for (final Entry<Integer, ArrayList<IComment>> commentIdToComment : commentIdsToComments.entrySet()) {
commentIdToFunctionNode.get(commentIdToComment.getKey()).initializeLocalFunctionComment(commentIdToComment.getValue());
}
}
} catch (final SQLException exception) {
throw new CouldntLoadDataException(exception);
}
}
use of com.google.security.zynamics.binnavi.disassembly.INaviFunction in project binnavi by google.
the class PostgreSQLViewsLoader method loadViewFunctionMapping.
/**
* Loads the view -> function mapping from the database.
*
* @param provider The SQL provider that provides the connection.
* @param flowgraphs List of all native Flow graph views of a module.
* @param functions List of all functions of a module.
* @param module The module from which to load the mapping.
*
* @return A view -> function mapping and a function -> view mapping.
*
* @throws CouldntLoadDataException Thrown if the mapping could not be loaded.
*/
public static final ImmutableBiMap<INaviView, INaviFunction> loadViewFunctionMapping(final AbstractSQLProvider provider, final List<IFlowgraphView> flowgraphs, final List<INaviFunction> functions, final CModule module) throws CouldntLoadDataException {
checkArguments(provider, module, flowgraphs, functions);
final HashMap<Integer, INaviView> viewmap = new HashMap<Integer, INaviView>();
for (final IFlowgraphView view : flowgraphs) {
viewmap.put(view.getConfiguration().getId(), view);
}
final HashMap<IAddress, INaviFunction> functionMap = new HashMap<IAddress, INaviFunction>();
for (final INaviFunction function : functions) {
functionMap.put(function.getAddress(), function);
}
final CConnection connection = provider.getConnection();
final String query = "SELECT view_id, function FROM " + CTableNames.FUNCTION_VIEWS_TABLE + " WHERE module_id = " + module.getConfiguration().getId();
final HashMap<INaviView, INaviFunction> viewFunctionMap = new HashMap<INaviView, INaviFunction>();
try {
final ResultSet resultSet = connection.executeQuery(query, true);
try {
while (resultSet.next()) {
final INaviView view = viewmap.get(resultSet.getInt("view_id"));
final INaviFunction function = functionMap.get(PostgreSQLHelpers.loadAddress(resultSet, "function"));
if ((view != null) && (function != null)) {
viewFunctionMap.put(view, function);
}
}
} finally {
resultSet.close();
}
return new ImmutableBiMap.Builder<INaviView, INaviFunction>().putAll(viewFunctionMap).build();
} catch (final SQLException e) {
throw new CouldntLoadDataException(e);
}
}
use of com.google.security.zynamics.binnavi.disassembly.INaviFunction in project binnavi by google.
the class PostgreSQLTypeInstanceFunctionsTests method deleteTypeInstanceReferenceTest5.
@Test
public void deleteTypeInstanceReferenceTest5() throws CouldntDeleteException, CouldntLoadDataException, LoadCancelledException, CPartialLoadException, CouldntSaveDataException {
module.load();
final TypeInstance typeInstance = module.getContent().getTypeInstanceContainer().getTypeInstances().get(0);
final TypeInstanceAddress address1 = typeInstance.getAddress();
final INaviFunction function = module.getContent().getFunctionContainer().getFunction(new CAddress("1001929", 16));
final INaviView view = module.getContent().getViewContainer().getView(function);
view.load();
final INaviInstruction instruction = view.getBasicBlocks().get(2).getInstructions().iterator().next();
Assert.assertNotNull(typeInstance);
provider.createTypeInstanceReference(typeInstance.getModule().getConfiguration().getId(), instruction.getAddress().toLong(), instruction.getOperandPosition(instruction.getOperands().get(1)), instruction.getOperands().get(0).getNodes().get(0).getId(), typeInstance.getId());
view.close();
module.close();
module.load();
view.load();
final TypeInstance typeInstance2 = module.getContent().getTypeInstanceContainer().getTypeInstance(typeInstance.getAddress());
Assert.assertEquals(address1, typeInstance2.getAddress());
final List<TypeInstanceReference> references = module.getContent().getTypeInstanceContainer().getReferences(typeInstance2);
Assert.assertTrue(!references.isEmpty());
final TypeInstanceReference reference = Iterables.find(references, new Predicate<TypeInstanceReference>() {
@Override
public boolean apply(final TypeInstanceReference reference) {
return reference.getAddress().equals(instruction.getAddress());
}
});
Assert.assertNotNull(reference);
provider.deleteTypeInstanceReference(module.getConfiguration().getId(), reference.getAddress().toBigInteger(), reference.getPosition(), reference.getTreeNode().get().getId());
}
use of com.google.security.zynamics.binnavi.disassembly.INaviFunction in project binnavi by google.
the class PostgreSQLVerifyCalcTest method verifyViews.
private static void verifyViews(final INaviModule module) throws CouldntLoadDataException, CPartialLoadException, LoadCancelledException {
final List<INaviView> views = module.getContent().getViewContainer().getViews();
final INaviView callgraph = views.get(0);
assertEquals(null, module.getContent().getViewContainer().getFunction(callgraph));
assertEquals(ViewType.Native, callgraph.getType());
assertEquals(GraphType.CALLGRAPH, callgraph.getGraphType());
callgraph.load();
callgraph.close();
for (int i = 1; i < views.size(); i++) {
final INaviView view = views.get(i);
final INaviFunction function = module.getContent().getViewContainer().getFunction(view);
assertEquals(view.getName(), function.getName());
assertEquals(ViewType.Native, view.getType());
assertEquals(GraphType.FLOWGRAPH, view.getGraphType());
assertEquals(view.getNodeCount(), function.getBasicBlockCount());
assertEquals(view.getEdgeCount(), function.getEdgeCount());
view.load();
function.load();
assertEquals(view.getNodeCount(), function.getBasicBlockCount());
assertEquals(view.getEdgeCount(), function.getEdgeCount());
view.close();
function.close();
}
}
use of com.google.security.zynamics.binnavi.disassembly.INaviFunction in project binnavi by google.
the class PostgreSQLVerifyNotepadTest method verifyInstructions.
@Test
public void verifyInstructions() throws CouldntLoadDataException, LoadCancelledException {
final INaviModule module = m_database.getContent().getModules().get(0);
module.load();
final INaviFunction function = module.getContent().getFunctionContainer().getFunction(new CAddress(BigInteger.valueOf(0x0100195D)));
function.load();
final List<IBlockNode> blocks = function.getBasicBlocks();
final IBlockNode block = blocks.get(0);
assertEquals(BigInteger.valueOf(0x0100195D), block.getAddress().toBigInteger());
final Iterable<INaviInstruction> instructions = block.getInstructions();
Iterables.get(instructions, 0).toString();
assertEquals("0100195D mov edi, edi", Iterables.get(instructions, 0).toString());
assertEquals("0100195F push ebp", Iterables.get(instructions, 1).toString());
assertEquals("01001960 mov ebp, esp", Iterables.get(instructions, 2).toString());
assertEquals("01001962 push ecx", Iterables.get(instructions, 3).toString());
assertEquals("01001963 push 2", Iterables.get(instructions, 4).toString());
assertEquals("01001965 lea eax, ss: [ebp + LCData]", Iterables.get(instructions, 5).toString());
assertEquals("01001968 push eax", Iterables.get(instructions, 6).toString());
assertEquals("01001969 push 13", Iterables.get(instructions, 7).toString());
assertEquals("0100196B push 1024", Iterables.get(instructions, 8).toString());
assertEquals("01001970 mov ds: [16819428], sub_1005F63", Iterables.get(instructions, 9).toString());
assertEquals("0100197A mov ds: [16819436], 12", Iterables.get(instructions, 10).toString());
assertEquals("01001984 call ds: [GetLocaleInfoW]", Iterables.get(instructions, 11).toString());
assertEquals("0100198A cmp word ss: [ebp + LCData], word 49", Iterables.get(instructions, 12).toString());
assertEquals("0100198F jnz loc_10019B1", Iterables.get(instructions, 13).toString());
module.close();
}
Aggregations