use of com.google.security.zynamics.binnavi.disassembly.CCallgraph in project binnavi by google.
the class PostgreSQLCallgraphLoader method loadCallgraph.
/**
* Loads the call graph of a module.
*
* The module, the call graph ID, and all functions of the function list must refer to objects
* stored in the database connected to by the provider argument.
*
* @param provider The SQL provider that provides the connection.
* @param module The module whose call graph is loaded.
* @param callgraphId The view ID of the call graph to load.
* @param functions A list that contains all functions of the module.
*
* @return The loaded call graph.
*
* @throws CouldntLoadDataException Thrown if the call graph could not be loaded.
*/
public static CCallgraph loadCallgraph(final AbstractSQLProvider provider, final CModule module, final int callgraphId, final List<INaviFunction> functions) throws CouldntLoadDataException {
checkArguments(provider, module, functions);
final CConnection connection = provider.getConnection();
try {
final Pair<List<ICallgraphNode>, Map<Integer, CCallgraphNode>> nodeResult = loadNodes(connection, callgraphId, functions);
final List<ICallgraphEdge> edges = loadEdges(connection, callgraphId, nodeResult.second());
return new CCallgraph(nodeResult.first(), edges);
} catch (final SQLException exception) {
throw new CouldntLoadDataException(exception);
}
}
use of com.google.security.zynamics.binnavi.disassembly.CCallgraph in project binnavi by google.
the class CPostgreSQLModuleContentTest method testCModuleContentConstructor.
@Test
public void testCModuleContentConstructor() throws LoadCancelledException, CouldntLoadDataException {
final CModule module = (CModule) getDatabase().getContent().getModules().get(0);
module.load();
final ListenerProvider<IModuleListener> listeners = new ListenerProvider<IModuleListener>();
final CCallgraph callgraph = module.getContent().getNativeCallgraph();
final IFilledList<INaviFunction> functions = new FilledList<INaviFunction>();
functions.add(module.getContent().getFunctionContainer().getFunctions().get(0));
final ICallgraphView nativeCallgraph = module.getContent().getViewContainer().getNativeCallgraphView();
final ImmutableList<IFlowgraphView> nativeFlowgraphs = module.getContent().getViewContainer().getNativeFlowgraphViews();
final List<INaviView> customViews = new ArrayList<INaviView>();
final ImmutableBiMap<INaviView, INaviFunction> viewFunctionMap = new ImmutableBiMap.Builder<INaviView, INaviFunction>().build();
new Pair<HashMap<INaviView, INaviFunction>, HashMap<INaviFunction, INaviView>>(null, null);
final IFilledList<TraceList> traces = new FilledList<TraceList>();
final SectionContainer sections = new SectionContainer(new SectionContainerBackend(getProvider(), module));
final TypeInstanceContainer instances = new TypeInstanceContainer(new TypeInstanceContainerBackend(getProvider(), module, module.getTypeManager(), sections), getProvider());
final CModuleContent moduleContent1 = new CModuleContent(module, getProvider(), listeners, callgraph, functions, nativeCallgraph, nativeFlowgraphs, customViews, viewFunctionMap, traces, sections, instances);
assertNotNull(moduleContent1);
try {
@SuppressWarnings("unused") final CModuleContent moduleContent = new CModuleContent(null, null, null, null, null, null, null, null, null, null, sections, instances);
fail();
} catch (final NullPointerException e) {
}
try {
@SuppressWarnings("unused") final CModuleContent moduleContent = new CModuleContent(module, null, null, null, null, null, null, null, null, null, null, null);
fail();
} catch (final NullPointerException e) {
}
try {
@SuppressWarnings("unused") final CModuleContent moduleContent = new CModuleContent(module, getProvider(), null, null, null, null, null, null, null, null, null, null);
fail();
} catch (final NullPointerException e) {
}
try {
@SuppressWarnings("unused") final CModuleContent moduleContent = new CModuleContent(module, getProvider(), listeners, null, null, null, null, null, null, null, null, null);
fail();
} catch (final NullPointerException e) {
}
try {
@SuppressWarnings("unused") final CModuleContent moduleContent = new CModuleContent(module, getProvider(), listeners, callgraph, null, null, null, null, null, null, null, null);
fail();
} catch (final NullPointerException e) {
}
try {
@SuppressWarnings("unused") final CModuleContent moduleContent = new CModuleContent(module, getProvider(), listeners, callgraph, functions, null, null, null, null, null, null, null);
fail();
} catch (final NullPointerException e) {
}
try {
@SuppressWarnings("unused") final CModuleContent moduleContent = new CModuleContent(module, getProvider(), listeners, callgraph, functions, nativeCallgraph, null, null, null, null, null, null);
fail();
} catch (final NullPointerException e) {
}
try {
@SuppressWarnings("unused") final CModuleContent moduleContent = new CModuleContent(module, getProvider(), listeners, callgraph, functions, nativeCallgraph, nativeFlowgraphs, null, null, null, null, null);
fail();
} catch (final NullPointerException e) {
}
try {
@SuppressWarnings("unused") final CModuleContent moduleContent = new CModuleContent(module, getProvider(), listeners, callgraph, functions, nativeCallgraph, nativeFlowgraphs, customViews, null, null, null, null);
fail();
} catch (final NullPointerException e) {
}
try {
@SuppressWarnings("unused") final CModuleContent moduleContent = new CModuleContent(module, getProvider(), listeners, callgraph, functions, nativeCallgraph, nativeFlowgraphs, customViews, viewFunctionMap, null, null, null);
fail();
} catch (final NullPointerException e) {
}
try {
@SuppressWarnings("unused") final CModuleContent moduleContent = new CModuleContent(module, getProvider(), listeners, callgraph, functions, nativeCallgraph, nativeFlowgraphs, customViews, viewFunctionMap, traces, null, null);
fail();
} catch (final NullPointerException e) {
}
}
use of com.google.security.zynamics.binnavi.disassembly.CCallgraph in project binnavi by google.
the class CModule method load.
@Override
public void load() throws CouldntLoadDataException, LoadCancelledException {
synchronized (m_loadReporter) {
if (isLoaded()) {
return;
}
if (!isInitialized()) {
throw new IllegalStateException("IE02617: The module is not initialized yet");
}
m_isLoading = true;
try {
if (!m_loadReporter.report(ModuleLoadEvents.Starting)) {
throw new LoadCancelledException();
}
if (!m_loadReporter.report(ModuleLoadEvents.LoadingCallgraphView)) {
throw new LoadCancelledException();
}
final ICallgraphView nativeCallgraph = m_provider.loadNativeCallgraph(this);
if (!m_loadReporter.report(ModuleLoadEvents.LoadingFlowgraphs)) {
throw new LoadCancelledException();
}
// When loading views, an empty flow graph (imported function) can not
// be distinguished
// from an empty call graph. This leads to problems in modules without
// functions. In
// these cases, the empty call graph is loaded as an imported function
// view. This is
// obviously wrong, so we are correcting this here.
final ImmutableList<IFlowgraphView> nativeFlowgraphs = nativeCallgraph.getNodeCount() == 0 ? new ImmutableList.Builder<IFlowgraphView>().build() : m_provider.loadNativeFlowgraphs(this);
if (!m_loadReporter.report(ModuleLoadEvents.LoadingCallgraphViews)) {
throw new LoadCancelledException();
}
final List<ICallgraphView> userCallgraphs = m_provider.loadCallgraphViews(this);
if (!m_loadReporter.report(ModuleLoadEvents.LoadingFlowgraphViews)) {
throw new LoadCancelledException();
}
final ImmutableList<IFlowgraphView> userFlowgraphs = m_provider.loadFlowgraphs(this);
if (!m_loadReporter.report(ModuleLoadEvents.LoadingMixedViews)) {
throw new LoadCancelledException();
}
final List<INaviView> userMixedGraphs = m_provider.loadMixedgraphs(this);
if (!m_loadReporter.report(ModuleLoadEvents.LoadingCallgraph)) {
throw new LoadCancelledException();
}
if (!m_loadReporter.report(ModuleLoadEvents.LoadingFunctions)) {
throw new LoadCancelledException();
}
// Note: the type manager needs to be loaded prior to functions, since a function might
// have an associated stack frame, which in turn needs the type system.
typeManager = new TypeManager(new TypeManagerDatabaseBackend(m_provider, this));
final List<INaviFunction> functions = m_provider.loadFunctions(this, nativeFlowgraphs);
final ImmutableBiMap<INaviView, INaviFunction> viewFunctionMap = m_provider.loadViewFunctionMapping(nativeFlowgraphs, functions, this);
final CCallgraph callgraph = m_provider.loadCallgraph(this, nativeCallgraph.getConfiguration().getId(), functions);
if (!m_loadReporter.report(ModuleLoadEvents.LoadingTraces)) {
throw new LoadCancelledException();
}
final List<TraceList> traces = m_provider.loadTraces(this);
if (!m_loadReporter.report(ModuleLoadEvents.LoadingGlobalVariables)) {
throw new LoadCancelledException();
}
final List<INaviView> customViews = new ArrayList<INaviView>();
customViews.addAll(userCallgraphs);
customViews.addAll(userFlowgraphs);
customViews.addAll(userMixedGraphs);
final List<INaviView> currentViews = new ArrayList<INaviView>(nativeFlowgraphs);
currentViews.addAll(customViews);
Collections.sort(currentViews, new Comparator<INaviView>() {
@Override
public int compare(final INaviView lhs, final INaviView rhs) {
return lhs.getConfiguration().getId() - rhs.getConfiguration().getId();
}
});
// Map viewId to corresponding index in currentViews.
final Map<Integer, Integer> viewIdToIndex = new HashMap<Integer, Integer>();
for (int i = 0; i < currentViews.size(); ++i) {
viewIdToIndex.put(currentViews.get(i).getConfiguration().getId(), i);
}
if (!m_loadReporter.report(ModuleLoadEvents.LoadingTypes)) {
throw new LoadCancelledException();
}
final SectionContainer sections = new SectionContainer(new SectionContainerBackend(m_provider, this));
final TypeInstanceContainer typeInstances = new TypeInstanceContainer(new TypeInstanceContainerBackend(m_provider, this, typeManager, sections), m_provider);
m_content = new CModuleContent(this, m_provider, m_listeners, callgraph, functions, nativeCallgraph, nativeFlowgraphs, customViews, viewFunctionMap, traces, sections, typeInstances);
typeInstances.initialize();
} catch (final CouldntLoadDataException e) {
m_isLoading = false;
throw e;
} catch (final LoadCancelledException e) {
m_isLoading = false;
throw e;
} finally {
m_loadReporter.report(ModuleLoadEvents.Finished);
}
for (final IModuleListener listener : m_listeners) {
try {
listener.loadedModule(this);
} catch (final Exception exception) {
CUtilityFunctions.logException(exception);
}
}
m_isLoading = false;
}
}
use of com.google.security.zynamics.binnavi.disassembly.CCallgraph in project binnavi by google.
the class CCallgraphCombiner method createCombinedCallgraph.
/**
* Combines the call graphs of the modules of an address space.
*
* @param project The project where the combined view is created.
* @param addressSpace Provides the modules whose call graphs are combined.
*
* @return The view that contains the combined call graph.
*/
public static INaviView createCombinedCallgraph(final INaviProject project, final INaviAddressSpace addressSpace) {
final INaviView view = project.getContent().createView("Combined Callgraph", "");
final Map<INaviFunction, CFunctionNode> nodeMap = new HashMap<INaviFunction, CFunctionNode>();
final Map<INaviFunction, INaviFunction> resolvedMap = new HashMap<INaviFunction, INaviFunction>();
final List<INaviModule> modules = addressSpace.getContent().getModules();
// functions that are not forwarded.
for (final INaviModule module : modules) {
final CCallgraph callgraph = module.getContent().getNativeCallgraph();
for (final ICallgraphNode callgraphNode : callgraph) {
final INaviFunction function = callgraphNode.getFunction();
final INaviFunction resolvedFunction = getResolvedFunction(function, modules);
if (resolvedFunction == null) {
final CFunctionNode node = view.getContent().createFunctionNode(function);
node.setColor(CFunctionNodeColorizer.getFunctionColor(function.getType()));
nodeMap.put(function, node);
} else {
resolvedMap.put(function, resolvedFunction);
}
}
}
// for the forwarded functions.
for (final INaviModule module : modules) {
final CCallgraph callgraph = module.getContent().getNativeCallgraph();
for (final ICallgraphEdge callgraphEdge : callgraph.getEdges()) {
final INaviFunction source = resolvedMap.containsKey(callgraphEdge.getSource().getFunction()) ? resolvedMap.get(callgraphEdge.getSource().getFunction()) : callgraphEdge.getSource().getFunction();
final INaviFunction target = resolvedMap.containsKey(callgraphEdge.getTarget().getFunction()) ? resolvedMap.get(callgraphEdge.getTarget().getFunction()) : callgraphEdge.getTarget().getFunction();
view.getContent().createEdge(nodeMap.get(source), nodeMap.get(target), EdgeType.JUMP_UNCONDITIONAL);
}
}
return view;
}
Aggregations