[Soot-list] One-way Call Graph from APK.

林松駿 80625jer at gmail.com
Tue Jun 2 09:03:58 EDT 2015


Hello all,

i using soot to build the call graph

because i need the call graph about apk file for my research

i have using this pages code 
https://groups.google.com/forum/#!searchin/soot-list/apk$20call$20graph/soot-list/pBLFCnoQeeo/oU6ua1GbiJEJ

but i can't fix the error at message 18

can anyone help me? 

thx

error info

Exception in thread "main" java.lang.NullPointerException
at test.DotGraph.getNode(DotGraph.java:58)
at test.DotGraph.drawNode(DotGraph.java:67)
at test.a.visit(a.java:125)
at test.a.main(a.java:104)




mycode
---------------------------
title a.java

package test;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import soot.MethodOrMethodContext;
import soot.PackManager;
import soot.Scene;
import soot.SootClass;
import soot.SootMethod;
import soot.jimple.infoflow.android.SetupApplication;
import soot.jimple.toolkits.callgraph.CHATransformer;
import soot.jimple.toolkits.callgraph.CallGraph;
import soot.jimple.toolkits.callgraph.Targets;
import soot.options.Options;
import soot.util.dot.DotGraphUtility;
import soot.util.dot.Renderable;
import soot.util.dot.DotGraphNode;

public class a {

private static DotGraph dot = new DotGraph("CallGraph");
public static HashMap <String,Boolean> visited = new 
HashMap<String,Boolean>();
public a() {

// TODO Auto-generated constructor stub

}

public static void main(String[] args) {

// TODO Auto-generated method stub

SetupApplication app = new 
SetupApplication("c:/sdk/platforms","c:/apk/recognizer.apk");

try {

app.calculateSourcesSinksEntrypoints("C:/call graph test/soot 
workspace/soot-test/SourcesAndSinks.txt");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (XmlPullParserException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

soot.G.reset();

Options.v().set_src_prec(Options.src_prec_apk);

Options.v().set_process_dir(Collections.singletonList("c:/apk/recognizer.apk"));

Options.v().set_android_jars("c:/sdk/platforms");

Options.v().set_whole_program(true);

Options.v().set_allow_phantom_refs(true);

Options.v().set_output_format(Options.output_format_none);

Options.v().setPhaseOption("cg", "on");

Scene.v().loadNecessaryClasses();

SootMethod entryPoint = app.getEntryPointCreator().createDummyMain();

Options.v().set_main_class(entryPoint.getSignature());

Scene.v().setEntryPoints(Collections.singletonList(entryPoint));

System.out.println("............"+entryPoint.getActiveBody());

PackManager.v().runPacks();

System.out.println();

System.out.println(Scene.v().getCallGraph().listener());

System.out.println("777"+entryPoint+"777");


CallGraph cg = Scene.v().getCallGraph();



visit(cg,entryPoint);
dot.plot("C:/"+ dot.DOT_EXTENSION);

}





private static void visit(CallGraph cg, SootMethod k)
{
String identifier = k.getName();
System.out.println("identifier:"+identifier);
 if(k.getName()!=k.getSignature())
System.out.println("yes111");
 visited.put(k.getSignature(),true); 
System.out.println("getSignature():"+k.getSignature());
System.out.println("visited:"+visited);
 dot.drawNode(identifier);
 //iterate over unvisited parents
Iterator<MethodOrMethodContext> ptargets = new Targets(cg.edgesInto(k));
 if(ptargets != null){
while(ptargets.hasNext())
{
SootMethod p = (SootMethod) ptargets.next();
 if(p == null) System.out.println("p is null");
 if(!visited.containsKey(p.getSignature()))
visit(cg,p);
}
}
 //iterate over unvisited children
Iterator<MethodOrMethodContext> ctargets = new Targets(cg.edgesOutOf(k));
 if(ctargets != null){
while(ctargets.hasNext())
{
SootMethod c = (SootMethod) ctargets.next();
if(c == null) System.out.println("c is null");
dot.drawEdge(identifier, c.getName());
 if(!visited.containsKey(c.getSignature()))
visit(cg,c);
}
}
}

}

-----

package test;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import soot.util.dot.DotGraphEdge;
import soot.util.dot.DotGraphNode;
import soot.util.dot.DotGraphUtility;
import soot.util.dot.Renderable;
import soot.util.dot.DotGraphNode;

public class DotGraph implements Renderable
{
public final static String DOT_EXTENSION = ".dot";
private HashMap <String,DotGraphNode> nodes;
private boolean isSubGraph;
private List<Renderable> drawElements;
private String graphname;
public DotGraph(String graphname)
{
this.drawElements = new LinkedList<Renderable>();
}
public DotGraph createSubGraph(String label)
{
DotGraph subgraph = new DotGraph(label);
subgraph.isSubGraph = true;
this.drawElements.add(subgraph);
System.out.println(subgraph);
return subgraph;
}
@Override
public void render(OutputStream out, int indent) throws IOException {
String graphname = this.graphname;
    if (!isSubGraph) {
      DotGraphUtility.renderLine(out, "digraph \""+graphname+"\" {", 
indent);
    } else {
 DotGraphUtility.renderLine(out, "subgraph \""+graphname+"\" {", indent);

    }
}
public void plot(String filename) {
    try {
      BufferedOutputStream out =new BufferedOutputStream(new 
FileOutputStream(filename));
      render(out, 0);
      out.close();
    } catch (IOException ioe) {
    }
  }
public DotGraphNode getNode(String name){
System.out.println("nodes:"+nodes);
System.out.println("name:"+name);
DotGraphNode node = nodes.get(name);
if (node == null) {
node = new DotGraphNode(name);
nodes.put(name, node);
}
return node;
}
public DotGraphNode drawNode(String name){
DotGraphNode node = getNode(name);
if(node == null) 
throw new RuntimeException("Assertion failed."); 
if(!this.drawElements.contains(node)) 
this.drawElements.add(node);
return node;
  }

public DotGraphEdge drawEdge(String from, String to) {
DotGraphNode src = drawNode(from);
DotGraphNode dst = drawNode(to);
DotGraphEdge edge = new DotGraphEdge(src, dst);
this.drawElements.add(edge);
return edge;

  }
} 



-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://mailman.CS.McGill.CA/pipermail/soot-list/attachments/20150602/b76ef520/attachment.html 


More information about the Soot-list mailing list