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.SwitchBlockNode;
51 import IR.Tree.SwitchStatementNode;
52 import IR.Tree.TertiaryNode;
53 import IR.Tree.TreeNode;
56 public class LocationInference {
59 SSJavaAnalysis ssjava;
61 List<ClassDescriptor> temp_toanalyzeList;
62 List<MethodDescriptor> temp_toanalyzeMethodList;
63 Map<MethodDescriptor, FlowGraph> mapMethodDescriptorToFlowGraph;
65 LinkedList<MethodDescriptor> toanalyze_methodDescList;
67 // map a method descriptor to its set of parameter descriptors
68 Map<MethodDescriptor, Set<Descriptor>> mapMethodDescriptorToParamDescSet;
70 // keep current descriptors to visit in fixed-point interprocedural analysis,
71 private Stack<MethodDescriptor> methodDescriptorsToVisitStack;
73 // map a class descriptor to a field lattice
74 private Map<ClassDescriptor, SSJavaLattice<String>> cd2lattice;
76 // map a method descriptor to a method lattice
77 private Map<MethodDescriptor, SSJavaLattice<String>> md2lattice;
79 // map a method/class descriptor to a hierarchy graph
80 private Map<Descriptor, HierarchyGraph> mapDescriptorToHierarchyGraph;
82 // map a method/class descriptor to a skeleton hierarchy graph
83 private Map<Descriptor, HierarchyGraph> mapDescriptorToSkeletonHierarchyGraph;
85 private Map<Descriptor, HierarchyGraph> mapDescriptorToSimpleHierarchyGraph;
87 // map a method/class descriptor to a skeleton hierarchy graph with combination nodes
88 private Map<Descriptor, HierarchyGraph> mapDescriptorToCombineSkeletonHierarchyGraph;
90 // map a descriptor to a simple lattice
91 private Map<Descriptor, SSJavaLattice<String>> mapDescriptorToSimpleLattice;
93 // map a method descriptor to the set of method invocation nodes which are
94 // invoked by the method descriptor
95 private Map<MethodDescriptor, Set<MethodInvokeNode>> mapMethodDescriptorToMethodInvokeNodeSet;
97 private Map<MethodInvokeNode, Map<Integer, NTuple<Descriptor>>> mapMethodInvokeNodeToArgIdxMap;
99 private Map<MethodInvokeNode, NTuple<Descriptor>> mapMethodInvokeNodeToBaseTuple;
101 private Map<MethodInvokeNode, Set<NTuple<Location>>> mapMethodInvokeNodeToPCLocTupleSet;
103 private Map<MethodDescriptor, MethodLocationInfo> mapMethodDescToMethodLocationInfo;
105 private Map<ClassDescriptor, LocationInfo> mapClassToLocationInfo;
107 private Map<MethodDescriptor, Set<MethodDescriptor>> mapMethodToCalleeSet;
109 private Map<MethodDescriptor, Set<FlowNode>> mapMethodDescToParamNodeFlowsToReturnValue;
111 private Map<String, Vector<String>> mapFileNameToLineVector;
113 private Map<Descriptor, Integer> mapDescToDefinitionLine;
115 private Map<Descriptor, LocationSummary> mapDescToLocationSummary;
117 private Map<MethodDescriptor, Set<MethodInvokeNode>> mapMethodDescToMethodInvokeNodeSet;
119 // maps a method descriptor to a sub global flow graph that captures all value flows caused by the
120 // set of callees reachable from the method
121 private Map<MethodDescriptor, GlobalFlowGraph> mapMethodDescriptorToSubGlobalFlowGraph;
123 private Map<MethodInvokeNode, Map<NTuple<Descriptor>, NTuple<Descriptor>>> mapMethodInvokeNodeToMapCallerArgToCalleeArg;
125 private Map<MethodDescriptor, Boolean> mapMethodDescriptorToCompositeReturnCase;
127 public static final String GLOBALLOC = "GLOBALLOC";
129 public static final String INTERLOC = "INTERLOC";
131 public static final String PCLOC = "_PCLOC_";
133 public static final String RLOC = "_RLOC_";
135 public static final Descriptor GLOBALDESC = new NameDescriptor(GLOBALLOC);
137 public static final Descriptor TOPDESC = new NameDescriptor(SSJavaAnalysis.TOP);
139 public static final Descriptor BOTTOMDESC = new NameDescriptor(SSJavaAnalysis.BOTTOM);
141 public static final Descriptor RETURNLOC = new NameDescriptor(RLOC);
143 public static final Descriptor LITERALDESC = new NameDescriptor("LITERAL");
145 public static final HNode TOPHNODE = new HNode(TOPDESC);
147 public static final HNode BOTTOMHNODE = new HNode(BOTTOMDESC);
149 public static String newline = System.getProperty("line.separator");
151 LocationInfo curMethodInfo;
153 private boolean hasChanges = false;
155 boolean debug = true;
157 public static int locSeed = 0;
159 private Stack<String> arrayAccessNodeStack;
161 public LocationInference(SSJavaAnalysis ssjava, State state) {
162 this.ssjava = ssjava;
164 this.temp_toanalyzeList = new ArrayList<ClassDescriptor>();
165 this.temp_toanalyzeMethodList = new ArrayList<MethodDescriptor>();
166 this.mapMethodDescriptorToFlowGraph = new HashMap<MethodDescriptor, FlowGraph>();
167 this.cd2lattice = new HashMap<ClassDescriptor, SSJavaLattice<String>>();
168 this.md2lattice = new HashMap<MethodDescriptor, SSJavaLattice<String>>();
169 this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
170 this.mapMethodDescriptorToMethodInvokeNodeSet =
171 new HashMap<MethodDescriptor, Set<MethodInvokeNode>>();
172 this.mapMethodInvokeNodeToArgIdxMap =
173 new HashMap<MethodInvokeNode, Map<Integer, NTuple<Descriptor>>>();
174 this.mapMethodDescToMethodLocationInfo = new HashMap<MethodDescriptor, MethodLocationInfo>();
175 this.mapMethodToCalleeSet = new HashMap<MethodDescriptor, Set<MethodDescriptor>>();
176 this.mapClassToLocationInfo = new HashMap<ClassDescriptor, LocationInfo>();
178 this.mapFileNameToLineVector = new HashMap<String, Vector<String>>();
179 this.mapDescToDefinitionLine = new HashMap<Descriptor, Integer>();
180 this.mapMethodDescToParamNodeFlowsToReturnValue =
181 new HashMap<MethodDescriptor, Set<FlowNode>>();
183 this.mapDescriptorToHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
184 this.mapMethodInvokeNodeToBaseTuple = new HashMap<MethodInvokeNode, NTuple<Descriptor>>();
186 this.mapDescriptorToSkeletonHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
187 this.mapDescriptorToCombineSkeletonHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
188 this.mapDescriptorToSimpleHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
190 this.mapDescriptorToSimpleLattice = new HashMap<Descriptor, SSJavaLattice<String>>();
192 this.mapDescToLocationSummary = new HashMap<Descriptor, LocationSummary>();
194 this.mapMethodDescriptorToSubGlobalFlowGraph = new HashMap<MethodDescriptor, GlobalFlowGraph>();
196 this.mapMethodInvokeNodeToMapCallerArgToCalleeArg =
197 new HashMap<MethodInvokeNode, Map<NTuple<Descriptor>, NTuple<Descriptor>>>();
199 this.mapMethodInvokeNodeToPCLocTupleSet =
200 new HashMap<MethodInvokeNode, Set<NTuple<Location>>>();
202 this.arrayAccessNodeStack = new Stack<String>();
204 this.mapMethodDescToMethodInvokeNodeSet =
205 new HashMap<MethodDescriptor, Set<MethodInvokeNode>>();
207 this.mapMethodDescriptorToCompositeReturnCase = new HashMap<MethodDescriptor, Boolean>();
211 public void setupToAnalyze() {
212 SymbolTable classtable = state.getClassSymbolTable();
213 temp_toanalyzeList.clear();
214 temp_toanalyzeList.addAll(classtable.getValueSet());
215 // Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
216 // public int compare(ClassDescriptor o1, ClassDescriptor o2) {
217 // return o1.getClassName().compareToIgnoreCase(o2.getClassName());
222 public void setupToAnalazeMethod(ClassDescriptor cd) {
224 SymbolTable methodtable = cd.getMethodTable();
225 temp_toanalyzeMethodList.clear();
226 temp_toanalyzeMethodList.addAll(methodtable.getValueSet());
227 Collections.sort(temp_toanalyzeMethodList, new Comparator<MethodDescriptor>() {
228 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
229 return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
234 public boolean toAnalyzeMethodIsEmpty() {
235 return temp_toanalyzeMethodList.isEmpty();
238 public boolean toAnalyzeIsEmpty() {
239 return temp_toanalyzeList.isEmpty();
242 public ClassDescriptor toAnalyzeNext() {
243 return temp_toanalyzeList.remove(0);
246 public MethodDescriptor toAnalyzeMethodNext() {
247 return temp_toanalyzeMethodList.remove(0);
250 public void inference() {
254 // construct value flow graph
255 constructFlowGraph();
257 constructGlobalFlowGraph();
261 assignCompositeLocation();
263 calculateExtraLocations();
264 addAdditionalOrderingConstraints();
266 _debug_writeFlowGraph();
270 constructHierarchyGraph();
272 debug_writeHierarchyDotFiles();
276 simplifyHierarchyGraph();
278 debug_writeSimpleHierarchyDotFiles();
280 constructSkeletonHierarchyGraph();
282 debug_writeSkeletonHierarchyDotFiles();
284 insertCombinationNodes();
286 debug_writeSkeletonCombinationHierarchyDotFiles();
290 debug_writeLattices();
292 updateCompositeLocationAssignments();
294 generateMethodSummary();
296 generateAnnoatedCode();
302 private void checkReturnNodes() {
303 LinkedList<MethodDescriptor> methodDescList =
304 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
306 while (!methodDescList.isEmpty()) {
307 MethodDescriptor md = methodDescList.removeLast();
309 if (md.getReturnType() != null && !md.getReturnType().isVoid()) {
310 checkFlowNodeReturnThisField(md);
312 // // in this case, this method will return the composite location that starts with 'this'
313 // FlowGraph flowGraph = getFlowGraph(md);
314 // Set<FlowNode> returnNodeSet = flowGraph.getReturnNodeSet();
321 private void updateFlowGraph() {
323 LinkedList<MethodDescriptor> methodDescList =
324 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
326 while (!methodDescList.isEmpty()) {
327 MethodDescriptor md = methodDescList.removeLast();
328 if (state.SSJAVADEBUG) {
329 System.out.println();
330 System.out.println("SSJAVA: Updating a flow graph: " + md);
331 propagateFlowsFromCalleesWithNoCompositeLocation(md);
336 public Map<NTuple<Descriptor>, NTuple<Descriptor>> getMapCallerArgToCalleeParam(
337 MethodInvokeNode min) {
339 if (!mapMethodInvokeNodeToMapCallerArgToCalleeArg.containsKey(min)) {
340 mapMethodInvokeNodeToMapCallerArgToCalleeArg.put(min,
341 new HashMap<NTuple<Descriptor>, NTuple<Descriptor>>());
344 return mapMethodInvokeNodeToMapCallerArgToCalleeArg.get(min);
347 public void addMapCallerArgToCalleeParam(MethodInvokeNode min, NTuple<Descriptor> callerArg,
348 NTuple<Descriptor> calleeParam) {
349 getMapCallerArgToCalleeParam(min).put(callerArg, calleeParam);
352 private void assignCompositeLocation() {
353 calculateGlobalValueFlowCompositeLocation();
354 translateCompositeLocationAssignmentToFlowGraph();
357 private void translateCompositeLocationAssignmentToFlowGraph() {
358 System.out.println("\nSSJAVA: Translate composite location assignments to flow graphs:");
359 MethodDescriptor methodEventLoopDesc = ssjava.getMethodContainingSSJavaLoop();
360 translateCompositeLocationAssignmentToFlowGraph(methodEventLoopDesc);
363 private void translateCompositeLocationAssignmentToFlowGraph2() {
364 System.out.println("\nSSJAVA: Translate composite location assignments to flow graphs:");
365 MethodDescriptor methodEventLoopDesc = ssjava.getMethodContainingSSJavaLoop();
366 translateCompositeLocationAssignmentToFlowGraph(methodEventLoopDesc);
369 private void addAdditionalOrderingConstraints() {
370 System.out.println("\nSSJAVA: Add addtional ordering constriants:");
371 MethodDescriptor methodEventLoopDesc = ssjava.getMethodContainingSSJavaLoop();
372 addAddtionalOrderingConstraints(methodEventLoopDesc);
373 // calculateReturnHolderLocation();
376 private void calculateReturnHolderLocation() {
377 LinkedList<MethodDescriptor> methodDescList =
378 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
380 while (!methodDescList.isEmpty()) {
381 MethodDescriptor md = methodDescList.removeLast();
383 FlowGraph fg = getFlowGraph(md);
384 Set<FlowNode> nodeSet = fg.getNodeSet();
385 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
386 FlowNode flowNode = (FlowNode) iterator.next();
387 if (flowNode.isFromHolder()) {
388 calculateCompositeLocationFromFlowGraph(md, flowNode);
395 private void updateCompositeLocationAssignments() {
397 LinkedList<MethodDescriptor> methodDescList =
398 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
400 while (!methodDescList.isEmpty()) {
401 MethodDescriptor md = methodDescList.removeLast();
403 System.out.println("\n#updateCompositeLocationAssignments=" + md);
405 FlowGraph flowGraph = getFlowGraph(md);
407 MethodSummary methodSummary = getMethodSummary(md);
409 Set<FlowNode> nodeSet = flowGraph.getNodeSet();
410 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
411 FlowNode node = (FlowNode) iterator.next();
412 System.out.println("-node=" + node + " node.getDescTuple=" + node.getDescTuple());
413 if (node.getCompositeLocation() != null) {
414 CompositeLocation compLoc = node.getCompositeLocation();
415 CompositeLocation updatedCompLoc = updateCompositeLocation(compLoc);
416 node.setCompositeLocation(updatedCompLoc);
417 System.out.println("---updatedCompLoc1=" + updatedCompLoc);
419 NTuple<Descriptor> descTuple = node.getDescTuple();
420 System.out.println("update desc=" + descTuple);
421 CompositeLocation compLoc = convertToCompositeLocation(md, descTuple);
422 compLoc = updateCompositeLocation(compLoc);
423 node.setCompositeLocation(compLoc);
424 System.out.println("---updatedCompLoc2=" + compLoc);
427 if (node.isDeclaratonNode()) {
428 Descriptor localVarDesc = node.getDescTuple().get(0);
429 CompositeLocation compLoc = updateCompositeLocation(node.getCompositeLocation());
430 methodSummary.addMapVarNameToInferCompLoc(localVarDesc, compLoc);
434 // update PCLOC and RETURNLOC if they have a composite location assignment
435 if (methodSummary.getRETURNLoc() != null) {
436 methodSummary.setRETURNLoc(updateCompositeLocation(methodSummary.getRETURNLoc()));
438 if (methodSummary.getPCLoc() != null) {
439 methodSummary.setPCLoc(updateCompositeLocation(methodSummary.getPCLoc()));
446 private CompositeLocation updateCompositeLocation(CompositeLocation compLoc) {
447 CompositeLocation updatedCompLoc = new CompositeLocation();
448 for (int i = 0; i < compLoc.getSize(); i++) {
449 Location loc = compLoc.get(i);
450 String nodeIdentifier = loc.getLocIdentifier();
451 Descriptor enclosingDesc = loc.getDescriptor();
453 if (!enclosingDesc.equals(GLOBALDESC)) {
454 LocationSummary locSummary = getLocationSummary(enclosingDesc);
455 HierarchyGraph scGraph = getSkeletonCombinationHierarchyGraph(enclosingDesc);
456 if (scGraph != null) {
457 HNode curNode = scGraph.getCurrentHNode(nodeIdentifier);
458 if (curNode != null) {
459 nodeIdentifier = curNode.getName();
462 locName = locSummary.getLocationName(nodeIdentifier);
464 locName = nodeIdentifier;
466 Location updatedLoc = new Location(enclosingDesc, locName);
467 updatedCompLoc.addLocation(updatedLoc);
470 return updatedCompLoc;
473 private void translateCompositeLocationAssignmentToFlowGraph(MethodDescriptor mdCaller) {
475 System.out.println("\n\n###translateCompositeLocationAssignmentToFlowGraph mdCaller="
478 // First, assign a composite location to a node in the flow graph
479 GlobalFlowGraph callerGlobalFlowGraph = getSubGlobalFlowGraph(mdCaller);
481 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
482 Map<Location, CompositeLocation> callerMapLocToCompLoc =
483 callerGlobalFlowGraph.getMapLocationToInferCompositeLocation();
485 Set<Location> methodLocSet = callerMapLocToCompLoc.keySet();
486 for (Iterator iterator = methodLocSet.iterator(); iterator.hasNext();) {
487 Location methodLoc = (Location) iterator.next();
488 if (methodLoc.getDescriptor().equals(mdCaller)) {
489 CompositeLocation inferCompLoc = callerMapLocToCompLoc.get(methodLoc);
490 assignCompositeLocationToFlowGraph(callerFlowGraph, methodLoc, inferCompLoc);
494 Set<MethodInvokeNode> minSet = mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
496 Set<MethodDescriptor> calleeSet = new HashSet<MethodDescriptor>();
497 for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
498 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
499 // need to translate a composite location that is started with the base
501 translateMapLocationToInferCompositeLocationToCalleeGraph(callerGlobalFlowGraph, min);
502 MethodDescriptor mdCallee = min.getMethod();
503 calleeSet.add(mdCallee);
507 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
508 MethodDescriptor callee = (MethodDescriptor) iterator.next();
509 translateCompositeLocationAssignmentToFlowGraph(callee);
514 private CompositeLocation translateArgCompLocToParamCompLoc(MethodInvokeNode min,
515 CompositeLocation argCompLoc) {
517 System.out.println("--------translateArgCompLocToParamCompLoc argCompLoc=" + argCompLoc);
518 MethodDescriptor mdCallee = min.getMethod();
519 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
521 NTuple<Location> argLocTuple = argCompLoc.getTuple();
522 Location argLocalLoc = argLocTuple.get(0);
524 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
525 Set<Integer> idxSet = mapIdxToArgTuple.keySet();
526 for (Iterator iterator2 = idxSet.iterator(); iterator2.hasNext();) {
527 Integer idx = (Integer) iterator2.next();
529 if (idx == 0 && !min.getMethod().isStatic()) {
533 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(idx);
534 if (argTuple.size() > 0 && argTuple.get(0).equals(argLocalLoc.getLocDescriptor())) {
535 // it matches with the current argument composite location
536 // so what is the corresponding parameter local descriptor?
537 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
538 System.out.println("----------found paramNode=" + paramNode);
539 NTuple<Descriptor> paramDescTuple = paramNode.getCurrentDescTuple();
541 NTuple<Location> newParamTupleFromArgTuple = translateToLocTuple(mdCallee, paramDescTuple);
542 for (int i = 1; i < argLocTuple.size(); i++) {
543 newParamTupleFromArgTuple.add(argLocTuple.get(i));
546 System.out.println("-----------newParamTuple=" + newParamTupleFromArgTuple);
547 return new CompositeLocation(newParamTupleFromArgTuple);
554 private void addAddtionalOrderingConstraints(MethodDescriptor mdCaller) {
556 // First, assign a composite location to a node in the flow graph
557 GlobalFlowGraph callerGlobalFlowGraph = getSubGlobalFlowGraph(mdCaller);
559 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
560 Map<Location, CompositeLocation> callerMapLocToCompLoc =
561 callerGlobalFlowGraph.getMapLocationToInferCompositeLocation();
562 Set<Location> methodLocSet = callerMapLocToCompLoc.keySet();
564 Set<MethodInvokeNode> minSet = mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
566 Set<MethodDescriptor> calleeSet = new HashSet<MethodDescriptor>();
567 for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
568 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
569 MethodDescriptor mdCallee = min.getMethod();
570 calleeSet.add(mdCallee);
573 // add an additional ordering constraint
574 // if the first element of a parameter composite location matches 'this' reference,
575 // the corresponding argument in the caller is required to be higher than the translated
576 // parameter location in the caller lattice
578 // addOrderingConstraintFromCompLocParamToArg(mdCaller, min);
581 // update return flow nodes in the caller
582 CompositeLocation returnLoc = getMethodSummary(mdCallee).getRETURNLoc();
583 System.out.println("### min=" + min.printNode(0) + " returnLoc=" + returnLoc);
584 if (returnLoc != null && returnLoc.get(0).getLocDescriptor().equals(mdCallee.getThis())
585 && returnLoc.getSize() > 1) {
586 System.out.println("###RETURN COMP LOC=" + returnLoc);
587 NTuple<Location> returnLocTuple = returnLoc.getTuple();
588 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
589 System.out.println("###basetuple=" + baseTuple);
590 NTuple<Descriptor> newReturnTuple = baseTuple.clone();
591 for (int i = 1; i < returnLocTuple.size(); i++) {
592 newReturnTuple.add(returnLocTuple.get(i).getLocDescriptor());
594 System.out.println("###NEW RETURN TUPLE FOR CALLER=" + newReturnTuple);
596 FlowReturnNode holderNode = callerFlowGraph.getFlowReturnNode(min);
597 NodeTupleSet holderTupleSet =
598 getNodeTupleSetFromReturnNode(getFlowGraph(mdCaller), holderNode);
600 callerFlowGraph.getFlowReturnNode(min).setNewTuple(newReturnTuple);
602 // then need to remove old constraints
604 System.out.println("###REMOVE OLD CONSTRAINTS=" + holderNode);
605 for (Iterator<NTuple<Descriptor>> iter = holderTupleSet.iterator(); iter.hasNext();) {
606 NTuple<Descriptor> tupleFromHolder = iter.next();
607 Set<FlowEdge> holderOutEdge = callerFlowGraph.getOutEdgeSet(holderNode);
608 for (Iterator iterator2 = holderOutEdge.iterator(); iterator2.hasNext();) {
609 FlowEdge outEdge = (FlowEdge) iterator2.next();
610 NTuple<Descriptor> toberemovedTuple = outEdge.getEndTuple();
611 System.out.println("---remove " + tupleFromHolder + " -> " + toberemovedTuple);
612 callerFlowGraph.removeEdge(tupleFromHolder, toberemovedTuple);
617 // if the return loc set was empty and later pcloc was connected to the return loc
618 // need to make sure that return loc reflects to this changes.
619 FlowReturnNode flowReturnNode = callerFlowGraph.getFlowReturnNode(min);
620 if (flowReturnNode != null && flowReturnNode.getReturnTupleSet().isEmpty()) {
622 if (needToUpdateReturnLocHolder(min.getMethod(), flowReturnNode)) {
623 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
624 NTuple<Descriptor> newReturnTuple = baseTuple.clone();
625 flowReturnNode.addTuple(newReturnTuple);
634 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
635 MethodDescriptor callee = (MethodDescriptor) iterator.next();
636 addAddtionalOrderingConstraints(callee);
641 private boolean needToUpdateReturnLocHolder(MethodDescriptor mdCallee,
642 FlowReturnNode flowReturnNode) {
643 FlowGraph fg = getFlowGraph(mdCallee);
644 MethodSummary summary = getMethodSummary(mdCallee);
645 CompositeLocation returnCompLoc = summary.getRETURNLoc();
646 NTuple<Descriptor> returnDescTuple = translateToDescTuple(returnCompLoc.getTuple());
647 Set<FlowNode> incomingNodeToReturnNode =
648 fg.getIncomingFlowNodeSet(fg.getFlowNode(returnDescTuple));
649 for (Iterator iterator = incomingNodeToReturnNode.iterator(); iterator.hasNext();) {
650 FlowNode inNode = (FlowNode) iterator.next();
651 if (inNode.getDescTuple().get(0).equals(mdCallee.getThis())) {
658 private void addMapMethodDescToMethodInvokeNodeSet(MethodInvokeNode min) {
659 MethodDescriptor md = min.getMethod();
660 if (!mapMethodDescToMethodInvokeNodeSet.containsKey(md)) {
661 mapMethodDescToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
663 mapMethodDescToMethodInvokeNodeSet.get(md).add(min);
666 private Set<MethodInvokeNode> getMethodInvokeNodeSetByMethodDesc(MethodDescriptor md) {
667 if (!mapMethodDescToMethodInvokeNodeSet.containsKey(md)) {
668 mapMethodDescToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
670 return mapMethodDescToMethodInvokeNodeSet.get(md);
673 private void addOrderingConstraintFromCompLocParamToArg(MethodDescriptor mdCaller,
674 MethodInvokeNode min) {
675 System.out.println("-addOrderingConstraintFromCompLocParamToArg=" + min.printNode(0));
677 GlobalFlowGraph globalGraph = getSubGlobalFlowGraph(ssjava.getMethodContainingSSJavaLoop());
679 Set<NTuple<Location>> pcLocTupleSet = getPCLocTupleSet(min);
681 MethodDescriptor mdCallee = min.getMethod();
683 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
684 for (int idx = 0; idx < calleeFlowGraph.getNumParameters(); idx++) {
685 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
686 NTuple<Location> globalParamLocTuple =
687 translateToLocTuple(mdCallee, paramNode.getDescTuple());
688 translateToLocTuple(mdCallee, paramNode.getDescTuple());
689 CompositeLocation compLoc = paramNode.getCompositeLocation();
690 System.out.println("---paramNode=" + paramNode + " compLoc=" + compLoc);
691 if (compLoc != null) {
692 NTuple<Descriptor> argTuple = getNodeTupleByArgIdx(min, idx);
693 NTuple<Location> globalArgLocTuple = translateToLocTuple(mdCaller, argTuple);
695 if (!isLiteralValueLocTuple(globalArgLocTuple)
696 && !isLiteralValueLocTuple(globalParamLocTuple)) {
697 if (!globalGraph.hasValueFlowEdge(globalArgLocTuple, globalParamLocTuple)) {
698 System.out.println("----- add global flow globalArgLocTuple=" + globalArgLocTuple
699 + "-> globalParamLocTuple=" + globalParamLocTuple);
701 System.out.println("B1");
702 globalGraph.addValueFlowEdge(globalArgLocTuple, globalParamLocTuple);
706 for (Iterator iterator = pcLocTupleSet.iterator(); iterator.hasNext();) {
707 NTuple<Location> pcLocTuple = (NTuple<Location>) iterator.next();
709 if (!isLiteralValueLocTuple(pcLocTuple) && !isLiteralValueLocTuple(globalParamLocTuple)) {
710 if (!globalGraph.hasValueFlowEdge(pcLocTuple, globalParamLocTuple)) {
712 .println("----- add global flow PCLOC="
714 + "-> globalParamLocTu!globalArgLocTuple.get(0).getLocDescriptor().equals(LITERALDESC)ple="
715 + globalParamLocTuple);
717 System.out.println("B2");
719 globalGraph.addValueFlowEdge(pcLocTuple, globalParamLocTuple);
728 private boolean isLiteralValueLocTuple(NTuple<Location> locTuple) {
729 return locTuple.get(0).getLocDescriptor().equals(LITERALDESC);
732 public void assignCompositeLocationToFlowGraph(FlowGraph flowGraph, Location loc,
733 CompositeLocation inferCompLoc) {
734 Descriptor localDesc = loc.getLocDescriptor();
736 Set<FlowNode> nodeSet = flowGraph.getNodeSet();
737 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
738 FlowNode node = (FlowNode) iterator.next();
739 if (node.getDescTuple().startsWith(localDesc)
740 && !node.getDescTuple().get(0).equals(LITERALDESC)) {
741 // need to assign the inferred composite location to this node
742 CompositeLocation newCompLoc = generateCompositeLocation(node.getDescTuple(), inferCompLoc);
743 node.setCompositeLocation(newCompLoc);
744 System.out.println("SET Node=" + node + " inferCompLoc=" + newCompLoc);
749 private CompositeLocation generateCompositeLocation(NTuple<Descriptor> nodeDescTuple,
750 CompositeLocation inferCompLoc) {
752 System.out.println("generateCompositeLocation=" + nodeDescTuple + " with inferCompLoc="
755 MethodDescriptor md = (MethodDescriptor) inferCompLoc.get(0).getDescriptor();
757 CompositeLocation newCompLoc = new CompositeLocation();
758 for (int i = 0; i < inferCompLoc.getSize(); i++) {
759 newCompLoc.addLocation(inferCompLoc.get(i));
762 Descriptor lastDescOfPrefix = nodeDescTuple.get(0);
763 Descriptor enclosingDescriptor;
764 if (lastDescOfPrefix instanceof InterDescriptor) {
765 enclosingDescriptor = getFlowGraph(md).getEnclosingDescriptor(lastDescOfPrefix);
767 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
770 for (int i = 1; i < nodeDescTuple.size(); i++) {
771 Descriptor desc = nodeDescTuple.get(i);
772 Location locElement = new Location(enclosingDescriptor, desc);
773 newCompLoc.addLocation(locElement);
775 enclosingDescriptor = ((FieldDescriptor) desc).getClassDescriptor();
781 private void translateMapLocationToInferCompositeLocationToCalleeGraph(
782 GlobalFlowGraph callerGraph, MethodInvokeNode min) {
784 MethodDescriptor mdCallee = min.getMethod();
785 MethodDescriptor mdCaller = callerGraph.getMethodDescriptor();
786 Map<Location, CompositeLocation> callerMapLocToCompLoc =
787 callerGraph.getMapLocationToInferCompositeLocation();
789 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
791 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
792 GlobalFlowGraph calleeGlobalGraph = getSubGlobalFlowGraph(mdCallee);
794 NTuple<Location> baseLocTuple = null;
795 if (mapMethodInvokeNodeToBaseTuple.containsKey(min)) {
796 baseLocTuple = translateToLocTuple(mdCaller, mapMethodInvokeNodeToBaseTuple.get(min));
799 System.out.println("\n-#translate caller=" + mdCaller + " infer composite loc to callee="
800 + mdCallee + " baseLocTuple=" + baseLocTuple);
801 // System.out.println("-mapIdxToArgTuple=" + mapIdxToArgTuple);
802 // System.out.println("-callerMapLocToCompLoc=" + callerMapLocToCompLoc);
804 Set<Location> keySet = callerMapLocToCompLoc.keySet();
805 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
806 Location key = (Location) iterator.next();
807 CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(key);
809 if (!key.getDescriptor().equals(mdCaller)) {
811 CompositeLocation newCalleeCompLoc;
812 if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
813 // System.out.println("-----need to translate callerCompLoc=" + callerCompLoc
814 // + " with baseTuple=" + baseLocTuple);
816 translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
818 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
819 System.out.println("1---key=" + key + " callerCompLoc=" + callerCompLoc
820 + " newCalleeCompLoc=" + newCalleeCompLoc);
821 System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
822 if (!newCalleeCompLoc.get(0).getDescriptor().equals(mdCallee)) {
826 // System.out.println("-----baseLoctuple=" + baseLocTuple);
828 // check if it is the global access
829 Location compLocFirstElement = callerCompLoc.getTuple().get(0);
830 if (compLocFirstElement.getDescriptor().equals(mdCallee)
831 && compLocFirstElement.getLocDescriptor().equals(GLOBALDESC)) {
833 newCalleeCompLoc = new CompositeLocation();
834 Location newMethodLoc = new Location(mdCallee, GLOBALDESC);
836 newCalleeCompLoc.addLocation(newMethodLoc);
837 for (int i = 1; i < callerCompLoc.getSize(); i++) {
838 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
840 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
841 System.out.println("2---key=" + key + " callerCompLoc=" + callerCompLoc
842 + " newCalleeCompLoc=" + newCalleeCompLoc);
843 System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
846 int paramIdx = getParamIdx(callerCompLoc, mapIdxToArgTuple);
847 if (paramIdx == -1) {
848 // here, the first element of the current composite location comes from the current
850 // so transfer the same composite location to the callee
851 if (!calleeGlobalGraph.contrainsInferCompositeLocationMapKey(key)) {
852 if (callerCompLoc.get(0).getDescriptor().equals(mdCallee)) {
853 System.out.println("3---key=" + key + " callerCompLoc=" + callerCompLoc
854 + " newCalleeCompLoc=" + callerCompLoc);
855 System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
856 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, callerCompLoc);
858 System.out.println("3---SKIP key=" + key + " callerCompLoc=" + callerCompLoc);
864 // It is the case where two parameters have relative orderings between them by having
865 // composite locations
866 // if we found the param idx, it means that the first part of the caller composite
867 // location corresponds to the one of arguments.
868 // for example, if the caller argument is <<caller.this>,<Decoder.br>>
869 // and the current caller composite location mapping
870 // <<caller.this>,<Decoder.br>,<Br.value>>
871 // and the parameter which matches with the caller argument is 'Br brParam'
872 // then, the translated callee composite location will be <<callee.brParam>,<Br.value>>
873 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(paramIdx);
875 FlowNode paramFlowNode = calleeFlowGraph.getParamFlowNode(paramIdx);
876 NTuple<Location> paramLocTuple =
877 translateToLocTuple(mdCallee, paramFlowNode.getDescTuple());
878 newCalleeCompLoc = new CompositeLocation();
879 for (int i = 0; i < paramLocTuple.size(); i++) {
880 newCalleeCompLoc.addLocation(paramLocTuple.get(i));
882 for (int i = argTuple.size(); i < callerCompLoc.getSize(); i++) {
883 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
885 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
886 System.out.println("4---key=" + key + " callerCompLoc=" + callerCompLoc
887 + " newCalleeCompLoc=" + newCalleeCompLoc);
888 System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
890 // System.out.println("-----argTuple=" + argTuple + " caller=" + mdCaller +
893 // System.out.println("-----paramIdx=" + paramIdx + " paramFlowNode=" + paramFlowNode);
902 // System.out.println("-----*AFTER TRANSLATING COMP LOC MAPPING, CALLEE MAPPING="
903 // + calleeGlobalGraph.getMapLocationToInferCompositeLocation());
905 System.out.println("#ASSIGN COMP LOC TO CALLEE PARAMS: callee=" + mdCallee + " caller="
907 // If the location of an argument has a composite location
908 // need to assign a proper composite location to the corresponding callee parameter
909 Set<Integer> idxSet = mapIdxToArgTuple.keySet();
910 for (Iterator iterator = idxSet.iterator(); iterator.hasNext();) {
911 Integer idx = (Integer) iterator.next();
913 if (idx == 0 && !min.getMethod().isStatic()) {
917 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(idx);
918 System.out.println("-argTuple=" + argTuple + " idx=" + idx);
919 if (argTuple.size() > 0) {
920 // check if an arg tuple has been already assigned to a composite location
921 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argTuple);
922 Location argLocalLoc = argLocTuple.get(0);
924 // if (!isPrimitiveType(argTuple)) {
925 if (callerMapLocToCompLoc.containsKey(argLocalLoc)) {
927 CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(argLocalLoc);
928 for (int i = 1; i < argLocTuple.size(); i++) {
929 callerCompLoc.addLocation(argLocTuple.get(i));
932 System.out.println("---callerCompLoc=" + callerCompLoc);
934 // if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
936 FlowNode calleeParamFlowNode = calleeFlowGraph.getParamFlowNode(idx);
938 NTuple<Descriptor> calleeParamDescTuple = calleeParamFlowNode.getDescTuple();
939 NTuple<Location> calleeParamLocTuple =
940 translateToLocTuple(mdCallee, calleeParamDescTuple);
942 int refParamIdx = getParamIdx(callerCompLoc, mapIdxToArgTuple);
943 System.out.println("-----paramIdx=" + refParamIdx);
944 if (refParamIdx == 0 && !mdCallee.isStatic()) {
946 System.out.println("-------need to translate callerCompLoc=" + callerCompLoc
947 + " with baseTuple=" + baseLocTuple + " calleeParamLocTuple="
948 + calleeParamLocTuple);
950 CompositeLocation newCalleeCompLoc =
951 translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
953 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
956 System.out.println("---------key=" + calleeParamLocTuple.get(0) + " callerCompLoc="
957 + callerCompLoc + " newCalleeCompLoc=" + newCalleeCompLoc);
959 } else if (refParamIdx != -1) {
960 // the first element of an argument composite location matches with one of paramtere
961 // composite locations
963 System.out.println("-------param match case=");
965 NTuple<Descriptor> argTupleRef = mapIdxToArgTuple.get(refParamIdx);
966 FlowNode refParamFlowNode = calleeFlowGraph.getParamFlowNode(refParamIdx);
967 NTuple<Location> refParamLocTuple =
968 translateToLocTuple(mdCallee, refParamFlowNode.getDescTuple());
970 System.out.println("---------refParamLocTuple=" + refParamLocTuple
971 + " from argTupleRef=" + argTupleRef);
973 CompositeLocation newCalleeCompLoc = new CompositeLocation();
974 for (int i = 0; i < refParamLocTuple.size(); i++) {
975 newCalleeCompLoc.addLocation(refParamLocTuple.get(i));
977 for (int i = argTupleRef.size(); i < callerCompLoc.getSize(); i++) {
978 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
981 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
984 calleeParamFlowNode.setCompositeLocation(newCalleeCompLoc);
985 System.out.println("-----------key=" + calleeParamLocTuple.get(0) + " callerCompLoc="
986 + callerCompLoc + " newCalleeCompLoc=" + newCalleeCompLoc);
989 CompositeLocation newCalleeCompLoc =
990 calculateCompositeLocationFromSubGlobalGraph(mdCallee, calleeParamFlowNode);
991 if (newCalleeCompLoc != null) {
992 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
994 calleeParamFlowNode.setCompositeLocation(newCalleeCompLoc);
998 System.out.println("-----------------calleeParamFlowNode="
999 + calleeParamFlowNode.getCompositeLocation());
1010 private CompositeLocation calculateCompositeLocationFromSubGlobalGraph(MethodDescriptor md,
1011 FlowNode paramNode) {
1013 System.out.println("#############################################################");
1014 System.out.println("calculateCompositeLocationFromSubGlobalGraph=" + paramNode);
1016 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
1017 NTuple<Location> paramLocTuple = translateToLocTuple(md, paramNode.getDescTuple());
1018 GlobalFlowNode paramGlobalNode = subGlobalFlowGraph.getFlowNode(paramLocTuple);
1020 List<NTuple<Location>> prefixList = calculatePrefixList(subGlobalFlowGraph, paramGlobalNode);
1022 Location prefixLoc = paramLocTuple.get(0);
1024 Set<GlobalFlowNode> reachableNodeSet =
1025 subGlobalFlowGraph.getReachableNodeSetByPrefix(paramGlobalNode.getLocTuple().get(0));
1026 // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
1028 // System.out.println("node=" + node + " prefixList=" + prefixList);
1030 for (int i = 0; i < prefixList.size(); i++) {
1031 NTuple<Location> curPrefix = prefixList.get(i);
1032 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
1034 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1035 GlobalFlowNode reachNode = (GlobalFlowNode) iterator2.next();
1036 if (reachNode.getLocTuple().startsWith(curPrefix)) {
1037 reachableCommonPrefixSet.add(reachNode.getLocTuple());
1040 // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
1042 if (!reachableCommonPrefixSet.isEmpty()) {
1044 MethodDescriptor curPrefixFirstElementMethodDesc =
1045 (MethodDescriptor) curPrefix.get(0).getDescriptor();
1047 MethodDescriptor nodePrefixLocFirstElementMethodDesc =
1048 (MethodDescriptor) prefixLoc.getDescriptor();
1050 // System.out.println("curPrefixFirstElementMethodDesc=" +
1051 // curPrefixFirstElementMethodDesc);
1052 // System.out.println("nodePrefixLocFirstElementMethodDesc="
1053 // + nodePrefixLocFirstElementMethodDesc);
1055 if (curPrefixFirstElementMethodDesc.equals(nodePrefixLocFirstElementMethodDesc)
1056 || isTransitivelyCalledFrom(nodePrefixLocFirstElementMethodDesc,
1057 curPrefixFirstElementMethodDesc)) {
1060 // if (!node.getLocTuple().startsWith(curPrefix.get(0))) {
1062 Location curPrefixLocalLoc = curPrefix.get(0);
1063 if (subGlobalFlowGraph.mapLocationToInferCompositeLocation.containsKey(curPrefixLocalLoc)) {
1064 // in this case, the local variable of the current prefix has already got a composite
1066 // so we just ignore the current composite location.
1068 // System.out.println("HERE WE DO NOT ASSIGN A COMPOSITE LOCATION TO =" + node
1069 // + " DUE TO " + curPrefix);
1073 if (!needToGenerateCompositeLocation(paramGlobalNode, curPrefix)) {
1074 System.out.println("NO NEED TO GENERATE COMP LOC to " + paramGlobalNode
1075 + " with prefix=" + curPrefix);
1076 // System.out.println("prefixList=" + prefixList);
1077 // System.out.println("reachableNodeSet=" + reachableNodeSet);
1081 Location targetLocalLoc = paramGlobalNode.getLocTuple().get(0);
1082 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
1083 System.out.println("NEED TO ASSIGN COMP LOC TO " + paramGlobalNode + " with prefix="
1085 System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc=" + newCompLoc);
1087 // makes sure that a newly generated location appears in the hierarchy graph
1088 for (int compIdx = 0; compIdx < newCompLoc.getSize(); compIdx++) {
1089 Location curLoc = newCompLoc.get(compIdx);
1090 getHierarchyGraph(curLoc.getDescriptor()).getHNode(curLoc.getLocDescriptor());
1093 subGlobalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
1105 private int getParamIdx(CompositeLocation compLoc,
1106 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple) {
1108 // if the composite location is started with the argument descriptor
1109 // return the argument's index. o.t. return -1
1111 Set<Integer> keySet = mapIdxToArgTuple.keySet();
1112 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1113 Integer key = (Integer) iterator.next();
1114 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(key);
1115 if (argTuple.size() > 0 && translateToDescTuple(compLoc.getTuple()).startsWith(argTuple)) {
1116 System.out.println("compLoc.getTuple=" + compLoc + " is started with " + argTuple);
1117 return key.intValue();
1124 private boolean isPrimitiveType(NTuple<Descriptor> argTuple) {
1126 Descriptor lastDesc = argTuple.get(argTuple.size() - 1);
1128 if (lastDesc instanceof FieldDescriptor) {
1129 return ((FieldDescriptor) lastDesc).getType().isPrimitive();
1130 } else if (lastDesc instanceof VarDescriptor) {
1131 return ((VarDescriptor) lastDesc).getType().isPrimitive();
1132 } else if (lastDesc instanceof InterDescriptor) {
1139 private CompositeLocation translateCompositeLocationToCallee(CompositeLocation callerCompLoc,
1140 NTuple<Location> baseLocTuple, MethodDescriptor mdCallee) {
1142 CompositeLocation newCalleeCompLoc = new CompositeLocation();
1144 Location calleeThisLoc = new Location(mdCallee, mdCallee.getThis());
1145 newCalleeCompLoc.addLocation(calleeThisLoc);
1147 // remove the base tuple from the caller
1148 // ex; In the method invoation foo.bar.methodA(), the callee will have the composite location
1149 // ,which is relative to the 'this' variable, <THIS,...>
1150 for (int i = baseLocTuple.size(); i < callerCompLoc.getSize(); i++) {
1151 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
1154 return newCalleeCompLoc;
1158 private void calculateGlobalValueFlowCompositeLocation() {
1160 System.out.println("SSJAVA: Calculate composite locations in the global value flow graph");
1161 MethodDescriptor methodDescEventLoop = ssjava.getMethodContainingSSJavaLoop();
1162 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(methodDescEventLoop);
1164 Set<Location> calculatedPrefixSet = new HashSet<Location>();
1166 Set<GlobalFlowNode> nodeSet = globalFlowGraph.getNodeSet();
1168 next: for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1169 GlobalFlowNode node = (GlobalFlowNode) iterator.next();
1171 Location prefixLoc = node.getLocTuple().get(0);
1173 if (calculatedPrefixSet.contains(prefixLoc)) {
1174 // the prefix loc has been already assigned to a composite location
1178 calculatedPrefixSet.add(prefixLoc);
1180 // Set<GlobalFlowNode> incomingNodeSet = globalFlowGraph.getIncomingNodeSet(node);
1181 List<NTuple<Location>> prefixList = calculatePrefixList(globalFlowGraph, node);
1183 Set<GlobalFlowNode> reachableNodeSet =
1184 globalFlowGraph.getReachableNodeSetByPrefix(node.getLocTuple().get(0));
1185 // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
1187 // System.out.println("node=" + node + " prefixList=" + prefixList);
1188 System.out.println("---prefixList=" + prefixList);
1190 nextprefix: for (int i = 0; i < prefixList.size(); i++) {
1191 NTuple<Location> curPrefix = prefixList.get(i);
1192 System.out.println("---curPrefix=" + curPrefix);
1193 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
1195 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1196 GlobalFlowNode reachNode = (GlobalFlowNode) iterator2.next();
1197 if (reachNode.getLocTuple().startsWith(curPrefix)) {
1198 reachableCommonPrefixSet.add(reachNode.getLocTuple());
1201 // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
1203 if (!reachableCommonPrefixSet.isEmpty()) {
1205 MethodDescriptor curPrefixFirstElementMethodDesc =
1206 (MethodDescriptor) curPrefix.get(0).getDescriptor();
1208 MethodDescriptor nodePrefixLocFirstElementMethodDesc =
1209 (MethodDescriptor) prefixLoc.getDescriptor();
1211 // System.out.println("curPrefixFirstElementMethodDesc=" +
1212 // curPrefixFirstElementMethodDesc);
1213 // System.out.println("nodePrefixLocFirstElementMethodDesc="
1214 // + nodePrefixLocFirstElementMethodDesc);
1216 if (curPrefixFirstElementMethodDesc.equals(nodePrefixLocFirstElementMethodDesc)
1217 || isTransitivelyCalledFrom(nodePrefixLocFirstElementMethodDesc,
1218 curPrefixFirstElementMethodDesc)) {
1221 // if (!node.getLocTuple().startsWith(curPrefix.get(0))) {
1223 Location curPrefixLocalLoc = curPrefix.get(0);
1224 if (globalFlowGraph.mapLocationToInferCompositeLocation.containsKey(curPrefixLocalLoc)) {
1225 // in this case, the local variable of the current prefix has already got a composite
1227 // so we just ignore the current composite location.
1229 // System.out.println("HERE WE DO NOT ASSIGN A COMPOSITE LOCATION TO =" + node
1230 // + " DUE TO " + curPrefix);
1235 if (!needToGenerateCompositeLocation(node, curPrefix)) {
1236 System.out.println("NO NEED TO GENERATE COMP LOC to " + node + " with prefix="
1238 // System.out.println("prefixList=" + prefixList);
1239 // System.out.println("reachableNodeSet=" + reachableNodeSet);
1240 continue nextprefix;
1243 Location targetLocalLoc = node.getLocTuple().get(0);
1244 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
1245 System.out.println("NEED TO ASSIGN COMP LOC TO " + node + " with prefix=" + curPrefix);
1246 System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc="
1248 globalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
1263 private boolean checkFlowNodeReturnThisField(MethodDescriptor md) {
1265 MethodDescriptor methodDescEventLoop = ssjava.getMethodContainingSSJavaLoop();
1266 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(methodDescEventLoop);
1268 FlowGraph flowGraph = getFlowGraph(md);
1270 ClassDescriptor enclosingDesc = getClassTypeDescriptor(md.getThis());
1271 if (enclosingDesc == null) {
1276 Set<FlowNode> returnNodeSet = flowGraph.getReturnNodeSet();
1277 Set<GlobalFlowNode> globalReturnNodeSet = new HashSet<GlobalFlowNode>();
1278 for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
1279 FlowNode flowNode = (FlowNode) iterator.next();
1280 NTuple<Location> locTuple = translateToLocTuple(md, flowNode.getDescTuple());
1281 GlobalFlowNode globalReturnNode = globalFlowGraph.getFlowNode(locTuple);
1282 globalReturnNodeSet.add(globalReturnNode);
1284 List<NTuple<Location>> prefixList = calculatePrefixList(globalFlowGraph, globalReturnNode);
1285 for (int i = 0; i < prefixList.size(); i++) {
1286 NTuple<Location> curPrefix = prefixList.get(i);
1287 ClassDescriptor cd =
1288 getClassTypeDescriptor(curPrefix.get(curPrefix.size() - 1).getLocDescriptor());
1289 if (cd != null && cd.equals(enclosingDesc)) {
1297 if (count == returnNodeSet.size()) {
1298 // in this case, all return nodes in the method returns values coming from a location that
1299 // starts with "this"
1301 System.out.println("$$$SET RETURN LOC TRUE=" + md);
1302 mapMethodDescriptorToCompositeReturnCase.put(md, Boolean.TRUE);
1304 // NameDescriptor returnLocDesc = new NameDescriptor("RLOC" + (locSeed++));
1305 // NTuple<Descriptor> rDescTuple = new NTuple<Descriptor>();
1306 // rDescTuple.add(md.getThis());
1307 // rDescTuple.add(returnLocDesc);
1309 // for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
1310 // FlowNode rnode = (FlowNode) iterator.next();
1311 // flowGraph.addValueFlowEdge(rnode.getDescTuple(), rDescTuple);
1314 // getMethodSummary(md).setRETURNLoc(new CompositeLocation(translateToLocTuple(md,
1318 mapMethodDescriptorToCompositeReturnCase.put(md, Boolean.FALSE);
1321 return mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
1325 private boolean needToGenerateCompositeLocation(GlobalFlowNode node, NTuple<Location> curPrefix) {
1326 // return true if there is a path between a node to which we want to give a composite location
1327 // and nodes which start with curPrefix
1329 System.out.println("---needToGenerateCompositeLocation curPrefix=" + curPrefix);
1331 Location targetLocalLoc = node.getLocTuple().get(0);
1333 MethodDescriptor md = (MethodDescriptor) targetLocalLoc.getDescriptor();
1334 FlowGraph flowGraph = getFlowGraph(md);
1336 FlowNode flowNode = flowGraph.getFlowNode(node.getDescTuple());
1337 Set<FlowNode> reachableSet = flowGraph.getReachFlowNodeSetFrom(flowNode);
1339 Set<FlowNode> paramNodeSet = flowGraph.getParamFlowNodeSet();
1340 for (Iterator iterator = paramNodeSet.iterator(); iterator.hasNext();) {
1341 FlowNode paramFlowNode = (FlowNode) iterator.next();
1342 if (curPrefix.startsWith(translateToLocTuple(md, paramFlowNode.getDescTuple()))) {
1343 System.out.println("here1?!");
1348 if (targetLocalLoc.getLocDescriptor() instanceof InterDescriptor) {
1349 Pair<MethodInvokeNode, Integer> pair =
1350 ((InterDescriptor) targetLocalLoc.getLocDescriptor()).getMethodArgIdxPair();
1353 System.out.println("$$$TARGETLOCALLOC HOLDER=" + targetLocalLoc);
1355 MethodInvokeNode min = pair.getFirst();
1356 Integer paramIdx = pair.getSecond();
1357 MethodDescriptor mdCallee = min.getMethod();
1359 FlowNode paramNode = getFlowGraph(mdCallee).getParamFlowNode(paramIdx);
1360 if (checkNodeReachToReturnNode(mdCallee, paramNode)) {
1361 System.out.println("here2?!");
1369 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
1370 Set<GlobalFlowNode> subGlobalReachableSet = subGlobalFlowGraph.getReachableNodeSetFrom(node);
1372 if (!md.isStatic()) {
1373 ClassDescriptor currentMethodThisType = getClassTypeDescriptor(md.getThis());
1374 for (int i = 0; i < curPrefix.size(); i++) {
1375 ClassDescriptor prefixType = getClassTypeDescriptor(curPrefix.get(i).getLocDescriptor());
1376 if (prefixType != null && prefixType.equals(currentMethodThisType)) {
1377 System.out.println("PREFIX TYPE MATCHES WITH=" + currentMethodThisType);
1379 if (mapMethodDescriptorToCompositeReturnCase.containsKey(md)) {
1380 boolean hasCompReturnLocWithThis =
1381 mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
1382 if (hasCompReturnLocWithThis) {
1383 if (checkNodeReachToReturnNode(md, flowNode)) {
1384 System.out.println("here3?!");
1390 for (Iterator iterator3 = subGlobalReachableSet.iterator(); iterator3.hasNext();) {
1391 GlobalFlowNode subGlobalReachalbeNode = (GlobalFlowNode) iterator3.next();
1392 if (subGlobalReachalbeNode.getLocTuple().get(0).getLocDescriptor().equals(md.getThis())) {
1393 System.out.println("PREFIX FOUND=" + subGlobalReachalbeNode);
1394 System.out.println("here4?!");
1402 // System.out.println("flowGraph.getReturnNodeSet()=" + flowGraph.getReturnNodeSet());
1403 // System.out.println("flowGraph.contains(node.getDescTuple())="
1404 // + flowGraph.contains(node.getDescTuple()) + " flowGraph.getFlowNode(node.getDescTuple())="
1405 // + flowGraph.getFlowNode(node.getDescTuple()));reachableSet
1407 // if (flowGraph.contains(node.getDescTuple())
1408 // && flowGraph.getReturnNodeSet().contains(flowGraph.getFlowNode(node.getDescTuple()))) {
1409 // // return checkFlowNodeReturnThisField(flowGraph);
1412 Location lastLocationOfPrefix = curPrefix.get(curPrefix.size() - 1);
1413 // check whether prefix appears in the list of parameters
1414 Set<MethodInvokeNode> minSet = mapMethodDescToMethodInvokeNodeSet.get(md);
1415 found: for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
1416 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
1417 Map<Integer, NTuple<Descriptor>> map = mapMethodInvokeNodeToArgIdxMap.get(min);
1418 Set<Integer> keySet = map.keySet();
1419 // System.out.println("min=" + min.printNode(0));
1421 for (Iterator iterator2 = keySet.iterator(); iterator2.hasNext();) {
1422 Integer argIdx = (Integer) iterator2.next();
1423 NTuple<Descriptor> argTuple = map.get(argIdx);
1425 if (!(!md.isStatic() && argIdx == 0)) {
1426 // if the argTuple is empty, we don't need to do with anything(LITERAL CASE).
1427 if (argTuple.size() > 0
1428 && argTuple.get(argTuple.size() - 1).equals(lastLocationOfPrefix.getLocDescriptor())) {
1429 NTuple<Location> locTuple =
1430 translateToLocTuple(md, flowGraph.getParamFlowNode(argIdx).getDescTuple());
1431 lastLocationOfPrefix = locTuple.get(0);
1432 System.out.println("ARG CASE=" + locTuple);
1433 for (Iterator iterator3 = subGlobalReachableSet.iterator(); iterator3.hasNext();) {
1434 GlobalFlowNode subGlobalReachalbeNode = (GlobalFlowNode) iterator3.next();
1435 // NTuple<Location> locTuple = translateToLocTuple(md, reachalbeNode.getDescTuple());
1436 NTuple<Location> globalReachlocTuple = subGlobalReachalbeNode.getLocTuple();
1437 for (int i = 0; i < globalReachlocTuple.size(); i++) {
1438 if (globalReachlocTuple.get(i).equals(lastLocationOfPrefix)) {
1439 System.out.println("ARG " + argTuple + " IS MATCHED WITH="
1440 + lastLocationOfPrefix);
1441 System.out.println("here5?!");
1452 // ClassDescriptor cd;
1453 // if (lastLocationOfPrefix.getLocDescriptor() instanceof VarDescriptor) {
1454 // cd = ((VarDescriptor) lastLocationOfPrefix.getLocDescriptor()).getType().getClassDesc();
1456 // // it is a field descriptor
1457 // cd = ((FieldDescriptor) lastLocationOfPrefix.getLocDescriptor()).getType().getClassDesc();
1460 // GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
1461 // Set<GlobalFlowNode> subGlobalReachableSet = subGlobalFlowGraph.getReachableNodeSetFrom(node);
1463 // System.out.println("TRY TO FIND lastLocationOfPrefix=" + lastLocationOfPrefix);
1464 // for (Iterator iterator2 = subGlobalReachableSet.iterator(); iterator2.hasNext();) {
1465 // GlobalFlowNode subGlobalReachalbeNode = (GlobalFlowNode) iterator2.next();
1466 // // NTuple<Location> locTuple = translateToLocTuple(md, reachalbeNode.getDescTuple());
1467 // NTuple<Location> locTuple = subGlobalReachalbeNode.getLocTuple();
1469 // for (int i = 0; i < locTuple.size(); i++) {
1470 // if (locTuple.get(i).equals(lastLocationOfPrefix)) {
1475 // Location lastLoc = locTuple.get(locTuple.size() - 1);
1476 // Descriptor enclosingDescriptor = lastLoc.getDescriptor();
1478 // if (enclosingDescriptor != null && enclosingDescriptor.equals(cd)) {
1479 // System.out.println("# WHY HERE?");
1480 // System.out.println("subGlobalReachalbeNode=" + subGlobalReachalbeNode);
1488 private boolean checkNodeReachToReturnNode(MethodDescriptor md, FlowNode node) {
1490 FlowGraph flowGraph = getFlowGraph(md);
1491 Set<FlowNode> reachableSet = flowGraph.getReachFlowNodeSetFrom(node);
1492 if (mapMethodDescriptorToCompositeReturnCase.containsKey(md)) {
1493 boolean hasCompReturnLocWithThis =
1494 mapMethodDescriptorToCompositeReturnCase.get(md).booleanValue();
1496 if (hasCompReturnLocWithThis) {
1497 for (Iterator iterator = flowGraph.getReturnNodeSet().iterator(); iterator.hasNext();) {
1498 FlowNode returnFlowNode = (FlowNode) iterator.next();
1499 if (reachableSet.contains(returnFlowNode)) {
1508 private void assignCompositeLocation(CompositeLocation compLocPrefix, GlobalFlowNode node) {
1509 CompositeLocation newCompLoc = compLocPrefix.clone();
1510 NTuple<Location> locTuple = node.getLocTuple();
1511 for (int i = 1; i < locTuple.size(); i++) {
1512 newCompLoc.addLocation(locTuple.get(i));
1514 node.setInferCompositeLocation(newCompLoc);
1517 private List<NTuple<Location>> calculatePrefixList(GlobalFlowGraph graph, GlobalFlowNode node) {
1519 System.out.println("\n##### calculatePrefixList node=" + node);
1521 Set<GlobalFlowNode> incomingNodeSetPrefix =
1522 graph.getIncomingNodeSetByPrefix(node.getLocTuple().get(0));
1523 // System.out.println("---incomingNodeSetPrefix=" + incomingNodeSetPrefix);
1525 Set<GlobalFlowNode> reachableNodeSetPrefix =
1526 graph.getReachableNodeSetByPrefix(node.getLocTuple().get(0));
1527 // System.out.println("---reachableNodeSetPrefix=" + reachableNodeSetPrefix);
1529 List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
1531 for (Iterator iterator = incomingNodeSetPrefix.iterator(); iterator.hasNext();) {
1532 GlobalFlowNode inNode = (GlobalFlowNode) iterator.next();
1533 NTuple<Location> inNodeTuple = inNode.getLocTuple();
1535 if (inNodeTuple.get(0).getLocDescriptor() instanceof InterDescriptor
1536 || inNodeTuple.get(0).getLocDescriptor().equals(GLOBALDESC)) {
1540 for (int i = 1; i < inNodeTuple.size(); i++) {
1541 NTuple<Location> prefix = inNodeTuple.subList(0, i);
1542 if (!prefixList.contains(prefix)) {
1543 prefixList.add(prefix);
1548 Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
1549 public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
1550 int s0 = arg0.size();
1551 int s1 = arg1.size();
1554 } else if (s0 == s1) {
1562 // remove a prefix which is not suitable for generating composite location
1563 Location localVarLoc = node.getLocTuple().get(0);
1564 MethodDescriptor md = (MethodDescriptor) localVarLoc.getDescriptor();
1565 ClassDescriptor cd = md.getClassDesc();
1568 Set<NTuple<Location>> toberemoved = new HashSet<NTuple<Location>>();
1569 // for (int i = 0; i < prefixList.size(); i++) {
1570 // NTuple<Location> prefixLocTuple = prefixList.get(i);
1571 // if (!containsClassDesc(cd, prefixLocTuple)) {
1572 // toberemoved.add(prefixLocTuple);
1576 // System.out.println("method class=" + cd + " toberemoved=" + toberemoved);
1578 prefixList.removeAll(toberemoved);
1584 private CompositeLocation calculateCompositeLocationFromFlowGraph(MethodDescriptor md,
1587 System.out.println("#############################################################");
1588 System.out.println("calculateCompositeLocationFromFlowGraph=" + node);
1590 FlowGraph flowGraph = getFlowGraph(md);
1591 // NTuple<Location> paramLocTuple = translateToLocTuple(md, paramNode.getDescTuple());
1592 // GlobalFlowNode paramGlobalNode = subGlobalFlowGraph.getFlowNode(paramLocTuple);
1594 List<NTuple<Location>> prefixList = calculatePrefixListFlowGraph(flowGraph, node);
1596 // Set<GlobalFlowNode> reachableNodeSet =
1597 // subGlobalFlowGraph.getReachableNodeSetByPrefix(paramGlobalNode.getLocTuple().get(0));
1599 Set<FlowNode> reachableNodeSet =
1600 flowGraph.getReachableSetFrom(node.getDescTuple().subList(0, 1));
1602 // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
1604 // System.out.println("node=" + node + " prefixList=" + prefixList);
1606 for (int i = 0; i < prefixList.size(); i++) {
1607 NTuple<Location> curPrefix = prefixList.get(i);
1608 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
1610 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
1611 FlowNode reachNode = (FlowNode) iterator2.next();
1612 NTuple<Location> reachLocTuple = translateToLocTuple(md, reachNode.getCurrentDescTuple());
1613 if (reachLocTuple.startsWith(curPrefix)) {
1614 reachableCommonPrefixSet.add(reachLocTuple);
1617 // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
1619 if (!reachableCommonPrefixSet.isEmpty()) {
1621 MethodDescriptor curPrefixFirstElementMethodDesc =
1622 (MethodDescriptor) curPrefix.get(0).getDescriptor();
1624 // MethodDescriptor nodePrefixLocFirstElementMethodDesc =
1625 // (MethodDescriptor) prefixLoc.getDescriptor();
1627 // System.out.println("curPrefixFirstElementMethodDesc=" +
1628 // curPrefixFirstElementMethodDesc);
1629 // System.out.println("nodePrefixLocFirstElementMethodDesc="
1630 // + nodePrefixLocFirstElementMethodDesc);
1633 // if (!node.getLocTuple().startsWith(curPrefix.get(0))) {
1635 Location curPrefixLocalLoc = curPrefix.get(0);
1637 Location targetLocalLoc = new Location(md, node.getDescTuple().get(0));
1638 // Location targetLocalLoc = paramGlobalNode.getLocTuple().get(0);
1640 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
1641 System.out.println("NEED2ASSIGN COMP LOC TO " + node + " with prefix=" + curPrefix);
1642 System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc=" + newCompLoc);
1644 // // makes sure that a newly generated location appears in the hierarchy graph
1645 // for (int compIdx = 0; compIdx < newCompLoc.getSize(); compIdx++) {
1646 // Location curLoc = newCompLoc.get(compIdx);
1647 // getHierarchyGraph(curLoc.getDescriptor()).getHNode(curLoc.getLocDescriptor());
1649 // subGlobalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
1650 node.setCompositeLocation(newCompLoc);
1660 private List<NTuple<Location>> calculatePrefixListFlowGraph(FlowGraph graph, FlowNode node) {
1662 System.out.println("\n##### calculatePrefixList node=" + node);
1664 MethodDescriptor md = graph.getMethodDescriptor();
1665 Set<FlowNode> incomingNodeSetPrefix =
1666 graph.getIncomingNodeSetByPrefix(node.getDescTuple().get(0));
1667 // System.out.println("---incomingNodeSetPrefix=" + incomingNodeSetPrefix);
1669 Set<FlowNode> reachableNodeSetPrefix =
1670 graph.getReachableSetFrom(node.getDescTuple().subList(0, 1));
1671 // System.out.println("---reachableNodeSetPrefix=" + reachableNodeSetPrefix);
1673 List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
1675 for (Iterator iterator = incomingNodeSetPrefix.iterator(); iterator.hasNext();) {
1676 FlowNode inNode = (FlowNode) iterator.next();
1677 NTuple<Location> inNodeTuple = translateToLocTuple(md, inNode.getCurrentDescTuple());
1679 // if (inNodeTuple.get(0).getLocDescriptor() instanceof InterDescriptor
1680 // || inNodeTuple.get(0).getLocDescriptor().equals(GLOBALDESC)) {
1684 for (int i = 1; i < inNodeTuple.size(); i++) {
1685 NTuple<Location> prefix = inNodeTuple.subList(0, i);
1686 if (!prefixList.contains(prefix)) {
1687 prefixList.add(prefix);
1692 Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
1693 public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
1694 int s0 = arg0.size();
1695 int s1 = arg1.size();
1698 } else if (s0 == s1) {
1710 private boolean containsClassDesc(ClassDescriptor cd, NTuple<Location> prefixLocTuple) {
1711 for (int i = 0; i < prefixLocTuple.size(); i++) {
1712 Location loc = prefixLocTuple.get(i);
1713 Descriptor locDesc = loc.getLocDescriptor();
1714 if (locDesc != null) {
1715 ClassDescriptor type = getClassTypeDescriptor(locDesc);
1716 if (type != null && type.equals(cd)) {
1724 private GlobalFlowGraph constructSubGlobalFlowGraph(FlowGraph flowGraph) {
1726 MethodDescriptor md = flowGraph.getMethodDescriptor();
1728 GlobalFlowGraph globalGraph = getSubGlobalFlowGraph(md);
1730 // Set<FlowNode> nodeSet = flowGraph.getNodeSet();
1731 Set<FlowEdge> edgeSet = flowGraph.getEdgeSet();
1733 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
1735 FlowEdge edge = (FlowEdge) iterator.next();
1736 NTuple<Descriptor> srcDescTuple = edge.getInitTuple();
1737 NTuple<Descriptor> dstDescTuple = edge.getEndTuple();
1739 if (flowGraph.getFlowNode(srcDescTuple) instanceof FlowReturnNode
1740 || flowGraph.getFlowNode(dstDescTuple) instanceof FlowReturnNode) {
1744 // here only keep the first element(method location) of the descriptor
1746 NTuple<Location> srcLocTuple = translateToLocTuple(md, srcDescTuple);
1747 // Location srcMethodLoc = srcLocTuple.get(0);
1748 // Descriptor srcVarDesc = srcMethodLoc.getLocDescriptor();
1749 // // if (flowGraph.isParamDesc(srcVarDesc) &&
1750 // (!srcVarDesc.equals(md.getThis()))) {
1751 // if (!srcVarDesc.equals(md.getThis())) {
1752 // srcLocTuple = new NTuple<Location>();
1753 // Location loc = new Location(md, srcVarDesc);
1754 // srcLocTuple.add(loc);
1757 NTuple<Location> dstLocTuple = translateToLocTuple(md, dstDescTuple);
1758 // Location dstMethodLoc = dstLocTuple.get(0);
1759 // Descriptor dstVarDesc = dstMethodLoc.getLocDescriptor();
1760 // if (!dstVarDesc.equals(md.getThis())) {
1761 // dstLocTuple = new NTuple<Location>();
1762 // Location loc = new Location(md, dstVarDesc);
1763 // dstLocTuple.add(loc);
1765 System.out.println("B11");
1767 globalGraph.addValueFlowEdge(srcLocTuple, dstLocTuple);
1774 private NTuple<Location> translateToLocTuple(MethodDescriptor md, NTuple<Descriptor> descTuple) {
1776 NTuple<Location> locTuple = new NTuple<Location>();
1778 Descriptor enclosingDesc = md;
1779 for (int i = 0; i < descTuple.size(); i++) {
1780 Descriptor desc = descTuple.get(i);
1782 Location loc = new Location(enclosingDesc, desc);
1785 if (desc instanceof VarDescriptor) {
1786 enclosingDesc = ((VarDescriptor) desc).getType().getClassDesc();
1787 } else if (desc instanceof FieldDescriptor) {
1788 enclosingDesc = ((FieldDescriptor) desc).getType().getClassDesc();
1790 // TODO: inter descriptor case
1791 enclosingDesc = desc;
1800 private void addValueFlowsFromCalleeSubGlobalFlowGraph(MethodDescriptor mdCaller) {
1802 // the transformation for a call site propagates flows through parameters
1803 // if the method is virtual, it also grab all relations from any possible
1806 Set<MethodInvokeNode> setMethodInvokeNode = getMethodInvokeNodeSet(mdCaller);
1808 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
1809 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
1810 MethodDescriptor mdCallee = min.getMethod();
1811 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1812 if (mdCallee.isStatic()) {
1813 setPossibleCallees.add(mdCallee);
1815 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
1816 // removes method descriptors that are not invoked by the caller
1817 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
1818 setPossibleCallees.addAll(calleeSet);
1821 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
1822 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
1823 propagateValueFlowsToCallerFromSubGlobalFlowGraph(min, mdCaller, possibleMdCallee);
1830 private void propagateValueFlowsToCallerFromSubGlobalFlowGraph(MethodInvokeNode min,
1831 MethodDescriptor mdCaller, MethodDescriptor possibleMdCallee) {
1833 System.out.println("---propagate from " + min.printNode(0) + " to caller=" + mdCaller);
1834 FlowGraph calleeFlowGraph = getFlowGraph(possibleMdCallee);
1835 Map<Integer, NTuple<Descriptor>> mapIdxToArg = mapMethodInvokeNodeToArgIdxMap.get(min);
1837 System.out.println("-----mapMethodInvokeNodeToArgIdxMap.get(min)="
1838 + mapMethodInvokeNodeToArgIdxMap.get(min));
1840 Set<Integer> keySet = mapIdxToArg.keySet();
1841 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1842 Integer idx = (Integer) iterator.next();
1843 NTuple<Descriptor> argDescTuple = mapIdxToArg.get(idx);
1844 if (argDescTuple.size() > 0) {
1845 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
1846 NTuple<Descriptor> paramDescTuple = calleeFlowGraph.getParamFlowNode(idx).getDescTuple();
1847 NTuple<Location> paramLocTuple = translateToLocTuple(possibleMdCallee, paramDescTuple);
1848 System.out.println("-------paramDescTuple=" + paramDescTuple + "->argDescTuple="
1850 addMapCallerArgToCalleeParam(min, argDescTuple, paramDescTuple);
1854 // addValueFlowBetweenParametersToCaller(min, mdCaller, possibleMdCallee);
1856 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
1857 GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(possibleMdCallee);
1858 Set<GlobalFlowNode> calleeNodeSet = calleeSubGlobalGraph.getNodeSet();
1859 for (Iterator iterator = calleeNodeSet.iterator(); iterator.hasNext();) {
1860 GlobalFlowNode calleeNode = (GlobalFlowNode) iterator.next();
1861 addValueFlowFromCalleeNode(min, mdCaller, possibleMdCallee, calleeNode);
1864 System.out.println("$$$GLOBAL PC LOC ADD=" + mdCaller);
1865 Set<NTuple<Location>> pcLocTupleSet = mapMethodInvokeNodeToPCLocTupleSet.get(min);
1866 System.out.println("---pcLocTupleSet=" + pcLocTupleSet);
1867 GlobalFlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
1868 for (Iterator iterator = calleeNodeSet.iterator(); iterator.hasNext();) {
1869 GlobalFlowNode calleeNode = (GlobalFlowNode) iterator.next();
1870 if (calleeNode.isParamNodeWithIncomingFlows()) {
1871 System.out.println("calleeNode.getLocTuple()" + calleeNode.getLocTuple());
1872 NTuple<Location> callerSrcNodeLocTuple =
1873 translateToCallerLocTuple(min, possibleMdCallee, mdCaller, calleeNode.getLocTuple());
1874 System.out.println("---callerSrcNodeLocTuple=" + callerSrcNodeLocTuple);
1875 if (callerSrcNodeLocTuple != null && callerSrcNodeLocTuple.size() > 0) {
1876 for (Iterator iterator2 = pcLocTupleSet.iterator(); iterator2.hasNext();) {
1877 NTuple<Location> pcLocTuple = (NTuple<Location>) iterator2.next();
1878 System.out.println("B12");
1880 callerSubGlobalGraph.addValueFlowEdge(pcLocTuple, callerSrcNodeLocTuple);
1889 private void addValueFlowFromCalleeNode(MethodInvokeNode min, MethodDescriptor mdCaller,
1890 MethodDescriptor mdCallee, GlobalFlowNode calleeSrcNode) {
1892 GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(mdCallee);
1893 GlobalFlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
1895 System.out.println("$addValueFlowFromCalleeNode calleeSrcNode=" + calleeSrcNode);
1897 NTuple<Location> callerSrcNodeLocTuple =
1898 translateToCallerLocTuple(min, mdCallee, mdCaller, calleeSrcNode.getLocTuple());
1899 System.out.println("---callerSrcNodeLocTuple=" + callerSrcNodeLocTuple);
1901 if (callerSrcNodeLocTuple != null && callerSrcNodeLocTuple.size() > 0) {
1903 Set<GlobalFlowNode> outNodeSet = calleeSubGlobalGraph.getOutNodeSet(calleeSrcNode);
1905 for (Iterator iterator = outNodeSet.iterator(); iterator.hasNext();) {
1906 GlobalFlowNode outNode = (GlobalFlowNode) iterator.next();
1907 NTuple<Location> callerDstNodeLocTuple =
1908 translateToCallerLocTuple(min, mdCallee, mdCaller, outNode.getLocTuple());
1909 // System.out.println("outNode=" + outNode + " callerDstNodeLocTuple="
1910 // + callerDstNodeLocTuple);
1911 if (callerSrcNodeLocTuple != null && callerDstNodeLocTuple != null
1912 && callerSrcNodeLocTuple.size() > 0 && callerDstNodeLocTuple.size() > 0) {
1913 System.out.println("B3");
1914 callerSubGlobalGraph.addValueFlowEdge(callerSrcNodeLocTuple, callerDstNodeLocTuple);
1921 private NTuple<Location> translateToCallerLocTuple(MethodInvokeNode min,
1922 MethodDescriptor mdCallee, MethodDescriptor mdCaller, NTuple<Location> nodeLocTuple) {
1923 // this method will return the same nodeLocTuple if the corresponding argument is literal
1926 // System.out.println("translateToCallerLocTuple=" + nodeLocTuple);
1928 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
1929 NTuple<Descriptor> nodeDescTuple = translateToDescTuple(nodeLocTuple);
1930 if (calleeFlowGraph.isParameter(nodeDescTuple)) {
1931 int paramIdx = calleeFlowGraph.getParamIdx(nodeDescTuple);
1932 NTuple<Descriptor> argDescTuple = mapMethodInvokeNodeToArgIdxMap.get(min).get(paramIdx);
1934 // if (isPrimitive(nodeLocTuple.get(0).getLocDescriptor())) {
1935 // // the type of argument is primitive.
1936 // return nodeLocTuple.clone();
1938 // System.out.println("paramIdx=" + paramIdx + " argDescTuple=" + argDescTuple + " from min="
1939 // + min.printNode(0));
1940 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
1942 NTuple<Location> callerLocTuple = new NTuple<Location>();
1944 callerLocTuple.addAll(argLocTuple);
1945 for (int i = 1; i < nodeLocTuple.size(); i++) {
1946 callerLocTuple.add(nodeLocTuple.get(i));
1948 return callerLocTuple;
1950 return nodeLocTuple.clone();
1955 public static boolean isPrimitive(Descriptor desc) {
1957 if (desc instanceof FieldDescriptor) {
1958 return ((FieldDescriptor) desc).getType().isPrimitive();
1959 } else if (desc instanceof VarDescriptor) {
1960 return ((VarDescriptor) desc).getType().isPrimitive();
1961 } else if (desc instanceof InterDescriptor) {
1968 public static boolean isReference(Descriptor desc) {
1970 if (desc instanceof FieldDescriptor) {
1972 TypeDescriptor type = ((FieldDescriptor) desc).getType();
1973 if (type.isArray()) {
1974 return !type.isPrimitive();
1976 return type.isPtr();
1979 } else if (desc instanceof VarDescriptor) {
1980 TypeDescriptor type = ((VarDescriptor) desc).getType();
1981 if (type.isArray()) {
1982 return !type.isPrimitive();
1984 return type.isPtr();
1991 private NTuple<Descriptor> translateToDescTuple(NTuple<Location> locTuple) {
1993 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
1994 for (int i = 0; i < locTuple.size(); i++) {
1995 descTuple.add(locTuple.get(i).getLocDescriptor());
2001 public LocationSummary getLocationSummary(Descriptor d) {
2002 if (!mapDescToLocationSummary.containsKey(d)) {
2003 if (d instanceof MethodDescriptor) {
2004 mapDescToLocationSummary.put(d, new MethodSummary((MethodDescriptor) d));
2005 } else if (d instanceof ClassDescriptor) {
2006 mapDescToLocationSummary.put(d, new FieldSummary());
2009 return mapDescToLocationSummary.get(d);
2012 private void generateMethodSummary() {
2014 Set<MethodDescriptor> keySet = md2lattice.keySet();
2015 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2016 MethodDescriptor md = (MethodDescriptor) iterator.next();
2018 System.out.println("\nSSJAVA: generate method summary: " + md);
2020 FlowGraph flowGraph = getFlowGraph(md);
2021 MethodSummary methodSummary = getMethodSummary(md);
2023 HierarchyGraph scGraph = getSkeletonCombinationHierarchyGraph(md);
2025 // set the 'this' reference location
2026 if (!md.isStatic()) {
2027 System.out.println("setThisLocName=" + scGraph.getHNode(md.getThis()).getName());
2028 methodSummary.setThisLocName(scGraph.getHNode(md.getThis()).getName());
2031 // set the 'global' reference location if needed
2032 if (methodSummary.hasGlobalAccess()) {
2033 methodSummary.setGlobalLocName(scGraph.getHNode(GLOBALDESC).getName());
2036 // construct a parameter mapping that maps a parameter descriptor to an
2037 // inferred composite location
2038 for (int paramIdx = 0; paramIdx < flowGraph.getNumParameters(); paramIdx++) {
2039 FlowNode flowNode = flowGraph.getParamFlowNode(paramIdx);
2040 CompositeLocation inferredCompLoc =
2041 updateCompositeLocation(flowNode.getCompositeLocation());
2042 // NTuple<Descriptor> descTuple = flowNode.getDescTuple();
2044 // CompositeLocation assignedCompLoc = flowNode.getCompositeLocation();
2045 // CompositeLocation inferredCompLoc;
2046 // if (assignedCompLoc != null) {
2047 // inferredCompLoc = translateCompositeLocation(assignedCompLoc);
2049 // Descriptor locDesc = descTuple.get(0);
2050 // Location loc = new Location(md, locDesc.getSymbol());
2051 // loc.setLocDescriptor(locDesc);
2052 // inferredCompLoc = new CompositeLocation(loc);
2054 System.out.println("-paramIdx=" + paramIdx + " infer=" + inferredCompLoc + " original="
2055 + flowNode.getCompositeLocation());
2057 Descriptor localVarDesc = flowNode.getDescTuple().get(0);
2058 methodSummary.addMapVarNameToInferCompLoc(localVarDesc, inferredCompLoc);
2059 methodSummary.addMapParamIdxToInferLoc(paramIdx, inferredCompLoc);
2066 private boolean hasOrderingRelation(NTuple<Location> locTuple1, NTuple<Location> locTuple2) {
2068 int size = locTuple1.size() >= locTuple2.size() ? locTuple2.size() : locTuple1.size();
2070 for (int idx = 0; idx < size; idx++) {
2071 Location loc1 = locTuple1.get(idx);
2072 Location loc2 = locTuple2.get(idx);
2074 Descriptor desc1 = loc1.getDescriptor();
2075 Descriptor desc2 = loc2.getDescriptor();
2077 if (!desc1.equals(desc2)) {
2078 throw new Error("Fail to compare " + locTuple1 + " and " + locTuple2);
2081 Descriptor locDesc1 = loc1.getLocDescriptor();
2082 Descriptor locDesc2 = loc2.getLocDescriptor();
2084 HierarchyGraph hierarchyGraph = getHierarchyGraph(desc1);
2086 HNode node1 = hierarchyGraph.getHNode(locDesc1);
2087 HNode node2 = hierarchyGraph.getHNode(locDesc2);
2089 System.out.println("---node1=" + node1 + " node2=" + node2);
2090 System.out.println("---hierarchyGraph.getIncomingNodeSet(node2)="
2091 + hierarchyGraph.getIncomingNodeSet(node2));
2093 if (locDesc1.equals(locDesc2)) {
2095 } else if (!hierarchyGraph.getIncomingNodeSet(node2).contains(node1)
2096 && !hierarchyGraph.getIncomingNodeSet(node1).contains(node2)) {
2108 private boolean isHigherThan(NTuple<Location> locTuple1, NTuple<Location> locTuple2) {
2110 int size = locTuple1.size() >= locTuple2.size() ? locTuple2.size() : locTuple1.size();
2112 for (int idx = 0; idx < size; idx++) {
2113 Location loc1 = locTuple1.get(idx);
2114 Location loc2 = locTuple2.get(idx);
2116 Descriptor desc1 = loc1.getDescriptor();
2117 Descriptor desc2 = loc2.getDescriptor();
2119 if (!desc1.equals(desc2)) {
2120 throw new Error("Fail to compare " + locTuple1 + " and " + locTuple2);
2123 Descriptor locDesc1 = loc1.getLocDescriptor();
2124 Descriptor locDesc2 = loc2.getLocDescriptor();
2126 HierarchyGraph hierarchyGraph = getHierarchyGraph(desc1);
2128 HNode node1 = hierarchyGraph.getHNode(locDesc1);
2129 HNode node2 = hierarchyGraph.getHNode(locDesc2);
2131 System.out.println("---node1=" + node1 + " node2=" + node2);
2132 System.out.println("---hierarchyGraph.getIncomingNodeSet(node2)="
2133 + hierarchyGraph.getIncomingNodeSet(node2));
2135 if (locDesc1.equals(locDesc2)) {
2137 } else if (hierarchyGraph.getIncomingNodeSet(node2).contains(node1)) {
2148 private CompositeLocation translateCompositeLocation(CompositeLocation compLoc) {
2149 CompositeLocation newCompLoc = new CompositeLocation();
2151 // System.out.println("compLoc=" + compLoc);
2152 for (int i = 0; i < compLoc.getSize(); i++) {
2153 Location loc = compLoc.get(i);
2154 Descriptor enclosingDescriptor = loc.getDescriptor();
2155 Descriptor locDescriptor = loc.getLocDescriptor();
2157 HNode hnode = getHierarchyGraph(enclosingDescriptor).getHNode(locDescriptor);
2158 // System.out.println("-hnode=" + hnode + " from=" + locDescriptor +
2159 // " enclosingDescriptor="
2160 // + enclosingDescriptor);
2161 // System.out.println("-getLocationSummary(enclosingDescriptor)="
2162 // + getLocationSummary(enclosingDescriptor));
2163 String locName = getLocationSummary(enclosingDescriptor).getLocationName(hnode.getName());
2164 // System.out.println("-locName=" + locName);
2165 Location newLoc = new Location(enclosingDescriptor, locName);
2166 newLoc.setLocDescriptor(locDescriptor);
2167 newCompLoc.addLocation(newLoc);
2173 private void debug_writeLattices() {
2175 Set<Descriptor> keySet = mapDescriptorToSimpleLattice.keySet();
2176 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2177 Descriptor key = (Descriptor) iterator.next();
2178 SSJavaLattice<String> simpleLattice = mapDescriptorToSimpleLattice.get(key);
2179 // HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(key);
2180 HierarchyGraph scHierarchyGraph = getSkeletonCombinationHierarchyGraph(key);
2181 if (key instanceof ClassDescriptor) {
2182 writeInferredLatticeDotFile((ClassDescriptor) key, scHierarchyGraph, simpleLattice,
2184 } else if (key instanceof MethodDescriptor) {
2185 MethodDescriptor md = (MethodDescriptor) key;
2186 writeInferredLatticeDotFile(md.getClassDesc(), md, scHierarchyGraph, simpleLattice,
2190 LocationSummary ls = getLocationSummary(key);
2191 System.out.println("####LOC SUMMARY=" + key + "\n" + ls.getMapHNodeNameToLocationName());
2194 Set<ClassDescriptor> cdKeySet = cd2lattice.keySet();
2195 for (Iterator iterator = cdKeySet.iterator(); iterator.hasNext();) {
2196 ClassDescriptor cd = (ClassDescriptor) iterator.next();
2197 writeInferredLatticeDotFile((ClassDescriptor) cd, getSkeletonCombinationHierarchyGraph(cd),
2198 cd2lattice.get(cd), "");
2201 Set<MethodDescriptor> mdKeySet = md2lattice.keySet();
2202 for (Iterator iterator = mdKeySet.iterator(); iterator.hasNext();) {
2203 MethodDescriptor md = (MethodDescriptor) iterator.next();
2204 writeInferredLatticeDotFile(md.getClassDesc(), md, getSkeletonCombinationHierarchyGraph(md),
2205 md2lattice.get(md), "");
2210 private void buildLattice() {
2212 BuildLattice buildLattice = new BuildLattice(this);
2214 Set<Descriptor> keySet = mapDescriptorToCombineSkeletonHierarchyGraph.keySet();
2215 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2216 Descriptor desc = (Descriptor) iterator.next();
2218 SSJavaLattice<String> simpleLattice = buildLattice.buildLattice(desc);
2220 addMapDescToSimpleLattice(desc, simpleLattice);
2222 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
2223 System.out.println("\n## insertIntermediateNodesToStraightLine:"
2224 + simpleHierarchyGraph.getName());
2225 SSJavaLattice<String> lattice =
2226 buildLattice.insertIntermediateNodesToStraightLine(desc, simpleLattice);
2227 lattice.removeRedundantEdges();
2229 if (desc instanceof ClassDescriptor) {
2231 cd2lattice.put((ClassDescriptor) desc, lattice);
2232 // ssjava.writeLatticeDotFile((ClassDescriptor) desc, null, lattice);
2233 } else if (desc instanceof MethodDescriptor) {
2235 md2lattice.put((MethodDescriptor) desc, lattice);
2236 MethodDescriptor md = (MethodDescriptor) desc;
2237 ClassDescriptor cd = md.getClassDesc();
2238 // ssjava.writeLatticeDotFile(cd, md, lattice);
2241 // System.out.println("\nSSJAVA: Insering Combination Nodes:" + desc);
2242 // HierarchyGraph skeletonGraph = getSkeletonHierarchyGraph(desc);
2243 // HierarchyGraph skeletonGraphWithCombinationNode =
2244 // skeletonGraph.clone();
2245 // skeletonGraphWithCombinationNode.setName(desc + "_SC");
2247 // HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
2248 // System.out.println("Identifying Combination Nodes:");
2249 // skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph);
2250 // skeletonGraphWithCombinationNode.simplifySkeletonCombinationHierarchyGraph();
2251 // mapDescriptorToCombineSkeletonHierarchyGraph.put(desc,
2252 // skeletonGraphWithCombinationNode);
2257 public void addMapDescToSimpleLattice(Descriptor desc, SSJavaLattice<String> lattice) {
2258 mapDescriptorToSimpleLattice.put(desc, lattice);
2261 public SSJavaLattice<String> getSimpleLattice(Descriptor desc) {
2262 return mapDescriptorToSimpleLattice.get(desc);
2265 private void simplifyHierarchyGraph() {
2266 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2267 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2268 Descriptor desc = (Descriptor) iterator.next();
2269 // System.out.println("SSJAVA: remove redundant edges: " + desc);
2270 HierarchyGraph simpleHierarchyGraph = getHierarchyGraph(desc).clone();
2271 simpleHierarchyGraph.setName(desc + "_SIMPLE");
2272 simpleHierarchyGraph.removeRedundantEdges();
2273 mapDescriptorToSimpleHierarchyGraph.put(desc, simpleHierarchyGraph);
2277 private void insertCombinationNodes() {
2278 Set<Descriptor> keySet = mapDescriptorToSkeletonHierarchyGraph.keySet();
2279 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2280 Descriptor desc = (Descriptor) iterator.next();
2281 System.out.println("\nSSJAVA: Insering Combination Nodes:" + desc);
2282 HierarchyGraph skeletonGraph = getSkeletonHierarchyGraph(desc);
2283 HierarchyGraph skeletonGraphWithCombinationNode = skeletonGraph.clone();
2284 skeletonGraphWithCombinationNode.setName(desc + "_SC");
2286 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
2287 System.out.println("Identifying Combination Nodes:");
2288 skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph);
2289 skeletonGraphWithCombinationNode.simplifySkeletonCombinationHierarchyGraph();
2290 mapDescriptorToCombineSkeletonHierarchyGraph.put(desc, skeletonGraphWithCombinationNode);
2294 private void constructSkeletonHierarchyGraph() {
2295 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2296 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2297 Descriptor desc = (Descriptor) iterator.next();
2298 System.out.println("SSJAVA: Constructing Skeleton Hierarchy Graph: " + desc);
2299 HierarchyGraph simpleGraph = getSimpleHierarchyGraph(desc);
2300 HierarchyGraph skeletonGraph = simpleGraph.generateSkeletonGraph();
2301 skeletonGraph.setMapDescToHNode(simpleGraph.getMapDescToHNode());
2302 skeletonGraph.setMapHNodeToDescSet(simpleGraph.getMapHNodeToDescSet());
2303 skeletonGraph.simplifyHierarchyGraph();
2304 // skeletonGraph.combineRedundantNodes(false);
2305 // skeletonGraph.removeRedundantEdges();
2306 mapDescriptorToSkeletonHierarchyGraph.put(desc, skeletonGraph);
2310 private void debug_writeHierarchyDotFiles() {
2312 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2313 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2314 Descriptor desc = (Descriptor) iterator.next();
2315 getHierarchyGraph(desc).writeGraph();
2320 private void debug_writeSimpleHierarchyDotFiles() {
2322 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2323 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2324 Descriptor desc = (Descriptor) iterator.next();
2325 getHierarchyGraph(desc).writeGraph();
2326 getSimpleHierarchyGraph(desc).writeGraph();
2331 private void debug_writeSkeletonHierarchyDotFiles() {
2333 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2334 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2335 Descriptor desc = (Descriptor) iterator.next();
2336 getSkeletonHierarchyGraph(desc).writeGraph();
2341 private void debug_writeSkeletonCombinationHierarchyDotFiles() {
2343 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2344 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2345 Descriptor desc = (Descriptor) iterator.next();
2346 getSkeletonCombinationHierarchyGraph(desc).writeGraph();
2351 public HierarchyGraph getSimpleHierarchyGraph(Descriptor d) {
2352 return mapDescriptorToSimpleHierarchyGraph.get(d);
2355 private HierarchyGraph getSkeletonHierarchyGraph(Descriptor d) {
2356 if (!mapDescriptorToSkeletonHierarchyGraph.containsKey(d)) {
2357 mapDescriptorToSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
2359 return mapDescriptorToSkeletonHierarchyGraph.get(d);
2362 public HierarchyGraph getSkeletonCombinationHierarchyGraph(Descriptor d) {
2363 if (!mapDescriptorToCombineSkeletonHierarchyGraph.containsKey(d)) {
2364 mapDescriptorToCombineSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
2366 return mapDescriptorToCombineSkeletonHierarchyGraph.get(d);
2369 private void constructHierarchyGraph() {
2371 LinkedList<MethodDescriptor> methodDescList =
2372 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
2374 while (!methodDescList.isEmpty()) {
2375 MethodDescriptor md = methodDescList.removeLast();
2376 if (state.SSJAVADEBUG) {
2377 HierarchyGraph hierarchyGraph = new HierarchyGraph(md);
2378 System.out.println();
2379 System.out.println("SSJAVA: Construcing the hierarchy graph from " + md);
2380 constructHierarchyGraph(md, hierarchyGraph);
2381 mapDescriptorToHierarchyGraph.put(md, hierarchyGraph);
2387 while (!toAnalyzeIsEmpty()) {
2388 ClassDescriptor cd = toAnalyzeNext();
2389 HierarchyGraph graph = getHierarchyGraph(cd);
2390 for (Iterator iter = cd.getFields(); iter.hasNext();) {
2391 FieldDescriptor fieldDesc = (FieldDescriptor) iter.next();
2392 if (!(fieldDesc.isStatic() && fieldDesc.isFinal())) {
2393 graph.getHNode(fieldDesc);
2398 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2399 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2400 Descriptor key = (Descriptor) iterator.next();
2401 HierarchyGraph graph = getHierarchyGraph(key);
2403 Set<HNode> nodeToBeConnected = new HashSet<HNode>();
2404 for (Iterator iterator2 = graph.getNodeSet().iterator(); iterator2.hasNext();) {
2405 HNode node = (HNode) iterator2.next();
2406 if (!node.isSkeleton() && !node.isCombinationNode()) {
2407 if (graph.getIncomingNodeSet(node).size() == 0) {
2408 nodeToBeConnected.add(node);
2413 for (Iterator iterator2 = nodeToBeConnected.iterator(); iterator2.hasNext();) {
2414 HNode node = (HNode) iterator2.next();
2415 System.out.println("NEED TO BE CONNECTED TO TOP=" + node);
2416 graph.addEdge(graph.getHNode(TOPDESC), node);
2423 private void constructHierarchyGraph2() {
2425 // do fixed-point analysis
2427 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
2429 // Collections.sort(descriptorListToAnalyze, new
2430 // Comparator<MethodDescriptor>() {
2431 // public int compare(MethodDescriptor o1, MethodDescriptor o2) {
2432 // return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
2436 // current descriptors to visit in fixed-point interprocedural analysis,
2437 // prioritized by dependency in the call graph
2438 methodDescriptorsToVisitStack.clear();
2440 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
2441 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
2443 while (!descriptorListToAnalyze.isEmpty()) {
2444 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
2445 methodDescriptorsToVisitStack.add(md);
2448 // analyze scheduled methods until there are no more to visit
2449 while (!methodDescriptorsToVisitStack.isEmpty()) {
2450 // start to analyze leaf node
2451 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
2453 HierarchyGraph hierarchyGraph = new HierarchyGraph(md);
2454 // MethodSummary methodSummary = new MethodSummary(md);
2456 // MethodLocationInfo methodInfo = new MethodLocationInfo(md);
2457 // curMethodInfo = methodInfo;
2459 System.out.println();
2460 System.out.println("SSJAVA: Construcing the hierarchy graph from " + md);
2462 constructHierarchyGraph(md, hierarchyGraph);
2464 HierarchyGraph prevHierarchyGraph = getHierarchyGraph(md);
2465 // MethodSummary prevMethodSummary = getMethodSummary(md);
2467 if (!hierarchyGraph.equals(prevHierarchyGraph)) {
2469 mapDescriptorToHierarchyGraph.put(md, hierarchyGraph);
2470 // mapDescToLocationSummary.put(md, methodSummary);
2472 // results for callee changed, so enqueue dependents caller for
2474 Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
2475 while (depsItr.hasNext()) {
2476 MethodDescriptor methodNext = depsItr.next();
2477 if (!methodDescriptorsToVisitStack.contains(methodNext)
2478 && methodDescriptorToVistSet.contains(methodNext)) {
2479 methodDescriptorsToVisitStack.add(methodNext);
2488 while (!toAnalyzeIsEmpty()) {
2489 ClassDescriptor cd = toAnalyzeNext();
2490 HierarchyGraph graph = getHierarchyGraph(cd);
2491 for (Iterator iter = cd.getFields(); iter.hasNext();) {
2492 FieldDescriptor fieldDesc = (FieldDescriptor) iter.next();
2493 if (!(fieldDesc.isStatic() && fieldDesc.isFinal())) {
2494 graph.getHNode(fieldDesc);
2499 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
2500 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2501 Descriptor key = (Descriptor) iterator.next();
2502 HierarchyGraph graph = getHierarchyGraph(key);
2504 Set<HNode> nodeToBeConnected = new HashSet<HNode>();
2505 for (Iterator iterator2 = graph.getNodeSet().iterator(); iterator2.hasNext();) {
2506 HNode node = (HNode) iterator2.next();
2507 if (!node.isSkeleton() && !node.isCombinationNode()) {
2508 if (graph.getIncomingNodeSet(node).size() == 0) {
2509 nodeToBeConnected.add(node);
2514 for (Iterator iterator2 = nodeToBeConnected.iterator(); iterator2.hasNext();) {
2515 HNode node = (HNode) iterator2.next();
2516 System.out.println("NEED TO BE CONNECTED TO TOP=" + node);
2517 graph.addEdge(graph.getHNode(TOPDESC), node);
2524 private HierarchyGraph getHierarchyGraph(Descriptor d) {
2525 if (!mapDescriptorToHierarchyGraph.containsKey(d)) {
2526 mapDescriptorToHierarchyGraph.put(d, new HierarchyGraph(d));
2528 return mapDescriptorToHierarchyGraph.get(d);
2531 private void constructHierarchyGraph(MethodDescriptor md, HierarchyGraph methodGraph) {
2533 // visit each node of method flow graph
2534 FlowGraph fg = getFlowGraph(md);
2535 // Set<FlowNode> nodeSet = fg.getNodeSet();
2537 Set<FlowEdge> edgeSet = fg.getEdgeSet();
2539 Set<Descriptor> paramDescSet = fg.getMapParamDescToIdx().keySet();
2540 for (Iterator iterator = paramDescSet.iterator(); iterator.hasNext();) {
2541 Descriptor desc = (Descriptor) iterator.next();
2542 methodGraph.getHNode(desc).setSkeleton(true);
2545 // for the method lattice, we need to look at the first element of
2546 // NTuple<Descriptor>
2547 boolean hasGlobalAccess = false;
2548 // for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
2549 // FlowNode originalSrcNode = (FlowNode) iterator.next();
2550 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
2551 FlowEdge edge = (FlowEdge) iterator.next();
2553 FlowNode originalSrcNode = fg.getFlowNode(edge.getInitTuple());
2554 Set<FlowNode> sourceNodeSet = new HashSet<FlowNode>();
2555 if (originalSrcNode instanceof FlowReturnNode) {
2556 FlowReturnNode rnode = (FlowReturnNode) originalSrcNode;
2557 System.out.println("rnode=" + rnode);
2558 Set<NTuple<Descriptor>> tupleSet = rnode.getReturnTupleSet();
2559 for (Iterator iterator2 = tupleSet.iterator(); iterator2.hasNext();) {
2560 NTuple<Descriptor> nTuple = (NTuple<Descriptor>) iterator2.next();
2561 sourceNodeSet.add(fg.getFlowNode(nTuple));
2562 System.out.println("&&&SOURCE fg.getFlowNode(nTuple)=" + fg.getFlowNode(nTuple));
2565 sourceNodeSet.add(originalSrcNode);
2568 // System.out.println("---sourceNodeSet=" + sourceNodeSet + " from originalSrcNode="
2569 // + originalSrcNode);
2571 for (Iterator iterator3 = sourceNodeSet.iterator(); iterator3.hasNext();) {
2572 FlowNode srcNode = (FlowNode) iterator3.next();
2574 NTuple<Descriptor> srcNodeTuple = srcNode.getDescTuple();
2575 Descriptor srcLocalDesc = srcNodeTuple.get(0);
2577 if (srcLocalDesc instanceof InterDescriptor
2578 && ((InterDescriptor) srcLocalDesc).getMethodArgIdxPair() != null) {
2580 if (srcNode.getCompositeLocation() == null) {
2585 // if the srcNode is started with the global descriptor
2586 // need to set as a skeleton node
2587 if (!hasGlobalAccess && srcNode.getDescTuple().startsWith(GLOBALDESC)) {
2588 hasGlobalAccess = true;
2591 // Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(originalSrcNode);
2592 // for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
2593 // FlowEdge outEdge = (FlowEdge) iterator2.next();
2594 // FlowNode originalDstNode = outEdge.getDst();
2595 FlowNode originalDstNode = fg.getFlowNode(edge.getEndTuple());
2597 Set<FlowNode> dstNodeSet = new HashSet<FlowNode>();
2598 if (originalDstNode instanceof FlowReturnNode) {
2599 FlowReturnNode rnode = (FlowReturnNode) originalDstNode;
2600 // System.out.println("\n-returnNode=" + rnode);
2601 Set<NTuple<Descriptor>> tupleSet = rnode.getReturnTupleSet();
2602 for (Iterator iterator4 = tupleSet.iterator(); iterator4.hasNext();) {
2603 NTuple<Descriptor> nTuple = (NTuple<Descriptor>) iterator4.next();
2604 dstNodeSet.add(fg.getFlowNode(nTuple));
2605 System.out.println("&&&DST fg.getFlowNode(nTuple)=" + fg.getFlowNode(nTuple));
2608 dstNodeSet.add(originalDstNode);
2610 // System.out.println("---dstNodeSet=" + dstNodeSet);
2611 for (Iterator iterator4 = dstNodeSet.iterator(); iterator4.hasNext();) {
2612 FlowNode dstNode = (FlowNode) iterator4.next();
2614 NTuple<Descriptor> dstNodeTuple = dstNode.getDescTuple();
2615 Descriptor dstLocalDesc = dstNodeTuple.get(0);
2617 if (dstLocalDesc instanceof InterDescriptor
2618 && ((InterDescriptor) dstLocalDesc).getMethodArgIdxPair() != null) {
2619 if (dstNode.getCompositeLocation() == null) {
2620 System.out.println("%%%%%%%%%%%%%SKIP=" + dstNode);
2625 // if (outEdge.getInitTuple().equals(srcNodeTuple)
2626 // && outEdge.getEndTuple().equals(dstNodeTuple)) {
2628 NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
2629 NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
2631 System.out.println("-srcCurTuple=" + srcCurTuple + " dstCurTuple=" + dstCurTuple
2632 + " srcNode=" + srcNode + " dstNode=" + dstNode);
2634 // srcCurTuple = translateBaseTuple(srcNode, srcCurTuple);
2635 // dstCurTuple = translateBaseTuple(dstNode, dstCurTuple);
2637 if ((srcCurTuple.size() > 1 && dstCurTuple.size() > 1)
2638 && srcCurTuple.get(0).equals(dstCurTuple.get(0))) {
2640 // value flows between fields
2641 Descriptor desc = srcCurTuple.get(0);
2642 ClassDescriptor classDesc;
2644 if (desc.equals(GLOBALDESC)) {
2645 classDesc = md.getClassDesc();
2647 VarDescriptor varDesc = (VarDescriptor) srcCurTuple.get(0);
2648 classDesc = varDesc.getType().getClassDesc();
2650 extractFlowsBetweenFields(classDesc, srcNode, dstNode, 1);
2652 } else if ((srcCurTuple.size() == 1 && dstCurTuple.size() == 1)
2653 || ((srcCurTuple.size() > 1 || dstCurTuple.size() > 1) && !srcCurTuple.get(0).equals(
2654 dstCurTuple.get(0)))) {
2656 // value flow between a primitive local var - a primitive local var or local var -
2659 Descriptor srcDesc = srcCurTuple.get(0);
2660 Descriptor dstDesc = dstCurTuple.get(0);
2662 methodGraph.addEdge(srcDesc, dstDesc);
2664 if (fg.isParamDesc(srcDesc)) {
2665 methodGraph.setParamHNode(srcDesc);
2667 if (fg.isParamDesc(dstDesc)) {
2668 methodGraph.setParamHNode(dstDesc);
2682 // If the method accesses static fields
2683 // set hasGloabalAccess true in the method summary.
2684 if (hasGlobalAccess) {
2685 getMethodSummary(md).setHasGlobalAccess();
2687 methodGraph.getHNode(GLOBALDESC).setSkeleton(true);
2689 if (ssjava.getMethodContainingSSJavaLoop().equals(md)) {
2690 // if the current method contains the event loop
2691 // we need to set all nodes of the hierarchy graph as a skeleton node
2692 Set<HNode> hnodeSet = methodGraph.getNodeSet();
2693 for (Iterator iterator = hnodeSet.iterator(); iterator.hasNext();) {
2694 HNode hnode = (HNode) iterator.next();
2695 hnode.setSkeleton(true);
2701 private NTuple<Descriptor> translateBaseTuple(FlowNode flowNode, NTuple<Descriptor> inTuple) {
2703 if (flowNode.getBaseTuple() != null) {
2705 NTuple<Descriptor> translatedTuple = new NTuple<Descriptor>();
2707 NTuple<Descriptor> baseTuple = flowNode.getBaseTuple();
2709 for (int i = 0; i < baseTuple.size(); i++) {
2710 translatedTuple.add(baseTuple.get(i));
2713 for (int i = 1; i < inTuple.size(); i++) {
2714 translatedTuple.add(inTuple.get(i));
2717 System.out.println("------TRANSLATED " + inTuple + " -> " + translatedTuple);
2718 return translatedTuple;
2726 private MethodSummary getMethodSummary(MethodDescriptor md) {
2727 if (!mapDescToLocationSummary.containsKey(md)) {
2728 mapDescToLocationSummary.put(md, new MethodSummary(md));
2730 return (MethodSummary) mapDescToLocationSummary.get(md);
2733 private void addMapClassDefinitionToLineNum(ClassDescriptor cd, String strLine, int lineNum) {
2735 String classSymbol = cd.getSymbol();
2736 int idx = classSymbol.lastIndexOf("$");
2738 classSymbol = classSymbol.substring(idx + 1);
2741 String pattern = "class " + classSymbol + " ";
2742 if (strLine.indexOf(pattern) != -1) {
2743 mapDescToDefinitionLine.put(cd, lineNum);
2747 private void addMapMethodDefinitionToLineNum(Set<MethodDescriptor> methodSet, String strLine,
2749 for (Iterator iterator = methodSet.iterator(); iterator.hasNext();) {
2750 MethodDescriptor md = (MethodDescriptor) iterator.next();
2751 String pattern = md.getMethodDeclaration();
2752 if (strLine.indexOf(pattern) != -1) {
2753 mapDescToDefinitionLine.put(md, lineNum);
2754 methodSet.remove(md);
2761 private void readOriginalSourceFiles() {
2763 SymbolTable classtable = state.getClassSymbolTable();
2765 Set<ClassDescriptor> classDescSet = new HashSet<ClassDescriptor>();
2766 classDescSet.addAll(classtable.getValueSet());
2769 // inefficient implement. it may re-visit the same file if the file
2770 // contains more than one class definitions.
2771 for (Iterator iterator = classDescSet.iterator(); iterator.hasNext();) {
2772 ClassDescriptor cd = (ClassDescriptor) iterator.next();
2774 Set<MethodDescriptor> methodSet = new HashSet<MethodDescriptor>();
2775 methodSet.addAll(cd.getMethodTable().getValueSet());
2777 String sourceFileName = cd.getSourceFileName();
2778 Vector<String> lineVec = new Vector<String>();
2780 mapFileNameToLineVector.put(sourceFileName, lineVec);
2782 BufferedReader in = new BufferedReader(new FileReader(sourceFileName));
2785 lineVec.add(""); // the index is started from 1.
2786 while ((strLine = in.readLine()) != null) {
2787 lineVec.add(lineNum, strLine);
2788 addMapClassDefinitionToLineNum(cd, strLine, lineNum);
2789 addMapMethodDefinitionToLineNum(methodSet, strLine, lineNum);
2795 } catch (IOException e) {
2796 e.printStackTrace();
2801 private String generateLatticeDefinition(Descriptor desc) {
2803 Set<String> sharedLocSet = new HashSet<String>();
2805 SSJavaLattice<String> lattice = getLattice(desc);
2806 String rtr = "@LATTICE(\"";
2808 Map<String, Set<String>> map = lattice.getTable();
2809 Set<String> keySet = map.keySet();
2810 boolean first = true;
2811 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2812 String key = (String) iterator.next();
2813 if (!key.equals(lattice.getTopItem())) {
2814 Set<String> connectedSet = map.get(key);
2816 if (connectedSet.size() == 1) {
2817 if (connectedSet.iterator().next().equals(lattice.getBottomItem())) {
2825 if (lattice.isSharedLoc(key)) {
2826 rtr += "," + key + "*";
2831 for (Iterator iterator2 = connectedSet.iterator(); iterator2.hasNext();) {
2832 String loc = (String) iterator2.next();
2833 if (!loc.equals(lattice.getBottomItem())) {
2840 rtr += loc + "<" + key;
2841 if (lattice.isSharedLoc(key) && (!sharedLocSet.contains(key))) {
2842 rtr += "," + key + "*";
2843 sharedLocSet.add(key);
2845 if (lattice.isSharedLoc(loc) && (!sharedLocSet.contains(loc))) {
2846 rtr += "," + loc + "*";
2847 sharedLocSet.add(loc);
2855 if (desc instanceof MethodDescriptor) {
2856 System.out.println("#EXTRA LOC DECLARATION GEN=" + desc);
2858 MethodDescriptor md = (MethodDescriptor) desc;
2859 MethodSummary methodSummary = getMethodSummary(md);
2861 TypeDescriptor returnType = ((MethodDescriptor) desc).getReturnType();
2862 if (!ssjava.getMethodContainingSSJavaLoop().equals(desc) && returnType != null
2863 && (!returnType.isVoid())) {
2864 CompositeLocation returnLoc = methodSummary.getRETURNLoc();
2865 if (returnLoc.getSize() == 1) {
2866 String returnLocStr = generateLocationAnnoatation(methodSummary.getRETURNLoc());
2867 if (rtr.indexOf(returnLocStr) == -1) {
2868 rtr += "," + returnLocStr;
2874 if (!ssjava.getMethodContainingSSJavaLoop().equals(desc)) {
2875 if (returnType != null && (!returnType.isVoid())) {
2877 "\n@RETURNLOC(\"" + generateLocationAnnoatation(methodSummary.getRETURNLoc()) + "\")";
2880 CompositeLocation pcLoc = methodSummary.getPCLoc();
2881 if ((pcLoc != null) && (!pcLoc.get(0).isTop())) {
2882 rtr += "\n@PCLOC(\"" + generateLocationAnnoatation(pcLoc) + "\")";
2886 if (!md.isStatic()) {
2887 rtr += "\n@THISLOC(\"" + methodSummary.getThisLocName() + "\")";
2889 rtr += "\n@GLOBALLOC(\"" + methodSummary.getGlobalLocName() + "\")";
2898 private void generateAnnoatedCode() {
2900 readOriginalSourceFiles();
2903 while (!toAnalyzeIsEmpty()) {
2904 ClassDescriptor cd = toAnalyzeNext();
2906 setupToAnalazeMethod(cd);
2908 String sourceFileName = cd.getSourceFileName();
2910 if (cd.isInterface()) {
2914 int classDefLine = mapDescToDefinitionLine.get(cd);
2915 Vector<String> sourceVec = mapFileNameToLineVector.get(sourceFileName);
2917 LocationSummary fieldLocSummary = getLocationSummary(cd);
2919 String fieldLatticeDefStr = generateLatticeDefinition(cd);
2920 String annoatedSrc = fieldLatticeDefStr + newline + sourceVec.get(classDefLine);
2921 sourceVec.set(classDefLine, annoatedSrc);
2923 // generate annotations for field declarations
2924 // Map<Descriptor, CompositeLocation> inferLocMap = fieldLocInfo.getMapDescToInferLocation();
2925 Map<String, String> mapFieldNameToLocName = fieldLocSummary.getMapHNodeNameToLocationName();
2927 for (Iterator iter = cd.getFields(); iter.hasNext();) {
2928 FieldDescriptor fd = (FieldDescriptor) iter.next();
2930 String locAnnotationStr;
2931 // CompositeLocation inferLoc = inferLocMap.get(fd);
2932 String locName = mapFieldNameToLocName.get(fd.getSymbol());
2934 if (locName != null) {
2935 // infer loc is null if the corresponding field is static and final
2936 // locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
2937 locAnnotationStr = "@LOC(\"" + locName + "\")";
2938 int fdLineNum = fd.getLineNum();
2939 String orgFieldDeclarationStr = sourceVec.get(fdLineNum);
2940 String fieldDeclaration = fd.toString();
2941 fieldDeclaration = fieldDeclaration.substring(0, fieldDeclaration.length() - 1);
2942 String annoatedStr = locAnnotationStr + " " + orgFieldDeclarationStr;
2943 sourceVec.set(fdLineNum, annoatedStr);
2948 while (!toAnalyzeMethodIsEmpty()) {
2949 MethodDescriptor md = toAnalyzeMethodNext();
2951 if (!ssjava.needTobeAnnotated(md)) {
2955 SSJavaLattice<String> methodLattice = md2lattice.get(md);
2956 if (methodLattice != null) {
2958 int methodDefLine = md.getLineNum();
2960 // MethodLocationInfo methodLocInfo = getMethodLocationInfo(md);
2961 // Map<Descriptor, CompositeLocation> methodInferLocMap =
2962 // methodLocInfo.getMapDescToInferLocation();
2964 MethodSummary methodSummary = getMethodSummary(md);
2966 Map<Descriptor, CompositeLocation> mapVarDescToInferLoc =
2967 methodSummary.getMapVarDescToInferCompositeLocation();
2968 System.out.println("-----md=" + md);
2969 System.out.println("-----mapVarDescToInferLoc=" + mapVarDescToInferLoc);
2971 Set<Descriptor> localVarDescSet = mapVarDescToInferLoc.keySet();
2973 Set<String> localLocElementSet = methodLattice.getElementSet();
2975 for (Iterator iterator = localVarDescSet.iterator(); iterator.hasNext();) {
2976 Descriptor localVarDesc = (Descriptor) iterator.next();
2977 System.out.println("-------localVarDesc=" + localVarDesc);
2978 CompositeLocation inferLoc = mapVarDescToInferLoc.get(localVarDesc);
2980 String localLocIdentifier = inferLoc.get(0).getLocIdentifier();
2981 if (!localLocElementSet.contains(localLocIdentifier)) {
2982 methodLattice.put(localLocIdentifier);
2985 String locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
2987 if (!isParameter(md, localVarDesc)) {
2988 if (mapDescToDefinitionLine.containsKey(localVarDesc)) {
2989 int varLineNum = mapDescToDefinitionLine.get(localVarDesc);
2990 String orgSourceLine = sourceVec.get(varLineNum);
2991 System.out.println("varLineNum=" + varLineNum + " org src=" + orgSourceLine);
2993 orgSourceLine.indexOf(generateVarDeclaration((VarDescriptor) localVarDesc));
2994 System.out.println("idx=" + idx
2995 + " generateVarDeclaration((VarDescriptor) localVarDesc)="
2996 + generateVarDeclaration((VarDescriptor) localVarDesc));
2998 String annoatedStr =
2999 orgSourceLine.substring(0, idx) + locAnnotationStr + " "
3000 + orgSourceLine.substring(idx);
3001 sourceVec.set(varLineNum, annoatedStr);
3004 String methodDefStr = sourceVec.get(methodDefLine);
3007 getParamLocation(methodDefStr,
3008 generateVarDeclaration((VarDescriptor) localVarDesc));
3009 System.out.println("methodDefStr=" + methodDefStr + " localVarDesc=" + localVarDesc
3013 String annoatedStr =
3014 methodDefStr.substring(0, idx) + locAnnotationStr + " "
3015 + methodDefStr.substring(idx);
3016 sourceVec.set(methodDefLine, annoatedStr);
3021 // check if the lattice has to have the location type for the this
3024 // boolean needToAddthisRef = hasThisReference(md);
3025 // if (localLocElementSet.contains("this")) {
3026 // methodLattice.put("this");
3029 String methodLatticeDefStr = generateLatticeDefinition(md);
3030 String annoatedStr = methodLatticeDefStr + newline + sourceVec.get(methodDefLine);
3031 sourceVec.set(methodDefLine, annoatedStr);
3041 private boolean hasThisReference(MethodDescriptor md) {
3043 FlowGraph fg = getFlowGraph(md);
3044 Set<FlowNode> nodeSet = fg.getNodeSet();
3045 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
3046 FlowNode flowNode = (FlowNode) iterator.next();
3047 if (flowNode.getDescTuple().get(0).equals(md.getThis())) {
3055 private int getParamLocation(String methodStr, String paramStr) {
3057 String pattern = paramStr + ",";
3059 int idx = methodStr.indexOf(pattern);
3063 pattern = paramStr + ")";
3064 return methodStr.indexOf(pattern);
3069 private String generateVarDeclaration(VarDescriptor varDesc) {
3071 TypeDescriptor td = varDesc.getType();
3072 String rtr = td.toString();
3074 for (int i = 0; i < td.getArrayCount(); i++) {
3078 rtr += " " + varDesc.getName();
3083 private String generateLocationAnnoatation(CompositeLocation loc) {
3086 Location methodLoc = loc.get(0);
3087 rtr += methodLoc.getLocIdentifier();
3089 for (int i = 1; i < loc.getSize(); i++) {
3090 Location element = loc.get(i);
3091 rtr += "," + element.getDescriptor().getSymbol() + "." + element.getLocIdentifier();
3097 private boolean isParameter(MethodDescriptor md, Descriptor localVarDesc) {
3098 return getFlowGraph(md).isParamDesc(localVarDesc);
3101 private String extractFileName(String fileName) {
3102 int idx = fileName.lastIndexOf("/");
3106 return fileName.substring(idx + 1);
3111 private void codeGen() {
3113 Set<String> originalFileNameSet = mapFileNameToLineVector.keySet();
3114 for (Iterator iterator = originalFileNameSet.iterator(); iterator.hasNext();) {
3115 String orgFileName = (String) iterator.next();
3116 String outputFileName = extractFileName(orgFileName);
3118 Vector<String> sourceVec = mapFileNameToLineVector.get(orgFileName);
3122 FileWriter fileWriter = new FileWriter("./infer/" + outputFileName);
3123 BufferedWriter out = new BufferedWriter(fileWriter);
3125 for (int i = 0; i < sourceVec.size(); i++) {
3126 out.write(sourceVec.get(i));
3130 } catch (IOException e) {
3131 e.printStackTrace();
3138 private void checkLattices() {
3140 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
3142 // current descriptors to visit in fixed-point interprocedural analysis,
3144 // dependency in the call graph
3145 methodDescriptorsToVisitStack.clear();
3147 // descriptorListToAnalyze.removeFirst();
3149 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
3150 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
3152 while (!descriptorListToAnalyze.isEmpty()) {
3153 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
3154 checkLatticesOfVirtualMethods(md);
3159 private void debug_writeLatticeDotFile() {
3160 // generate lattice dot file
3164 while (!toAnalyzeIsEmpty()) {
3165 ClassDescriptor cd = toAnalyzeNext();
3167 setupToAnalazeMethod(cd);
3169 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
3170 if (classLattice != null) {
3171 ssjava.writeLatticeDotFile(cd, null, classLattice);
3172 debug_printDescriptorToLocNameMapping(cd);
3175 while (!toAnalyzeMethodIsEmpty()) {
3176 MethodDescriptor md = toAnalyzeMethodNext();
3177 SSJavaLattice<String> methodLattice = md2lattice.get(md);
3178 if (methodLattice != null) {
3179 ssjava.writeLatticeDotFile(cd, md, methodLattice);
3180 debug_printDescriptorToLocNameMapping(md);
3187 private void debug_printDescriptorToLocNameMapping(Descriptor desc) {
3189 LocationInfo info = getLocationInfo(desc);
3190 System.out.println("## " + desc + " ##");
3191 System.out.println(info.getMapDescToInferLocation());
3192 LocationInfo locInfo = getLocationInfo(desc);
3193 System.out.println("mapping=" + locInfo.getMapLocSymbolToDescSet());
3194 System.out.println("###################");
3198 private void calculateExtraLocations() {
3200 LinkedList<MethodDescriptor> methodDescList = ssjava.getSortedDescriptors();
3201 for (Iterator iterator = methodDescList.iterator(); iterator.hasNext();) {
3202 MethodDescriptor md = (MethodDescriptor) iterator.next();
3203 if (!ssjava.getMethodContainingSSJavaLoop().equals(md)) {
3204 calculateExtraLocations(md);
3210 private void checkLatticesOfVirtualMethods(MethodDescriptor md) {
3212 if (!md.isStatic()) {
3213 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
3214 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
3216 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
3217 MethodDescriptor mdCallee = (MethodDescriptor) iterator.next();
3218 if (!md.equals(mdCallee)) {
3219 checkConsistency(md, mdCallee);
3227 private void checkConsistency(MethodDescriptor md1, MethodDescriptor md2) {
3229 // check that two lattice have the same relations between parameters(+PC
3230 // LOC, GLOBAL_LOC RETURN LOC)
3232 List<CompositeLocation> list1 = new ArrayList<CompositeLocation>();
3233 List<CompositeLocation> list2 = new ArrayList<CompositeLocation>();
3235 MethodLocationInfo locInfo1 = getMethodLocationInfo(md1);
3236 MethodLocationInfo locInfo2 = getMethodLocationInfo(md2);
3238 Map<Integer, CompositeLocation> paramMap1 = locInfo1.getMapParamIdxToInferLoc();
3239 Map<Integer, CompositeLocation> paramMap2 = locInfo2.getMapParamIdxToInferLoc();
3241 int numParam = locInfo1.getMapParamIdxToInferLoc().keySet().size();
3243 // add location types of paramters
3244 for (int idx = 0; idx < numParam; idx++) {
3245 list1.add(paramMap1.get(Integer.valueOf(idx)));
3246 list2.add(paramMap2.get(Integer.valueOf(idx)));
3249 // add program counter location
3250 list1.add(locInfo1.getPCLoc());
3251 list2.add(locInfo2.getPCLoc());
3253 if (!md1.getReturnType().isVoid()) {
3254 // add return value location
3255 CompositeLocation rtrLoc1 = getMethodLocationInfo(md1).getReturnLoc();
3256 CompositeLocation rtrLoc2 = getMethodLocationInfo(md2).getReturnLoc();
3261 // add global location type
3262 if (md1.isStatic()) {
3263 CompositeLocation globalLoc1 =
3264 new CompositeLocation(new Location(md1, locInfo1.getGlobalLocName()));
3265 CompositeLocation globalLoc2 =
3266 new CompositeLocation(new Location(md2, locInfo2.getGlobalLocName()));
3267 list1.add(globalLoc1);
3268 list2.add(globalLoc2);
3271 for (int i = 0; i < list1.size(); i++) {
3272 CompositeLocation locA1 = list1.get(i);
3273 CompositeLocation locA2 = list2.get(i);
3274 for (int k = 0; k < list1.size(); k++) {
3276 CompositeLocation locB1 = list1.get(k);
3277 CompositeLocation locB2 = list2.get(k);
3278 boolean r1 = isGreaterThan(getLattice(md1), locA1, locB1);
3280 boolean r2 = isGreaterThan(getLattice(md1), locA2, locB2);
3283 throw new Error("The method " + md1 + " is not consistent with the method " + md2
3284 + ".:: They have a different ordering relation between locations (" + locA1 + ","
3285 + locB1 + ") and (" + locA2 + "," + locB2 + ").");
3293 private String getSymbol(int idx, FlowNode node) {
3294 Descriptor desc = node.getDescTuple().get(idx);
3295 return desc.getSymbol();
3298 private Descriptor getDescriptor(int idx, FlowNode node) {
3299 Descriptor desc = node.getDescTuple().get(idx);
3303 private void calculatePCLOC(MethodDescriptor md) {
3305 System.out.println("#CalculatePCLOC");
3306 MethodSummary methodSummary = getMethodSummary(md);
3307 FlowGraph fg = getFlowGraph(md);
3308 Map<Integer, CompositeLocation> mapParamToLoc = methodSummary.getMapParamIdxToInferLoc();
3310 // calculate the initial program counter location
3311 // PC location is higher than location types of parameters which has incoming flows.
3313 Set<NTuple<Location>> paramLocTupleHavingInFlowSet = new HashSet<NTuple<Location>>();
3314 Set<Descriptor> paramDescNOTHavingInFlowSet = new HashSet<Descriptor>();
3315 // Set<FlowNode> paramNodeNOThavingInFlowSet = new HashSet<FlowNode>();
3317 int numParams = fg.getNumParameters();
3318 for (int i = 0; i < numParams; i++) {
3319 FlowNode paramFlowNode = fg.getParamFlowNode(i);
3320 Descriptor prefix = paramFlowNode.getDescTuple().get(0);
3321 NTuple<Descriptor> paramDescTuple = paramFlowNode.getCurrentDescTuple();
3322 NTuple<Location> paramLocTuple = translateToLocTuple(md, paramDescTuple);
3324 Set<FlowNode> inNodeToParamSet = fg.getIncomingNodeSetByPrefix(prefix);
3325 if (inNodeToParamSet.size() > 0) {
3326 // parameter has in-value flows
3328 for (Iterator iterator = inNodeToParamSet.iterator(); iterator.hasNext();) {
3329 FlowNode inNode = (FlowNode) iterator.next();
3330 Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(inNode);
3331 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
3332 FlowEdge flowEdge = (FlowEdge) iterator2.next();
3333 if (flowEdge.getEndTuple().startsWith(prefix)) {
3334 NTuple<Location> paramLocTupleWithIncomingFlow =
3335 translateToLocTuple(md, flowEdge.getEndTuple());