WALA icon indicating copy to clipboard operation
WALA copied to clipboard

How to add source files and what is the entrypoint?

Open qingdujun opened this issue 5 years ago • 10 comments

I want to add all the files in a directory to the Scope and then slice the program.

But I have encountered a lot of problems.

  • First, I added a directory, but only a .java in the directory was added.
  • Second, I don't know what entrypoint means.

So, I used com.ibm.wala.ipa.callgraph.impl.AllApplicationEntrypoints;, but it shows Number of EntryPoints: 0.

Immediately caused makeCallGraph to fail. “Could not create a entrypoint callsites:” problems.

Thanks.

ref1 - source dir call graph, ref2 - sdg for java,ref3 - entrypoint callsites,ref4 - addToScope

And, this is my code.

public static void doSlice() throws IOException, ClassHierarchyException, IllegalArgumentException, CancelException, InvalidClassFileException {
		String sourceDir = "/Users/qingdujun/home/whole/test/tomcat";
		AnalysisScope scope = new JavaSourceAnalysisScope();
		String[] stdlibs = WalaProperties.getJ2SEJarFiles();
		for (String s : stdlibs) {
//			System.out.println(s);
			scope.addToScope(ClassLoaderReference.Primordial, new JarFile(s));
		}
		scope.addToScope(JavaSourceAnalysisScope.SOURCE, new SourceDirectoryTreeModule(new File(sourceDir)));
//		scope.addSourceFileToScope(ClassLoaderReference.Application, new File(sourceDir+"/Sliceme.java"), sourceDir+"/Sliceme.java");

//		scope.setExclusions(new FileOfClasses(new ByteArrayInputStream(EXCLUSIONS.getBytes("UTF-8"))));
		IClassHierarchy cha = ClassHierarchyFactory.make(scope, new ECJClassLoaderFactory(scope.getExclusions()));
//		IClassHierarchy cha = ClassHierarchyFactory.make(scope);
		System.out.println(cha.getNumberOfClasses()+" classed");
//	    System.out.println(Warnings.asString());
//	    Warnings.clear();
	    AnalysisOptions options = new AnalysisOptions();
//	    Iterable<? extends Entrypoint> entrypoints = Util.makeMainEntrypoints(JavaSourceAnalysisScope.SOURCE, cha, new String[] {"mainClass"});
	    Iterable<? extends Entrypoint> entrypoints = new AllApplicationEntrypoints2(scope, cha);
	    options.setEntrypoints(entrypoints);
//	    options.setReflectionOptions(ReflectionOptions.NONE);
	    AnalysisCache cache = new AnalysisCacheImpl(AstIRFactory.makeDefaultFactory());
	    CallGraphBuilder<InstanceKey> builder = new ZeroCFABuilderFactory().make(options, cache, cha, scope);
	    System.out.println("building call graph...");
	    CallGraph cg = builder.makeCallGraph(options, null);
//	    PointerAnalysis<InstanceKey> pointerAnalysis = builder.getPointerAnalysis();
//	    DataDependenceOptions dOptions = DataDependenceOptions.NO_BASE_NO_EXCEPTIONS;
//	    ControlDependenceOptions cOptions = ControlDependenceOptions.NONE;
//	    SDG<InstanceKey> sdg = new SDG<InstanceKey>(cg, pointerAnalysis, new AstJavaModRef<InstanceKey>(), dOptions, cOptions);
//	    CGNode call = findMethod(cg, "main",null);
//		Statement ss = findCallTo(call, "append");//readByte
//		Collection<Statement> collection = Slicer.computeBackwardSlice(sdg, ss);
//		dumpSlice(collection);
	}

qingdujun avatar Jun 03 '19 11:06 qingdujun

i encountered the same problem,did you solved this issue?

liberatorqjw avatar Oct 21 '19 13:10 liberatorqjw

AllApplicationEntrypoints looks for classes from the Application loader. What does AllApplicationEntrypoints2 do?

On Mon, Oct 21, 2019 at 9:48 AM Jiawei Qin [email protected] wrote:

i encountered the same problem,did you solved this issue?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/wala/WALA/issues/520?email_source=notifications&email_token=AAMTO7UR2NZE74H4OJNI3V3QPWXLFA5CNFSM4HSHE6OKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEB2MHEA#issuecomment-544523152, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAMTO7XAT267H2SA3JPBBSTQPWXLFANCNFSM4HSHE6OA .

juliandolby avatar Oct 21 '19 15:10 juliandolby

I write this;

String sourceDir = "/tmp/test_data/SpringBootDemo/SpringBootDemo/src/main/java/";
    AnalysisScope scope = new JavaSourceAnalysisScope();
    // add standard libraries to scope
    String[] stdlibs = WalaProperties.getJ2SEJarFiles();
    for (String stdlib : stdlibs) {
      scope.addToScope(ClassLoaderReference.Primordial, new JarFile(stdlib));
    }
    
    ExampleUtil.addDefaultExclusions(scope);
//    ExampleUtil.addTailExclusions(scope);	
    
    // add the source directory
    scope.addToScope(JavaSourceAnalysisScope.SOURCE, new SourceDirectoryTreeModule(new File(sourceDir)));
    
    // build the class hierarchy
 IClassHierarchy cha = ClassHierarchyFactory.make(scope, new ECJClassLoaderFactory(scope.getExclusions()));

but issue is factory getloader is null

