+ while (!toAnalyzeIsEmpty()) {
+ ClassDescriptor cd = toAnalyzeNext();
+ setupToAnalazeMethod(cd);
+
+ SSJavaLattice<String> classLattice = cd2lattice.get(cd);
+ if (classLattice != null) {
+ System.out.println("@@@check lattice=" + cd);
+ checkLatticeProperty(cd, classLattice);
+ }
+
+ while (!toAnalyzeMethodIsEmpty()) {
+ MethodDescriptor md = toAnalyzeMethodNext();
+ SSJavaLattice<String> methodLattice = md2lattice.get(md);
+ if (methodLattice != null) {
+ System.out.println("@@@check lattice=" + md);
+ checkLatticeProperty(md, methodLattice);
+ }
+ }
+ }
+
+ setupToAnalyze();
+
+ while (!toAnalyzeIsEmpty()) {
+ ClassDescriptor cd = toAnalyzeNext();
+
+ setupToAnalazeMethod(cd);
+
+ SSJavaLattice<String> classLattice = cd2lattice.get(cd);
+ if (classLattice != null) {
+ classLattice.removeRedundantEdges();
+ }
+
+ while (!toAnalyzeMethodIsEmpty()) {
+ MethodDescriptor md = toAnalyzeMethodNext();
+ SSJavaLattice<String> methodLattice = md2lattice.get(md);
+ if (methodLattice != null) {
+ methodLattice.removeRedundantEdges();
+ }
+ }
+ }
+
+ }
+
+ private boolean checkLatticeProperty(Descriptor d, SSJavaLattice<String> lattice) {
+ // if two elements has the same incoming node set,
+ // we need to merge two elements ...
+
+ boolean isUpdated;
+ boolean isModified = false;
+ do {
+ isUpdated = removeNodeSharingSameIncomingNodes(d, lattice);
+ if (!isModified && isUpdated) {
+ isModified = true;
+ }
+ } while (isUpdated);
+
+ return isModified;
+ }
+
+ private boolean removeNodeSharingSameIncomingNodes(Descriptor d, SSJavaLattice<String> lattice) {
+ LocationInfo locInfo = getLocationInfo(d);
+ Map<String, Set<String>> map = lattice.getIncomingElementMap();
+ Set<String> keySet = map.keySet();
+ for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
+ String key = (String) iterator.next();
+ Set<String> incomingSetKey = map.get(key);
+
+ // System.out.println("key=" + key + " incomingSetKey=" +
+ // incomingSetKey);
+ if (incomingSetKey.size() > 0) {
+ for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
+ String cur = (String) iterator2.next();
+ if (!cur.equals(key)) {
+ Set<String> incomingSetCur = map.get(cur);
+ if (incomingSetCur.equals(incomingSetKey)) {
+ if (!(incomingSetCur.size() == 1 && incomingSetCur.contains(lattice.getTopItem()))) {
+ // NEED TO MERGE HERE!!!!
+ System.out.println("@@@Try merge=" + cur + " " + key);
+
+ Set<String> mergeSet = new HashSet<String>();
+ mergeSet.add(cur);
+ mergeSet.add(key);
+
+ String newMergeLoc = "MLoc" + (SSJavaLattice.seed++);
+
+ System.out.println("---ASSIGN NEW MERGE LOC=" + newMergeLoc + " to " + mergeSet);
+ lattice.mergeIntoNewLocation(mergeSet, newMergeLoc);
+
+ for (Iterator miterator = mergeSet.iterator(); miterator.hasNext();) {
+ String oldLocSymbol = (String) miterator.next();
+
+ Set<Pair<Descriptor, Descriptor>> inferLocSet =
+ locInfo.getRelatedInferLocSet(oldLocSymbol);
+ System.out.println("---update related locations=" + inferLocSet
+ + " oldLocSymbol=" + oldLocSymbol);
+
+ for (Iterator miterator2 = inferLocSet.iterator(); miterator2.hasNext();) {
+ Pair<Descriptor, Descriptor> pair =
+ (Pair<Descriptor, Descriptor>) miterator2.next();
+ Descriptor enclosingDesc = pair.getFirst();
+ Descriptor desc = pair.getSecond();
+
+ System.out.println("---inferLoc pair=" + pair);
+
+ CompositeLocation inferLoc =
+ getLocationInfo(enclosingDesc).getInferLocation(desc);
+ System.out.println("oldLoc=" + inferLoc);
+ // if (curMethodInfo.md.equals(enclosingDesc)) {
+ // inferLoc = curMethodInfo.getInferLocation(desc);
+ // } else {
+ // inferLoc =
+ // getLocationInfo(enclosingDesc).getInferLocation(desc);
+ // }
+
+ Location locElement = inferLoc.get(inferLoc.getSize() - 1);
+
+ locElement.setLocIdentifier(newMergeLoc);
+ locInfo.addMapLocSymbolToRelatedInferLoc(newMergeLoc, enclosingDesc, desc);
+
+ // if (curMethodInfo.md.equals(enclosingDesc)) {
+ // inferLoc = curMethodInfo.getInferLocation(desc);
+ // } else {
+ // inferLoc =
+ // getLocationInfo(enclosingDesc).getInferLocation(desc);
+ // }
+
+ inferLoc = getLocationInfo(enclosingDesc).getInferLocation(desc);
+ System.out.println("---New Infer Loc=" + inferLoc);
+
+ }
+
+ locInfo.removeRelatedInferLocSet(oldLocSymbol, newMergeLoc);
+
+ }
+
+ for (Iterator iterator3 = mergeSet.iterator(); iterator3.hasNext();) {
+ String oldLoc = (String) iterator3.next();
+ lattice.remove(oldLoc);
+ }
+ return true;
+ }
+ }
+ }
+ }
+ }
+
+ }
+ return false;
+ }
+
+ private void checkLattices() {
+
+ LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
+
+ // current descriptors to visit in fixed-point interprocedural analysis,
+ // prioritized by
+ // dependency in the call graph
+ methodDescriptorsToVisitStack.clear();
+
+ // descriptorListToAnalyze.removeFirst();
+
+ Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
+ methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
+
+ while (!descriptorListToAnalyze.isEmpty()) {
+ MethodDescriptor md = descriptorListToAnalyze.removeFirst();
+ checkLatticesOfVirtualMethods(md);
+ }
+
+ }
+
+ private void debug_writeLatticeDotFile() {
+ // generate lattice dot file
+
+ setupToAnalyze();
+
+ while (!toAnalyzeIsEmpty()) {
+ ClassDescriptor cd = toAnalyzeNext();
+
+ setupToAnalazeMethod(cd);
+
+ SSJavaLattice<String> classLattice = cd2lattice.get(cd);
+ if (classLattice != null) {
+ ssjava.writeLatticeDotFile(cd, null, classLattice);
+ debug_printDescriptorToLocNameMapping(cd);
+ }
+
+ while (!toAnalyzeMethodIsEmpty()) {
+ MethodDescriptor md = toAnalyzeMethodNext();
+ SSJavaLattice<String> methodLattice = md2lattice.get(md);
+ if (methodLattice != null) {
+ ssjava.writeLatticeDotFile(cd, md, methodLattice);
+ debug_printDescriptorToLocNameMapping(md);
+ }
+ }
+ }
+
+ }
+
+ private void debug_printDescriptorToLocNameMapping(Descriptor desc) {
+
+ LocationInfo info = getLocationInfo(desc);
+ System.out.println("## " + desc + " ##");
+ System.out.println(info.getMapDescToInferLocation());
+ LocationInfo locInfo = getLocationInfo(desc);
+ System.out.println("mapping=" + locInfo.getMapLocSymbolToDescSet());
+ System.out.println("###################");
+
+ }
+
+ private void inferLattices() {
+
+ // do fixed-point analysis
+
+ ssjava.init();
+ LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
+
+ // Collections.sort(descriptorListToAnalyze, new
+ // Comparator<MethodDescriptor>() {
+ // public int compare(MethodDescriptor o1, MethodDescriptor o2) {
+ // return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
+ // }
+ // });
+
+ // current descriptors to visit in fixed-point interprocedural analysis,
+ // prioritized by
+ // dependency in the call graph
+ methodDescriptorsToVisitStack.clear();
+
+ // descriptorListToAnalyze.removeFirst();
+
+ Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
+ methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
+
+ while (!descriptorListToAnalyze.isEmpty()) {
+ MethodDescriptor md = descriptorListToAnalyze.removeFirst();
+ methodDescriptorsToVisitStack.add(md);
+ }
+
+ // analyze scheduled methods until there are no more to visit
+ while (!methodDescriptorsToVisitStack.isEmpty()) {
+ // start to analyze leaf node
+ MethodDescriptor md = methodDescriptorsToVisitStack.pop();
+
+ SSJavaLattice<String> methodLattice =
+ new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
+
+ MethodLocationInfo methodInfo = new MethodLocationInfo(md);
+ curMethodInfo = methodInfo;
+
+ System.out.println();
+ System.out.println("SSJAVA: Inferencing the lattice from " + md);
+
+ try {
+ analyzeMethodLattice(md, methodLattice, methodInfo);
+ } catch (CyclicFlowException e) {
+ throw new Error("Fail to generate the method lattice for " + md);
+ }
+
+ SSJavaLattice<String> prevMethodLattice = getMethodLattice(md);
+ MethodLocationInfo prevMethodInfo = getMethodLocationInfo(md);
+
+ if ((!methodLattice.equals(prevMethodLattice)) || (!methodInfo.equals(prevMethodInfo))) {
+
+ setMethodLattice(md, methodLattice);
+ setMethodLocInfo(md, methodInfo);
+
+ // results for callee changed, so enqueue dependents caller for
+ // further analysis
+ Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
+ while (depsItr.hasNext()) {
+ MethodDescriptor methodNext = depsItr.next();
+ if (!methodDescriptorsToVisitStack.contains(methodNext)
+ && methodDescriptorToVistSet.contains(methodNext)) {
+ methodDescriptorsToVisitStack.add(methodNext);
+ }
+ }
+
+ }
+
+ }
+
+ }
+
+ private void calculateExtraLocations() {
+ LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
+ for (Iterator iterator = descriptorListToAnalyze.iterator(); iterator.hasNext();) {
+ MethodDescriptor md = (MethodDescriptor) iterator.next();
+ calculateExtraLocations(md);
+ }
+ }
+
+ private void setMethodLocInfo(MethodDescriptor md, MethodLocationInfo methodInfo) {
+ mapMethodDescToMethodLocationInfo.put(md, methodInfo);
+ }
+
+ private void checkLatticesOfVirtualMethods(MethodDescriptor md) {
+
+ if (!md.isStatic()) {
+ Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
+ setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
+
+ for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
+ MethodDescriptor mdCallee = (MethodDescriptor) iterator.next();
+ if (!md.equals(mdCallee)) {
+ checkConsistency(md, mdCallee);
+ }
+ }
+
+ }
+
+ }
+
+ private void checkConsistency(MethodDescriptor md1, MethodDescriptor md2) {
+
+ // check that two lattice have the same relations between parameters(+PC
+ // LOC, GLOBAL_LOC RETURN LOC)
+
+ List<CompositeLocation> list1 = new ArrayList<CompositeLocation>();
+ List<CompositeLocation> list2 = new ArrayList<CompositeLocation>();
+
+ MethodLocationInfo locInfo1 = getMethodLocationInfo(md1);
+ MethodLocationInfo locInfo2 = getMethodLocationInfo(md2);
+
+ Map<Integer, CompositeLocation> paramMap1 = locInfo1.getMapParamIdxToInferLoc();
+ Map<Integer, CompositeLocation> paramMap2 = locInfo2.getMapParamIdxToInferLoc();
+
+ int numParam = locInfo1.getMapParamIdxToInferLoc().keySet().size();
+
+ // add location types of paramters
+ for (int idx = 0; idx < numParam; idx++) {
+ list1.add(paramMap1.get(Integer.valueOf(idx)));
+ list2.add(paramMap2.get(Integer.valueOf(idx)));
+ }
+
+ // add program counter location
+ list1.add(locInfo1.getPCLoc());
+ list2.add(locInfo2.getPCLoc());
+
+ if (!md1.getReturnType().isVoid()) {
+ // add return value location
+ CompositeLocation rtrLoc1 = getMethodLocationInfo(md1).getReturnLoc();
+ CompositeLocation rtrLoc2 = getMethodLocationInfo(md2).getReturnLoc();
+ list1.add(rtrLoc1);
+ list2.add(rtrLoc2);
+ }
+
+ // add global location type
+ if (md1.isStatic()) {
+ CompositeLocation globalLoc1 =
+ new CompositeLocation(new Location(md1, locInfo1.getGlobalLocName()));
+ CompositeLocation globalLoc2 =
+ new CompositeLocation(new Location(md2, locInfo2.getGlobalLocName()));
+ list1.add(globalLoc1);
+ list2.add(globalLoc2);
+ }
+
+ for (int i = 0; i < list1.size(); i++) {
+ CompositeLocation locA1 = list1.get(i);
+ CompositeLocation locA2 = list2.get(i);
+ for (int k = 0; k < list1.size(); k++) {
+ if (i != k) {
+ CompositeLocation locB1 = list1.get(k);
+ CompositeLocation locB2 = list2.get(k);
+ boolean r1 = isGreaterThan(getLattice(md1), locA1, locB1);
+
+ boolean r2 = isGreaterThan(getLattice(md1), locA2, locB2);
+
+ if (r1 != r2) {
+ throw new Error("The method " + md1 + " is not consistent with the method " + md2
+ + ".:: They have a different ordering relation between locations (" + locA1 + ","
+ + locB1 + ") and (" + locA2 + "," + locB2 + ").");
+ }
+ }
+ }
+ }
+
+ }
+
+ private String getSymbol(int idx, FlowNode node) {
+ Descriptor desc = node.getDescTuple().get(idx);
+ return desc.getSymbol();
+ }
+
+ private Descriptor getDescriptor(int idx, FlowNode node) {
+ Descriptor desc = node.getDescTuple().get(idx);
+ return desc;
+ }
+
+ private void analyzeMethodLattice(MethodDescriptor md, SSJavaLattice<String> methodLattice,
+ MethodLocationInfo methodInfo) throws CyclicFlowException {
+
+ // first take a look at method invocation nodes to newly added relations
+ // from the callee
+ analyzeLatticeMethodInvocationNode(md, methodLattice, methodInfo);
+
+ if (!md.isStatic()) {
+ // set the this location
+ String thisLocSymbol = md.getThis().getSymbol();
+ methodInfo.setThisLocName(thisLocSymbol);
+ }
+
+ // set the global location
+ methodInfo.setGlobalLocName(LocationInference.GLOBALLOC);
+ methodInfo.mapDescriptorToLocation(GLOBALDESC, new CompositeLocation(
+ new Location(md, GLOBALLOC)));
+
+ // visit each node of method flow graph
+ FlowGraph fg = getFlowGraph(md);
+ Set<FlowNode> nodeSet = fg.getNodeSet();
+
+ // for the method lattice, we need to look at the first element of
+ // NTuple<Descriptor>
+ for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
+ FlowNode srcNode = (FlowNode) iterator.next();
+
+ Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(srcNode);
+ for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
+ FlowEdge outEdge = (FlowEdge) iterator2.next();
+ FlowNode dstNode = outEdge.getDst();
+
+ NTuple<Descriptor> srcNodeTuple = srcNode.getDescTuple();
+ NTuple<Descriptor> dstNodeTuple = dstNode.getDescTuple();
+
+ if (outEdge.getInitTuple().equals(srcNodeTuple)
+ && outEdge.getEndTuple().equals(dstNodeTuple)) {
+
+ if ((srcNodeTuple.size() > 1 && dstNodeTuple.size() > 1)
+ && srcNodeTuple.get(0).equals(dstNodeTuple.get(0))) {
+
+ // value flows between fields
+ Descriptor desc = srcNodeTuple.get(0);
+ ClassDescriptor classDesc;
+
+ if (desc.equals(GLOBALDESC)) {
+ classDesc = md.getClassDesc();
+ } else {
+ VarDescriptor varDesc = (VarDescriptor) srcNodeTuple.get(0);
+ classDesc = varDesc.getType().getClassDesc();
+ }
+ extractRelationFromFieldFlows(classDesc, srcNode, dstNode, 1);
+
+ } else {
+ // value flow between local var - local var or local var - field
+ addRelationToLattice(md, methodLattice, methodInfo, srcNode, dstNode);
+ }
+ }
+ }
+ }
+
+ // create mapping from param idx to inferred composite location
+
+ int offset;
+ if (!md.isStatic()) {
+ // add 'this' reference location
+ offset = 1;
+ methodInfo.addMapParamIdxToInferLoc(0, methodInfo.getInferLocation(md.getThis()));
+ } else {
+ offset = 0;
+ }
+
+ for (int idx = 0; idx < md.numParameters(); idx++) {
+ Descriptor paramDesc = md.getParameter(idx);
+ CompositeLocation inferParamLoc = methodInfo.getInferLocation(paramDesc);
+ methodInfo.addMapParamIdxToInferLoc(idx + offset, inferParamLoc);
+ }
+
+ }
+
+ private void calculateExtraLocations(MethodDescriptor md) {
+ // calcualte pcloc, returnloc,...
+
+ SSJavaLattice<String> methodLattice = getMethodLattice(md);
+ MethodLocationInfo methodInfo = getMethodLocationInfo(md);
+ FlowGraph fg = getFlowGraph(md);
+ Set<FlowNode> nodeSet = fg.getNodeSet();
+
+ for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
+ FlowNode flowNode = (FlowNode) iterator.next();
+ if (flowNode.isDeclaratonNode()) {
+ CompositeLocation inferLoc = methodInfo.getInferLocation(flowNode.getDescTuple().get(0));
+ String locIdentifier = inferLoc.get(0).getLocIdentifier();
+ if (!methodLattice.containsKey(locIdentifier)) {
+ methodLattice.put(locIdentifier);
+ }
+
+ }
+ }
+
+ Map<Integer, CompositeLocation> mapParamToLoc = methodInfo.getMapParamIdxToInferLoc();
+ Set<Integer> paramIdxSet = mapParamToLoc.keySet();
+
+ try {
+ if (!ssjava.getMethodContainingSSJavaLoop().equals(md)) {
+ // calculate the initial program counter location
+ // PC location is higher than location types of all parameters
+ String pcLocSymbol = "PCLOC";
+
+ Set<CompositeLocation> paramInFlowSet = new HashSet<CompositeLocation>();
+
+ for (Iterator iterator = paramIdxSet.iterator(); iterator.hasNext();) {
+ Integer paramIdx = (Integer) iterator.next();
+
+ FlowNode paramFlowNode = fg.getParamFlowNode(paramIdx);
+
+ if (fg.getIncomingFlowNodeSet(paramFlowNode).size() > 0) {
+ // parameter has in-value flows
+ CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
+ paramInFlowSet.add(inferLoc);
+ }
+ }
+
+ if (paramInFlowSet.size() > 0) {
+ CompositeLocation lowestLoc = getLowest(methodLattice, paramInFlowSet);
+ assert (lowestLoc != null);
+ methodInfo.setPCLoc(lowestLoc);
+ }
+
+ }
+
+ // calculate a return location
+ // the return location type is lower than all parameters and location
+ // types
+ // of return values
+ if (!md.getReturnType().isVoid()) {
+ // first, generate the set of return value location types that starts
+ // with
+ // 'this' reference
+
+ Set<CompositeLocation> inferFieldReturnLocSet = new HashSet<CompositeLocation>();
+
+ Set<FlowNode> paramFlowNode = getParamNodeFlowingToReturnValue(md);
+ Set<CompositeLocation> inferParamLocSet = new HashSet<CompositeLocation>();
+ if (paramFlowNode != null) {
+ for (Iterator iterator = paramFlowNode.iterator(); iterator.hasNext();) {
+ FlowNode fn = (FlowNode) iterator.next();
+ CompositeLocation inferLoc =
+ generateInferredCompositeLocation(methodInfo, getFlowGraph(md).getLocationTuple(fn));
+ inferParamLocSet.add(inferLoc);
+ }
+ }
+
+ Set<FlowNode> returnNodeSet = fg.getReturnNodeSet();
+
+ skip: for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
+ FlowNode returnNode = (FlowNode) iterator.next();
+ CompositeLocation inferReturnLoc =
+ generateInferredCompositeLocation(methodInfo, fg.getLocationTuple(returnNode));
+ if (inferReturnLoc.get(0).getLocIdentifier().equals("this")) {
+ // if the location type of the return value matches "this" reference
+ // then, check whether this return value is equal to/lower than all
+ // of
+ // parameters that possibly flow into the return values
+ for (Iterator iterator2 = inferParamLocSet.iterator(); iterator2.hasNext();) {
+ CompositeLocation paramInferLoc = (CompositeLocation) iterator2.next();
+
+ if ((!paramInferLoc.equals(inferReturnLoc))
+ && !isGreaterThan(methodLattice, paramInferLoc, inferReturnLoc)) {
+ continue skip;
+ }
+ }
+ inferFieldReturnLocSet.add(inferReturnLoc);
+
+ }
+ }
+
+ if (inferFieldReturnLocSet.size() > 0) {
+
+ CompositeLocation returnLoc = getLowest(methodLattice, inferFieldReturnLocSet);
+ if (returnLoc == null) {
+ // in this case, assign <'this',bottom> to the RETURNLOC
+ returnLoc = new CompositeLocation(new Location(md, md.getThis().getSymbol()));
+ returnLoc.addLocation(new Location(md.getClassDesc(), getLattice(md.getClassDesc())
+ .getBottomItem()));
+ }
+ methodInfo.setReturnLoc(returnLoc);
+
+ } else {
+ String returnLocSymbol = "RETURNLOC";
+ CompositeLocation returnLocInferLoc =
+ new CompositeLocation(new Location(md, returnLocSymbol));
+ methodInfo.setReturnLoc(returnLocInferLoc);
+
+ for (Iterator iterator = paramIdxSet.iterator(); iterator.hasNext();) {
+ Integer paramIdx = (Integer) iterator.next();
+ CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
+ String paramLocLocalSymbol = inferLoc.get(0).getLocIdentifier();
+ if (!methodLattice.isGreaterThan(paramLocLocalSymbol, returnLocSymbol)) {
+ addRelationHigherToLower(methodLattice, methodInfo, paramLocLocalSymbol,
+ returnLocSymbol);
+ }
+ }
+
+ for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
+ FlowNode returnNode = (FlowNode) iterator.next();
+ CompositeLocation inferLoc =
+ generateInferredCompositeLocation(methodInfo, fg.getLocationTuple(returnNode));
+ if (!isGreaterThan(methodLattice, inferLoc, returnLocInferLoc)) {
+ addRelation(methodLattice, methodInfo, inferLoc, returnLocInferLoc);
+ }
+ }
+
+ }
+
+ }
+ } catch (CyclicFlowException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ private Set<String> getHigherLocSymbolThan(SSJavaLattice<String> lattice, String loc) {
+ Set<String> higherLocSet = new HashSet<String>();
+
+ Set<String> locSet = lattice.getTable().keySet();
+ for (Iterator iterator = locSet.iterator(); iterator.hasNext();) {
+ String element = (String) iterator.next();
+ if (lattice.isGreaterThan(element, loc) && (!element.equals(lattice.getTopItem()))) {
+ higherLocSet.add(element);
+ }
+ }
+ return higherLocSet;
+ }
+
+ private CompositeLocation getLowest(SSJavaLattice<String> methodLattice,
+ Set<CompositeLocation> set) {
+
+ CompositeLocation lowest = set.iterator().next();
+
+ if (set.size() == 1) {
+ return lowest;
+ }
+
+ for (Iterator iterator = set.iterator(); iterator.hasNext();) {
+ CompositeLocation loc = (CompositeLocation) iterator.next();
+
+ if ((!loc.equals(lowest)) && (!isComparable(methodLattice, lowest, loc))) {
+ // if there is a case where composite locations are incomparable, just
+ // return null
+ return null;
+ }
+
+ if ((!loc.equals(lowest)) && isGreaterThan(methodLattice, lowest, loc)) {
+ lowest = loc;
+ }
+ }
+ return lowest;
+ }
+
+ private boolean isComparable(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
+ CompositeLocation comp2) {
+
+ int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
+
+ for (int idx = 0; idx < size; idx++) {
+ Location loc1 = comp1.get(idx);
+ Location loc2 = comp2.get(idx);
+
+ Descriptor desc1 = loc1.getDescriptor();
+ Descriptor desc2 = loc2.getDescriptor();
+
+ if (!desc1.equals(desc2)) {
+ throw new Error("Fail to compare " + comp1 + " and " + comp2);
+ }
+
+ String symbol1 = loc1.getLocIdentifier();
+ String symbol2 = loc2.getLocIdentifier();
+
+ SSJavaLattice<String> lattice;
+ if (idx == 0) {
+ lattice = methodLattice;
+ } else {
+ lattice = getLattice(desc1);
+ }
+
+ if (symbol1.equals(symbol2)) {
+ continue;
+ } else if (!lattice.isComparable(symbol1, symbol2)) {
+ return false;
+ }
+
+ }
+
+ return true;
+ }
+
+ private boolean isGreaterThan(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
+ CompositeLocation comp2) {
+
+ int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
+
+ for (int idx = 0; idx < size; idx++) {
+ Location loc1 = comp1.get(idx);
+ Location loc2 = comp2.get(idx);
+
+ Descriptor desc1 = loc1.getDescriptor();
+ Descriptor desc2 = loc2.getDescriptor();
+
+ if (!desc1.equals(desc2)) {
+ throw new Error("Fail to compare " + comp1 + " and " + comp2);
+ }
+
+ String symbol1 = loc1.getLocIdentifier();
+ String symbol2 = loc2.getLocIdentifier();
+
+ SSJavaLattice<String> lattice;
+ if (idx == 0) {
+ lattice = methodLattice;
+ } else {
+ lattice = getLattice(desc1);
+ }
+
+ if (symbol1.equals(symbol2)) {
+ continue;
+ } else if (lattice.isGreaterThan(symbol1, symbol2)) {
+ return true;
+ } else {
+ return false;
+ }
+
+ }
+
+ return false;
+ }
+
+ private void recursiveAddRelationToLattice(int idx, MethodDescriptor md,
+ CompositeLocation srcInferLoc, CompositeLocation dstInferLoc) throws CyclicFlowException {
+
+ String srcLocSymbol = srcInferLoc.get(idx).getLocIdentifier();
+ String dstLocSymbol = dstInferLoc.get(idx).getLocIdentifier();
+
+ if (srcLocSymbol.equals(dstLocSymbol)) {
+ recursiveAddRelationToLattice(idx + 1, md, srcInferLoc, dstInferLoc);
+ } else {
+
+ Descriptor parentDesc = srcInferLoc.get(idx).getDescriptor();
+ LocationInfo locInfo = getLocationInfo(parentDesc);
+
+ addRelationHigherToLower(getLattice(parentDesc), getLocationInfo(parentDesc), srcLocSymbol,
+ dstLocSymbol);
+ }
+
+ }
+
+ // private void propagateFlowsFromCallee(MethodInvokeNode min, MethodDescriptor mdCaller,
+ // MethodDescriptor mdCallee) {
+ //
+ // // the transformation for a call site propagates all relations between
+ // // parameters from the callee
+ // // if the method is virtual, it also grab all relations from any possible
+ // // callees
+ //
+ // Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
+ // if (mdCallee.isStatic()) {
+ // setPossibleCallees.add(mdCallee);
+ // } else {
+ // Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
+ // // removes method descriptors that are not invoked by the caller
+ // calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
+ // setPossibleCallees.addAll(calleeSet);
+ // }
+ //
+ // for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
+ // MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
+ // propagateFlowsToCaller(min, mdCaller, possibleMdCallee);
+ // }
+ //
+ // }
+
+ private void contributeCalleeFlows(MethodInvokeNode min, MethodDescriptor mdCaller,
+ MethodDescriptor mdCallee) {
+
+ System.out.println("\n##contributeCalleeFlows callee=" + mdCallee + "TO caller=" + mdCaller);
+
+ getSubGlobalFlowGraph(mdCallee);
+
+ }
+
+ private FlowGraph getSubGlobalFlowGraph(MethodDescriptor md) {
+ return mapMethodDescriptorToSubGlobalFlowGraph.get(md);
+ }
+
+ private void propagateFlowsToCallerWithNoCompositeLocation(MethodInvokeNode min,
+ MethodDescriptor mdCaller, MethodDescriptor mdCallee) {
+
+ System.out.println("\n##PROPAGATE callee=" + mdCallee + "TO caller=" + mdCaller);
+
+ // if the parameter A reaches to the parameter B
+ // then, add an edge the argument A -> the argument B to the caller's flow
+ // graph
+
+ FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
+ FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
+ int numParam = calleeFlowGraph.getNumParameters();
+
+ for (int i = 0; i < numParam; i++) {
+ for (int k = 0; k < numParam; k++) {
+
+ if (i != k) {
+
+ FlowNode paramNode1 = calleeFlowGraph.getParamFlowNode(i);
+ FlowNode paramNode2 = calleeFlowGraph.getParamFlowNode(k);
+
+ NodeTupleSet tupleSetArg1 = getNodeTupleSetByArgIdx(min, i);
+ NodeTupleSet tupleSetArg2 = getNodeTupleSetByArgIdx(min, k);
+
+ for (Iterator<NTuple<Descriptor>> iter1 = tupleSetArg1.iterator(); iter1.hasNext();) {
+ NTuple<Descriptor> arg1Tuple = iter1.next();
+
+ for (Iterator<NTuple<Descriptor>> iter2 = tupleSetArg2.iterator(); iter2.hasNext();) {
+ NTuple<Descriptor> arg2Tuple = iter2.next();
+
+ // check if the callee propagates an ordering constraints through
+ // parameters
+
+ Set<FlowNode> localReachSet =
+ calleeFlowGraph.getLocalReachFlowNodeSetFrom(paramNode1);
+
+ if (localReachSet.contains(paramNode2)) {
+ // need to propagate an ordering relation s.t. arg1 is higher
+ // than arg2
+
+ System.out
+ .println("-param1=" + paramNode1 + " is higher than param2=" + paramNode2);
+ System.out.println("-arg1Tuple=" + arg1Tuple + " is higher than arg2Tuple="
+ + arg2Tuple);
+
+ // otherwise, flows between method/field locations...
+ callerFlowGraph.addValueFlowEdge(arg1Tuple, arg2Tuple);
+ System.out.println("arg1=" + arg1Tuple + " arg2=" + arg2Tuple);
+
+ }
+
+ }
+
+ }
+ System.out.println();
+ }
+ }
+ }
+ System.out.println("##\n");
+
+ }
+
+ private void propagateFlowsToCaller(MethodInvokeNode min, MethodDescriptor mdCaller,
+ MethodDescriptor mdCallee) {
+
+ System.out.println("\n##PROPAGATE callee=" + mdCallee + "TO caller=" + mdCaller);
+
+ // if the parameter A reaches to the parameter B
+ // then, add an edge the argument A -> the argument B to the caller's flow
+ // graph
+
+ // TODO
+ // also if a parameter is a composite location and is started with "this" reference,
+ // need to make sure that the corresponding argument is higher than the translated location of
+ // the parameter.
+
+ FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
+ FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
+ int numParam = calleeFlowGraph.getNumParameters();
+
+ for (int i = 0; i < numParam; i++) {
+ for (int k = 0; k < numParam; k++) {
+
+ if (i != k) {
+
+ FlowNode paramNode1 = calleeFlowGraph.getParamFlowNode(i);
+ FlowNode paramNode2 = calleeFlowGraph.getParamFlowNode(k);
+
+ System.out.println("param1=" + paramNode1 + " curDescTuple="
+ + paramNode1.getCurrentDescTuple());
+ System.out.println("param2=" + paramNode2 + " curDescTuple="
+ + paramNode2.getCurrentDescTuple());
+
+ NodeTupleSet tupleSetArg1 = getNodeTupleSetByArgIdx(min, i);
+ NodeTupleSet tupleSetArg2 = getNodeTupleSetByArgIdx(min, k);
+
+ for (Iterator<NTuple<Descriptor>> iter1 = tupleSetArg1.iterator(); iter1.hasNext();) {
+ NTuple<Descriptor> arg1Tuple = iter1.next();
+
+ for (Iterator<NTuple<Descriptor>> iter2 = tupleSetArg2.iterator(); iter2.hasNext();) {
+ NTuple<Descriptor> arg2Tuple = iter2.next();
+
+ // check if the callee propagates an ordering constraints through
+ // parameters
+
+ Set<FlowNode> localReachSet =
+ calleeFlowGraph.getLocalReachFlowNodeSetFrom(paramNode1);
+
+ if (localReachSet.contains(paramNode2)) {
+ // need to propagate an ordering relation s.t. arg1 is higher
+ // than arg2
+
+ System.out
+ .println("-param1=" + paramNode1 + " is higher than param2=" + paramNode2);
+ System.out.println("-arg1Tuple=" + arg1Tuple + " is higher than arg2Tuple="
+ + arg2Tuple);
+
+ if (!min.getMethod().isStatic()) {
+ // check if this is the case that values flow to/from the
+ // current object reference 'this'
+
+ NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
+ Descriptor baseRef = baseTuple.get(baseTuple.size() - 1);
+
+ System.out.println("paramNode1.getCurrentDescTuple()="
+ + paramNode1.getCurrentDescTuple());
+ // calculate the prefix of the argument
+
+ if (arg2Tuple.size() == 1 && arg2Tuple.get(0).equals(baseRef)) {
+ // in this case, the callee flow causes a caller flow to the object whose method
+ // is invoked.
+
+ if (!paramNode1.getCurrentDescTuple().startsWith(mdCallee.getThis())) {
+ // check whether ???
+
+ NTuple<Descriptor> param1Prefix =
+ calculatePrefixForParam(callerFlowGraph, calleeFlowGraph, min, arg1Tuple,
+ paramNode1);
+
+ if (param1Prefix != null && param1Prefix.startsWith(mdCallee.getThis())) {
+ // in this case, we need to create a new edge 'this.FIELD'->'this'
+ // but we couldn't... instead we assign a new composite location started
+ // with 'this' reference to the corresponding parameter
+
+ CompositeLocation compLocForParam1 =
+ generateCompositeLocation(mdCallee, param1Prefix);
+
+ System.out
+ .println("set comp loc=" + compLocForParam1 + " to " + paramNode1);
+ paramNode1.setCompositeLocation(compLocForParam1);
+
+ // then, we need to make sure that the corresponding argument in the caller
+ // is required to be higher than or equal to the translated parameter
+ // location
+
+ NTuple<Descriptor> translatedParamTuple =
+ translateCompositeLocationToCaller(min, compLocForParam1);
+
+ // TODO : check if the arg >= the tranlated parameter
+
+ System.out.println("add a flow edge= " + arg1Tuple + "->"
+ + translatedParamTuple);
+ callerFlowGraph.addValueFlowEdge(arg1Tuple, translatedParamTuple);
+
+ continue;
+
+ }
+
+ } else {
+ // param1 has already been assigned a composite location
+
+ System.out.println("--param1 has already been assigned a composite location");
+ CompositeLocation compLocForParam1 = paramNode1.getCompositeLocation();
+ NTuple<Descriptor> translatedParamTuple =
+ translateCompositeLocationToCaller(min, compLocForParam1);
+
+ // TODO : check if the arg >= the tranlated parameter
+
+ System.out.println("add a flow edge= " + arg1Tuple + "->"
+ + translatedParamTuple);
+ callerFlowGraph.addValueFlowEdge(arg1Tuple, translatedParamTuple);
+
+ continue;
+
+ }
+
+ } else if (arg1Tuple.size() == 1 && arg1Tuple.get(0).equals(baseRef)) {
+ // in this case, the callee flow causes a caller flow originated from the object
+ // whose
+ // method is invoked.
+
+ System.out.println("###FROM CASE");
+
+ if (!paramNode2.getCurrentDescTuple().startsWith(mdCallee.getThis())) {
+
+ NTuple<Descriptor> param2Prefix =
+ calculatePrefixForParam(callerFlowGraph, calleeFlowGraph, min, arg2Tuple,
+ paramNode2);
+
+ if (param2Prefix != null && param2Prefix.startsWith(mdCallee.getThis())) {
+ // in this case, we need to create a new edge 'this' ->
+ // 'this.FIELD' but we couldn't... instead we assign the corresponding
+ // parameter a new composite location started with 'this' reference
+
+ CompositeLocation compLocForParam2 =
+ generateCompositeLocation(mdCallee, param2Prefix);
+
+ // System.out.println("set comp loc=" + compLocForParam2
+ // +
+ // " to " + paramNode2);
+ paramNode1.setCompositeLocation(compLocForParam2);
+ continue;
+ }
+ }
+
+ }
+ }
+
+ // otherwise, flows between method/field locations...
+ callerFlowGraph.addValueFlowEdge(arg1Tuple, arg2Tuple);
+ System.out.println("arg1=" + arg1Tuple + " arg2=" + arg2Tuple);
+
+ }
+
+ }
+
+ }
+ System.out.println();
+ }
+ }
+ }
+ System.out.println("##\n");
+ }
+
+ private NTuple<Descriptor> translateCompositeLocationToCaller(MethodInvokeNode min,
+ CompositeLocation compLocForParam1) {
+ NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
+
+ NTuple<Descriptor> tuple = new NTuple<Descriptor>();
+
+ for (int i = 0; i < baseTuple.size(); i++) {
+ tuple.add(baseTuple.get(i));
+ }
+
+ for (int i = 1; i < compLocForParam1.getSize(); i++) {
+ Location loc = compLocForParam1.get(i);
+ tuple.add(loc.getLocDescriptor());
+ }
+
+ return tuple;
+ }
+
+ private CompositeLocation generateCompositeLocation(MethodDescriptor md,
+ NTuple<Descriptor> paramPrefix) {
+
+ System.out.println("generateCompositeLocation=" + paramPrefix);
+
+ CompositeLocation newCompLoc = convertToCompositeLocation(md, paramPrefix);
+
+ Descriptor lastDescOfPrefix = paramPrefix.get(paramPrefix.size() - 1);
+ // System.out.println("lastDescOfPrefix=" + lastDescOfPrefix + " kind="
+ // + lastDescOfPrefix.getClass());
+ ClassDescriptor enclosingDescriptor;
+ if (lastDescOfPrefix instanceof FieldDescriptor) {
+ enclosingDescriptor = ((FieldDescriptor) lastDescOfPrefix).getType().getClassDesc();
+ // System.out.println("enclosingDescriptor0=" + enclosingDescriptor);
+ } else {
+ // var descriptor case
+ enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
+ }
+ // System.out.println("enclosingDescriptor=" + enclosingDescriptor);
+
+ LocationDescriptor newLocDescriptor = generateNewLocationDescriptor();
+ newLocDescriptor.setEnclosingClassDesc(enclosingDescriptor);
+
+ Location newLoc = new Location(enclosingDescriptor, newLocDescriptor.getSymbol());
+ newLoc.setLocDescriptor(newLocDescriptor);
+ newCompLoc.addLocation(newLoc);
+
+ // System.out.println("--newCompLoc=" + newCompLoc);
+ return newCompLoc;
+ }
+
+ private NTuple<Descriptor> calculatePrefixForParam(FlowGraph callerFlowGraph,
+ FlowGraph calleeFlowGraph, MethodInvokeNode min, NTuple<Descriptor> arg1Tuple,
+ FlowNode paramNode1) {
+
+ NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
+ Descriptor baseRef = baseTuple.get(baseTuple.size() - 1);
+ System.out.println("baseRef=" + baseRef);
+
+ FlowNode flowNodeArg1 = callerFlowGraph.getFlowNode(arg1Tuple);
+ List<NTuple<Descriptor>> callerPrefixList = calculatePrefixList(callerFlowGraph, flowNodeArg1);
+ System.out.println("callerPrefixList=" + callerPrefixList);
+
+ List<NTuple<Descriptor>> prefixList = calculatePrefixList(calleeFlowGraph, paramNode1);
+ System.out.println("###prefixList from node=" + paramNode1 + " =" + prefixList);
+
+ List<NTuple<Descriptor>> calleePrefixList =
+ translatePrefixListToCallee(baseRef, min.getMethod(), callerPrefixList);
+
+ System.out.println("calleePrefixList=" + calleePrefixList);
+
+ Set<FlowNode> reachNodeSetFromParam1 = calleeFlowGraph.getReachFlowNodeSetFrom(paramNode1);
+ System.out.println("reachNodeSetFromParam1=" + reachNodeSetFromParam1);
+
+ for (int i = 0; i < calleePrefixList.size(); i++) {
+ NTuple<Descriptor> curPrefix = calleePrefixList.get(i);
+ Set<NTuple<Descriptor>> reachableCommonPrefixSet = new HashSet<NTuple<Descriptor>>();
+
+ for (Iterator iterator2 = reachNodeSetFromParam1.iterator(); iterator2.hasNext();) {
+ FlowNode reachNode = (FlowNode) iterator2.next();
+ if (reachNode.getCurrentDescTuple().startsWith(curPrefix)) {
+ reachableCommonPrefixSet.add(reachNode.getCurrentDescTuple());
+ }
+ }
+
+ if (!reachableCommonPrefixSet.isEmpty()) {
+ System.out.println("###REACHABLECOMONPREFIX=" + reachableCommonPrefixSet
+ + " with curPreFix=" + curPrefix);
+ return curPrefix;
+ }
+
+ }
+
+ return null;
+ }
+
+ private List<NTuple<Descriptor>> translatePrefixListToCallee(Descriptor baseRef,
+ MethodDescriptor mdCallee, List<NTuple<Descriptor>> callerPrefixList) {
+
+ List<NTuple<Descriptor>> calleePrefixList = new ArrayList<NTuple<Descriptor>>();
+
+ for (int i = 0; i < callerPrefixList.size(); i++) {
+ NTuple<Descriptor> prefix = callerPrefixList.get(i);
+ if (prefix.startsWith(baseRef)) {
+ NTuple<Descriptor> calleePrefix = new NTuple<Descriptor>();
+ calleePrefix.add(mdCallee.getThis());
+ for (int k = 1; k < prefix.size(); k++) {
+ calleePrefix.add(prefix.get(k));
+ }
+ calleePrefixList.add(calleePrefix);
+ }
+ }
+
+ return calleePrefixList;
+
+ }
+
+ private List<NTuple<Descriptor>> calculatePrefixList(FlowGraph flowGraph, FlowNode flowNode) {
+
+ System.out.println("\n##### calculatePrefixList=" + flowNode);
+
+ Set<FlowNode> inNodeSet = flowGraph.getIncomingFlowNodeSet(flowNode);
+ inNodeSet.add(flowNode);
+
+ System.out.println("inNodeSet=" + inNodeSet);
+
+ List<NTuple<Descriptor>> prefixList = new ArrayList<NTuple<Descriptor>>();
+
+ for (Iterator iterator = inNodeSet.iterator(); iterator.hasNext();) {
+ FlowNode inNode = (FlowNode) iterator.next();
+
+ NTuple<Descriptor> inNodeTuple = inNode.getCurrentDescTuple();
+
+ // CompositeLocation inNodeInferredLoc =
+ // generateInferredCompositeLocation(methodInfo, inNodeTuple);
+ // NTuple<Location> inNodeInferredLocTuple = inNodeInferredLoc.getTuple();
+
+ for (int i = 1; i < inNodeTuple.size(); i++) {
+ NTuple<Descriptor> prefix = inNodeTuple.subList(0, i);
+ if (!prefixList.contains(prefix)) {
+ prefixList.add(prefix);
+ }
+ }
+ }
+
+ Collections.sort(prefixList, new Comparator<NTuple<Descriptor>>() {
+ public int compare(NTuple<Descriptor> arg0, NTuple<Descriptor> arg1) {
+ int s0 = arg0.size();
+ int s1 = arg1.size();
+ if (s0 > s1) {
+ return -1;
+ } else if (s0 == s1) {
+ return 0;
+ } else {
+ return 1;
+ }
+ }
+ });
+
+ return prefixList;
+
+ }