2007-02-02 17:25:09 +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.ipa.callgraph;
|
|
|
|
|
2012-10-02 19:27:31 +00:00
|
|
|
import java.util.Set;
|
|
|
|
|
2007-07-06 22:09:08 +00:00
|
|
|
import com.ibm.wala.cast.ipa.callgraph.AstCallGraph;
|
|
|
|
import com.ibm.wala.cast.js.cfg.JSInducedCFG;
|
2009-04-09 20:31:14 +00:00
|
|
|
import com.ibm.wala.cast.js.loader.JSCallSiteReference;
|
2007-07-06 22:09:08 +00:00
|
|
|
import com.ibm.wala.cast.js.ssa.JavaScriptInvoke;
|
2012-10-02 19:27:31 +00:00
|
|
|
import com.ibm.wala.cast.js.types.JavaScriptMethods;
|
2007-07-06 22:09:08 +00:00
|
|
|
import com.ibm.wala.cast.js.types.JavaScriptTypes;
|
|
|
|
import com.ibm.wala.cfg.InducedCFG;
|
|
|
|
import com.ibm.wala.classLoader.CallSiteReference;
|
2012-10-02 19:27:31 +00:00
|
|
|
import com.ibm.wala.classLoader.IMethod;
|
2007-07-06 22:09:08 +00:00
|
|
|
import com.ibm.wala.classLoader.NewSiteReference;
|
2007-07-20 15:20:23 +00:00
|
|
|
import com.ibm.wala.ipa.callgraph.AnalysisCache;
|
2007-07-06 22:09:08 +00:00
|
|
|
import com.ibm.wala.ipa.callgraph.AnalysisOptions;
|
|
|
|
import com.ibm.wala.ipa.callgraph.CGNode;
|
|
|
|
import com.ibm.wala.ipa.callgraph.impl.Everywhere;
|
|
|
|
import com.ibm.wala.ipa.callgraph.impl.FakeRootMethod;
|
|
|
|
import com.ibm.wala.ipa.cha.IClassHierarchy;
|
|
|
|
import com.ibm.wala.ssa.SSAAbstractInvokeInstruction;
|
2008-03-03 21:32:36 +00:00
|
|
|
import com.ibm.wala.ssa.SSAInstruction;
|
2007-07-06 22:09:08 +00:00
|
|
|
import com.ibm.wala.ssa.SSANewInstruction;
|
|
|
|
import com.ibm.wala.types.MethodReference;
|
|
|
|
import com.ibm.wala.types.TypeReference;
|
2012-10-02 19:27:31 +00:00
|
|
|
import com.ibm.wala.util.collections.HashSetFactory;
|
2007-02-02 17:25:09 +00:00
|
|
|
|
|
|
|
public class JSCallGraph extends AstCallGraph {
|
|
|
|
|
2007-07-20 15:20:23 +00:00
|
|
|
public JSCallGraph(IClassHierarchy cha, AnalysisOptions options, AnalysisCache cache) {
|
|
|
|
super(cha, options, cache);
|
2007-02-02 17:25:09 +00:00
|
|
|
}
|
|
|
|
|
2007-07-20 15:20:23 +00:00
|
|
|
public final static MethodReference fakeRoot = MethodReference.findOrCreate(JavaScriptTypes.FakeRoot, FakeRootMethod.name,
|
|
|
|
FakeRootMethod.descr);
|
2007-02-02 17:25:09 +00:00
|
|
|
|
2007-06-01 03:32:56 +00:00
|
|
|
public static class JSFakeRoot extends ScriptFakeRoot {
|
|
|
|
|
2007-07-20 15:20:23 +00:00
|
|
|
public JSFakeRoot(IClassHierarchy cha, AnalysisOptions options, AnalysisCache cache) {
|
|
|
|
super(fakeRoot, cha.lookupClass(JavaScriptTypes.FakeRoot), cha, options, cache);
|
2007-02-02 17:25:09 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2008-03-03 21:32:36 +00:00
|
|
|
public InducedCFG makeControlFlowGraph(SSAInstruction[] instructions) {
|
|
|
|
return new JSInducedCFG(instructions, this, Everywhere.EVERYWHERE);
|
2007-02-02 17:25:09 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2007-07-06 22:09:08 +00:00
|
|
|
public SSANewInstruction addAllocation(TypeReference T) {
|
2007-02-02 17:25:09 +00:00
|
|
|
if (cha.isSubclassOf(cha.lookupClass(T), cha.lookupClass(JavaScriptTypes.Root))) {
|
2007-04-12 14:54:57 +00:00
|
|
|
int instance = nextLocal++;
|
|
|
|
NewSiteReference ref = NewSiteReference.make(statements.size(), T);
|
2011-04-13 10:56:36 +00:00
|
|
|
SSANewInstruction result = getDeclaringClass().getClassLoader().getInstructionFactory().NewInstruction(statements.size(), instance, ref);
|
2007-04-12 14:54:57 +00:00
|
|
|
statements.add(result);
|
|
|
|
return result;
|
2007-02-02 17:25:09 +00:00
|
|
|
} else {
|
2007-07-06 22:09:08 +00:00
|
|
|
return super.addAllocation(T);
|
2007-02-02 17:25:09 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-12 14:54:57 +00:00
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2007-04-12 14:54:57 +00:00
|
|
|
public SSAAbstractInvokeInstruction addDirectCall(int function, int[] params, CallSiteReference site) {
|
2008-01-16 16:33:10 +00:00
|
|
|
CallSiteReference newSite = new JSCallSiteReference(statements.size());
|
2007-04-12 14:54:57 +00:00
|
|
|
|
|
|
|
JavaScriptInvoke s = new JavaScriptInvoke(function, nextLocal++, params, nextLocal++, newSite);
|
2007-02-02 17:25:09 +00:00
|
|
|
statements.add(s);
|
2007-04-12 14:54:57 +00:00
|
|
|
|
2007-02-02 17:25:09 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-10 18:33:07 +00:00
|
|
|
@Override
|
|
|
|
protected CGNode makeFakeWorldClinitNode() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2009-06-22 15:06:12 +00:00
|
|
|
protected CGNode makeFakeRootNode() throws com.ibm.wala.util.CancelException {
|
2007-07-20 15:20:23 +00:00
|
|
|
return findOrCreateNode(new JSFakeRoot(cha, options, getAnalysisCache()), Everywhere.EVERYWHERE);
|
2007-02-02 17:25:09 +00:00
|
|
|
}
|
2012-10-02 19:27:31 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Set<CGNode> getNodes(MethodReference m) {
|
|
|
|
if (m.getName().equals(JavaScriptMethods.ctorAtom)) {
|
|
|
|
// TODO cache this?
|
|
|
|
Set<CGNode> result = HashSetFactory.make(1);
|
|
|
|
for (CGNode n : this) {
|
|
|
|
IMethod method = n.getMethod();
|
|
|
|
if (method.getName().equals(JavaScriptMethods.ctorAtom) && method.getDeclaringClass().getReference().equals(m.getDeclaringClass())) {
|
|
|
|
result.add(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return super.getNodes(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-02 17:25:09 +00:00
|
|
|
}
|