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()) + "\")";
324 rtr += "\n@THISLOC(\"this\")";
325 rtr += "\n@GLOBALLOC(\"GLOBALLOC\")";
327 CompositeLocation pcLoc = methodLocInfo.getPCLoc();
329 rtr += "\n@PCLOC(\"" + generateLocationAnnoatation(pcLoc) + "\")";
337 private void generateAnnoatedCode() {
339 readOriginalSourceFiles();
342 while (!toAnalyzeIsEmpty()) {
343 ClassDescriptor cd = toAnalyzeNext();
345 setupToAnalazeMethod(cd);
347 LocationInfo locInfo = mapClassToLocationInfo.get(cd);
348 String sourceFileName = cd.getSourceFileName();
350 if (cd.isInterface()) {
354 int classDefLine = mapDescToDefinitionLine.get(cd);
355 Vector<String> sourceVec = mapFileNameToLineVector.get(sourceFileName);
357 if (locInfo == null) {
358 locInfo = getLocationInfo(cd);
361 for (Iterator iter = cd.getFields(); iter.hasNext();) {
362 FieldDescriptor fieldDesc = (FieldDescriptor) iter.next();
363 if (!(fieldDesc.isStatic() && fieldDesc.isFinal())) {
364 String locIdentifier = locInfo.getFieldInferLocation(fieldDesc).getLocIdentifier();
365 if (!getLattice(cd).getElementSet().contains(locIdentifier)) {
366 getLattice(cd).put(locIdentifier);
371 String fieldLatticeDefStr = generateLatticeDefinition(cd);
372 String annoatedSrc = fieldLatticeDefStr + newline + sourceVec.get(classDefLine);
373 sourceVec.set(classDefLine, annoatedSrc);
375 // generate annotations for field declarations
376 LocationInfo fieldLocInfo = getLocationInfo(cd);
377 Map<Descriptor, CompositeLocation> inferLocMap = fieldLocInfo.getMapDescToInferLocation();
379 for (Iterator iter = cd.getFields(); iter.hasNext();) {
380 FieldDescriptor fd = (FieldDescriptor) iter.next();
382 String locAnnotationStr;
383 CompositeLocation inferLoc = inferLocMap.get(fd);
385 if (inferLoc != null) {
386 // infer loc is null if the corresponding field is static and final
387 locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
388 int fdLineNum = fd.getLineNum();
389 String orgFieldDeclarationStr = sourceVec.get(fdLineNum);
390 String fieldDeclaration = fd.toString();
391 fieldDeclaration = fieldDeclaration.substring(0, fieldDeclaration.length() - 1);
392 String annoatedStr = locAnnotationStr + " " + orgFieldDeclarationStr;
393 sourceVec.set(fdLineNum, annoatedStr);
398 while (!toAnalyzeMethodIsEmpty()) {
399 MethodDescriptor md = toAnalyzeMethodNext();
401 if (!ssjava.needTobeAnnotated(md)) {
405 SSJavaLattice<String> methodLattice = md2lattice.get(md);
406 if (methodLattice != null) {
408 int methodDefLine = md.getLineNum();
410 MethodLocationInfo methodLocInfo = getMethodLocationInfo(md);
412 Map<Descriptor, CompositeLocation> methodInferLocMap =
413 methodLocInfo.getMapDescToInferLocation();
414 Set<Descriptor> localVarDescSet = methodInferLocMap.keySet();
416 Set<String> localLocElementSet = methodLattice.getElementSet();
418 for (Iterator iterator = localVarDescSet.iterator(); iterator.hasNext();) {
419 Descriptor localVarDesc = (Descriptor) iterator.next();
420 CompositeLocation inferLoc = methodInferLocMap.get(localVarDesc);
422 String localLocIdentifier = inferLoc.get(0).getLocIdentifier();
423 if (!localLocElementSet.contains(localLocIdentifier)) {
424 methodLattice.put(localLocIdentifier);
427 String locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
429 if (!isParameter(md, localVarDesc)) {
430 if (mapDescToDefinitionLine.containsKey(localVarDesc)) {
431 int varLineNum = mapDescToDefinitionLine.get(localVarDesc);
432 String orgSourceLine = sourceVec.get(varLineNum);
434 orgSourceLine.indexOf(generateVarDeclaration((VarDescriptor) localVarDesc));
437 orgSourceLine.substring(0, idx) + locAnnotationStr + " "
438 + orgSourceLine.substring(idx);
439 sourceVec.set(varLineNum, annoatedStr);
442 String methodDefStr = sourceVec.get(methodDefLine);
445 getParamLocation(methodDefStr,
446 generateVarDeclaration((VarDescriptor) localVarDesc));
451 methodDefStr.substring(0, idx) + locAnnotationStr + " "
452 + methodDefStr.substring(idx);
453 sourceVec.set(methodDefLine, annoatedStr);
458 // check if the lattice has to have the location type for the this
461 // boolean needToAddthisRef = hasThisReference(md);
462 if (localLocElementSet.contains("this")) {
463 methodLattice.put("this");
466 String methodLatticeDefStr = generateLatticeDefinition(md);
467 String annoatedStr = methodLatticeDefStr + newline + sourceVec.get(methodDefLine);
468 sourceVec.set(methodDefLine, annoatedStr);
478 private boolean hasThisReference(MethodDescriptor md) {
480 FlowGraph fg = getFlowGraph(md);
481 Set<FlowNode> nodeSet = fg.getNodeSet();
482 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
483 FlowNode flowNode = (FlowNode) iterator.next();
484 if (flowNode.getDescTuple().get(0).equals(md.getThis())) {
492 private int getParamLocation(String methodStr, String paramStr) {
494 String pattern = paramStr + ",";
496 int idx = methodStr.indexOf(pattern);
500 pattern = paramStr + ")";
501 return methodStr.indexOf(pattern);
506 private String generateVarDeclaration(VarDescriptor varDesc) {
508 TypeDescriptor td = varDesc.getType();
509 String rtr = td.toString();
511 for (int i = 0; i < td.getArrayCount(); i++) {
515 rtr += " " + varDesc.getName();
520 private String generateLocationAnnoatation(CompositeLocation loc) {
523 Location methodLoc = loc.get(0);
524 rtr += methodLoc.getLocIdentifier();
526 for (int i = 1; i < loc.getSize(); i++) {
527 Location element = loc.get(i);
528 rtr += "," + element.getDescriptor().getSymbol() + "." + element.getLocIdentifier();
534 private boolean isParameter(MethodDescriptor md, Descriptor localVarDesc) {
535 return getFlowGraph(md).isParamDesc(localVarDesc);
538 private String extractFileName(String fileName) {
539 int idx = fileName.lastIndexOf("/");
543 return fileName.substring(idx + 1);
548 private void codeGen() {
550 Set<String> originalFileNameSet = mapFileNameToLineVector.keySet();
551 for (Iterator iterator = originalFileNameSet.iterator(); iterator.hasNext();) {
552 String orgFileName = (String) iterator.next();
553 String outputFileName = extractFileName(orgFileName);
555 Vector<String> sourceVec = mapFileNameToLineVector.get(orgFileName);
559 FileWriter fileWriter = new FileWriter("./infer/" + outputFileName);
560 BufferedWriter out = new BufferedWriter(fileWriter);
562 for (int i = 0; i < sourceVec.size(); i++) {
563 out.write(sourceVec.get(i));
567 } catch (IOException e) {
575 private void simplifyLattices() {
577 // generate lattice dot file
580 while (!toAnalyzeIsEmpty()) {
581 ClassDescriptor cd = toAnalyzeNext();
583 setupToAnalazeMethod(cd);
585 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
586 if (classLattice != null) {
587 classLattice.removeRedundantEdges();
590 while (!toAnalyzeMethodIsEmpty()) {
591 MethodDescriptor md = toAnalyzeMethodNext();
592 SSJavaLattice<String> methodLattice = md2lattice.get(md);
593 if (methodLattice != null) {
594 methodLattice.removeRedundantEdges();
601 private void checkLattices() {
603 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
605 // current descriptors to visit in fixed-point interprocedural analysis,
607 // dependency in the call graph
608 methodDescriptorsToVisitStack.clear();
610 // descriptorListToAnalyze.removeFirst();
612 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
613 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
615 while (!descriptorListToAnalyze.isEmpty()) {
616 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
617 checkLatticesOfVirtualMethods(md);
622 private void debug_writeLatticeDotFile() {
623 // generate lattice dot file
627 while (!toAnalyzeIsEmpty()) {
628 ClassDescriptor cd = toAnalyzeNext();
630 setupToAnalazeMethod(cd);
632 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
633 if (classLattice != null) {
634 ssjava.writeLatticeDotFile(cd, null, classLattice);
635 debug_printDescriptorToLocNameMapping(cd);
638 while (!toAnalyzeMethodIsEmpty()) {
639 MethodDescriptor md = toAnalyzeMethodNext();
640 SSJavaLattice<String> methodLattice = md2lattice.get(md);
641 if (methodLattice != null) {
642 ssjava.writeLatticeDotFile(cd, md, methodLattice);
643 debug_printDescriptorToLocNameMapping(md);
650 private void debug_printDescriptorToLocNameMapping(Descriptor desc) {
652 LocationInfo info = getLocationInfo(desc);
653 System.out.println("## " + desc + " ##");
654 System.out.println(info.getMapDescToInferLocation());
655 LocationInfo locInfo = getLocationInfo(desc);
656 System.out.println("mapping=" + locInfo.getMapLocSymbolToDescSet());
657 System.out.println("###################");
661 private void inferLattices() {
663 // do fixed-point analysis
666 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
668 // Collections.sort(descriptorListToAnalyze, new
669 // Comparator<MethodDescriptor>() {
670 // public int compare(MethodDescriptor o1, MethodDescriptor o2) {
671 // return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
675 // current descriptors to visit in fixed-point interprocedural analysis,
677 // dependency in the call graph
678 methodDescriptorsToVisitStack.clear();
680 // descriptorListToAnalyze.removeFirst();
682 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
683 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
685 while (!descriptorListToAnalyze.isEmpty()) {
686 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
687 methodDescriptorsToVisitStack.add(md);
690 // analyze scheduled methods until there are no more to visit
691 while (!methodDescriptorsToVisitStack.isEmpty()) {
692 // start to analyze leaf node
693 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
695 SSJavaLattice<String> methodLattice =
696 new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
698 MethodLocationInfo methodInfo = new MethodLocationInfo(md);
699 curMethodInfo = methodInfo;
701 System.out.println();
702 System.out.println("SSJAVA: Inferencing the lattice from " + md);
705 analyzeMethodLattice(md, methodLattice, methodInfo);
706 } catch (CyclicFlowException e) {
707 throw new Error("Fail to generate the method lattice for " + md);
710 SSJavaLattice<String> prevMethodLattice = getMethodLattice(md);
711 MethodLocationInfo prevMethodInfo = getMethodLocationInfo(md);
713 if ((!methodLattice.equals(prevMethodLattice)) || (!methodInfo.equals(prevMethodInfo))) {
715 setMethodLattice(md, methodLattice);
716 setMethodLocInfo(md, methodInfo);
718 // results for callee changed, so enqueue dependents caller for
720 Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
721 while (depsItr.hasNext()) {
722 MethodDescriptor methodNext = depsItr.next();
723 if (!methodDescriptorsToVisitStack.contains(methodNext)
724 && methodDescriptorToVistSet.contains(methodNext)) {
725 methodDescriptorsToVisitStack.add(methodNext);
733 descriptorListToAnalyze = ssjava.getSortedDescriptors();
734 for (Iterator iterator = descriptorListToAnalyze.iterator(); iterator.hasNext();) {
735 MethodDescriptor md = (MethodDescriptor) iterator.next();
736 calculateExtraLocations(md);
741 private void setMethodLocInfo(MethodDescriptor md, MethodLocationInfo methodInfo) {
742 mapMethodDescToMethodLocationInfo.put(md, methodInfo);
745 private void checkLatticesOfVirtualMethods(MethodDescriptor md) {
747 if (!md.isStatic()) {
748 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
749 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
751 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
752 MethodDescriptor mdCallee = (MethodDescriptor) iterator.next();
753 if (!md.equals(mdCallee)) {
754 checkConsistency(md, mdCallee);
762 private void checkConsistency(MethodDescriptor md1, MethodDescriptor md2) {
764 // check that two lattice have the same relations between parameters(+PC
765 // LOC, GLOBAL_LOC RETURN LOC)
767 List<CompositeLocation> list1 = new ArrayList<CompositeLocation>();
768 List<CompositeLocation> list2 = new ArrayList<CompositeLocation>();
770 MethodLocationInfo locInfo1 = getMethodLocationInfo(md1);
771 MethodLocationInfo locInfo2 = getMethodLocationInfo(md2);
773 Map<Integer, CompositeLocation> paramMap1 = locInfo1.getMapParamIdxToInferLoc();
774 Map<Integer, CompositeLocation> paramMap2 = locInfo2.getMapParamIdxToInferLoc();
776 int numParam = locInfo1.getMapParamIdxToInferLoc().keySet().size();
778 // add location types of paramters
779 for (int idx = 0; idx < numParam; idx++) {
780 list1.add(paramMap1.get(Integer.valueOf(idx)));
781 list2.add(paramMap2.get(Integer.valueOf(idx)));
784 // add program counter location
785 list1.add(locInfo1.getPCLoc());
786 list2.add(locInfo2.getPCLoc());
788 if (!md1.getReturnType().isVoid()) {
789 // add return value location
790 CompositeLocation rtrLoc1 = getMethodLocationInfo(md1).getReturnLoc();
791 CompositeLocation rtrLoc2 = getMethodLocationInfo(md2).getReturnLoc();
796 // add global location type
797 if (md1.isStatic()) {
798 CompositeLocation globalLoc1 =
799 new CompositeLocation(new Location(md1, locInfo1.getGlobalLocName()));
800 CompositeLocation globalLoc2 =
801 new CompositeLocation(new Location(md2, locInfo2.getGlobalLocName()));
802 list1.add(globalLoc1);
803 list2.add(globalLoc2);
806 for (int i = 0; i < list1.size(); i++) {
807 CompositeLocation locA1 = list1.get(i);
808 CompositeLocation locA2 = list2.get(i);
809 for (int k = 0; k < list1.size(); k++) {
811 CompositeLocation locB1 = list1.get(k);
812 CompositeLocation locB2 = list2.get(k);
813 boolean r1 = isGreaterThan(getLattice(md1), locA1, locB1);
815 boolean r2 = isGreaterThan(getLattice(md1), locA2, locB2);
818 throw new Error("The method " + md1 + " is not consistent with the method " + md2
819 + ".:: They have a different ordering relation between locations (" + locA1 + ","
820 + locB1 + ") and (" + locA2 + "," + locB2 + ").");
828 private String getSymbol(int idx, FlowNode node) {
829 Descriptor desc = node.getDescTuple().get(idx);
830 return desc.getSymbol();
833 private Descriptor getDescriptor(int idx, FlowNode node) {
834 Descriptor desc = node.getDescTuple().get(idx);
838 private void analyzeMethodLattice(MethodDescriptor md, SSJavaLattice<String> methodLattice,
839 MethodLocationInfo methodInfo) throws CyclicFlowException {
841 // first take a look at method invocation nodes to newly added relations
843 analyzeLatticeMethodInvocationNode(md, methodLattice, methodInfo);
845 if (!md.isStatic()) {
846 // set the this location
847 String thisLocSymbol = md.getThis().getSymbol();
848 methodInfo.setThisLocName(thisLocSymbol);
851 // set the global location
852 methodInfo.setGlobalLocName(LocationInference.GLOBALLOC);
853 methodInfo.mapDescriptorToLocation(GLOBALDESC, new CompositeLocation(
854 new Location(md, GLOBALLOC)));
856 // visit each node of method flow graph
857 FlowGraph fg = getFlowGraph(md);
858 Set<FlowNode> nodeSet = fg.getNodeSet();
860 // for the method lattice, we need to look at the first element of
861 // NTuple<Descriptor>
862 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
863 FlowNode srcNode = (FlowNode) iterator.next();
865 Set<FlowEdge> outEdgeSet = srcNode.getOutEdgeSet();
866 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
867 FlowEdge outEdge = (FlowEdge) iterator2.next();
868 FlowNode dstNode = outEdge.getDst();
870 NTuple<Descriptor> srcNodeTuple = srcNode.getDescTuple();
871 NTuple<Descriptor> dstNodeTuple = dstNode.getDescTuple();
873 if (outEdge.getInitTuple().equals(srcNodeTuple)
874 && outEdge.getEndTuple().equals(dstNodeTuple)) {
876 if ((srcNodeTuple.size() > 1 && dstNodeTuple.size() > 1)
877 && srcNodeTuple.get(0).equals(dstNodeTuple.get(0))) {
879 // value flows between fields
880 Descriptor desc = srcNodeTuple.get(0);
881 ClassDescriptor classDesc;
883 if (desc.equals(GLOBALDESC)) {
884 classDesc = md.getClassDesc();
886 VarDescriptor varDesc = (VarDescriptor) srcNodeTuple.get(0);
887 classDesc = varDesc.getType().getClassDesc();
889 extractRelationFromFieldFlows(classDesc, srcNode, dstNode, 1);
892 // value flow between local var - local var or local var - field
893 addRelationToLattice(md, methodLattice, methodInfo, srcNode, dstNode);
899 // create mapping from param idx to inferred composite location
902 if (!md.isStatic()) {
903 // add 'this' reference location
905 methodInfo.addMapParamIdxToInferLoc(0, methodInfo.getInferLocation(md.getThis()));
910 for (int idx = 0; idx < md.numParameters(); idx++) {
911 Descriptor paramDesc = md.getParameter(idx);
912 CompositeLocation inferParamLoc = methodInfo.getInferLocation(paramDesc);
913 methodInfo.addMapParamIdxToInferLoc(idx + offset, inferParamLoc);
918 private void calculateExtraLocations(MethodDescriptor md) {
919 // calcualte pcloc, returnloc,...
921 SSJavaLattice<String> methodLattice = getMethodLattice(md);
922 MethodLocationInfo methodInfo = getMethodLocationInfo(md);
923 FlowGraph fg = getFlowGraph(md);
924 Set<FlowNode> nodeSet = fg.getNodeSet();
926 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
927 FlowNode flowNode = (FlowNode) iterator.next();
928 if (flowNode.isDeclaratonNode()) {
929 CompositeLocation inferLoc = methodInfo.getInferLocation(flowNode.getDescTuple().get(0));
930 String locIdentifier = inferLoc.get(0).getLocIdentifier();
931 if (!methodLattice.containsKey(locIdentifier)) {
932 methodLattice.put(locIdentifier);
938 Map<Integer, CompositeLocation> mapParamToLoc = methodInfo.getMapParamIdxToInferLoc();
939 Set<Integer> paramIdxSet = mapParamToLoc.keySet();
942 if (!ssjava.getMethodContainingSSJavaLoop().equals(md)) {
943 // calculate the initial program counter location
944 // PC location is higher than location types of all parameters
945 String pcLocSymbol = "PCLOC";
947 Set<CompositeLocation> paramInFlowSet = new HashSet<CompositeLocation>();
949 for (Iterator iterator = paramIdxSet.iterator(); iterator.hasNext();) {
950 Integer paramIdx = (Integer) iterator.next();
952 FlowNode paramFlowNode = fg.getParamFlowNode(paramIdx);
954 if (fg.getIncomingFlowNodeSet(paramFlowNode).size() > 0) {
955 // parameter has in-value flows
956 CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
957 paramInFlowSet.add(inferLoc);
961 if (paramInFlowSet.size() > 0) {
962 CompositeLocation lowestLoc = getLowest(methodLattice, paramInFlowSet);
963 assert (lowestLoc != null);
964 methodInfo.setPCLoc(lowestLoc);
969 // calculate a return location
970 // the return location type is lower than all parameters and location
973 if (!md.getReturnType().isVoid()) {
974 // first, generate the set of return value location types that starts
978 Set<CompositeLocation> inferFieldReturnLocSet = new HashSet<CompositeLocation>();
980 Set<FlowNode> paramFlowNode = getParamNodeFlowingToReturnValue(md);
981 Set<CompositeLocation> inferParamLocSet = new HashSet<CompositeLocation>();
982 if (paramFlowNode != null) {
983 for (Iterator iterator = paramFlowNode.iterator(); iterator.hasNext();) {
984 FlowNode fn = (FlowNode) iterator.next();
985 CompositeLocation inferLoc =
986 generateInferredCompositeLocation(methodInfo, getFlowGraph(md).getLocationTuple(fn));
987 inferParamLocSet.add(inferLoc);
991 Set<FlowNode> returnNodeSet = fg.getReturnNodeSet();
993 skip: for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
994 FlowNode returnNode = (FlowNode) iterator.next();
995 CompositeLocation inferReturnLoc =
996 generateInferredCompositeLocation(methodInfo, fg.getLocationTuple(returnNode));
997 if (inferReturnLoc.get(0).getLocIdentifier().equals("this")) {
998 // if the location type of the return value matches "this" reference
999 // then, check whether this return value is equal to/lower than all
1001 // parameters that possibly flow into the return values
1002 for (Iterator iterator2 = inferParamLocSet.iterator(); iterator2.hasNext();) {
1003 CompositeLocation paramInferLoc = (CompositeLocation) iterator2.next();
1005 if ((!paramInferLoc.equals(inferReturnLoc))
1006 && !isGreaterThan(methodLattice, paramInferLoc, inferReturnLoc)) {
1010 inferFieldReturnLocSet.add(inferReturnLoc);
1015 if (inferFieldReturnLocSet.size() > 0) {
1017 CompositeLocation returnLoc = getLowest(methodLattice, inferFieldReturnLocSet);
1018 if (returnLoc == null) {
1019 // in this case, assign <'this',bottom> to the RETURNLOC
1020 returnLoc = new CompositeLocation(new Location(md, md.getThis().getSymbol()));
1021 returnLoc.addLocation(new Location(md.getClassDesc(), getLattice(md.getClassDesc())
1024 methodInfo.setReturnLoc(returnLoc);
1027 String returnLocSymbol = "RETURNLOC";
1028 CompositeLocation returnLocInferLoc =
1029 new CompositeLocation(new Location(md, returnLocSymbol));
1030 methodInfo.setReturnLoc(returnLocInferLoc);
1032 for (Iterator iterator = paramIdxSet.iterator(); iterator.hasNext();) {
1033 Integer paramIdx = (Integer) iterator.next();
1034 CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
1035 String paramLocLocalSymbol = inferLoc.get(0).getLocIdentifier();
1036 if (!methodLattice.isGreaterThan(paramLocLocalSymbol, returnLocSymbol)) {
1037 addRelationHigherToLower(methodLattice, methodInfo, paramLocLocalSymbol,
1042 for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
1043 FlowNode returnNode = (FlowNode) iterator.next();
1044 CompositeLocation inferLoc =
1045 generateInferredCompositeLocation(methodInfo, fg.getLocationTuple(returnNode));
1046 if (!isGreaterThan(methodLattice, inferLoc, returnLocInferLoc)) {
1047 addRelation(methodLattice, methodInfo, inferLoc, returnLocInferLoc);
1054 } catch (CyclicFlowException e) {
1055 e.printStackTrace();
1060 private Set<String> getHigherLocSymbolThan(SSJavaLattice<String> lattice, String loc) {
1061 Set<String> higherLocSet = new HashSet<String>();
1063 Set<String> locSet = lattice.getTable().keySet();
1064 for (Iterator iterator = locSet.iterator(); iterator.hasNext();) {
1065 String element = (String) iterator.next();
1066 if (lattice.isGreaterThan(element, loc) && (!element.equals(lattice.getTopItem()))) {
1067 higherLocSet.add(element);
1070 return higherLocSet;
1073 private CompositeLocation getLowest(SSJavaLattice<String> methodLattice,
1074 Set<CompositeLocation> set) {
1076 CompositeLocation lowest = set.iterator().next();
1078 if (set.size() == 1) {
1082 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
1083 CompositeLocation loc = (CompositeLocation) iterator.next();
1085 if ((!loc.equals(lowest)) && (!isComparable(methodLattice, lowest, loc))) {
1086 // if there is a case where composite locations are incomparable, just
1091 if ((!loc.equals(lowest)) && isGreaterThan(methodLattice, lowest, loc)) {
1098 private boolean isComparable(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
1099 CompositeLocation comp2) {
1101 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
1103 for (int idx = 0; idx < size; idx++) {
1104 Location loc1 = comp1.get(idx);
1105 Location loc2 = comp2.get(idx);
1107 Descriptor desc1 = loc1.getDescriptor();
1108 Descriptor desc2 = loc2.getDescriptor();
1110 if (!desc1.equals(desc2)) {
1111 throw new Error("Fail to compare " + comp1 + " and " + comp2);
1114 String symbol1 = loc1.getLocIdentifier();
1115 String symbol2 = loc2.getLocIdentifier();
1117 SSJavaLattice<String> lattice;
1119 lattice = methodLattice;
1121 lattice = getLattice(desc1);
1124 if (symbol1.equals(symbol2)) {
1126 } else if (!lattice.isComparable(symbol1, symbol2)) {
1135 private boolean isGreaterThan(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
1136 CompositeLocation comp2) {
1138 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
1140 for (int idx = 0; idx < size; idx++) {
1141 Location loc1 = comp1.get(idx);
1142 Location loc2 = comp2.get(idx);
1144 Descriptor desc1 = loc1.getDescriptor();
1145 Descriptor desc2 = loc2.getDescriptor();
1147 if (!desc1.equals(desc2)) {
1148 throw new Error("Fail to compare " + comp1 + " and " + comp2);
1151 String symbol1 = loc1.getLocIdentifier();
1152 String symbol2 = loc2.getLocIdentifier();
1154 SSJavaLattice<String> lattice;
1156 lattice = methodLattice;
1158 lattice = getLattice(desc1);
1161 if (symbol1.equals(symbol2)) {
1163 } else if (lattice.isGreaterThan(symbol1, symbol2)) {
1174 private void recursiveAddRelationToLattice(int idx, MethodDescriptor md,
1175 CompositeLocation srcInferLoc, CompositeLocation dstInferLoc) throws CyclicFlowException {
1177 String srcLocSymbol = srcInferLoc.get(idx).getLocIdentifier();
1178 String dstLocSymbol = dstInferLoc.get(idx).getLocIdentifier();
1180 if (srcLocSymbol.equals(dstLocSymbol)) {
1181 recursiveAddRelationToLattice(idx + 1, md, srcInferLoc, dstInferLoc);
1184 Descriptor parentDesc = srcInferLoc.get(idx).getDescriptor();
1185 LocationInfo locInfo = getLocationInfo(parentDesc);
1187 addRelationHigherToLower(getLattice(parentDesc), getLocationInfo(parentDesc), srcLocSymbol,
1193 private void analyzeLatticeMethodInvocationNode(MethodDescriptor mdCaller,
1194 SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo)
1195 throws CyclicFlowException {
1197 // the transformation for a call site propagates all relations between
1198 // parameters from the callee
1199 // if the method is virtual, it also grab all relations from any possible
1202 Set<MethodInvokeNode> setMethodInvokeNode =
1203 mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
1205 if (setMethodInvokeNode != null) {
1207 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
1208 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
1209 MethodDescriptor mdCallee = min.getMethod();
1210 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1211 if (mdCallee.isStatic()) {
1212 setPossibleCallees.add(mdCallee);
1214 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
1215 // removes method descriptors that are not invoked by the caller
1216 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
1217 setPossibleCallees.addAll(calleeSet);
1220 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
1221 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
1222 propagateRelationToCaller(min, mdCaller, possibleMdCallee, methodLattice, methodInfo);
1230 private void propagateRelationToCaller(MethodInvokeNode min, MethodDescriptor mdCaller,
1231 MethodDescriptor possibleMdCallee, SSJavaLattice<String> methodLattice,
1232 MethodLocationInfo methodInfo) throws CyclicFlowException {
1234 SSJavaLattice<String> calleeLattice = getMethodLattice(possibleMdCallee);
1235 MethodLocationInfo calleeLocInfo = getMethodLocationInfo(possibleMdCallee);
1236 FlowGraph calleeFlowGraph = getFlowGraph(possibleMdCallee);
1238 int numParam = calleeLocInfo.getNumParam();
1239 for (int i = 0; i < numParam; i++) {
1240 CompositeLocation param1 = calleeLocInfo.getParamCompositeLocation(i);
1241 for (int k = 0; k < numParam; k++) {
1243 CompositeLocation param2 = calleeLocInfo.getParamCompositeLocation(k);
1245 if (isGreaterThan(getLattice(possibleMdCallee), param1, param2)) {
1246 NodeTupleSet argDescTupleSet1 = getNodeTupleSetByArgIdx(min, i);
1247 NodeTupleSet argDescTupleSet2 = getNodeTupleSetByArgIdx(min, k);
1249 // the callee has the relation in which param1 is higher than param2
1250 // therefore, the caller has to have the relation in which arg1 is
1253 for (Iterator<NTuple<Descriptor>> iterator = argDescTupleSet1.iterator(); iterator
1255 NTuple<Descriptor> argDescTuple1 = iterator.next();
1257 for (Iterator<NTuple<Descriptor>> iterator2 = argDescTupleSet2.iterator(); iterator2
1259 NTuple<Descriptor> argDescTuple2 = iterator2.next();
1261 // retreive inferred location by the local var descriptor
1263 NTuple<Location> tuple1 = getFlowGraph(mdCaller).getLocationTuple(argDescTuple1);
1264 NTuple<Location> tuple2 = getFlowGraph(mdCaller).getLocationTuple(argDescTuple2);
1266 // CompositeLocation higherInferLoc =
1267 // methodInfo.getInferLocation(argTuple1.get(0));
1268 // CompositeLocation lowerInferLoc =
1269 // methodInfo.getInferLocation(argTuple2.get(0));
1271 CompositeLocation inferLoc1 = generateInferredCompositeLocation(methodInfo, tuple1);
1272 CompositeLocation inferLoc2 = generateInferredCompositeLocation(methodInfo, tuple2);
1274 // addRelation(methodLattice, methodInfo, inferLoc1, inferLoc2);
1276 addFlowGraphEdge(mdCaller, argDescTuple1, argDescTuple2);
1289 private CompositeLocation generateInferredCompositeLocation(MethodLocationInfo methodInfo,
1290 NTuple<Location> tuple) {
1292 // first, retrieve inferred location by the local var descriptor
1293 CompositeLocation inferLoc = new CompositeLocation();
1295 CompositeLocation localVarInferLoc =
1296 methodInfo.getInferLocation(tuple.get(0).getLocDescriptor());
1298 localVarInferLoc.get(0).setLocDescriptor(tuple.get(0).getLocDescriptor());
1300 for (int i = 0; i < localVarInferLoc.getSize(); i++) {
1301 inferLoc.addLocation(localVarInferLoc.get(i));
1304 for (int i = 1; i < tuple.size(); i++) {
1305 Location cur = tuple.get(i);
1306 Descriptor enclosingDesc = cur.getDescriptor();
1307 Descriptor curDesc = cur.getLocDescriptor();
1309 Location inferLocElement;
1310 if (curDesc == null) {
1311 // in this case, we have a newly generated location.
1312 inferLocElement = new Location(enclosingDesc, cur.getLocIdentifier());
1314 String fieldLocSymbol =
1315 getLocationInfo(enclosingDesc).getInferLocation(curDesc).get(0).getLocIdentifier();
1316 inferLocElement = new Location(enclosingDesc, fieldLocSymbol);
1317 inferLocElement.setLocDescriptor(curDesc);
1320 inferLoc.addLocation(inferLocElement);
1324 assert (inferLoc.get(0).getLocDescriptor().getSymbol() == inferLoc.get(0).getLocIdentifier());
1328 private void addRelation(SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo,
1329 CompositeLocation srcInferLoc, CompositeLocation dstInferLoc) throws CyclicFlowException {
1331 System.out.println("addRelation --- srcInferLoc=" + srcInferLoc + " dstInferLoc="
1333 String srcLocalLocSymbol = srcInferLoc.get(0).getLocIdentifier();
1334 String dstLocalLocSymbol = dstInferLoc.get(0).getLocIdentifier();
1336 if (srcInferLoc.getSize() == 1 && dstInferLoc.getSize() == 1) {
1337 // add a new relation to the local lattice
1338 addRelationHigherToLower(methodLattice, methodInfo, srcLocalLocSymbol, dstLocalLocSymbol);
1339 } else if (srcInferLoc.getSize() > 1 && dstInferLoc.getSize() > 1) {
1340 // both src and dst have assigned to a composite location
1342 if (!srcLocalLocSymbol.equals(dstLocalLocSymbol)) {
1343 addRelationHigherToLower(methodLattice, methodInfo, srcLocalLocSymbol, dstLocalLocSymbol);
1345 recursivelyAddRelation(1, srcInferLoc, dstInferLoc);
1348 // either src or dst has assigned to a composite location
1349 if (!srcLocalLocSymbol.equals(dstLocalLocSymbol)) {
1350 addRelationHigherToLower(methodLattice, methodInfo, srcLocalLocSymbol, dstLocalLocSymbol);
1354 System.out.println();
1358 public LocationInfo getLocationInfo(Descriptor d) {
1359 if (d instanceof MethodDescriptor) {
1360 return getMethodLocationInfo((MethodDescriptor) d);
1362 return getFieldLocationInfo((ClassDescriptor) d);
1366 private MethodLocationInfo getMethodLocationInfo(MethodDescriptor md) {
1368 if (!mapMethodDescToMethodLocationInfo.containsKey(md)) {
1369 mapMethodDescToMethodLocationInfo.put(md, new MethodLocationInfo(md));
1372 return mapMethodDescToMethodLocationInfo.get(md);
1376 private LocationInfo getFieldLocationInfo(ClassDescriptor cd) {
1378 if (!mapClassToLocationInfo.containsKey(cd)) {
1379 mapClassToLocationInfo.put(cd, new LocationInfo(cd));
1382 return mapClassToLocationInfo.get(cd);
1386 private void addRelationToLattice(MethodDescriptor md, SSJavaLattice<String> methodLattice,
1387 MethodLocationInfo methodInfo, FlowNode srcNode, FlowNode dstNode) throws CyclicFlowException {
1389 System.out.println();
1390 System.out.println("### addRelationToLattice src=" + srcNode + " dst=" + dstNode);
1392 // add a new binary relation of dstNode < srcNode
1393 FlowGraph flowGraph = getFlowGraph(md);
1395 System.out.println("***** src composite case::");
1396 calculateCompositeLocation(flowGraph, methodLattice, methodInfo, srcNode);
1398 CompositeLocation srcInferLoc =
1399 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(srcNode));
1400 CompositeLocation dstInferLoc =
1401 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(dstNode));
1402 addRelation(methodLattice, methodInfo, srcInferLoc, dstInferLoc);
1403 } catch (CyclicFlowException e) {
1404 // there is a cyclic value flow... try to calculate a composite location
1405 // for the destination node
1406 System.out.println("***** dst composite case::");
1407 calculateCompositeLocation(flowGraph, methodLattice, methodInfo, dstNode);
1408 CompositeLocation srcInferLoc =
1409 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(srcNode));
1410 CompositeLocation dstInferLoc =
1411 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(dstNode));
1413 addRelation(methodLattice, methodInfo, srcInferLoc, dstInferLoc);
1414 } catch (CyclicFlowException e1) {
1415 throw new Error("Failed to merge cyclic value flows into a shared location.");
1421 private void recursivelyAddRelation(int idx, CompositeLocation srcInferLoc,
1422 CompositeLocation dstInferLoc) throws CyclicFlowException {
1424 String srcLocSymbol = srcInferLoc.get(idx).getLocIdentifier();
1425 String dstLocSymbol = dstInferLoc.get(idx).getLocIdentifier();
1427 Descriptor parentDesc = srcInferLoc.get(idx).getDescriptor();
1429 if (srcLocSymbol.equals(dstLocSymbol)) {
1430 // check if it is the case of shared location
1431 if (srcInferLoc.getSize() == (idx + 1) && dstInferLoc.getSize() == (idx + 1)) {
1432 Location inferLocElement = srcInferLoc.get(idx);
1433 System.out.println("SET SHARED LOCATION=" + inferLocElement);
1434 getLattice(inferLocElement.getDescriptor())
1435 .addSharedLoc(inferLocElement.getLocIdentifier());
1436 } else if (srcInferLoc.getSize() > (idx + 1) && dstInferLoc.getSize() > (idx + 1)) {
1437 recursivelyAddRelation(idx + 1, srcInferLoc, dstInferLoc);
1440 addRelationHigherToLower(getLattice(parentDesc), getLocationInfo(parentDesc), srcLocSymbol,
1445 private void recursivelyAddCompositeRelation(MethodDescriptor md, FlowGraph flowGraph,
1446 MethodLocationInfo methodInfo, FlowNode srcNode, FlowNode dstNode, Descriptor srcDesc,
1447 Descriptor dstDesc) throws CyclicFlowException {
1449 CompositeLocation inferSrcLoc;
1450 CompositeLocation inferDstLoc = methodInfo.getInferLocation(dstDesc);
1452 if (srcNode.getDescTuple().size() > 1) {
1454 inferSrcLoc = new CompositeLocation();
1456 NTuple<Location> locTuple = flowGraph.getLocationTuple(srcNode);
1457 for (int i = 0; i < locTuple.size(); i++) {
1458 inferSrcLoc.addLocation(locTuple.get(i));
1462 inferSrcLoc = methodInfo.getInferLocation(srcDesc);
1465 if (dstNode.getDescTuple().size() > 1) {
1467 inferDstLoc = new CompositeLocation();
1469 NTuple<Location> locTuple = flowGraph.getLocationTuple(dstNode);
1470 for (int i = 0; i < locTuple.size(); i++) {
1471 inferDstLoc.addLocation(locTuple.get(i));
1475 inferDstLoc = methodInfo.getInferLocation(dstDesc);
1478 recursiveAddRelationToLattice(1, md, inferSrcLoc, inferDstLoc);
1481 private void addPrefixMapping(Map<NTuple<Location>, Set<NTuple<Location>>> map,
1482 NTuple<Location> prefix, NTuple<Location> element) {
1484 if (!map.containsKey(prefix)) {
1485 map.put(prefix, new HashSet<NTuple<Location>>());
1487 map.get(prefix).add(element);
1490 private boolean calculateCompositeLocation(FlowGraph flowGraph,
1491 SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo, FlowNode flowNode)
1492 throws CyclicFlowException {
1494 Descriptor localVarDesc = flowNode.getDescTuple().get(0);
1495 NTuple<Location> flowNodelocTuple = flowGraph.getLocationTuple(flowNode);
1497 if (localVarDesc.equals(methodInfo.getMethodDesc())) {
1501 Set<FlowNode> inNodeSet = flowGraph.getIncomingFlowNodeSet(flowNode);
1502 Set<FlowNode> reachableNodeSet = flowGraph.getReachableFlowNodeSet(flowNode);
1504 Map<NTuple<Location>, Set<NTuple<Location>>> mapPrefixToIncomingLocTupleSet =
1505 new HashMap<NTuple<Location>, Set<NTuple<Location>>>();
1507 List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
1509 for (Iterator iterator = inNodeSet.iterator(); iterator.hasNext();) {
1510 FlowNode inNode = (FlowNode) iterator.next();
1511 NTuple<Location> inNodeTuple = flowGraph.getLocationTuple(inNode);
1513 CompositeLocation inNodeInferredLoc =
1514 generateInferredCompositeLocation(methodInfo, inNodeTuple);
1516 NTuple<Location> inNodeInferredLocTuple = inNodeInferredLoc.getTuple();
1518 for (int i = 1; i < inNodeInferredLocTuple.size(); i++) {
1519 NTuple<Location> prefix = inNodeInferredLocTuple.subList(0, i);
1520 if (!prefixList.contains(prefix)) {
1521 prefixList.add(prefix);
1523 addPrefixMapping(mapPrefixToIncomingLocTupleSet, prefix, inNodeInferredLocTuple);
1527 Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
1528 public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
1529 int s0 = arg0.size();
1530 int s1 = arg1.size();
1533 } else if (s0 == s1) {
1541 System.out.println("prefixList=" + prefixList);
1542 System.out.println("reachableNodeSet=" + reachableNodeSet);
1544 // find out reachable nodes that have the longest common prefix
1545 for (int i = 0; i < prefixList.size(); i++) {
1546 NTuple<Location> curPrefix = prefixList.get(i);
1547 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
1549 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1550 FlowNode reachableNode = (FlowNode) iterator2.next();
1551 NTuple<Location> reachLocTuple = flowGraph.getLocationTuple(reachableNode);
1552 CompositeLocation reachLocInferLoc =
1553 generateInferredCompositeLocation(methodInfo, reachLocTuple);
1554 if (reachLocInferLoc.getTuple().startsWith(curPrefix)) {
1555 reachableCommonPrefixSet.add(reachLocTuple);
1559 if (!reachableCommonPrefixSet.isEmpty()) {
1560 // found reachable nodes that start with the prefix curPrefix
1561 // need to assign a composite location
1563 // first, check if there are more than one the set of locations that has
1564 // the same length of the longest reachable prefix, no way to assign
1565 // a composite location to the input local var
1566 prefixSanityCheck(prefixList, i, flowGraph, reachableNodeSet);
1568 Set<NTuple<Location>> incomingCommonPrefixSet =
1569 mapPrefixToIncomingLocTupleSet.get(curPrefix);
1571 int idx = curPrefix.size();
1572 NTuple<Location> element = incomingCommonPrefixSet.iterator().next();
1573 Descriptor desc = element.get(idx).getDescriptor();
1575 SSJavaLattice<String> lattice = getLattice(desc);
1576 LocationInfo locInfo = getLocationInfo(desc);
1578 CompositeLocation inferLocation =
1579 generateInferredCompositeLocation(methodInfo, flowNodelocTuple);
1581 // methodInfo.getInferLocation(localVarDesc);
1582 CompositeLocation newInferLocation = new CompositeLocation();
1584 if (inferLocation.getTuple().startsWith(curPrefix)) {
1585 // the same infer location is already existed. no need to do
1587 System.out.println("NO ATTEMPT TO MAKE A COMPOSITE LOCATION curPrefix=" + curPrefix);
1590 // assign a new composite location
1592 // String oldMethodLocationSymbol =
1593 // inferLocation.get(0).getLocIdentifier();
1594 String newLocSymbol = "Loc" + (SSJavaLattice.seed++);
1595 for (int locIdx = 0; locIdx < curPrefix.size(); locIdx++) {
1596 newInferLocation.addLocation(curPrefix.get(locIdx));
1598 Location newLocationElement = new Location(desc, newLocSymbol);
1599 newInferLocation.addLocation(newLocationElement);
1601 // maps local variable to location types of the common prefix
1602 methodInfo.mapDescriptorToLocation(localVarDesc, newInferLocation.clone());
1604 // methodInfo.mapDescriptorToLocation(localVarDesc, newInferLocation);
1605 addMapLocSymbolToInferredLocation(methodInfo.getMethodDesc(), localVarDesc,
1607 methodInfo.removeMaplocalVarToLocSet(localVarDesc);
1609 // add the field/var descriptor to the set of the location symbol
1610 int lastIdx = flowNode.getDescTuple().size() - 1;
1611 Descriptor lastFlowNodeDesc = flowNode.getDescTuple().get(lastIdx);
1612 Descriptor enclosinglastLastFlowNodeDesc = flowNodelocTuple.get(lastIdx).getDescriptor();
1614 CompositeLocation newlyInferredLocForFlowNode =
1615 generateInferredCompositeLocation(methodInfo, flowNodelocTuple);
1616 Location lastInferLocElement =
1617 newlyInferredLocForFlowNode.get(newlyInferredLocForFlowNode.getSize() - 1);
1618 Descriptor enclosingLastInferLocElement = lastInferLocElement.getDescriptor();
1620 // getLocationInfo(enclosingLastInferLocElement).addMapLocSymbolToDescSet(
1621 // lastInferLocElement.getLocIdentifier(), lastFlowNodeDesc);
1622 getLocationInfo(enclosingLastInferLocElement).addMapLocSymbolToRelatedInferLoc(
1623 lastInferLocElement.getLocIdentifier(), enclosinglastLastFlowNodeDesc,
1626 // clean up the previous location
1627 // Location prevInferLocElement =
1628 // inferLocation.get(inferLocation.getSize() - 1);
1629 // Descriptor prevEnclosingDesc = prevInferLocElement.getDescriptor();
1631 // SSJavaLattice<String> targetLattice;
1632 // LocationInfo targetInfo;
1633 // if (prevEnclosingDesc.equals(methodInfo.getMethodDesc())) {
1634 // targetLattice = methodLattice;
1635 // targetInfo = methodInfo;
1637 // targetLattice = getLattice(prevInferLocElement.getDescriptor());
1638 // targetInfo = getLocationInfo(prevInferLocElement.getDescriptor());
1641 // Set<Pair<Descriptor, Descriptor>> associstedDescSet =
1642 // targetInfo.getRelatedInferLocSet(prevInferLocElement.getLocIdentifier());
1644 // if (associstedDescSet.size() == 1) {
1645 // targetLattice.remove(prevInferLocElement.getLocIdentifier());
1647 // associstedDescSet.remove(lastFlowNodeDesc);
1652 System.out.println("curPrefix=" + curPrefix);
1653 System.out.println("ASSIGN NEW COMPOSITE LOCATION =" + newInferLocation + " to "
1656 String newlyInsertedLocName =
1657 newInferLocation.get(newInferLocation.getSize() - 1).getLocIdentifier();
1659 System.out.println("-- add in-flow");
1660 for (Iterator iterator = incomingCommonPrefixSet.iterator(); iterator.hasNext();) {
1661 NTuple<Location> tuple = (NTuple<Location>) iterator.next();
1662 Location loc = tuple.get(idx);
1663 String higher = loc.getLocIdentifier();
1664 addRelationHigherToLower(lattice, locInfo, higher, newlyInsertedLocName);
1667 System.out.println("-- add out flow");
1668 for (Iterator iterator = reachableCommonPrefixSet.iterator(); iterator.hasNext();) {
1669 NTuple<Location> tuple = (NTuple<Location>) iterator.next();
1670 if (tuple.size() > idx) {
1671 Location loc = tuple.get(idx);
1672 String lower = loc.getLocIdentifier();
1674 // locInfo.getFieldInferLocation(loc.getLocDescriptor()).getLocIdentifier();
1675 addRelationHigherToLower(lattice, locInfo, newlyInsertedLocName, lower);
1688 private void addMapLocSymbolToInferredLocation(MethodDescriptor md, Descriptor localVar,
1689 CompositeLocation inferLoc) {
1691 Location locElement = inferLoc.get((inferLoc.getSize() - 1));
1692 Descriptor enclosingDesc = locElement.getDescriptor();
1693 LocationInfo locInfo = getLocationInfo(enclosingDesc);
1694 locInfo.addMapLocSymbolToRelatedInferLoc(locElement.getLocIdentifier(), md, localVar);
1697 private boolean isCompositeLocation(CompositeLocation cl) {
1698 return cl.getSize() > 1;
1701 private boolean containsNonPrimitiveElement(Set<Descriptor> descSet) {
1702 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
1703 Descriptor desc = (Descriptor) iterator.next();
1705 if (desc.equals(LocationInference.GLOBALDESC)) {
1707 } else if (desc instanceof VarDescriptor) {
1708 if (!((VarDescriptor) desc).getType().isPrimitive()) {
1711 } else if (desc instanceof FieldDescriptor) {
1712 if (!((FieldDescriptor) desc).getType().isPrimitive()) {
1721 private void addRelationHigherToLower(SSJavaLattice<String> lattice, LocationInfo locInfo,
1722 String higher, String lower) throws CyclicFlowException {
1724 System.out.println("---addRelationHigherToLower " + higher + " -> " + lower
1725 + " to the lattice of " + locInfo.getDescIdentifier());
1726 // if (higher.equals(lower) && lattice.isSharedLoc(higher)) {
1729 Set<String> cycleElementSet = lattice.getPossibleCycleElements(higher, lower);
1731 boolean hasNonPrimitiveElement = false;
1732 for (Iterator iterator = cycleElementSet.iterator(); iterator.hasNext();) {
1733 String cycleElementLocSymbol = (String) iterator.next();
1735 Set<Descriptor> descSet = locInfo.getDescSet(cycleElementLocSymbol);
1736 if (containsNonPrimitiveElement(descSet)) {
1737 hasNonPrimitiveElement = true;
1742 if (hasNonPrimitiveElement) {
1743 System.out.println("#Check cycle= " + lower + " < " + higher + " cycleElementSet="
1745 // if there is non-primitive element in the cycle, no way to merge cyclic
1746 // elements into the shared location
1747 throw new CyclicFlowException();
1750 if (cycleElementSet.size() > 0) {
1752 String newSharedLoc = "SharedLoc" + (SSJavaLattice.seed++);
1754 System.out.println("---ASSIGN NEW SHARED LOC=" + newSharedLoc + " to " + cycleElementSet);
1755 lattice.mergeIntoSharedLocation(cycleElementSet, newSharedLoc);
1757 for (Iterator iterator = cycleElementSet.iterator(); iterator.hasNext();) {
1758 String oldLocSymbol = (String) iterator.next();
1760 Set<Pair<Descriptor, Descriptor>> inferLocSet = locInfo.getRelatedInferLocSet(oldLocSymbol);
1761 System.out.println("---update related locations=" + inferLocSet);
1762 for (Iterator iterator2 = inferLocSet.iterator(); iterator2.hasNext();) {
1763 Pair<Descriptor, Descriptor> pair = (Pair<Descriptor, Descriptor>) iterator2.next();
1764 Descriptor enclosingDesc = pair.getFirst();
1765 Descriptor desc = pair.getSecond();
1767 CompositeLocation inferLoc;
1768 if (curMethodInfo.md.equals(enclosingDesc)) {
1769 inferLoc = curMethodInfo.getInferLocation(desc);
1771 inferLoc = getLocationInfo(enclosingDesc).getInferLocation(desc);
1774 Location locElement = inferLoc.get(inferLoc.getSize() - 1);
1776 locElement.setLocIdentifier(newSharedLoc);
1777 locInfo.addMapLocSymbolToRelatedInferLoc(newSharedLoc, enclosingDesc, desc);
1779 if (curMethodInfo.md.equals(enclosingDesc)) {
1780 inferLoc = curMethodInfo.getInferLocation(desc);
1782 inferLoc = getLocationInfo(enclosingDesc).getInferLocation(desc);
1784 System.out.println("---New Infer Loc=" + inferLoc);
1787 locInfo.removeRelatedInferLocSet(oldLocSymbol, newSharedLoc);
1791 lattice.addSharedLoc(newSharedLoc);
1793 } else if (!lattice.isGreaterThan(higher, lower)) {
1794 lattice.addRelationHigherToLower(higher, lower);
1798 private void replaceOldLocWithNewLoc(SSJavaLattice<String> methodLattice, String oldLocSymbol,
1799 String newLocSymbol) {
1801 if (methodLattice.containsKey(oldLocSymbol)) {
1802 methodLattice.substituteLocation(oldLocSymbol, newLocSymbol);
1807 private void prefixSanityCheck(List<NTuple<Location>> prefixList, int curIdx,
1808 FlowGraph flowGraph, Set<FlowNode> reachableNodeSet) {
1810 NTuple<Location> curPrefix = prefixList.get(curIdx);
1812 for (int i = curIdx + 1; i < prefixList.size(); i++) {
1813 NTuple<Location> prefixTuple = prefixList.get(i);
1815 if (curPrefix.startsWith(prefixTuple)) {
1819 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1820 FlowNode reachableNode = (FlowNode) iterator2.next();
1821 NTuple<Location> reachLocTuple = flowGraph.getLocationTuple(reachableNode);
1822 if (reachLocTuple.startsWith(prefixTuple)) {
1824 throw new Error("Failed to generate a composite location");
1830 public boolean isPrimitiveLocalVariable(FlowNode node) {
1831 VarDescriptor varDesc = (VarDescriptor) node.getDescTuple().get(0);
1832 return varDesc.getType().isPrimitive();
1835 private SSJavaLattice<String> getLattice(Descriptor d) {
1836 if (d instanceof MethodDescriptor) {
1837 return getMethodLattice((MethodDescriptor) d);
1839 return getFieldLattice((ClassDescriptor) d);
1843 private SSJavaLattice<String> getMethodLattice(MethodDescriptor md) {
1844 if (!md2lattice.containsKey(md)) {
1845 md2lattice.put(md, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
1847 return md2lattice.get(md);
1850 private void setMethodLattice(MethodDescriptor md, SSJavaLattice<String> lattice) {
1851 md2lattice.put(md, lattice);
1854 private void extractRelationFromFieldFlows(ClassDescriptor cd, FlowNode srcNode,
1855 FlowNode dstNode, int idx) throws CyclicFlowException {
1857 if (srcNode.getDescTuple().get(idx).equals(dstNode.getDescTuple().get(idx))
1858 && srcNode.getDescTuple().size() > (idx + 1) && dstNode.getDescTuple().size() > (idx + 1)) {
1859 // value flow between fields: we don't need to add a binary relation
1862 Descriptor desc = srcNode.getDescTuple().get(idx);
1863 ClassDescriptor classDesc;
1866 classDesc = ((VarDescriptor) desc).getType().getClassDesc();
1868 classDesc = ((FieldDescriptor) desc).getType().getClassDesc();
1871 extractRelationFromFieldFlows(classDesc, srcNode, dstNode, idx + 1);
1875 Descriptor srcFieldDesc = srcNode.getDescTuple().get(idx);
1876 Descriptor dstFieldDesc = dstNode.getDescTuple().get(idx);
1878 // add a new binary relation of dstNode < srcNode
1879 SSJavaLattice<String> fieldLattice = getFieldLattice(cd);
1880 LocationInfo fieldInfo = getFieldLocationInfo(cd);
1882 String srcSymbol = fieldInfo.getFieldInferLocation(srcFieldDesc).getLocIdentifier();
1883 String dstSymbol = fieldInfo.getFieldInferLocation(dstFieldDesc).getLocIdentifier();
1885 addRelationHigherToLower(fieldLattice, fieldInfo, srcSymbol, dstSymbol);
1891 public SSJavaLattice<String> getFieldLattice(ClassDescriptor cd) {
1892 if (!cd2lattice.containsKey(cd)) {
1893 cd2lattice.put(cd, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
1895 return cd2lattice.get(cd);
1898 public LinkedList<MethodDescriptor> computeMethodList() {
1900 Set<MethodDescriptor> toSort = new HashSet<MethodDescriptor>();
1904 Set<MethodDescriptor> visited = new HashSet<MethodDescriptor>();
1905 Set<MethodDescriptor> reachableCallee = new HashSet<MethodDescriptor>();
1907 while (!toAnalyzeIsEmpty()) {
1908 ClassDescriptor cd = toAnalyzeNext();
1910 setupToAnalazeMethod(cd);
1911 toanalyzeMethodList.removeAll(visited);
1913 while (!toAnalyzeMethodIsEmpty()) {
1914 MethodDescriptor md = toAnalyzeMethodNext();
1915 if ((!visited.contains(md))
1916 && (ssjava.needTobeAnnotated(md) || reachableCallee.contains(md))) {
1918 // creates a mapping from a method descriptor to virtual methods
1919 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1920 if (md.isStatic()) {
1921 setPossibleCallees.add(md);
1923 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
1926 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getCalleeSet(md);
1927 Set<MethodDescriptor> needToAnalyzeCalleeSet = new HashSet<MethodDescriptor>();
1929 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
1930 MethodDescriptor calleemd = (MethodDescriptor) iterator.next();
1931 if ((!ssjava.isTrustMethod(calleemd))
1932 && (!ssjava.isSSJavaUtil(calleemd.getClassDesc()))) {
1933 if (!visited.contains(calleemd)) {
1934 toanalyzeMethodList.add(calleemd);
1936 reachableCallee.add(calleemd);
1937 needToAnalyzeCalleeSet.add(calleemd);
1941 mapMethodToCalleeSet.put(md, needToAnalyzeCalleeSet);
1950 return ssjava.topologicalSort(toSort);
1954 public void constructFlowGraph() {
1956 LinkedList<MethodDescriptor> methodDescList = computeMethodList();
1958 while (!methodDescList.isEmpty()) {
1959 MethodDescriptor md = methodDescList.removeLast();
1960 if (state.SSJAVADEBUG) {
1961 System.out.println();
1962 System.out.println("SSJAVA: Constructing a flow graph: " + md);
1964 // creates a mapping from a parameter descriptor to its index
1965 Map<Descriptor, Integer> mapParamDescToIdx = new HashMap<Descriptor, Integer>();
1967 if (!md.isStatic()) {
1969 mapParamDescToIdx.put(md.getThis(), 0);
1972 for (int i = 0; i < md.numParameters(); i++) {
1973 Descriptor paramDesc = (Descriptor) md.getParameter(i);
1974 mapParamDescToIdx.put(paramDesc, new Integer(i + offset));
1977 FlowGraph fg = new FlowGraph(md, mapParamDescToIdx);
1978 mapMethodDescriptorToFlowGraph.put(md, fg);
1980 analyzeMethodBody(md.getClassDesc(), md);
1983 _debug_printGraph();
1987 private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md) {
1988 BlockNode bn = state.getMethodBody(md);
1989 NodeTupleSet implicitFlowTupleSet = new NodeTupleSet();
1990 analyzeFlowBlockNode(md, md.getParameterTable(), bn, implicitFlowTupleSet);
1993 private void analyzeFlowBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn,
1994 NodeTupleSet implicitFlowTupleSet) {
1996 bn.getVarTable().setParent(nametable);
1997 for (int i = 0; i < bn.size(); i++) {
1998 BlockStatementNode bsn = bn.get(i);
1999 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
2004 private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
2005 BlockStatementNode bsn, NodeTupleSet implicitFlowTupleSet) {
2007 switch (bsn.kind()) {
2008 case Kind.BlockExpressionNode:
2009 analyzeBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn, implicitFlowTupleSet);
2012 case Kind.DeclarationNode:
2013 analyzeFlowDeclarationNode(md, nametable, (DeclarationNode) bsn, implicitFlowTupleSet);
2016 case Kind.IfStatementNode:
2017 analyzeFlowIfStatementNode(md, nametable, (IfStatementNode) bsn, implicitFlowTupleSet);
2021 analyzeFlowLoopNode(md, nametable, (LoopNode) bsn, implicitFlowTupleSet);
2024 case Kind.ReturnNode:
2025 analyzeFlowReturnNode(md, nametable, (ReturnNode) bsn, implicitFlowTupleSet);
2028 case Kind.SubBlockNode:
2029 analyzeFlowSubBlockNode(md, nametable, (SubBlockNode) bsn, implicitFlowTupleSet);
2032 case Kind.ContinueBreakNode:
2035 case Kind.SwitchStatementNode:
2036 analyzeSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn);
2043 private void analyzeSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
2044 SwitchStatementNode bsn) {
2045 // TODO Auto-generated method stub
2048 private void analyzeFlowSubBlockNode(MethodDescriptor md, SymbolTable nametable,
2049 SubBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
2050 analyzeFlowBlockNode(md, nametable, sbn.getBlockNode(), implicitFlowTupleSet);
2053 private void analyzeFlowReturnNode(MethodDescriptor md, SymbolTable nametable, ReturnNode rn,
2054 NodeTupleSet implicitFlowTupleSet) {
2056 ExpressionNode returnExp = rn.getReturnExpression();
2058 if (returnExp != null) {
2059 NodeTupleSet nodeSet = new NodeTupleSet();
2060 analyzeFlowExpressionNode(md, nametable, returnExp, nodeSet, false);
2062 FlowGraph fg = getFlowGraph(md);
2064 // annotate the elements of the node set as the return location
2065 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
2066 NTuple<Descriptor> returnDescTuple = (NTuple<Descriptor>) iterator.next();
2067 fg.addReturnFlowNode(returnDescTuple);
2068 for (Iterator iterator2 = implicitFlowTupleSet.iterator(); iterator2.hasNext();) {
2069 NTuple<Descriptor> implicitFlowDescTuple = (NTuple<Descriptor>) iterator2.next();
2070 fg.addValueFlowEdge(implicitFlowDescTuple, returnDescTuple);
2077 private void analyzeFlowLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln,
2078 NodeTupleSet implicitFlowTupleSet) {
2080 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
2082 NodeTupleSet condTupleNode = new NodeTupleSet();
2083 analyzeFlowExpressionNode(md, nametable, ln.getCondition(), condTupleNode, null,
2084 implicitFlowTupleSet, false);
2085 condTupleNode.addTupleSet(implicitFlowTupleSet);
2087 // add edges from condNodeTupleSet to all nodes of conditional nodes
2088 analyzeFlowBlockNode(md, nametable, ln.getBody(), condTupleNode);
2091 // check 'for loop' case
2092 BlockNode bn = ln.getInitializer();
2093 bn.getVarTable().setParent(nametable);
2094 for (int i = 0; i < bn.size(); i++) {
2095 BlockStatementNode bsn = bn.get(i);
2096 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
2099 NodeTupleSet condTupleNode = new NodeTupleSet();
2100 analyzeFlowExpressionNode(md, bn.getVarTable(), ln.getCondition(), condTupleNode, null,
2101 implicitFlowTupleSet, false);
2102 condTupleNode.addTupleSet(implicitFlowTupleSet);
2104 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(), condTupleNode);
2105 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(), condTupleNode);
2111 private void analyzeFlowIfStatementNode(MethodDescriptor md, SymbolTable nametable,
2112 IfStatementNode isn, NodeTupleSet implicitFlowTupleSet) {
2114 NodeTupleSet condTupleNode = new NodeTupleSet();
2115 analyzeFlowExpressionNode(md, nametable, isn.getCondition(), condTupleNode, null,
2116 implicitFlowTupleSet, false);
2118 // add edges from condNodeTupleSet to all nodes of conditional nodes
2119 condTupleNode.addTupleSet(implicitFlowTupleSet);
2120 analyzeFlowBlockNode(md, nametable, isn.getTrueBlock(), condTupleNode);
2122 if (isn.getFalseBlock() != null) {
2123 analyzeFlowBlockNode(md, nametable, isn.getFalseBlock(), condTupleNode);
2128 private void analyzeFlowDeclarationNode(MethodDescriptor md, SymbolTable nametable,
2129 DeclarationNode dn, NodeTupleSet implicitFlowTupleSet) {
2131 VarDescriptor vd = dn.getVarDescriptor();
2132 mapDescToDefinitionLine.put(vd, dn.getNumLine());
2133 NTuple<Descriptor> tupleLHS = new NTuple<Descriptor>();
2135 FlowNode fn = getFlowGraph(md).createNewFlowNode(tupleLHS);
2136 fn.setDeclarationNode();
2138 if (dn.getExpression() != null) {
2140 NodeTupleSet tupleSetRHS = new NodeTupleSet();
2141 analyzeFlowExpressionNode(md, nametable, dn.getExpression(), tupleSetRHS, null,
2142 implicitFlowTupleSet, false);
2144 // add a new flow edge from rhs to lhs
2145 for (Iterator<NTuple<Descriptor>> iter = tupleSetRHS.iterator(); iter.hasNext();) {
2146 NTuple<Descriptor> from = iter.next();
2147 addFlowGraphEdge(md, from, tupleLHS);
2154 private void analyzeBlockExpressionNode(MethodDescriptor md, SymbolTable nametable,
2155 BlockExpressionNode ben, NodeTupleSet implicitFlowTupleSet) {
2156 analyzeFlowExpressionNode(md, nametable, ben.getExpression(), null, null, implicitFlowTupleSet,
2160 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
2161 ExpressionNode en, NodeTupleSet nodeSet, boolean isLHS) {
2162 return analyzeFlowExpressionNode(md, nametable, en, nodeSet, null, new NodeTupleSet(), isLHS);
2165 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
2166 ExpressionNode en, NodeTupleSet nodeSet, NTuple<Descriptor> base,
2167 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
2169 // note that expression node can create more than one flow node
2170 // nodeSet contains of flow nodes
2171 // base is always assigned to null except the case of a name node!
2173 NTuple<Descriptor> flowTuple;
2175 switch (en.kind()) {
2177 case Kind.AssignmentNode:
2178 analyzeFlowAssignmentNode(md, nametable, (AssignmentNode) en, nodeSet, base,
2179 implicitFlowTupleSet);
2182 case Kind.FieldAccessNode:
2184 analyzeFlowFieldAccessNode(md, nametable, (FieldAccessNode) en, nodeSet, base,
2185 implicitFlowTupleSet, isLHS);
2186 if (flowTuple != null) {
2187 nodeSet.addTuple(flowTuple);
2192 NodeTupleSet nameNodeSet = new NodeTupleSet();
2194 analyzeFlowNameNode(md, nametable, (NameNode) en, nameNodeSet, base, implicitFlowTupleSet);
2195 if (flowTuple != null) {
2196 nodeSet.addTuple(flowTuple);
2201 analyzeFlowOpNode(md, nametable, (OpNode) en, nodeSet, implicitFlowTupleSet);
2204 case Kind.CreateObjectNode:
2205 analyzeCreateObjectNode(md, nametable, (CreateObjectNode) en);
2208 case Kind.ArrayAccessNode:
2209 analyzeFlowArrayAccessNode(md, nametable, (ArrayAccessNode) en, nodeSet, isLHS);
2212 case Kind.LiteralNode:
2213 analyzeLiteralNode(md, nametable, (LiteralNode) en);
2216 case Kind.MethodInvokeNode:
2217 analyzeFlowMethodInvokeNode(md, nametable, (MethodInvokeNode) en, nodeSet,
2218 implicitFlowTupleSet);
2221 case Kind.TertiaryNode:
2222 analyzeFlowTertiaryNode(md, nametable, (TertiaryNode) en, nodeSet, implicitFlowTupleSet);
2226 analyzeFlowCastNode(md, nametable, (CastNode) en, nodeSet, base, implicitFlowTupleSet);
2228 // case Kind.InstanceOfNode:
2229 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
2232 // case Kind.ArrayInitializerNode:
2233 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
2237 // case Kind.ClassTypeNode:
2238 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
2241 // case Kind.OffsetNode:
2242 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
2250 private void analyzeFlowCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn,
2251 NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
2253 analyzeFlowExpressionNode(md, nametable, cn.getExpression(), nodeSet, base,
2254 implicitFlowTupleSet, false);
2258 private void analyzeFlowTertiaryNode(MethodDescriptor md, SymbolTable nametable, TertiaryNode tn,
2259 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
2261 NodeTupleSet tertiaryTupleNode = new NodeTupleSet();
2262 analyzeFlowExpressionNode(md, nametable, tn.getCond(), tertiaryTupleNode, null,
2263 implicitFlowTupleSet, false);
2265 // add edges from tertiaryTupleNode to all nodes of conditional nodes
2266 tertiaryTupleNode.addTupleSet(implicitFlowTupleSet);
2267 analyzeFlowExpressionNode(md, nametable, tn.getTrueExpr(), tertiaryTupleNode, null,
2268 implicitFlowTupleSet, false);
2270 analyzeFlowExpressionNode(md, nametable, tn.getFalseExpr(), tertiaryTupleNode, null,
2271 implicitFlowTupleSet, false);
2273 nodeSet.addTupleSet(tertiaryTupleNode);
2277 private void addMapCallerMethodDescToMethodInvokeNodeSet(MethodDescriptor caller,
2278 MethodInvokeNode min) {
2279 Set<MethodInvokeNode> set = mapMethodDescriptorToMethodInvokeNodeSet.get(caller);
2281 set = new HashSet<MethodInvokeNode>();
2282 mapMethodDescriptorToMethodInvokeNodeSet.put(caller, set);
2287 private void addParamNodeFlowingToReturnValue(MethodDescriptor md, FlowNode fn) {
2289 if (!mapMethodDescToParamNodeFlowsToReturnValue.containsKey(md)) {
2290 mapMethodDescToParamNodeFlowsToReturnValue.put(md, new HashSet<FlowNode>());
2292 mapMethodDescToParamNodeFlowsToReturnValue.get(md).add(fn);
2295 private Set<FlowNode> getParamNodeFlowingToReturnValue(MethodDescriptor md) {
2296 return mapMethodDescToParamNodeFlowsToReturnValue.get(md);
2299 private void analyzeFlowMethodInvokeNode(MethodDescriptor md, SymbolTable nametable,
2300 MethodInvokeNode min, NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
2302 if (nodeSet == null) {
2303 nodeSet = new NodeTupleSet();
2306 addMapCallerMethodDescToMethodInvokeNodeSet(md, min);
2308 MethodDescriptor calleeMethodDesc = min.getMethod();
2310 NameDescriptor baseName = min.getBaseName();
2311 boolean isSystemout = false;
2312 if (baseName != null) {
2313 isSystemout = baseName.getSymbol().equals("System.out");
2316 if (!ssjava.isSSJavaUtil(calleeMethodDesc.getClassDesc())
2317 && !ssjava.isTrustMethod(calleeMethodDesc) && !isSystemout) {
2319 FlowGraph calleeFlowGraph = getFlowGraph(calleeMethodDesc);
2320 Set<FlowNode> calleeReturnSet = calleeFlowGraph.getReturnNodeSet();
2322 if (min.getExpression() != null) {
2324 NodeTupleSet baseNodeSet = new NodeTupleSet();
2325 analyzeFlowExpressionNode(md, nametable, min.getExpression(), baseNodeSet, null,
2326 implicitFlowTupleSet, false);
2328 if (!min.getMethod().isStatic()) {
2329 addArgIdxMap(min, 0, baseNodeSet);
2331 for (Iterator iterator = calleeReturnSet.iterator(); iterator.hasNext();) {
2332 FlowNode returnNode = (FlowNode) iterator.next();
2333 NTuple<Descriptor> returnDescTuple = returnNode.getDescTuple();
2334 if (returnDescTuple.startsWith(calleeMethodDesc.getThis())) {
2335 // the location type of the return value is started with 'this'
2337 for (Iterator<NTuple<Descriptor>> baseIter = baseNodeSet.iterator(); baseIter
2339 NTuple<Descriptor> baseTuple = baseIter.next();
2340 NTuple<Descriptor> inFlowTuple = new NTuple<Descriptor>(baseTuple.getList());
2341 inFlowTuple.addAll(returnDescTuple.subList(1, returnDescTuple.size()));
2342 nodeSet.addTuple(inFlowTuple);
2345 Set<FlowNode> inFlowSet = calleeFlowGraph.getIncomingFlowNodeSet(returnNode);
2346 for (Iterator iterator2 = inFlowSet.iterator(); iterator2.hasNext();) {
2347 FlowNode inFlowNode = (FlowNode) iterator2.next();
2348 if (inFlowNode.getDescTuple().startsWith(calleeMethodDesc.getThis())) {
2349 nodeSet.addTupleSet(baseNodeSet);
2357 // analyze parameter flows
2359 if (min.numArgs() > 0) {
2362 if (min.getMethod().isStatic()) {
2368 for (int i = 0; i < min.numArgs(); i++) {
2369 ExpressionNode en = min.getArg(i);
2370 int idx = i + offset;
2371 NodeTupleSet argTupleSet = new NodeTupleSet();
2372 analyzeFlowExpressionNode(md, nametable, en, argTupleSet, true);
2373 // if argument is liternal node, argTuple is set to NULL.
2374 addArgIdxMap(min, idx, argTupleSet);
2375 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
2376 if (hasInFlowTo(calleeFlowGraph, paramNode, calleeReturnSet)
2377 || calleeMethodDesc.getModifiers().isNative()) {
2378 addParamNodeFlowingToReturnValue(calleeMethodDesc, paramNode);
2379 nodeSet.addTupleSet(argTupleSet);
2389 private boolean hasInFlowTo(FlowGraph fg, FlowNode inNode, Set<FlowNode> nodeSet) {
2390 // return true if inNode has in-flows to nodeSet
2391 Set<FlowNode> reachableSet = fg.getReachableFlowNodeSet(inNode);
2392 for (Iterator iterator = reachableSet.iterator(); iterator.hasNext();) {
2393 FlowNode fn = (FlowNode) iterator.next();
2394 if (nodeSet.contains(fn)) {
2401 private NodeTupleSet getNodeTupleSetByArgIdx(MethodInvokeNode min, int idx) {
2402 return mapMethodInvokeNodeToArgIdxMap.get(min).get(new Integer(idx));
2405 private void addArgIdxMap(MethodInvokeNode min, int idx, NodeTupleSet tupleSet) {
2406 Map<Integer, NodeTupleSet> mapIdxToTupleSet = mapMethodInvokeNodeToArgIdxMap.get(min);
2407 if (mapIdxToTupleSet == null) {
2408 mapIdxToTupleSet = new HashMap<Integer, NodeTupleSet>();
2409 mapMethodInvokeNodeToArgIdxMap.put(min, mapIdxToTupleSet);
2411 mapIdxToTupleSet.put(new Integer(idx), tupleSet);
2414 private void analyzeFlowMethodParameters(MethodDescriptor callermd, SymbolTable nametable,
2415 MethodInvokeNode min, NodeTupleSet nodeSet) {
2417 if (min.numArgs() > 0) {
2420 if (min.getMethod().isStatic()) {
2424 // NTuple<Descriptor> thisArgTuple = new NTuple<Descriptor>();
2425 // thisArgTuple.add(callermd.getThis());
2426 // NodeTupleSet argTupleSet = new NodeTupleSet();
2427 // argTupleSet.addTuple(thisArgTuple);
2428 // addArgIdxMap(min, 0, argTupleSet);
2429 // nodeSet.addTuple(thisArgTuple);
2432 for (int i = 0; i < min.numArgs(); i++) {
2433 ExpressionNode en = min.getArg(i);
2434 NodeTupleSet argTupleSet = new NodeTupleSet();
2435 analyzeFlowExpressionNode(callermd, nametable, en, argTupleSet, false);
2436 // if argument is liternal node, argTuple is set to NULL.
2437 addArgIdxMap(min, i + offset, argTupleSet);
2438 nodeSet.addTupleSet(argTupleSet);
2445 private void analyzeLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode en) {
2449 private void analyzeFlowArrayAccessNode(MethodDescriptor md, SymbolTable nametable,
2450 ArrayAccessNode aan, NodeTupleSet nodeSet, boolean isLHS) {
2452 NodeTupleSet expNodeTupleSet = new NodeTupleSet();
2453 NTuple<Descriptor> base =
2454 analyzeFlowExpressionNode(md, nametable, aan.getExpression(), expNodeTupleSet, isLHS);
2456 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
2457 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, isLHS);
2460 // need to create an edge from idx to array
2461 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
2462 NTuple<Descriptor> idxTuple = idxIter.next();
2463 for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
2464 NTuple<Descriptor> arrTuple = arrIter.next();
2465 getFlowGraph(md).addValueFlowEdge(idxTuple, arrTuple);
2469 nodeSet.addTupleSet(expNodeTupleSet);
2471 nodeSet.addTupleSet(expNodeTupleSet);
2472 nodeSet.addTupleSet(idxNodeTupleSet);
2476 private void analyzeCreateObjectNode(MethodDescriptor md, SymbolTable nametable,
2477 CreateObjectNode en) {
2478 // TODO Auto-generated method stub
2482 private void analyzeFlowOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on,
2483 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
2485 NodeTupleSet leftOpSet = new NodeTupleSet();
2486 NodeTupleSet rightOpSet = new NodeTupleSet();
2489 analyzeFlowExpressionNode(md, nametable, on.getLeft(), leftOpSet, null, implicitFlowTupleSet,
2492 if (on.getRight() != null) {
2494 analyzeFlowExpressionNode(md, nametable, on.getRight(), rightOpSet, null,
2495 implicitFlowTupleSet, false);
2498 Operation op = on.getOp();
2500 switch (op.getOp()) {
2502 case Operation.UNARYPLUS:
2503 case Operation.UNARYMINUS:
2504 case Operation.LOGIC_NOT:
2506 nodeSet.addTupleSet(leftOpSet);
2509 case Operation.LOGIC_OR:
2510 case Operation.LOGIC_AND:
2511 case Operation.COMP:
2512 case Operation.BIT_OR:
2513 case Operation.BIT_XOR:
2514 case Operation.BIT_AND:
2515 case Operation.ISAVAILABLE:
2516 case Operation.EQUAL:
2517 case Operation.NOTEQUAL:
2524 case Operation.MULT:
2527 case Operation.LEFTSHIFT:
2528 case Operation.RIGHTSHIFT:
2529 case Operation.URIGHTSHIFT:
2531 // there are two operands
2532 nodeSet.addTupleSet(leftOpSet);
2533 nodeSet.addTupleSet(rightOpSet);
2537 throw new Error(op.toString());
2542 private NTuple<Descriptor> analyzeFlowNameNode(MethodDescriptor md, SymbolTable nametable,
2543 NameNode nn, NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
2546 base = new NTuple<Descriptor>();
2549 NameDescriptor nd = nn.getName();
2551 if (nd.getBase() != null) {
2553 analyzeFlowExpressionNode(md, nametable, nn.getExpression(), nodeSet, base,
2554 implicitFlowTupleSet, false);
2556 // base node has the top location
2560 String varname = nd.toString();
2561 if (varname.equals("this")) {
2563 base.add(md.getThis());
2567 Descriptor d = (Descriptor) nametable.get(varname);
2569 if (d instanceof VarDescriptor) {
2570 VarDescriptor vd = (VarDescriptor) d;
2572 } else if (d instanceof FieldDescriptor) {
2573 // the type of field descriptor has a location!
2574 FieldDescriptor fd = (FieldDescriptor) d;
2575 if (fd.isStatic()) {
2577 // if it is 'static final', no need to have flow node for the TOP
2581 // if 'static', assign the default GLOBAL LOCATION to the first
2582 // element of the tuple
2583 base.add(GLOBALDESC);
2586 // the location of field access starts from this, followed by field
2588 base.add(md.getThis());
2592 } else if (d == null) {
2593 // access static field
2594 base.add(GLOBALDESC);
2595 // base.add(nn.getField());
2598 // FieldDescriptor fd = nn.getField();addFlowGraphEdge
2600 // MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
2601 // String globalLocId = localLattice.getGlobalLoc();
2602 // if (globalLocId == null) {
2604 // Error("Method lattice does not define global variable location at "
2605 // + generateErrorMessage(md.getClassDesc(), nn));
2607 // loc.addLocation(new Location(md, globalLocId));
2609 // Location fieldLoc = (Location) fd.getType().getExtension();
2610 // loc.addLocation(fieldLoc);
2616 getFlowGraph(md).createNewFlowNode(base);
2622 private NTuple<Descriptor> analyzeFlowFieldAccessNode(MethodDescriptor md, SymbolTable nametable,
2623 FieldAccessNode fan, NodeTupleSet nodeSet, NTuple<Descriptor> base,
2624 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
2626 ExpressionNode left = fan.getExpression();
2627 TypeDescriptor ltd = left.getType();
2628 FieldDescriptor fd = fan.getField();
2630 String varName = null;
2631 if (left.kind() == Kind.NameNode) {
2632 NameDescriptor nd = ((NameNode) left).getName();
2633 varName = nd.toString();
2636 if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) {
2637 // using a class name directly or access using this
2638 if (fd.isStatic() && fd.isFinal()) {
2643 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
2644 if (left instanceof ArrayAccessNode) {
2646 ArrayAccessNode aan = (ArrayAccessNode) left;
2647 left = aan.getExpression();
2648 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, base,
2649 implicitFlowTupleSet, isLHS);
2650 nodeSet.addTupleSet(idxNodeTupleSet);
2653 analyzeFlowExpressionNode(md, nametable, left, nodeSet, base, implicitFlowTupleSet, isLHS);
2656 // in this case, field is TOP location
2660 NTuple<Descriptor> flowFieldTuple = new NTuple<Descriptor>(base.toList());
2662 if (!left.getType().isPrimitive()) {
2664 if (!fd.getSymbol().equals("length")) {
2665 // array.length access, just have the location of the array
2666 flowFieldTuple.add(fd);
2667 nodeSet.removeTuple(base);
2671 getFlowGraph(md).createNewFlowNode(flowFieldTuple);
2674 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
2675 NTuple<Descriptor> idxTuple = idxIter.next();
2676 getFlowGraph(md).addValueFlowEdge(idxTuple, flowFieldTuple);
2680 return flowFieldTuple;
2686 private void debug_printTreeNode(TreeNode tn) {
2688 System.out.println("DEBUG: " + tn.printNode(0) + " line#=" + tn.getNumLine());
2692 private void analyzeFlowAssignmentNode(MethodDescriptor md, SymbolTable nametable,
2693 AssignmentNode an, NodeTupleSet nodeSet, NTuple<Descriptor> base,
2694 NodeTupleSet implicitFlowTupleSet) {
2696 NodeTupleSet nodeSetRHS = new NodeTupleSet();
2697 NodeTupleSet nodeSetLHS = new NodeTupleSet();
2699 boolean postinc = true;
2700 if (an.getOperation().getBaseOp() == null
2701 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
2702 .getBaseOp().getOp() != Operation.POSTDEC)) {
2705 // if LHS is array access node, need to capture value flows between an array
2706 // and its index value
2707 analyzeFlowExpressionNode(md, nametable, an.getDest(), nodeSetLHS, null, implicitFlowTupleSet,
2711 // analyze value flows of rhs expression
2712 analyzeFlowExpressionNode(md, nametable, an.getSrc(), nodeSetRHS, null, implicitFlowTupleSet,
2715 // System.out.println("-analyzeFlowAssignmentNode=" + an.printNode(0));
2716 // System.out.println("-nodeSetLHS=" + nodeSetLHS);
2717 // System.out.println("-nodeSetRHS=" + nodeSetRHS);
2718 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
2719 // System.out.println("-");
2721 if (an.getOperation().getOp() >= 2 && an.getOperation().getOp() <= 12) {
2722 // if assignment contains OP+EQ operator, creates edges from LHS to LHS
2723 for (Iterator<NTuple<Descriptor>> iter = nodeSetLHS.iterator(); iter.hasNext();) {
2724 NTuple<Descriptor> fromTuple = iter.next();
2725 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2726 NTuple<Descriptor> toTuple = iter2.next();
2727 addFlowGraphEdge(md, fromTuple, toTuple);
2732 // creates edges from RHS to LHS
2733 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
2734 NTuple<Descriptor> fromTuple = iter.next();
2735 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2736 NTuple<Descriptor> toTuple = iter2.next();
2737 addFlowGraphEdge(md, fromTuple, toTuple);
2741 // creates edges from implicitFlowTupleSet to LHS
2742 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
2743 NTuple<Descriptor> fromTuple = iter.next();
2744 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2745 NTuple<Descriptor> toTuple = iter2.next();
2746 addFlowGraphEdge(md, fromTuple, toTuple);
2752 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2753 NTuple<Descriptor> tuple = iter2.next();
2754 addFlowGraphEdge(md, tuple, tuple);
2757 // creates edges from implicitFlowTupleSet to LHS
2758 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
2759 NTuple<Descriptor> fromTuple = iter.next();
2760 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2761 NTuple<Descriptor> toTuple = iter2.next();
2762 addFlowGraphEdge(md, fromTuple, toTuple);
2768 if (nodeSet != null) {
2769 nodeSet.addTupleSet(nodeSetLHS);
2773 public FlowGraph getFlowGraph(MethodDescriptor md) {
2774 return mapMethodDescriptorToFlowGraph.get(md);
2777 private boolean addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
2778 NTuple<Descriptor> to) {
2780 // return true if it adds a new edge
2781 FlowGraph graph = getFlowGraph(md);
2782 graph.addValueFlowEdge(from, to);
2786 public void _debug_printGraph() {
2787 Set<MethodDescriptor> keySet = mapMethodDescriptorToFlowGraph.keySet();
2789 for (Iterator<MethodDescriptor> iterator = keySet.iterator(); iterator.hasNext();) {
2790 MethodDescriptor md = (MethodDescriptor) iterator.next();
2791 FlowGraph fg = mapMethodDescriptorToFlowGraph.get(md);
2794 } catch (IOException e) {
2795 e.printStackTrace();
2803 class CyclicFlowException extends Exception {