1 package Analysis.SSJava;
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.FileReader;
6 import java.io.FileWriter;
7 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.Comparator;
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.LinkedList;
15 import java.util.List;
18 import java.util.Stack;
19 import java.util.Vector;
21 import IR.ClassDescriptor;
23 import IR.FieldDescriptor;
24 import IR.MethodDescriptor;
25 import IR.NameDescriptor;
28 import IR.SymbolTable;
29 import IR.TypeDescriptor;
30 import IR.VarDescriptor;
31 import IR.Tree.ArrayAccessNode;
32 import IR.Tree.AssignmentNode;
33 import IR.Tree.BlockExpressionNode;
34 import IR.Tree.BlockNode;
35 import IR.Tree.BlockStatementNode;
36 import IR.Tree.CastNode;
37 import IR.Tree.CreateObjectNode;
38 import IR.Tree.DeclarationNode;
39 import IR.Tree.ExpressionNode;
40 import IR.Tree.FieldAccessNode;
41 import IR.Tree.IfStatementNode;
43 import IR.Tree.LiteralNode;
44 import IR.Tree.LoopNode;
45 import IR.Tree.MethodInvokeNode;
46 import IR.Tree.NameNode;
47 import IR.Tree.OpNode;
48 import IR.Tree.ReturnNode;
49 import IR.Tree.SubBlockNode;
50 import IR.Tree.SwitchStatementNode;
51 import IR.Tree.TertiaryNode;
52 import IR.Tree.TreeNode;
55 public class LocationInference {
58 SSJavaAnalysis ssjava;
60 List<ClassDescriptor> toanalyzeList;
61 List<MethodDescriptor> toanalyzeMethodList;
62 Map<MethodDescriptor, FlowGraph> mapMethodDescriptorToFlowGraph;
64 // map a method descriptor to its set of parameter descriptors
65 Map<MethodDescriptor, Set<Descriptor>> mapMethodDescriptorToParamDescSet;
67 // keep current descriptors to visit in fixed-point interprocedural analysis,
68 private Stack<MethodDescriptor> methodDescriptorsToVisitStack;
70 // map a class descriptor to a field lattice
71 private Map<ClassDescriptor, SSJavaLattice<String>> cd2lattice;
73 // map a method descriptor to a method lattice
74 private Map<MethodDescriptor, SSJavaLattice<String>> md2lattice;
76 // map a method descriptor to the set of method invocation nodes which are
77 // invoked by the method descriptor
78 private Map<MethodDescriptor, Set<MethodInvokeNode>> mapMethodDescriptorToMethodInvokeNodeSet;
80 private Map<MethodInvokeNode, Map<Integer, NodeTupleSet>> mapMethodInvokeNodeToArgIdxMap;
82 private Map<MethodDescriptor, MethodLocationInfo> mapMethodDescToMethodLocationInfo;
84 private Map<ClassDescriptor, LocationInfo> mapClassToLocationInfo;
86 private Map<MethodDescriptor, Set<MethodDescriptor>> mapMethodToCalleeSet;
88 private Map<MethodDescriptor, Set<FlowNode>> mapMethodDescToParamNodeFlowsToReturnValue;
90 private Map<String, Vector<String>> mapFileNameToLineVector;
92 private Map<Descriptor, Integer> mapDescToDefinitionLine;
94 public static final String GLOBALLOC = "GLOBALLOC";
96 public static final String TOPLOC = "TOPLOC";
98 public static final Descriptor GLOBALDESC = new NameDescriptor(GLOBALLOC);
100 public static final Descriptor TOPDESC = new NameDescriptor(TOPLOC);
102 public static String newline = System.getProperty("line.separator");
104 LocationInfo curMethodInfo;
106 boolean debug = true;
108 public LocationInference(SSJavaAnalysis ssjava, State state) {
109 this.ssjava = ssjava;
111 this.toanalyzeList = new ArrayList<ClassDescriptor>();
112 this.toanalyzeMethodList = new ArrayList<MethodDescriptor>();
113 this.mapMethodDescriptorToFlowGraph = new HashMap<MethodDescriptor, FlowGraph>();
114 this.cd2lattice = new HashMap<ClassDescriptor, SSJavaLattice<String>>();
115 this.md2lattice = new HashMap<MethodDescriptor, SSJavaLattice<String>>();
116 this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
117 this.mapMethodDescriptorToMethodInvokeNodeSet =
118 new HashMap<MethodDescriptor, Set<MethodInvokeNode>>();
119 this.mapMethodInvokeNodeToArgIdxMap =
120 new HashMap<MethodInvokeNode, Map<Integer, NodeTupleSet>>();
121 this.mapMethodDescToMethodLocationInfo = new HashMap<MethodDescriptor, MethodLocationInfo>();
122 this.mapMethodToCalleeSet = new HashMap<MethodDescriptor, Set<MethodDescriptor>>();
123 this.mapClassToLocationInfo = new HashMap<ClassDescriptor, LocationInfo>();
125 this.mapFileNameToLineVector = new HashMap<String, Vector<String>>();
126 this.mapDescToDefinitionLine = new HashMap<Descriptor, Integer>();
127 this.mapMethodDescToParamNodeFlowsToReturnValue =
128 new HashMap<MethodDescriptor, Set<FlowNode>>();
131 public void setupToAnalyze() {
132 SymbolTable classtable = state.getClassSymbolTable();
133 toanalyzeList.clear();
134 toanalyzeList.addAll(classtable.getValueSet());
135 // Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
136 // public int compare(ClassDescriptor o1, ClassDescriptor o2) {
137 // return o1.getClassName().compareToIgnoreCase(o2.getClassName());
142 public void setupToAnalazeMethod(ClassDescriptor cd) {
144 SymbolTable methodtable = cd.getMethodTable();
145 toanalyzeMethodList.clear();
146 toanalyzeMethodList.addAll(methodtable.getValueSet());
147 Collections.sort(toanalyzeMethodList, new Comparator<MethodDescriptor>() {
148 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
149 return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
154 public boolean toAnalyzeMethodIsEmpty() {
155 return toanalyzeMethodList.isEmpty();
158 public boolean toAnalyzeIsEmpty() {
159 return toanalyzeList.isEmpty();
162 public ClassDescriptor toAnalyzeNext() {
163 return toanalyzeList.remove(0);
166 public MethodDescriptor toAnalyzeMethodNext() {
167 return toanalyzeMethodList.remove(0);
170 public void inference() {
172 // 1) construct value flow graph
173 constructFlowGraph();
175 // 2) construct lattices
180 debug_writeLatticeDotFile();
182 // 3) check properties
185 // 4) generate annotated source codes
186 generateAnnoatedCode();
190 private void addMapClassDefinitionToLineNum(ClassDescriptor cd, String strLine, int lineNum) {
192 String classSymbol = cd.getSymbol();
193 int idx = classSymbol.lastIndexOf("$");
195 classSymbol = classSymbol.substring(idx + 1);
198 String pattern = "class " + classSymbol + " ";
199 if (strLine.indexOf(pattern) != -1) {
200 mapDescToDefinitionLine.put(cd, lineNum);
204 private void addMapMethodDefinitionToLineNum(Set<MethodDescriptor> methodSet, String strLine,
206 for (Iterator iterator = methodSet.iterator(); iterator.hasNext();) {
207 MethodDescriptor md = (MethodDescriptor) iterator.next();
208 String pattern = md.getMethodDeclaration();
209 if (strLine.indexOf(pattern) != -1) {
210 mapDescToDefinitionLine.put(md, lineNum);
211 methodSet.remove(md);
218 private void readOriginalSourceFiles() {
220 SymbolTable classtable = state.getClassSymbolTable();
222 Set<ClassDescriptor> classDescSet = new HashSet<ClassDescriptor>();
223 classDescSet.addAll(classtable.getValueSet());
226 // inefficient implement. it may re-visit the same file if the file
227 // contains more than one class definitions.
228 for (Iterator iterator = classDescSet.iterator(); iterator.hasNext();) {
229 ClassDescriptor cd = (ClassDescriptor) iterator.next();
231 Set<MethodDescriptor> methodSet = new HashSet<MethodDescriptor>();
232 methodSet.addAll(cd.getMethodTable().getValueSet());
234 String sourceFileName = cd.getSourceFileName();
235 Vector<String> lineVec = new Vector<String>();
237 mapFileNameToLineVector.put(sourceFileName, lineVec);
239 BufferedReader in = new BufferedReader(new FileReader(sourceFileName));
242 lineVec.add(""); // the index is started from 1.
243 while ((strLine = in.readLine()) != null) {
244 lineVec.add(lineNum, strLine);
245 addMapClassDefinitionToLineNum(cd, strLine, lineNum);
246 addMapMethodDefinitionToLineNum(methodSet, strLine, lineNum);
252 } catch (IOException e) {
258 private String generateLatticeDefinition(Descriptor desc) {
260 Set<String> sharedLocSet = new HashSet<String>();
262 SSJavaLattice<String> lattice = getLattice(desc);
263 String rtr = "@LATTICE(\"";
265 Map<String, Set<String>> map = lattice.getTable();
266 Set<String> keySet = map.keySet();
267 boolean first = true;
268 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
269 String key = (String) iterator.next();
270 if (!key.equals(lattice.getTopItem())) {
271 Set<String> connectedSet = map.get(key);
273 if (connectedSet.size() == 1) {
274 if (connectedSet.iterator().next().equals(lattice.getBottomItem())) {
282 if (lattice.isSharedLoc(key)) {
283 rtr += "," + key + "*";
288 for (Iterator iterator2 = connectedSet.iterator(); iterator2.hasNext();) {
289 String loc = (String) iterator2.next();
290 if (!loc.equals(lattice.getBottomItem())) {
297 rtr += loc + "<" + key;
298 if (lattice.isSharedLoc(key) && (!sharedLocSet.contains(key))) {
299 rtr += "," + key + "*";
300 sharedLocSet.add(key);
302 if (lattice.isSharedLoc(loc) && (!sharedLocSet.contains(loc))) {
303 rtr += "," + loc + "*";
304 sharedLocSet.add(loc);
314 if (desc instanceof MethodDescriptor) {
315 TypeDescriptor returnType = ((MethodDescriptor) desc).getReturnType();
317 MethodLocationInfo methodLocInfo = getMethodLocationInfo((MethodDescriptor) desc);
319 if (returnType != null && (!returnType.isVoid())) {
321 "\n@RETURNLOC(\"" + generateLocationAnnoatation(methodLocInfo.getReturnLoc()) + "\")";
323 rtr += "\n@THISLOC(\"this\")\n@GLOBALLOC(\"GLOBALLOC\")";
325 if (lattice.containsKey("PCLOC")) {
326 rtr += "\n@PCLOC(\"PCLOC\")";
333 private void generateAnnoatedCode() {
335 readOriginalSourceFiles();
338 while (!toAnalyzeIsEmpty()) {
339 ClassDescriptor cd = toAnalyzeNext();
341 setupToAnalazeMethod(cd);
343 LocationInfo locInfo = mapClassToLocationInfo.get(cd);
344 String sourceFileName = cd.getSourceFileName();
346 if (cd.isInterface()) {
350 int classDefLine = mapDescToDefinitionLine.get(cd);
351 Vector<String> sourceVec = mapFileNameToLineVector.get(sourceFileName);
353 if (locInfo == null) {
354 locInfo = getLocationInfo(cd);
357 for (Iterator iter = cd.getFields(); iter.hasNext();) {
358 Descriptor fieldDesc = (Descriptor) iter.next();
359 String locIdentifier = locInfo.getFieldInferLocation(fieldDesc).getLocIdentifier();
360 if (!getLattice(cd).containsKey(locIdentifier)) {
361 getLattice(cd).put(locIdentifier);
365 String fieldLatticeDefStr = generateLatticeDefinition(cd);
366 String annoatedSrc = fieldLatticeDefStr + newline + sourceVec.get(classDefLine);
367 sourceVec.set(classDefLine, annoatedSrc);
369 // generate annotations for field declarations
370 LocationInfo fieldLocInfo = getLocationInfo(cd);
371 Map<Descriptor, CompositeLocation> inferLocMap = fieldLocInfo.getMapDescToInferLocation();
373 for (Iterator iter = cd.getFields(); iter.hasNext();) {
374 FieldDescriptor fd = (FieldDescriptor) iter.next();
376 String locAnnotationStr;
377 if (inferLocMap.containsKey(fd)) {
378 CompositeLocation inferLoc = inferLocMap.get(fd);
379 locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
381 // if the field is not accssed by SS part, just assigns dummy
383 locAnnotationStr = "@LOC(\"LOC\")";
385 int fdLineNum = fd.getLineNum();
386 String orgFieldDeclarationStr = sourceVec.get(fdLineNum);
387 String fieldDeclaration = fd.toString();
388 fieldDeclaration = fieldDeclaration.substring(0, fieldDeclaration.length() - 1);
390 String annoatedStr = locAnnotationStr + " " + orgFieldDeclarationStr;
391 sourceVec.set(fdLineNum, annoatedStr);
395 while (!toAnalyzeMethodIsEmpty()) {
396 MethodDescriptor md = toAnalyzeMethodNext();
398 if (!ssjava.needTobeAnnotated(md)) {
402 SSJavaLattice<String> methodLattice = md2lattice.get(md);
403 if (methodLattice != null) {
405 int methodDefLine = md.getLineNum();
407 MethodLocationInfo methodLocInfo = getMethodLocationInfo(md);
409 Map<Descriptor, CompositeLocation> methodInferLocMap =
410 methodLocInfo.getMapDescToInferLocation();
411 Set<Descriptor> localVarDescSet = methodInferLocMap.keySet();
413 for (Iterator iterator = localVarDescSet.iterator(); iterator.hasNext();) {
414 Descriptor localVarDesc = (Descriptor) iterator.next();
415 CompositeLocation inferLoc = methodInferLocMap.get(localVarDesc);
417 String locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
419 if (!isParameter(md, localVarDesc)) {
420 if (mapDescToDefinitionLine.containsKey(localVarDesc)) {
421 int varLineNum = mapDescToDefinitionLine.get(localVarDesc);
422 String orgSourceLine = sourceVec.get(varLineNum);
424 orgSourceLine.indexOf(generateVarDeclaration((VarDescriptor) localVarDesc));
427 orgSourceLine.substring(0, idx) + locAnnotationStr + " "
428 + orgSourceLine.substring(idx);
429 sourceVec.set(varLineNum, annoatedStr);
432 String methodDefStr = sourceVec.get(methodDefLine);
435 getParamLocation(methodDefStr,
436 generateVarDeclaration((VarDescriptor) localVarDesc));
441 methodDefStr.substring(0, idx) + locAnnotationStr + " "
442 + methodDefStr.substring(idx);
443 sourceVec.set(methodDefLine, annoatedStr);
448 String methodLatticeDefStr = generateLatticeDefinition(md);
449 String annoatedStr = methodLatticeDefStr + newline + sourceVec.get(methodDefLine);
450 sourceVec.set(methodDefLine, annoatedStr);
460 private int getParamLocation(String methodStr, String paramStr) {
462 String pattern = paramStr + ",";
464 int idx = methodStr.indexOf(pattern);
468 pattern = paramStr + ")";
469 return methodStr.indexOf(pattern);
474 private String generateVarDeclaration(VarDescriptor varDesc) {
476 TypeDescriptor td = varDesc.getType();
477 String rtr = td.toString();
479 for (int i = 0; i < td.getArrayCount(); i++) {
483 rtr += " " + varDesc.getName();
488 private String generateLocationAnnoatation(CompositeLocation loc) {
491 Location methodLoc = loc.get(0);
492 rtr += methodLoc.getLocIdentifier();
494 for (int i = 1; i < loc.getSize(); i++) {
495 Location element = loc.get(i);
496 rtr += "," + element.getDescriptor().getSymbol() + "." + element.getLocIdentifier();
502 private boolean isParameter(MethodDescriptor md, Descriptor localVarDesc) {
503 return getFlowGraph(md).isParamDesc(localVarDesc);
506 private String extractFileName(String fileName) {
507 int idx = fileName.lastIndexOf("/");
511 return fileName.substring(idx + 1);
516 private void codeGen() {
518 Set<String> originalFileNameSet = mapFileNameToLineVector.keySet();
519 for (Iterator iterator = originalFileNameSet.iterator(); iterator.hasNext();) {
520 String orgFileName = (String) iterator.next();
521 String outputFileName = extractFileName(orgFileName);
523 Vector<String> sourceVec = mapFileNameToLineVector.get(orgFileName);
527 FileWriter fileWriter = new FileWriter("./infer/" + outputFileName);
528 BufferedWriter out = new BufferedWriter(fileWriter);
530 for (int i = 0; i < sourceVec.size(); i++) {
531 out.write(sourceVec.get(i));
535 } catch (IOException e) {
543 private void simplifyLattices() {
545 // generate lattice dot file
548 while (!toAnalyzeIsEmpty()) {
549 ClassDescriptor cd = toAnalyzeNext();
551 setupToAnalazeMethod(cd);
553 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
554 if (classLattice != null) {
555 classLattice.removeRedundantEdges();
558 while (!toAnalyzeMethodIsEmpty()) {
559 MethodDescriptor md = toAnalyzeMethodNext();
560 SSJavaLattice<String> methodLattice = md2lattice.get(md);
561 if (methodLattice != null) {
562 methodLattice.removeRedundantEdges();
569 private void checkLattices() {
571 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
573 // current descriptors to visit in fixed-point interprocedural analysis,
575 // dependency in the call graph
576 methodDescriptorsToVisitStack.clear();
578 // descriptorListToAnalyze.removeFirst();
580 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
581 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
583 while (!descriptorListToAnalyze.isEmpty()) {
584 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
585 checkLatticesOfVirtualMethods(md);
590 private void debug_writeLatticeDotFile() {
591 // generate lattice dot file
595 while (!toAnalyzeIsEmpty()) {
596 ClassDescriptor cd = toAnalyzeNext();
598 setupToAnalazeMethod(cd);
600 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
601 if (classLattice != null) {
602 ssjava.writeLatticeDotFile(cd, null, classLattice);
603 debug_printDescriptorToLocNameMapping(cd);
606 while (!toAnalyzeMethodIsEmpty()) {
607 MethodDescriptor md = toAnalyzeMethodNext();
608 SSJavaLattice<String> methodLattice = md2lattice.get(md);
609 if (methodLattice != null) {
610 ssjava.writeLatticeDotFile(cd, md, methodLattice);
611 debug_printDescriptorToLocNameMapping(md);
618 private void debug_printDescriptorToLocNameMapping(Descriptor desc) {
620 LocationInfo info = getLocationInfo(desc);
621 System.out.println("## " + desc + " ##");
622 System.out.println(info.getMapDescToInferLocation());
623 LocationInfo locInfo = getLocationInfo(desc);
624 System.out.println("mapping=" + locInfo.getMapLocSymbolToDescSet());
625 System.out.println("###################");
629 private void inferLattices() {
631 // do fixed-point analysis
634 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
636 // Collections.sort(descriptorListToAnalyze, new
637 // Comparator<MethodDescriptor>() {
638 // public int compare(MethodDescriptor o1, MethodDescriptor o2) {
639 // return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
643 // current descriptors to visit in fixed-point interprocedural analysis,
645 // dependency in the call graph
646 methodDescriptorsToVisitStack.clear();
648 // descriptorListToAnalyze.removeFirst();
650 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
651 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
653 while (!descriptorListToAnalyze.isEmpty()) {
654 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
655 methodDescriptorsToVisitStack.add(md);
658 // analyze scheduled methods until there are no more to visit
659 while (!methodDescriptorsToVisitStack.isEmpty()) {
660 // start to analyze leaf node
661 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
663 SSJavaLattice<String> methodLattice =
664 new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
666 MethodLocationInfo methodInfo = new MethodLocationInfo(md);
667 curMethodInfo = methodInfo;
669 System.out.println();
670 System.out.println("SSJAVA: Inferencing the lattice from " + md);
673 analyzeMethodLattice(md, methodLattice, methodInfo);
674 } catch (CyclicFlowException e) {
675 throw new Error("Fail to generate the method lattice for " + md);
678 SSJavaLattice<String> prevMethodLattice = getMethodLattice(md);
679 MethodLocationInfo prevMethodInfo = getMethodLocationInfo(md);
681 if ((!methodLattice.equals(prevMethodLattice)) || (!methodInfo.equals(prevMethodInfo))) {
683 setMethodLattice(md, methodLattice);
684 setMethodLocInfo(md, methodInfo);
686 // results for callee changed, so enqueue dependents caller for
688 Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
689 while (depsItr.hasNext()) {
690 MethodDescriptor methodNext = depsItr.next();
691 if (!methodDescriptorsToVisitStack.contains(methodNext)
692 && methodDescriptorToVistSet.contains(methodNext)) {
693 methodDescriptorsToVisitStack.add(methodNext);
701 descriptorListToAnalyze = ssjava.getSortedDescriptors();
702 for (Iterator iterator = descriptorListToAnalyze.iterator(); iterator.hasNext();) {
703 MethodDescriptor md = (MethodDescriptor) iterator.next();
704 calculateExtraLocations(md);
709 private void setMethodLocInfo(MethodDescriptor md, MethodLocationInfo methodInfo) {
710 mapMethodDescToMethodLocationInfo.put(md, methodInfo);
713 private void checkLatticesOfVirtualMethods(MethodDescriptor md) {
715 if (!md.isStatic()) {
716 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
717 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
719 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
720 MethodDescriptor mdCallee = (MethodDescriptor) iterator.next();
721 if (!md.equals(mdCallee)) {
722 checkConsistency(md, mdCallee);
730 private void checkConsistency(MethodDescriptor md1, MethodDescriptor md2) {
732 // check that two lattice have the same relations between parameters(+PC
733 // LOC, GLOBAL_LOC RETURN LOC)
735 List<CompositeLocation> list1 = new ArrayList<CompositeLocation>();
736 List<CompositeLocation> list2 = new ArrayList<CompositeLocation>();
738 MethodLocationInfo locInfo1 = getMethodLocationInfo(md1);
739 MethodLocationInfo locInfo2 = getMethodLocationInfo(md2);
741 Map<Integer, CompositeLocation> paramMap1 = locInfo1.getMapParamIdxToInferLoc();
742 Map<Integer, CompositeLocation> paramMap2 = locInfo2.getMapParamIdxToInferLoc();
744 int numParam = locInfo1.getMapParamIdxToInferLoc().keySet().size();
746 // add location types of paramters
747 for (int idx = 0; idx < numParam; idx++) {
748 list1.add(paramMap1.get(Integer.valueOf(idx)));
749 list2.add(paramMap2.get(Integer.valueOf(idx)));
752 // add program counter location
753 list1.add(locInfo1.getPCLoc());
754 list2.add(locInfo2.getPCLoc());
756 if (!md1.getReturnType().isVoid()) {
757 // add return value location
758 CompositeLocation rtrLoc1 = getMethodLocationInfo(md1).getReturnLoc();
759 CompositeLocation rtrLoc2 = getMethodLocationInfo(md2).getReturnLoc();
764 // add global location type
765 if (md1.isStatic()) {
766 CompositeLocation globalLoc1 =
767 new CompositeLocation(new Location(md1, locInfo1.getGlobalLocName()));
768 CompositeLocation globalLoc2 =
769 new CompositeLocation(new Location(md2, locInfo2.getGlobalLocName()));
770 list1.add(globalLoc1);
771 list2.add(globalLoc2);
774 for (int i = 0; i < list1.size(); i++) {
775 CompositeLocation locA1 = list1.get(i);
776 CompositeLocation locA2 = list2.get(i);
777 for (int k = 0; k < list1.size(); k++) {
779 CompositeLocation locB1 = list1.get(k);
780 CompositeLocation locB2 = list2.get(k);
781 boolean r1 = isGreaterThan(getLattice(md1), locA1, locB1);
783 boolean r2 = isGreaterThan(getLattice(md1), locA2, locB2);
786 throw new Error("The method " + md1 + " is not consistent with the method " + md2
787 + ".:: They have a different ordering relation between locations (" + locA1 + ","
788 + locB1 + ") and (" + locA2 + "," + locB2 + ").");
796 private String getSymbol(int idx, FlowNode node) {
797 Descriptor desc = node.getDescTuple().get(idx);
798 return desc.getSymbol();
801 private Descriptor getDescriptor(int idx, FlowNode node) {
802 Descriptor desc = node.getDescTuple().get(idx);
806 private void analyzeMethodLattice(MethodDescriptor md, SSJavaLattice<String> methodLattice,
807 MethodLocationInfo methodInfo) throws CyclicFlowException {
809 // first take a look at method invocation nodes to newly added relations
811 analyzeLatticeMethodInvocationNode(md, methodLattice, methodInfo);
813 if (!md.isStatic()) {
814 // set the this location
815 String thisLocSymbol = md.getThis().getSymbol();
816 methodInfo.setThisLocName(thisLocSymbol);
819 // set the global location
820 methodInfo.setGlobalLocName(LocationInference.GLOBALLOC);
821 methodInfo.mapDescriptorToLocation(GLOBALDESC, new CompositeLocation(
822 new Location(md, GLOBALLOC)));
824 // visit each node of method flow graph
825 FlowGraph fg = getFlowGraph(md);
826 Set<FlowNode> nodeSet = fg.getNodeSet();
828 // for the method lattice, we need to look at the first element of
829 // NTuple<Descriptor>
830 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
831 FlowNode srcNode = (FlowNode) iterator.next();
833 Set<FlowEdge> outEdgeSet = srcNode.getOutEdgeSet();
834 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
835 FlowEdge outEdge = (FlowEdge) iterator2.next();
836 FlowNode dstNode = outEdge.getDst();
838 NTuple<Descriptor> srcNodeTuple = srcNode.getDescTuple();
839 NTuple<Descriptor> dstNodeTuple = dstNode.getDescTuple();
841 if (outEdge.getInitTuple().equals(srcNodeTuple)
842 && outEdge.getEndTuple().equals(dstNodeTuple)) {
844 if ((srcNodeTuple.size() > 1 && dstNodeTuple.size() > 1)
845 && srcNodeTuple.get(0).equals(dstNodeTuple.get(0))) {
847 // value flows between fields
848 Descriptor desc = srcNodeTuple.get(0);
849 ClassDescriptor classDesc;
851 if (desc.equals(GLOBALDESC)) {
852 classDesc = md.getClassDesc();
854 VarDescriptor varDesc = (VarDescriptor) srcNodeTuple.get(0);
855 classDesc = varDesc.getType().getClassDesc();
857 extractRelationFromFieldFlows(classDesc, srcNode, dstNode, 1);
860 // value flow between local var - local var or local var - field
861 addRelationToLattice(md, methodLattice, methodInfo, srcNode, dstNode);
867 // create mapping from param idx to inferred composite location
870 if (!md.isStatic()) {
871 // add 'this' reference location
873 methodInfo.addMapParamIdxToInferLoc(0, methodInfo.getInferLocation(md.getThis()));
878 for (int idx = 0; idx < md.numParameters(); idx++) {
879 Descriptor paramDesc = md.getParameter(idx);
880 CompositeLocation inferParamLoc = methodInfo.getInferLocation(paramDesc);
881 methodInfo.addMapParamIdxToInferLoc(idx + offset, inferParamLoc);
886 private void calculateExtraLocations(MethodDescriptor md) {
887 // calcualte pcloc, returnloc,...
889 SSJavaLattice<String> methodLattice = getMethodLattice(md);
890 MethodLocationInfo methodInfo = getMethodLocationInfo(md);
891 FlowGraph fg = getFlowGraph(md);
892 Set<FlowNode> nodeSet = fg.getNodeSet();
894 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
895 FlowNode flowNode = (FlowNode) iterator.next();
896 if (flowNode.isDeclaratonNode()) {
897 CompositeLocation inferLoc = methodInfo.getInferLocation(flowNode.getDescTuple().get(0));
898 String locIdentifier = inferLoc.get(0).getLocIdentifier();
899 if (!methodLattice.containsKey(locIdentifier)) {
900 methodLattice.put(locIdentifier);
906 Map<Integer, CompositeLocation> mapParamToLoc = methodInfo.getMapParamIdxToInferLoc();
907 Set<Integer> keySet = mapParamToLoc.keySet();
910 if (!ssjava.getMethodContainingSSJavaLoop().equals(md)) {
911 // calculate the initial program counter location
912 // PC location is higher than location types of all parameters
913 String pcLocSymbol = "PCLOC";
915 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
916 Integer paramIdx = (Integer) iterator.next();
917 CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
918 String paramLocLocalSymbol = inferLoc.get(0).getLocIdentifier();
919 if (!methodLattice.isGreaterThan(pcLocSymbol, paramLocLocalSymbol)) {
920 addRelationHigherToLower(methodLattice, methodInfo, pcLocSymbol, paramLocLocalSymbol);
922 Set<String> higherLocSet = getHigherLocSymbolThan(methodLattice, paramLocLocalSymbol);
923 higherLocSet.remove(pcLocSymbol);
924 for (Iterator iterator2 = higherLocSet.iterator(); iterator2.hasNext();) {
925 String loc = (String) iterator2.next();
926 addRelationHigherToLower(methodLattice, methodInfo, pcLocSymbol, loc);
931 Set<String> locElementSet = methodLattice.getElementSet();
932 locElementSet.remove(pcLocSymbol);
933 for (Iterator iterator = locElementSet.iterator(); iterator.hasNext();) {
934 String loc = (String) iterator.next();
935 if (!methodLattice.isGreaterThan(pcLocSymbol, loc)) {
936 addRelationHigherToLower(methodLattice, methodInfo, pcLocSymbol, loc);
942 // calculate a return location
943 // the return location type is lower than all parameters and location
946 if (!md.getReturnType().isVoid()) {
947 // first, generate the set of return value location types that starts
951 Set<CompositeLocation> inferFieldReturnLocSet = new HashSet<CompositeLocation>();
953 Set<FlowNode> paramFlowNode = getParamNodeFlowingToReturnValue(md);
954 Set<CompositeLocation> inferParamLocSet = new HashSet<CompositeLocation>();
955 if (paramFlowNode != null) {
956 for (Iterator iterator = paramFlowNode.iterator(); iterator.hasNext();) {
957 FlowNode fn = (FlowNode) iterator.next();
958 CompositeLocation inferLoc =
959 generateInferredCompositeLocation(methodInfo, getFlowGraph(md).getLocationTuple(fn));
960 inferParamLocSet.add(inferLoc);
964 Set<FlowNode> returnNodeSet = fg.getReturnNodeSet();
965 skip: for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
966 FlowNode returnNode = (FlowNode) iterator.next();
967 CompositeLocation inferReturnLoc =
968 generateInferredCompositeLocation(methodInfo, fg.getLocationTuple(returnNode));
969 if (inferReturnLoc.get(0).getLocIdentifier().equals("this")) {
970 // if the location type of the return value matches "this" reference
971 // then, check whether this return value is equal to/lower than all
973 // parameters that possibly flow into the return values
974 for (Iterator iterator2 = inferParamLocSet.iterator(); iterator2.hasNext();) {
975 CompositeLocation paramInferLoc = (CompositeLocation) iterator2.next();
977 if ((!paramInferLoc.equals(inferReturnLoc))
978 && !isGreaterThan(methodLattice, paramInferLoc, inferReturnLoc)) {
982 inferFieldReturnLocSet.add(inferReturnLoc);
987 if (inferFieldReturnLocSet.size() > 0) {
989 CompositeLocation returnLoc = getLowest(methodLattice, inferFieldReturnLocSet);
990 methodInfo.setReturnLoc(returnLoc);
993 String returnLocSymbol = "RETURNLOC";
994 CompositeLocation returnLocInferLoc =
995 new CompositeLocation(new Location(md, returnLocSymbol));
996 methodInfo.setReturnLoc(returnLocInferLoc);
998 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
999 Integer paramIdx = (Integer) iterator.next();
1000 CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
1001 String paramLocLocalSymbol = inferLoc.get(0).getLocIdentifier();
1002 if (!methodLattice.isGreaterThan(paramLocLocalSymbol, returnLocSymbol)) {
1003 addRelationHigherToLower(methodLattice, methodInfo, paramLocLocalSymbol,
1008 for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
1009 FlowNode returnNode = (FlowNode) iterator.next();
1010 CompositeLocation inferLoc =
1011 generateInferredCompositeLocation(methodInfo, fg.getLocationTuple(returnNode));
1012 if (!isGreaterThan(methodLattice, inferLoc, returnLocInferLoc)) {
1013 addRelation(methodLattice, methodInfo, inferLoc, returnLocInferLoc);
1020 } catch (CyclicFlowException e) {
1021 e.printStackTrace();
1026 private Set<String> getHigherLocSymbolThan(SSJavaLattice<String> lattice, String loc) {
1027 Set<String> higherLocSet = new HashSet<String>();
1029 Set<String> locSet = lattice.getTable().keySet();
1030 for (Iterator iterator = locSet.iterator(); iterator.hasNext();) {
1031 String element = (String) iterator.next();
1032 if (lattice.isGreaterThan(element, loc) && (!element.equals(lattice.getTopItem()))) {
1033 higherLocSet.add(element);
1036 return higherLocSet;
1039 private CompositeLocation getLowest(SSJavaLattice<String> methodLattice,
1040 Set<CompositeLocation> set) {
1042 CompositeLocation lowest = set.iterator().next();
1044 if (set.size() == 1) {
1048 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
1049 CompositeLocation loc = (CompositeLocation) iterator.next();
1050 if (isGreaterThan(methodLattice, lowest, loc)) {
1057 private boolean isGreaterThan(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
1058 CompositeLocation comp2) {
1060 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
1062 for (int idx = 0; idx < size; idx++) {
1063 Location loc1 = comp1.get(idx);
1064 Location loc2 = comp2.get(idx);
1066 Descriptor desc1 = loc1.getDescriptor();
1067 Descriptor desc2 = loc2.getDescriptor();
1069 if (!desc1.equals(desc2)) {
1070 throw new Error("Fail to compare " + comp1 + " and " + comp2);
1073 String symbol1 = loc1.getLocIdentifier();
1074 String symbol2 = loc2.getLocIdentifier();
1076 SSJavaLattice<String> lattice;
1078 lattice = methodLattice;
1080 lattice = getLattice(desc1);
1082 if (symbol1.equals(symbol2)) {
1084 } else if (lattice.isGreaterThan(symbol1, symbol2)) {
1095 private void recursiveAddRelationToLattice(int idx, MethodDescriptor md,
1096 CompositeLocation srcInferLoc, CompositeLocation dstInferLoc) throws CyclicFlowException {
1098 String srcLocSymbol = srcInferLoc.get(idx).getLocIdentifier();
1099 String dstLocSymbol = dstInferLoc.get(idx).getLocIdentifier();
1101 if (srcLocSymbol.equals(dstLocSymbol)) {
1102 recursiveAddRelationToLattice(idx + 1, md, srcInferLoc, dstInferLoc);
1105 Descriptor parentDesc = srcInferLoc.get(idx).getDescriptor();
1106 LocationInfo locInfo = getLocationInfo(parentDesc);
1108 addRelationHigherToLower(getLattice(parentDesc), getLocationInfo(parentDesc), srcLocSymbol,
1114 private void analyzeLatticeMethodInvocationNode(MethodDescriptor mdCaller,
1115 SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo)
1116 throws CyclicFlowException {
1118 // the transformation for a call site propagates all relations between
1119 // parameters from the callee
1120 // if the method is virtual, it also grab all relations from any possible
1123 Set<MethodInvokeNode> setMethodInvokeNode =
1124 mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
1126 if (setMethodInvokeNode != null) {
1128 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
1129 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
1130 MethodDescriptor mdCallee = min.getMethod();
1131 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1132 if (mdCallee.isStatic()) {
1133 setPossibleCallees.add(mdCallee);
1135 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
1136 // removes method descriptors that are not invoked by the caller
1137 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
1138 setPossibleCallees.addAll(calleeSet);
1141 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
1142 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
1143 propagateRelationToCaller(min, mdCaller, possibleMdCallee, methodLattice, methodInfo);
1151 private void propagateRelationToCaller(MethodInvokeNode min, MethodDescriptor mdCaller,
1152 MethodDescriptor possibleMdCallee, SSJavaLattice<String> methodLattice,
1153 MethodLocationInfo methodInfo) throws CyclicFlowException {
1156 SSJavaLattice<String> calleeLattice = getMethodLattice(possibleMdCallee);
1157 MethodLocationInfo calleeLocInfo = getMethodLocationInfo(possibleMdCallee);
1158 FlowGraph calleeFlowGraph = getFlowGraph(possibleMdCallee);
1160 int numParam = calleeLocInfo.getNumParam();
1161 for (int i = 0; i < numParam; i++) {
1162 CompositeLocation param1 = calleeLocInfo.getParamCompositeLocation(i);
1163 for (int k = 0; k < numParam; k++) {
1165 CompositeLocation param2 = calleeLocInfo.getParamCompositeLocation(k);
1167 if (isGreaterThan(getLattice(possibleMdCallee), param1, param2)) {
1168 NodeTupleSet argDescTupleSet1 = getNodeTupleSetByArgIdx(min, i);
1169 NodeTupleSet argDescTupleSet2 = getNodeTupleSetByArgIdx(min, k);
1171 // the callee has the relation in which param1 is higher than param2
1172 // therefore, the caller has to have the relation in which arg1 is
1175 for (Iterator<NTuple<Descriptor>> iterator = argDescTupleSet1.iterator(); iterator
1177 NTuple<Descriptor> argDescTuple1 = iterator.next();
1179 for (Iterator<NTuple<Descriptor>> iterator2 = argDescTupleSet2.iterator(); iterator2
1181 NTuple<Descriptor> argDescTuple2 = iterator2.next();
1183 // retreive inferred location by the local var descriptor
1185 NTuple<Location> tuple1 = getFlowGraph(mdCaller).getLocationTuple(argDescTuple1);
1186 NTuple<Location> tuple2 = getFlowGraph(mdCaller).getLocationTuple(argDescTuple2);
1188 // CompositeLocation higherInferLoc =
1189 // methodInfo.getInferLocation(argTuple1.get(0));
1190 // CompositeLocation lowerInferLoc =
1191 // methodInfo.getInferLocation(argTuple2.get(0));
1193 CompositeLocation inferLoc1 = generateInferredCompositeLocation(methodInfo, tuple1);
1194 CompositeLocation inferLoc2 = generateInferredCompositeLocation(methodInfo, tuple2);
1196 // addRelation(methodLattice, methodInfo, inferLoc1, inferLoc2);
1198 addFlowGraphEdge(mdCaller, argDescTuple1, argDescTuple2);
1211 private CompositeLocation generateInferredCompositeLocation(MethodLocationInfo methodInfo,
1212 NTuple<Location> tuple) {
1214 // first, retrieve inferred location by the local var descriptor
1215 CompositeLocation inferLoc = new CompositeLocation();
1217 CompositeLocation localVarInferLoc =
1218 methodInfo.getInferLocation(tuple.get(0).getLocDescriptor());
1220 localVarInferLoc.get(0).setLocDescriptor(tuple.get(0).getLocDescriptor());
1222 for (int i = 0; i < localVarInferLoc.getSize(); i++) {
1223 inferLoc.addLocation(localVarInferLoc.get(i));
1226 for (int i = 1; i < tuple.size(); i++) {
1227 Location cur = tuple.get(i);
1228 Descriptor enclosingDesc = cur.getDescriptor();
1229 Descriptor curDesc = cur.getLocDescriptor();
1231 Location inferLocElement;
1232 if (curDesc == null) {
1233 // in this case, we have a newly generated location.
1234 inferLocElement = new Location(enclosingDesc, cur.getLocIdentifier());
1236 String fieldLocSymbol =
1237 getLocationInfo(enclosingDesc).getInferLocation(curDesc).get(0).getLocIdentifier();
1238 inferLocElement = new Location(enclosingDesc, fieldLocSymbol);
1239 inferLocElement.setLocDescriptor(curDesc);
1242 inferLoc.addLocation(inferLocElement);
1246 assert (inferLoc.get(0).getLocDescriptor().getSymbol() == inferLoc.get(0).getLocIdentifier());
1250 private void addRelation(SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo,
1251 CompositeLocation srcInferLoc, CompositeLocation dstInferLoc) throws CyclicFlowException {
1253 System.out.println("addRelation --- srcInferLoc=" + srcInferLoc + " dstInferLoc="
1255 String srcLocalLocSymbol = srcInferLoc.get(0).getLocIdentifier();
1256 String dstLocalLocSymbol = dstInferLoc.get(0).getLocIdentifier();
1258 if (srcInferLoc.getSize() == 1 && dstInferLoc.getSize() == 1) {
1259 // add a new relation to the local lattice
1260 addRelationHigherToLower(methodLattice, methodInfo, srcLocalLocSymbol, dstLocalLocSymbol);
1261 } else if (srcInferLoc.getSize() > 1 && dstInferLoc.getSize() > 1) {
1262 // both src and dst have assigned to a composite location
1264 if (!srcLocalLocSymbol.equals(dstLocalLocSymbol)) {
1265 addRelationHigherToLower(methodLattice, methodInfo, srcLocalLocSymbol, dstLocalLocSymbol);
1267 recursivelyAddRelation(1, srcInferLoc, dstInferLoc);
1270 // either src or dst has assigned to a composite location
1271 if (!srcLocalLocSymbol.equals(dstLocalLocSymbol)) {
1272 addRelationHigherToLower(methodLattice, methodInfo, srcLocalLocSymbol, dstLocalLocSymbol);
1276 System.out.println();
1280 public LocationInfo getLocationInfo(Descriptor d) {
1281 if (d instanceof MethodDescriptor) {
1282 return getMethodLocationInfo((MethodDescriptor) d);
1284 return getFieldLocationInfo((ClassDescriptor) d);
1288 private MethodLocationInfo getMethodLocationInfo(MethodDescriptor md) {
1290 if (!mapMethodDescToMethodLocationInfo.containsKey(md)) {
1291 mapMethodDescToMethodLocationInfo.put(md, new MethodLocationInfo(md));
1294 return mapMethodDescToMethodLocationInfo.get(md);
1298 private LocationInfo getFieldLocationInfo(ClassDescriptor cd) {
1300 if (!mapClassToLocationInfo.containsKey(cd)) {
1301 mapClassToLocationInfo.put(cd, new LocationInfo(cd));
1304 return mapClassToLocationInfo.get(cd);
1308 private void addRelationToLattice(MethodDescriptor md, SSJavaLattice<String> methodLattice,
1309 MethodLocationInfo methodInfo, FlowNode srcNode, FlowNode dstNode) throws CyclicFlowException {
1311 System.out.println();
1312 System.out.println("### addRelationToLattice src=" + srcNode + " dst=" + dstNode);
1314 // add a new binary relation of dstNode < srcNode
1315 FlowGraph flowGraph = getFlowGraph(md);
1317 System.out.println("***** src composite case::");
1318 calculateCompositeLocation(flowGraph, methodLattice, methodInfo, srcNode);
1320 CompositeLocation srcInferLoc =
1321 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(srcNode));
1322 CompositeLocation dstInferLoc =
1323 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(dstNode));
1324 addRelation(methodLattice, methodInfo, srcInferLoc, dstInferLoc);
1325 } catch (CyclicFlowException e) {
1326 // there is a cyclic value flow... try to calculate a composite location
1327 // for the destination node
1328 System.out.println("***** dst composite case::");
1329 calculateCompositeLocation(flowGraph, methodLattice, methodInfo, dstNode);
1330 CompositeLocation srcInferLoc =
1331 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(srcNode));
1332 CompositeLocation dstInferLoc =
1333 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(dstNode));
1335 addRelation(methodLattice, methodInfo, srcInferLoc, dstInferLoc);
1336 } catch (CyclicFlowException e1) {
1337 throw new Error("Failed to merge cyclic value flows into a shared location.");
1343 private void recursivelyAddRelation(int idx, CompositeLocation srcInferLoc,
1344 CompositeLocation dstInferLoc) throws CyclicFlowException {
1346 String srcLocSymbol = srcInferLoc.get(idx).getLocIdentifier();
1347 String dstLocSymbol = dstInferLoc.get(idx).getLocIdentifier();
1349 Descriptor parentDesc = srcInferLoc.get(idx).getDescriptor();
1351 if (srcLocSymbol.equals(dstLocSymbol)) {
1352 // check if it is the case of shared location
1353 if (srcInferLoc.getSize() == (idx + 1) && dstInferLoc.getSize() == (idx + 1)) {
1354 Location inferLocElement = srcInferLoc.get(idx);
1355 System.out.println("SET SHARED LOCATION=" + inferLocElement);
1356 getLattice(inferLocElement.getDescriptor())
1357 .addSharedLoc(inferLocElement.getLocIdentifier());
1358 } else if (srcInferLoc.getSize() > (idx + 1) && dstInferLoc.getSize() > (idx + 1)) {
1359 recursivelyAddRelation(idx + 1, srcInferLoc, dstInferLoc);
1362 addRelationHigherToLower(getLattice(parentDesc), getLocationInfo(parentDesc), srcLocSymbol,
1367 private void recursivelyAddCompositeRelation(MethodDescriptor md, FlowGraph flowGraph,
1368 MethodLocationInfo methodInfo, FlowNode srcNode, FlowNode dstNode, Descriptor srcDesc,
1369 Descriptor dstDesc) throws CyclicFlowException {
1371 CompositeLocation inferSrcLoc;
1372 CompositeLocation inferDstLoc = methodInfo.getInferLocation(dstDesc);
1374 if (srcNode.getDescTuple().size() > 1) {
1376 inferSrcLoc = new CompositeLocation();
1378 NTuple<Location> locTuple = flowGraph.getLocationTuple(srcNode);
1379 for (int i = 0; i < locTuple.size(); i++) {
1380 inferSrcLoc.addLocation(locTuple.get(i));
1384 inferSrcLoc = methodInfo.getInferLocation(srcDesc);
1387 if (dstNode.getDescTuple().size() > 1) {
1389 inferDstLoc = new CompositeLocation();
1391 NTuple<Location> locTuple = flowGraph.getLocationTuple(dstNode);
1392 for (int i = 0; i < locTuple.size(); i++) {
1393 inferDstLoc.addLocation(locTuple.get(i));
1397 inferDstLoc = methodInfo.getInferLocation(dstDesc);
1400 recursiveAddRelationToLattice(1, md, inferSrcLoc, inferDstLoc);
1403 private void addPrefixMapping(Map<NTuple<Location>, Set<NTuple<Location>>> map,
1404 NTuple<Location> prefix, NTuple<Location> element) {
1406 if (!map.containsKey(prefix)) {
1407 map.put(prefix, new HashSet<NTuple<Location>>());
1409 map.get(prefix).add(element);
1412 private boolean calculateCompositeLocation(FlowGraph flowGraph,
1413 SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo, FlowNode flowNode)
1414 throws CyclicFlowException {
1416 Descriptor localVarDesc = flowNode.getDescTuple().get(0);
1417 NTuple<Location> flowNodelocTuple = flowGraph.getLocationTuple(flowNode);
1419 if (localVarDesc.equals(methodInfo.getMethodDesc())) {
1423 Set<FlowNode> inNodeSet = flowGraph.getIncomingFlowNodeSet(flowNode);
1424 Set<FlowNode> reachableNodeSet = flowGraph.getReachableFlowNodeSet(flowNode);
1426 Map<NTuple<Location>, Set<NTuple<Location>>> mapPrefixToIncomingLocTupleSet =
1427 new HashMap<NTuple<Location>, Set<NTuple<Location>>>();
1429 List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
1431 for (Iterator iterator = inNodeSet.iterator(); iterator.hasNext();) {
1432 FlowNode inNode = (FlowNode) iterator.next();
1433 NTuple<Location> inNodeTuple = flowGraph.getLocationTuple(inNode);
1435 CompositeLocation inNodeInferredLoc =
1436 generateInferredCompositeLocation(methodInfo, inNodeTuple);
1438 NTuple<Location> inNodeInferredLocTuple = inNodeInferredLoc.getTuple();
1440 for (int i = 1; i < inNodeInferredLocTuple.size(); i++) {
1441 NTuple<Location> prefix = inNodeInferredLocTuple.subList(0, i);
1442 if (!prefixList.contains(prefix)) {
1443 prefixList.add(prefix);
1445 addPrefixMapping(mapPrefixToIncomingLocTupleSet, prefix, inNodeInferredLocTuple);
1449 Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
1450 public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
1451 int s0 = arg0.size();
1452 int s1 = arg1.size();
1455 } else if (s0 == s1) {
1463 System.out.println("prefixList=" + prefixList);
1464 System.out.println("reachableNodeSet=" + reachableNodeSet);
1466 // find out reachable nodes that have the longest common prefix
1467 for (int i = 0; i < prefixList.size(); i++) {
1468 NTuple<Location> curPrefix = prefixList.get(i);
1469 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
1471 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1472 FlowNode reachableNode = (FlowNode) iterator2.next();
1473 NTuple<Location> reachLocTuple = flowGraph.getLocationTuple(reachableNode);
1474 CompositeLocation reachLocInferLoc =
1475 generateInferredCompositeLocation(methodInfo, reachLocTuple);
1476 if (reachLocInferLoc.getTuple().startsWith(curPrefix)) {
1477 reachableCommonPrefixSet.add(reachLocTuple);
1481 if (!reachableCommonPrefixSet.isEmpty()) {
1482 // found reachable nodes that start with the prefix curPrefix
1483 // need to assign a composite location
1485 // first, check if there are more than one the set of locations that has
1486 // the same length of the longest reachable prefix, no way to assign
1487 // a composite location to the input local var
1488 prefixSanityCheck(prefixList, i, flowGraph, reachableNodeSet);
1490 Set<NTuple<Location>> incomingCommonPrefixSet =
1491 mapPrefixToIncomingLocTupleSet.get(curPrefix);
1493 int idx = curPrefix.size();
1494 NTuple<Location> element = incomingCommonPrefixSet.iterator().next();
1495 Descriptor desc = element.get(idx).getDescriptor();
1497 SSJavaLattice<String> lattice = getLattice(desc);
1498 LocationInfo locInfo = getLocationInfo(desc);
1500 CompositeLocation inferLocation =
1501 generateInferredCompositeLocation(methodInfo, flowNodelocTuple);
1503 // methodInfo.getInferLocation(localVarDesc);
1504 CompositeLocation newInferLocation = new CompositeLocation();
1506 if (inferLocation.getTuple().startsWith(curPrefix)) {
1507 // the same infer location is already existed. no need to do
1509 System.out.println("NO ATTEMPT TO MAKE A COMPOSITE LOCATION curPrefix=" + curPrefix);
1512 // assign a new composite location
1514 // String oldMethodLocationSymbol =
1515 // inferLocation.get(0).getLocIdentifier();
1516 String newLocSymbol = "Loc" + (SSJavaLattice.seed++);
1517 for (int locIdx = 0; locIdx < curPrefix.size(); locIdx++) {
1518 newInferLocation.addLocation(curPrefix.get(locIdx));
1520 Location newLocationElement = new Location(desc, newLocSymbol);
1521 newInferLocation.addLocation(newLocationElement);
1523 // maps local variable to location types of the common prefix
1524 methodInfo.mapDescriptorToLocation(localVarDesc, newInferLocation.clone());
1526 // methodInfo.mapDescriptorToLocation(localVarDesc, newInferLocation);
1527 addMapLocSymbolToInferredLocation(methodInfo.getMethodDesc(), localVarDesc,
1529 methodInfo.removeMaplocalVarToLocSet(localVarDesc);
1531 // add the field/var descriptor to the set of the location symbol
1532 int lastIdx = flowNode.getDescTuple().size() - 1;
1533 Descriptor lastFlowNodeDesc = flowNode.getDescTuple().get(lastIdx);
1534 Descriptor enclosinglastLastFlowNodeDesc = flowNodelocTuple.get(lastIdx).getDescriptor();
1536 CompositeLocation newlyInferredLocForFlowNode =
1537 generateInferredCompositeLocation(methodInfo, flowNodelocTuple);
1538 Location lastInferLocElement =
1539 newlyInferredLocForFlowNode.get(newlyInferredLocForFlowNode.getSize() - 1);
1540 Descriptor enclosingLastInferLocElement = lastInferLocElement.getDescriptor();
1542 // getLocationInfo(enclosingLastInferLocElement).addMapLocSymbolToDescSet(
1543 // lastInferLocElement.getLocIdentifier(), lastFlowNodeDesc);
1544 getLocationInfo(enclosingLastInferLocElement).addMapLocSymbolToRelatedInferLoc(
1545 lastInferLocElement.getLocIdentifier(), enclosinglastLastFlowNodeDesc,
1548 // clean up the previous location
1549 // Location prevInferLocElement =
1550 // inferLocation.get(inferLocation.getSize() - 1);
1551 // Descriptor prevEnclosingDesc = prevInferLocElement.getDescriptor();
1553 // SSJavaLattice<String> targetLattice;
1554 // LocationInfo targetInfo;
1555 // if (prevEnclosingDesc.equals(methodInfo.getMethodDesc())) {
1556 // targetLattice = methodLattice;
1557 // targetInfo = methodInfo;
1559 // targetLattice = getLattice(prevInferLocElement.getDescriptor());
1560 // targetInfo = getLocationInfo(prevInferLocElement.getDescriptor());
1563 // Set<Pair<Descriptor, Descriptor>> associstedDescSet =
1564 // targetInfo.getRelatedInferLocSet(prevInferLocElement.getLocIdentifier());
1566 // if (associstedDescSet.size() == 1) {
1567 // targetLattice.remove(prevInferLocElement.getLocIdentifier());
1569 // associstedDescSet.remove(lastFlowNodeDesc);
1574 System.out.println("curPrefix=" + curPrefix);
1575 System.out.println("ASSIGN NEW COMPOSITE LOCATION =" + newInferLocation + " to "
1578 String newlyInsertedLocName =
1579 newInferLocation.get(newInferLocation.getSize() - 1).getLocIdentifier();
1581 System.out.println("-- add in-flow");
1582 for (Iterator iterator = incomingCommonPrefixSet.iterator(); iterator.hasNext();) {
1583 NTuple<Location> tuple = (NTuple<Location>) iterator.next();
1584 Location loc = tuple.get(idx);
1585 String higher = loc.getLocIdentifier();
1586 addRelationHigherToLower(lattice, locInfo, higher, newlyInsertedLocName);
1589 System.out.println("-- add out flow");
1590 for (Iterator iterator = reachableCommonPrefixSet.iterator(); iterator.hasNext();) {
1591 NTuple<Location> tuple = (NTuple<Location>) iterator.next();
1592 if (tuple.size() > idx) {
1593 Location loc = tuple.get(idx);
1594 String lower = loc.getLocIdentifier();
1596 // locInfo.getFieldInferLocation(loc.getLocDescriptor()).getLocIdentifier();
1597 addRelationHigherToLower(lattice, locInfo, newlyInsertedLocName, lower);
1610 private void addMapLocSymbolToInferredLocation(MethodDescriptor md, Descriptor localVar,
1611 CompositeLocation inferLoc) {
1613 Location locElement = inferLoc.get((inferLoc.getSize() - 1));
1614 Descriptor enclosingDesc = locElement.getDescriptor();
1615 LocationInfo locInfo = getLocationInfo(enclosingDesc);
1616 locInfo.addMapLocSymbolToRelatedInferLoc(locElement.getLocIdentifier(), md, localVar);
1619 private boolean isCompositeLocation(CompositeLocation cl) {
1620 return cl.getSize() > 1;
1623 private boolean containsNonPrimitiveElement(Set<Descriptor> descSet) {
1624 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
1625 Descriptor desc = (Descriptor) iterator.next();
1627 if (desc.equals(LocationInference.GLOBALDESC)) {
1629 } else if (desc instanceof VarDescriptor) {
1630 if (!((VarDescriptor) desc).getType().isPrimitive()) {
1633 } else if (desc instanceof FieldDescriptor) {
1634 if (!((FieldDescriptor) desc).getType().isPrimitive()) {
1643 private void addRelationHigherToLower(SSJavaLattice<String> lattice, LocationInfo locInfo,
1644 String higher, String lower) throws CyclicFlowException {
1646 System.out.println("---addRelationHigherToLower " + higher + " -> " + lower
1647 + " to the lattice of " + locInfo.getDescIdentifier());
1648 // if (higher.equals(lower) && lattice.isSharedLoc(higher)) {
1651 Set<String> cycleElementSet = lattice.getPossibleCycleElements(higher, lower);
1653 boolean hasNonPrimitiveElement = false;
1654 for (Iterator iterator = cycleElementSet.iterator(); iterator.hasNext();) {
1655 String cycleElementLocSymbol = (String) iterator.next();
1657 Set<Descriptor> descSet = locInfo.getDescSet(cycleElementLocSymbol);
1658 if (containsNonPrimitiveElement(descSet)) {
1659 hasNonPrimitiveElement = true;
1664 if (hasNonPrimitiveElement) {
1665 System.out.println("#Check cycle= " + lower + " < " + higher + " cycleElementSet="
1667 // if there is non-primitive element in the cycle, no way to merge cyclic
1668 // elements into the shared location
1669 throw new CyclicFlowException();
1672 if (cycleElementSet.size() > 0) {
1674 String newSharedLoc = "SharedLoc" + (SSJavaLattice.seed++);
1676 System.out.println("---ASSIGN NEW SHARED LOC=" + newSharedLoc + " to " + cycleElementSet);
1677 lattice.mergeIntoSharedLocation(cycleElementSet, newSharedLoc);
1679 for (Iterator iterator = cycleElementSet.iterator(); iterator.hasNext();) {
1680 String oldLocSymbol = (String) iterator.next();
1682 Set<Pair<Descriptor, Descriptor>> inferLocSet = locInfo.getRelatedInferLocSet(oldLocSymbol);
1683 System.out.println("---update related locations=" + inferLocSet);
1684 for (Iterator iterator2 = inferLocSet.iterator(); iterator2.hasNext();) {
1685 Pair<Descriptor, Descriptor> pair = (Pair<Descriptor, Descriptor>) iterator2.next();
1686 Descriptor enclosingDesc = pair.getFirst();
1687 Descriptor desc = pair.getSecond();
1689 CompositeLocation inferLoc;
1690 if (curMethodInfo.md.equals(enclosingDesc)) {
1691 inferLoc = curMethodInfo.getInferLocation(desc);
1693 inferLoc = getLocationInfo(enclosingDesc).getInferLocation(desc);
1696 Location locElement = inferLoc.get(inferLoc.getSize() - 1);
1698 locElement.setLocIdentifier(newSharedLoc);
1699 locInfo.addMapLocSymbolToRelatedInferLoc(newSharedLoc, enclosingDesc, desc);
1701 if (curMethodInfo.md.equals(enclosingDesc)) {
1702 inferLoc = curMethodInfo.getInferLocation(desc);
1704 inferLoc = getLocationInfo(enclosingDesc).getInferLocation(desc);
1706 System.out.println("---New Infer Loc=" + inferLoc);
1709 locInfo.removeRelatedInferLocSet(oldLocSymbol, newSharedLoc);
1713 lattice.addSharedLoc(newSharedLoc);
1715 } else if (!lattice.isGreaterThan(higher, lower)) {
1716 lattice.addRelationHigherToLower(higher, lower);
1720 private void replaceOldLocWithNewLoc(SSJavaLattice<String> methodLattice, String oldLocSymbol,
1721 String newLocSymbol) {
1723 if (methodLattice.containsKey(oldLocSymbol)) {
1724 methodLattice.substituteLocation(oldLocSymbol, newLocSymbol);
1729 private void prefixSanityCheck(List<NTuple<Location>> prefixList, int curIdx,
1730 FlowGraph flowGraph, Set<FlowNode> reachableNodeSet) {
1732 NTuple<Location> curPrefix = prefixList.get(curIdx);
1734 for (int i = curIdx + 1; i < prefixList.size(); i++) {
1735 NTuple<Location> prefixTuple = prefixList.get(i);
1737 if (curPrefix.startsWith(prefixTuple)) {
1741 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1742 FlowNode reachableNode = (FlowNode) iterator2.next();
1743 NTuple<Location> reachLocTuple = flowGraph.getLocationTuple(reachableNode);
1744 if (reachLocTuple.startsWith(prefixTuple)) {
1746 throw new Error("Failed to generate a composite location");
1752 public boolean isPrimitiveLocalVariable(FlowNode node) {
1753 VarDescriptor varDesc = (VarDescriptor) node.getDescTuple().get(0);
1754 return varDesc.getType().isPrimitive();
1757 private SSJavaLattice<String> getLattice(Descriptor d) {
1758 if (d instanceof MethodDescriptor) {
1759 return getMethodLattice((MethodDescriptor) d);
1761 return getFieldLattice((ClassDescriptor) d);
1765 private SSJavaLattice<String> getMethodLattice(MethodDescriptor md) {
1766 if (!md2lattice.containsKey(md)) {
1767 md2lattice.put(md, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
1769 return md2lattice.get(md);
1772 private void setMethodLattice(MethodDescriptor md, SSJavaLattice<String> lattice) {
1773 md2lattice.put(md, lattice);
1776 private void extractRelationFromFieldFlows(ClassDescriptor cd, FlowNode srcNode,
1777 FlowNode dstNode, int idx) throws CyclicFlowException {
1779 if (srcNode.getDescTuple().get(idx).equals(dstNode.getDescTuple().get(idx))
1780 && srcNode.getDescTuple().size() > (idx + 1) && dstNode.getDescTuple().size() > (idx + 1)) {
1781 // value flow between fields: we don't need to add a binary relation
1784 Descriptor desc = srcNode.getDescTuple().get(idx);
1785 ClassDescriptor classDesc;
1788 classDesc = ((VarDescriptor) desc).getType().getClassDesc();
1790 classDesc = ((FieldDescriptor) desc).getType().getClassDesc();
1793 extractRelationFromFieldFlows(classDesc, srcNode, dstNode, idx + 1);
1797 Descriptor srcFieldDesc = srcNode.getDescTuple().get(idx);
1798 Descriptor dstFieldDesc = dstNode.getDescTuple().get(idx);
1800 // add a new binary relation of dstNode < srcNode
1801 SSJavaLattice<String> fieldLattice = getFieldLattice(cd);
1802 LocationInfo fieldInfo = getFieldLocationInfo(cd);
1804 String srcSymbol = fieldInfo.getFieldInferLocation(srcFieldDesc).getLocIdentifier();
1805 String dstSymbol = fieldInfo.getFieldInferLocation(dstFieldDesc).getLocIdentifier();
1807 addRelationHigherToLower(fieldLattice, fieldInfo, srcSymbol, dstSymbol);
1813 public SSJavaLattice<String> getFieldLattice(ClassDescriptor cd) {
1814 if (!cd2lattice.containsKey(cd)) {
1815 cd2lattice.put(cd, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
1817 return cd2lattice.get(cd);
1820 public LinkedList<MethodDescriptor> computeMethodList() {
1822 Set<MethodDescriptor> toSort = new HashSet<MethodDescriptor>();
1826 Set<MethodDescriptor> visited = new HashSet<MethodDescriptor>();
1827 Set<MethodDescriptor> reachableCallee = new HashSet<MethodDescriptor>();
1829 while (!toAnalyzeIsEmpty()) {
1830 ClassDescriptor cd = toAnalyzeNext();
1832 setupToAnalazeMethod(cd);
1833 toanalyzeMethodList.removeAll(visited);
1835 while (!toAnalyzeMethodIsEmpty()) {
1836 MethodDescriptor md = toAnalyzeMethodNext();
1837 if ((!visited.contains(md))
1838 && (ssjava.needTobeAnnotated(md) || reachableCallee.contains(md))) {
1840 // creates a mapping from a method descriptor to virtual methods
1841 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1842 if (md.isStatic()) {
1843 setPossibleCallees.add(md);
1845 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
1848 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getCalleeSet(md);
1849 Set<MethodDescriptor> needToAnalyzeCalleeSet = new HashSet<MethodDescriptor>();
1851 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
1852 MethodDescriptor calleemd = (MethodDescriptor) iterator.next();
1853 if ((!ssjava.isTrustMethod(calleemd))
1854 && (!ssjava.isSSJavaUtil(calleemd.getClassDesc()))) {
1855 if (!visited.contains(calleemd)) {
1856 toanalyzeMethodList.add(calleemd);
1858 reachableCallee.add(calleemd);
1859 needToAnalyzeCalleeSet.add(calleemd);
1863 mapMethodToCalleeSet.put(md, needToAnalyzeCalleeSet);
1872 return ssjava.topologicalSort(toSort);
1876 public void constructFlowGraph() {
1878 LinkedList<MethodDescriptor> methodDescList = computeMethodList();
1880 while (!methodDescList.isEmpty()) {
1881 MethodDescriptor md = methodDescList.removeLast();
1882 if (state.SSJAVADEBUG) {
1883 System.out.println();
1884 System.out.println("SSJAVA: Constructing a flow graph: " + md);
1886 // creates a mapping from a parameter descriptor to its index
1887 Map<Descriptor, Integer> mapParamDescToIdx = new HashMap<Descriptor, Integer>();
1889 if (!md.isStatic()) {
1891 mapParamDescToIdx.put(md.getThis(), 0);
1894 for (int i = 0; i < md.numParameters(); i++) {
1895 Descriptor paramDesc = (Descriptor) md.getParameter(i);
1896 mapParamDescToIdx.put(paramDesc, new Integer(i + offset));
1899 FlowGraph fg = new FlowGraph(md, mapParamDescToIdx);
1900 mapMethodDescriptorToFlowGraph.put(md, fg);
1902 analyzeMethodBody(md.getClassDesc(), md);
1905 _debug_printGraph();
1909 private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md) {
1910 BlockNode bn = state.getMethodBody(md);
1911 NodeTupleSet implicitFlowTupleSet = new NodeTupleSet();
1912 analyzeFlowBlockNode(md, md.getParameterTable(), bn, implicitFlowTupleSet);
1915 private void analyzeFlowBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn,
1916 NodeTupleSet implicitFlowTupleSet) {
1918 bn.getVarTable().setParent(nametable);
1919 for (int i = 0; i < bn.size(); i++) {
1920 BlockStatementNode bsn = bn.get(i);
1921 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
1926 private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
1927 BlockStatementNode bsn, NodeTupleSet implicitFlowTupleSet) {
1929 switch (bsn.kind()) {
1930 case Kind.BlockExpressionNode:
1931 analyzeBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn, implicitFlowTupleSet);
1934 case Kind.DeclarationNode:
1935 analyzeFlowDeclarationNode(md, nametable, (DeclarationNode) bsn, implicitFlowTupleSet);
1938 case Kind.IfStatementNode:
1939 analyzeFlowIfStatementNode(md, nametable, (IfStatementNode) bsn, implicitFlowTupleSet);
1943 analyzeFlowLoopNode(md, nametable, (LoopNode) bsn, implicitFlowTupleSet);
1946 case Kind.ReturnNode:
1947 analyzeFlowReturnNode(md, nametable, (ReturnNode) bsn, implicitFlowTupleSet);
1950 case Kind.SubBlockNode:
1951 analyzeFlowSubBlockNode(md, nametable, (SubBlockNode) bsn, implicitFlowTupleSet);
1954 case Kind.ContinueBreakNode:
1957 case Kind.SwitchStatementNode:
1958 analyzeSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn);
1965 private void analyzeSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
1966 SwitchStatementNode bsn) {
1967 // TODO Auto-generated method stub
1970 private void analyzeFlowSubBlockNode(MethodDescriptor md, SymbolTable nametable,
1971 SubBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
1972 analyzeFlowBlockNode(md, nametable, sbn.getBlockNode(), implicitFlowTupleSet);
1975 private void analyzeFlowReturnNode(MethodDescriptor md, SymbolTable nametable, ReturnNode rn,
1976 NodeTupleSet implicitFlowTupleSet) {
1978 ExpressionNode returnExp = rn.getReturnExpression();
1980 if (returnExp != null) {
1981 NodeTupleSet nodeSet = new NodeTupleSet();
1982 analyzeFlowExpressionNode(md, nametable, returnExp, nodeSet, false);
1984 FlowGraph fg = getFlowGraph(md);
1986 // annotate the elements of the node set as the return location
1987 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1988 NTuple<Descriptor> returnDescTuple = (NTuple<Descriptor>) iterator.next();
1989 fg.addReturnFlowNode(returnDescTuple);
1990 for (Iterator iterator2 = implicitFlowTupleSet.iterator(); iterator2.hasNext();) {
1991 NTuple<Descriptor> implicitFlowDescTuple = (NTuple<Descriptor>) iterator2.next();
1992 fg.addValueFlowEdge(implicitFlowDescTuple, returnDescTuple);
1999 private void analyzeFlowLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln,
2000 NodeTupleSet implicitFlowTupleSet) {
2002 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
2004 NodeTupleSet condTupleNode = new NodeTupleSet();
2005 analyzeFlowExpressionNode(md, nametable, ln.getCondition(), condTupleNode, null,
2006 implicitFlowTupleSet, false);
2007 condTupleNode.addTupleSet(implicitFlowTupleSet);
2009 // add edges from condNodeTupleSet to all nodes of conditional nodes
2010 analyzeFlowBlockNode(md, nametable, ln.getBody(), condTupleNode);
2013 // check 'for loop' case
2014 BlockNode bn = ln.getInitializer();
2015 bn.getVarTable().setParent(nametable);
2016 for (int i = 0; i < bn.size(); i++) {
2017 BlockStatementNode bsn = bn.get(i);
2018 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
2021 NodeTupleSet condTupleNode = new NodeTupleSet();
2022 analyzeFlowExpressionNode(md, bn.getVarTable(), ln.getCondition(), condTupleNode, null,
2023 implicitFlowTupleSet, false);
2024 condTupleNode.addTupleSet(implicitFlowTupleSet);
2026 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(), condTupleNode);
2027 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(), condTupleNode);
2033 private void analyzeFlowIfStatementNode(MethodDescriptor md, SymbolTable nametable,
2034 IfStatementNode isn, NodeTupleSet implicitFlowTupleSet) {
2036 NodeTupleSet condTupleNode = new NodeTupleSet();
2037 analyzeFlowExpressionNode(md, nametable, isn.getCondition(), condTupleNode, null,
2038 implicitFlowTupleSet, false);
2040 // add edges from condNodeTupleSet to all nodes of conditional nodes
2041 condTupleNode.addTupleSet(implicitFlowTupleSet);
2042 analyzeFlowBlockNode(md, nametable, isn.getTrueBlock(), condTupleNode);
2044 if (isn.getFalseBlock() != null) {
2045 analyzeFlowBlockNode(md, nametable, isn.getFalseBlock(), condTupleNode);
2050 private void analyzeFlowDeclarationNode(MethodDescriptor md, SymbolTable nametable,
2051 DeclarationNode dn, NodeTupleSet implicitFlowTupleSet) {
2053 VarDescriptor vd = dn.getVarDescriptor();
2054 mapDescToDefinitionLine.put(vd, dn.getNumLine());
2055 NTuple<Descriptor> tupleLHS = new NTuple<Descriptor>();
2057 FlowNode fn = getFlowGraph(md).createNewFlowNode(tupleLHS);
2058 fn.setDeclarationNode();
2060 if (dn.getExpression() != null) {
2062 NodeTupleSet tupleSetRHS = new NodeTupleSet();
2063 analyzeFlowExpressionNode(md, nametable, dn.getExpression(), tupleSetRHS, null,
2064 implicitFlowTupleSet, false);
2066 // add a new flow edge from rhs to lhs
2067 for (Iterator<NTuple<Descriptor>> iter = tupleSetRHS.iterator(); iter.hasNext();) {
2068 NTuple<Descriptor> from = iter.next();
2069 addFlowGraphEdge(md, from, tupleLHS);
2076 private void analyzeBlockExpressionNode(MethodDescriptor md, SymbolTable nametable,
2077 BlockExpressionNode ben, NodeTupleSet implicitFlowTupleSet) {
2078 analyzeFlowExpressionNode(md, nametable, ben.getExpression(), null, null, implicitFlowTupleSet,
2082 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
2083 ExpressionNode en, NodeTupleSet nodeSet, boolean isLHS) {
2084 return analyzeFlowExpressionNode(md, nametable, en, nodeSet, null, new NodeTupleSet(), isLHS);
2087 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
2088 ExpressionNode en, NodeTupleSet nodeSet, NTuple<Descriptor> base,
2089 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
2091 // note that expression node can create more than one flow node
2092 // nodeSet contains of flow nodes
2093 // base is always assigned to null except the case of a name node!
2095 NTuple<Descriptor> flowTuple;
2097 switch (en.kind()) {
2099 case Kind.AssignmentNode:
2100 analyzeFlowAssignmentNode(md, nametable, (AssignmentNode) en, nodeSet, base,
2101 implicitFlowTupleSet);
2104 case Kind.FieldAccessNode:
2106 analyzeFlowFieldAccessNode(md, nametable, (FieldAccessNode) en, nodeSet, base,
2107 implicitFlowTupleSet, isLHS);
2108 if (flowTuple != null) {
2109 nodeSet.addTuple(flowTuple);
2114 NodeTupleSet nameNodeSet = new NodeTupleSet();
2116 analyzeFlowNameNode(md, nametable, (NameNode) en, nameNodeSet, base, implicitFlowTupleSet);
2117 if (flowTuple != null) {
2118 nodeSet.addTuple(flowTuple);
2123 analyzeFlowOpNode(md, nametable, (OpNode) en, nodeSet, implicitFlowTupleSet);
2126 case Kind.CreateObjectNode:
2127 analyzeCreateObjectNode(md, nametable, (CreateObjectNode) en);
2130 case Kind.ArrayAccessNode:
2131 analyzeFlowArrayAccessNode(md, nametable, (ArrayAccessNode) en, nodeSet, isLHS);
2134 case Kind.LiteralNode:
2135 analyzeLiteralNode(md, nametable, (LiteralNode) en);
2138 case Kind.MethodInvokeNode:
2139 analyzeFlowMethodInvokeNode(md, nametable, (MethodInvokeNode) en, nodeSet,
2140 implicitFlowTupleSet);
2143 case Kind.TertiaryNode:
2144 analyzeFlowTertiaryNode(md, nametable, (TertiaryNode) en, nodeSet, implicitFlowTupleSet);
2148 analyzeFlowCastNode(md, nametable, (CastNode) en, nodeSet, base, implicitFlowTupleSet);
2150 // case Kind.InstanceOfNode:
2151 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
2154 // case Kind.ArrayInitializerNode:
2155 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
2159 // case Kind.ClassTypeNode:
2160 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
2163 // case Kind.OffsetNode:
2164 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
2172 private void analyzeFlowCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn,
2173 NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
2175 analyzeFlowExpressionNode(md, nametable, cn.getExpression(), nodeSet, base,
2176 implicitFlowTupleSet, false);
2180 private void analyzeFlowTertiaryNode(MethodDescriptor md, SymbolTable nametable, TertiaryNode tn,
2181 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
2183 NodeTupleSet tertiaryTupleNode = new NodeTupleSet();
2184 analyzeFlowExpressionNode(md, nametable, tn.getCond(), tertiaryTupleNode, null,
2185 implicitFlowTupleSet, false);
2187 // add edges from tertiaryTupleNode to all nodes of conditional nodes
2188 tertiaryTupleNode.addTupleSet(implicitFlowTupleSet);
2189 analyzeFlowExpressionNode(md, nametable, tn.getTrueExpr(), tertiaryTupleNode, null,
2190 implicitFlowTupleSet, false);
2192 analyzeFlowExpressionNode(md, nametable, tn.getFalseExpr(), tertiaryTupleNode, null,
2193 implicitFlowTupleSet, false);
2195 nodeSet.addTupleSet(tertiaryTupleNode);
2199 private void addMapCallerMethodDescToMethodInvokeNodeSet(MethodDescriptor caller,
2200 MethodInvokeNode min) {
2201 Set<MethodInvokeNode> set = mapMethodDescriptorToMethodInvokeNodeSet.get(caller);
2203 set = new HashSet<MethodInvokeNode>();
2204 mapMethodDescriptorToMethodInvokeNodeSet.put(caller, set);
2209 private void addParamNodeFlowingToReturnValue(MethodDescriptor md, FlowNode fn) {
2211 if (!mapMethodDescToParamNodeFlowsToReturnValue.containsKey(md)) {
2212 mapMethodDescToParamNodeFlowsToReturnValue.put(md, new HashSet<FlowNode>());
2214 mapMethodDescToParamNodeFlowsToReturnValue.get(md).add(fn);
2217 private Set<FlowNode> getParamNodeFlowingToReturnValue(MethodDescriptor md) {
2218 return mapMethodDescToParamNodeFlowsToReturnValue.get(md);
2221 private void analyzeFlowMethodInvokeNode(MethodDescriptor md, SymbolTable nametable,
2222 MethodInvokeNode min, NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
2225 if (nodeSet == null) {
2226 nodeSet = new NodeTupleSet();
2229 addMapCallerMethodDescToMethodInvokeNodeSet(md, min);
2231 MethodDescriptor calleeMethodDesc = min.getMethod();
2233 NameDescriptor baseName = min.getBaseName();
2234 boolean isSystemout = false;
2235 if (baseName != null) {
2236 isSystemout = baseName.getSymbol().equals("System.out");
2239 if (!ssjava.isSSJavaUtil(calleeMethodDesc.getClassDesc())
2240 && !ssjava.isTrustMethod(calleeMethodDesc) && !isSystemout) {
2242 FlowGraph calleeFlowGraph = getFlowGraph(calleeMethodDesc);
2243 Set<FlowNode> calleeReturnSet = calleeFlowGraph.getReturnNodeSet();
2245 if (min.getExpression() != null) {
2247 NodeTupleSet baseNodeSet = new NodeTupleSet();
2248 analyzeFlowExpressionNode(md, nametable, min.getExpression(), baseNodeSet, null,
2249 implicitFlowTupleSet, false);
2252 if (!min.getMethod().isStatic()) {
2253 addArgIdxMap(min, 0, baseNodeSet);
2256 for (Iterator iterator = calleeReturnSet.iterator(); iterator.hasNext();) {
2257 FlowNode returnNode = (FlowNode) iterator.next();
2258 NTuple<Descriptor> returnDescTuple = returnNode.getDescTuple();
2259 if (returnDescTuple.startsWith(calleeMethodDesc.getThis())) {
2260 // the location type of the return value is started with 'this'
2262 for (Iterator<NTuple<Descriptor>> baseIter = baseNodeSet.iterator(); baseIter
2264 NTuple<Descriptor> baseTuple = baseIter.next();
2265 NTuple<Descriptor> inFlowTuple = new NTuple<Descriptor>(baseTuple.getList());
2266 inFlowTuple.addAll(returnDescTuple.subList(1, returnDescTuple.size()));
2267 nodeSet.addTuple(inFlowTuple);
2270 Set<FlowNode> inFlowSet = calleeFlowGraph.getIncomingFlowNodeSet(returnNode);
2271 for (Iterator iterator2 = inFlowSet.iterator(); iterator2.hasNext();) {
2272 FlowNode inFlowNode = (FlowNode) iterator2.next();
2273 if (inFlowNode.getDescTuple().startsWith(calleeMethodDesc.getThis())) {
2274 nodeSet.addTupleSet(baseNodeSet);
2282 // analyze parameter flows
2284 if (min.numArgs() > 0) {
2287 if (min.getMethod().isStatic()) {
2293 for (int i = 0; i < min.numArgs(); i++) {
2294 ExpressionNode en = min.getArg(i);
2295 int idx = i + offset;
2296 NodeTupleSet argTupleSet = new NodeTupleSet();
2297 analyzeFlowExpressionNode(md, nametable, en, argTupleSet, true);
2298 // if argument is liternal node, argTuple is set to NULL.
2299 addArgIdxMap(min, idx, argTupleSet);
2300 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
2301 if (hasInFlowTo(calleeFlowGraph, paramNode, calleeReturnSet)
2302 || calleeMethodDesc.getModifiers().isNative()) {
2303 addParamNodeFlowingToReturnValue(calleeMethodDesc, paramNode);
2304 nodeSet.addTupleSet(argTupleSet);
2314 private boolean hasInFlowTo(FlowGraph fg, FlowNode inNode, Set<FlowNode> nodeSet) {
2315 // return true if inNode has in-flows to nodeSet
2316 Set<FlowNode> reachableSet = fg.getReachableFlowNodeSet(inNode);
2317 for (Iterator iterator = reachableSet.iterator(); iterator.hasNext();) {
2318 FlowNode fn = (FlowNode) iterator.next();
2319 if (nodeSet.contains(fn)) {
2326 private NodeTupleSet getNodeTupleSetByArgIdx(MethodInvokeNode min, int idx) {
2327 return mapMethodInvokeNodeToArgIdxMap.get(min).get(new Integer(idx));
2330 private void addArgIdxMap(MethodInvokeNode min, int idx, NodeTupleSet tupleSet) {
2331 Map<Integer, NodeTupleSet> mapIdxToTupleSet = mapMethodInvokeNodeToArgIdxMap.get(min);
2332 if (mapIdxToTupleSet == null) {
2333 mapIdxToTupleSet = new HashMap<Integer, NodeTupleSet>();
2334 mapMethodInvokeNodeToArgIdxMap.put(min, mapIdxToTupleSet);
2336 mapIdxToTupleSet.put(new Integer(idx), tupleSet);
2339 private void analyzeFlowMethodParameters(MethodDescriptor callermd, SymbolTable nametable,
2340 MethodInvokeNode min, NodeTupleSet nodeSet) {
2342 if (min.numArgs() > 0) {
2345 if (min.getMethod().isStatic()) {
2349 // NTuple<Descriptor> thisArgTuple = new NTuple<Descriptor>();
2350 // thisArgTuple.add(callermd.getThis());
2351 // NodeTupleSet argTupleSet = new NodeTupleSet();
2352 // argTupleSet.addTuple(thisArgTuple);
2353 // addArgIdxMap(min, 0, argTupleSet);
2354 // nodeSet.addTuple(thisArgTuple);
2357 for (int i = 0; i < min.numArgs(); i++) {
2358 ExpressionNode en = min.getArg(i);
2359 NodeTupleSet argTupleSet = new NodeTupleSet();
2360 analyzeFlowExpressionNode(callermd, nametable, en, argTupleSet, false);
2361 // if argument is liternal node, argTuple is set to NULL.
2362 addArgIdxMap(min, i + offset, argTupleSet);
2363 nodeSet.addTupleSet(argTupleSet);
2370 private void analyzeLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode en) {
2374 private void analyzeFlowArrayAccessNode(MethodDescriptor md, SymbolTable nametable,
2375 ArrayAccessNode aan, NodeTupleSet nodeSet, boolean isLHS) {
2377 NodeTupleSet expNodeTupleSet = new NodeTupleSet();
2378 analyzeFlowExpressionNode(md, nametable, aan.getExpression(), expNodeTupleSet, isLHS);
2380 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
2381 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, isLHS);
2384 // need to create an edge from idx to array
2385 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
2386 NTuple<Descriptor> idxTuple = idxIter.next();
2387 for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
2388 NTuple<Descriptor> arrTuple = arrIter.next();
2389 getFlowGraph(md).addValueFlowEdge(idxTuple, arrTuple);
2393 nodeSet.addTupleSet(expNodeTupleSet);
2395 nodeSet.addTupleSet(expNodeTupleSet);
2396 nodeSet.addTupleSet(idxNodeTupleSet);
2400 private void analyzeCreateObjectNode(MethodDescriptor md, SymbolTable nametable,
2401 CreateObjectNode en) {
2402 // TODO Auto-generated method stub
2406 private void analyzeFlowOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on,
2407 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
2409 NodeTupleSet leftOpSet = new NodeTupleSet();
2410 NodeTupleSet rightOpSet = new NodeTupleSet();
2413 analyzeFlowExpressionNode(md, nametable, on.getLeft(), leftOpSet, null, implicitFlowTupleSet,
2416 if (on.getRight() != null) {
2418 analyzeFlowExpressionNode(md, nametable, on.getRight(), rightOpSet, null,
2419 implicitFlowTupleSet, false);
2422 Operation op = on.getOp();
2424 switch (op.getOp()) {
2426 case Operation.UNARYPLUS:
2427 case Operation.UNARYMINUS:
2428 case Operation.LOGIC_NOT:
2430 nodeSet.addTupleSet(leftOpSet);
2433 case Operation.LOGIC_OR:
2434 case Operation.LOGIC_AND:
2435 case Operation.COMP:
2436 case Operation.BIT_OR:
2437 case Operation.BIT_XOR:
2438 case Operation.BIT_AND:
2439 case Operation.ISAVAILABLE:
2440 case Operation.EQUAL:
2441 case Operation.NOTEQUAL:
2448 case Operation.MULT:
2451 case Operation.LEFTSHIFT:
2452 case Operation.RIGHTSHIFT:
2453 case Operation.URIGHTSHIFT:
2455 // there are two operands
2456 nodeSet.addTupleSet(leftOpSet);
2457 nodeSet.addTupleSet(rightOpSet);
2461 throw new Error(op.toString());
2466 private NTuple<Descriptor> analyzeFlowNameNode(MethodDescriptor md, SymbolTable nametable,
2467 NameNode nn, NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
2470 base = new NTuple<Descriptor>();
2473 NameDescriptor nd = nn.getName();
2475 if (nd.getBase() != null) {
2477 analyzeFlowExpressionNode(md, nametable, nn.getExpression(), nodeSet, base,
2478 implicitFlowTupleSet, false);
2480 // base node has the top location
2484 String varname = nd.toString();
2485 if (varname.equals("this")) {
2487 base.add(md.getThis());
2491 Descriptor d = (Descriptor) nametable.get(varname);
2493 if (d instanceof VarDescriptor) {
2494 VarDescriptor vd = (VarDescriptor) d;
2496 } else if (d instanceof FieldDescriptor) {
2497 // the type of field descriptor has a location!
2498 FieldDescriptor fd = (FieldDescriptor) d;
2499 if (fd.isStatic()) {
2501 // if it is 'static final', no need to have flow node for the TOP
2505 // if 'static', assign the default GLOBAL LOCATION to the first
2506 // element of the tuple
2507 base.add(GLOBALDESC);
2510 // the location of field access starts from this, followed by field
2512 base.add(md.getThis());
2516 } else if (d == null) {
2517 // access static field
2518 base.add(GLOBALDESC);
2519 // base.add(nn.getField());
2522 // FieldDescriptor fd = nn.getField();addFlowGraphEdge
2524 // MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
2525 // String globalLocId = localLattice.getGlobalLoc();
2526 // if (globalLocId == null) {
2528 // Error("Method lattice does not define global variable location at "
2529 // + generateErrorMessage(md.getClassDesc(), nn));
2531 // loc.addLocation(new Location(md, globalLocId));
2533 // Location fieldLoc = (Location) fd.getType().getExtension();
2534 // loc.addLocation(fieldLoc);
2540 getFlowGraph(md).createNewFlowNode(base);
2546 private NTuple<Descriptor> analyzeFlowFieldAccessNode(MethodDescriptor md, SymbolTable nametable,
2547 FieldAccessNode fan, NodeTupleSet nodeSet, NTuple<Descriptor> base,
2548 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
2550 ExpressionNode left = fan.getExpression();
2551 TypeDescriptor ltd = left.getType();
2552 FieldDescriptor fd = fan.getField();
2554 String varName = null;
2555 if (left.kind() == Kind.NameNode) {
2556 NameDescriptor nd = ((NameNode) left).getName();
2557 varName = nd.toString();
2560 if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) {
2561 // using a class name directly or access using this
2562 if (fd.isStatic() && fd.isFinal()) {
2567 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
2568 if (left instanceof ArrayAccessNode) {
2570 ArrayAccessNode aan = (ArrayAccessNode) left;
2571 left = aan.getExpression();
2572 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, base,
2573 implicitFlowTupleSet, isLHS);
2574 nodeSet.addTupleSet(idxNodeTupleSet);
2577 analyzeFlowExpressionNode(md, nametable, left, nodeSet, base, implicitFlowTupleSet, isLHS);
2580 // in this case, field is TOP location
2584 NTuple<Descriptor> flowFieldTuple = new NTuple<Descriptor>(base.toList());
2586 if (!left.getType().isPrimitive()) {
2588 if (!fd.getSymbol().equals("length")) {
2589 // array.length access, just have the location of the array
2590 flowFieldTuple.add(fd);
2591 nodeSet.removeTuple(base);
2595 getFlowGraph(md).createNewFlowNode(flowFieldTuple);
2598 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
2599 NTuple<Descriptor> idxTuple = idxIter.next();
2600 getFlowGraph(md).addValueFlowEdge(idxTuple, flowFieldTuple);
2604 return flowFieldTuple;
2610 private void debug_printTreeNode(TreeNode tn) {
2612 System.out.println("DEBUG: " + tn.printNode(0) + " line#=" + tn.getNumLine());
2616 private void analyzeFlowAssignmentNode(MethodDescriptor md, SymbolTable nametable,
2617 AssignmentNode an, NodeTupleSet nodeSet, NTuple<Descriptor> base,
2618 NodeTupleSet implicitFlowTupleSet) {
2620 NodeTupleSet nodeSetRHS = new NodeTupleSet();
2621 NodeTupleSet nodeSetLHS = new NodeTupleSet();
2623 boolean postinc = true;
2624 if (an.getOperation().getBaseOp() == null
2625 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
2626 .getBaseOp().getOp() != Operation.POSTDEC)) {
2629 // if LHS is array access node, need to capture value flows between an array
2630 // and its index value
2631 analyzeFlowExpressionNode(md, nametable, an.getDest(), nodeSetLHS, null, implicitFlowTupleSet,
2635 // analyze value flows of rhs expression
2636 analyzeFlowExpressionNode(md, nametable, an.getSrc(), nodeSetRHS, null, implicitFlowTupleSet,
2639 // System.out.println("-analyzeFlowAssignmentNode=" + an.printNode(0));
2640 // System.out.println("-nodeSetLHS=" + nodeSetLHS);
2641 // System.out.println("-nodeSetRHS=" + nodeSetRHS);
2642 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
2643 // System.out.println("-");
2645 if (an.getOperation().getOp() >= 2 && an.getOperation().getOp() <= 12) {
2646 // if assignment contains OP+EQ operator, creates edges from LHS to LHS
2647 for (Iterator<NTuple<Descriptor>> iter = nodeSetLHS.iterator(); iter.hasNext();) {
2648 NTuple<Descriptor> fromTuple = iter.next();
2649 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2650 NTuple<Descriptor> toTuple = iter2.next();
2651 addFlowGraphEdge(md, fromTuple, toTuple);
2656 // creates edges from RHS to LHS
2657 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
2658 NTuple<Descriptor> fromTuple = iter.next();
2659 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2660 NTuple<Descriptor> toTuple = iter2.next();
2661 addFlowGraphEdge(md, fromTuple, toTuple);
2665 // creates edges from implicitFlowTupleSet to LHS
2666 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
2667 NTuple<Descriptor> fromTuple = iter.next();
2668 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2669 NTuple<Descriptor> toTuple = iter2.next();
2670 addFlowGraphEdge(md, fromTuple, toTuple);
2676 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2677 NTuple<Descriptor> tuple = iter2.next();
2678 addFlowGraphEdge(md, tuple, tuple);
2681 // creates edges from implicitFlowTupleSet to LHS
2682 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
2683 NTuple<Descriptor> fromTuple = iter.next();
2684 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2685 NTuple<Descriptor> toTuple = iter2.next();
2686 addFlowGraphEdge(md, fromTuple, toTuple);
2692 if (nodeSet != null) {
2693 nodeSet.addTupleSet(nodeSetLHS);
2697 public FlowGraph getFlowGraph(MethodDescriptor md) {
2698 return mapMethodDescriptorToFlowGraph.get(md);
2701 private boolean addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
2702 NTuple<Descriptor> to) {
2704 // return true if it adds a new edge
2705 FlowGraph graph = getFlowGraph(md);
2706 graph.addValueFlowEdge(from, to);
2710 public void _debug_printGraph() {
2711 Set<MethodDescriptor> keySet = mapMethodDescriptorToFlowGraph.keySet();
2713 for (Iterator<MethodDescriptor> iterator = keySet.iterator(); iterator.hasNext();) {
2714 MethodDescriptor md = (MethodDescriptor) iterator.next();
2715 FlowGraph fg = mapMethodDescriptorToFlowGraph.get(md);
2718 } catch (IOException e) {
2719 e.printStackTrace();
2727 class CyclicFlowException extends Exception {