简体   繁体   English

将 Soot 用作库时如何设置?

[英]How can I set up Soot when using it as a library?

I want to use Soot library to build an SSA from *.java file.我想使用 Soot 库从 *.java 文件构建 SSA。 But all the examples I found use Soot as standalone tool, not library.但是我发现的所有示例都使用 Soot 作为独立工具,而不是库。 Can anyone give me example hot to do it in program?谁能给我一个例子热在程序中做到这一点?

For a start I am just trying to load my class from the source file and print it (TestClass.class is in the directory A/home/abdullin/workspace/test ):首先,我只是尝试从源文件加载我的类并打印它(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)
    }
}

But when I run this, I get runtime exception Aborting: can't find classfile TestClass .但是当我运行它时,我得到运行时异常Aborting: can't find classfile TestClass If I change Scene.v().getSootClass("TestClass") to Scene.v().loadClassAndSupport("TestClass") as they do in some of their tutorials, soot finds my class, but it is not complete.如果我将Scene.v().getSootClass("TestClass")更改为Scene.v().loadClassAndSupport("TestClass")就像他们在他们的一些教程中所做的那样,烟灰会找到我的类,但它不完整。 It prints me signatures of class methods, but can't find their bodies, activeBody field is null.它向我打印类方法的签名,但找不到它们的主体, activeBody字段为空。

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

First, make sure that the Soot jar is in the classpath.首先,确保 Soot jar 在类路径中。

Then, set up Soot using the classes soot.G and soot.options.Options ( G.reset() and Options.v().parse() are methods of interest, also see command line options ).然后,使用类soot.Gsoot.options.OptionsG.reset()Options.v().parse()是感兴趣的方法,另见命令行选项)设置烟灰。

Using soot.Scene.v().setSootClassPath() and similar you can tell Soot where to find the class files of the code you want to analyze.使用soot.Scene.v().setSootClassPath()和类似的方法,您可以告诉 Soot 在哪里可以找到要分析的代码的类文件。

You can then use Scene.v().getSootClass() to obtain SootClass objects.然后您可以使用Scene.v().getSootClass()来获取 SootClass 对象。 Make sure that Soot loads all classes after setting the class you want to analyze as main class:在将要分析的类设置为主类后,确保 Soot 加载所有类:

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

After this, you can use Soot to obtain various types of graphs and run you analyses, as described in the Survivor's guide在此之后,您可以使用 Soot 来获取各种类型的图表并运行您的分析,如Survivor's guide 中所述

You can read this post ( https://o2lab.github.io/710/p/a1.html ).您可以阅读这篇文章( https://o2lab.github.io/710/p/a1.html )。 But if you try to analyze a jar file, you should unzip it and get a set of class files.但是如果你试图分析一个 jar 文件,你应该解压它并得到一组类文件。 Then you should add your classes directory into the soot_class_path.然后您应该将您的类目录添加到 soot_class_path 中。

Demo:演示:

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

If you replace如果你更换

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

by

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

, the program also works. ,该程序也有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM