}
set.add(neighbor);
- System.out.println("add a new neighbor " + neighbor + " to " + node);
+// System.out.println("add a new neighbor " + neighbor + " to " + node);
}
public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
- FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
- FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
+ FlowNode fromNode = getFlowNode(fromDescTuple);
+ FlowNode toNode = getFlowNode(toDescTuple);
- System.out.println("create an edge from " + fromNode + " to " + toNode);
+ // System.out.println("create an edge from " + fromNode + " to " + toNode);
int fromTupleSize = fromDescTuple.size();
NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
fromNode.addOutEdge(edge);
- System.out.println("add a new edge=" + edge);
+// System.out.println("add a new edge=" + edge);
}
getFlowNode(baseTuple).addFieldNode(node);
}
- System.out.println("Creating new node=" + node);
+// System.out.println("Creating new node=" + node);
return node;
} else {
return mapDescTupleToInferNode.get(tuple);
NTuple<Location> locTuple = new NTuple<Location>();
ClassDescriptor cd = null;
- for (int i = 0; i < descTuple.size(); i++) {
- Descriptor curDesc = descTuple.get(i);
- Location loc;
- if (i == 0) {
- loc = new Location(md, curDesc.getSymbol());
- loc.setLocDescriptor(md);
- cd = ((VarDescriptor) curDesc).getType().getClassDesc();
- } else {
- loc = new Location(cd, curDesc.getSymbol());
- loc.setLocDescriptor(curDesc);
- cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
+ Descriptor localDesc = fn.getDescTuple().get(0);
+ if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
+ Location topLoc = new Location(md, Location.TOP);
+ locTuple.add(topLoc);
+ } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
+ Location topLoc = new Location(md, LocationInference.GLOBALLOC);
+ locTuple.add(topLoc);
+ } else {
+ // normal case
+ for (int i = 0; i < descTuple.size(); i++) {
+ Descriptor curDesc = descTuple.get(i);
+ Location loc;
+ if (i == 0) {
+ loc = new Location(md, curDesc.getSymbol());
+ loc.setLocDescriptor(md);
+ cd = ((VarDescriptor) curDesc).getType().getClassDesc();
+ } else {
+ loc = new Location(cd, curDesc.getSymbol());
+ loc.setLocDescriptor(curDesc);
+ cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
+ }
+ locTuple.add(loc);
}
- locTuple.add(loc);
}
+
mapFlowNodeToLocTuple.put(fn, locTuple);
}
return mapFlowNodeToLocTuple.get(fn);
import IR.Tree.SubBlockNode;
import IR.Tree.SwitchStatementNode;
import IR.Tree.TertiaryNode;
+import IR.Tree.TreeNode;
public class LocationInference {
private Map<MethodDescriptor, Set<MethodDescriptor>> mapMethodDescToPossibleMethodDescSet;
+ public static final String GLOBALLOC = "GLOBALLOC";
+
+ public static final String TOPLOC = "TOPLOC";
+
+ public static final Descriptor GLOBALDESC = new NameDescriptor(GLOBALLOC);
+
+ public static final Descriptor TOPDESC = new NameDescriptor(TOPLOC);
+
boolean debug = true;
public LocationInference(SSJavaAnalysis ssjava, State state) {
for (Iterator iterator2 = paramLocNameSet1.iterator(); iterator2.hasNext();) {
String locName2 = (String) iterator2.next();
-
if (!locName1.equals(locName2)) {
boolean r1 = lattice1.isGreaterThan(locName1, locName2);
// from the callee
analyzeLatticeMethodInvocationNode(md);
- // grab the this location if the method use the 'this' reference
+ // set the this location
String thisLocSymbol = md.getThis().getSymbol();
- // if (methodLattice.getKeySet().contains(thisLocSymbol)) {
methodInfo.setThisLocName(thisLocSymbol);
- // }
+
+ // set the global location
+ methodInfo.setGlobalLocName(LocationInference.GLOBALLOC);
// visit each node of method flow graph
FlowGraph fg = getFlowGraph(md);
for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
FlowNode rtrNode = (FlowNode) iterator.next();
- String localSymbol = rtrNode.getDescTuple().get(0).getSymbol();
+ String localSymbol =
+ methodInfo.getInferLocation(rtrNode.getDescTuple().get(0)).get(0).getLocIdentifier();
returnVarSymbolSet.add(localSymbol);
}
String srcSymbol = methodInfo.getInferLocation(srcDesc).get(0).getLocIdentifier();
String dstSymbol = methodInfo.getInferLocation(dstDesc).get(0).getLocIdentifier();
-
if (srcNode.isParameter()) {
int paramIdx = flowGraph.getParamIdx(srcNode.getDescTuple());
methodInfo.addParameter(srcSymbol, srcDesc, paramIdx);
}
-
}
private void recursivelyAddCompositeRelation(MethodDescriptor md, FlowGraph flowGraph,
}
});
-
for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
FlowNode reachableNode = (FlowNode) iterator2.next();
if (reachableNode.getDescTuple().size() == 1) {
}
-
if (!reachableCommonPrefixSet.isEmpty()) {
// found reachable nodes that start with the prefix curPrefix
// need to assign a composite location
Location fieldLoc = new Location(desc, newLocSymbol);
inferLocation.addLocation(fieldLoc);
-
methodInfo.mapDescriptorToLocation(localVarDesc, inferLocation);
methodInfo.removeMaplocalVarToLocSet(localVarDesc);
String localLocName = methodInfo.getInferLocation(localVarDesc).get(0).getLocIdentifier();
return true;
-
}
newlyInsertedLocName = inferLocation.get(inferLocation.getSize() - 1).getLocIdentifier();
Location loc = tuple.get(idx);
String higher = locInfo.getFieldInferLocation(loc.getLocDescriptor()).getLocIdentifier();
- System.out.println("here3");
-
+ System.out.println("--");
+ System.out.println("add in-flow relation:");
addRelationHigherToLower(lattice, locInfo, higher, newlyInsertedLocName);
}
-
+ System.out.println("end of add-inflow relation");
for (Iterator iterator = localInNodeSet.iterator(); iterator.hasNext();) {
FlowNode localNode = (FlowNode) iterator.next();
&& inNodeInferLoc.getSize() == (curPrefix.size() + 1)) {
String higher = inNodeInferLoc.get(inNodeInferLoc.getSize() - 1).getLocIdentifier();
if (!higher.equals(newlyInsertedLocName)) {
-
+ System.out.println("add localInNodeSet relation:");
addRelationHigherToLower(lattice, locInfo, higher, newlyInsertedLocName);
}
} else {
Location loc = tuple.get(idx);
String lower = locInfo.getFieldInferLocation(loc.getLocDescriptor()).getLocIdentifier();
// lattice.addRelationHigherToLower(newlyInsertedLocName, lower);
-
+ System.out.println("add out-flow relation:");
addRelationHigherToLower(lattice, locInfo, newlyInsertedLocName, lower);
}
+ System.out.println("end of add out-flow relation");
for (Iterator iterator = localOutNodeSet.iterator(); iterator.hasNext();) {
FlowNode localOutNode = (FlowNode) iterator.next();
&& outNodeInferLoc.getSize() == (curPrefix.size() + 1)) {
String lower = outNodeInferLoc.get(outNodeInferLoc.getSize() - 1).getLocIdentifier();
+ System.out.println("add outNodeInferLoc relation:");
addRelationHigherToLower(lattice, locInfo, newlyInsertedLocName, lower);
for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
Descriptor desc = (Descriptor) iterator.next();
- if (desc instanceof VarDescriptor) {
+ if (desc.equals(LocationInference.GLOBALDESC)) {
+ return true;
+ } else if (desc instanceof VarDescriptor) {
if (!((VarDescriptor) desc).getType().isPrimitive()) {
return true;
}
private void addRelationHigherToLower(SSJavaLattice<String> lattice, LocationInfo locInfo,
String higher, String lower) {
+ // if (higher.equals(lower) && lattice.isSharedLoc(higher)) {
+ // return;
+ // }
+
Set<String> cycleElementSet = lattice.getPossibleCycleElements(higher, lower);
+ System.out.println("#Check cycle=" + lower + " < " + higher);
+ System.out.println("#cycleElementSet=" + cycleElementSet);
boolean hasNonPrimitiveElement = false;
for (Iterator iterator = cycleElementSet.iterator(); iterator.hasNext();) {
locInfo.mergeMapping(oldLocSymbol, newSharedLoc);
}
+ lattice.addSharedLoc(newSharedLoc);
} else if (!lattice.isGreaterThan(higher, lower)) {
lattice.addRelationHigherToLower(higher, lower);
private void prefixSanityCheck(List<NTuple<Location>> prefixList, int curIdx,
FlowGraph flowGraph, Set<FlowNode> reachableNodeSet) {
-
NTuple<Location> curPrefix = prefixList.get(curIdx);
for (int i = curIdx + 1; i < prefixList.size(); i++) {
private void extractRelationFromFieldFlows(ClassDescriptor cd, FlowNode srcNode,
FlowNode dstNode, int idx) {
-
if (srcNode.getDescTuple().get(idx).equals(dstNode.getDescTuple().get(idx))
&& srcNode.getDescTuple().size() > (idx + 1) && dstNode.getDescTuple().size() > (idx + 1)) {
// value flow between fields: we don't need to add a binary relation
SSJavaLattice<String> fieldLattice = getFieldLattice(cd);
LocationInfo fieldInfo = getFieldLocationInfo(cd);
-
String srcSymbol = fieldInfo.getFieldInferLocation(srcFieldDesc).getLocIdentifier();
String dstSymbol = fieldInfo.getFieldInferLocation(dstFieldDesc).getLocIdentifier();
ExpressionNode returnExp = rn.getReturnExpression();
- NodeTupleSet nodeSet = new NodeTupleSet();
- analyzeFlowExpressionNode(md, nametable, returnExp, nodeSet, false);
+ if (returnExp != null) {
+ NodeTupleSet nodeSet = new NodeTupleSet();
+ analyzeFlowExpressionNode(md, nametable, returnExp, nodeSet, false);
- FlowGraph fg = getFlowGraph(md);
+ FlowGraph fg = getFlowGraph(md);
- // annotate the elements of the node set as the return location
- for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
- NTuple<Descriptor> returnDescTuple = (NTuple<Descriptor>) iterator.next();
- fg.setReturnFlowNode(returnDescTuple);
- for (Iterator iterator2 = implicitFlowTupleSet.iterator(); iterator2.hasNext();) {
- NTuple<Descriptor> implicitFlowDescTuple = (NTuple<Descriptor>) iterator2.next();
- fg.addValueFlowEdge(implicitFlowDescTuple, returnDescTuple);
+ // annotate the elements of the node set as the return location
+ for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
+ NTuple<Descriptor> returnDescTuple = (NTuple<Descriptor>) iterator.next();
+ fg.setReturnFlowNode(returnDescTuple);
+ for (Iterator iterator2 = implicitFlowTupleSet.iterator(); iterator2.hasNext();) {
+ NTuple<Descriptor> implicitFlowDescTuple = (NTuple<Descriptor>) iterator2.next();
+ fg.addValueFlowEdge(implicitFlowDescTuple, returnDescTuple);
+ }
}
}
if (min.getExpression() != null) {
NodeTupleSet baseNodeSet = new NodeTupleSet();
- analyzeFlowExpressionNode(calleeMD, nametable, min.getExpression(), baseNodeSet, null,
+ analyzeFlowExpressionNode(md, nametable, min.getExpression(), baseNodeSet, null,
implicitFlowTupleSet, false);
} else {
FieldDescriptor fd = (FieldDescriptor) d;
if (fd.isStatic()) {
if (fd.isFinal()) {
- // if it is 'static final', the location has TOP since no one can
- // change its value
- // loc.addLocation(Location.createTopLocation(md));
- // return loc;
+ // if it is 'static final', assign the default TOP LOCATION
+ // DESCRIPTOR
+ base.add(TOPDESC);
+ return base;
} else {
- // if 'static', the location has pre-assigned global loc
- // MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
- // String globalLocId = localLattice.getGlobalLoc();
- // if (globalLocId == null) {
- // throw new
- // Error("Global location element is not defined in the method " +
- // md);
- // }
- // Location globalLoc = new Location(md, globalLocId);
- //
- // loc.addLocation(globalLoc);
+ // if 'static', assign the default GLOBAL LOCATION to the first
+ // element of the tuple
+ base.add(GLOBALDESC);
}
} else {
// the location of field access starts from this, followed by field
base.add(fd);
} else if (d == null) {
// access static field
+ base.add(GLOBALDESC);
+ // base.add(nn.getField());
+ return base;
+
// FieldDescriptor fd = nn.getField();addFlowGraphEdge
//
// MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
}
}
- // if (left instanceof ArrayAccessNode) {
- // ArrayAccessNode aan = (ArrayAccessNode) left;
- // left = aan.getExpression();
- // }
+ if (left instanceof ArrayAccessNode) {
+ ArrayAccessNode aan = (ArrayAccessNode) left;
+ left = aan.getExpression();
+ }
// fanNodeSet
base =
analyzeFlowExpressionNode(md, nametable, left, nodeSet, base, implicitFlowTupleSet, false);
if (!left.getType().isPrimitive()) {
if (fd.getSymbol().equals("length")) {
- // TODO
- // array.length access, return the location of the array
- // return loc;
+ // array.length access, just have the location of the array
+ } else {
+ base.add(fd);
}
- base.add(fd);
}
getFlowGraph(md).createNewFlowNode(base);
}
+ private void debug_printTreeNode(TreeNode tn) {
+
+ System.out.println("DEBUG: " + tn.printNode(0) + " line#=" + tn.getNumLine());
+
+ }
+
private void analyzeFlowAssignmentNode(MethodDescriptor md, SymbolTable nametable,
AssignmentNode an, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
- // System.out.println("#an=" + an.printNode(0) + " an src=" +
- // an.getSrc().printNode(0) + " dst="
- // + an.getDest().printNode(0));
NodeTupleSet nodeSetRHS = new NodeTupleSet();
NodeTupleSet nodeSetLHS = new NodeTupleSet();