com.ibm.wala.ipa.callgraph.CGNode Java Examples

The following examples show how to use com.ibm.wala.ipa.callgraph.CGNode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CallGraphAnalyzer.java    From fasten with Apache License 2.0 6 votes vote down vote up
/**
 * Iterate over nodes in Wala call graph and add calls that "belong" to application class
 * loader to lists of resolved / unresolved calls of partial call graph.
 */
public void resolveCalls() {
    for (final CGNode node : this.rawCallGraph) {
        final var nodeReference = node.getMethod().getReference();

        if (applicationClassLoaderFilter.test(node)) {
            continue;
        }

        final var methodNode = analysisContext.findOrCreate(nodeReference);

        for (final var callSites = node.iterateCallSites(); callSites.hasNext(); ) {
            final var callSite = callSites.next();

            final var targetWithCorrectClassLoader =
                    correctClassLoader(callSite.getDeclaredTarget());

            final var targetMethodNode =
                    analysisContext.findOrCreate(targetWithCorrectClassLoader);

            addCall(methodNode, targetMethodNode, getInvocationLabel(callSite));
        }

    }
}
 
Example #2
Source File: ArtifactResolverTest.java    From fasten with Apache License 2.0 6 votes vote down vote up
@Test
void findJarFileUsingMethod() throws IOException {
    ArtifactResolver artifactResolver = new ArtifactResolver(graph.getClassHierarchy());
    JarFile jarFile = new JarFile(jar);

    CGNode correctClassLoaderNode = null;

    for (CGNode node : graph) {

        if (!node.getMethod()
                .getDeclaringClass()
                .getClassLoader()
                .getReference()
                .equals(ClassLoaderReference.Application)) {
            continue;
        }

        correctClassLoaderNode = node;
        break;
    }

    assertNotNull(correctClassLoaderNode);
    assertEquals(jarFile.getName(), artifactResolver
            .findJarFileUsingMethod(correctClassLoaderNode.getMethod().getReference()).getName());
}
 
Example #3
Source File: WalaCallgraphConstructor.java    From steady with Apache License 2.0 6 votes vote down vote up
/**
 * check whether all entrypoints are existing in callgraph
 *
 * @throws CallgraphConstructException
 */
private void checkEntrypoints(boolean _policy) throws CallgraphConstructException {
    HashSet<com.sap.psr.vulas.shared.json.model.ConstructId> ep_diff = new HashSet<com.sap.psr.vulas.shared.json.model.ConstructId>();
    com.sap.psr.vulas.shared.json.model.ConstructId cid = null;
    ep_diff.addAll(this.filteredEP);
    for (CGNode node : this.callgraph.getEntrypointNodes()) {
        cid = getCid(node.getMethod());
        ep_diff.remove(cid);
    }
    if (_policy && (!ep_diff.isEmpty()))
        throw new CallgraphConstructException("Strict policy applied; terminating as there are [" + ep_diff.size() + "] entry points missing in call graph", null);
    if (ep_diff.size() == this.filteredEP.size())
        throw new CallgraphConstructException("[0/" + ep_diff.size() + "] entry points found in call graph", null);

    if ((!_policy) && (!ep_diff.isEmpty())) {
        WalaCallgraphConstructor.log.warn("There should be [" + this.filteredEP.size() + "] entrypoints set; but [" +
                ep_diff.size() + "] entrypoints are missing in the call graph");
        for (com.sap.psr.vulas.shared.json.model.ConstructId m : ep_diff)
            WalaCallgraphConstructor.log.warn("  [" + m.getQname() + "] is missing");
    } else {
        WalaCallgraphConstructor.log.info("All [" + this.filteredEP.size() + "] entrypoints exist in the call graph");
    }
}
 
Example #4
Source File: WalaUtils.java    From LibScout with Apache License 2.0 6 votes vote down vote up
public static CGNode getCGNode(IMethod method, CallGraph cg) {
	logger.debug("Retrieve CGNode for " + method.getSignature());

	MethodReference ref = method.getReference();
	if (ref == null) return null;

	Set<CGNode> cgnode = cg.getNodes(ref);
	if (cgnode.isEmpty()) {
		logger.warn("Number of CGNode(s) for " + method.getSignature() + " is " + cgnode.size());
		return null;
	}
	/*else if (cgnode.size() > 1) {
		logger.warn("Number of CGNode(s) for " + methodSignature + " is " + cgnode.size() +  "  refMethod.sig: " + refMethod.getSignature());
	}*/
					
	return cgnode.iterator().next();
}
 
