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<String, Vector<String>> mapFileNameToLineVector;
90 private Map<Descriptor, Integer> mapDescToDefinitionLine;
92 public static final String GLOBALLOC = "GLOBALLOC";
94 public static final String TOPLOC = "TOPLOC";
96 public static final Descriptor GLOBALDESC = new NameDescriptor(GLOBALLOC);
98 public static final Descriptor TOPDESC = new NameDescriptor(TOPLOC);
100 public static String newline = System.getProperty("line.separator");
102 LocationInfo curMethodInfo;
104 boolean debug = true;
106 public LocationInference(SSJavaAnalysis ssjava, State state) {
107 this.ssjava = ssjava;
109 this.toanalyzeList = new ArrayList<ClassDescriptor>();
110 this.toanalyzeMethodList = new ArrayList<MethodDescriptor>();
111 this.mapMethodDescriptorToFlowGraph = new HashMap<MethodDescriptor, FlowGraph>();
112 this.cd2lattice = new HashMap<ClassDescriptor, SSJavaLattice<String>>();
113 this.md2lattice = new HashMap<MethodDescriptor, SSJavaLattice<String>>();
114 this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
115 this.mapMethodDescriptorToMethodInvokeNodeSet =
116 new HashMap<MethodDescriptor, Set<MethodInvokeNode>>();
117 this.mapMethodInvokeNodeToArgIdxMap =
118 new HashMap<MethodInvokeNode, Map<Integer, NodeTupleSet>>();
119 this.mapMethodDescToMethodLocationInfo = new HashMap<MethodDescriptor, MethodLocationInfo>();
120 this.mapMethodToCalleeSet = new HashMap<MethodDescriptor, Set<MethodDescriptor>>();
121 this.mapClassToLocationInfo = new HashMap<ClassDescriptor, LocationInfo>();
123 this.mapFileNameToLineVector = new HashMap<String, Vector<String>>();
124 this.mapDescToDefinitionLine = new HashMap<Descriptor, Integer>();
127 public void setupToAnalyze() {
128 SymbolTable classtable = state.getClassSymbolTable();
129 toanalyzeList.clear();
130 toanalyzeList.addAll(classtable.getValueSet());
131 Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
132 public int compare(ClassDescriptor o1, ClassDescriptor o2) {
133 return o1.getClassName().compareToIgnoreCase(o2.getClassName());
138 public void setupToAnalazeMethod(ClassDescriptor cd) {
140 SymbolTable methodtable = cd.getMethodTable();
141 toanalyzeMethodList.clear();
142 toanalyzeMethodList.addAll(methodtable.getValueSet());
143 Collections.sort(toanalyzeMethodList, new Comparator<MethodDescriptor>() {
144 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
145 return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
150 public boolean toAnalyzeMethodIsEmpty() {
151 return toanalyzeMethodList.isEmpty();
154 public boolean toAnalyzeIsEmpty() {
155 return toanalyzeList.isEmpty();
158 public ClassDescriptor toAnalyzeNext() {
159 return toanalyzeList.remove(0);
162 public MethodDescriptor toAnalyzeMethodNext() {
163 return toanalyzeMethodList.remove(0);
166 public void inference() {
168 // 1) construct value flow graph
169 constructFlowGraph();
171 // 2) construct lattices
176 debug_writeLatticeDotFile();
178 // 3) check properties
181 // 4) generate annotated source codes
182 generateAnnoatedCode();
186 private void addMapClassDefinitionToLineNum(ClassDescriptor cd, String strLine, int lineNum) {
187 String pattern = "class " + cd.getSymbol() + " ";
188 if (strLine.indexOf(pattern) != -1) {
189 mapDescToDefinitionLine.put(cd, lineNum);
193 private void addMapMethodDefinitionToLineNum(Set<MethodDescriptor> methodSet, String strLine,
195 for (Iterator iterator = methodSet.iterator(); iterator.hasNext();) {
196 MethodDescriptor md = (MethodDescriptor) iterator.next();
197 String pattern = md.getMethodDeclaration();
198 if (strLine.indexOf(pattern) != -1) {
199 mapDescToDefinitionLine.put(md, lineNum);
200 methodSet.remove(md);
207 private void readOriginalSourceFiles() {
209 SymbolTable classtable = state.getClassSymbolTable();
211 Set<ClassDescriptor> classDescSet = new HashSet<ClassDescriptor>();
212 classDescSet.addAll(classtable.getValueSet());
215 // inefficient implement. it may re-visit the same file if the file
216 // contains more than one class definitions.
217 for (Iterator iterator = classDescSet.iterator(); iterator.hasNext();) {
218 ClassDescriptor cd = (ClassDescriptor) iterator.next();
220 Set<MethodDescriptor> methodSet = new HashSet<MethodDescriptor>();
221 methodSet.addAll(cd.getMethodTable().getValueSet());
223 String sourceFileName = cd.getSourceFileName();
224 Vector<String> lineVec = new Vector<String>();
226 mapFileNameToLineVector.put(sourceFileName, lineVec);
228 BufferedReader in = new BufferedReader(new FileReader(sourceFileName));
231 lineVec.add(""); // the index is started from 1.
232 while ((strLine = in.readLine()) != null) {
233 lineVec.add(lineNum, strLine);
234 addMapClassDefinitionToLineNum(cd, strLine, lineNum);
235 addMapMethodDefinitionToLineNum(methodSet, strLine, lineNum);
241 } catch (IOException e) {
247 private String generateLatticeDefinition(Descriptor desc) {
249 SSJavaLattice<String> lattice = getLattice(desc);
250 String rtr = "@LATTICE(\"";
252 Map<String, Set<String>> map = lattice.getTable();
253 Set<String> keySet = map.keySet();
254 boolean first = true;
255 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
256 String key = (String) iterator.next();
257 if (!key.equals(lattice.getTopItem())) {
258 Set<String> connectedSet = map.get(key);
260 if (connectedSet.size() == 1) {
261 if (connectedSet.iterator().next().equals(lattice.getBottomItem())) {
272 for (Iterator iterator2 = connectedSet.iterator(); iterator2.hasNext();) {
273 String loc = (String) iterator2.next();
274 if (!loc.equals(lattice.getBottomItem())) {
281 rtr += loc + "<" + key;
289 if (desc instanceof MethodDescriptor) {
290 TypeDescriptor returnType = ((MethodDescriptor) desc).getReturnType();
291 if (returnType != null && (!returnType.isVoid())) {
292 rtr += "\n@RETURNLOC(\"RETURNLOC\")";
294 rtr += "\n@THISLOC(\"this\")\n@PCLOC(\"PCLOC\")\n@GLOBALLOC(\"GLOBALLOC\")";
301 private void generateAnnoatedCode() {
303 readOriginalSourceFiles();
307 while (!toAnalyzeIsEmpty()) {
308 ClassDescriptor cd = toAnalyzeNext();
310 setupToAnalazeMethod(cd);
312 LocationInfo locInfo = mapClassToLocationInfo.get(cd);
314 String sourceFileName = cd.getSourceFileName();
316 int classDefLine = mapDescToDefinitionLine.get(cd);
317 Vector<String> sourceVec = mapFileNameToLineVector.get(sourceFileName);
319 if (locInfo != null) {
321 Map<Descriptor, CompositeLocation> mapDescToInferLoc = locInfo.getMapDescToInferLocation();
322 Set<Descriptor> fieldDescSet = mapDescToInferLoc.keySet();
323 for (Iterator iterator = fieldDescSet.iterator(); iterator.hasNext();) {
324 Descriptor fieldDesc = (Descriptor) iterator.next();
325 String locIdentifier = locInfo.getFieldInferLocation(fieldDesc).getLocIdentifier();
326 if (!getLattice(cd).containsKey(locIdentifier)) {
327 getLattice(cd).put(locIdentifier);
331 String fieldLatticeDefStr = generateLatticeDefinition(cd);
332 String annoatedSrc = fieldLatticeDefStr + newline + sourceVec.get(classDefLine);
333 sourceVec.set(classDefLine, annoatedSrc);
335 // generate annotations for field declarations
336 LocationInfo fieldLocInfo = getLocationInfo(cd);
337 Map<Descriptor, CompositeLocation> inferLocMap = fieldLocInfo.getMapDescToInferLocation();
339 for (Iterator iter = cd.getFields(); iter.hasNext();) {
340 FieldDescriptor fd = (FieldDescriptor) iter.next();
342 String locAnnotationStr;
343 if (inferLocMap.containsKey(fd)) {
344 CompositeLocation inferLoc = inferLocMap.get(fd);
345 locAnnotationStr = generateLocationAnnoatation(inferLoc);
347 // if the field is not accssed by SS part, just assigns dummy
349 locAnnotationStr = "@LOC(\"LOC\")";
351 int fdLineNum = fd.getLineNum();
352 String orgFieldDeclarationStr = sourceVec.get(fdLineNum);
353 String fieldDeclaration = fd.toString();
354 fieldDeclaration = fieldDeclaration.substring(0, fieldDeclaration.length() - 1);
356 int idx = orgFieldDeclarationStr.indexOf(fieldDeclaration);
358 orgFieldDeclarationStr.substring(0, idx) + locAnnotationStr + " "
359 + orgFieldDeclarationStr.substring(idx);
360 sourceVec.set(fdLineNum, annoatedStr);
366 while (!toAnalyzeMethodIsEmpty()) {
367 MethodDescriptor md = toAnalyzeMethodNext();
368 SSJavaLattice<String> methodLattice = md2lattice.get(md);
369 if (methodLattice != null) {
371 int methodDefLine = md.getLineNum();
373 MethodLocationInfo methodLocInfo = getMethodLocationInfo(md);
375 Map<Descriptor, CompositeLocation> inferLocMap =
376 methodLocInfo.getMapDescToInferLocation();
377 Set<Descriptor> localVarDescSet = inferLocMap.keySet();
379 for (Iterator iterator = localVarDescSet.iterator(); iterator.hasNext();) {
380 Descriptor localVarDesc = (Descriptor) iterator.next();
381 CompositeLocation inferLoc = inferLocMap.get(localVarDesc);
383 String locAnnotationStr = generateLocationAnnoatation(inferLoc);
385 if (!isParameter(md, localVarDesc)) {
386 if (mapDescToDefinitionLine.containsKey(localVarDesc)) {
387 int varLineNum = mapDescToDefinitionLine.get(localVarDesc);
389 String orgSourceLine = sourceVec.get(varLineNum);
390 int idx = orgSourceLine.indexOf(localVarDesc.toString());
392 orgSourceLine.substring(0, idx) + locAnnotationStr + " "
393 + orgSourceLine.substring(idx);
394 sourceVec.set(varLineNum, annoatedStr);
397 String methodDefStr = sourceVec.get(methodDefLine);
398 int idx = methodDefStr.indexOf(localVarDesc.toString());
401 methodDefStr.substring(0, idx) + locAnnotationStr + " "
402 + methodDefStr.substring(idx);
403 sourceVec.set(methodDefLine, annoatedStr);
408 String methodLatticeDefStr = generateLatticeDefinition(md);
409 String annoatedStr = methodLatticeDefStr + newline + sourceVec.get(methodDefLine);
410 sourceVec.set(methodDefLine, annoatedStr);
419 private String generateLocationAnnoatation(CompositeLocation loc) {
420 String rtr = "@LOC(\"";
423 Location methodLoc = loc.get(0);
424 rtr += methodLoc.getLocIdentifier();
426 for (int i = 1; i < loc.getSize(); i++) {
427 Location element = loc.get(i);
428 rtr += "," + element.getDescriptor().getSymbol() + "." + element.getLocIdentifier();
435 private boolean isParameter(MethodDescriptor md, Descriptor localVarDesc) {
436 return getFlowGraph(md).isParamDesc(localVarDesc);
439 private String extractFileName(String fileName) {
440 int idx = fileName.lastIndexOf("/");
444 return fileName.substring(idx + 1);
449 private void codeGen() {
451 Set<String> originalFileNameSet = mapFileNameToLineVector.keySet();
452 for (Iterator iterator = originalFileNameSet.iterator(); iterator.hasNext();) {
453 String orgFileName = (String) iterator.next();
454 String outputFileName = extractFileName(orgFileName);
456 Vector<String> sourceVec = mapFileNameToLineVector.get(orgFileName);
460 FileWriter fileWriter = new FileWriter("./infer/" + outputFileName);
461 BufferedWriter out = new BufferedWriter(fileWriter);
463 for (int i = 0; i < sourceVec.size(); i++) {
464 out.write(sourceVec.get(i));
468 } catch (IOException e) {
476 private void simplifyLattices() {
478 // generate lattice dot file
481 while (!toAnalyzeIsEmpty()) {
482 ClassDescriptor cd = toAnalyzeNext();
484 setupToAnalazeMethod(cd);
486 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
487 if (classLattice != null) {
488 classLattice.removeRedundantEdges();
491 while (!toAnalyzeMethodIsEmpty()) {
492 MethodDescriptor md = toAnalyzeMethodNext();
493 SSJavaLattice<String> methodLattice = md2lattice.get(md);
494 if (methodLattice != null) {
495 methodLattice.removeRedundantEdges();
502 private void checkLattices() {
504 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
506 // current descriptors to visit in fixed-point interprocedural analysis,
508 // dependency in the call graph
509 methodDescriptorsToVisitStack.clear();
511 descriptorListToAnalyze.removeFirst();
513 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
514 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
516 while (!descriptorListToAnalyze.isEmpty()) {
517 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
518 checkLatticesOfVirtualMethods(md);
523 private void debug_writeLatticeDotFile() {
524 // generate lattice dot file
528 while (!toAnalyzeIsEmpty()) {
529 ClassDescriptor cd = toAnalyzeNext();
531 setupToAnalazeMethod(cd);
533 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
534 if (classLattice != null) {
535 ssjava.writeLatticeDotFile(cd, null, classLattice);
536 debug_printDescriptorToLocNameMapping(cd);
539 while (!toAnalyzeMethodIsEmpty()) {
540 MethodDescriptor md = toAnalyzeMethodNext();
541 SSJavaLattice<String> methodLattice = md2lattice.get(md);
542 if (methodLattice != null) {
543 ssjava.writeLatticeDotFile(cd, md, methodLattice);
544 debug_printDescriptorToLocNameMapping(md);
551 private void debug_printDescriptorToLocNameMapping(Descriptor desc) {
553 LocationInfo info = getLocationInfo(desc);
554 System.out.println("## " + desc + " ##");
555 System.out.println(info.getMapDescToInferLocation());
556 LocationInfo locInfo = getLocationInfo(desc);
557 System.out.println("mapping=" + locInfo.getMapLocSymbolToDescSet());
558 System.out.println("###################");
562 private void inferLattices() {
564 // do fixed-point analysis
566 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
568 Collections.sort(descriptorListToAnalyze, new Comparator<MethodDescriptor>() {
569 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
570 return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
574 // current descriptors to visit in fixed-point interprocedural analysis,
576 // dependency in the call graph
577 methodDescriptorsToVisitStack.clear();
579 // descriptorListToAnalyze.removeFirst();
581 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
582 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
584 while (!descriptorListToAnalyze.isEmpty()) {
585 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
586 methodDescriptorsToVisitStack.add(md);
589 // analyze scheduled methods until there are no more to visit
590 while (!methodDescriptorsToVisitStack.isEmpty()) {
591 // start to analyze leaf node
592 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
594 SSJavaLattice<String> methodLattice =
595 new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
597 MethodLocationInfo methodInfo = new MethodLocationInfo(md);
598 curMethodInfo = methodInfo;
600 System.out.println();
601 System.out.println("SSJAVA: Inferencing the lattice from " + md);
604 analyzeMethodLattice(md, methodLattice, methodInfo);
605 } catch (CyclicFlowException e) {
606 throw new Error("Fail to generate the method lattice for " + md);
609 SSJavaLattice<String> prevMethodLattice = getMethodLattice(md);
610 MethodLocationInfo prevMethodInfo = getMethodLocationInfo(md);
612 if ((!methodLattice.equals(prevMethodLattice)) || (!methodInfo.equals(prevMethodInfo))) {
614 setMethodLattice(md, methodLattice);
615 setMethodLocInfo(md, methodInfo);
617 // results for callee changed, so enqueue dependents caller for
619 Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
620 while (depsItr.hasNext()) {
621 MethodDescriptor methodNext = depsItr.next();
622 if (!methodDescriptorsToVisitStack.contains(methodNext)
623 && methodDescriptorToVistSet.contains(methodNext)) {
624 methodDescriptorsToVisitStack.add(methodNext);
633 private void setMethodLocInfo(MethodDescriptor md, MethodLocationInfo methodInfo) {
634 mapMethodDescToMethodLocationInfo.put(md, methodInfo);
637 private void checkLatticesOfVirtualMethods(MethodDescriptor md) {
639 if (!md.isStatic()) {
640 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
641 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
643 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
644 MethodDescriptor mdCallee = (MethodDescriptor) iterator.next();
645 if (!md.equals(mdCallee)) {
646 checkConsistency(md, mdCallee);
654 private void checkConsistency(MethodDescriptor md1, MethodDescriptor md2) {
656 // check that two lattice have the same relations between parameters(+PC
657 // LOC, GLOBAL_LOC RETURN LOC)
659 List<CompositeLocation> list1 = new ArrayList<CompositeLocation>();
660 List<CompositeLocation> list2 = new ArrayList<CompositeLocation>();
662 MethodLocationInfo locInfo1 = getMethodLocationInfo(md1);
663 MethodLocationInfo locInfo2 = getMethodLocationInfo(md2);
665 Map<Integer, CompositeLocation> paramMap1 = locInfo1.getMapParamIdxToInferLoc();
666 Map<Integer, CompositeLocation> paramMap2 = locInfo2.getMapParamIdxToInferLoc();
668 int numParam = locInfo1.getMapParamIdxToInferLoc().keySet().size();
670 // add location types of paramters
671 for (int idx = 0; idx < numParam; idx++) {
672 list1.add(paramMap1.get(Integer.valueOf(idx)));
673 list2.add(paramMap2.get(Integer.valueOf(idx)));
676 // add program counter location
677 list1.add(locInfo1.getPCLoc());
678 list2.add(locInfo2.getPCLoc());
680 if (!md1.getReturnType().isVoid()) {
681 // add return value location
682 CompositeLocation rtrLoc1 =
683 new CompositeLocation(new Location(md1, locInfo1.getReturnLocName()));
684 CompositeLocation rtrLoc2 =
685 new CompositeLocation(new Location(md2, locInfo2.getReturnLocName()));
690 // add global location type
691 if (md1.isStatic()) {
692 CompositeLocation globalLoc1 =
693 new CompositeLocation(new Location(md1, locInfo1.getGlobalLocName()));
694 CompositeLocation globalLoc2 =
695 new CompositeLocation(new Location(md2, locInfo2.getGlobalLocName()));
696 list1.add(globalLoc1);
697 list2.add(globalLoc2);
700 for (int i = 0; i < list1.size(); i++) {
701 CompositeLocation locA1 = list1.get(i);
702 CompositeLocation locA2 = list2.get(i);
703 for (int k = 0; k < list1.size(); k++) {
705 CompositeLocation locB1 = list1.get(k);
706 CompositeLocation locB2 = list2.get(k);
707 boolean r1 = isGreaterThan(getLattice(md1), locA1, locB1);
709 boolean r2 = isGreaterThan(getLattice(md1), locA2, locB2);
712 throw new Error("The method " + md1 + " is not consistent with the method " + md2
713 + ".:: They have a different ordering relation between locations (" + locA1 + ","
714 + locB1 + ") and (" + locA2 + "," + locB2 + ").");
722 private String getSymbol(int idx, FlowNode node) {
723 Descriptor desc = node.getDescTuple().get(idx);
724 return desc.getSymbol();
727 private Descriptor getDescriptor(int idx, FlowNode node) {
728 Descriptor desc = node.getDescTuple().get(idx);
732 private void analyzeMethodLattice(MethodDescriptor md, SSJavaLattice<String> methodLattice,
733 MethodLocationInfo methodInfo) throws CyclicFlowException {
735 // first take a look at method invocation nodes to newly added relations
737 analyzeLatticeMethodInvocationNode(md, methodLattice, methodInfo);
739 if (!md.isStatic()) {
740 // set the this location
741 String thisLocSymbol = md.getThis().getSymbol();
742 methodInfo.setThisLocName(thisLocSymbol);
745 // set the global location
746 methodInfo.setGlobalLocName(LocationInference.GLOBALLOC);
747 methodInfo.mapDescriptorToLocation(GLOBALDESC, new CompositeLocation(
748 new Location(md, GLOBALLOC)));
750 // visit each node of method flow graph
751 FlowGraph fg = getFlowGraph(md);
752 Set<FlowNode> nodeSet = fg.getNodeSet();
754 // for the method lattice, we need to look at the first element of
755 // NTuple<Descriptor>
756 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
757 FlowNode srcNode = (FlowNode) iterator.next();
759 Set<FlowEdge> outEdgeSet = srcNode.getOutEdgeSet();
760 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
761 FlowEdge outEdge = (FlowEdge) iterator2.next();
762 FlowNode dstNode = outEdge.getDst();
764 NTuple<Descriptor> srcNodeTuple = srcNode.getDescTuple();
765 NTuple<Descriptor> dstNodeTuple = dstNode.getDescTuple();
767 if (outEdge.getInitTuple().equals(srcNodeTuple)
768 && outEdge.getEndTuple().equals(dstNodeTuple)) {
770 if ((srcNodeTuple.size() > 1 && dstNodeTuple.size() > 1)
771 && srcNodeTuple.get(0).equals(dstNodeTuple.get(0))) {
773 // value flows between fields
774 Descriptor desc = srcNodeTuple.get(0);
775 ClassDescriptor classDesc;
777 if (desc.equals(GLOBALDESC)) {
778 classDesc = md.getClassDesc();
780 VarDescriptor varDesc = (VarDescriptor) srcNodeTuple.get(0);
781 classDesc = varDesc.getType().getClassDesc();
783 extractRelationFromFieldFlows(classDesc, srcNode, dstNode, 1);
786 // value flow between local var - local var or local var - field
787 addRelationToLattice(md, methodLattice, methodInfo, srcNode, dstNode);
790 // else if (srcNodeTuple.size() == 1 || dstNodeTuple.size() == 1) {
791 // // for the method lattice, we need to look at the first element of
792 // // NTuple<Descriptor>
793 // // in this case, take a look at connected nodes at the local level
794 // addRelationToLattice(md, methodLattice, methodInfo, srcNode,
798 // (!srcNode.getDescTuple().get(0).equals(dstNode.getDescTuple().get(0)))
800 // // in this case, take a look at connected nodes at the local level
801 // addRelationToLattice(md, methodLattice, methodInfo, srcNode,
804 // Descriptor srcDesc = srcNode.getDescTuple().get(0);
805 // Descriptor dstDesc = dstNode.getDescTuple().get(0);
806 // recursivelyAddCompositeRelation(md, fg, methodInfo, srcNode,
809 // // recursiveAddRelationToLattice(1, md, srcNode, dstNode);
817 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
818 FlowNode flowNode = (FlowNode) iterator.next();
819 if (flowNode.isDeclaratonNode()) {
820 CompositeLocation inferLoc = methodInfo.getInferLocation(flowNode.getDescTuple().get(0));
821 String locIdentifier = inferLoc.get(0).getLocIdentifier();
822 if (!methodLattice.containsKey(locIdentifier)) {
823 methodLattice.put(locIdentifier);
829 // create mapping from param idx to inferred composite location
832 if (!md.isStatic()) {
833 // add 'this' reference location
835 methodInfo.addMapParamIdxToInferLoc(0, methodInfo.getInferLocation(md.getThis()));
840 for (int idx = 0; idx < md.numParameters(); idx++) {
841 Descriptor paramDesc = md.getParameter(idx);
842 CompositeLocation inferParamLoc = methodInfo.getInferLocation(paramDesc);
843 methodInfo.addMapParamIdxToInferLoc(idx + offset, inferParamLoc);
846 // calculate the initial program counter location
847 // PC location is higher than location types of all parameters
848 String pcLocSymbol = "PCLOC";
849 Map<Integer, CompositeLocation> mapParamToLoc = methodInfo.getMapParamIdxToInferLoc();
850 Set<Integer> keySet = mapParamToLoc.keySet();
851 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
852 Integer paramIdx = (Integer) iterator.next();
853 CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
854 String paramLocLocalSymbol = inferLoc.get(0).getLocIdentifier();
855 if (!methodLattice.isGreaterThan(pcLocSymbol, paramLocLocalSymbol)) {
856 addRelationHigherToLower(methodLattice, methodInfo, pcLocSymbol, paramLocLocalSymbol);
860 // calculate a return location
861 // the return location type is lower than all parameters
862 if (!md.getReturnType().isVoid()) {
864 String returnLocSymbol = "RETURNLOC";
866 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
867 Integer paramIdx = (Integer) iterator.next();
868 CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
869 String paramLocLocalSymbol = inferLoc.get(0).getLocIdentifier();
870 if (!methodLattice.isGreaterThan(paramLocLocalSymbol, returnLocSymbol)) {
871 addRelationHigherToLower(methodLattice, methodInfo, paramLocLocalSymbol, returnLocSymbol);
878 private boolean isGreaterThan(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
879 CompositeLocation comp2) {
881 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
883 for (int idx = 0; idx < size; idx++) {
884 Location loc1 = comp1.get(idx);
885 Location loc2 = comp2.get(idx);
887 Descriptor desc1 = loc1.getDescriptor();
888 Descriptor desc2 = loc2.getDescriptor();
890 if (!desc1.equals(desc2)) {
891 throw new Error("Fail to compare " + comp1 + " and " + comp2);
894 String symbol1 = loc1.getLocIdentifier();
895 String symbol2 = loc2.getLocIdentifier();
897 SSJavaLattice<String> lattice;
899 lattice = methodLattice;
901 lattice = getLattice(desc1);
903 if (symbol1.equals(symbol2)) {
905 } else if (lattice.isGreaterThan(symbol1, symbol2)) {
916 private void recursiveAddRelationToLattice(int idx, MethodDescriptor md,
917 CompositeLocation srcInferLoc, CompositeLocation dstInferLoc) throws CyclicFlowException {
919 String srcLocSymbol = srcInferLoc.get(idx).getLocIdentifier();
920 String dstLocSymbol = dstInferLoc.get(idx).getLocIdentifier();
922 if (srcLocSymbol.equals(dstLocSymbol)) {
923 recursiveAddRelationToLattice(idx + 1, md, srcInferLoc, dstInferLoc);
926 Descriptor parentDesc = srcInferLoc.get(idx).getDescriptor();
927 LocationInfo locInfo = getLocationInfo(parentDesc);
929 addRelationHigherToLower(getLattice(parentDesc), getLocationInfo(parentDesc), srcLocSymbol,
935 private void analyzeLatticeMethodInvocationNode(MethodDescriptor mdCaller,
936 SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo)
937 throws CyclicFlowException {
939 // the transformation for a call site propagates all relations between
940 // parameters from the callee
941 // if the method is virtual, it also grab all relations from any possible
944 Set<MethodInvokeNode> setMethodInvokeNode =
945 mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
947 if (setMethodInvokeNode != null) {
949 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
950 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
951 MethodDescriptor mdCallee = min.getMethod();
952 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
953 if (mdCallee.isStatic()) {
954 setPossibleCallees.add(mdCallee);
956 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
957 // removes method descriptors that are not invoked by the caller
958 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
959 setPossibleCallees.addAll(calleeSet);
962 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
963 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
964 propagateRelationToCaller(min, mdCaller, possibleMdCallee, methodLattice, methodInfo);
972 private void propagateRelationToCaller(MethodInvokeNode min, MethodDescriptor mdCaller,
973 MethodDescriptor possibleMdCallee, SSJavaLattice<String> methodLattice,
974 MethodLocationInfo methodInfo) throws CyclicFlowException {
976 SSJavaLattice<String> calleeLattice = getMethodLattice(possibleMdCallee);
977 MethodLocationInfo calleeLocInfo = getMethodLocationInfo(possibleMdCallee);
978 FlowGraph calleeFlowGraph = getFlowGraph(possibleMdCallee);
980 int numParam = calleeLocInfo.getNumParam();
981 for (int i = 0; i < numParam; i++) {
982 CompositeLocation param1 = calleeLocInfo.getParamCompositeLocation(i);
983 for (int k = 0; k < numParam; k++) {
985 CompositeLocation param2 = calleeLocInfo.getParamCompositeLocation(k);
986 if (isGreaterThan(getLattice(possibleMdCallee), param1, param2)) {
987 NodeTupleSet argDescTupleSet1 = getNodeTupleSetByArgIdx(min, i);
988 NodeTupleSet argDescTupleSet2 = getNodeTupleSetByArgIdx(min, k);
990 // the callee has the relation in which param1 is higher than param2
991 // therefore, the caller has to have the relation in which arg1 is
994 for (Iterator<NTuple<Descriptor>> iterator = argDescTupleSet1.iterator(); iterator
996 NTuple<Descriptor> argDescTuple1 = iterator.next();
998 for (Iterator<NTuple<Descriptor>> iterator2 = argDescTupleSet2.iterator(); iterator2
1000 NTuple<Descriptor> argDescTuple2 = iterator2.next();
1002 // retreive inferred location by the local var descriptor
1004 NTuple<Location> tuple1 = getFlowGraph(mdCaller).getLocationTuple(argDescTuple1);
1005 NTuple<Location> tuple2 = getFlowGraph(mdCaller).getLocationTuple(argDescTuple2);
1007 // CompositeLocation higherInferLoc =
1008 // methodInfo.getInferLocation(argTuple1.get(0));
1009 // CompositeLocation lowerInferLoc =
1010 // methodInfo.getInferLocation(argTuple2.get(0));
1012 CompositeLocation inferLoc1 = generateInferredCompositeLocation(methodInfo, tuple1);
1013 CompositeLocation inferLoc2 = generateInferredCompositeLocation(methodInfo, tuple2);
1015 // addRelation(methodLattice, methodInfo, inferLoc1, inferLoc2);
1017 addFlowGraphEdge(mdCaller, argDescTuple1, argDescTuple2);
1030 private CompositeLocation generateInferredCompositeLocation(MethodLocationInfo methodInfo,
1031 NTuple<Location> tuple) {
1033 // first, retrieve inferred location by the local var descriptor
1034 CompositeLocation inferLoc = new CompositeLocation();
1036 CompositeLocation localVarInferLoc =
1037 methodInfo.getInferLocation(tuple.get(0).getLocDescriptor());
1039 localVarInferLoc.get(0).setLocDescriptor(tuple.get(0).getLocDescriptor());
1041 for (int i = 0; i < localVarInferLoc.getSize(); i++) {
1042 inferLoc.addLocation(localVarInferLoc.get(i));
1044 // System.out.println("@@@@@localVarInferLoc=" + localVarInferLoc);
1046 for (int i = 1; i < tuple.size(); i++) {
1047 Location cur = tuple.get(i);
1048 Descriptor enclosingDesc = cur.getDescriptor();
1049 Descriptor curDesc = cur.getLocDescriptor();
1051 Location inferLocElement;
1052 if (curDesc == null) {
1053 // in this case, we have a newly generated location.
1054 // System.out.println("!!! generated location=" +
1055 // cur.getLocIdentifier());
1056 inferLocElement = new Location(enclosingDesc, cur.getLocIdentifier());
1058 String fieldLocSymbol =
1059 getLocationInfo(enclosingDesc).getInferLocation(curDesc).get(0).getLocIdentifier();
1060 inferLocElement = new Location(enclosingDesc, fieldLocSymbol);
1061 inferLocElement.setLocDescriptor(curDesc);
1064 inferLoc.addLocation(inferLocElement);
1068 assert (inferLoc.get(0).getLocDescriptor().getSymbol() == inferLoc.get(0).getLocIdentifier());
1072 private void addRelation(SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo,
1073 CompositeLocation srcInferLoc, CompositeLocation dstInferLoc) throws CyclicFlowException {
1075 System.out.println("addRelation --- srcInferLoc=" + srcInferLoc + " dstInferLoc="
1077 String srcLocalLocSymbol = srcInferLoc.get(0).getLocIdentifier();
1078 String dstLocalLocSymbol = dstInferLoc.get(0).getLocIdentifier();
1080 if (srcInferLoc.getSize() == 1 && dstInferLoc.getSize() == 1) {
1081 // add a new relation to the local lattice
1082 addRelationHigherToLower(methodLattice, methodInfo, srcLocalLocSymbol, dstLocalLocSymbol);
1083 } else if (srcInferLoc.getSize() > 1 && dstInferLoc.getSize() > 1) {
1084 // both src and dst have assigned to a composite location
1086 if (!srcLocalLocSymbol.equals(dstLocalLocSymbol)) {
1087 addRelationHigherToLower(methodLattice, methodInfo, srcLocalLocSymbol, dstLocalLocSymbol);
1089 recursivelyAddRelation(1, srcInferLoc, dstInferLoc);
1092 // either src or dst has assigned to a composite location
1093 if (!srcLocalLocSymbol.equals(dstLocalLocSymbol)) {
1094 addRelationHigherToLower(methodLattice, methodInfo, srcLocalLocSymbol, dstLocalLocSymbol);
1098 System.out.println();
1102 public LocationInfo getLocationInfo(Descriptor d) {
1103 if (d instanceof MethodDescriptor) {
1104 return getMethodLocationInfo((MethodDescriptor) d);
1106 return getFieldLocationInfo((ClassDescriptor) d);
1110 private MethodLocationInfo getMethodLocationInfo(MethodDescriptor md) {
1112 if (!mapMethodDescToMethodLocationInfo.containsKey(md)) {
1113 mapMethodDescToMethodLocationInfo.put(md, new MethodLocationInfo(md));
1116 return mapMethodDescToMethodLocationInfo.get(md);
1120 private LocationInfo getFieldLocationInfo(ClassDescriptor cd) {
1122 if (!mapClassToLocationInfo.containsKey(cd)) {
1123 mapClassToLocationInfo.put(cd, new LocationInfo(cd));
1126 return mapClassToLocationInfo.get(cd);
1130 private void addRelationToLattice(MethodDescriptor md, SSJavaLattice<String> methodLattice,
1131 MethodLocationInfo methodInfo, FlowNode srcNode, FlowNode dstNode) throws CyclicFlowException {
1133 System.out.println();
1134 System.out.println("### addRelationToLattice src=" + srcNode + " dst=" + dstNode);
1136 // add a new binary relation of dstNode < srcNode
1137 FlowGraph flowGraph = getFlowGraph(md);
1139 System.out.println("***** src composite case::");
1140 calculateCompositeLocation(flowGraph, methodLattice, methodInfo, srcNode);
1142 CompositeLocation srcInferLoc =
1143 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(srcNode));
1144 CompositeLocation dstInferLoc =
1145 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(dstNode));
1146 addRelation(methodLattice, methodInfo, srcInferLoc, dstInferLoc);
1147 } catch (CyclicFlowException e) {
1148 // there is a cyclic value flow... try to calculate a composite location
1149 // for the destination node
1150 System.out.println("***** dst composite case::");
1151 calculateCompositeLocation(flowGraph, methodLattice, methodInfo, dstNode);
1152 CompositeLocation srcInferLoc =
1153 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(srcNode));
1154 CompositeLocation dstInferLoc =
1155 generateInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(dstNode));
1157 addRelation(methodLattice, methodInfo, srcInferLoc, dstInferLoc);
1158 } catch (CyclicFlowException e1) {
1159 throw new Error("Failed to merge cyclic value flows into a shared location.");
1165 private void recursivelyAddRelation(int idx, CompositeLocation srcInferLoc,
1166 CompositeLocation dstInferLoc) throws CyclicFlowException {
1168 String srcLocSymbol = srcInferLoc.get(idx).getLocIdentifier();
1169 String dstLocSymbol = dstInferLoc.get(idx).getLocIdentifier();
1171 Descriptor parentDesc = srcInferLoc.get(idx).getDescriptor();
1173 if (srcLocSymbol.equals(dstLocSymbol)) {
1174 // check if it is the case of shared location
1175 if (srcInferLoc.getSize() == (idx + 1) && dstInferLoc.getSize() == (idx + 1)) {
1176 Location inferLocElement = srcInferLoc.get(idx);
1177 System.out.println("SET SHARED LOCATION=" + inferLocElement);
1178 getLattice(inferLocElement.getDescriptor())
1179 .addSharedLoc(inferLocElement.getLocIdentifier());
1180 } else if (srcInferLoc.getSize() > (idx + 1) && dstInferLoc.getSize() > (idx + 1)) {
1181 recursivelyAddRelation(idx + 1, srcInferLoc, dstInferLoc);
1184 addRelationHigherToLower(getLattice(parentDesc), getLocationInfo(parentDesc), srcLocSymbol,
1189 private void recursivelyAddCompositeRelation(MethodDescriptor md, FlowGraph flowGraph,
1190 MethodLocationInfo methodInfo, FlowNode srcNode, FlowNode dstNode, Descriptor srcDesc,
1191 Descriptor dstDesc) throws CyclicFlowException {
1193 CompositeLocation inferSrcLoc;
1194 CompositeLocation inferDstLoc = methodInfo.getInferLocation(dstDesc);
1196 if (srcNode.getDescTuple().size() > 1) {
1198 inferSrcLoc = new CompositeLocation();
1200 NTuple<Location> locTuple = flowGraph.getLocationTuple(srcNode);
1201 for (int i = 0; i < locTuple.size(); i++) {
1202 inferSrcLoc.addLocation(locTuple.get(i));
1206 inferSrcLoc = methodInfo.getInferLocation(srcDesc);
1209 if (dstNode.getDescTuple().size() > 1) {
1211 inferDstLoc = new CompositeLocation();
1213 NTuple<Location> locTuple = flowGraph.getLocationTuple(dstNode);
1214 for (int i = 0; i < locTuple.size(); i++) {
1215 inferDstLoc.addLocation(locTuple.get(i));
1219 inferDstLoc = methodInfo.getInferLocation(dstDesc);
1222 recursiveAddRelationToLattice(1, md, inferSrcLoc, inferDstLoc);
1225 private void addPrefixMapping(Map<NTuple<Location>, Set<NTuple<Location>>> map,
1226 NTuple<Location> prefix, NTuple<Location> element) {
1228 if (!map.containsKey(prefix)) {
1229 map.put(prefix, new HashSet<NTuple<Location>>());
1231 map.get(prefix).add(element);
1234 private boolean calculateCompositeLocation(FlowGraph flowGraph,
1235 SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo, FlowNode flowNode)
1236 throws CyclicFlowException {
1238 Descriptor localVarDesc = flowNode.getDescTuple().get(0);
1239 NTuple<Location> flowNodelocTuple = flowGraph.getLocationTuple(flowNode);
1241 if (localVarDesc.equals(methodInfo.getMethodDesc())) {
1245 Set<FlowNode> inNodeSet = flowGraph.getIncomingFlowNodeSet(flowNode);
1246 Set<FlowNode> reachableNodeSet = flowGraph.getReachableFlowNodeSet(flowNode);
1248 Map<NTuple<Location>, Set<NTuple<Location>>> mapPrefixToIncomingLocTupleSet =
1249 new HashMap<NTuple<Location>, Set<NTuple<Location>>>();
1251 List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
1253 for (Iterator iterator = inNodeSet.iterator(); iterator.hasNext();) {
1254 FlowNode inNode = (FlowNode) iterator.next();
1255 NTuple<Location> inNodeTuple = flowGraph.getLocationTuple(inNode);
1257 CompositeLocation inNodeInferredLoc =
1258 generateInferredCompositeLocation(methodInfo, inNodeTuple);
1260 NTuple<Location> inNodeInferredLocTuple = inNodeInferredLoc.getTuple();
1262 for (int i = 1; i < inNodeInferredLocTuple.size(); i++) {
1263 NTuple<Location> prefix = inNodeInferredLocTuple.subList(0, i);
1264 if (!prefixList.contains(prefix)) {
1265 prefixList.add(prefix);
1267 addPrefixMapping(mapPrefixToIncomingLocTupleSet, prefix, inNodeInferredLocTuple);
1271 Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
1272 public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
1273 int s0 = arg0.size();
1274 int s1 = arg1.size();
1277 } else if (s0 == s1) {
1285 // find out reachable nodes that have the longest common prefix
1286 for (int i = 0; i < prefixList.size(); i++) {
1287 NTuple<Location> curPrefix = prefixList.get(i);
1288 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
1290 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1291 FlowNode reachableNode = (FlowNode) iterator2.next();
1292 NTuple<Location> reachLocTuple = flowGraph.getLocationTuple(reachableNode);
1293 CompositeLocation reachLocInferLoc =
1294 generateInferredCompositeLocation(methodInfo, reachLocTuple);
1295 if (reachLocInferLoc.getTuple().startsWith(curPrefix)) {
1296 reachableCommonPrefixSet.add(reachLocTuple);
1300 if (!reachableCommonPrefixSet.isEmpty()) {
1301 // found reachable nodes that start with the prefix curPrefix
1302 // need to assign a composite location
1304 // first, check if there are more than one the set of locations that has
1305 // the same length of the longest reachable prefix, no way to assign
1306 // a composite location to the input local var
1307 prefixSanityCheck(prefixList, i, flowGraph, reachableNodeSet);
1309 Set<NTuple<Location>> incomingCommonPrefixSet =
1310 mapPrefixToIncomingLocTupleSet.get(curPrefix);
1312 int idx = curPrefix.size();
1313 NTuple<Location> element = incomingCommonPrefixSet.iterator().next();
1314 Descriptor desc = element.get(idx).getDescriptor();
1316 SSJavaLattice<String> lattice = getLattice(desc);
1317 LocationInfo locInfo = getLocationInfo(desc);
1319 CompositeLocation inferLocation =
1320 generateInferredCompositeLocation(methodInfo, flowNodelocTuple);
1322 // methodInfo.getInferLocation(localVarDesc);
1323 CompositeLocation newInferLocation = new CompositeLocation();
1325 if (inferLocation.getTuple().startsWith(curPrefix)) {
1326 // the same infer location is already existed. no need to do
1330 // assign a new composite location
1332 // String oldMethodLocationSymbol =
1333 // inferLocation.get(0).getLocIdentifier();
1334 String newLocSymbol = "Loc" + (SSJavaLattice.seed++);
1335 for (int locIdx = 0; locIdx < curPrefix.size(); locIdx++) {
1336 newInferLocation.addLocation(curPrefix.get(locIdx));
1338 Location newLocationElement = new Location(desc, newLocSymbol);
1339 newInferLocation.addLocation(newLocationElement);
1341 // maps local variable to location types of the common prefix
1342 methodInfo.mapDescriptorToLocation(localVarDesc, newInferLocation.clone());
1344 // methodInfo.mapDescriptorToLocation(localVarDesc, newInferLocation);
1345 addMapLocSymbolToInferredLocation(methodInfo.getMethodDesc(), localVarDesc,
1347 methodInfo.removeMaplocalVarToLocSet(localVarDesc);
1349 // add the field/var descriptor to the set of the location symbol
1350 int lastIdx = flowNode.getDescTuple().size() - 1;
1351 Descriptor lastFlowNodeDesc = flowNode.getDescTuple().get(lastIdx);
1352 Descriptor enclosinglastLastFlowNodeDesc = flowNodelocTuple.get(lastIdx).getDescriptor();
1354 CompositeLocation newlyInferredLocForFlowNode =
1355 generateInferredCompositeLocation(methodInfo, flowNodelocTuple);
1356 Location lastInferLocElement =
1357 newlyInferredLocForFlowNode.get(newlyInferredLocForFlowNode.getSize() - 1);
1358 Descriptor enclosingLastInferLocElement = lastInferLocElement.getDescriptor();
1360 // getLocationInfo(enclosingLastInferLocElement).addMapLocSymbolToDescSet(
1361 // lastInferLocElement.getLocIdentifier(), lastFlowNodeDesc);
1362 getLocationInfo(enclosingLastInferLocElement).addMapLocSymbolToRelatedInferLoc(
1363 lastInferLocElement.getLocIdentifier(), enclosinglastLastFlowNodeDesc,
1366 // clean up the previous location
1367 // Location prevInferLocElement =
1368 // inferLocation.get(inferLocation.getSize() - 1);
1369 // Descriptor prevEnclosingDesc = prevInferLocElement.getDescriptor();
1371 // SSJavaLattice<String> targetLattice;
1372 // LocationInfo targetInfo;
1373 // if (prevEnclosingDesc.equals(methodInfo.getMethodDesc())) {
1374 // targetLattice = methodLattice;
1375 // targetInfo = methodInfo;
1377 // targetLattice = getLattice(prevInferLocElement.getDescriptor());
1378 // targetInfo = getLocationInfo(prevInferLocElement.getDescriptor());
1381 // Set<Pair<Descriptor, Descriptor>> associstedDescSet =
1382 // targetInfo.getRelatedInferLocSet(prevInferLocElement.getLocIdentifier());
1384 // if (associstedDescSet.size() == 1) {
1385 // targetLattice.remove(prevInferLocElement.getLocIdentifier());
1387 // associstedDescSet.remove(lastFlowNodeDesc);
1392 System.out.println("ASSIGN NEW COMPOSITE LOCATION =" + newInferLocation + " to "
1395 String newlyInsertedLocName =
1396 newInferLocation.get(newInferLocation.getSize() - 1).getLocIdentifier();
1398 System.out.println("-- add in-flow");
1399 for (Iterator iterator = incomingCommonPrefixSet.iterator(); iterator.hasNext();) {
1400 NTuple<Location> tuple = (NTuple<Location>) iterator.next();
1401 Location loc = tuple.get(idx);
1402 String higher = loc.getLocIdentifier();
1403 addRelationHigherToLower(lattice, locInfo, higher, newlyInsertedLocName);
1406 System.out.println("-- add out flow");
1407 for (Iterator iterator = reachableCommonPrefixSet.iterator(); iterator.hasNext();) {
1408 NTuple<Location> tuple = (NTuple<Location>) iterator.next();
1409 if (tuple.size() > idx) {
1410 Location loc = tuple.get(idx);
1411 String lower = loc.getLocIdentifier();
1413 // locInfo.getFieldInferLocation(loc.getLocDescriptor()).getLocIdentifier();
1414 addRelationHigherToLower(lattice, locInfo, newlyInsertedLocName, lower);
1427 private void addMapLocSymbolToInferredLocation(MethodDescriptor md, Descriptor localVar,
1428 CompositeLocation inferLoc) {
1430 Location locElement = inferLoc.get((inferLoc.getSize() - 1));
1431 Descriptor enclosingDesc = locElement.getDescriptor();
1432 LocationInfo locInfo = getLocationInfo(enclosingDesc);
1433 locInfo.addMapLocSymbolToRelatedInferLoc(locElement.getLocIdentifier(), md, localVar);
1436 private boolean isCompositeLocation(CompositeLocation cl) {
1437 return cl.getSize() > 1;
1440 private boolean containsNonPrimitiveElement(Set<Descriptor> descSet) {
1441 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
1442 Descriptor desc = (Descriptor) iterator.next();
1444 if (desc.equals(LocationInference.GLOBALDESC)) {
1446 } else if (desc instanceof VarDescriptor) {
1447 if (!((VarDescriptor) desc).getType().isPrimitive()) {
1450 } else if (desc instanceof FieldDescriptor) {
1451 if (!((FieldDescriptor) desc).getType().isPrimitive()) {
1460 private void addRelationHigherToLower(SSJavaLattice<String> lattice, LocationInfo locInfo,
1461 String higher, String lower) throws CyclicFlowException {
1463 System.out.println("---addRelationHigherToLower " + higher + " -> " + lower
1464 + " to the lattice of " + locInfo.getDescIdentifier());
1465 // if (higher.equals(lower) && lattice.isSharedLoc(higher)) {
1468 Set<String> cycleElementSet = lattice.getPossibleCycleElements(higher, lower);
1470 boolean hasNonPrimitiveElement = false;
1471 for (Iterator iterator = cycleElementSet.iterator(); iterator.hasNext();) {
1472 String cycleElementLocSymbol = (String) iterator.next();
1474 Set<Descriptor> descSet = locInfo.getDescSet(cycleElementLocSymbol);
1475 if (containsNonPrimitiveElement(descSet)) {
1476 hasNonPrimitiveElement = true;
1481 if (hasNonPrimitiveElement) {
1482 System.out.println("#Check cycle= " + lower + " < " + higher + " cycleElementSet="
1484 // if there is non-primitive element in the cycle, no way to merge cyclic
1485 // elements into the shared location
1486 throw new CyclicFlowException();
1489 if (cycleElementSet.size() > 0) {
1491 String newSharedLoc = "SharedLoc" + (SSJavaLattice.seed++);
1493 System.out.println("---ASSIGN NEW SHARED LOC=" + newSharedLoc + " to " + cycleElementSet);
1494 lattice.mergeIntoSharedLocation(cycleElementSet, newSharedLoc);
1496 for (Iterator iterator = cycleElementSet.iterator(); iterator.hasNext();) {
1497 String oldLocSymbol = (String) iterator.next();
1499 Set<Pair<Descriptor, Descriptor>> inferLocSet = locInfo.getRelatedInferLocSet(oldLocSymbol);
1500 System.out.println("---update related locations=" + inferLocSet);
1501 for (Iterator iterator2 = inferLocSet.iterator(); iterator2.hasNext();) {
1502 Pair<Descriptor, Descriptor> pair = (Pair<Descriptor, Descriptor>) iterator2.next();
1503 Descriptor enclosingDesc = pair.getFirst();
1504 Descriptor desc = pair.getSecond();
1506 CompositeLocation inferLoc;
1507 if (curMethodInfo.md.equals(enclosingDesc)) {
1508 inferLoc = curMethodInfo.getInferLocation(desc);
1510 inferLoc = getLocationInfo(enclosingDesc).getInferLocation(desc);
1513 Location locElement = inferLoc.get(inferLoc.getSize() - 1);
1515 locElement.setLocIdentifier(newSharedLoc);
1516 locInfo.addMapLocSymbolToRelatedInferLoc(newSharedLoc, enclosingDesc, desc);
1518 if (curMethodInfo.md.equals(enclosingDesc)) {
1519 inferLoc = curMethodInfo.getInferLocation(desc);
1521 inferLoc = getLocationInfo(enclosingDesc).getInferLocation(desc);
1523 System.out.println("---New Infer Loc=" + inferLoc);
1526 locInfo.removeRelatedInferLocSet(oldLocSymbol, newSharedLoc);
1530 lattice.addSharedLoc(newSharedLoc);
1532 } else if (!lattice.isGreaterThan(higher, lower)) {
1533 lattice.addRelationHigherToLower(higher, lower);
1537 private void replaceOldLocWithNewLoc(SSJavaLattice<String> methodLattice, String oldLocSymbol,
1538 String newLocSymbol) {
1540 if (methodLattice.containsKey(oldLocSymbol)) {
1541 methodLattice.substituteLocation(oldLocSymbol, newLocSymbol);
1546 private void prefixSanityCheck(List<NTuple<Location>> prefixList, int curIdx,
1547 FlowGraph flowGraph, Set<FlowNode> reachableNodeSet) {
1549 NTuple<Location> curPrefix = prefixList.get(curIdx);
1551 for (int i = curIdx + 1; i < prefixList.size(); i++) {
1552 NTuple<Location> prefixTuple = prefixList.get(i);
1554 if (curPrefix.startsWith(prefixTuple)) {
1558 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1559 FlowNode reachableNode = (FlowNode) iterator2.next();
1560 NTuple<Location> reachLocTuple = flowGraph.getLocationTuple(reachableNode);
1561 if (reachLocTuple.startsWith(prefixTuple)) {
1563 throw new Error("Failed to generate a composite location");
1569 public boolean isPrimitiveLocalVariable(FlowNode node) {
1570 VarDescriptor varDesc = (VarDescriptor) node.getDescTuple().get(0);
1571 return varDesc.getType().isPrimitive();
1574 private SSJavaLattice<String> getLattice(Descriptor d) {
1575 if (d instanceof MethodDescriptor) {
1576 return getMethodLattice((MethodDescriptor) d);
1578 return getFieldLattice((ClassDescriptor) d);
1582 private SSJavaLattice<String> getMethodLattice(MethodDescriptor md) {
1583 if (!md2lattice.containsKey(md)) {
1584 md2lattice.put(md, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
1586 return md2lattice.get(md);
1589 private void setMethodLattice(MethodDescriptor md, SSJavaLattice<String> lattice) {
1590 md2lattice.put(md, lattice);
1593 private void extractRelationFromFieldFlows(ClassDescriptor cd, FlowNode srcNode,
1594 FlowNode dstNode, int idx) throws CyclicFlowException {
1596 if (srcNode.getDescTuple().get(idx).equals(dstNode.getDescTuple().get(idx))
1597 && srcNode.getDescTuple().size() > (idx + 1) && dstNode.getDescTuple().size() > (idx + 1)) {
1598 // value flow between fields: we don't need to add a binary relation
1601 Descriptor desc = srcNode.getDescTuple().get(idx);
1602 ClassDescriptor classDesc;
1605 classDesc = ((VarDescriptor) desc).getType().getClassDesc();
1607 classDesc = ((FieldDescriptor) desc).getType().getClassDesc();
1610 extractRelationFromFieldFlows(classDesc, srcNode, dstNode, idx + 1);
1614 Descriptor srcFieldDesc = srcNode.getDescTuple().get(idx);
1615 Descriptor dstFieldDesc = dstNode.getDescTuple().get(idx);
1617 // add a new binary relation of dstNode < srcNode
1618 SSJavaLattice<String> fieldLattice = getFieldLattice(cd);
1619 LocationInfo fieldInfo = getFieldLocationInfo(cd);
1621 String srcSymbol = fieldInfo.getFieldInferLocation(srcFieldDesc).getLocIdentifier();
1622 String dstSymbol = fieldInfo.getFieldInferLocation(dstFieldDesc).getLocIdentifier();
1624 addRelationHigherToLower(fieldLattice, fieldInfo, srcSymbol, dstSymbol);
1630 public SSJavaLattice<String> getFieldLattice(ClassDescriptor cd) {
1631 if (!cd2lattice.containsKey(cd)) {
1632 cd2lattice.put(cd, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
1634 return cd2lattice.get(cd);
1637 public void constructFlowGraph() {
1641 Set<MethodDescriptor> visited = new HashSet<MethodDescriptor>();
1642 Set<MethodDescriptor> reachableCallee = new HashSet<MethodDescriptor>();
1644 while (!toAnalyzeIsEmpty()) {
1645 ClassDescriptor cd = toAnalyzeNext();
1647 setupToAnalazeMethod(cd);
1648 toanalyzeMethodList.removeAll(visited);
1650 while (!toAnalyzeMethodIsEmpty()) {
1651 MethodDescriptor md = toAnalyzeMethodNext();
1652 if ((!visited.contains(md))
1653 && (ssjava.needTobeAnnotated(md) || reachableCallee.contains(md))) {
1654 if (state.SSJAVADEBUG) {
1655 System.out.println();
1656 System.out.println("SSJAVA: Constructing a flow graph: " + md);
1659 // creates a mapping from a method descriptor to virtual methods
1660 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1661 if (md.isStatic()) {
1662 setPossibleCallees.add(md);
1664 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
1667 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getCalleeSet(md);
1668 Set<MethodDescriptor> needToAnalyzeCalleeSet = new HashSet<MethodDescriptor>();
1670 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
1671 MethodDescriptor calleemd = (MethodDescriptor) iterator.next();
1672 if ((!ssjava.isTrustMethod(calleemd))
1673 && (!ssjava.isSSJavaUtil(calleemd.getClassDesc()))) {
1674 if (!visited.contains(calleemd)) {
1675 toanalyzeMethodList.add(calleemd);
1677 reachableCallee.add(calleemd);
1678 needToAnalyzeCalleeSet.add(calleemd);
1682 mapMethodToCalleeSet.put(md, needToAnalyzeCalleeSet);
1684 // creates a mapping from a parameter descriptor to its index
1685 Map<Descriptor, Integer> mapParamDescToIdx = new HashMap<Descriptor, Integer>();
1686 int offset = md.isStatic() ? 0 : 1;
1687 for (int i = 0; i < md.numParameters(); i++) {
1688 Descriptor paramDesc = (Descriptor) md.getParameter(i);
1689 mapParamDescToIdx.put(paramDesc, new Integer(i + offset));
1692 FlowGraph fg = new FlowGraph(md, mapParamDescToIdx);
1693 mapMethodDescriptorToFlowGraph.put(md, fg);
1696 analyzeMethodBody(cd, md);
1702 _debug_printGraph();
1705 private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md) {
1706 BlockNode bn = state.getMethodBody(md);
1707 NodeTupleSet implicitFlowTupleSet = new NodeTupleSet();
1708 analyzeFlowBlockNode(md, md.getParameterTable(), bn, implicitFlowTupleSet);
1711 private void analyzeFlowBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn,
1712 NodeTupleSet implicitFlowTupleSet) {
1714 bn.getVarTable().setParent(nametable);
1715 for (int i = 0; i < bn.size(); i++) {
1716 BlockStatementNode bsn = bn.get(i);
1717 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
1722 private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
1723 BlockStatementNode bsn, NodeTupleSet implicitFlowTupleSet) {
1725 switch (bsn.kind()) {
1726 case Kind.BlockExpressionNode:
1727 analyzeBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn, implicitFlowTupleSet);
1730 case Kind.DeclarationNode:
1731 analyzeFlowDeclarationNode(md, nametable, (DeclarationNode) bsn, implicitFlowTupleSet);
1734 case Kind.IfStatementNode:
1735 analyzeFlowIfStatementNode(md, nametable, (IfStatementNode) bsn, implicitFlowTupleSet);
1739 analyzeFlowLoopNode(md, nametable, (LoopNode) bsn, implicitFlowTupleSet);
1742 case Kind.ReturnNode:
1743 analyzeFlowReturnNode(md, nametable, (ReturnNode) bsn, implicitFlowTupleSet);
1746 case Kind.SubBlockNode:
1747 analyzeFlowSubBlockNode(md, nametable, (SubBlockNode) bsn, implicitFlowTupleSet);
1750 case Kind.ContinueBreakNode:
1753 case Kind.SwitchStatementNode:
1754 analyzeSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn);
1761 private void analyzeSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
1762 SwitchStatementNode bsn) {
1763 // TODO Auto-generated method stub
1766 private void analyzeFlowSubBlockNode(MethodDescriptor md, SymbolTable nametable,
1767 SubBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
1768 analyzeFlowBlockNode(md, nametable, sbn.getBlockNode(), implicitFlowTupleSet);
1771 private void analyzeFlowReturnNode(MethodDescriptor md, SymbolTable nametable, ReturnNode rn,
1772 NodeTupleSet implicitFlowTupleSet) {
1774 ExpressionNode returnExp = rn.getReturnExpression();
1776 if (returnExp != null) {
1777 NodeTupleSet nodeSet = new NodeTupleSet();
1778 analyzeFlowExpressionNode(md, nametable, returnExp, nodeSet, false);
1780 FlowGraph fg = getFlowGraph(md);
1782 // annotate the elements of the node set as the return location
1783 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1784 NTuple<Descriptor> returnDescTuple = (NTuple<Descriptor>) iterator.next();
1785 fg.setReturnFlowNode(returnDescTuple);
1786 for (Iterator iterator2 = implicitFlowTupleSet.iterator(); iterator2.hasNext();) {
1787 NTuple<Descriptor> implicitFlowDescTuple = (NTuple<Descriptor>) iterator2.next();
1788 fg.addValueFlowEdge(implicitFlowDescTuple, returnDescTuple);
1795 private void analyzeFlowLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln,
1796 NodeTupleSet implicitFlowTupleSet) {
1798 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
1800 NodeTupleSet condTupleNode = new NodeTupleSet();
1801 analyzeFlowExpressionNode(md, nametable, ln.getCondition(), condTupleNode, null,
1802 implicitFlowTupleSet, false);
1803 condTupleNode.addTupleSet(implicitFlowTupleSet);
1805 // add edges from condNodeTupleSet to all nodes of conditional nodes
1806 analyzeFlowBlockNode(md, nametable, ln.getBody(), condTupleNode);
1809 // check 'for loop' case
1810 BlockNode bn = ln.getInitializer();
1811 bn.getVarTable().setParent(nametable);
1812 for (int i = 0; i < bn.size(); i++) {
1813 BlockStatementNode bsn = bn.get(i);
1814 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
1817 NodeTupleSet condTupleNode = new NodeTupleSet();
1818 analyzeFlowExpressionNode(md, bn.getVarTable(), ln.getCondition(), condTupleNode, null,
1819 implicitFlowTupleSet, false);
1820 condTupleNode.addTupleSet(implicitFlowTupleSet);
1822 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(), condTupleNode);
1823 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(), condTupleNode);
1829 private void analyzeFlowIfStatementNode(MethodDescriptor md, SymbolTable nametable,
1830 IfStatementNode isn, NodeTupleSet implicitFlowTupleSet) {
1832 NodeTupleSet condTupleNode = new NodeTupleSet();
1833 analyzeFlowExpressionNode(md, nametable, isn.getCondition(), condTupleNode, null,
1834 implicitFlowTupleSet, false);
1836 // add edges from condNodeTupleSet to all nodes of conditional nodes
1837 condTupleNode.addTupleSet(implicitFlowTupleSet);
1838 analyzeFlowBlockNode(md, nametable, isn.getTrueBlock(), condTupleNode);
1840 if (isn.getFalseBlock() != null) {
1841 analyzeFlowBlockNode(md, nametable, isn.getFalseBlock(), condTupleNode);
1846 private void analyzeFlowDeclarationNode(MethodDescriptor md, SymbolTable nametable,
1847 DeclarationNode dn, NodeTupleSet implicitFlowTupleSet) {
1849 VarDescriptor vd = dn.getVarDescriptor();
1850 mapDescToDefinitionLine.put(vd, dn.getNumLine());
1851 NTuple<Descriptor> tupleLHS = new NTuple<Descriptor>();
1853 FlowNode fn = getFlowGraph(md).createNewFlowNode(tupleLHS);
1854 fn.setDeclarationNode();
1856 if (dn.getExpression() != null) {
1858 NodeTupleSet tupleSetRHS = new NodeTupleSet();
1859 analyzeFlowExpressionNode(md, nametable, dn.getExpression(), tupleSetRHS, null,
1860 implicitFlowTupleSet, false);
1862 // add a new flow edge from rhs to lhs
1863 for (Iterator<NTuple<Descriptor>> iter = tupleSetRHS.iterator(); iter.hasNext();) {
1864 NTuple<Descriptor> from = iter.next();
1865 addFlowGraphEdge(md, from, tupleLHS);
1872 private void analyzeBlockExpressionNode(MethodDescriptor md, SymbolTable nametable,
1873 BlockExpressionNode ben, NodeTupleSet implicitFlowTupleSet) {
1874 analyzeFlowExpressionNode(md, nametable, ben.getExpression(), null, null, implicitFlowTupleSet,
1878 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
1879 ExpressionNode en, NodeTupleSet nodeSet, boolean isLHS) {
1880 return analyzeFlowExpressionNode(md, nametable, en, nodeSet, null, new NodeTupleSet(), isLHS);
1883 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
1884 ExpressionNode en, NodeTupleSet nodeSet, NTuple<Descriptor> base,
1885 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
1887 // note that expression node can create more than one flow node
1888 // nodeSet contains of flow nodes
1889 // base is always assigned to null except the case of a name node!
1891 NTuple<Descriptor> flowTuple;
1893 switch (en.kind()) {
1895 case Kind.AssignmentNode:
1896 analyzeFlowAssignmentNode(md, nametable, (AssignmentNode) en, nodeSet, base,
1897 implicitFlowTupleSet);
1900 case Kind.FieldAccessNode:
1902 analyzeFlowFieldAccessNode(md, nametable, (FieldAccessNode) en, nodeSet, base,
1903 implicitFlowTupleSet, isLHS);
1904 if (flowTuple != null) {
1905 nodeSet.addTuple(flowTuple);
1910 NodeTupleSet nameNodeSet = new NodeTupleSet();
1912 analyzeFlowNameNode(md, nametable, (NameNode) en, nameNodeSet, base, implicitFlowTupleSet);
1913 if (flowTuple != null) {
1914 nodeSet.addTuple(flowTuple);
1919 analyzeFlowOpNode(md, nametable, (OpNode) en, nodeSet, implicitFlowTupleSet);
1922 case Kind.CreateObjectNode:
1923 analyzeCreateObjectNode(md, nametable, (CreateObjectNode) en);
1926 case Kind.ArrayAccessNode:
1927 analyzeFlowArrayAccessNode(md, nametable, (ArrayAccessNode) en, nodeSet, isLHS);
1930 case Kind.LiteralNode:
1931 analyzeLiteralNode(md, nametable, (LiteralNode) en);
1934 case Kind.MethodInvokeNode:
1935 analyzeFlowMethodInvokeNode(md, nametable, (MethodInvokeNode) en, implicitFlowTupleSet);
1938 case Kind.TertiaryNode:
1939 analyzeFlowTertiaryNode(md, nametable, (TertiaryNode) en, nodeSet, implicitFlowTupleSet);
1943 analyzeFlowCastNode(md, nametable, (CastNode) en, nodeSet, base, implicitFlowTupleSet);
1945 // case Kind.InstanceOfNode:
1946 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
1949 // case Kind.ArrayInitializerNode:
1950 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
1954 // case Kind.ClassTypeNode:
1955 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
1958 // case Kind.OffsetNode:
1959 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
1967 private void analyzeFlowCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn,
1968 NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
1970 analyzeFlowExpressionNode(md, nametable, cn.getExpression(), nodeSet, base,
1971 implicitFlowTupleSet, false);
1975 private void analyzeFlowTertiaryNode(MethodDescriptor md, SymbolTable nametable, TertiaryNode tn,
1976 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
1978 NodeTupleSet tertiaryTupleNode = new NodeTupleSet();
1979 analyzeFlowExpressionNode(md, nametable, tn.getCond(), tertiaryTupleNode, null,
1980 implicitFlowTupleSet, false);
1982 // add edges from tertiaryTupleNode to all nodes of conditional nodes
1983 tertiaryTupleNode.addTupleSet(implicitFlowTupleSet);
1984 analyzeFlowExpressionNode(md, nametable, tn.getTrueExpr(), tertiaryTupleNode, null,
1985 implicitFlowTupleSet, false);
1987 analyzeFlowExpressionNode(md, nametable, tn.getFalseExpr(), tertiaryTupleNode, null,
1988 implicitFlowTupleSet, false);
1990 nodeSet.addTupleSet(tertiaryTupleNode);
1994 private void addMapCallerMethodDescToMethodInvokeNodeSet(MethodDescriptor caller,
1995 MethodInvokeNode min) {
1996 Set<MethodInvokeNode> set = mapMethodDescriptorToMethodInvokeNodeSet.get(caller);
1998 set = new HashSet<MethodInvokeNode>();
1999 mapMethodDescriptorToMethodInvokeNodeSet.put(caller, set);
2004 private void analyzeFlowMethodInvokeNode(MethodDescriptor md, SymbolTable nametable,
2005 MethodInvokeNode min, NodeTupleSet implicitFlowTupleSet) {
2007 addMapCallerMethodDescToMethodInvokeNodeSet(md, min);
2009 MethodDescriptor calleeMD = min.getMethod();
2011 NameDescriptor baseName = min.getBaseName();
2012 boolean isSystemout = false;
2013 if (baseName != null) {
2014 isSystemout = baseName.getSymbol().equals("System.out");
2017 if (!ssjava.isSSJavaUtil(calleeMD.getClassDesc()) && !ssjava.isTrustMethod(calleeMD)
2018 && !calleeMD.getModifiers().isNative() && !isSystemout) {
2020 // CompositeLocation baseLocation = null;
2021 if (min.getExpression() != null) {
2023 NodeTupleSet baseNodeSet = new NodeTupleSet();
2024 analyzeFlowExpressionNode(md, nametable, min.getExpression(), baseNodeSet, null,
2025 implicitFlowTupleSet, false);
2028 if (min.getMethod().isStatic()) {
2029 // String globalLocId = ssjava.getMethodLattice(md).getGlobalLoc();
2030 // if (globalLocId == null) {
2032 // Error("Method lattice does not define global variable location at "
2033 // + generateErrorMessage(md.getClassDesc(), min));
2035 // baseLocation = new CompositeLocation(new Location(md,
2039 // String thisLocId = ssjava.getMethodLattice(md).getThisLoc();
2040 // baseLocation = new CompositeLocation(new Location(md, thisLocId));
2045 // if (constraint != null) {
2046 // int compareResult =
2047 // CompositeLattice.compare(constraint, baseLocation, true,
2048 // generateErrorMessage(cd, min));
2049 // if (compareResult != ComparisonResult.GREATER) {
2050 // // if the current constraint is higher than method's THIS location
2051 // // no need to check constraints!
2052 // CompositeLocation calleeConstraint =
2053 // translateCallerLocToCalleeLoc(calleeMD, baseLocation, constraint);
2054 // // System.out.println("check method body for constraint:" + calleeMD +
2055 // // " calleeConstraint="
2056 // // + calleeConstraint);
2057 // checkMethodBody(calleeMD.getClassDesc(), calleeMD, calleeConstraint);
2061 analyzeFlowMethodParameters(md, nametable, min);
2063 // checkCalleeConstraints(md, nametable, min, baseLocation, constraint);
2065 // checkCallerArgumentLocationConstraints(md, nametable, min,
2066 // baseLocation, constraint);
2068 if (min.getMethod().getReturnType() != null && !min.getMethod().getReturnType().isVoid()) {
2069 // If method has a return value, compute the highest possible return
2070 // location in the caller's perspective
2071 // CompositeLocation ceilingLoc =
2072 // computeCeilingLocationForCaller(md, nametable, min, baseLocation,
2074 // return ceilingLoc;
2078 // return new CompositeLocation(Location.createTopLocation(md));
2082 private NodeTupleSet getNodeTupleSetByArgIdx(MethodInvokeNode min, int idx) {
2083 return mapMethodInvokeNodeToArgIdxMap.get(min).get(new Integer(idx));
2086 private void addArgIdxMap(MethodInvokeNode min, int idx, NodeTupleSet tupleSet) {
2087 Map<Integer, NodeTupleSet> mapIdxToTupleSet = mapMethodInvokeNodeToArgIdxMap.get(min);
2088 if (mapIdxToTupleSet == null) {
2089 mapIdxToTupleSet = new HashMap<Integer, NodeTupleSet>();
2090 mapMethodInvokeNodeToArgIdxMap.put(min, mapIdxToTupleSet);
2092 mapIdxToTupleSet.put(new Integer(idx), tupleSet);
2095 private void analyzeFlowMethodParameters(MethodDescriptor callermd, SymbolTable nametable,
2096 MethodInvokeNode min) {
2098 if (min.numArgs() > 0) {
2101 if (min.getMethod().isStatic()) {
2105 NTuple<Descriptor> thisArgTuple = new NTuple<Descriptor>();
2106 thisArgTuple.add(callermd.getThis());
2107 NodeTupleSet argTupleSet = new NodeTupleSet();
2108 argTupleSet.addTuple(thisArgTuple);
2109 addArgIdxMap(min, 0, argTupleSet);
2112 for (int i = 0; i < min.numArgs(); i++) {
2113 ExpressionNode en = min.getArg(i);
2114 NodeTupleSet argTupleSet = new NodeTupleSet();
2115 analyzeFlowExpressionNode(callermd, nametable, en, argTupleSet, false);
2116 // if argument is liternal node, argTuple is set to NULL.
2117 addArgIdxMap(min, i + offset, argTupleSet);
2124 private void analyzeLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode en) {
2128 private void analyzeFlowArrayAccessNode(MethodDescriptor md, SymbolTable nametable,
2129 ArrayAccessNode aan, NodeTupleSet nodeSet, boolean isLHS) {
2131 NodeTupleSet expNodeTupleSet = new NodeTupleSet();
2132 analyzeFlowExpressionNode(md, nametable, aan.getExpression(), expNodeTupleSet, isLHS);
2134 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
2135 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, isLHS);
2138 // need to create an edge from idx to array
2139 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
2140 NTuple<Descriptor> idxTuple = idxIter.next();
2141 for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
2142 NTuple<Descriptor> arrTuple = arrIter.next();
2143 getFlowGraph(md).addValueFlowEdge(idxTuple, arrTuple);
2147 nodeSet.addTupleSet(expNodeTupleSet);
2149 nodeSet.addTupleSet(expNodeTupleSet);
2150 nodeSet.addTupleSet(idxNodeTupleSet);
2154 private void analyzeCreateObjectNode(MethodDescriptor md, SymbolTable nametable,
2155 CreateObjectNode en) {
2156 // TODO Auto-generated method stub
2160 private void analyzeFlowOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on,
2161 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
2163 NodeTupleSet leftOpSet = new NodeTupleSet();
2164 NodeTupleSet rightOpSet = new NodeTupleSet();
2167 analyzeFlowExpressionNode(md, nametable, on.getLeft(), leftOpSet, null, implicitFlowTupleSet,
2170 if (on.getRight() != null) {
2172 analyzeFlowExpressionNode(md, nametable, on.getRight(), rightOpSet, null,
2173 implicitFlowTupleSet, false);
2176 Operation op = on.getOp();
2178 switch (op.getOp()) {
2180 case Operation.UNARYPLUS:
2181 case Operation.UNARYMINUS:
2182 case Operation.LOGIC_NOT:
2184 nodeSet.addTupleSet(leftOpSet);
2187 case Operation.LOGIC_OR:
2188 case Operation.LOGIC_AND:
2189 case Operation.COMP:
2190 case Operation.BIT_OR:
2191 case Operation.BIT_XOR:
2192 case Operation.BIT_AND:
2193 case Operation.ISAVAILABLE:
2194 case Operation.EQUAL:
2195 case Operation.NOTEQUAL:
2202 case Operation.MULT:
2205 case Operation.LEFTSHIFT:
2206 case Operation.RIGHTSHIFT:
2207 case Operation.URIGHTSHIFT:
2209 // there are two operands
2210 nodeSet.addTupleSet(leftOpSet);
2211 nodeSet.addTupleSet(rightOpSet);
2215 throw new Error(op.toString());
2220 private NTuple<Descriptor> analyzeFlowNameNode(MethodDescriptor md, SymbolTable nametable,
2221 NameNode nn, NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
2224 base = new NTuple<Descriptor>();
2227 NameDescriptor nd = nn.getName();
2229 if (nd.getBase() != null) {
2231 analyzeFlowExpressionNode(md, nametable, nn.getExpression(), nodeSet, base,
2232 implicitFlowTupleSet, false);
2234 // base node has the top location
2238 String varname = nd.toString();
2239 if (varname.equals("this")) {
2241 base.add(md.getThis());
2245 Descriptor d = (Descriptor) nametable.get(varname);
2247 if (d instanceof VarDescriptor) {
2248 VarDescriptor vd = (VarDescriptor) d;
2250 } else if (d instanceof FieldDescriptor) {
2251 // the type of field descriptor has a location!
2252 FieldDescriptor fd = (FieldDescriptor) d;
2253 if (fd.isStatic()) {
2255 // if it is 'static final', no need to have flow node for the TOP
2259 // if 'static', assign the default GLOBAL LOCATION to the first
2260 // element of the tuple
2261 base.add(GLOBALDESC);
2264 // the location of field access starts from this, followed by field
2266 base.add(md.getThis());
2270 } else if (d == null) {
2271 // access static field
2272 base.add(GLOBALDESC);
2273 // base.add(nn.getField());
2276 // FieldDescriptor fd = nn.getField();addFlowGraphEdge
2278 // MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
2279 // String globalLocId = localLattice.getGlobalLoc();
2280 // if (globalLocId == null) {
2282 // Error("Method lattice does not define global variable location at "
2283 // + generateErrorMessage(md.getClassDesc(), nn));
2285 // loc.addLocation(new Location(md, globalLocId));
2287 // Location fieldLoc = (Location) fd.getType().getExtension();
2288 // loc.addLocation(fieldLoc);
2295 getFlowGraph(md).createNewFlowNode(base);
2301 private NTuple<Descriptor> analyzeFlowFieldAccessNode(MethodDescriptor md, SymbolTable nametable,
2302 FieldAccessNode fan, NodeTupleSet nodeSet, NTuple<Descriptor> base,
2303 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
2305 ExpressionNode left = fan.getExpression();
2306 TypeDescriptor ltd = left.getType();
2307 FieldDescriptor fd = fan.getField();
2309 String varName = null;
2310 if (left.kind() == Kind.NameNode) {
2311 NameDescriptor nd = ((NameNode) left).getName();
2312 varName = nd.toString();
2315 if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) {
2316 // using a class name directly or access using this
2317 if (fd.isStatic() && fd.isFinal()) {
2322 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
2323 if (left instanceof ArrayAccessNode) {
2325 ArrayAccessNode aan = (ArrayAccessNode) left;
2326 left = aan.getExpression();
2327 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, base,
2328 implicitFlowTupleSet, isLHS);
2329 nodeSet.addTupleSet(idxNodeTupleSet);
2332 analyzeFlowExpressionNode(md, nametable, left, nodeSet, base, implicitFlowTupleSet, isLHS);
2335 // in this case, field is TOP location
2339 NTuple<Descriptor> flowFieldTuple = new NTuple<Descriptor>(base.toList());
2341 if (!left.getType().isPrimitive()) {
2343 if (!fd.getSymbol().equals("length")) {
2344 // array.length access, just have the location of the array
2345 flowFieldTuple.add(fd);
2346 nodeSet.removeTuple(base);
2350 getFlowGraph(md).createNewFlowNode(flowFieldTuple);
2353 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
2354 NTuple<Descriptor> idxTuple = idxIter.next();
2355 getFlowGraph(md).addValueFlowEdge(idxTuple, flowFieldTuple);
2359 return flowFieldTuple;
2365 private void debug_printTreeNode(TreeNode tn) {
2367 System.out.println("DEBUG: " + tn.printNode(0) + " line#=" + tn.getNumLine());
2371 private void analyzeFlowAssignmentNode(MethodDescriptor md, SymbolTable nametable,
2372 AssignmentNode an, NodeTupleSet nodeSet, NTuple<Descriptor> base,
2373 NodeTupleSet implicitFlowTupleSet) {
2375 NodeTupleSet nodeSetRHS = new NodeTupleSet();
2376 NodeTupleSet nodeSetLHS = new NodeTupleSet();
2378 boolean postinc = true;
2379 if (an.getOperation().getBaseOp() == null
2380 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
2381 .getBaseOp().getOp() != Operation.POSTDEC)) {
2384 // if LHS is array access node, need to capture value flows between an array
2385 // and its index value
2386 analyzeFlowExpressionNode(md, nametable, an.getDest(), nodeSetLHS, null, implicitFlowTupleSet,
2390 // analyze value flows of rhs expression
2391 analyzeFlowExpressionNode(md, nametable, an.getSrc(), nodeSetRHS, null, implicitFlowTupleSet,
2394 // System.out.println("-analyzeFlowAssignmentNode=" + an.printNode(0));
2395 // System.out.println("-nodeSetLHS=" + nodeSetLHS);
2396 // System.out.println("-nodeSetRHS=" + nodeSetRHS);
2397 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
2398 // System.out.println("-");
2400 if (an.getOperation().getOp() >= 2 && an.getOperation().getOp() <= 12) {
2401 // if assignment contains OP+EQ operator, creates edges from LHS to LHS
2402 for (Iterator<NTuple<Descriptor>> iter = nodeSetLHS.iterator(); iter.hasNext();) {
2403 NTuple<Descriptor> fromTuple = iter.next();
2404 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2405 NTuple<Descriptor> toTuple = iter2.next();
2406 addFlowGraphEdge(md, fromTuple, toTuple);
2411 // creates edges from RHS to LHS
2412 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
2413 NTuple<Descriptor> fromTuple = iter.next();
2414 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2415 NTuple<Descriptor> toTuple = iter2.next();
2416 addFlowGraphEdge(md, fromTuple, toTuple);
2420 // creates edges from implicitFlowTupleSet to LHS
2421 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
2422 NTuple<Descriptor> fromTuple = iter.next();
2423 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2424 NTuple<Descriptor> toTuple = iter2.next();
2425 addFlowGraphEdge(md, fromTuple, toTuple);
2431 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2432 NTuple<Descriptor> tuple = iter2.next();
2433 addFlowGraphEdge(md, tuple, tuple);
2436 // creates edges from implicitFlowTupleSet to LHS
2437 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
2438 NTuple<Descriptor> fromTuple = iter.next();
2439 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2440 NTuple<Descriptor> toTuple = iter2.next();
2441 addFlowGraphEdge(md, fromTuple, toTuple);
2447 if (nodeSet != null) {
2448 nodeSet.addTupleSet(nodeSetLHS);
2452 public FlowGraph getFlowGraph(MethodDescriptor md) {
2453 return mapMethodDescriptorToFlowGraph.get(md);
2456 private boolean addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
2457 NTuple<Descriptor> to) {
2459 // return true if it adds a new edge
2460 FlowGraph graph = getFlowGraph(md);
2461 graph.addValueFlowEdge(from, to);
2465 public void _debug_printGraph() {
2466 Set<MethodDescriptor> keySet = mapMethodDescriptorToFlowGraph.keySet();
2468 for (Iterator<MethodDescriptor> iterator = keySet.iterator(); iterator.hasNext();) {
2469 MethodDescriptor md = (MethodDescriptor) iterator.next();
2470 FlowGraph fg = mapMethodDescriptorToFlowGraph.get(md);
2473 } catch (IOException e) {
2474 e.printStackTrace();
2482 class CyclicFlowException extends Exception {