在JAVA中使用Soot和Tamiflex进行反射调用

时间:2017-02-15 07:03:51

标签: java static-analysis soot

我试图在具有反射类的java程序上看到vta的Call Graph的结果。为此,我使用Tamiflex来解析java程序中的所有反射调用。输出类文件,然后用作调用图分析的参数。这样做会导致以下异常。

Exception in thread "main" java.lang.RuntimeException
    at soot.jimple.spark.pag.MethodPAG.addToPAG(MethodPAG.java:62)
    at soot.jimple.spark.builder.ContextInsensitiveBuilder.build(ContextInsensitiveBuilder.java:91)
    at soot.jimple.spark.SparkTransformer.internalTransform(SparkTransformer.java:84)
    at soot.SceneTransformer.transform(SceneTransformer.java:39)
    at RTAMeasures$1.setSparkAnalysis(RTAMeasures.java:105)
    at RTAMeasures$1.internalTransform(RTAMeasures.java:57)
    at soot.SceneTransformer.transform(SceneTransformer.java:39)
    at soot.Transform.apply(Transform.java:89)
    at soot.ScenePack.internalApply(ScenePack.java:43)
    at soot.Pack.apply(Pack.java:114)
    at soot.PackManager.runWholeProgramPacks(PackManager.java:418)
    at soot.PackManager.runPacks(PackManager.java:336)
    at soot.Main.run(Main.java:198)
    at soot.Main.main(Main.java:141)
    at RTAMeasures.main(RTAMeasures.java:111)

我用于使用Tamiflex的命令:

  

java -javaagent:poa-2.0.3.jar ReflectionDemo

然后我使用了以下命令。

  

java -cp soot-2.5.0.jar soot.Main -w -app -allow-phantom-refs -p   cg.spark enabled -cp   $ JAVA_HOME / lib中/ rt.jar中:$ JAVA_HOME / lib目录/ jce.jar:出:. -包括   org.apache。 - 包括org.w3c。 -main-class ReflectionDemo SomeClass   ReflectionDemo

sootOuptut目录中的类文件用作打印调用图边缘的程序的输入。

JAVA中的反射示例。 --------------------------- ReflectionDemo.java -------------------- ------------------------------

import java.lang.reflect.*;
public class ReflectionDemo {

  public static void main(String[] args) throws ClassNotFoundException,
    NoSuchMethodException, SecurityException, InstantiationException,
    IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        // TODO Auto-generated method stub

        Class c = Class.forName("SomeClass");
        Method m = c.getDeclaredMethod("SomeMethod");

        Object o = c.newInstance();
        m.invoke(o, null);
    }

}

--------------------------- SomeClass.java ----------------- ---------------------------------

public class SomeClass {

    public SomeClass(){}

    public void SomeMethod(){
        System.out.println("Some Method is called ");
    }
}

分析代码。

--------------------------- RTAMeasures.java ----------------- ---------------------------------

public class RTAMeasures {
    public static void main(String[] args) {
        List<String> argList = new ArrayList<String>(Arrays.asList(args));
        argList.addAll(Arrays.asList(new String[]{
                    "-w",
"-cp","/home/mandar/MTP/Impl/classes/sootOutput",
                    "-allow-phantom-refs",
                    "-main-class",
                    "ReflectionDemo",
                    "ReflectionDemo"
                    }));



        PackManager.v().getPack("wjtp").add(new Transform("wjtp.myTrans",
                new SceneTransformer() {

            @Override
            protected void internalTransform(String arg0, Map arg1) {

                HashMap<Integer,Integer> outMap = new HashMap<Integer,Integer>();
                Scene.v().loadNecessaryClasses();

                setSparkAnalysis();


                CallGraph cg = Scene.v().getCallGraph();
                System.out.println("Call graph size : "+cg.size());

                Iterator methods = cg.sourceMethods();

                PrintStream out = null;
                try {
                    out = new PrintStream(new FileOutputStream("logs/vta.txt"));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

                Iterator<SootClass> itc = Scene.v().getClasses().iterator();
                while(itc.hasNext()){
                    SootClass sC = (SootClass)itc.next();
                    out.println("\t\tClass Name:  " + sC.getName());

                    Iterator<SootMethod> methodIt = sC.getMethods().iterator();

                    while(methodIt.hasNext()){
                        SootMethod m = (SootMethod) methodIt.next();
                        out.println("\t\tMethod Name: "+m.getName());
                        if(!m.isJavaLibraryMethod() && !m.isConstructor()){

                            Iterator<MethodOrMethodContext> targets = new Targets(cg.edgesOutOf(m));
                            while (targets.hasNext()) {
                                   SootMethod tgt = (SootMethod)targets.next();
                                   out.println(m + " may call " + tgt);
                            }
                        }
                    }
                }
            }

            private void setSparkAnalysis() {
                HashMap<String,String> opt = new HashMap<String,String>();
                opt.put("enabled","true");
                opt.put("verbose","false");
                opt.put("vta","true");
                opt.put("on-fly-cg","true");
                opt.put("set-impl","double");
                opt.put("double-set-old","hybrid");
                opt.put("propagator", "iter");
                opt.put("double-set-new","hybrid");

                SparkTransformer.v().transform("",opt);
            }
        }));

        args = argList.toArray(new String[0]);

        soot.Main.main(args);
    }
}

请让我知道我做错了什么步骤。我是第一次使用Tamiflex。

提前致谢。

此致

Mandar。

0 个答案:

没有答案