use of com.google.security.zynamics.binnavi.disassembly.views.INaviView in project binnavi by google.
the class CResultColorTest method testFirst.
@Test
public void testFirst() throws InternalTranslationException {
final MockSqlProvider mockProvider = new MockSqlProvider();
final MockModule mockModule = new MockModule(mockProvider);
// str 6, , t1
final COperandTreeNode rootNode1 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_SIZE_PREFIX_ID, "b4", null, new ArrayList<IReference>(), mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
final COperandTreeNode childNode1 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_IMMEDIATE_INT_ID, "6", null, new ArrayList<IReference>(), mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
COperandTreeNode.link(rootNode1, childNode1);
final COperandTreeNode rootNode2 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_SIZE_PREFIX_ID, "b4", null, new ArrayList<IReference>(), mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
final COperandTreeNode childNode2 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_SYMBOL_ID, ",", null, new ArrayList<IReference>(), mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
COperandTreeNode.link(rootNode2, childNode2);
final COperandTreeNode rootNode3 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_SIZE_PREFIX_ID, "b4", null, new ArrayList<IReference>(), mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
final COperandTreeNode childNode3 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_REGISTER_ID, "t1", null, new ArrayList<IReference>(), mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
COperandTreeNode.link(rootNode3, childNode3);
final COperandTree operand1 = new COperandTree(rootNode1, mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
final COperandTree operand2 = new COperandTree(rootNode2, mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
final COperandTree operand3 = new COperandTree(rootNode3, mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
final List<COperandTree> operands1 = Lists.newArrayList(operand1, operand2, operand3);
final INaviInstruction instruction1 = new CInstruction(false, mockModule, new CAddress(1), "str", operands1, new byte[0], "REIL", mockProvider);
// str 5, , t0
final COperandTreeNode rootNode4 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_SIZE_PREFIX_ID, "b4", null, new ArrayList<IReference>(), mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
final COperandTreeNode childNode4 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_IMMEDIATE_INT_ID, "5", null, new ArrayList<IReference>(), mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
COperandTreeNode.link(rootNode4, childNode4);
final COperandTreeNode rootNode5 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_SIZE_PREFIX_ID, "b4", null, new ArrayList<IReference>(), mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
final COperandTreeNode childNode5 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_SYMBOL_ID, ",", null, new ArrayList<IReference>(), mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
COperandTreeNode.link(rootNode5, childNode5);
final COperandTreeNode rootNode6 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_SIZE_PREFIX_ID, "b4", null, new ArrayList<IReference>(), mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
final COperandTreeNode childNode6 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_REGISTER_ID, "t0", null, new ArrayList<IReference>(), mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
COperandTreeNode.link(rootNode6, childNode6);
final COperandTree operand4 = new COperandTree(rootNode4, mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
final COperandTree operand5 = new COperandTree(rootNode5, mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
final COperandTree operand6 = new COperandTree(rootNode6, mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
final List<COperandTree> operands2 = Lists.newArrayList(operand4, operand5, operand6);
final INaviInstruction instruction2 = new CInstruction(false, mockModule, new CAddress(2), "str", operands2, new byte[0], "REIL", mockProvider);
// add t0, t1, t2
final COperandTreeNode rootNode7 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_SIZE_PREFIX_ID, "b4", null, new ArrayList<IReference>(), mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
final COperandTreeNode childNode7 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_REGISTER_ID, "t0", null, new ArrayList<IReference>(), mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
COperandTreeNode.link(rootNode7, childNode7);
final COperandTreeNode rootNode8 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_SIZE_PREFIX_ID, "b4", null, new ArrayList<IReference>(), mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
final COperandTreeNode childNode8 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_REGISTER_ID, "t1", null, new ArrayList<IReference>(), mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
COperandTreeNode.link(rootNode8, childNode8);
final COperandTreeNode rootNode9 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_SIZE_PREFIX_ID, "b4", null, new ArrayList<IReference>(), mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
final COperandTreeNode childNode9 = new COperandTreeNode(-1, IOperandTree.NODE_TYPE_REGISTER_ID, "t2", null, new ArrayList<IReference>(), mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
COperandTreeNode.link(rootNode9, childNode9);
final COperandTree operand7 = new COperandTree(rootNode7, mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
final COperandTree operand8 = new COperandTree(rootNode8, mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
final COperandTree operand9 = new COperandTree(rootNode9, mockProvider, mockModule.getTypeManager(), mockModule.getContent().getTypeInstanceContainer());
final List<COperandTree> operands3 = Lists.newArrayList(operand7, operand8, operand9);
final INaviInstruction instruction3 = new CInstruction(false, mockModule, new CAddress(3), "add", operands3, new byte[0], "REIL", mockProvider);
final INaviView view = mockModule.getContent().getViewContainer().createView("", "");
final List<INaviInstruction> instructions = Lists.newArrayList(instruction1, instruction2, instruction3);
view.getContent().createCodeNode(null, instructions);
final RegisterTrackingOptions options = new RegisterTrackingOptions(true, new HashSet<String>(), false, AnalysisDirection.UP);
final CTrackingResult results = CTracking.track(view, instruction3, "t2", options);
for (final CInstructionResult result : results.getResults()) {
if (result.getInstruction() == instruction1) {
assertEquals(Color.RED, CResultColor.determineBackgroundColor(instruction3, "t2", result));
} else if (result.getInstruction() == instruction2) {
assertEquals(Color.CYAN, CResultColor.determineBackgroundColor(instruction3, "t2", result));
} else if (result.getInstruction() == instruction3) {
assertEquals(Color.GREEN, CResultColor.determineBackgroundColor(instruction3, "t2", result));
}
}
}
use of com.google.security.zynamics.binnavi.disassembly.views.INaviView in project binnavi by google.
the class PostgreSQLViewFunctions method getDerivedViews.
/**
* Returns the derived views of the given view.
*
* @param provider Provides the connection to the database.
* @param view The view whose derived views are returned.
*
* @return The derived views.
*
* @throws CouldntLoadDataException Thrown if the derived views could not be determined.
*/
public static List<INaviView> getDerivedViews(final AbstractSQLProvider provider, final INaviView view) throws CouldntLoadDataException {
checkArguments(provider, view);
final List<INaviView> views = new ArrayList<INaviView>();
// currently project views can not have derived views.
if (view.getConfiguration().getModule() == null) {
return views;
}
final String query = "SELECT * FROM get_derived_views(?)";
try (PreparedStatement statement = provider.getConnection().getConnection().prepareStatement(query)) {
// TODO(timkornau): this should be changed to the ViewManager.
final List<INaviView> moduleViews = view.getConfiguration().getModule().getContent().getViewContainer().getViews();
statement.setInt(1, view.getConfiguration().getId());
final ResultSet resultSet = statement.executeQuery();
if (resultSet == null) {
return views;
}
while (resultSet.next()) {
final int viewId = resultSet.getInt(1);
if (viewId != view.getConfiguration().getId()) {
for (final INaviView moduleView : moduleViews) {
if (moduleView.getConfiguration().getId() == viewId) {
views.add(moduleView);
break;
}
}
}
}
return views;
} catch (final SQLException e) {
throw new CouldntLoadDataException(e);
}
}
use of com.google.security.zynamics.binnavi.disassembly.views.INaviView in project binnavi by google.
the class PostgreSQLViewNotificationParser method informModuleNotification.
/**
* The inform function for notifications from the bn_module_views table. This function is used
* when a module view is created or deleted.
*
* @param moduleViewNotificationContainer The {@link ViewNotificationContainer} to use as data
* source.
* @param provider The {@link SQLProvider} to access the database.
* @throws CouldntLoadDataException if the information could not be loaded from the database.
*/
private void informModuleNotification(final ViewNotificationContainer moduleViewNotificationContainer, final SQLProvider provider) throws CouldntLoadDataException {
if (moduleViewNotificationContainer.getDatabaseOperation().equals("INSERT")) {
final INaviModule module = moduleViewNotificationContainer.getNotificationModule().get();
if (!module.isLoaded()) {
return;
}
final Integer viewId = moduleViewNotificationContainer.getViewId();
final ImmutableNaviViewConfiguration databaseViewConfiguration = provider.loadFlowGraphInformation(module, viewId);
final CModuleViewGenerator generator = new CModuleViewGenerator(provider, module);
final INaviView view = generator.generate(databaseViewConfiguration, GraphType.FLOWGRAPH);
module.getContent().getViewContainer().addView(view);
}
if (moduleViewNotificationContainer.getDatabaseOperation().equals("UPDATE")) {
// updates will not happen as this is only a mapping of id to id.
return;
}
if (moduleViewNotificationContainer.getDatabaseOperation().equals("DELETE")) {
final INaviModule module = moduleViewNotificationContainer.getNotificationModule().get();
if (!module.isLoaded()) {
return;
}
final Integer viewId = moduleViewNotificationContainer.getViewId();
final INaviView view = ViewManager.get(provider).getView(viewId);
module.getContent().getViewContainer().deleteViewInternal(view);
}
}
use of com.google.security.zynamics.binnavi.disassembly.views.INaviView 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.views.INaviView in project binnavi by google.
the class PostgreSQLViewNotificationParser method informViewNotification.
/**
* The inform function for notifications from the bn_views table. This function is used when a
* view gets edited. But this function will not perform any action when a view is deleted or
* created.
*
* @param viewNotificationContainer The {@link ViewNotificationContainer} to use as data source.
* @param provider The {@link SQLProvider} to access the database with.
* @throws CouldntLoadDataException if the information could not be loaded from the database.
*/
private void informViewNotification(final ViewNotificationContainer viewNotificationContainer, final SQLProvider provider) throws CouldntLoadDataException {
if (viewNotificationContainer.getDatabaseOperation().equals("INSERT")) {
// we ignore inserts as we need to know if this was a module or project view.
return;
}
if (viewNotificationContainer.getDatabaseOperation().equals("UPDATE")) {
final Integer viewId = viewNotificationContainer.getViewId();
final INaviView view = viewNotificationContainer.getView().get();
final ImmutableNaviViewConfiguration databaseViewConfiguration = view.getConfiguration().getModule() == null ? provider.loadFlowGraphInformation(view.getConfiguration().getProject(), viewId) : provider.loadFlowGraphInformation(view.getConfiguration().getModule(), viewId);
if (databaseViewConfiguration == null) {
return;
}
if (!databaseViewConfiguration.getName().equals(view.getConfiguration().getName())) {
view.getConfiguration().setNameInternal(databaseViewConfiguration.getName());
}
if (databaseViewConfiguration.getDescription() != view.getConfiguration().getDescription() && !databaseViewConfiguration.getDescription().equals(view.getConfiguration().getDescription())) {
view.getConfiguration().setDescriptionInternal(databaseViewConfiguration.getDescription());
}
if (databaseViewConfiguration.getStarState() != view.getConfiguration().isStared()) {
view.getConfiguration().setStaredInternal(databaseViewConfiguration.getStarState());
}
view.getConfiguration().setModificationDateInternal(databaseViewConfiguration.getModificationDate());
}
if (viewNotificationContainer.getDatabaseOperation().equals("DELETE")) {
// we ignore deletes as we need to know if this was a module or project view.
return;
}
}
Aggregations