2012-01-06 21:21:26 +00:00
|
|
|
package com.ibm.wala.cast.js.ipa.callgraph;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
2012-01-06 21:21:40 +00:00
|
|
|
import java.util.Map;
|
2012-01-06 21:21:26 +00:00
|
|
|
|
|
|
|
import com.ibm.wala.cast.js.ipa.summaries.JavaScriptSummarizedFunction;
|
|
|
|
import com.ibm.wala.cast.js.ipa.summaries.JavaScriptSummary;
|
|
|
|
import com.ibm.wala.cast.js.loader.JSCallSiteReference;
|
|
|
|
import com.ibm.wala.cast.js.loader.JavaScriptLoader;
|
|
|
|
import com.ibm.wala.cast.js.ssa.JSInstructionFactory;
|
|
|
|
import com.ibm.wala.cast.js.types.JavaScriptMethods;
|
|
|
|
import com.ibm.wala.cast.js.types.JavaScriptTypes;
|
|
|
|
import com.ibm.wala.cast.loader.AstFunctionClass;
|
|
|
|
import com.ibm.wala.classLoader.CallSiteReference;
|
|
|
|
import com.ibm.wala.classLoader.IClass;
|
|
|
|
import com.ibm.wala.classLoader.IMethod;
|
|
|
|
import com.ibm.wala.classLoader.NewSiteReference;
|
|
|
|
import com.ibm.wala.ipa.callgraph.CGNode;
|
|
|
|
import com.ibm.wala.ipa.callgraph.MethodTargetSelector;
|
|
|
|
import com.ibm.wala.ipa.cha.IClassHierarchy;
|
|
|
|
import com.ibm.wala.ssa.IR;
|
|
|
|
import com.ibm.wala.ssa.SSAAbstractInvokeInstruction;
|
|
|
|
import com.ibm.wala.types.Descriptor;
|
|
|
|
import com.ibm.wala.types.MethodReference;
|
2012-01-06 21:21:40 +00:00
|
|
|
import com.ibm.wala.util.collections.HashMapFactory;
|
2012-01-06 21:21:26 +00:00
|
|
|
import com.ibm.wala.util.strings.Atom;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate IR to model Function.call() and Function.apply()
|
|
|
|
*
|
|
|
|
* @see <a
|
|
|
|
* href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/Apply">MDN
|
|
|
|
* Function.apply() docs</a>
|
|
|
|
* @see <a
|
|
|
|
* href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/Call">MDN
|
|
|
|
* Function.call() docs</a>
|
|
|
|
*
|
|
|
|
* @author manu
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public class JavaScriptFunctionCallApplyTargetSelector implements MethodTargetSelector {
|
|
|
|
|
|
|
|
private final IClassHierarchy cha;
|
|
|
|
private final MethodTargetSelector base;
|
|
|
|
|
|
|
|
public JavaScriptFunctionCallApplyTargetSelector(IClassHierarchy cha, MethodTargetSelector base) {
|
|
|
|
this.cha = cha;
|
|
|
|
this.base = base;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* (non-Javadoc)
|
|
|
|
*
|
|
|
|
* @see
|
|
|
|
* com.ibm.wala.ipa.callgraph.MethodTargetSelector#getCalleeTarget(com.ibm
|
|
|
|
* .wala.ipa.callgraph.CGNode, com.ibm.wala.classLoader.CallSiteReference,
|
|
|
|
* com.ibm.wala.classLoader.IClass)
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public IMethod getCalleeTarget(CGNode caller, CallSiteReference site, IClass receiver) {
|
|
|
|
if (cha.isSubclassOf(receiver, cha.lookupClass(JavaScriptTypes.CodeBody))) {
|
|
|
|
// TODO better way to do these tests
|
|
|
|
String s = receiver.toString();
|
|
|
|
if (s.equals("function Lprologue.js/functionCall")) {
|
|
|
|
return getFunctionCallTarget(caller, site, receiver);
|
|
|
|
} else if (s.equals("function Lprologue.js/functionApply")) {
|
|
|
|
return getFunctionApplyTarget(caller, site, receiver);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return base.getCalleeTarget(caller, site, receiver);
|
|
|
|
}
|
|
|
|
|
|
|
|
private IMethod getFunctionApplyTarget(CGNode caller, CallSiteReference site, IClass receiver) {
|
|
|
|
System.err.println("TODO: handle Function.apply()");
|
|
|
|
return base.getCalleeTarget(caller, site, receiver);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-06 21:21:40 +00:00
|
|
|
* cache synthetic method for each arity of Function.call() invocation
|
|
|
|
*/
|
|
|
|
private final Map<Integer, JavaScriptSummarizedFunction> callModels = HashMapFactory.make();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* generate a synthetic method modeling the invocation of Function.call() at
|
|
|
|
* the site
|
|
|
|
*
|
2012-01-06 21:21:26 +00:00
|
|
|
* @param caller
|
|
|
|
* @param site
|
|
|
|
* @param receiver
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
private IMethod getFunctionCallTarget(CGNode caller, CallSiteReference site, IClass receiver) {
|
2012-01-06 21:21:40 +00:00
|
|
|
int nargs = getNumberOfArgsPassed(caller, site);
|
|
|
|
if (callModels.containsKey(nargs)) {
|
|
|
|
return callModels.get(nargs);
|
|
|
|
}
|
|
|
|
JSInstructionFactory insts = (JSInstructionFactory) receiver.getClassLoader().getInstructionFactory();
|
2012-01-06 21:21:26 +00:00
|
|
|
Atom atom = Atom.findOrCreateUnicodeAtom("call" + nargs);
|
|
|
|
Descriptor desc = Descriptor.findOrCreateUTF8(JavaScriptLoader.JS, "()LRoot;");
|
2012-01-06 21:21:40 +00:00
|
|
|
MethodReference ref = MethodReference.findOrCreate(receiver.getReference(), atom, desc);
|
2012-01-06 21:21:26 +00:00
|
|
|
JavaScriptSummary S = new JavaScriptSummary(ref, nargs);
|
|
|
|
|
|
|
|
// generate invocation instruction for the real method being invoked
|
|
|
|
int resultVal = nargs + 2;
|
|
|
|
CallSiteReference cs = new JSCallSiteReference(S.getNextProgramCounter());
|
2012-01-06 21:21:40 +00:00
|
|
|
int[] params = new int[nargs - 2];
|
2012-01-06 21:21:26 +00:00
|
|
|
for (int i = 0; i < params.length; i++) {
|
2012-01-06 21:21:40 +00:00
|
|
|
// add 3 to skip v1 (which points to Function.call() itself) and v2 (the
|
|
|
|
// real function being invoked)
|
|
|
|
params[i] = i + 3;
|
2012-01-06 21:21:26 +00:00
|
|
|
}
|
|
|
|
// function being invoked is in v2
|
2012-01-06 21:21:40 +00:00
|
|
|
S.addStatement(insts.Invoke(2, resultVal, params, resultVal + 1, site));
|
2012-01-06 21:21:26 +00:00
|
|
|
S.getNextProgramCounter();
|
2012-01-06 21:21:40 +00:00
|
|
|
|
2012-01-06 21:21:26 +00:00
|
|
|
S.addStatement(insts.ReturnInstruction(resultVal, false));
|
|
|
|
S.getNextProgramCounter();
|
|
|
|
|
|
|
|
JavaScriptSummarizedFunction t = new JavaScriptSummarizedFunction(ref, S, receiver);
|
2012-01-06 21:21:40 +00:00
|
|
|
callModels.put(nargs, t);
|
2012-01-06 21:21:26 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2012-01-06 21:21:40 +00:00
|
|
|
private int getNumberOfArgsPassed(CGNode caller, CallSiteReference site) {
|
|
|
|
IR callerIR = caller.getIR();
|
|
|
|
SSAAbstractInvokeInstruction callStmts[] = callerIR.getCalls(site);
|
|
|
|
assert callStmts.length == 1;
|
|
|
|
int nargs = callStmts[0].getNumberOfParameters();
|
|
|
|
return nargs;
|
|
|
|
}
|
|
|
|
|
2012-01-06 21:21:26 +00:00
|
|
|
}
|