Example #5
Source File: MethodTest.java    From fasten with Apache License 2.0 5 votes vote down vote up
@Test
public void equalsTest() {
    final var analysisContext = new AnalysisContext(ssttgraph.getClassHierarchy());

    List<Method> methods = new ArrayList<>();

    for (final CGNode node : ssttgraph) {
        final var nodeReference = node.getMethod().getReference();
        methods.add(analysisContext.findOrCreate(nodeReference));
    }


    final var refMethod = methods.get(3);
    final var methodSameNamespaceDiffSymbol = methods.get(5);
    final var methodDiffNamespaceDiffSymbol = methods.get(0);
    final var methodDiffNamespaceSameSymbol = methods.get(7);
    final var methodSameReference = new InternalMethod(refMethod.getReference(), null);


    assertEquals(refMethod, refMethod);
    assertEquals(refMethod, methodSameReference);
    assertNotEquals(refMethod, null);
    assertNotEquals(refMethod, new Object());
    assertNotEquals(refMethod, methodSameNamespaceDiffSymbol);
    assertNotEquals(refMethod, methodDiffNamespaceDiffSymbol);
    assertNotEquals(refMethod, methodDiffNamespaceSameSymbol);
}
 
Example #6
Source File: WalaCallgraphConstructor.java    From steady with Apache License 2.0 5 votes vote down vote up
/**
 * Normalizing a wala callgraph to a general graph represented by ConstructId
 *
 * @return a {@link com.ibm.wala.util.graph.Graph} object.
 */
public Graph<com.sap.psr.vulas.shared.json.model.ConstructId> getCallgraph() {
    Graph<com.sap.psr.vulas.shared.json.model.ConstructId> graph = SlowSparseNumberedGraph.make();

    if (this.callgraph != null) {
        int edges_no = 0;
        Iterator<CGNode> nodes = this.callgraph.iterator();
        CGNode srcnode = null, tgtnode = null;
        com.sap.psr.vulas.shared.json.model.ConstructId src_cid = null, tgt_cid = null;
        Iterator<CGNode> succNodes = null;
        while (nodes.hasNext()) {
            srcnode = nodes.next();
            src_cid = getCid(srcnode.getMethod());
            graph.addNode(src_cid);
            //add edges
            succNodes = this.callgraph.getSuccNodes(srcnode);
            while (succNodes.hasNext()) {
                tgtnode = succNodes.next();
                tgt_cid = getCid(tgtnode.getMethod());
                graph.addNode(tgt_cid);
                if (!graph.hasEdge(src_cid, tgt_cid)) {
                    graph.addEdge(src_cid, tgt_cid);
                    edges_no++;
                }
            }
        }
        WalaCallgraphConstructor.log.info("Normalized call graph has [" + graph.getNumberOfNodes() + " nodes] (with distinct ConstructId) and [" + edges_no + " edges]");
    }
    // No callgrpah exists
    else {
        throw new IllegalStateException("There exists no call graph");
    }
    return graph;
}
 
Example #7
Source File: WalaUtils.java    From LibScout with Apache License 2.0 5 votes vote down vote up
public static CGNode getCGNode(String methodSignature, CallGraph cg) {
	logger.debug("Retrieve CGNode for " + methodSignature);

	IMethod refMethod = getIMethod(cg.getClassHierarchy(), methodSignature);
	if (refMethod == null) return null;

	return getCGNode(refMethod, cg);
}
 
Example #8
Source File: WalaUtils.java    From LibScout with Apache License 2.0 4 votes vote down vote up
public static IR getIR(IMethod method, CallGraph cg) {
	CGNode node = getCGNode(method, cg); 
	return node == null? null : node.getIR();
}
 
Example #9
Source File: WalaUtils.java    From LibScout with Apache License 2.0 4 votes vote down vote up
public static IR getIR(String methodSignature, CallGraph cg) {
	CGNode node = getCGNode(methodSignature, cg); 
	return node == null? null : node.getIR();
}