soot-oss / soot

Soot - A Java optimization framework
GNU Lesser General Public License v2.1
2.88k stars 708 forks source link

How to write soot code to achieve java file to jimple file conversion #796

Closed SomeoneSometimes closed 7 years ago

SomeoneSometimes commented 7 years ago

I try this code

        Options.v().set_src_prec(Options.src_prec_java);
        Options.v().set_output_format(Options.output_format_jimple);
        Options.v().set_soot_classpath("G:/FlowDroid/rt.jar");
        Options.v().set_process_dir(Arrays.asList("G:/FlowDroid/test.java"));

        Scene.v().loadNecessaryClasses();
        for (SootClass i: Scene.v().getApplicationClasses()) {
            System.out.println(i.getName());
        }

The test.java code is analyzed as follows

package hello_wold;

public class HelloWorld {
    public static void main(String[] args){
        System.out.println("hello world!");
    }
}

The following exception occurs during the run

Exception in thread "main" java.lang.RuntimeException: Invalid class source type
    at soot.SourceLocator.getClassesUnder(SourceLocator.java:294)
    at soot.Scene.loadNecessaryClasses(Scene.java:1361)

Who can help me? Thanks!

Update: Now I change the Option of "process_dir" to Options.v().set_process_dir(Arrays.asList("G:/FlowDroid/test/"));.There is only one java file HelloWorld.java in the test folder),and occurs this error 31583081-842d679e-b1c6-11e7-92fd-7bda681ee08c

The version of java used is 1.8,When I use the soot command line, the 1.8 version of the java error, then I replaced the 1.6 version can be successfully run.Strangely, my flowdroid in the 1.8 version of the Java can run, the above error is java version of the problem? There is also a question is that when my java inside package xxxx;, try to convert this java file will always produce errors,why?

mbenz89 commented 7 years ago

Unfortunately, Soot's java frontend is heavily outdated. I strongly discourage from using it. You should be able to solve the problem by compiling your sources to bytecode first and then run Soot on the .class files with -src-prec class.

SomeoneSometimes commented 7 years ago

Thanks! According your advice, i translate my .java file to .class file fistly, and it run successful!

But i hava another question, when my .java file has package createThread, the run always go wrong. dzio8 u wles 6 xtn a So if i just want to translate one .java file in a project, what additional configurations should i add?Thanks!

mbenz89 commented 7 years ago

It is important that you specify the root of the package structure as your process path. For instance, if you have java class a.b.MyClass and you specify the bin folder as output directory for javac, the folder structure will look like this: bin/a/b/MyClass.class. In this case, you should specify the process path of soot like this: "soot -process-dir bin/".

This should solve the error.

SomeoneSometimes commented 7 years ago

Interesting! As your description, I added a layer of folders, now my folder structure looks like this:..../hhh/createThread/hello.class,and my Options is Options.v().set_process_dir(Arrays.asList("G:/FlowDroid/hhh")); ,it run successful! Thank you very much!