Skip to content

Commit

Permalink
rename getNumberOfParameters to getNumberOfPositionalParameters since…
Browse files Browse the repository at this point in the history
… the Python front end now supports keyword parameters

support variable names in IR for synthetic summaries
  • Loading branch information
juliandolby committed Apr 12, 2018
1 parent 41d254d commit 436d316
Show file tree
Hide file tree
Showing 28 changed files with 89 additions and 41 deletions.
Expand Up @@ -55,7 +55,7 @@ protected boolean isFunctionConstructorInvoke(JavaScriptInvoke invk) {
if(fndef instanceof AstGlobalRead) {
AstGlobalRead agr = (AstGlobalRead)fndef;
if(agr.getGlobalName().equals("global Function")) {
if(invk.getNumberOfParameters() != 2)
if(invk.getNumberOfPositionalParameters() != 2)
return false;
// this may be a genuine use of "new Function()", not a declaration/expression
if(!symtab.isStringConstant(invk.getUse(1)))
Expand Down
Expand Up @@ -103,7 +103,7 @@ private static void addEdge(FlowGraph flowgraph, CallVertex c, FuncVertex callee
offset = 1;
}

for(int i=0;i<invk.getNumberOfParameters();++i) {
for(int i=0;i<invk.getNumberOfPositionalParameters();++i) {
// only flow receiver into 'this' if invk is, in fact, a method call
flowgraph.addEdge(factory.makeVarVertex(caller, invk.getUse(i)), factory.makeArgVertex(callee));
//if(i != 1 || !invk.getDeclaredTarget().getSelector().equals(AstMethodReference.fnSelector))
Expand All @@ -126,7 +126,7 @@ private static void addReflectiveCallEdge(FlowGraph flowgraph, CallVertex c, IPr
System.err.println("callees " + realCallees + " for " + caller);
for(FuncVertex realCallee: realCallees) {
// flow from arguments to parameters
for(int i=2;i<invk.getNumberOfParameters();++i)
for(int i=2;i<invk.getNumberOfPositionalParameters();++i)
flowgraph.addEdge(factory.makeVarVertex(caller, invk.getUse(i)), factory.makeParamVertex(realCallee, i-1));

// flow from return vertex to result vertex
Expand Down
Expand Up @@ -109,7 +109,7 @@ public void visitJavaScriptInvoke(JavaScriptInvoke invk) {
JavaScriptInvoke use_invk = (JavaScriptInvoke)use;

// yes, so add edges from arguments to parameters...
for(int i=2;i<use_invk.getNumberOfParameters();++i)
for(int i=2;i<use_invk.getNumberOfPositionalParameters();++i)
flowgraph.addEdge(factory.makeVarVertex(caller, use_invk.getUse(i)), factory.makeParamVertex(callee, i));

// ...and from return to result
Expand All @@ -129,7 +129,7 @@ public void visitJavaScriptInvoke(JavaScriptInvoke invk) {

// if it's not a local call, add flows from/to unknown
if(!(def instanceof JavaScriptInvoke) || !isFunctionConstructorInvoke((JavaScriptInvoke)def)) {
for(int i=1;i<invk.getNumberOfParameters();++i)
for(int i=1;i<invk.getNumberOfPositionalParameters();++i)
flowgraph.addEdge(factory.makeVarVertex(caller, invk.getUse(i)), factory.makeUnknownVertex());
flowgraph.addEdge(factory.makeUnknownVertex(), factory.makeVarVertex(caller, invk.getDef()));
}
Expand Down
Expand Up @@ -143,7 +143,7 @@ private void addCallEdge(FlowGraph flowgraph, CallVertex c, FuncVertex callee, S
offset = 1;
}

for(int i=0;i<invk.getNumberOfParameters();++i) {
for(int i=0;i<invk.getNumberOfPositionalParameters();++i) {
// only flow receiver into 'this' if invk is, in fact, a method call
flowgraph.addEdge(factory.makeVarVertex(caller, invk.getUse(i)), factory.makeArgVertex(callee));
if(i != 1 || !invk.getDeclaredTarget().getSelector().equals(AstMethodReference.fnSelector))
Expand All @@ -166,7 +166,7 @@ private void addReflectiveCallEdge(FlowGraph flowgraph, VarVertex reflectiveCall
FuncVertex caller = reflectiveCallee.getFunction();

// flow from arguments to parameters
for(int i=2;i<invk.getNumberOfParameters();++i) {
for(int i=2;i<invk.getNumberOfPositionalParameters();++i) {
addFlowEdge(flowgraph, factory.makeVarVertex(caller, invk.getUse(i)), factory.makeParamVertex(realCallee, i-1), worklist);

// flow from return vertex to result vertex
Expand Down
Expand Up @@ -392,7 +392,7 @@ public void visitJavaScriptInvoke(JavaScriptInvoke invk) {
flowgraph.addEdge(cs, factory.makeVarVertex(func, invk.getDef()));

// also passed as 'this' to constructor
if (invk.getNumberOfParameters() > 1) {
if (invk.getNumberOfPositionalParameters() > 1) {
flowgraph.addEdge(cs, factory.makeVarVertex(func, invk.getUse(0)));
}
}
Expand Down
Expand Up @@ -135,9 +135,9 @@ public Context getCalleeTarget(CGNode caller, CallSiteReference site, IMethod ca
int v = -1;
for (SSAAbstractInvokeInstruction x : caller.getIR().getCalls(site)) {
if (v == -1) {
v = x.getNumberOfParameters();
v = x.getNumberOfPositionalParameters();
} else {
if (v != x.getNumberOfParameters()) {
if (v != x.getNumberOfPositionalParameters()) {
return baseContext;
}
}
Expand Down
Expand Up @@ -1035,7 +1035,7 @@ public static void processCallingConstraintsInternal(AstSSAPropagationCallGraphB
}

int paramCount = targetST.getParameterValueNumbers().length;
int argCount = instruction.getNumberOfParameters();
int argCount = instruction.getNumberOfPositionalParameters();

// the first two arguments are the function object and the receiver, neither of which
// should become part of the arguments array
Expand Down
Expand Up @@ -46,7 +46,7 @@ public IMethod getCalleeTarget(CGNode caller, CallSiteReference site, IClass rec
IR callerIR = caller.getIR();
SSAAbstractInvokeInstruction callStmts[] = callerIR.getCalls(site);
assert callStmts.length == 1;
int nargs = callStmts[0].getNumberOfParameters();
int nargs = callStmts[0].getNumberOfPositionalParameters();
return constructors.findOrCreateConstructorMethod(callerIR, callStmts[0], receiver, nargs - 1);
} else {
return base.getCalleeTarget(caller, site, receiver);
Expand Down
Expand Up @@ -194,7 +194,7 @@ private static int getNumberOfArgsPassed(CGNode caller, CallSiteReference site)
IR callerIR = caller.getIR();
SSAAbstractInvokeInstruction callStmts[] = callerIR.getCalls(site);
assert callStmts.length == 1;
int nargs = callStmts[0].getNumberOfParameters();
int nargs = callStmts[0].getNumberOfPositionalParameters();
return nargs;
}

Expand Down
Expand Up @@ -132,7 +132,7 @@ private IntSet identifyDependentParameters(CGNode caller, CallSiteReference site
SSAAbstractInvokeInstruction inst = caller.getIR().getCalls(site)[0];
DefUse du = caller.getDU();

for(int i = 0; i < inst.getNumberOfParameters(); i++) {
for(int i = 0; i < inst.getNumberOfPositionalParameters(); i++) {
MutableIntSet values = IntSetUtil.make();
values.add(inst.getUse(i));
collectValues(du, du.getDef(inst.getUse(i)), values);
Expand Down
Expand Up @@ -48,7 +48,7 @@ public Position getEndPosition(SSASourcePositionMap positions) {
}

public int getNumberOfArguments() {
return invoke.getNumberOfParameters() - 2; // deduct one for the function object, one for the receiver
return invoke.getNumberOfPositionalParameters() - 2; // deduct one for the function object, one for the receiver
}

@Override
Expand Down
Expand Up @@ -80,7 +80,7 @@ public SSAInstruction copyForSSA(SSAInstructionFactory insts, int[] defs, int[]

@Override
public int getNumberOfUses() {
return getNumberOfParameters();
return getNumberOfPositionalParameters();
}

@Override
Expand Down Expand Up @@ -126,7 +126,7 @@ public void visit(IVisitor v) {
}

@Override
public int getNumberOfParameters() {
public int getNumberOfPositionalParameters() {
if (params == null) {
return 1;
} else {
Expand Down
Expand Up @@ -89,7 +89,7 @@ public SSAInstruction copyForSSA(SSAInstructionFactory insts, int[] defs, int[]
}

@Override
public int getNumberOfParameters() {
public int getNumberOfPositionalParameters() {
if (params == null) {
return 0;
} else {
Expand All @@ -107,7 +107,7 @@ public void visit(IVisitor v) {

@Override
public int getNumberOfUses() {
return getNumberOfParameters();
return getNumberOfPositionalParameters();
}

@Override
Expand All @@ -119,7 +119,7 @@ public int hashCode() {

@Override
public int getUse(int j) {
if (j < getNumberOfParameters())
if (j < getNumberOfPositionalParameters())
return params[j];
else {
return super.getUse(j);
Expand Down
Expand Up @@ -57,7 +57,7 @@ public static boolean isFakeRoot(CallGraph CG, CGNode node) {
* including the this pointer.
*/
public static int[] getParameterNumbers(SSAAbstractInvokeInstruction invokeInstruction) {
final int number = invokeInstruction.getNumberOfParameters();
final int number = invokeInstruction.getNumberOfPositionalParameters();
final int[] parameterNumbers = new int[number];
assert (parameterNumbers.length == invokeInstruction.getNumberOfUses());

Expand Down
Expand Up @@ -302,10 +302,10 @@ public String toString() {
@Override
public Set<CGNode> getNodes(MethodReference m) {
IMethod im = getClassHierarchy().resolveMethod(m);
if (im == null) {
return Collections.emptySet();
if (im != null) {
m = im.getReference();
}
Set<CGNode> result = mr2Nodes.get(im.getReference());
Set<CGNode> result = mr2Nodes.get(m);
Set<CGNode> empty = Collections.emptySet();
return (result == null) ? empty : result;
}
Expand Down
Expand Up @@ -1617,7 +1617,7 @@ protected void processCallingConstraints(CGNode caller, SSAAbstractInvokeInstruc
// }
// } else {
// generate contraints from parameter passing
int nUses = instruction.getNumberOfParameters();
int nUses = instruction.getNumberOfPositionalParameters();
int nExpected = target.getMethod().getNumberOfParameters();

/*
Expand All @@ -1635,7 +1635,7 @@ protected void processCallingConstraints(CGNode caller, SSAAbstractInvokeInstruc
// TODO: we need much more precise filters than cones in order to handle
// the various types of dispatch logic. We need a filter that expresses
// "the set of types s.t. x.foo resolves to y.foo."
for (int i = 0; i < instruction.getNumberOfParameters(); i++) {
for (int i = 0; i < instruction.getNumberOfPositionalParameters(); i++) {
if (target.getMethod().getParameterType(i).isReferenceType()) {
PointerKey formal = getTargetPointerKey(target, i);
if (constParams != null && constParams[i] != null) {
Expand Down
2 changes: 1 addition & 1 deletion com.ibm.wala.core/src/com/ibm/wala/ipa/slicer/SDG.java
Expand Up @@ -696,7 +696,7 @@ public boolean hasEdge(Statement src, Statement dst) {
// don't track reflection into reflective invokes
return false;
}
for (int i = 0; i < call.getNumberOfParameters(); i++) {
for (int i = 0; i < call.getNumberOfPositionalParameters(); i++) {
if (call.getUse(i) == caller.getValueNumber()) {
if (callee.getValueNumber() == i + 1) {
return true;
Expand Down
Expand Up @@ -186,7 +186,7 @@ protected SyntheticMethod findOrCreateSyntheticMethod(MethodReference m, boolean
syntheticMethods.put(m, null);
return null;
}
SummarizedMethod n = new SummarizedMethod(m, summ, C);
SummarizedMethod n = new SummarizedMethodWithNames(m, summ, C);
syntheticMethods.put(m, n);
return n;
} else {
Expand Down
Expand Up @@ -151,7 +151,7 @@ public Collection<IField> getDeclaredInstanceFields() {

private Map<Atom,IField> makeFields() {
Map<Atom,IField> result = HashMapFactory.make();
for(int i = 0; i < invoke.getNumberOfParameters(); i++) {
for(int i = 0; i < invoke.getNumberOfPositionalParameters(); i++) {
final int yuck = i;
result.put(Atom.findOrCreateUnicodeAtom("c" + yuck), new IField() {
@Override
Expand Down Expand Up @@ -248,8 +248,8 @@ private IMethod makeTrampoline() {
MethodSummary summary = new MethodSummary(ref);

int inst = 0;
int args = invoke.getNumberOfParameters(), v = args + 1;
for(int i = 0; i < invoke.getNumberOfParameters(); i++) {
int args = invoke.getNumberOfPositionalParameters(), v = args + 1;
for(int i = 0; i < invoke.getNumberOfPositionalParameters(); i++) {
Atom f = Atom.findOrCreateUnicodeAtom("c" + i);
summary.addStatement(insts.GetInstruction(inst++, v++, 1, getField(f).getReference()));
}
Expand All @@ -273,11 +273,11 @@ private IMethod makeTrampoline() {

int numParams = getClassHierarchy().resolveMethod(callee).getNumberOfParameters();
int params[] = new int[ numParams ];
for(int i = isNew? 1: 0; i < invoke.getNumberOfParameters(); i++) {
for(int i = isNew? 1: 0; i < invoke.getNumberOfPositionalParameters(); i++) {
params[i] = args + i + 1;
}
int n = 2;
for(int i = invoke.getNumberOfParameters(); i < numParams; i++) {
for(int i = invoke.getNumberOfPositionalParameters(); i < numParams; i++) {
params[i] = n++;
}

Expand Down
Expand Up @@ -20,6 +20,7 @@
import com.ibm.wala.types.MethodReference;
import com.ibm.wala.types.TypeReference;
import com.ibm.wala.util.collections.HashMapFactory;
import com.ibm.wala.util.strings.Atom;
import com.ibm.wala.util.warnings.Warning;

/**
Expand Down Expand Up @@ -69,13 +70,30 @@ public class MethodSummary {
*/
private boolean isFactory = false;

/**
* Known names for values
*/
private Map<Integer, Atom> valueNames = null;

public MethodSummary(MethodReference method) {
if (method == null) {
throw new IllegalArgumentException("null method");
}
this.method = method;
}

public void setValueNames(Map<Integer, Atom> nameTable) {
this.valueNames = nameTable;
}

public Map<Integer, Atom> getValueNames() {
return valueNames;
}

public Atom getValue(Integer v) {
return valueNames != null && valueNames.containsKey(v)? valueNames.get(v): null;
}

public int getNumberOfStatements() {
return (statements == null ? 0 : statements.size());
}
Expand Down
Expand Up @@ -115,4 +115,9 @@ public TypeReference getParameterType(int i) {
return summary.getParameterType(i);
}

@Override
public String getLocalVariableName(int bcIndex, int localNumber) {
return summary.getValue(localNumber).toString();
}

}
Expand Up @@ -73,6 +73,10 @@ public class SummarizedMethodWithNames extends SummarizedMethod {
private final MethodSummary summary;
private final Map<Integer, Atom> localNames;

public SummarizedMethodWithNames(MethodReference ref, MethodSummary summary, IClass declaringClass) {
this(ref, summary, declaringClass, summary.getValueNames());
}

public SummarizedMethodWithNames(MethodReference ref, MethodSummary summary, IClass declaringClass, Map<Integer, Atom> localNames)
throws NullPointerException {
super(ref, summary, declaringClass);
Expand Down
Expand Up @@ -20,6 +20,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
Expand Down Expand Up @@ -183,10 +184,12 @@ public class XMLMethodSummaryReader implements BytecodeConstants {

private final static String A_NUM_ARGS = "numArgs";

private final static String A_PARAM_NAMES = "paramNames";

private final static String V_NULL = "null";

private final static String V_TRUE = "true";

public XMLMethodSummaryReader(InputStream xmlFile, AnalysisScope scope) {
super();
if (xmlFile == null) {
Expand Down Expand Up @@ -924,6 +927,24 @@ private void startMethod(Attributes atts) {
for (int i = 0; i < nParams; i++) {
symbolTable.put("arg" + i, new Integer(i + 1));
}

int pn = 1;
String paramDescString = atts.getValue(A_PARAM_NAMES);
if (paramDescString != null) {
StringTokenizer paramNames = new StringTokenizer(paramDescString);
while (paramNames.hasMoreTokens()) {
symbolTable.put(paramNames.nextToken(), pn++);
}
}

Map<Integer,Atom> nameTable = HashMapFactory.make();
for(Map.Entry<String, Integer> x : symbolTable.entrySet()) {
if (! x.getKey().startsWith("arg")) {
nameTable.put(x.getValue(), Atom.findOrCreateUnicodeAtom(x.getKey()));
}
}

governingMethod.setValueNames(nameTable);
}

/**
Expand Down
Expand Up @@ -139,7 +139,7 @@ public int getDef() {
/**
* How many parameters does this call specify?
*/
public abstract int getNumberOfParameters();
public abstract int getNumberOfPositionalParameters();

/**
* How many distinct values does this call return?
Expand Down Expand Up @@ -199,9 +199,9 @@ public String toString(SymbolTable symbolTable) {
s.append(" ");
s.append(site.getDeclaredTarget().toString());

if (getNumberOfParameters() > 0) {
if (getNumberOfPositionalParameters() > 0) {
s.append(" ").append(getValueString(symbolTable, getUse(0)));
for (int i = 1; i < getNumberOfParameters(); i++) {
for (int i = 1; i < getNumberOfPositionalParameters(); i++) {
s.append(",").append(getValueString(symbolTable, getUse(i)));
}
}
Expand Down

0 comments on commit 436d316

Please sign in to comment.