Java源码示例:soot.jimple.toolkits.callgraph.CallGraph
示例1
@Override
protected void internalTransform(String phaseName, Map option) {
Map<String, String> cgMap = new HashMap<String, String>();
cgMap.put("enabled", "true");
cgMap.put("apponly", "true");
List<SootMethod> entryMthds = new ArrayList<SootMethod>();
for (SootClass sootClass : Scene.v().getApplicationClasses()) {
if (entryClses.contains(sootClass.getName())) {// entry class
for (SootMethod method : sootClass.getMethods()) {
entryMthds.add(method);
}
}
}
Scene.v().setEntryPoints(entryMthds);
CHATransformer.v().transform("wjtp", cgMap);
CallGraph cg = Scene.v().getCallGraph();
Iterator<Edge> ite = cg.iterator();
riskMthds = getRiskMthds();
while (ite.hasNext()) {
Edge edge = ite.next();
if (isRiskCall(edge)) {
riskRlts.add(new MethodCall(edge.src().getSignature(), edge.tgt().getSignature()));
}
}
}
示例2
public Graph getGraph() {
Set<Node> nds = new HashSet<Node>();
List<MethodCall> calls = new ArrayList<MethodCall>();
// form calls and nds
CallGraph cg = Scene.v().getCallGraph();
if (cg != null) {
Iterator<Edge> ite = cg.iterator();
while (ite.hasNext()) {
Edge edge = ite.next();
if (Conf.FLT_INTERFACE) {
if (edge.kind().name().equals("INTERFACE"))
continue;
}
String srcClsName = edge.src().getDeclaringClass().getName();
String tgtClsName = edge.tgt().getDeclaringClass().getName();
if (entryClses.contains(tgtClsName)) {
// edge to entry-jar
} else if (conflictJarClses.contains(srcClsName)) {
// edge from conflict-jar
} else {
String tgtMthdName = edge.tgt().getSignature();
String srcMthdName = edge.src().getSignature();
calls.add(new MethodCall(srcMthdName, tgtMthdName));
nds.add(new Node(srcMthdName, entryClses.contains(srcClsName)));
nds.add(new Node(tgtMthdName, entryClses.contains(tgtClsName)));
}
}
}
return new Graph(nds, calls);
}
示例3
public static void main(String[] args) {
List<String> argsList = new ArrayList<String>(Arrays.asList(args));
argsList.addAll(Arrays.asList(new String[]{
"-w",
"-main-class",
"testers.CallGraphs",//main-class
"testers.CallGraphs",//argument classes
"testers.A" //
}));
PackManager.v().getPack("wjtp").add(new Transform("wjtp.myTrans", new SceneTransformer() {
@Override
protected void internalTransform(String phaseName, Map options) {
CHATransformer.v().transform();
SootClass a = Scene.v().getSootClass("testers.A");
SootMethod src = Scene.v().getMainClass().getMethodByName("doStuff");
CallGraph cg = Scene.v().getCallGraph();
Iterator<MethodOrMethodContext> targets = new Targets(cg.edgesOutOf(src));
while (targets.hasNext()) {
SootMethod tgt = (SootMethod)targets.next();
System.out.println(src + " may call " + tgt);
}
}
}));
args = argsList.toArray(new String[0]);
soot.Main.main(args);
}
示例4
public CallGraph getCallGraph()
{
if(!hasCallGraph()) {
throw new RuntimeException( "No call graph present in Scene. Maybe you want Whole Program mode (-w)." );
}
return activeCallGraph;
}
示例5
public static int countCallEdgesForCallsite(Stmt callsite, boolean stopForMutiple)
{
CallGraph cg = Scene.v().getCallGraph();
int count = 0;
for ( Iterator<Edge> it = cg.edgesOutOf(callsite);
it.hasNext(); ) {
it.next();
++count;
if ( stopForMutiple && count > 1) break;
}
return count;
}
示例6
protected HashMutableDirectedGraph<EquivalentValue> getInvokeInfoFlowSummary(
InvokeExpr ie, Stmt is, SootMethod context)
{
// get the data flow graph for each possible target of ie,
// then combine them conservatively and return the result.
HashMutableDirectedGraph<EquivalentValue> ret = null;
SootMethodRef methodRef = ie.getMethodRef();
String subSig = methodRef.resolve().getSubSignature();
CallGraph cg = Scene.v().getCallGraph();
for(Iterator<Edge> edges = cg.edgesOutOf(is); edges.hasNext();)
{
Edge e = edges.next();
SootMethod target = e.getTgt().method();
// Verify that this target is an implementation of the method we intend to call,
// and not just a class initializer or other unintended control flow.
if(target.getSubSignature().equals(subSig))
{
HashMutableDirectedGraph<EquivalentValue> ifs = getMethodInfoFlowSummary(
target, context.getDeclaringClass().isApplicationClass());
if(ret == null)
ret = ifs;
else
{
for(EquivalentValue node : ifs.getNodes())
{
if(!ret.containsNode(node))
ret.addNode(node);
for(EquivalentValue succ : ifs.getSuccsOf(node))
ret.addEdge(node, succ);
}
}
}
}
return ret;
// return getMethodInfoFlowSummary(methodRef.resolve(), context.getDeclaringClass().isApplicationClass());
}
示例7
public MultiRunStatementsFinder(UnitGraph g, SootMethod sm, Set<SootMethod> multiCalledMethods, CallGraph cg)
{
super(g);
nodeToIndex = new HashMap<Object, Integer>();
// System.out.println("===entering MultiObjectAllocSites==");
doAnalysis();
//testMultiObjSites(sm);
findMultiCalledMethodsIntra(multiCalledMethods, cg);
// testMultiObjSites(sm);
}
示例8
private void byMCalledS0(PegCallGraph pcg) {
Iterator it = pcg.iterator();
while (it.hasNext()){
SootMethod sm = (SootMethod)it.next();
UnitGraph graph = new CompleteUnitGraph(sm.getActiveBody());
CallGraph callGraph = Scene.v().getCallGraph();
MultiRunStatementsFinder finder = new MultiRunStatementsFinder(graph, sm, multiCalledMethods, callGraph);
FlowSet fs = finder.getMultiRunStatements();
}
}
示例9
public CallGraph getCallGraphCopy() {
CallGraph copy = new CallGraph();
HashSet<SootMethod> visited = new HashSet<>();
for (SootMethod entryPoint : Scene.v().getEntryPoints()) {
if (!visited.contains(entryPoint)) {
addEdgesForCallees(entryPoint, visited, copy);
}
}
return copy;
}
示例10
private void addEdgesForCallees(SootMethod sootMethod, HashSet<SootMethod> visited, CallGraph copy) {
visited.add(sootMethod);
for (Unit callsite : precomputedGraph.getCallsFromWithin(sootMethod)) {
for (SootMethod callee : precomputedGraph.getCalleesOfCallAt(callsite)) {
copy.addEdge(new Edge(sootMethod, (Stmt) callsite, callee));
if (!visited.contains(callee)) {
addEdgesForCallees(callee, visited, copy);
}
}
}
}
示例11
public CallGraph getCallGraphCopy() {
CallGraph copy = new CallGraph();
for (Edge edge : demandDrivenCallGraph) {
Edge edgeCopy = new Edge(edge.src(), edge.srcUnit(), edge.tgt(), edge.kind());
copy.addEdge(edgeCopy);
}
return copy;
}
示例12
@Override
public void resetCallGraph() {
demandDrivenCallGraph = new CallGraph();
numberOfEdgesTakenFromPrecomputedCallGraph = 0;
unbalancedMethods.clear();
calleeListeners.clear();
callerListeners.clear();
}
示例13
public void setCallGraph(CallGraph cg)
{
reachableMethods = null;
activeCallGraph = cg;
}
示例14
/**
* Report the virtual callsites resolution result for the user's code.
*/
public void checkCallGraph()
{
int[] limits = new int[] { 1, 2, 4, 8 };
evalRes.total_call_edges = new Histogram(limits);
CallGraph cg = Scene.v().getCallGraph();
for ( Stmt callsite : ptsProvider.multiCallsites ) {
Iterator<Edge> edges = cg.edgesOutOf(callsite);
if ( !edges.hasNext() ) continue;
evalRes.n_callsites++;
// get an edge
Edge anyEdge = edges.next();
SootMethod src = anyEdge.src();
if ( !ptsProvider.isReachableMethod(src) ||
!ptsProvider.isValidMethod(src) )
continue;
// get the base pointer
CgEdge p = ptsProvider.getInternalEdgeFromSootEdge(anyEdge);
LocalVarNode vn = (LocalVarNode)p.base_var;
// test the call graph
int edge_cnt = 1;
while ( edges.hasNext() ) {
++edge_cnt;
edges.next();
}
evalRes.n_geom_call_edges += edge_cnt;
if ( edge_cnt == 1 ) ++evalRes.n_geom_solved_all;
// test app method
if ( !src.isJavaLibraryMethod() ) {
InvokeExpr ie = callsite.getInvokeExpr();
if ( edge_cnt == 1 ) {
++evalRes.n_geom_solved_app;
if ( ptsProvider.getOpts().verbose() ) {
outputer.println();
outputer.println("<<<<<<<<< Additional Solved Call >>>>>>>>>>");
outputer.println(src.toString());
outputer.println(ie.toString());
}
} else {
// We try to test if this callsite is solvable
// under some contexts
Histogram call_edges = new Histogram(limits);
test_1cfa_call_graph(vn, src, ie.getMethod(), call_edges);
evalRes.total_call_edges.merge(call_edges);
call_edges = null;
}
evalRes.n_geom_user_edges += edge_cnt;
evalRes.n_user_callsites++;
}
}
ptsProvider.ps.println();
ptsProvider.ps.println("--------> Virtual Callsites Evaluation <---------");
ptsProvider.ps.printf("Total virtual callsites (app code): %d (%d)\n",
evalRes.n_callsites, evalRes.n_user_callsites);
ptsProvider.ps.printf("Total virtual call edges (app code): %d (%d)\n",
evalRes.n_geom_call_edges, evalRes.n_geom_user_edges);
ptsProvider.ps.printf("Virtual callsites additionally solved by geomPTA compared to SPARK (app code) = %d (%d)\n",
evalRes.n_geom_solved_all, evalRes.n_geom_solved_app);
evalRes.total_call_edges.printResult(ptsProvider.ps,
"Testing of unsolved callsites on 1-CFA call graph: ");
if (ptsProvider.getOpts().verbose())
ptsProvider.outputNotEvaluatedMethods();
}
示例15
/**
* Computes the set of {@link EquivalentValue}s of all field references that are used
* in this method but not set by the method or any method transitively called by this method.
*/
private Set<Value> trackableFields() {
Set<Value> usedFieldRefs = new HashSet<Value>();
//add all field references that are in use boxes
for (Unit unit : this.graph) {
Stmt s = (Stmt) unit;
List<ValueBox> useBoxes = s.getUseBoxes();
for (ValueBox useBox : useBoxes) {
Value val = useBox.getValue();
if(val instanceof FieldRef) {
FieldRef fieldRef = (FieldRef) val;
if(fieldRef.getType() instanceof RefLikeType)
usedFieldRefs.add(new EquivalentValue(fieldRef));
}
}
}
//prune all fields that are written to
if(!usedFieldRefs.isEmpty()) {
if(!Scene.v().hasCallGraph()) {
throw new IllegalStateException("No call graph found!");
}
CallGraph cg = Scene.v().getCallGraph();
ReachableMethods reachableMethods = new ReachableMethods(cg,Collections.<MethodOrMethodContext>singletonList(container));
reachableMethods.update();
for (Iterator<MethodOrMethodContext> iterator = reachableMethods.listener(); iterator.hasNext();) {
SootMethod m = (SootMethod) iterator.next();
if(m.hasActiveBody() &&
//exclude static initializer of same class (assume that it has already been executed)
!(m.getName().equals(SootMethod.staticInitializerName) && m.getDeclaringClass().equals(container.getDeclaringClass()))) {
for (Unit u : m.getActiveBody().getUnits()) {
List<ValueBox> defBoxes = u.getDefBoxes();
for (ValueBox defBox : defBoxes) {
Value value = defBox.getValue();
if(value instanceof FieldRef) {
usedFieldRefs.remove(new EquivalentValue(value));
}
}
}
}
}
}
return usedFieldRefs;
}
示例16
@Override
public CallGraph getCallGraphCopy() {
return delegate.getCallGraphCopy();
}
示例17
@Override
protected void internalTransform(String phaseName, @SuppressWarnings("rawtypes") Map options) {
PropagationTimers.v().totalTimer.start();
PropagationTimers.v().misc.start();
StringValueAnalysis.initialize();
JimpleBasedInterproceduralCFG iCfg = new PropagationIcfg();
AnalysisParameters.v().setIcfg(iCfg);
PropagationProblem problem = new PropagationProblem(iCfg);
for (SootMethod ep : Scene.v().getEntryPoints()) {
if (ep.isConcrete()) {
problem.getInitialSeeds().add(ep.getActiveBody().getUnits().getFirst());
}
}
int iterationCounter = 0;
PropagationSolver solver = null;
while (iterationCounter < MAX_ITERATIONS) {
IterationSolver.v().initialize(solver);
PropagationTimers.v().misc.end();
PropagationTimers.v().problemGeneration.start();
solver = new PropagationSolver(problem);
PropagationTimers.v().problemGeneration.end();
PropagationTimers.v().ideSolution.start();
logger.info("Solving propagation problem (iteration " + iterationCounter + ")");
solver.solve();
PropagationTimers.v().ideSolution.end();
PropagationTimers.v().misc.start();
if (!AnalysisParameters.v().isIterative() || IterationSolver.v().hasFoundFixedPoint()) {
iterationCounter = MAX_ITERATIONS;
} else {
++iterationCounter;
}
}
PropagationTimers.v().misc.end();
logger.info("Reached a fixed point");
Results.addResult(resultBuilder.buildResult(solver));
PropagationTimers.v().totalTimer.end();
if (logger.isDebugEnabled()) {
CallGraph cg = Scene.v().getCallGraph();
Iterator<Edge> it = cg.listener();
while (it.hasNext()) {
soot.jimple.toolkits.callgraph.Edge e = (soot.jimple.toolkits.callgraph.Edge) it.next();
logger.debug("" + e.src() + e.srcStmt() + " =" + e.kind() + "=> " + e.tgt());
}
if (printer != null) {
printer.print(solver);
}
}
}
示例18
public SootMethod appendNonComponents(SootMethod mainMethod)
{
Set<String> coveredMethods = new HashSet<String>();
CallGraph cg = Scene.v().getCallGraph();
for (Iterator<Edge> iter = cg.iterator(); iter.hasNext(); )
{
Edge edge = iter.next();
coveredMethods.add(edge.src().getSignature());
coveredMethods.add(edge.tgt().getSignature());
}
Chain<SootClass> sootClasses = Scene.v().getApplicationClasses();
for (Iterator<SootClass> iter = sootClasses.iterator(); iter.hasNext();)
{
SootClass sc = iter.next();
List<SootMethod> methodList = sc.getMethods();
for (SootMethod sm : methodList)
{
if (sm.getDeclaringClass().getName().startsWith("android.support"))
{
continue;
}
if (sc.isPhantom() || ! sm.isConcrete())
{
continue;
}
if (sm.getName().equals("<init>") || sm.getName().equals("<clinit>"))
{
continue;
}
if (coveredMethods.contains(sm.getSignature()))
{
//Already covered.
continue;
}
mainMethod = addMethod(mainMethod, sm.getSignature());
}
}
return mainMethod;
}
示例19
CallGraph getCallGraphCopy();