Skip to content

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(
    at soot.coffi.CFG.jimplify(
    at com.LiveVariableAnalysis.main(

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);"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;
        }"Loading method_info: %s ...", methodInfo.toName(mainClassFile.constant_pool)));

        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);"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);
        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.


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.