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
|
|
|
|
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;
|
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
|
|
|
/**
|
|
|
|
* TODO this class is a mess. rewrite.
|
|
|
|
*/
|
2007-02-02 17:28:04 +00:00
|
|
|
public class Util extends com.ibm.wala.cast.js.ipa.callgraph.Util {
|
|
|
|
|
2010-09-09 14:09:16 +00:00
|
|
|
public static JSCFABuilder makeScriptCGBuilder(String dir, String name, boolean useOneCFA) throws IOException {
|
2007-02-02 17:28:04 +00:00
|
|
|
JavaScriptLoaderFactory loaders = Util.makeLoaders();
|
|
|
|
|
2007-05-15 17:45:03 +00:00
|
|
|
URL script = Util.class.getClassLoader().getResource(dir + File.separator + name);
|
2007-02-02 17:28:04 +00:00
|
|
|
if (script == null) {
|
|
|
|
script = Util.class.getClassLoader().getResource(dir + "/" + name);
|
|
|
|
}
|
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-01-06 21:48:53 +00:00
|
|
|
return makeCG(loaders, scope, useOneCFA, true);
|
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 {
|
2007-11-06 04:14:53 +00:00
|
|
|
return makeScriptCGBuilder(dir, name, false);
|
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 {
|
2007-11-06 04:14:53 +00:00
|
|
|
return makeScriptCG(dir, name, false);
|
|
|
|
}
|
|
|
|
|
2011-05-06 15:14:32 +00:00
|
|
|
public static CallGraph makeScriptCG(String dir, String name, boolean useOneCFA) throws IOException, IllegalArgumentException,
|
|
|
|
CancelException {
|
2007-11-06 04:14:53 +00:00
|
|
|
PropagationCallGraphBuilder b = makeScriptCGBuilder(dir, name, useOneCFA);
|
2007-05-15 17:45:03 +00:00
|
|
|
CallGraph CG = b.makeCallGraph(b.getOptions());
|
2011-04-28 19:08:06 +00:00
|
|
|
dumpCG(b.getPointerAnalysis(), CG);
|
2007-02-02 17:28:04 +00:00
|
|
|
return CG;
|
|
|
|
}
|
|
|
|
|
2011-05-06 15:14:32 +00:00
|
|
|
public static CallGraph makeScriptCG(SourceModule[] scripts, boolean useOneCFA) throws IOException, IllegalArgumentException,
|
|
|
|
CancelException {
|
2012-01-06 21:48:53 +00:00
|
|
|
PropagationCallGraphBuilder b = makeCGBuilder(makeLoaders(), scripts, useOneCFA, true);
|
2007-05-15 17:45:03 +00:00
|
|
|
CallGraph CG = b.makeCallGraph(b.getOptions());
|
2011-04-28 19:08:06 +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-01-06 21:48:53 +00:00
|
|
|
return makeHTMLCGBuilder(url, true);
|
|
|
|
}
|
|
|
|
public static JSCFABuilder makeHTMLCGBuilder(URL url, boolean handleCallApply) throws IOException {
|
2011-04-04 21:20:31 +00:00
|
|
|
JavaScriptLoader.addBootstrapFile(WebUtil.preamble);
|
|
|
|
Set<MappedSourceModule> script = WebUtil.extractScriptFromHTML(url);
|
2012-01-06 21:48:53 +00:00
|
|
|
JSCFABuilder builder = makeCGBuilder(new WebPageLoaderFactory(translatorFactory, preprocessor), script.toArray(new SourceModule[script.size()]), false, handleCallApply);
|
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-06 21:27:03 +00:00
|
|
|
//dumpCG(b.getPointerAnalysis(), CG);
|
2011-05-06 15:14:32 +00:00
|
|
|
return CG;
|
|
|
|
}
|
|
|
|
|
2012-01-06 21:48:53 +00:00
|
|
|
public static CallGraph makeHTMLCG(URL url, boolean handleCallApply) throws IOException, IllegalArgumentException,
|
2011-05-06 15:14:32 +00:00
|
|
|
CancelException {
|
2012-01-06 21:48:53 +00:00
|
|
|
PropagationCallGraphBuilder b = makeHTMLCGBuilder(url, handleCallApply);
|
|
|
|
CallGraph CG = b.makeCallGraph(b.getOptions());
|
2007-02-02 17:28:04 +00:00
|
|
|
return CG;
|
|
|
|
}
|
|
|
|
|
2012-01-06 21:48:53 +00:00
|
|
|
public static JSCFABuilder makeCGBuilder(JavaScriptLoaderFactory loaders, SourceModule[] scripts, boolean useOneCFA, boolean handleCallApply) throws IOException {
|
2007-12-10 04:33:40 +00:00
|
|
|
AnalysisScope scope = makeScope(scripts, loaders, JavaScriptLoader.JS);
|
2012-01-06 21:48:53 +00:00
|
|
|
return makeCG(loaders, scope, useOneCFA, handleCallApply);
|
2007-02-02 17:28:04 +00:00
|
|
|
}
|
|
|
|
|
2012-01-06 21:48:53 +00:00
|
|
|
protected static JSCFABuilder makeCG(JavaScriptLoaderFactory loaders, AnalysisScope scope, boolean useOneCFA, boolean handleCallApply) 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-01-06 21:48:53 +00:00
|
|
|
options.setHandleCallApply(handleCallApply);
|
2008-12-16 14:54:09 +00:00
|
|
|
AnalysisCache cache = makeCache();
|
2007-02-02 17:28:04 +00:00
|
|
|
|
2010-05-12 17:19:54 +00:00
|
|
|
JSCFABuilder builder = new JSZeroOrOneXCFABuilder(cha, options, cache, null, null, ZeroXInstanceKeys.ALLOCATIONS, 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|