1 package Analysis.SSJava;
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Collection;
6 import java.util.Collections;
7 import java.util.Comparator;
8 import java.util.HashMap;
9 import java.util.HashSet;
10 import java.util.Iterator;
11 import java.util.LinkedList;
12 import java.util.List;
15 import java.util.Stack;
17 import IR.ClassDescriptor;
19 import IR.FieldDescriptor;
20 import IR.MethodDescriptor;
21 import IR.NameDescriptor;
24 import IR.SymbolTable;
25 import IR.TypeDescriptor;
26 import IR.VarDescriptor;
27 import IR.Tree.ArrayAccessNode;
28 import IR.Tree.AssignmentNode;
29 import IR.Tree.BlockExpressionNode;
30 import IR.Tree.BlockNode;
31 import IR.Tree.BlockStatementNode;
32 import IR.Tree.CastNode;
33 import IR.Tree.CreateObjectNode;
34 import IR.Tree.DeclarationNode;
35 import IR.Tree.ExpressionNode;
36 import IR.Tree.FieldAccessNode;
37 import IR.Tree.IfStatementNode;
39 import IR.Tree.LiteralNode;
40 import IR.Tree.LoopNode;
41 import IR.Tree.MethodInvokeNode;
42 import IR.Tree.NameNode;
43 import IR.Tree.OpNode;
44 import IR.Tree.ReturnNode;
45 import IR.Tree.SubBlockNode;
46 import IR.Tree.SwitchStatementNode;
47 import IR.Tree.TertiaryNode;
48 import IR.Tree.TreeNode;
50 public class LocationInference {
53 SSJavaAnalysis ssjava;
55 List<ClassDescriptor> toanalyzeList;
56 List<MethodDescriptor> toanalyzeMethodList;
57 Map<MethodDescriptor, FlowGraph> mapMethodDescriptorToFlowGraph;
59 // map a method descriptor to its set of parameter descriptors
60 Map<MethodDescriptor, Set<Descriptor>> mapMethodDescriptorToParamDescSet;
62 // keep current descriptors to visit in fixed-point interprocedural analysis,
63 private Stack<MethodDescriptor> methodDescriptorsToVisitStack;
65 // map a class descriptor to a field lattice
66 private Map<ClassDescriptor, SSJavaLattice<String>> cd2lattice;
68 // map a method descriptor to a method lattice
69 private Map<MethodDescriptor, SSJavaLattice<String>> md2lattice;
71 // map a method descriptor to the set of method invocation nodes which are
72 // invoked by the method descriptor
73 private Map<MethodDescriptor, Set<MethodInvokeNode>> mapMethodDescriptorToMethodInvokeNodeSet;
75 private Map<MethodInvokeNode, Map<Integer, NodeTupleSet>> mapMethodInvokeNodeToArgIdxMap;
77 private Map<MethodDescriptor, MethodLocationInfo> mapMethodDescToMethodLocationInfo;
79 private Map<ClassDescriptor, LocationInfo> mapClassToLocationInfo;
81 private Map<MethodDescriptor, Set<MethodDescriptor>> mapMethodToCalleeSet;
83 public static final String GLOBALLOC = "GLOBALLOC";
85 public static final String TOPLOC = "TOPLOC";
87 public static final Descriptor GLOBALDESC = new NameDescriptor(GLOBALLOC);
89 public static final Descriptor TOPDESC = new NameDescriptor(TOPLOC);
93 public LocationInference(SSJavaAnalysis ssjava, State state) {
96 this.toanalyzeList = new ArrayList<ClassDescriptor>();
97 this.toanalyzeMethodList = new ArrayList<MethodDescriptor>();
98 this.mapMethodDescriptorToFlowGraph = new HashMap<MethodDescriptor, FlowGraph>();
99 this.cd2lattice = new HashMap<ClassDescriptor, SSJavaLattice<String>>();
100 this.md2lattice = new HashMap<MethodDescriptor, SSJavaLattice<String>>();
101 this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
102 this.mapMethodDescriptorToMethodInvokeNodeSet =
103 new HashMap<MethodDescriptor, Set<MethodInvokeNode>>();
104 this.mapMethodInvokeNodeToArgIdxMap =
105 new HashMap<MethodInvokeNode, Map<Integer, NodeTupleSet>>();
106 this.mapMethodDescToMethodLocationInfo = new HashMap<MethodDescriptor, MethodLocationInfo>();
107 this.mapMethodToCalleeSet = new HashMap<MethodDescriptor, Set<MethodDescriptor>>();
108 this.mapClassToLocationInfo = new HashMap<ClassDescriptor, LocationInfo>();
111 public void setupToAnalyze() {
112 SymbolTable classtable = state.getClassSymbolTable();
113 toanalyzeList.clear();
114 toanalyzeList.addAll(classtable.getValueSet());
115 Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
116 public int compare(ClassDescriptor o1, ClassDescriptor o2) {
117 return o1.getClassName().compareToIgnoreCase(o2.getClassName());
122 public void setupToAnalazeMethod(ClassDescriptor cd) {
124 SymbolTable methodtable = cd.getMethodTable();
125 toanalyzeMethodList.clear();
126 toanalyzeMethodList.addAll(methodtable.getValueSet());
127 Collections.sort(toanalyzeMethodList, new Comparator<MethodDescriptor>() {
128 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
129 return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
134 public boolean toAnalyzeMethodIsEmpty() {
135 return toanalyzeMethodList.isEmpty();
138 public boolean toAnalyzeIsEmpty() {
139 return toanalyzeList.isEmpty();
142 public ClassDescriptor toAnalyzeNext() {
143 return toanalyzeList.remove(0);
146 public MethodDescriptor toAnalyzeMethodNext() {
147 return toanalyzeMethodList.remove(0);
150 public void inference() {
152 // 1) construct value flow graph
153 constructFlowGraph();
155 // 2) construct lattices
160 debug_writeLatticeDotFile();
162 // 3) check properties
167 private void simplifyLattices() {
169 // generate lattice dot file
172 while (!toAnalyzeIsEmpty()) {
173 ClassDescriptor cd = toAnalyzeNext();
175 setupToAnalazeMethod(cd);
177 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
178 if (classLattice != null) {
179 classLattice.removeRedundantEdges();
182 while (!toAnalyzeMethodIsEmpty()) {
183 MethodDescriptor md = toAnalyzeMethodNext();
184 if (ssjava.needTobeAnnotated(md)) {
185 SSJavaLattice<String> methodLattice = md2lattice.get(md);
186 if (methodLattice != null) {
187 methodLattice.removeRedundantEdges();
195 private void checkLattices() {
197 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
199 // current descriptors to visit in fixed-point interprocedural analysis,
201 // dependency in the call graph
202 methodDescriptorsToVisitStack.clear();
204 descriptorListToAnalyze.removeFirst();
206 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
207 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
209 while (!descriptorListToAnalyze.isEmpty()) {
210 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
211 checkLatticesOfVirtualMethods(md);
216 private void debug_writeLatticeDotFile() {
217 // generate lattice dot file
221 while (!toAnalyzeIsEmpty()) {
222 ClassDescriptor cd = toAnalyzeNext();
224 setupToAnalazeMethod(cd);
226 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
227 if (classLattice != null) {
228 ssjava.writeLatticeDotFile(cd, null, classLattice);
229 debug_printDescriptorToLocNameMapping(cd);
232 while (!toAnalyzeMethodIsEmpty()) {
233 MethodDescriptor md = toAnalyzeMethodNext();
234 if (ssjava.needTobeAnnotated(md)) {
235 SSJavaLattice<String> methodLattice = md2lattice.get(md);
236 if (methodLattice != null) {
237 ssjava.writeLatticeDotFile(cd, md, methodLattice);
238 debug_printDescriptorToLocNameMapping(md);
246 private void debug_printDescriptorToLocNameMapping(Descriptor desc) {
248 LocationInfo info = getLocationInfo(desc);
249 System.out.println("## " + desc + " ##");
250 System.out.println(info.getMapDescToInferLocation());
251 LocationInfo locInfo = getLocationInfo(desc);
252 System.out.println("mapping=" + locInfo.getMapLocSymbolToDescSet());
253 System.out.println("###################");
257 private void inferLattices() {
259 // do fixed-point analysis
261 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
263 Collections.sort(descriptorListToAnalyze, new Comparator<MethodDescriptor>() {
264 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
265 return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
269 // current descriptors to visit in fixed-point interprocedural analysis,
271 // dependency in the call graph
272 methodDescriptorsToVisitStack.clear();
274 descriptorListToAnalyze.removeFirst();
276 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
277 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
279 while (!descriptorListToAnalyze.isEmpty()) {
280 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
281 methodDescriptorsToVisitStack.add(md);
284 // analyze scheduled methods until there are no more to visit
285 while (!methodDescriptorsToVisitStack.isEmpty()) {
286 // start to analyze leaf node
287 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
289 SSJavaLattice<String> methodLattice =
290 new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM);
292 MethodLocationInfo methodInfo = new MethodLocationInfo(md);
294 System.out.println();
295 System.out.println("SSJAVA: Inferencing the lattice from " + md);
298 analyzeMethodLattice(md, methodLattice, methodInfo);
299 } catch (CyclicFlowException e) {
300 throw new Error("Fail to generate the method lattice for " + md);
303 SSJavaLattice<String> prevMethodLattice = getMethodLattice(md);
304 MethodLocationInfo prevMethodInfo = getMethodLocationInfo(md);
306 if ((!methodLattice.equals(prevMethodLattice)) || (!methodInfo.equals(prevMethodInfo))) {
308 setMethodLattice(md, methodLattice);
309 setMethodLocInfo(md, methodInfo);
311 // results for callee changed, so enqueue dependents caller for
313 Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
314 while (depsItr.hasNext()) {
315 MethodDescriptor methodNext = depsItr.next();
316 if (!methodDescriptorsToVisitStack.contains(methodNext)
317 && methodDescriptorToVistSet.contains(methodNext)) {
318 methodDescriptorsToVisitStack.add(methodNext);
327 private void setMethodLocInfo(MethodDescriptor md, MethodLocationInfo methodInfo) {
328 mapMethodDescToMethodLocationInfo.put(md, methodInfo);
331 private void checkLatticesOfVirtualMethods(MethodDescriptor md) {
333 if (!md.isStatic()) {
334 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
335 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
337 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
338 MethodDescriptor mdCallee = (MethodDescriptor) iterator.next();
339 if (!md.equals(mdCallee)) {
340 checkConsistency(md, mdCallee);
348 private void checkConsistency(MethodDescriptor md1, MethodDescriptor md2) {
350 // check that two lattice have the same relations between parameters(+PC
351 // LOC, GLOBAL_LOC RETURN LOC)
353 List<CompositeLocation> list1 = new ArrayList<CompositeLocation>();
354 List<CompositeLocation> list2 = new ArrayList<CompositeLocation>();
356 MethodLocationInfo locInfo1 = getMethodLocationInfo(md1);
357 MethodLocationInfo locInfo2 = getMethodLocationInfo(md2);
359 Map<Integer, CompositeLocation> paramMap1 = locInfo1.getMapParamIdxToInferLoc();
360 Map<Integer, CompositeLocation> paramMap2 = locInfo2.getMapParamIdxToInferLoc();
362 int numParam = locInfo1.getMapParamIdxToInferLoc().keySet().size();
364 // add location types of paramters
365 for (int idx = 0; idx < numParam; idx++) {
366 list1.add(paramMap1.get(Integer.valueOf(idx)));
367 list2.add(paramMap2.get(Integer.valueOf(idx)));
370 // add program counter location
371 list1.add(locInfo1.getPCLoc());
372 list2.add(locInfo2.getPCLoc());
374 if (!md1.getReturnType().isVoid()) {
375 // add return value location
376 CompositeLocation rtrLoc1 =
377 new CompositeLocation(new Location(md1, locInfo1.getReturnLocName()));
378 CompositeLocation rtrLoc2 =
379 new CompositeLocation(new Location(md2, locInfo2.getReturnLocName()));
384 // add global location type
385 if (md1.isStatic()) {
386 CompositeLocation globalLoc1 =
387 new CompositeLocation(new Location(md1, locInfo1.getGlobalLocName()));
388 CompositeLocation globalLoc2 =
389 new CompositeLocation(new Location(md2, locInfo2.getGlobalLocName()));
390 list1.add(globalLoc1);
391 list2.add(globalLoc2);
394 for (int i = 0; i < list1.size(); i++) {
395 CompositeLocation locA1 = list1.get(i);
396 CompositeLocation locA2 = list2.get(i);
397 for (int k = 0; k < list1.size(); k++) {
399 CompositeLocation locB1 = list1.get(k);
400 CompositeLocation locB2 = list2.get(k);
401 boolean r1 = isGreaterThan(getLattice(md1), locA1, locB1);
403 boolean r2 = isGreaterThan(getLattice(md1), locA2, locB2);
406 throw new Error("The method " + md1 + " is not consistent with the method " + md2
407 + ".:: They have a different ordering relation between locations (" + locA1 + ","
408 + locB1 + ") and (" + locA2 + "," + locB2 + ").");
416 private String getSymbol(int idx, FlowNode node) {
417 Descriptor desc = node.getDescTuple().get(idx);
418 return desc.getSymbol();
421 private Descriptor getDescriptor(int idx, FlowNode node) {
422 Descriptor desc = node.getDescTuple().get(idx);
426 private void analyzeMethodLattice(MethodDescriptor md, SSJavaLattice<String> methodLattice,
427 MethodLocationInfo methodInfo) throws CyclicFlowException {
429 // first take a look at method invocation nodes to newly added relations
431 analyzeLatticeMethodInvocationNode(md, methodLattice, methodInfo);
433 if (!md.isStatic()) {
434 // set the this location
435 String thisLocSymbol = md.getThis().getSymbol();
436 methodInfo.setThisLocName(thisLocSymbol);
439 // set the global location
440 methodInfo.setGlobalLocName(LocationInference.GLOBALLOC);
442 // visit each node of method flow graph
443 FlowGraph fg = getFlowGraph(md);
444 Set<FlowNode> nodeSet = fg.getNodeSet();
446 // for the method lattice, we need to look at the first element of
447 // NTuple<Descriptor>
448 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
449 FlowNode srcNode = (FlowNode) iterator.next();
451 Set<FlowEdge> outEdgeSet = srcNode.getOutEdgeSet();
452 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
453 FlowEdge outEdge = (FlowEdge) iterator2.next();
454 FlowNode dstNode = outEdge.getDst();
456 NTuple<Descriptor> srcNodeTuple = srcNode.getDescTuple();
457 NTuple<Descriptor> dstNodeTuple = dstNode.getDescTuple();
459 if (outEdge.getInitTuple().equals(srcNodeTuple)
460 && outEdge.getEndTuple().equals(dstNodeTuple)) {
462 if ((srcNodeTuple.size() > 1 && dstNodeTuple.size() > 1)
463 && srcNodeTuple.get(0).equals(dstNodeTuple.get(0))) {
465 // value flows between fields
466 VarDescriptor varDesc = (VarDescriptor) srcNodeTuple.get(0);
467 ClassDescriptor varClassDesc = varDesc.getType().getClassDesc();
468 extractRelationFromFieldFlows(varClassDesc, srcNode, dstNode, 1);
470 } else if (srcNodeTuple.size() == 1 || dstNodeTuple.size() == 1) {
471 // for the method lattice, we need to look at the first element of
472 // NTuple<Descriptor>
473 // in this case, take a look at connected nodes at the local level
474 addRelationToLattice(md, methodLattice, methodInfo, srcNode, dstNode);
477 if (!srcNode.getDescTuple().get(0).equals(dstNode.getDescTuple().get(0))) {
478 // in this case, take a look at connected nodes at the local level
479 addRelationToLattice(md, methodLattice, methodInfo, srcNode, dstNode);
481 Descriptor srcDesc = srcNode.getDescTuple().get(0);
482 Descriptor dstDesc = dstNode.getDescTuple().get(0);
483 recursivelyAddCompositeRelation(md, fg, methodInfo, srcNode, dstNode, srcDesc,
485 // recursiveAddRelationToLattice(1, md, srcNode, dstNode);
493 // create mapping from param idx to inferred composite location
496 if (!md.isStatic()) {
497 // add 'this' reference location
499 methodInfo.addMapParamIdxToInferLoc(0, methodInfo.getInferLocation(md.getThis()));
504 for (int idx = 0; idx < md.numParameters(); idx++) {
505 Descriptor paramDesc = md.getParameter(idx);
506 CompositeLocation inferParamLoc = methodInfo.getInferLocation(paramDesc);
507 methodInfo.addMapParamIdxToInferLoc(idx + offset, inferParamLoc);
510 // calculate the initial program counter location
511 // PC location is higher than location types of all parameters
512 String pcLocSymbol = "PCLOC";
513 Map<Integer, CompositeLocation> mapParamToLoc = methodInfo.getMapParamIdxToInferLoc();
514 Set<Integer> keySet = mapParamToLoc.keySet();
515 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
516 Integer paramIdx = (Integer) iterator.next();
517 CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
518 String paramLocLocalSymbol = inferLoc.get(0).getLocIdentifier();
519 if (!methodLattice.isGreaterThan(pcLocSymbol, paramLocLocalSymbol)) {
520 addRelationHigherToLower(methodLattice, methodInfo, pcLocSymbol, paramLocLocalSymbol);
524 // calculate a return location
525 // the return location type is lower than all parameters
526 if (!md.getReturnType().isVoid()) {
528 String returnLocSymbol = "RETURNLOC";
530 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
531 Integer paramIdx = (Integer) iterator.next();
532 CompositeLocation inferLoc = mapParamToLoc.get(paramIdx);
533 String paramLocLocalSymbol = inferLoc.get(0).getLocIdentifier();
534 if (!methodLattice.isGreaterThan(paramLocLocalSymbol, returnLocSymbol)) {
535 addRelationHigherToLower(methodLattice, methodInfo, paramLocLocalSymbol, returnLocSymbol);
542 private boolean isGreaterThan(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
543 CompositeLocation comp2) {
545 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
547 for (int idx = 0; idx < size; idx++) {
548 Location loc1 = comp1.get(idx);
549 Location loc2 = comp2.get(idx);
551 Descriptor desc1 = loc1.getDescriptor();
552 Descriptor desc2 = loc2.getDescriptor();
554 if (!desc1.equals(desc2)) {
555 throw new Error("Fail to compare " + comp1 + " and " + comp2);
558 String symbol1 = loc1.getLocIdentifier();
559 String symbol2 = loc2.getLocIdentifier();
561 SSJavaLattice<String> lattice;
563 lattice = methodLattice;
565 lattice = getLattice(desc1);
567 if (symbol1.equals(symbol2)) {
569 } else if (lattice.isGreaterThan(symbol1, symbol2)) {
580 private void recursiveAddRelationToLattice(int idx, MethodDescriptor md,
581 CompositeLocation srcInferLoc, CompositeLocation dstInferLoc) throws CyclicFlowException {
583 String srcLocSymbol = srcInferLoc.get(idx).getLocIdentifier();
584 String dstLocSymbol = dstInferLoc.get(idx).getLocIdentifier();
586 if (srcLocSymbol.equals(dstLocSymbol)) {
587 recursiveAddRelationToLattice(idx + 1, md, srcInferLoc, dstInferLoc);
590 Descriptor parentDesc = srcInferLoc.get(idx).getDescriptor();
591 LocationInfo locInfo = getLocationInfo(parentDesc);
593 addRelationHigherToLower(getLattice(parentDesc), getLocationInfo(parentDesc), srcLocSymbol,
599 private void analyzeLatticeMethodInvocationNode(MethodDescriptor mdCaller,
600 SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo)
601 throws CyclicFlowException {
603 // the transformation for a call site propagates all relations between
604 // parameters from the callee
605 // if the method is virtual, it also grab all relations from any possible
608 Set<MethodInvokeNode> setMethodInvokeNode =
609 mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
611 if (setMethodInvokeNode != null) {
613 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
614 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
615 MethodDescriptor mdCallee = min.getMethod();
616 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
617 if (mdCallee.isStatic()) {
618 setPossibleCallees.add(mdCallee);
620 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
621 // removes method descriptors that are not invoked by the caller
622 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
623 setPossibleCallees.addAll(calleeSet);
626 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
627 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
628 propagateRelationToCaller(min, mdCaller, possibleMdCallee, methodLattice, methodInfo);
636 private void propagateRelationToCaller(MethodInvokeNode min, MethodDescriptor mdCaller,
637 MethodDescriptor possibleMdCallee, SSJavaLattice<String> methodLattice,
638 MethodLocationInfo methodInfo) throws CyclicFlowException {
640 SSJavaLattice<String> calleeLattice = getMethodLattice(possibleMdCallee);
641 MethodLocationInfo calleeLocInfo = getMethodLocationInfo(possibleMdCallee);
642 FlowGraph calleeFlowGraph = getFlowGraph(possibleMdCallee);
644 int numParam = calleeLocInfo.getNumParam();
645 for (int i = 0; i < numParam; i++) {
646 CompositeLocation param1 = calleeLocInfo.getParamCompositeLocation(i);
647 for (int k = 0; k < numParam; k++) {
649 CompositeLocation param2 = calleeLocInfo.getParamCompositeLocation(k);
650 if (isGreaterThan(getLattice(possibleMdCallee), param1, param2)) {
651 NodeTupleSet argDescTupleSet1 = getNodeTupleSetByArgIdx(min, i);
652 NodeTupleSet argDescTupleSet2 = getNodeTupleSetByArgIdx(min, k);
654 // the callee has the relation in which param1 is higher than param2
655 // therefore, the caller has to have the relation in which arg1 is
658 for (Iterator<NTuple<Descriptor>> iterator = argDescTupleSet1.iterator(); iterator
660 NTuple<Descriptor> argDescTuple1 = iterator.next();
662 for (Iterator<NTuple<Descriptor>> iterator2 = argDescTupleSet2.iterator(); iterator2
664 NTuple<Descriptor> argDescTuple2 = iterator2.next();
666 // retreive inferred location by the local var descriptor
668 NTuple<Location> tuple1 = getFlowGraph(mdCaller).getLocationTuple(argDescTuple1);
669 NTuple<Location> tuple2 = getFlowGraph(mdCaller).getLocationTuple(argDescTuple2);
671 // CompositeLocation higherInferLoc =
672 // methodInfo.getInferLocation(argTuple1.get(0));
673 // CompositeLocation lowerInferLoc =
674 // methodInfo.getInferLocation(argTuple2.get(0));
676 CompositeLocation inferLoc1 =
677 calcualteInferredCompositeLocation(methodInfo, tuple1);
678 CompositeLocation inferLoc2 =
679 calcualteInferredCompositeLocation(methodInfo, tuple2);
681 addRelation(methodLattice, methodInfo, inferLoc1, inferLoc2);
694 private CompositeLocation calcualteInferredCompositeLocation(MethodLocationInfo methodInfo,
695 NTuple<Location> tuple) {
697 // first, retrieve inferred location by the local var descriptor
699 CompositeLocation inferLoc = new CompositeLocation();
701 CompositeLocation localVarInferLoc =
702 methodInfo.getInferLocation(tuple.get(0).getLocDescriptor());
703 for (int i = 0; i < localVarInferLoc.getSize(); i++) {
704 inferLoc.addLocation(localVarInferLoc.get(i));
707 for (int i = 1; i < tuple.size(); i++) {
708 Location cur = tuple.get(i);
709 Descriptor enclosingDesc = cur.getDescriptor();
710 Descriptor curDesc = cur.getLocDescriptor();
712 String fieldLocSymbol =
713 getLocationInfo(enclosingDesc).getInferLocation(curDesc).get(0).getLocIdentifier();
714 Location inferLocElement = new Location(enclosingDesc, fieldLocSymbol);
716 inferLoc.addLocation(inferLocElement);
723 private void addRelation(SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo,
724 CompositeLocation srcInferLoc, CompositeLocation dstInferLoc) throws CyclicFlowException {
726 String srcLocalLocSymbol = srcInferLoc.get(0).getLocIdentifier();
727 String dstLocalLocSymbol = dstInferLoc.get(0).getLocIdentifier();
729 if (srcInferLoc.getSize() == 1 && dstInferLoc.getSize() == 1) {
730 // add a new relation to the local lattice
731 addRelationHigherToLower(methodLattice, methodInfo, srcLocalLocSymbol, dstLocalLocSymbol);
732 } else if (srcInferLoc.getSize() > 1 && dstInferLoc.getSize() > 1) {
733 // both src and dst have assigned to a composite location
735 if (!srcLocalLocSymbol.equals(dstLocalLocSymbol)) {
736 addRelationHigherToLower(methodLattice, methodInfo, srcLocalLocSymbol, dstLocalLocSymbol);
738 recursivelyAddRelation(1, srcInferLoc, dstInferLoc);
741 // either src or dst has assigned to a composite location
742 if (!srcLocalLocSymbol.equals(dstLocalLocSymbol)) {
743 addRelationHigherToLower(methodLattice, methodInfo, srcLocalLocSymbol, dstLocalLocSymbol);
749 private LocationInfo getLocationInfo(Descriptor d) {
750 if (d instanceof MethodDescriptor) {
751 return getMethodLocationInfo((MethodDescriptor) d);
753 return getFieldLocationInfo((ClassDescriptor) d);
757 private MethodLocationInfo getMethodLocationInfo(MethodDescriptor md) {
759 if (!mapMethodDescToMethodLocationInfo.containsKey(md)) {
760 mapMethodDescToMethodLocationInfo.put(md, new MethodLocationInfo(md));
763 return mapMethodDescToMethodLocationInfo.get(md);
767 private LocationInfo getFieldLocationInfo(ClassDescriptor cd) {
769 if (!mapClassToLocationInfo.containsKey(cd)) {
770 mapClassToLocationInfo.put(cd, new LocationInfo(cd));
773 return mapClassToLocationInfo.get(cd);
777 private void addRelationToLattice(MethodDescriptor md, SSJavaLattice<String> methodLattice,
778 MethodLocationInfo methodInfo, FlowNode srcNode, FlowNode dstNode) throws CyclicFlowException {
780 System.out.println();
781 System.out.println("### addRelationToLattice src=" + srcNode + " dst=" + dstNode);
783 // add a new binary relation of dstNode < srcNode
784 FlowGraph flowGraph = getFlowGraph(md);
786 calculateCompositeLocation(flowGraph, methodLattice, methodInfo, srcNode);
788 CompositeLocation srcInferLoc =
789 calcualteInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(srcNode));
790 CompositeLocation dstInferLoc =
791 calcualteInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(dstNode));
794 addRelation(methodLattice, methodInfo, srcInferLoc, dstInferLoc);
795 } catch (CyclicFlowException e) {
796 // there is a cyclic value flow... try to calculate a composite location
797 // for the destination node
798 calculateCompositeLocation(flowGraph, methodLattice, methodInfo, dstNode);
800 calcualteInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(dstNode));
802 addRelation(methodLattice, methodInfo, srcInferLoc, dstInferLoc);
803 } catch (CyclicFlowException e1) {
804 throw new Error("Failed to merge cyclic value flows into a shared location.");
810 private void recursivelyAddRelation(int idx, CompositeLocation srcInferLoc,
811 CompositeLocation dstInferLoc) throws CyclicFlowException {
813 String srcLocSymbol = srcInferLoc.get(idx).getLocIdentifier();
814 String dstLocSymbol = dstInferLoc.get(idx).getLocIdentifier();
816 Descriptor parentDesc = srcInferLoc.get(idx).getDescriptor();
818 if (srcLocSymbol.equals(dstLocSymbol)) {
819 // check if it is the case of shared location
820 if (srcInferLoc.getSize() == (idx + 1) && dstInferLoc.getSize() == (idx + 1)) {
821 Location inferLocElement = srcInferLoc.get(idx);
822 getLattice(inferLocElement.getDescriptor())
823 .addSharedLoc(inferLocElement.getLocIdentifier());
824 } else if (srcInferLoc.getSize() > (idx + 1) && dstInferLoc.getSize() > (idx + 1)) {
825 recursivelyAddRelation(idx + 1, srcInferLoc, dstInferLoc);
828 addRelationHigherToLower(getLattice(parentDesc), getLocationInfo(parentDesc), srcLocSymbol,
833 private void recursivelyAddCompositeRelation(MethodDescriptor md, FlowGraph flowGraph,
834 MethodLocationInfo methodInfo, FlowNode srcNode, FlowNode dstNode, Descriptor srcDesc,
835 Descriptor dstDesc) throws CyclicFlowException {
837 CompositeLocation inferSrcLoc;
838 CompositeLocation inferDstLoc = methodInfo.getInferLocation(dstDesc);
840 if (srcNode.getDescTuple().size() > 1) {
842 inferSrcLoc = new CompositeLocation();
844 NTuple<Location> locTuple = flowGraph.getLocationTuple(srcNode);
845 for (int i = 0; i < locTuple.size(); i++) {
846 inferSrcLoc.addLocation(locTuple.get(i));
850 inferSrcLoc = methodInfo.getInferLocation(srcDesc);
853 if (dstNode.getDescTuple().size() > 1) {
855 inferDstLoc = new CompositeLocation();
857 NTuple<Location> locTuple = flowGraph.getLocationTuple(dstNode);
858 for (int i = 0; i < locTuple.size(); i++) {
859 inferDstLoc.addLocation(locTuple.get(i));
863 inferDstLoc = methodInfo.getInferLocation(dstDesc);
866 recursiveAddRelationToLattice(1, md, inferSrcLoc, inferDstLoc);
869 private void addPrefixMapping(Map<NTuple<Location>, Set<NTuple<Location>>> map,
870 NTuple<Location> prefix, NTuple<Location> element) {
872 if (!map.containsKey(prefix)) {
873 map.put(prefix, new HashSet<NTuple<Location>>());
875 map.get(prefix).add(element);
878 private boolean calculateCompositeLocation(FlowGraph flowGraph,
879 SSJavaLattice<String> methodLattice, MethodLocationInfo methodInfo, FlowNode flowNode)
880 throws CyclicFlowException {
882 Descriptor localVarDesc = flowNode.getDescTuple().get(0);
884 Set<FlowNode> inNodeSet = flowGraph.getIncomingFlowNodeSet(flowNode);
885 Set<FlowNode> reachableNodeSet = flowGraph.getReachableFlowNodeSet(flowNode);
887 Map<NTuple<Location>, Set<NTuple<Location>>> mapPrefixToIncomingLocTupleSet =
888 new HashMap<NTuple<Location>, Set<NTuple<Location>>>();
890 Set<FlowNode> localInNodeSet = new HashSet<FlowNode>();
891 Set<FlowNode> localOutNodeSet = new HashSet<FlowNode>();
893 CompositeLocation flowNodeInferLoc =
894 calcualteInferredCompositeLocation(methodInfo, flowGraph.getLocationTuple(flowNode));
896 List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
898 for (Iterator iterator = inNodeSet.iterator(); iterator.hasNext();) {
899 FlowNode inNode = (FlowNode) iterator.next();
900 NTuple<Location> inTuple = flowGraph.getLocationTuple(inNode);
902 if (inTuple.size() > 1) {
903 for (int i = 1; i < inTuple.size(); i++) {
904 NTuple<Location> prefix = inTuple.subList(0, i);
905 if (!prefixList.contains(prefix)) {
906 prefixList.add(prefix);
908 addPrefixMapping(mapPrefixToIncomingLocTupleSet, prefix, inTuple);
911 localInNodeSet.add(inNode);
915 Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
916 public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
917 int s0 = arg0.size();
918 int s1 = arg1.size();
921 } else if (s0 == s1) {
929 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
930 FlowNode reachableNode = (FlowNode) iterator2.next();
931 if (reachableNode.getDescTuple().size() == 1) {
932 localOutNodeSet.add(reachableNode);
936 // find out reachable nodes that have the longest common prefix
937 for (int i = 0; i < prefixList.size(); i++) {
938 NTuple<Location> curPrefix = prefixList.get(i);
939 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
941 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
942 FlowNode reachableNode = (FlowNode) iterator2.next();
943 NTuple<Location> reachLocTuple = flowGraph.getLocationTuple(reachableNode);
944 if (reachLocTuple.startsWith(curPrefix)) {
945 reachableCommonPrefixSet.add(reachLocTuple);
949 // check if the lattice has the relation in which higher prefix is
950 // actually lower than the current node
951 CompositeLocation prefixInferLoc = calcualteInferredCompositeLocation(methodInfo, curPrefix);
952 if (isGreaterThan(methodLattice, flowNodeInferLoc, prefixInferLoc)) {
953 reachableCommonPrefixSet.add(curPrefix);
956 if (!reachableCommonPrefixSet.isEmpty()) {
957 // found reachable nodes that start with the prefix curPrefix
958 // need to assign a composite location
960 // first, check if there are more than one the set of locations that has
961 // the same length of the longest reachable prefix, no way to assign
962 // a composite location to the input local var
963 prefixSanityCheck(prefixList, i, flowGraph, reachableNodeSet);
965 Set<NTuple<Location>> incomingCommonPrefixSet =
966 mapPrefixToIncomingLocTupleSet.get(curPrefix);
968 int idx = curPrefix.size();
969 NTuple<Location> element = incomingCommonPrefixSet.iterator().next();
970 Descriptor desc = element.get(idx).getDescriptor();
972 SSJavaLattice<String> lattice = getLattice(desc);
973 LocationInfo locInfo = getLocationInfo(desc);
975 // CompositeLocation inferLocation =
976 // methodInfo.getInferLocation(flowNode);
977 CompositeLocation inferLocation = methodInfo.getInferLocation(localVarDesc);
979 String newlyInsertedLocName;
980 if (inferLocation.getSize() == 1) {
981 // need to replace the old local location with a new composite
984 String oldMethodLocationSymbol = inferLocation.get(0).getLocIdentifier();
986 String newLocSymbol = "Loc" + (SSJavaLattice.seed++);
987 inferLocation = new CompositeLocation();
988 for (int locIdx = 0; locIdx < curPrefix.size(); locIdx++) {
989 inferLocation.addLocation(curPrefix.get(locIdx));
991 Location fieldLoc = new Location(desc, newLocSymbol);
992 inferLocation.addLocation(fieldLoc);
994 methodInfo.mapDescriptorToLocation(localVarDesc, inferLocation);
995 methodInfo.removeMaplocalVarToLocSet(localVarDesc);
997 String newMethodLocationSymbol = curPrefix.get(0).getLocIdentifier();
999 replaceOldLocWithNewLoc(methodLattice, oldMethodLocationSymbol, newMethodLocationSymbol);
1003 String localLocName = methodInfo.getInferLocation(localVarDesc).get(0).getLocIdentifier();
1008 newlyInsertedLocName = inferLocation.get(inferLocation.getSize() - 1).getLocIdentifier();
1010 for (Iterator iterator = incomingCommonPrefixSet.iterator(); iterator.hasNext();) {
1011 NTuple<Location> tuple = (NTuple<Location>) iterator.next();
1013 Location loc = tuple.get(idx);
1014 String higher = locInfo.getFieldInferLocation(loc.getLocDescriptor()).getLocIdentifier();
1015 System.out.println("--");
1016 System.out.println("add in-flow relation:");
1017 addRelationHigherToLower(lattice, locInfo, higher, newlyInsertedLocName);
1019 System.out.println("end of add-inflow relation");
1021 for (Iterator iterator = localInNodeSet.iterator(); iterator.hasNext();) {
1022 FlowNode localNode = (FlowNode) iterator.next();
1024 if (localNode.equals(flowNode)) {
1028 Descriptor localInVarDesc = localNode.getDescTuple().get(0);
1029 CompositeLocation inNodeInferLoc = methodInfo.getInferLocation(localInVarDesc);
1031 if (isCompositeLocation(inNodeInferLoc)) {
1032 // need to make sure that newLocSymbol is lower than the infernode
1033 // location in the field lattice
1035 if (inNodeInferLoc.getTuple().startsWith(curPrefix)
1036 && inNodeInferLoc.getSize() == (curPrefix.size() + 1)) {
1037 String higher = inNodeInferLoc.get(inNodeInferLoc.getSize() - 1).getLocIdentifier();
1038 if (!higher.equals(newlyInsertedLocName)) {
1039 System.out.println("add localInNodeSet relation:");
1040 addRelationHigherToLower(lattice, locInfo, higher, newlyInsertedLocName);
1043 throw new Error("Failed to generate a composite location.");
1050 for (Iterator iterator = reachableCommonPrefixSet.iterator(); iterator.hasNext();) {
1051 NTuple<Location> tuple = (NTuple<Location>) iterator.next();
1052 if (tuple.size() > idx) {
1053 Location loc = tuple.get(idx);
1054 String lower = locInfo.getFieldInferLocation(loc.getLocDescriptor()).getLocIdentifier();
1055 // lattice.addRelationHigherToLower(newlyInsertedLocName, lower);
1056 System.out.println("add out-flow relation:");
1057 addRelationHigherToLower(lattice, locInfo, newlyInsertedLocName, lower);
1060 System.out.println("end of add out-flow relation");
1062 for (Iterator iterator = localOutNodeSet.iterator(); iterator.hasNext();) {
1063 FlowNode localOutNode = (FlowNode) iterator.next();
1065 if (localOutNode.equals(flowNode)) {
1069 Descriptor localOutDesc = localOutNode.getDescTuple().get(0);
1070 CompositeLocation outNodeInferLoc = methodInfo.getInferLocation(localOutDesc);
1072 if (isCompositeLocation(outNodeInferLoc)) {
1073 // need to make sure that newLocSymbol is higher than the infernode
1076 if (outNodeInferLoc.getTuple().startsWith(curPrefix)
1077 && outNodeInferLoc.getSize() == (curPrefix.size() + 1)) {
1079 String lower = outNodeInferLoc.get(outNodeInferLoc.getSize() - 1).getLocIdentifier();
1080 System.out.println("add outNodeInferLoc relation:");
1082 addRelationHigherToLower(lattice, locInfo, newlyInsertedLocName, lower);
1085 throw new Error("Failed to generate a composite location.");
1099 private boolean isCompositeLocation(CompositeLocation cl) {
1100 return cl.getSize() > 1;
1103 private boolean containsNonPrimitiveElement(Set<Descriptor> descSet) {
1104 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
1105 Descriptor desc = (Descriptor) iterator.next();
1107 if (desc.equals(LocationInference.GLOBALDESC)) {
1109 } else if (desc instanceof VarDescriptor) {
1110 if (!((VarDescriptor) desc).getType().isPrimitive()) {
1113 } else if (desc instanceof FieldDescriptor) {
1114 if (!((FieldDescriptor) desc).getType().isPrimitive()) {
1123 private void addRelationHigherToLower(SSJavaLattice<String> lattice, LocationInfo locInfo,
1124 String higher, String lower) throws CyclicFlowException {
1126 // if (higher.equals(lower) && lattice.isSharedLoc(higher)) {
1129 Set<String> cycleElementSet = lattice.getPossibleCycleElements(higher, lower);
1130 System.out.println("#Check cycle=" + lower + " < " + higher);
1131 System.out.println("#cycleElementSet=" + cycleElementSet);
1133 boolean hasNonPrimitiveElement = false;
1134 for (Iterator iterator = cycleElementSet.iterator(); iterator.hasNext();) {
1135 String cycleElementLocSymbol = (String) iterator.next();
1137 Set<Descriptor> descSet = locInfo.getDescSet(cycleElementLocSymbol);
1138 if (containsNonPrimitiveElement(descSet)) {
1139 hasNonPrimitiveElement = true;
1144 if (hasNonPrimitiveElement) {
1145 // if there is non-primitive element in the cycle, no way to merge cyclic
1146 // elements into the shared location
1147 throw new CyclicFlowException();
1150 if (cycleElementSet.size() > 0) {
1151 String newSharedLoc = "SharedLoc" + (SSJavaLattice.seed++);
1153 lattice.mergeIntoSharedLocation(cycleElementSet, newSharedLoc);
1155 for (Iterator iterator = cycleElementSet.iterator(); iterator.hasNext();) {
1156 String oldLocSymbol = (String) iterator.next();
1157 locInfo.mergeMapping(oldLocSymbol, newSharedLoc);
1160 lattice.addSharedLoc(newSharedLoc);
1162 } else if (!lattice.isGreaterThan(higher, lower)) {
1163 lattice.addRelationHigherToLower(higher, lower);
1167 private void replaceOldLocWithNewLoc(SSJavaLattice<String> methodLattice, String oldLocSymbol,
1168 String newLocSymbol) {
1170 if (methodLattice.containsKey(oldLocSymbol)) {
1171 methodLattice.substituteLocation(oldLocSymbol, newLocSymbol);
1176 private void prefixSanityCheck(List<NTuple<Location>> prefixList, int curIdx,
1177 FlowGraph flowGraph, Set<FlowNode> reachableNodeSet) {
1179 NTuple<Location> curPrefix = prefixList.get(curIdx);
1181 for (int i = curIdx + 1; i < prefixList.size(); i++) {
1182 NTuple<Location> prefixTuple = prefixList.get(i);
1184 if (curPrefix.startsWith(prefixTuple)) {
1188 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1189 FlowNode reachableNode = (FlowNode) iterator2.next();
1190 NTuple<Location> reachLocTuple = flowGraph.getLocationTuple(reachableNode);
1191 if (reachLocTuple.startsWith(prefixTuple)) {
1193 throw new Error("Failed to generate a composite location");
1199 public boolean isPrimitiveLocalVariable(FlowNode node) {
1200 VarDescriptor varDesc = (VarDescriptor) node.getDescTuple().get(0);
1201 return varDesc.getType().isPrimitive();
1204 private SSJavaLattice<String> getLattice(Descriptor d) {
1205 if (d instanceof MethodDescriptor) {
1206 return getMethodLattice((MethodDescriptor) d);
1208 return getFieldLattice((ClassDescriptor) d);
1212 private SSJavaLattice<String> getMethodLattice(MethodDescriptor md) {
1213 if (!md2lattice.containsKey(md)) {
1214 md2lattice.put(md, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
1216 return md2lattice.get(md);
1219 private void setMethodLattice(MethodDescriptor md, SSJavaLattice<String> lattice) {
1220 md2lattice.put(md, lattice);
1223 private void extractRelationFromFieldFlows(ClassDescriptor cd, FlowNode srcNode,
1224 FlowNode dstNode, int idx) throws CyclicFlowException {
1226 if (srcNode.getDescTuple().get(idx).equals(dstNode.getDescTuple().get(idx))
1227 && srcNode.getDescTuple().size() > (idx + 1) && dstNode.getDescTuple().size() > (idx + 1)) {
1228 // value flow between fields: we don't need to add a binary relation
1231 Descriptor desc = srcNode.getDescTuple().get(idx);
1232 ClassDescriptor classDesc;
1235 classDesc = ((VarDescriptor) desc).getType().getClassDesc();
1237 classDesc = ((FieldDescriptor) desc).getType().getClassDesc();
1240 extractRelationFromFieldFlows(classDesc, srcNode, dstNode, idx + 1);
1244 Descriptor srcFieldDesc = srcNode.getDescTuple().get(idx);
1245 Descriptor dstFieldDesc = dstNode.getDescTuple().get(idx);
1247 // add a new binary relation of dstNode < srcNode
1248 SSJavaLattice<String> fieldLattice = getFieldLattice(cd);
1249 LocationInfo fieldInfo = getFieldLocationInfo(cd);
1251 String srcSymbol = fieldInfo.getFieldInferLocation(srcFieldDesc).getLocIdentifier();
1252 String dstSymbol = fieldInfo.getFieldInferLocation(dstFieldDesc).getLocIdentifier();
1254 addRelationHigherToLower(fieldLattice, fieldInfo, srcSymbol, dstSymbol);
1260 public SSJavaLattice<String> getFieldLattice(ClassDescriptor cd) {
1261 if (!cd2lattice.containsKey(cd)) {
1262 cd2lattice.put(cd, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
1264 return cd2lattice.get(cd);
1267 public void constructFlowGraph() {
1271 Set<MethodDescriptor> visited = new HashSet<MethodDescriptor>();
1272 Set<MethodDescriptor> reachableCallee = new HashSet<MethodDescriptor>();
1274 while (!toAnalyzeIsEmpty()) {
1275 ClassDescriptor cd = toAnalyzeNext();
1277 setupToAnalazeMethod(cd);
1278 toanalyzeMethodList.removeAll(visited);
1280 while (!toAnalyzeMethodIsEmpty()) {
1281 MethodDescriptor md = toAnalyzeMethodNext();
1282 if ((!visited.contains(md))
1283 && (ssjava.needTobeAnnotated(md) || reachableCallee.contains(md))) {
1284 if (state.SSJAVADEBUG) {
1285 System.out.println();
1286 System.out.println("SSJAVA: Constructing a flow graph: " + md);
1289 // creates a mapping from a method descriptor to virtual methods
1290 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1291 if (md.isStatic()) {
1292 setPossibleCallees.add(md);
1294 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
1297 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getCalleeSet(md);
1298 Set<MethodDescriptor> needToAnalyzeCalleeSet = new HashSet<MethodDescriptor>();
1300 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
1301 MethodDescriptor calleemd = (MethodDescriptor) iterator.next();
1302 if ((!ssjava.isTrustMethod(calleemd))
1303 && (!ssjava.isSSJavaUtil(calleemd.getClassDesc()))) {
1304 if (!visited.contains(calleemd)) {
1305 toanalyzeMethodList.add(calleemd);
1307 reachableCallee.add(calleemd);
1308 needToAnalyzeCalleeSet.add(calleemd);
1312 mapMethodToCalleeSet.put(md, needToAnalyzeCalleeSet);
1314 // creates a mapping from a parameter descriptor to its index
1315 Map<Descriptor, Integer> mapParamDescToIdx = new HashMap<Descriptor, Integer>();
1316 int offset = md.isStatic() ? 0 : 1;
1317 for (int i = 0; i < md.numParameters(); i++) {
1318 Descriptor paramDesc = (Descriptor) md.getParameter(i);
1319 mapParamDescToIdx.put(paramDesc, new Integer(i + offset));
1322 FlowGraph fg = new FlowGraph(md, mapParamDescToIdx);
1323 mapMethodDescriptorToFlowGraph.put(md, fg);
1326 analyzeMethodBody(cd, md);
1332 _debug_printGraph();
1335 private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md) {
1336 BlockNode bn = state.getMethodBody(md);
1337 NodeTupleSet implicitFlowTupleSet = new NodeTupleSet();
1338 analyzeFlowBlockNode(md, md.getParameterTable(), bn, implicitFlowTupleSet);
1341 private void analyzeFlowBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn,
1342 NodeTupleSet implicitFlowTupleSet) {
1344 bn.getVarTable().setParent(nametable);
1345 for (int i = 0; i < bn.size(); i++) {
1346 BlockStatementNode bsn = bn.get(i);
1347 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
1352 private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
1353 BlockStatementNode bsn, NodeTupleSet implicitFlowTupleSet) {
1355 switch (bsn.kind()) {
1356 case Kind.BlockExpressionNode:
1357 analyzeBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn, implicitFlowTupleSet);
1360 case Kind.DeclarationNode:
1361 analyzeFlowDeclarationNode(md, nametable, (DeclarationNode) bsn, implicitFlowTupleSet);
1364 case Kind.IfStatementNode:
1365 analyzeFlowIfStatementNode(md, nametable, (IfStatementNode) bsn, implicitFlowTupleSet);
1369 analyzeFlowLoopNode(md, nametable, (LoopNode) bsn, implicitFlowTupleSet);
1372 case Kind.ReturnNode:
1373 analyzeFlowReturnNode(md, nametable, (ReturnNode) bsn, implicitFlowTupleSet);
1376 case Kind.SubBlockNode:
1377 analyzeFlowSubBlockNode(md, nametable, (SubBlockNode) bsn, implicitFlowTupleSet);
1380 case Kind.ContinueBreakNode:
1383 case Kind.SwitchStatementNode:
1384 analyzeSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn);
1391 private void analyzeSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
1392 SwitchStatementNode bsn) {
1393 // TODO Auto-generated method stub
1396 private void analyzeFlowSubBlockNode(MethodDescriptor md, SymbolTable nametable,
1397 SubBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
1398 analyzeFlowBlockNode(md, nametable, sbn.getBlockNode(), implicitFlowTupleSet);
1401 private void analyzeFlowReturnNode(MethodDescriptor md, SymbolTable nametable, ReturnNode rn,
1402 NodeTupleSet implicitFlowTupleSet) {
1404 ExpressionNode returnExp = rn.getReturnExpression();
1406 if (returnExp != null) {
1407 NodeTupleSet nodeSet = new NodeTupleSet();
1408 analyzeFlowExpressionNode(md, nametable, returnExp, nodeSet, false);
1410 FlowGraph fg = getFlowGraph(md);
1412 // annotate the elements of the node set as the return location
1413 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1414 NTuple<Descriptor> returnDescTuple = (NTuple<Descriptor>) iterator.next();
1415 fg.setReturnFlowNode(returnDescTuple);
1416 for (Iterator iterator2 = implicitFlowTupleSet.iterator(); iterator2.hasNext();) {
1417 NTuple<Descriptor> implicitFlowDescTuple = (NTuple<Descriptor>) iterator2.next();
1418 fg.addValueFlowEdge(implicitFlowDescTuple, returnDescTuple);
1425 private void analyzeFlowLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln,
1426 NodeTupleSet implicitFlowTupleSet) {
1428 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
1430 NodeTupleSet condTupleNode = new NodeTupleSet();
1431 analyzeFlowExpressionNode(md, nametable, ln.getCondition(), condTupleNode, null,
1432 implicitFlowTupleSet, false);
1433 condTupleNode.addTupleSet(implicitFlowTupleSet);
1435 // add edges from condNodeTupleSet to all nodes of conditional nodes
1436 analyzeFlowBlockNode(md, nametable, ln.getBody(), condTupleNode);
1439 // check 'for loop' case
1440 BlockNode bn = ln.getInitializer();
1441 bn.getVarTable().setParent(nametable);
1442 for (int i = 0; i < bn.size(); i++) {
1443 BlockStatementNode bsn = bn.get(i);
1444 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
1447 NodeTupleSet condTupleNode = new NodeTupleSet();
1448 analyzeFlowExpressionNode(md, bn.getVarTable(), ln.getCondition(), condTupleNode, null,
1449 implicitFlowTupleSet, false);
1450 condTupleNode.addTupleSet(implicitFlowTupleSet);
1452 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(), condTupleNode);
1453 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(), condTupleNode);
1459 private void analyzeFlowIfStatementNode(MethodDescriptor md, SymbolTable nametable,
1460 IfStatementNode isn, NodeTupleSet implicitFlowTupleSet) {
1462 NodeTupleSet condTupleNode = new NodeTupleSet();
1463 analyzeFlowExpressionNode(md, nametable, isn.getCondition(), condTupleNode, null,
1464 implicitFlowTupleSet, false);
1466 // add edges from condNodeTupleSet to all nodes of conditional nodes
1467 condTupleNode.addTupleSet(implicitFlowTupleSet);
1468 analyzeFlowBlockNode(md, nametable, isn.getTrueBlock(), condTupleNode);
1470 if (isn.getFalseBlock() != null) {
1471 analyzeFlowBlockNode(md, nametable, isn.getFalseBlock(), condTupleNode);
1476 private void analyzeFlowDeclarationNode(MethodDescriptor md, SymbolTable nametable,
1477 DeclarationNode dn, NodeTupleSet implicitFlowTupleSet) {
1479 VarDescriptor vd = dn.getVarDescriptor();
1480 NTuple<Descriptor> tupleLHS = new NTuple<Descriptor>();
1482 getFlowGraph(md).createNewFlowNode(tupleLHS);
1484 if (dn.getExpression() != null) {
1486 NodeTupleSet tupleSetRHS = new NodeTupleSet();
1487 analyzeFlowExpressionNode(md, nametable, dn.getExpression(), tupleSetRHS, null,
1488 implicitFlowTupleSet, false);
1490 // add a new flow edge from rhs to lhs
1491 for (Iterator<NTuple<Descriptor>> iter = tupleSetRHS.iterator(); iter.hasNext();) {
1492 NTuple<Descriptor> from = iter.next();
1493 addFlowGraphEdge(md, from, tupleLHS);
1500 private void analyzeBlockExpressionNode(MethodDescriptor md, SymbolTable nametable,
1501 BlockExpressionNode ben, NodeTupleSet implicitFlowTupleSet) {
1502 analyzeFlowExpressionNode(md, nametable, ben.getExpression(), null, null, implicitFlowTupleSet,
1506 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
1507 ExpressionNode en, NodeTupleSet nodeSet, boolean isLHS) {
1508 return analyzeFlowExpressionNode(md, nametable, en, nodeSet, null, new NodeTupleSet(), isLHS);
1511 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
1512 ExpressionNode en, NodeTupleSet nodeSet, NTuple<Descriptor> base,
1513 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
1515 // note that expression node can create more than one flow node
1516 // nodeSet contains of flow nodes
1517 // base is always assigned to null except the case of a name node!
1519 NTuple<Descriptor> flowTuple;
1521 switch (en.kind()) {
1523 case Kind.AssignmentNode:
1524 analyzeFlowAssignmentNode(md, nametable, (AssignmentNode) en, base, implicitFlowTupleSet);
1527 case Kind.FieldAccessNode:
1529 analyzeFlowFieldAccessNode(md, nametable, (FieldAccessNode) en, nodeSet, base,
1530 implicitFlowTupleSet);
1531 if (flowTuple != null) {
1532 nodeSet.addTuple(flowTuple);
1537 NodeTupleSet nameNodeSet = new NodeTupleSet();
1539 analyzeFlowNameNode(md, nametable, (NameNode) en, nameNodeSet, base, implicitFlowTupleSet);
1540 if (flowTuple != null) {
1541 nodeSet.addTuple(flowTuple);
1546 analyzeFlowOpNode(md, nametable, (OpNode) en, nodeSet, implicitFlowTupleSet);
1549 case Kind.CreateObjectNode:
1550 analyzeCreateObjectNode(md, nametable, (CreateObjectNode) en);
1553 case Kind.ArrayAccessNode:
1554 analyzeFlowArrayAccessNode(md, nametable, (ArrayAccessNode) en, nodeSet, isLHS);
1557 case Kind.LiteralNode:
1558 analyzeLiteralNode(md, nametable, (LiteralNode) en);
1561 case Kind.MethodInvokeNode:
1562 analyzeFlowMethodInvokeNode(md, nametable, (MethodInvokeNode) en, implicitFlowTupleSet);
1565 case Kind.TertiaryNode:
1566 analyzeFlowTertiaryNode(md, nametable, (TertiaryNode) en, nodeSet, implicitFlowTupleSet);
1570 analyzeFlowCastNode(md, nametable, (CastNode) en, nodeSet, base, implicitFlowTupleSet);
1572 // case Kind.InstanceOfNode:
1573 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
1576 // case Kind.ArrayInitializerNode:
1577 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
1581 // case Kind.ClassTypeNode:
1582 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
1585 // case Kind.OffsetNode:
1586 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
1594 private void analyzeFlowCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn,
1595 NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
1597 analyzeFlowExpressionNode(md, nametable, cn.getExpression(), nodeSet, base,
1598 implicitFlowTupleSet, false);
1602 private void analyzeFlowTertiaryNode(MethodDescriptor md, SymbolTable nametable, TertiaryNode tn,
1603 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
1605 NodeTupleSet tertiaryTupleNode = new NodeTupleSet();
1606 analyzeFlowExpressionNode(md, nametable, tn.getCond(), tertiaryTupleNode, null,
1607 implicitFlowTupleSet, false);
1609 // add edges from tertiaryTupleNode to all nodes of conditional nodes
1610 tertiaryTupleNode.addTupleSet(implicitFlowTupleSet);
1611 analyzeFlowExpressionNode(md, nametable, tn.getTrueExpr(), tertiaryTupleNode, null,
1612 implicitFlowTupleSet, false);
1614 analyzeFlowExpressionNode(md, nametable, tn.getFalseExpr(), tertiaryTupleNode, null,
1615 implicitFlowTupleSet, false);
1617 nodeSet.addTupleSet(tertiaryTupleNode);
1621 private void addMapCallerMethodDescToMethodInvokeNodeSet(MethodDescriptor caller,
1622 MethodInvokeNode min) {
1623 Set<MethodInvokeNode> set = mapMethodDescriptorToMethodInvokeNodeSet.get(caller);
1625 set = new HashSet<MethodInvokeNode>();
1626 mapMethodDescriptorToMethodInvokeNodeSet.put(caller, set);
1631 private void analyzeFlowMethodInvokeNode(MethodDescriptor md, SymbolTable nametable,
1632 MethodInvokeNode min, NodeTupleSet implicitFlowTupleSet) {
1634 addMapCallerMethodDescToMethodInvokeNodeSet(md, min);
1636 MethodDescriptor calleeMD = min.getMethod();
1638 NameDescriptor baseName = min.getBaseName();
1639 boolean isSystemout = false;
1640 if (baseName != null) {
1641 isSystemout = baseName.getSymbol().equals("System.out");
1644 if (!ssjava.isSSJavaUtil(calleeMD.getClassDesc()) && !ssjava.isTrustMethod(calleeMD)
1645 && !calleeMD.getModifiers().isNative() && !isSystemout) {
1647 // CompositeLocation baseLocation = null;
1648 if (min.getExpression() != null) {
1650 NodeTupleSet baseNodeSet = new NodeTupleSet();
1651 analyzeFlowExpressionNode(md, nametable, min.getExpression(), baseNodeSet, null,
1652 implicitFlowTupleSet, false);
1655 if (min.getMethod().isStatic()) {
1656 // String globalLocId = ssjava.getMethodLattice(md).getGlobalLoc();
1657 // if (globalLocId == null) {
1659 // Error("Method lattice does not define global variable location at "
1660 // + generateErrorMessage(md.getClassDesc(), min));
1662 // baseLocation = new CompositeLocation(new Location(md,
1666 // String thisLocId = ssjava.getMethodLattice(md).getThisLoc();
1667 // baseLocation = new CompositeLocation(new Location(md, thisLocId));
1672 // if (constraint != null) {
1673 // int compareResult =
1674 // CompositeLattice.compare(constraint, baseLocation, true,
1675 // generateErrorMessage(cd, min));
1676 // if (compareResult != ComparisonResult.GREATER) {
1677 // // if the current constraint is higher than method's THIS location
1678 // // no need to check constraints!
1679 // CompositeLocation calleeConstraint =
1680 // translateCallerLocToCalleeLoc(calleeMD, baseLocation, constraint);
1681 // // System.out.println("check method body for constraint:" + calleeMD +
1682 // // " calleeConstraint="
1683 // // + calleeConstraint);
1684 // checkMethodBody(calleeMD.getClassDesc(), calleeMD, calleeConstraint);
1688 analyzeFlowMethodParameters(md, nametable, min);
1690 // checkCalleeConstraints(md, nametable, min, baseLocation, constraint);
1692 // checkCallerArgumentLocationConstraints(md, nametable, min,
1693 // baseLocation, constraint);
1695 if (min.getMethod().getReturnType() != null && !min.getMethod().getReturnType().isVoid()) {
1696 // If method has a return value, compute the highest possible return
1697 // location in the caller's perspective
1698 // CompositeLocation ceilingLoc =
1699 // computeCeilingLocationForCaller(md, nametable, min, baseLocation,
1701 // return ceilingLoc;
1705 // return new CompositeLocation(Location.createTopLocation(md));
1709 private NodeTupleSet getNodeTupleSetByArgIdx(MethodInvokeNode min, int idx) {
1710 return mapMethodInvokeNodeToArgIdxMap.get(min).get(new Integer(idx));
1713 private void addArgIdxMap(MethodInvokeNode min, int idx, NodeTupleSet tupleSet) {
1714 Map<Integer, NodeTupleSet> mapIdxToTupleSet = mapMethodInvokeNodeToArgIdxMap.get(min);
1715 if (mapIdxToTupleSet == null) {
1716 mapIdxToTupleSet = new HashMap<Integer, NodeTupleSet>();
1717 mapMethodInvokeNodeToArgIdxMap.put(min, mapIdxToTupleSet);
1719 mapIdxToTupleSet.put(new Integer(idx), tupleSet);
1722 private void analyzeFlowMethodParameters(MethodDescriptor callermd, SymbolTable nametable,
1723 MethodInvokeNode min) {
1726 if (min.numArgs() > 0) {
1729 if (min.getMethod().isStatic()) {
1733 NTuple<Descriptor> thisArgTuple = new NTuple<Descriptor>();
1734 thisArgTuple.add(callermd.getThis());
1735 NodeTupleSet argTupleSet = new NodeTupleSet();
1736 argTupleSet.addTuple(thisArgTuple);
1737 addArgIdxMap(min, 0, argTupleSet);
1740 for (int i = 0; i < min.numArgs(); i++) {
1741 ExpressionNode en = min.getArg(i);
1742 NodeTupleSet argTupleSet = new NodeTupleSet();
1743 analyzeFlowExpressionNode(callermd, nametable, en, argTupleSet, false);
1744 // if argument is liternal node, argTuple is set to NULL.
1745 addArgIdxMap(min, i + offset, argTupleSet);
1752 private void analyzeLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode en) {
1756 private void analyzeFlowArrayAccessNode(MethodDescriptor md, SymbolTable nametable,
1757 ArrayAccessNode aan, NodeTupleSet nodeSet, boolean isLHS) {
1759 NodeTupleSet expNodeTupleSet = new NodeTupleSet();
1760 analyzeFlowExpressionNode(md, nametable, aan.getExpression(), expNodeTupleSet, isLHS);
1762 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
1763 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, isLHS);
1766 // need to create an edge from idx to array
1768 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
1769 NTuple<Descriptor> idxTuple = idxIter.next();
1770 for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
1771 NTuple<Descriptor> arrTuple = arrIter.next();
1772 getFlowGraph(md).addValueFlowEdge(idxTuple, arrTuple);
1776 nodeSet.addTupleSet(expNodeTupleSet);
1778 nodeSet.addTupleSet(expNodeTupleSet);
1779 nodeSet.addTupleSet(idxNodeTupleSet);
1783 private void analyzeCreateObjectNode(MethodDescriptor md, SymbolTable nametable,
1784 CreateObjectNode en) {
1785 // TODO Auto-generated method stub
1789 private void analyzeFlowOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on,
1790 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
1792 NodeTupleSet leftOpSet = new NodeTupleSet();
1793 NodeTupleSet rightOpSet = new NodeTupleSet();
1796 analyzeFlowExpressionNode(md, nametable, on.getLeft(), leftOpSet, null, implicitFlowTupleSet,
1799 if (on.getRight() != null) {
1801 analyzeFlowExpressionNode(md, nametable, on.getRight(), rightOpSet, null,
1802 implicitFlowTupleSet, false);
1805 Operation op = on.getOp();
1807 switch (op.getOp()) {
1809 case Operation.UNARYPLUS:
1810 case Operation.UNARYMINUS:
1811 case Operation.LOGIC_NOT:
1813 nodeSet.addTupleSet(leftOpSet);
1816 case Operation.LOGIC_OR:
1817 case Operation.LOGIC_AND:
1818 case Operation.COMP:
1819 case Operation.BIT_OR:
1820 case Operation.BIT_XOR:
1821 case Operation.BIT_AND:
1822 case Operation.ISAVAILABLE:
1823 case Operation.EQUAL:
1824 case Operation.NOTEQUAL:
1831 case Operation.MULT:
1834 case Operation.LEFTSHIFT:
1835 case Operation.RIGHTSHIFT:
1836 case Operation.URIGHTSHIFT:
1838 // there are two operands
1839 nodeSet.addTupleSet(leftOpSet);
1840 nodeSet.addTupleSet(rightOpSet);
1844 throw new Error(op.toString());
1849 private NTuple<Descriptor> analyzeFlowNameNode(MethodDescriptor md, SymbolTable nametable,
1850 NameNode nn, NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
1854 base = new NTuple<Descriptor>();
1857 NameDescriptor nd = nn.getName();
1859 if (nd.getBase() != null) {
1861 analyzeFlowExpressionNode(md, nametable, nn.getExpression(), nodeSet, base,
1862 implicitFlowTupleSet, false);
1864 // base node has the top location
1868 String varname = nd.toString();
1869 if (varname.equals("this")) {
1871 base.add(md.getThis());
1875 Descriptor d = (Descriptor) nametable.get(varname);
1877 if (d instanceof VarDescriptor) {
1878 VarDescriptor vd = (VarDescriptor) d;
1880 } else if (d instanceof FieldDescriptor) {
1881 // the type of field descriptor has a location!
1882 FieldDescriptor fd = (FieldDescriptor) d;
1883 if (fd.isStatic()) {
1885 // if it is 'static final', no need to have flow node for the TOP
1889 // if 'static', assign the default GLOBAL LOCATION to the first
1890 // element of the tuple
1891 base.add(GLOBALDESC);
1894 // the location of field access starts from this, followed by field
1896 base.add(md.getThis());
1900 } else if (d == null) {
1901 // access static field
1902 base.add(GLOBALDESC);
1903 // base.add(nn.getField());
1906 // FieldDescriptor fd = nn.getField();addFlowGraphEdge
1908 // MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
1909 // String globalLocId = localLattice.getGlobalLoc();
1910 // if (globalLocId == null) {
1912 // Error("Method lattice does not define global variable location at "
1913 // + generateErrorMessage(md.getClassDesc(), nn));
1915 // loc.addLocation(new Location(md, globalLocId));
1917 // Location fieldLoc = (Location) fd.getType().getExtension();
1918 // loc.addLocation(fieldLoc);
1925 getFlowGraph(md).createNewFlowNode(base);
1931 private NTuple<Descriptor> analyzeFlowFieldAccessNode(MethodDescriptor md, SymbolTable nametable,
1932 FieldAccessNode fan, NodeTupleSet nodeSet, NTuple<Descriptor> base,
1933 NodeTupleSet implicitFlowTupleSet) {
1936 ExpressionNode left = fan.getExpression();
1937 TypeDescriptor ltd = left.getType();
1938 FieldDescriptor fd = fan.getField();
1940 String varName = null;
1941 if (left.kind() == Kind.NameNode) {
1942 NameDescriptor nd = ((NameNode) left).getName();
1943 varName = nd.toString();
1946 if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) {
1947 // using a class name directly or access using this
1948 if (fd.isStatic() && fd.isFinal()) {
1949 // loc.addLocation(Location.createTopLocation(md));
1954 if (left instanceof ArrayAccessNode) {
1955 ArrayAccessNode aan = (ArrayAccessNode) left;
1956 left = aan.getExpression();
1960 analyzeFlowExpressionNode(md, nametable, left, nodeSet, base, implicitFlowTupleSet, false);
1962 // in this case, field is TOP location
1966 if (!left.getType().isPrimitive()) {
1968 if (fd.getSymbol().equals("length")) {
1969 // array.length access, just have the location of the array
1976 getFlowGraph(md).createNewFlowNode(base);
1983 private void debug_printTreeNode(TreeNode tn) {
1985 System.out.println("DEBUG: " + tn.printNode(0) + " line#=" + tn.getNumLine());
1989 private void analyzeFlowAssignmentNode(MethodDescriptor md, SymbolTable nametable,
1990 AssignmentNode an, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
1992 NodeTupleSet nodeSetRHS = new NodeTupleSet();
1993 NodeTupleSet nodeSetLHS = new NodeTupleSet();
1995 boolean postinc = true;
1996 if (an.getOperation().getBaseOp() == null
1997 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
1998 .getBaseOp().getOp() != Operation.POSTDEC)) {
2001 // if LHS is array access node, need to capture value flows between an array
2002 // and its index value
2003 analyzeFlowExpressionNode(md, nametable, an.getDest(), nodeSetLHS, null, implicitFlowTupleSet,
2007 // analyze value flows of rhs expression
2008 analyzeFlowExpressionNode(md, nametable, an.getSrc(), nodeSetRHS, null, implicitFlowTupleSet,
2011 // System.out.println("-analyzeFlowAssignmentNode=" + an.printNode(0));
2012 // System.out.println("-nodeSetLHS=" + nodeSetLHS);
2013 // System.out.println("-nodeSetRHS=" + nodeSetRHS);
2014 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
2015 // System.out.println("-");
2017 if (an.getOperation().getOp() >= 2 && an.getOperation().getOp() <= 12) {
2018 // if assignment contains OP+EQ operator, creates edges from LHS to LHS
2019 for (Iterator<NTuple<Descriptor>> iter = nodeSetLHS.iterator(); iter.hasNext();) {
2020 NTuple<Descriptor> fromTuple = iter.next();
2021 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2022 NTuple<Descriptor> toTuple = iter2.next();
2023 addFlowGraphEdge(md, fromTuple, toTuple);
2028 // creates edges from RHS to LHS
2029 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
2030 NTuple<Descriptor> fromTuple = iter.next();
2031 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2032 NTuple<Descriptor> toTuple = iter2.next();
2033 addFlowGraphEdge(md, fromTuple, toTuple);
2037 // creates edges from implicitFlowTupleSet to LHS
2038 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
2039 NTuple<Descriptor> fromTuple = iter.next();
2040 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2041 NTuple<Descriptor> toTuple = iter2.next();
2042 addFlowGraphEdge(md, fromTuple, toTuple);
2048 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
2049 NTuple<Descriptor> tuple = iter2.next();
2050 addFlowGraphEdge(md, tuple, tuple);
2057 public FlowGraph getFlowGraph(MethodDescriptor md) {
2058 return mapMethodDescriptorToFlowGraph.get(md);
2061 private boolean addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
2062 NTuple<Descriptor> to) {
2064 // return true if it adds a new edge
2065 FlowGraph graph = getFlowGraph(md);
2066 graph.addValueFlowEdge(from, to);
2070 public void _debug_printGraph() {
2071 Set<MethodDescriptor> keySet = mapMethodDescriptorToFlowGraph.keySet();
2073 for (Iterator<MethodDescriptor> iterator = keySet.iterator(); iterator.hasNext();) {
2074 MethodDescriptor md = (MethodDescriptor) iterator.next();
2075 FlowGraph fg = mapMethodDescriptorToFlowGraph.get(md);
2078 } catch (IOException e) {
2079 e.printStackTrace();
2087 class CyclicFlowException extends Exception {