0

我想使用 Soot 库从 *.java 文件构建 SSA。但是我发现的所有示例都使用 Soot 作为独立工具,而不是库。谁能给我一个在程序中做的例子吗?

首先,我只是想从源文件中加载我的类并打印它(TestClass.class 在目录中A/home/abdullin/workspace/test):

import soot.G
import soot.Scene
import soot.options.Options

fun main(args: Array<String>) {
    G.reset();
    Options.v().set_whole_program(true)
    Scene.v().loadBasicClasses()

    Scene.v().sootClassPath = "${Scene.v().defaultClassPath()}:/home/abdullin/workspace/test"

    val sc = Scene.v().getSootClass("TestClass")
    Scene.v().loadNecessaryClasses()
    sc.setApplicationClass()

    println(sc.name)
    sc.methods.forEach {
        println(it)
    }
}

但是当我运行它时,我得到运行时异常Aborting: can't find classfile TestClass。如果我像他们在某些教程中所做的那样更改为,soot 会找到我的课程,但它并不完整Scene.v().getSootClass("TestClass")Scene.v().loadClassAndSupport("TestClass")它打印出类方法的签名,但找不到它们的主体,activeBody字段为空。

TestClass
<TestClass: void <init>()>
<TestClass: void main(java.lang.String[])>
<TestClass: void f1()>
4

2 回答 2

3

首先,确保 Soot jar 在类路径中。

soot.G然后,使用类和soot.options.OptionsG.reset()Options.v().parse()是感兴趣的方法,另请参阅命令行选项)设置 Soot 。

使用soot.Scene.v().setSootClassPath()和类似的方法,您可以告诉 Soot 在哪里可以找到您要分析的代码的类文件。

然后可以使用Scene.v().getSootClass()来获取 SootClass 对象。确保在将要分析的类设置为主类后,Soot 会加载所有类:

mySootClass.setApplicationClass();
Scene.v().loadNecessaryClasses();

在此之后,您可以使用 Soot 获取各种类型的图表并运行您的分析,如幸存者指南中所述

于 2018-02-05T10:42:51.963 回答
0

你可以阅读这篇文章(https://o2lab.github.io/710/p/a1.html)。但是如果你尝试分析一个 jar 文件,你应该解压它并得到一组类文件。然后你应该将你的类目录添加到 soot_class_path 中。

演示:

public static void main(String[] args) {
    //spotbugs -- testing
    String classesDir = "D:\\wkspace\\seed8\\dir\\spotbugs";
    String mainClass = "edu.umd.cs.findbugs.LaunchAppropriateUI";

    //set classpath
    String jreDir = System.getProperty("java.home") + "\\lib\\jce.jar";
    String jceDir = System.getProperty("java.home") + "\\lib\\rt.jar";
    String path = jreDir + File.pathSeparator + jceDir + File.pathSeparator + classesDir;
    Scene.v().setSootClassPath(path);

    //add an intra-procedural analysis phase to Soot
    TestCallGraphSootJar_3 analysis = new TestCallGraphSootJar_3();
    PackManager.v().getPack("wjtp").add(new Transform("wjtp.TestSootCallGraph", analysis));

    excludeJDKLibrary();

    Options.v().set_process_dir(Arrays.asList(classesDir));
    Options.v().set_whole_program(true);
    //Options.v().set_app(true);

    SootClass appClass = Scene.v().loadClassAndSupport(mainClass);
    Scene.v().setMainClass(appClass);
    Scene.v().loadNecessaryClasses();

    //enableCHACallGraph();
    enableSparkCallGraph();

    PackManager.v().runPacks();
}

如果你更换

SootClass appclass = Scene.v().loadClassAndSupport(mainclass);
Scene.v().setMainClass(appclass);
Scene.v().loadNecessaryClasses();

经过

Scene.v().loadNecessaryClasses();
SootClass appclass = Scene.v().getSootClass(mainclass);
Scene.v().setMainClass(appclass);

,该程序也有效。

于 2020-05-30T22:37:14.307 回答