Exception in thread "main" com.ibm.wala.ipa.cha.ClassHierarchyException: factory.getLoader failed java.lang.NullPointerException
	at com.ibm.wala.ipa.cha.ClassHierarchy.<init>(ClassHierarchy.java:291)
	at com.ibm.wala.ipa.cha.ClassHierarchy.<init>(ClassHierarchy.java:203)
	at com.ibm.wala.ipa.cha.ClassHierarchyFactory.make(ClassHierarchyFactory.java:81)
	at com.ibm.wala.ipa.cha.ClassHierarchyFactory.make(ClassHierarchyFactory.java:67)
	at com.ibm.wala.examples.analysis.SourceDirCallGraph.main(SourceDirCallGraph.java:80)

liberatorqjw avatar Oct 22 '19 02:10 liberatorqjw

@liberatorqjw , I got the same exception. Have you solved this?

Anemone95 avatar Dec 05 '19 12:12 Anemone95

Does the SourceDirCallGraph driver work for people having this issue?

https://github.com/wala/WALA/blob/master/com.ibm.wala.cast.java.ecj/src/com/ibm/wala/cast/java/ecj/util/SourceDirCallGraph.java

I have tested that before. If it doesn't work, can you provide a small example that shows the crash?

msridhar avatar Dec 06 '19 01:12 msridhar

@msridhar Thank you for the quick reply. I wrote a simple example in https://github.com/Anemone95/wala-demo/blob/master/wala-code/src/main/java/top/anemone/walaDemo/MySourceDirCallGraph.java. After mvn clean package, using the following commands to reproduce this issue:

$ java -cp wala-code/target/lib/:wala-code/target/wala-code-1.0-SNAPSHOT.jar top.anemone.walaDemo.MySourceDirCallGraph -sourceDir wala-target/src/main/java -mainClass Ltop/anemone/walaTarget/Main
Exception in thread "main" com.ibm.wala.ipa.cha.ClassHierarchyException: factory.getLoader failed java.lang.NullPointerException
        at com.ibm.wala.ipa.cha.ClassHierarchy.<init>(ClassHierarchy.java:291)
        at com.ibm.wala.ipa.cha.ClassHierarchy.<init>(ClassHierarchy.java:203)
        at com.ibm.wala.ipa.cha.ClassHierarchyFactory.make(ClassHierarchyFactory.java:81)
        at com.ibm.wala.ipa.cha.ClassHierarchyFactory.make(ClassHierarchyFactory.java:67)
        at com.ibm.wala.cast.java.ecj.util.SourceDirCallGraph.main(SourceDirCallGraph.java:63)
        at top.anemone.walaDemo.MySourceDirCallGraph.main(MySourceDirCallGraph.java:11)

Anemone95 avatar Dec 06 '19 08:12 Anemone95

Yes, I write as follows:

 scope.addToScope(JavaSourceAnalysisScope.SOURCE, new SourceDirectoryTreeModule(new File(sourceDir)));

    // build the class hierarchy
    IClassHierarchy cha = ClassHierarchyFactory.make(scope, new com.ibm.wala.cast.java.translator.jdt.ecj.ECJClassLoaderFactory(scope.getExclusions()));
   .......
IAnalysisCacheView cache = new AnalysisCacheImpl(AstIRFactory.makeDefaultFactory());

I think the key poinr is this

IAnalysisCacheView cache = new AnalysisCacheImpl(AstIRFactory.makeDefaultFactory());

liberatorqjw avatar Dec 06 '19 08:12 liberatorqjw

Looked into this a bit. It looks like there are a couple of issues going on with WALA master. But regarding the factory.getLoader failed java.lang.NullPointerException issue, I think this is because the source front-end is crashing when there is a missing third-party jar file. @Anemone95 I think if you add the jar with the Gson class to your AnalysisScope then things might work; can you try?

I will try to work on fixing these issues soon.

msridhar avatar Dec 07 '19 05:12 msridhar

On WALA master the example from @Anemone95 fails as follows:

Exception in thread "main" com.ibm.wala.ipa.cha.ClassHierarchyException: factory.getLoader failed
        at com.ibm.wala.ipa.cha.ClassHierarchy.<init>(ClassHierarchy.java:291)
        at com.ibm.wala.ipa.cha.ClassHierarchy.<init>(ClassHierarchy.java:203)
        at com.ibm.wala.ipa.cha.ClassHierarchyFactory.make(ClassHierarchyFactory.java:81)
        at com.ibm.wala.ipa.cha.ClassHierarchyFactory.make(ClassHierarchyFactory.java:67)
        at com.ibm.wala.cast.java.ecj.util.SourceDirCallGraph.main(SourceDirCallGraph.java:63)
Caused by: java.lang.NullPointerException
        at com.ibm.wala.cast.java.translator.jdt.JDTJava2CAstTranslator$ProcedureEntity.<init>(JDTJava2CAstTranslator.java:1088)
        at com.ibm.wala.cast.java.translator.jdt.JDTJava2CAstTranslator$ProcedureEntity.<init>(JDTJava2CAstTranslator.java:1011)
        at com.ibm.wala.cast.java.translator.jdt.JDTJava2CAstTranslator$ProcedureEntity.<init>(JDTJava2CAstTranslator.java:956)

The NullPointerException comes because the JDT frontend tries to resolve Gson and fails.

@juliandolby do you know if the JDT / ECJ frontend is supposed to be robust to source files referencing classes that are missing from the analysis scope?

msridhar avatar Dec 08 '19 05:12 msridhar

I can confirm from my experience that the frontend is absolutely not robust to classes missing from the analysis scope. The JDT/ECJ Java2CAstTranslator runs into a myriad of NPEs if you don't provide the classes or implement an alternative (I hacked together the latter and it was quite an undertaking)

see #99

mattkindy avatar Jan 28 '20 20:01 mattkindy