烟灰 - 从CFG获取Jimplebody

发布于 2025-01-18 19:32:34 字数 2645 浏览 3 评论 0原文

我想从Java类获取unitgraph。 我通过classfile加载它,然后获取maws> main()main()method_info。 然后,我创建一个cfg,然后尝试将其转换为unitgraph。 我的方法是获取 cfg的jimplebody ,然后创建unitgraph。 但是,我无法通过调用来获得cfg.jimplify(...),因为它引发了以下错误:

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)

我的代码如下:

        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()));

我知道还有其他创建unitgraph的方法,例如:

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

以这种方式,我需要设置caste.v()。setSootClassPath(path) for jce.jar.jarrt.jar,我不想在代码中发生。因此,如果有另一种方法可以在没有设置这样的路径的情况下获得unitgraph,请帮助我。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

负佳期 2025-01-25 19:32:34

虽然我仍然无法将 method_info 转换为 SootMathod 然后获得 UnitGraph,但我可以使用 Options.v()。 set_prepend_classpath(true) 以避免直接设置 jce.jarrt.jar。这也达到了我的目的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文