2007-02-02 17:28:04 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Copyright (c) 2002 - 2006 IBM Corporation.
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
*
|
|
|
|
* Contributors:
|
|
|
|
* IBM Corporation - initial API and implementation
|
|
|
|
*****************************************************************************/
|
|
|
|
package com.ibm.wala.cast.js.test;
|
|
|
|
|
2007-02-08 19:08:41 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.JarURLConnection;
|
|
|
|
import java.net.URL;
|
2011-04-04 21:20:31 +00:00
|
|
|
import java.util.Set;
|
2007-02-02 17:28:04 +00:00
|
|
|
|
2007-02-08 19:08:41 +00:00
|
|
|
import junit.framework.Assert;
|
2007-02-02 17:28:04 +00:00
|
|
|
|
2012-01-27 20:15:33 +00:00
|
|
|
import com.ibm.wala.cast.ir.ssa.AstIRFactory;
|
2011-04-04 21:20:31 +00:00
|
|
|
import com.ibm.wala.cast.js.html.MappedSourceModule;
|
2011-05-23 16:27:07 +00:00
|
|
|
import com.ibm.wala.cast.js.html.WebPageLoaderFactory;
|
2011-04-04 21:20:31 +00:00
|
|
|
import com.ibm.wala.cast.js.html.WebUtil;
|
2012-01-06 21:48:01 +00:00
|
|
|
import com.ibm.wala.cast.js.ipa.callgraph.JSAnalysisOptions;
|
2009-04-30 13:16:52 +00:00
|
|
|
import com.ibm.wala.cast.js.ipa.callgraph.JSCFABuilder;
|
|
|
|
import com.ibm.wala.cast.js.ipa.callgraph.JSZeroOrOneXCFABuilder;
|
2007-12-06 14:58:36 +00:00
|
|
|
import com.ibm.wala.cast.js.loader.JavaScriptLoader;
|
2007-02-08 19:08:41 +00:00
|
|
|
import com.ibm.wala.cast.js.loader.JavaScriptLoaderFactory;
|
|
|
|
import com.ibm.wala.classLoader.SourceFileModule;
|
2011-04-04 21:20:31 +00:00
|
|
|
import com.ibm.wala.classLoader.SourceModule;
|
2007-07-20 15:20:13 +00:00
|
|
|
import com.ibm.wala.ipa.callgraph.AnalysisCache;
|
2007-02-08 19:08:41 +00:00
|
|
|
import com.ibm.wala.ipa.callgraph.AnalysisScope;
|
|
|
|
import com.ibm.wala.ipa.callgraph.CallGraph;
|
2007-05-15 17:45:03 +00:00
|
|
|
import com.ibm.wala.ipa.callgraph.Entrypoint;
|
2007-02-08 19:08:41 +00:00
|
|
|
import com.ibm.wala.ipa.callgraph.propagation.PropagationCallGraphBuilder;
|
|
|
|
import com.ibm.wala.ipa.callgraph.propagation.cfa.ZeroXInstanceKeys;
|
2007-07-06 22:08:41 +00:00
|
|
|
import com.ibm.wala.ipa.cha.ClassHierarchyException;
|
|
|
|
import com.ibm.wala.ipa.cha.IClassHierarchy;
|
2012-01-27 20:15:33 +00:00
|
|
|
import com.ibm.wala.ssa.IRFactory;
|
2009-06-22 15:06:12 +00:00
|
|
|
import com.ibm.wala.util.CancelException;
|
2007-02-02 17:28:04 +00:00
|
|
|
|
2012-01-06 21:48:53 +00:00
|
|
|
/**
|
2012-02-17 20:22:55 +00:00
|
|
|
* TODO this class is a mess. rewrite.
|
2012-01-06 21:48:53 +00:00
|
|
|
*/
|
2012-02-17 20:21:59 +00:00
|
|
|
public class JSCallGraphBuilderUtil extends com.ibm.wala.cast.js.ipa.callgraph.JSCallGraphUtil {
|
2007-02-02 17:28:04 +00:00
|
|
|
|
2012-02-17 20:22:14 +00:00
|
|
|
public static enum CGBuilderType {
|
2012-02-17 20:22:55 +00:00
|
|
|
ZERO_ONE_CFA(false, false, true), ZERO_ONE_CFA_NO_CALL_APPLY(false, false, false), ZERO_ONE_CFA_PRECISE_LEXICAL(false, true,
|
|
|
|
true), ONE_CFA(true, false, true), ONE_CFA_PRECISE_LEXICAL(true, true, true);
|
|
|
|
|
2012-02-17 20:22:14 +00:00
|
|
|
private final boolean useOneCFA;
|
2012-02-17 20:22:55 +00:00
|
|
|
|
|
|
|
private final boolean usePreciseLexical;
|
|
|
|
|
2012-02-17 20:22:14 +00:00
|
|
|
private final boolean handleCallApply;
|
|
|
|
|
2012-02-17 20:22:55 +00:00
|
|
|
private CGBuilderType(boolean useOneCFA, boolean usePreciseLexical, boolean handleCallApply) {
|
2012-02-17 20:22:14 +00:00
|
|
|
this.useOneCFA = useOneCFA;
|
2012-02-17 20:22:55 +00:00
|
|
|
this.usePreciseLexical = usePreciseLexical;
|
2012-02-17 20:22:14 +00:00
|
|
|
this.handleCallApply = handleCallApply;
|
|
|
|
}
|
|
|
|
|
2012-02-17 20:22:55 +00:00
|
|
|
public boolean useOneCFA() {
|
2012-02-17 20:22:14 +00:00
|
|
|
return useOneCFA;
|
|
|
|
}
|
|
|
|
|
2012-02-17 20:22:55 +00:00
|
|
|
public boolean usePreciseLexical() {
|
|
|
|
return usePreciseLexical;
|
|
|
|
}
|
|
|
|
public boolean handleCallApply() {
|
2012-02-17 20:22:14 +00:00
|
|
|
return handleCallApply;
|
|
|
|
}
|
2012-02-17 20:22:55 +00:00
|
|
|
|
2012-02-17 20:22:14 +00:00
|
|
|
}
|
2012-02-17 20:22:55 +00:00
|
|
|
|
2012-02-17 20:22:14 +00:00
|
|
|
public static JSCFABuilder makeScriptCGBuilder(String dir, String name, CGBuilderType builderType) throws IOException {
|
2012-02-17 20:21:59 +00:00
|
|
|
JavaScriptLoaderFactory loaders = JSCallGraphBuilderUtil.makeLoaders();
|
2007-02-02 17:28:04 +00:00
|
|
|
|
2012-01-27 20:15:33 +00:00
|
|
|
AnalysisScope scope = makeScriptScope(dir, name, loaders);
|
|
|
|
|
2012-02-17 20:22:14 +00:00
|
|
|
return makeCG(loaders, scope, builderType, AstIRFactory.makeDefaultFactory());
|
2012-01-27 20:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static AnalysisScope makeScriptScope(String dir, String name, JavaScriptLoaderFactory loaders) throws IOException {
|
2012-02-17 20:21:59 +00:00
|
|
|
URL script = JSCallGraphBuilderUtil.class.getClassLoader().getResource(dir + File.separator + name);
|
2007-02-02 17:28:04 +00:00
|
|
|
if (script == null) {
|
2012-02-17 20:21:59 +00:00
|
|
|
script = JSCallGraphBuilderUtil.class.getClassLoader().getResource(dir + "/" + name);
|
2007-02-02 17:28:04 +00:00
|
|
|
}
|
2009-04-30 13:16:52 +00:00
|
|
|
assert script != null : "cannot find " + dir + " and " + name;
|
2007-02-02 17:28:04 +00:00
|
|
|
|
|
|
|
AnalysisScope scope;
|
|
|
|
if (script.openConnection() instanceof JarURLConnection) {
|
2007-12-10 04:33:40 +00:00
|
|
|
scope = makeScope(new URL[] { script }, loaders, JavaScriptLoader.JS);
|
2007-02-02 17:28:04 +00:00
|
|
|
} else {
|
2007-12-10 04:33:40 +00:00
|
|
|
scope = makeScope(new SourceFileModule[] { makeSourceModule(script, dir, name) }, loaders, JavaScriptLoader.JS);
|
2007-02-02 17:28:04 +00:00
|
|
|
}
|
2012-02-17 20:22:14 +00:00
|
|
|
|
2012-01-27 20:15:33 +00:00
|
|
|
return scope;
|
2007-11-06 04:14:53 +00:00
|
|
|
}
|
|
|
|
|
2010-09-09 14:09:16 +00:00
|
|
|
public static JSCFABuilder makeScriptCGBuilder(String dir, String name) throws IOException {
|
2012-02-17 20:22:14 +00:00
|
|
|
return makeScriptCGBuilder(dir, name, CGBuilderType.ZERO_ONE_CFA);
|
2007-02-02 17:28:04 +00:00
|
|
|
}
|
|
|
|
|
2007-11-13 16:18:48 +00:00
|
|
|
public static CallGraph makeScriptCG(String dir, String name) throws IOException, IllegalArgumentException, CancelException {
|
2012-02-17 20:22:14 +00:00
|
|
|
return makeScriptCG(dir, name, CGBuilderType.ZERO_ONE_CFA);
|
2012-01-27 20:15:33 +00:00
|
|
|
}
|
|
|
|
|
2012-02-17 20:22:55 +00:00
|
|
|
public static CallGraph makeScriptCG(String dir, String name, CGBuilderType builderType) throws IOException,
|
|
|
|
IllegalArgumentException, CancelException {
|
2012-02-17 20:22:14 +00:00
|
|
|
PropagationCallGraphBuilder b = makeScriptCGBuilder(dir, name, builderType);
|
2007-05-15 17:45:03 +00:00
|
|
|
CallGraph CG = b.makeCallGraph(b.getOptions());
|
2012-03-01 02:45:51 +00:00
|
|
|
// dumpCG(b.getPointerAnalysis(), CG);
|
2007-02-02 17:28:04 +00:00
|
|
|
return CG;
|
|
|
|
}
|
|
|
|
|
2012-02-17 20:22:14 +00:00
|
|
|
public static CallGraph makeScriptCG(SourceModule[] scripts, CGBuilderType builderType, IRFactory irFactory) throws IOException, IllegalArgumentException,
|
2011-05-06 15:14:32 +00:00
|
|
|
CancelException {
|
2012-02-17 20:22:14 +00:00
|
|
|
PropagationCallGraphBuilder b = makeCGBuilder(makeLoaders(), scripts, builderType, irFactory);
|
2007-05-15 17:45:03 +00:00
|
|
|
CallGraph CG = b.makeCallGraph(b.getOptions());
|
2012-03-01 02:45:51 +00:00
|
|
|
// dumpCG(b.getPointerAnalysis(), CG);
|
2007-02-02 17:28:04 +00:00
|
|
|
return CG;
|
|
|
|
}
|
|
|
|
|
2010-09-09 14:09:16 +00:00
|
|
|
public static JSCFABuilder makeHTMLCGBuilder(URL url) throws IOException {
|
2012-02-17 20:22:14 +00:00
|
|
|
return makeHTMLCGBuilder(url, CGBuilderType.ZERO_ONE_CFA);
|
2012-01-06 21:48:53 +00:00
|
|
|
}
|
2012-02-17 20:22:55 +00:00
|
|
|
|
2012-02-17 20:22:14 +00:00
|
|
|
public static JSCFABuilder makeHTMLCGBuilder(URL url, CGBuilderType builderType) throws IOException {
|
2011-04-04 21:20:31 +00:00
|
|
|
JavaScriptLoader.addBootstrapFile(WebUtil.preamble);
|
|
|
|
Set<MappedSourceModule> script = WebUtil.extractScriptFromHTML(url);
|
2012-02-17 20:22:14 +00:00
|
|
|
JSCFABuilder builder = makeCGBuilder(new WebPageLoaderFactory(translatorFactory, preprocessor), script.toArray(new SourceModule[script.size()]), builderType, AstIRFactory.makeDefaultFactory());
|
2010-09-09 14:09:16 +00:00
|
|
|
builder.setBaseURL(url);
|
|
|
|
return builder;
|
2007-02-02 17:28:04 +00:00
|
|
|
}
|
|
|
|
|
2007-11-13 16:18:48 +00:00
|
|
|
public static CallGraph makeHTMLCG(URL url) throws IOException, IllegalArgumentException, CancelException {
|
2007-02-02 17:28:04 +00:00
|
|
|
PropagationCallGraphBuilder b = makeHTMLCGBuilder(url);
|
2007-05-15 17:45:03 +00:00
|
|
|
CallGraph CG = b.makeCallGraph(b.getOptions());
|
2012-01-27 20:15:33 +00:00
|
|
|
dumpCG(b.getPointerAnalysis(), CG);
|
2011-05-06 15:14:32 +00:00
|
|
|
return CG;
|
|
|
|
}
|
|
|
|
|
2012-02-17 20:22:14 +00:00
|
|
|
public static CallGraph makeHTMLCG(URL url, CGBuilderType builderType) throws IOException, IllegalArgumentException,
|
2011-05-06 15:14:32 +00:00
|
|
|
CancelException {
|
2012-02-17 20:22:14 +00:00
|
|
|
PropagationCallGraphBuilder b = makeHTMLCGBuilder(url, builderType);
|
2012-01-06 21:48:53 +00:00
|
|
|
CallGraph CG = b.makeCallGraph(b.getOptions());
|
2007-02-02 17:28:04 +00:00
|
|
|
return CG;
|
|
|
|
}
|
|
|
|
|
2012-02-17 20:22:14 +00:00
|
|
|
public static JSCFABuilder makeCGBuilder(JavaScriptLoaderFactory loaders, SourceModule[] scripts, CGBuilderType builderType, IRFactory irFactory) throws IOException {
|
2007-12-10 04:33:40 +00:00
|
|
|
AnalysisScope scope = makeScope(scripts, loaders, JavaScriptLoader.JS);
|
2012-02-17 20:22:14 +00:00
|
|
|
return makeCG(loaders, scope, builderType, irFactory);
|
2007-02-02 17:28:04 +00:00
|
|
|
}
|
|
|
|
|
2012-02-17 20:22:14 +00:00
|
|
|
protected static JSCFABuilder makeCG(JavaScriptLoaderFactory loaders, AnalysisScope scope, CGBuilderType builderType, IRFactory irFactory) throws IOException {
|
2007-02-02 17:28:04 +00:00
|
|
|
try {
|
2007-07-06 22:08:41 +00:00
|
|
|
IClassHierarchy cha = makeHierarchy(scope, loaders);
|
2012-01-06 21:52:26 +00:00
|
|
|
com.ibm.wala.cast.js.util.Util.checkForFrontEndErrors(cha);
|
2007-05-15 17:45:03 +00:00
|
|
|
Iterable<Entrypoint> roots = makeScriptRoots(cha);
|
2012-01-06 21:48:01 +00:00
|
|
|
JSAnalysisOptions options = makeOptions(scope, cha, roots);
|
2012-02-17 20:22:55 +00:00
|
|
|
options.setHandleCallApply(builderType.handleCallApply());
|
|
|
|
options.setUsePreciseLexical(builderType.usePreciseLexical());
|
2012-01-27 20:15:33 +00:00
|
|
|
AnalysisCache cache = makeCache(irFactory);
|
2012-02-17 20:22:55 +00:00
|
|
|
JSCFABuilder builder = new JSZeroOrOneXCFABuilder(cha, options, cache, null, null, ZeroXInstanceKeys.ALLOCATIONS,
|
|
|
|
builderType.useOneCFA());
|
2007-02-02 17:28:04 +00:00
|
|
|
|
|
|
|
return builder;
|
|
|
|
} catch (ClassHierarchyException e) {
|
2007-05-15 17:45:03 +00:00
|
|
|
Assert.assertTrue("internal error building class hierarchy", false);
|
2007-02-02 17:28:04 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|