javastatic-analysiscode-analysiscontrol-flow-graphsoot

Soot - Get JimpleBody from a CFG


I'd like to get UnitGraph from a Java Class. I load it by ClassFile and get the method_info of main(). Then I create a CFG and try to convert it into a UnitGraph. My method is to get JimpleBody of the CFG and then create a UnitGraph. However, I can't get JimpleBody by invoking cfg.jimplify(...) since it throws the following error:

Exception in thread "main" java.lang.RuntimeException: no method associated w/ body
    at soot.Body.getMethod(Body.java:137)
    at soot.coffi.CFG.jimplify(CFG.java:814)
    at com.LiveVariableAnalysis.main(LiveVariableAnalysis.java:151)

My code is as follows:

        String mainClassName = "Calculate";
        String mainClassPath = String.format("./target/test-classes/%s.class", mainClassName);
        ClassFile mainClassFile = new ClassFile(mainClassName);
        FileInputStream is = new FileInputStream(mainClassPath);
        mainClassFile.loadClassFile(is);
        logger.info(String.format("Loading Class: %s ...", mainClassFile));

        method_info methodInfo = null;
        for (method_info method: mainClassFile.methods) {
            if (Objects.equals(method.toName(mainClassFile.constant_pool), "main")) {
                methodInfo = method;
            }
        }
        logger.info(String.format("Loading method_info: %s ...", methodInfo.toName(mainClassFile.constant_pool)));

        mainClassFile.parseMethod(methodInfo);
        CFG cfg = new CFG(methodInfo);

        JimpleBody jimpleBody = new JimpleBody();
        // Error occurs here
        cfg.jimplify(mainClassFile.constant_pool, mainClassFile.this_class, mainClassFile.bootstrap_methods_attribute, jimpleBody);

        UnitGraph unitGraph = new ClassicCompleteUnitGraph(jimpleBody);
        logger.info(String.format("Creating unitGraph with %d units ...", unitGraph.size()));

I know there are other ways to create a UnitGraph, such as:

        String mainClassName = "Calculate";
        SootClass mainClass = Scene.v().loadClassAndSupport(className);
        Scene.v().setMainClass(mainClass);
        soot.Main.main(args);
        SootClass mainClass = Scene.v().getMainClass();
        String methodSignature = "void main(java.lang.String[])";
        SootMethod mainMethod = mainClass.getMethod(methodSignature);
        Body jimpleBody = mainMethod.retrieveActiveBody();
        UnitGraph unitGraph = new ClassicCompleteUnitGraph(jimpleBody);

However, in this way, I need to set Scene.v().setSootClassPath(path) for jce.jar and rt.jar, which I don't want to occur in my code. So if there is another way I can get UnitGraph without setting such a path, please help me.


Solution

  • Although I still can't turn method_info into a SootMathod and then get a UnitGraph, I can use Options.v().set_prepend_classpath(true) to avoid set jce.jar and rt.jar dirctly. This also achieves my goal.