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 // maps a method descriptor to a sub global flow graph that captures all value flows caused by the
118 // set of callees reachable from the method
119 private Map<MethodDescriptor, GlobalFlowGraph> mapMethodDescriptorToSubGlobalFlowGraph;
121 private Map<MethodInvokeNode, Map<NTuple<Descriptor>, NTuple<Descriptor>>> mapMethodInvokeNodeToMapCallerArgToCalleeArg;
123 public static final String GLOBALLOC = "GLOBALLOC";
125 public static final String INTERLOC = "INTERLOC";
127 public static final String PCLOC = "_PCLOC_";
129 public static final String RLOC = "_RLOC_";
131 public static final Descriptor GLOBALDESC = new NameDescriptor(GLOBALLOC);
133 public static final Descriptor TOPDESC = new NameDescriptor(SSJavaAnalysis.TOP);
135 public static final Descriptor BOTTOMDESC = new NameDescriptor(SSJavaAnalysis.BOTTOM);
137 public static final Descriptor RETURNLOC = new NameDescriptor(RLOC);
139 public static final Descriptor LITERALDESC = new NameDescriptor("LITERAL");
141 public static final HNode TOPHNODE = new HNode(TOPDESC);
143 public static final HNode BOTTOMHNODE = new HNode(BOTTOMDESC);
145 public static String newline = System.getProperty("line.separator");
147 LocationInfo curMethodInfo;
149 private boolean hasChanges = false;
151 boolean debug = true;
153 public static int locSeed = 0;
155 private Stack<String> arrayAccessNodeStack;
157 public LocationInference(SSJavaAnalysis ssjava, State state) {
158 this.ssjava = ssjava;
160 this.temp_toanalyzeList = new ArrayList<ClassDescriptor>();
161 this.temp_toanalyzeMethodList = new ArrayList<MethodDescriptor>();
162 this.mapMethodDescriptorToFlowGraph = new HashMap<MethodDescriptor, FlowGraph>();
163 this.cd2lattice = new HashMap<ClassDescriptor, SSJavaLattice<String>>();
164 this.md2lattice = new HashMap<MethodDescriptor, SSJavaLattice<String>>();
165 this.methodDescriptorsToVisitStack = new Stack<MethodDescriptor>();
166 this.mapMethodDescriptorToMethodInvokeNodeSet =
167 new HashMap<MethodDescriptor, Set<MethodInvokeNode>>();
168 this.mapMethodInvokeNodeToArgIdxMap =
169 new HashMap<MethodInvokeNode, Map<Integer, NTuple<Descriptor>>>();
170 this.mapMethodDescToMethodLocationInfo = new HashMap<MethodDescriptor, MethodLocationInfo>();
171 this.mapMethodToCalleeSet = new HashMap<MethodDescriptor, Set<MethodDescriptor>>();
172 this.mapClassToLocationInfo = new HashMap<ClassDescriptor, LocationInfo>();
174 this.mapFileNameToLineVector = new HashMap<String, Vector<String>>();
175 this.mapDescToDefinitionLine = new HashMap<Descriptor, Integer>();
176 this.mapMethodDescToParamNodeFlowsToReturnValue =
177 new HashMap<MethodDescriptor, Set<FlowNode>>();
179 this.mapDescriptorToHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
180 this.mapMethodInvokeNodeToBaseTuple = new HashMap<MethodInvokeNode, NTuple<Descriptor>>();
182 this.mapDescriptorToSkeletonHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
183 this.mapDescriptorToCombineSkeletonHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
184 this.mapDescriptorToSimpleHierarchyGraph = new HashMap<Descriptor, HierarchyGraph>();
186 this.mapDescriptorToSimpleLattice = new HashMap<Descriptor, SSJavaLattice<String>>();
188 this.mapDescToLocationSummary = new HashMap<Descriptor, LocationSummary>();
190 this.mapMethodDescriptorToSubGlobalFlowGraph = new HashMap<MethodDescriptor, GlobalFlowGraph>();
192 this.mapMethodInvokeNodeToMapCallerArgToCalleeArg =
193 new HashMap<MethodInvokeNode, Map<NTuple<Descriptor>, NTuple<Descriptor>>>();
195 this.mapMethodInvokeNodeToPCLocTupleSet =
196 new HashMap<MethodInvokeNode, Set<NTuple<Location>>>();
198 this.arrayAccessNodeStack = new Stack<String>();
202 public void setupToAnalyze() {
203 SymbolTable classtable = state.getClassSymbolTable();
204 temp_toanalyzeList.clear();
205 temp_toanalyzeList.addAll(classtable.getValueSet());
206 // Collections.sort(toanalyzeList, new Comparator<ClassDescriptor>() {
207 // public int compare(ClassDescriptor o1, ClassDescriptor o2) {
208 // return o1.getClassName().compareToIgnoreCase(o2.getClassName());
213 public void setupToAnalazeMethod(ClassDescriptor cd) {
215 SymbolTable methodtable = cd.getMethodTable();
216 temp_toanalyzeMethodList.clear();
217 temp_toanalyzeMethodList.addAll(methodtable.getValueSet());
218 Collections.sort(temp_toanalyzeMethodList, new Comparator<MethodDescriptor>() {
219 public int compare(MethodDescriptor o1, MethodDescriptor o2) {
220 return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
225 public boolean toAnalyzeMethodIsEmpty() {
226 return temp_toanalyzeMethodList.isEmpty();
229 public boolean toAnalyzeIsEmpty() {
230 return temp_toanalyzeList.isEmpty();
233 public ClassDescriptor toAnalyzeNext() {
234 return temp_toanalyzeList.remove(0);
237 public MethodDescriptor toAnalyzeMethodNext() {
238 return temp_toanalyzeMethodList.remove(0);
241 public void inference() {
245 // construct value flow graph
246 constructFlowGraph();
248 constructGlobalFlowGraph();
249 // _debug_writeFlowGraph();
253 assignCompositeLocation();
255 calculateExtraLocations();
257 addAdditionalOrderingConstraints();
258 System.out.println("&&&&&&&&&&&&&&&&&&&&&&has changes=" + hasChanges);
259 } while (hasChanges);
261 _debug_writeFlowGraph();
265 constructHierarchyGraph();
267 debug_writeHierarchyDotFiles();
269 simplifyHierarchyGraph();
271 debug_writeSimpleHierarchyDotFiles();
273 constructSkeletonHierarchyGraph();
275 debug_writeSkeletonHierarchyDotFiles();
277 insertCombinationNodes();
279 debug_writeSkeletonCombinationHierarchyDotFiles();
283 debug_writeLattices();
285 updateCompositeLocationAssignments();
287 generateMethodSummary();
289 generateAnnoatedCode();
295 private void updateFlowGraph() {
297 LinkedList<MethodDescriptor> methodDescList =
298 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
300 while (!methodDescList.isEmpty()) {
301 MethodDescriptor md = methodDescList.removeLast();
302 if (state.SSJAVADEBUG) {
303 System.out.println();
304 System.out.println("SSJAVA: Updating a flow graph: " + md);
305 propagateFlowsFromCalleesWithNoCompositeLocation(md);
310 public Map<NTuple<Descriptor>, NTuple<Descriptor>> getMapCallerArgToCalleeParam(
311 MethodInvokeNode min) {
313 if (!mapMethodInvokeNodeToMapCallerArgToCalleeArg.containsKey(min)) {
314 mapMethodInvokeNodeToMapCallerArgToCalleeArg.put(min,
315 new HashMap<NTuple<Descriptor>, NTuple<Descriptor>>());
318 return mapMethodInvokeNodeToMapCallerArgToCalleeArg.get(min);
321 public void addMapCallerArgToCalleeParam(MethodInvokeNode min, NTuple<Descriptor> callerArg,
322 NTuple<Descriptor> calleeParam) {
323 getMapCallerArgToCalleeParam(min).put(callerArg, calleeParam);
326 private void assignCompositeLocation() {
327 calculateGlobalValueFlowCompositeLocation();
328 translateCompositeLocationAssignmentToFlowGraph();
331 private void translateCompositeLocationAssignmentToFlowGraph() {
332 System.out.println("\nSSJAVA: Translate composite location assignments to flow graphs:");
333 MethodDescriptor methodEventLoopDesc = ssjava.getMethodContainingSSJavaLoop();
334 translateCompositeLocationAssignmentToFlowGraph(methodEventLoopDesc);
337 private void addAdditionalOrderingConstraints() {
338 System.out.println("\nSSJAVA: Add addtional ordering constriants:");
339 MethodDescriptor methodEventLoopDesc = ssjava.getMethodContainingSSJavaLoop();
340 addAddtionalOrderingConstraints(methodEventLoopDesc);
343 private void updateCompositeLocationAssignments() {
345 LinkedList<MethodDescriptor> methodDescList =
346 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
348 while (!methodDescList.isEmpty()) {
349 MethodDescriptor md = methodDescList.removeLast();
351 System.out.println("\n#updateCompositeLocationAssignments=" + md);
353 FlowGraph flowGraph = getFlowGraph(md);
355 MethodSummary methodSummary = getMethodSummary(md);
357 Set<FlowNode> nodeSet = flowGraph.getNodeSet();
358 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
359 FlowNode node = (FlowNode) iterator.next();
360 System.out.println("-node=" + node + " node.getDescTuple=" + node.getDescTuple());
361 if (node.getCompositeLocation() != null) {
362 CompositeLocation compLoc = node.getCompositeLocation();
363 CompositeLocation updatedCompLoc = updateCompositeLocation(compLoc);
364 node.setCompositeLocation(updatedCompLoc);
365 System.out.println("---updatedCompLoc1=" + updatedCompLoc);
367 NTuple<Descriptor> descTuple = node.getDescTuple();
368 System.out.println("update desc=" + descTuple);
369 CompositeLocation compLoc = convertToCompositeLocation(md, descTuple);
370 compLoc = updateCompositeLocation(compLoc);
371 node.setCompositeLocation(compLoc);
372 System.out.println("---updatedCompLoc2=" + compLoc);
375 if (node.isDeclaratonNode()) {
376 Descriptor localVarDesc = node.getDescTuple().get(0);
377 CompositeLocation compLoc = updateCompositeLocation(node.getCompositeLocation());
378 methodSummary.addMapVarNameToInferCompLoc(localVarDesc, compLoc);
382 // update PCLOC and RETURNLOC if they have a composite location assignment
383 if (methodSummary.getRETURNLoc() != null) {
384 methodSummary.setRETURNLoc(updateCompositeLocation(methodSummary.getRETURNLoc()));
386 if (methodSummary.getPCLoc() != null) {
387 methodSummary.setPCLoc(updateCompositeLocation(methodSummary.getPCLoc()));
394 private CompositeLocation updateCompositeLocation(CompositeLocation compLoc) {
395 CompositeLocation updatedCompLoc = new CompositeLocation();
396 for (int i = 0; i < compLoc.getSize(); i++) {
397 Location loc = compLoc.get(i);
398 String nodeIdentifier = loc.getLocIdentifier();
399 Descriptor enclosingDesc = loc.getDescriptor();
401 if (!enclosingDesc.equals(GLOBALDESC)) {
402 LocationSummary locSummary = getLocationSummary(enclosingDesc);
403 HierarchyGraph scGraph = getSkeletonCombinationHierarchyGraph(enclosingDesc);
404 if (scGraph != null) {
405 HNode curNode = scGraph.getCurrentHNode(nodeIdentifier);
406 if (curNode != null) {
407 nodeIdentifier = curNode.getName();
410 locName = locSummary.getLocationName(nodeIdentifier);
412 locName = nodeIdentifier;
414 Location updatedLoc = new Location(enclosingDesc, locName);
415 updatedCompLoc.addLocation(updatedLoc);
418 return updatedCompLoc;
421 private void translateCompositeLocationAssignmentToFlowGraph(MethodDescriptor mdCaller) {
423 System.out.println("\n\n###translateCompositeLocationAssignmentToFlowGraph mdCaller="
426 // First, assign a composite location to a node in the flow graph
427 GlobalFlowGraph callerGlobalFlowGraph = getSubGlobalFlowGraph(mdCaller);
429 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
430 Map<Location, CompositeLocation> callerMapLocToCompLoc =
431 callerGlobalFlowGraph.getMapLocationToInferCompositeLocation();
433 Set<Location> methodLocSet = callerMapLocToCompLoc.keySet();
434 for (Iterator iterator = methodLocSet.iterator(); iterator.hasNext();) {
435 Location methodLoc = (Location) iterator.next();
436 if (methodLoc.getDescriptor().equals(mdCaller)) {
437 CompositeLocation inferCompLoc = callerMapLocToCompLoc.get(methodLoc);
438 assignCompositeLocationToFlowGraph(callerFlowGraph, methodLoc, inferCompLoc);
442 Set<MethodInvokeNode> minSet = mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
444 Set<MethodDescriptor> calleeSet = new HashSet<MethodDescriptor>();
445 for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
446 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
447 // need to translate a composite location that is started with the base
449 translateMapLocationToInferCompositeLocationToCalleeGraph(callerGlobalFlowGraph, min);
450 MethodDescriptor mdCallee = min.getMethod();
451 calleeSet.add(mdCallee);
453 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
455 NTuple<Descriptor> methodInvokeBaseDescTuple = mapMethodInvokeNodeToBaseTuple.get(min);
456 NTuple<Location> methodInvokeBaseLocTuple = null;
457 if (methodInvokeBaseDescTuple != null) {
458 methodInvokeBaseLocTuple = translateToLocTuple(mdCaller, methodInvokeBaseDescTuple);
464 // If the location of an argument has a composite location
465 // need to assign a proper composite location to the corresponding callee parameter
466 // System.out.println("---translate arg composite location to callee param. min="
467 // + min.printNode(0));
468 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
469 Set<Integer> idxSet = mapIdxToArgTuple.keySet();
470 for (Iterator iterator2 = idxSet.iterator(); iterator2.hasNext();) {
471 Integer idx = (Integer) iterator2.next();
473 if (idx == 0 && !min.getMethod().isStatic()) {
477 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(idx);
478 if (argTuple.size() > 0) {
479 // check if an arg tuple has been already assigned to a composite location
480 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argTuple);
481 Location argLocalLoc = argLocTuple.get(0);
483 // if (!isPrimitiveType(argTuple)) {
484 if (callerMapLocToCompLoc.containsKey(argLocalLoc)) {
486 CompositeLocation argLocalCompositeLocation = callerMapLocToCompLoc.get(argLocalLoc);
487 CompositeLocation argCompLoc = argLocalCompositeLocation.clone();
488 for (int i = 1; i < argLocTuple.size(); i++) {
489 argCompLoc.addLocation(argLocTuple.get(i));
492 FlowNode calleeParamFlowNode = calleeFlowGraph.getParamFlowNode(idx);
495 .println("----- argLocTuple=" + argLocTuple + " argLocalLoc=+" + argLocalLoc);
496 System.out.println("-------need to translate argCompLoc=" + argCompLoc
497 + " with baseTuple=" + methodInvokeBaseLocTuple + " calleeParamLocTuple="
498 + calleeParamFlowNode);
500 CompositeLocation paramCompLoc = translateArgCompLocToParamCompLoc(min, argCompLoc);
501 calleeParamFlowNode.setCompositeLocation(paramCompLoc);
503 // if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
505 // FlowNode calleeParamFlowNode = calleeFlowGraph.getParamFlowNode(idx);
506 // NTuple<Descriptor> calleeParamDescTuple = calleeParamFlowNode.getDescTuple();
507 // NTuple<Location> calleeParamLocTuple
508 // =###translateCompositeLocationAssignmentToFlowGraph mdCaller=public static void
509 // huffcodetab.huffman_decoder(int htIdx, int x, BitReserve br)
511 // translateToLocTuple(mdCallee, calleeParamDescTuple);
513 // System.out.println("---need to translate callerCompLoc=" + callerCompLoc
514 // + " with baseTuple=" + baseLocTuple + " calleeParamLocTuple="
515 // + calleeParamLocTuple);
517 // CompositeLocation newCalleeCompLoc =
518 // translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
520 // calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
521 // newCalleeCompLoc);
523 // System.out.println("---callee loc=" + calleeParamLocTuple.get(0)
524 // + " newCalleeCompLoc=" + newCalleeCompLoc);
526 // // System.out.println("###need to assign composite location to=" +
527 // // calleeParamDescTuple
528 // // + " with baseTuple=" + baseLocTuple);
538 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
539 MethodDescriptor callee = (MethodDescriptor) iterator.next();
540 translateCompositeLocationAssignmentToFlowGraph(callee);
543 for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
544 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
545 // add an additional ordering constraint
546 // if the first element of a parameter composite location matches 'this' reference,
547 // the corresponding argument in the caller is required to be higher than the translated
548 // parameter location in the caller lattice
550 // addOrderingConstraintFromCompLocParamToArg(mdCaller, min);
555 private CompositeLocation translateArgCompLocToParamCompLoc(MethodInvokeNode min,
556 CompositeLocation argCompLoc) {
558 System.out.println("--------translateArgCompLocToParamCompLoc argCompLoc=" + argCompLoc);
559 MethodDescriptor mdCallee = min.getMethod();
560 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
562 NTuple<Location> argLocTuple = argCompLoc.getTuple();
563 Location argLocalLoc = argLocTuple.get(0);
565 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
566 Set<Integer> idxSet = mapIdxToArgTuple.keySet();
567 for (Iterator iterator2 = idxSet.iterator(); iterator2.hasNext();) {
568 Integer idx = (Integer) iterator2.next();
570 if (idx == 0 && !min.getMethod().isStatic()) {
574 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(idx);
575 if (argTuple.size() > 0 && argTuple.get(0).equals(argLocalLoc.getLocDescriptor())) {
576 // it matches with the current argument composite location
577 // so what is the corresponding parameter local descriptor?
578 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
579 System.out.println("----------found paramNode=" + paramNode);
580 NTuple<Descriptor> paramDescTuple = paramNode.getCurrentDescTuple();
582 NTuple<Location> newParamTupleFromArgTuple = translateToLocTuple(mdCallee, paramDescTuple);
583 for (int i = 1; i < argLocTuple.size(); i++) {
584 newParamTupleFromArgTuple.add(argLocTuple.get(i));
587 System.out.println("-----------newParamTuple=" + newParamTupleFromArgTuple);
588 return new CompositeLocation(newParamTupleFromArgTuple);
595 private void addAddtionalOrderingConstraints(MethodDescriptor mdCaller) {
597 // First, assign a composite location to a node in the flow graph
598 GlobalFlowGraph callerGlobalFlowGraph = getSubGlobalFlowGraph(mdCaller);
600 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
601 Map<Location, CompositeLocation> callerMapLocToCompLoc =
602 callerGlobalFlowGraph.getMapLocationToInferCompositeLocation();
603 Set<Location> methodLocSet = callerMapLocToCompLoc.keySet();
605 Set<MethodInvokeNode> minSet = mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
607 Set<MethodDescriptor> calleeSet = new HashSet<MethodDescriptor>();
608 for (Iterator iterator = minSet.iterator(); iterator.hasNext();) {
609 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
610 MethodDescriptor mdCallee = min.getMethod();
611 calleeSet.add(mdCallee);
614 // add an additional ordering constraint
615 // if the first element of a parameter composite location matches 'this' reference,
616 // the corresponding argument in the caller is required to be higher than the translated
617 // parameter location in the caller lattice
619 // addOrderingConstraintFromCompLocParamToArg(mdCaller, min);
622 // update return flow nodes in the caller
623 CompositeLocation returnLoc = getMethodSummary(mdCallee).getRETURNLoc();
625 System.out.println("### min=" + min.printNode(0) + " returnLoc=" + returnLoc);
626 if (returnLoc != null && returnLoc.get(0).getLocDescriptor().equals(mdCallee.getThis())
627 && returnLoc.getSize() > 1) {
628 System.out.println("###RETURN COMP LOC=" + returnLoc);
629 NTuple<Location> returnLocTuple = returnLoc.getTuple();
630 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
631 NTuple<Descriptor> newReturnTuple = baseTuple.clone();
632 for (int i = 1; i < returnLocTuple.size(); i++) {
633 newReturnTuple.add(returnLocTuple.get(i).getLocDescriptor());
635 System.out.println("###NEW RETURN TUPLE FOR CALLER=" + newReturnTuple);
636 callerFlowGraph.getFlowReturnNode(min).setNewTuple(newReturnTuple);
641 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
642 MethodDescriptor callee = (MethodDescriptor) iterator.next();
643 addAddtionalOrderingConstraints(callee);
648 private void addOrderingConstraintFromCompLocParamToArg(MethodDescriptor mdCaller,
649 MethodInvokeNode min) {
650 System.out.println("-addOrderingConstraintFromCompLocParamToArg=" + min.printNode(0));
652 GlobalFlowGraph globalGraph = getSubGlobalFlowGraph(ssjava.getMethodContainingSSJavaLoop());
654 Set<NTuple<Location>> pcLocTupleSet = getPCLocTupleSet(min);
656 MethodDescriptor mdCallee = min.getMethod();
658 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
659 for (int idx = 0; idx < calleeFlowGraph.getNumParameters(); idx++) {
660 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
661 NTuple<Location> globalParamLocTuple =
662 translateToLocTuple(mdCallee, paramNode.getDescTuple());
663 translateToLocTuple(mdCallee, paramNode.getDescTuple());
664 CompositeLocation compLoc = paramNode.getCompositeLocation();
665 System.out.println("---paramNode=" + paramNode + " compLoc=" + compLoc);
666 if (compLoc != null) {
667 NTuple<Descriptor> argTuple = getNodeTupleByArgIdx(min, idx);
668 NTuple<Location> globalArgLocTuple = translateToLocTuple(mdCaller, argTuple);
670 if (!isLiteralValueLocTuple(globalArgLocTuple)
671 && !isLiteralValueLocTuple(globalParamLocTuple)) {
672 if (!globalGraph.hasValueFlowEdge(globalArgLocTuple, globalParamLocTuple)) {
673 System.out.println("----- add global flow globalArgLocTuple=" + globalArgLocTuple
674 + "-> globalParamLocTuple=" + globalParamLocTuple);
676 globalGraph.addValueFlowEdge(globalArgLocTuple, globalParamLocTuple);
680 for (Iterator iterator = pcLocTupleSet.iterator(); iterator.hasNext();) {
681 NTuple<Location> pcLocTuple = (NTuple<Location>) iterator.next();
683 if (!isLiteralValueLocTuple(pcLocTuple) && !isLiteralValueLocTuple(globalParamLocTuple)) {
684 if (!globalGraph.hasValueFlowEdge(pcLocTuple, globalParamLocTuple)) {
686 .println("----- add global flow PCLOC="
688 + "-> globalParamLocTu!globalArgLocTuple.get(0).getLocDescriptor().equals(LITERALDESC)ple="
689 + globalParamLocTuple);
691 globalGraph.addValueFlowEdge(pcLocTuple, globalParamLocTuple);
700 private boolean isLiteralValueLocTuple(NTuple<Location> locTuple) {
701 return locTuple.get(0).getLocDescriptor().equals(LITERALDESC);
704 public void assignCompositeLocationToFlowGraph(FlowGraph flowGraph, Location loc,
705 CompositeLocation inferCompLoc) {
706 Descriptor localDesc = loc.getLocDescriptor();
708 Set<FlowNode> nodeSet = flowGraph.getNodeSet();
709 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
710 FlowNode node = (FlowNode) iterator.next();
711 if (node.getDescTuple().startsWith(localDesc)
712 && !node.getDescTuple().get(0).equals(LITERALDESC)) {
713 // need to assign the inferred composite location to this node
714 CompositeLocation newCompLoc = generateCompositeLocation(node.getDescTuple(), inferCompLoc);
715 node.setCompositeLocation(newCompLoc);
716 System.out.println("SET Node=" + node + " inferCompLoc=" + newCompLoc);
721 private CompositeLocation generateCompositeLocation(NTuple<Descriptor> nodeDescTuple,
722 CompositeLocation inferCompLoc) {
724 System.out.println("generateCompositeLocation=" + nodeDescTuple + " with inferCompLoc="
727 CompositeLocation newCompLoc = new CompositeLocation();
728 for (int i = 0; i < inferCompLoc.getSize(); i++) {
729 newCompLoc.addLocation(inferCompLoc.get(i));
732 Descriptor lastDescOfPrefix = nodeDescTuple.get(0);
733 Descriptor enclosingDescriptor;
734 if (lastDescOfPrefix instanceof InterDescriptor) {
735 enclosingDescriptor = null;
737 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
740 for (int i = 1; i < nodeDescTuple.size(); i++) {
741 Descriptor desc = nodeDescTuple.get(i);
742 Location locElement = new Location(enclosingDescriptor, desc);
743 newCompLoc.addLocation(locElement);
745 enclosingDescriptor = ((FieldDescriptor) desc).getClassDescriptor();
751 private void translateMapLocationToInferCompositeLocationToCalleeGraph(
752 GlobalFlowGraph callerGraph, MethodInvokeNode min) {
754 MethodDescriptor mdCallee = min.getMethod();
755 MethodDescriptor mdCaller = callerGraph.getMethodDescriptor();
756 Map<Location, CompositeLocation> callerMapLocToCompLoc =
757 callerGraph.getMapLocationToInferCompositeLocation();
759 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
761 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
762 GlobalFlowGraph calleeGlobalGraph = getSubGlobalFlowGraph(mdCallee);
764 NTuple<Location> baseLocTuple = null;
765 if (mapMethodInvokeNodeToBaseTuple.containsKey(min)) {
766 baseLocTuple = translateToLocTuple(mdCaller, mapMethodInvokeNodeToBaseTuple.get(min));
769 System.out.println("\n-#translate caller=" + mdCaller + " infer composite loc to callee="
770 + mdCallee + " baseLocTuple=" + baseLocTuple);
771 // System.out.println("-mapIdxToArgTuple=" + mapIdxToArgTuple);
772 System.out.println("-callerMapLocToCompLoc=" + callerMapLocToCompLoc);
774 Set<Location> keySet = callerMapLocToCompLoc.keySet();
775 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
776 Location key = (Location) iterator.next();
777 CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(key);
779 if (!key.getDescriptor().equals(mdCaller)) {
781 CompositeLocation newCalleeCompLoc;
782 if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
783 System.out.println("-----need to translate callerCompLoc=" + callerCompLoc
784 + " with baseTuple=" + baseLocTuple);
786 translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
788 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
789 System.out.println("---key=" + key + " callerCompLoc=" + callerCompLoc
790 + " newCalleeCompLoc=" + newCalleeCompLoc);
791 System.out.println("-----baseLoctuple=" + baseLocTuple);
792 System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
794 // check if it is the global access
795 Location compLocFirstElement = callerCompLoc.getTuple().get(0);
796 if (compLocFirstElement.getDescriptor().equals(mdCallee)
797 && compLocFirstElement.getLocDescriptor().equals(GLOBALDESC)) {
799 newCalleeCompLoc = new CompositeLocation();
800 Location newMethodLoc = new Location(mdCallee, GLOBALDESC);
802 newCalleeCompLoc.addLocation(newMethodLoc);
803 for (int i = 1; i < callerCompLoc.getSize(); i++) {
804 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
806 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
807 System.out.println("---key=" + key + " callerCompLoc=" + callerCompLoc
808 + " newCalleeCompLoc=" + newCalleeCompLoc);
809 System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
812 int paramIdx = getParamIdx(callerCompLoc, mapIdxToArgTuple);
813 if (paramIdx == -1) {
816 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(paramIdx);
818 FlowNode paramFlowNode = calleeFlowGraph.getParamFlowNode(paramIdx);
819 System.out.println("-----paramIdx=" + paramIdx + " paramFlowNode=" + paramFlowNode);
820 NTuple<Location> paramLocTuple =
821 translateToLocTuple(mdCallee, paramFlowNode.getDescTuple());
822 newCalleeCompLoc = new CompositeLocation();
823 for (int i = 0; i < paramLocTuple.size(); i++) {
824 newCalleeCompLoc.addLocation(paramLocTuple.get(i));
826 for (int i = argTuple.size(); i < callerCompLoc.getSize(); i++) {
827 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
829 calleeGlobalGraph.addMapLocationToInferCompositeLocation(key, newCalleeCompLoc);
830 System.out.println("---key=" + key + " callerCompLoc=" + callerCompLoc
831 + " newCalleeCompLoc=" + newCalleeCompLoc);
832 System.out.println("------argTuple=" + argTuple);
833 System.out.println("-----caller=" + mdCaller + " callee=" + mdCallee);
842 // System.out.println("-----*AFTER TRANSLATING COMP LOC MAPPING, CALLEE MAPPING="
843 // + calleeGlobalGraph.getMapLocationToInferCompositeLocation());
845 // If the location of an argument has a composite location
846 // need to assign a proper composite location to the corresponding callee parameter
847 Set<Integer> idxSet = mapIdxToArgTuple.keySet();
848 for (Iterator iterator = idxSet.iterator(); iterator.hasNext();) {
849 Integer idx = (Integer) iterator.next();
851 if (idx == 0 && !min.getMethod().isStatic()) {
855 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(idx);
856 if (argTuple.size() > 0) {
857 // check if an arg tuple has been already assigned to a composite location
858 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argTuple);
859 Location argLocalLoc = argLocTuple.get(0);
861 // if (!isPrimitiveType(argTuple)) {
862 if (callerMapLocToCompLoc.containsKey(argLocalLoc)) {
864 CompositeLocation callerCompLoc = callerMapLocToCompLoc.get(argLocalLoc);
865 for (int i = 1; i < argLocTuple.size(); i++) {
866 callerCompLoc.addLocation(argLocTuple.get(i));
869 if (baseLocTuple != null && callerCompLoc.getTuple().startsWith(baseLocTuple)) {
871 FlowNode calleeParamFlowNode = calleeFlowGraph.getParamFlowNode(idx);
872 NTuple<Descriptor> calleeParamDescTuple = calleeParamFlowNode.getDescTuple();
873 NTuple<Location> calleeParamLocTuple =
874 translateToLocTuple(mdCallee, calleeParamDescTuple);
876 // System.out.println("---need to translate callerCompLoc=" + callerCompLoc
877 // + " with baseTuple=" + baseLocTuple + " calleeParamLocTuple="
878 // + calleeParamLocTuple);
880 CompositeLocation newCalleeCompLoc =
881 translateCompositeLocationToCallee(callerCompLoc, baseLocTuple, mdCallee);
883 calleeGlobalGraph.addMapLocationToInferCompositeLocation(calleeParamLocTuple.get(0),
886 // System.out.println("---callee loc=" + calleeParamLocTuple.get(0)
887 // + " newCalleeCompLoc=" + newCalleeCompLoc);
898 private int getParamIdx(CompositeLocation compLoc,
899 Map<Integer, NTuple<Descriptor>> mapIdxToArgTuple) {
901 // if the composite location is started with the argument descriptor
902 // return the argument's index. o.t. return -1
904 Set<Integer> keySet = mapIdxToArgTuple.keySet();
905 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
906 Integer key = (Integer) iterator.next();
907 NTuple<Descriptor> argTuple = mapIdxToArgTuple.get(key);
908 if (argTuple.size() > 0 && translateToDescTuple(compLoc.getTuple()).startsWith(argTuple)) {
909 System.out.println("compLoc.getTuple=" + compLoc + " is started with " + argTuple);
910 return key.intValue();
917 private boolean isPrimitiveType(NTuple<Descriptor> argTuple) {
919 Descriptor lastDesc = argTuple.get(argTuple.size() - 1);
921 if (lastDesc instanceof FieldDescriptor) {
922 return ((FieldDescriptor) lastDesc).getType().isPrimitive();
923 } else if (lastDesc instanceof VarDescriptor) {
924 return ((VarDescriptor) lastDesc).getType().isPrimitive();
925 } else if (lastDesc instanceof InterDescriptor) {
932 private CompositeLocation translateCompositeLocationToCallee(CompositeLocation callerCompLoc,
933 NTuple<Location> baseLocTuple, MethodDescriptor mdCallee) {
935 CompositeLocation newCalleeCompLoc = new CompositeLocation();
937 Location calleeThisLoc = new Location(mdCallee, mdCallee.getThis());
938 newCalleeCompLoc.addLocation(calleeThisLoc);
940 // remove the base tuple from the caller
941 // ex; In the method invoation foo.bar.methodA(), the callee will have the composite location
942 // ,which is relative to the 'this' variable, <THIS,...>
943 for (int i = baseLocTuple.size(); i < callerCompLoc.getSize(); i++) {
944 newCalleeCompLoc.addLocation(callerCompLoc.get(i));
947 return newCalleeCompLoc;
951 private void calculateGlobalValueFlowCompositeLocation() {
953 System.out.println("SSJAVA: Calculate composite locations in the global value flow graph");
954 MethodDescriptor methodDescEventLoop = ssjava.getMethodContainingSSJavaLoop();
955 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(methodDescEventLoop);
957 Set<Location> calculatedPrefixSet = new HashSet<Location>();
959 Set<GlobalFlowNode> nodeSet = globalFlowGraph.getNodeSet();
961 next: for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
962 GlobalFlowNode node = (GlobalFlowNode) iterator.next();
964 Location prefixLoc = node.getLocTuple().get(0);
966 if (calculatedPrefixSet.contains(prefixLoc)) {
967 // the prefix loc has been already assigned to a composite location
971 calculatedPrefixSet.add(prefixLoc);
973 // Set<GlobalFlowNode> incomingNodeSet = globalFlowGraph.getIncomingNodeSet(node);
974 List<NTuple<Location>> prefixList = calculatePrefixList(globalFlowGraph, node);
976 Set<GlobalFlowNode> reachableNodeSet =
977 globalFlowGraph.getReachableNodeSetByPrefix(node.getLocTuple().get(0));
978 // Set<GlobalFlowNode> reachNodeSet = globalFlowGraph.getReachableNodeSetFrom(node);
980 // System.out.println("node=" + node + " prefixList=" + prefixList);
982 for (int i = 0; i < prefixList.size(); i++) {
983 NTuple<Location> curPrefix = prefixList.get(i);
984 Set<NTuple<Location>> reachableCommonPrefixSet = new HashSet<NTuple<Location>>();
986 for (Iterator iterator2 = reachableNodeSet.iterator(); iterator2.hasNext();) {
987 GlobalFlowNode reachNode = (GlobalFlowNode) iterator2.next();
988 if (reachNode.getLocTuple().startsWith(curPrefix)) {
989 reachableCommonPrefixSet.add(reachNode.getLocTuple());
992 // System.out.println("reachableCommonPrefixSet=" + reachableCommonPrefixSet);
994 if (!reachableCommonPrefixSet.isEmpty()) {
996 MethodDescriptor curPrefixFirstElementMethodDesc =
997 (MethodDescriptor) curPrefix.get(0).getDescriptor();
999 MethodDescriptor nodePrefixLocFirstElementMethodDesc =
1000 (MethodDescriptor) prefixLoc.getDescriptor();
1002 // System.out.println("curPrefixFirstElementMethodDesc=" +
1003 // curPrefixFirstElementMethodDesc);
1004 // System.out.println("nodePrefixLocFirstElementMethodDesc="
1005 // + nodePrefixLocFirstElementMethodDesc);
1007 if (curPrefixFirstElementMethodDesc.equals(nodePrefixLocFirstElementMethodDesc)
1008 || isTransitivelyCalledFrom(nodePrefixLocFirstElementMethodDesc,
1009 curPrefixFirstElementMethodDesc)) {
1012 // if (!node.getLocTuple().startsWith(curPrefix.get(0))) {
1014 Location curPrefixLocalLoc = curPrefix.get(0);
1015 if (globalFlowGraph.mapLocationToInferCompositeLocation.containsKey(curPrefixLocalLoc)) {
1016 // in this case, the local variable of the current prefix has already got a composite
1018 // so we just ignore the current composite location.
1020 // System.out.println("HERE WE DO NOT ASSIGN A COMPOSITE LOCATION TO =" + node
1021 // + " DUE TO " + curPrefix);
1026 Location targetLocalLoc = node.getLocTuple().get(0);
1027 // CompositeLocation curCompLoc = globalFlowGraph.getCompositeLocation(targetLocalLoc);
1028 // if ((curPrefix.size() + 1) > curCompLoc.getSize()) {
1030 CompositeLocation newCompLoc = generateCompositeLocation(curPrefix);
1031 System.out.println("NEED TO ASSIGN COMP LOC TO " + node + " with prefix=" + curPrefix);
1032 System.out.println("-targetLocalLoc=" + targetLocalLoc + " - newCompLoc="
1034 globalFlowGraph.addMapLocationToInferCompositeLocation(targetLocalLoc, newCompLoc);
1047 // Set<GlobalFlowNode> inNodeSet =
1048 // graph.getIncomingNodeSetWithPrefix(prefix);
1049 // System.out.println("inNodeSet=" + inNodeSet + " from=" + node);
1052 private void assignCompositeLocation(CompositeLocation compLocPrefix, GlobalFlowNode node) {
1053 CompositeLocation newCompLoc = compLocPrefix.clone();
1054 NTuple<Location> locTuple = node.getLocTuple();
1055 for (int i = 1; i < locTuple.size(); i++) {
1056 newCompLoc.addLocation(locTuple.get(i));
1058 node.setInferCompositeLocation(newCompLoc);
1061 private List<NTuple<Location>> calculatePrefixList(GlobalFlowGraph graph, GlobalFlowNode node) {
1063 System.out.println("\n##### calculatePrefixList node=" + node);
1065 Set<GlobalFlowNode> incomingNodeSetPrefix =
1066 graph.getIncomingNodeSetByPrefix(node.getLocTuple().get(0));
1067 System.out.println("incomingNodeSetPrefix=" + incomingNodeSetPrefix);
1069 Set<GlobalFlowNode> reachableNodeSetPrefix =
1070 graph.getReachableNodeSetByPrefix(node.getLocTuple().get(0));
1071 System.out.println("reachableNodeSetPrefix=" + reachableNodeSetPrefix);
1073 List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
1075 for (Iterator iterator = incomingNodeSetPrefix.iterator(); iterator.hasNext();) {
1076 GlobalFlowNode inNode = (GlobalFlowNode) iterator.next();
1077 NTuple<Location> inNodeTuple = inNode.getLocTuple();
1078 for (int i = 1; i < inNodeTuple.size(); i++) {
1079 NTuple<Location> prefix = inNodeTuple.subList(0, i);
1080 if (!prefixList.contains(prefix)) {
1081 prefixList.add(prefix);
1086 Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
1087 public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
1088 int s0 = arg0.size();
1089 int s1 = arg1.size();
1092 } else if (s0 == s1) {
1100 // remove a prefix which is not suitable for generating composite location
1101 Location localVarLoc = node.getLocTuple().get(0);
1102 MethodDescriptor md = (MethodDescriptor) localVarLoc.getDescriptor();
1103 ClassDescriptor cd = md.getClassDesc();
1106 Set<NTuple<Location>> toberemoved = new HashSet<NTuple<Location>>();
1107 // for (int i = 0; i < prefixList.size(); i++) {
1108 // NTuple<Location> prefixLocTuple = prefixList.get(i);
1109 // if (!containsClassDesc(cd, prefixLocTuple)) {
1110 // toberemoved.add(prefixLocTuple);
1114 // System.out.println("method class=" + cd + " toberemoved=" + toberemoved);
1116 prefixList.removeAll(toberemoved);
1120 // List<NTuple<Location>> prefixList = new ArrayList<NTuple<Location>>();
1122 // for (Iterator iterator = incomingNodeSet.iterator(); iterator.hasNext();) {
1123 // GlobalFlowNode inNode = (GlobalFlowNode) iterator.next();
1124 // NTuple<Location> inNodeTuple = inNode.getLocTuple();
1126 // for (int i = 1; i < inNodeTuple.size(); i++) {
1127 // NTuple<Location> prefix = inNodeTuple.subList(0, i);
1128 // if (!prefixList.contains(prefix)) {
1129 // prefixList.add(prefix);
1134 // Collections.sort(prefixList, new Comparator<NTuple<Location>>() {
1135 // public int compare(NTuple<Location> arg0, NTuple<Location> arg1) {
1136 // int s0 = arg0.size();
1137 // int s1 = arg1.size();
1140 // } else if (s0 == s1) {
1147 // return prefixList;
1150 private boolean containsClassDesc(ClassDescriptor cd, NTuple<Location> prefixLocTuple) {
1151 for (int i = 0; i < prefixLocTuple.size(); i++) {
1152 Location loc = prefixLocTuple.get(i);
1153 Descriptor locDesc = loc.getLocDescriptor();
1154 if (locDesc != null) {
1155 ClassDescriptor type = getClassTypeDescriptor(locDesc);
1156 if (type != null && type.equals(cd)) {
1164 private GlobalFlowGraph constructSubGlobalFlowGraph(FlowGraph flowGraph) {
1166 MethodDescriptor md = flowGraph.getMethodDescriptor();
1168 GlobalFlowGraph globalGraph = getSubGlobalFlowGraph(md);
1170 // Set<FlowNode> nodeSet = flowGraph.getNodeSet();
1171 Set<FlowEdge> edgeSet = flowGraph.getEdgeSet();
1173 for (Iterator iterator = edgeSet.iterator(); iterator.hasNext();) {
1175 FlowEdge edge = (FlowEdge) iterator.next();
1176 NTuple<Descriptor> srcDescTuple = edge.getInitTuple();
1177 NTuple<Descriptor> dstDescTuple = edge.getEndTuple();
1179 // here only keep the first element(method location) of the descriptor
1181 NTuple<Location> srcLocTuple = translateToLocTuple(md, srcDescTuple);
1182 // Location srcMethodLoc = srcLocTuple.get(0);
1183 // Descriptor srcVarDesc = srcMethodLoc.getLocDescriptor();
1184 // // if (flowGraph.isParamDesc(srcVarDesc) &&
1185 // (!srcVarDesc.equals(md.getThis()))) {
1186 // if (!srcVarDesc.equals(md.getThis())) {
1187 // srcLocTuple = new NTuple<Location>();
1188 // Location loc = new Location(md, srcVarDesc);
1189 // srcLocTuple.add(loc);
1192 NTuple<Location> dstLocTuple = translateToLocTuple(md, dstDescTuple);
1193 // Location dstMethodLoc = dstLocTuple.get(0);
1194 // Descriptor dstVarDesc = dstMethodLoc.getLocDescriptor();
1195 // if (!dstVarDesc.equals(md.getThis())) {
1196 // dstLocTuple = new NTuple<Location>();
1197 // Location loc = new Location(md, dstVarDesc);
1198 // dstLocTuple.add(loc);
1201 globalGraph.addValueFlowEdge(srcLocTuple, dstLocTuple);
1208 private NTuple<Location> translateToLocTuple(MethodDescriptor md, NTuple<Descriptor> descTuple) {
1210 NTuple<Location> locTuple = new NTuple<Location>();
1212 Descriptor enclosingDesc = md;
1213 // System.out.println("md=" + md + " descTuple=" + descTuple);
1214 for (int i = 0; i < descTuple.size(); i++) {
1215 Descriptor desc = descTuple.get(i);
1217 Location loc = new Location(enclosingDesc, desc);
1220 if (desc instanceof VarDescriptor) {
1221 enclosingDesc = ((VarDescriptor) desc).getType().getClassDesc();
1222 } else if (desc instanceof FieldDescriptor) {
1223 enclosingDesc = ((FieldDescriptor) desc).getType().getClassDesc();
1225 // TODO: inter descriptor case
1226 enclosingDesc = desc;
1235 private void addValueFlowsFromCalleeSubGlobalFlowGraph(MethodDescriptor mdCaller,
1236 GlobalFlowGraph subGlobalFlowGraph) {
1238 // the transformation for a call site propagates flows through parameters
1239 // if the method is virtual, it also grab all relations from any possible
1242 Set<MethodInvokeNode> setMethodInvokeNode = getMethodInvokeNodeSet(mdCaller);
1244 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
1245 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
1246 MethodDescriptor mdCallee = min.getMethod();
1247 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
1248 if (mdCallee.isStatic()) {
1249 setPossibleCallees.add(mdCallee);
1251 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
1252 // removes method descriptors that are not invoked by the caller
1253 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
1254 setPossibleCallees.addAll(calleeSet);
1257 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
1258 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
1259 propagateValueFlowsToCallerFromSubGlobalFlowGraph(min, mdCaller, possibleMdCallee);
1266 private void propagateValueFlowsToCallerFromSubGlobalFlowGraph(MethodInvokeNode min,
1267 MethodDescriptor mdCaller, MethodDescriptor possibleMdCallee) {
1269 System.out.println("---propagate from " + min.printNode(0) + " to caller=" + mdCaller);
1270 FlowGraph calleeFlowGraph = getFlowGraph(possibleMdCallee);
1271 Map<Integer, NTuple<Descriptor>> mapIdxToArg = mapMethodInvokeNodeToArgIdxMap.get(min);
1273 // System.out.println("-----mapMethodInvokeNodeToArgIdxMap.get(min)="
1274 // + mapMethodInvokeNodeToArgIdxMap.get(min));
1275 Set<Integer> keySet = mapIdxToArg.keySet();
1276 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1277 Integer idx = (Integer) iterator.next();
1278 NTuple<Descriptor> argDescTuple = mapIdxToArg.get(idx);
1279 if (argDescTuple.size() > 0) {
1280 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
1281 NTuple<Descriptor> paramDescTuple = calleeFlowGraph.getParamFlowNode(idx).getDescTuple();
1282 NTuple<Location> paramLocTuple = translateToLocTuple(possibleMdCallee, paramDescTuple);
1283 addMapCallerArgToCalleeParam(min, argDescTuple, paramDescTuple);
1287 addValueFlowBetweenParametersToCaller(min, mdCaller, possibleMdCallee);
1289 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
1290 GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(possibleMdCallee);
1291 Set<GlobalFlowNode> calleeNodeSet = calleeSubGlobalGraph.getNodeSet();
1292 for (Iterator iterator = calleeNodeSet.iterator(); iterator.hasNext();) {
1293 GlobalFlowNode calleeNode = (GlobalFlowNode) iterator.next();
1294 addValueFlowFromCalleeNode(min, mdCaller, possibleMdCallee, calleeNode);
1299 private void addValueFlowBetweenParametersToCaller(MethodInvokeNode min,
1300 MethodDescriptor mdCaller, MethodDescriptor mdCallee) {
1302 System.out.println("***addValueFlowBetweenParametersToCaller from mdCallee=" + mdCallee);
1304 Set<NTuple<Location>> PCLocTupleSet = mapMethodInvokeNodeToPCLocTupleSet.get(min);
1305 System.out.println("-PCLocTupleSet=" + PCLocTupleSet);
1307 GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(mdCallee);
1308 GlobalFlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
1310 // if the parameter A reaches to the parameter B
1311 // then, add an edge the argument A -> the argument B to the global flow graph
1312 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
1313 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
1314 int numParam = calleeFlowGraph.getNumParameters();
1316 for (int i = 0; i < numParam; i++) {
1317 for (int k = 0; k < numParam; k++) {
1321 System.out.println("i=" + i + " k=" + k);
1323 FlowNode paramNode1 = calleeFlowGraph.getParamFlowNode(i);
1324 FlowNode paramNode2 = calleeFlowGraph.getParamFlowNode(k);
1326 NTuple<Descriptor> arg1Tuple = getNodeTupleByArgIdx(min, i);
1327 NTuple<Descriptor> arg2Tuple = getNodeTupleByArgIdx(min, k);
1329 NTuple<Descriptor> paramDescTuple1 = paramNode1.getCurrentDescTuple();
1330 NTuple<Descriptor> paramDescTuple2 = paramNode2.getCurrentDescTuple();
1332 if (paramDescTuple1.get(0).equals(paramDescTuple2.get(0))) {
1333 // if two parameters share the same prefix
1334 // it already has been assigned to a composite location
1335 // so we don't need to add an additional ordering relation caused by these two
1340 NTuple<Location> paramLocTuple1 = translateToLocTuple(mdCallee, paramDescTuple1);
1341 NTuple<Location> paramLocTuple2 = translateToLocTuple(mdCallee, paramDescTuple2);
1343 // check if the callee propagates an ordering constraints through
1346 // Set<FlowNode> localReachSet = calleeFlowGraph.getLocalReachFlowNodeSetFrom(paramNode1);
1348 Set<GlobalFlowNode> reachToParam1Set =
1349 calleeSubGlobalGraph.getIncomingNodeSetByPrefix(paramLocTuple1.get(0));
1351 // System.out.println("-- localReachSet from param1=" + localReachSet);
1353 GlobalFlowNode globalFlowNodeParam1 = calleeSubGlobalGraph.getFlowNode(paramLocTuple1);
1354 GlobalFlowNode globalFlowNodeParam2 = calleeSubGlobalGraph.getFlowNode(paramLocTuple2);
1356 System.out.println("-param1CurTuple=" + paramDescTuple1 + " param2CurTuple="
1359 System.out.println("arg1Tuple=" + arg1Tuple + " arg2Tuple=" + arg2Tuple);
1360 // System.out.println("-reachToParam1Set=" + reachToParam1Set);
1362 if (arg1Tuple.size() > 0 && arg2Tuple.size() > 0
1363 && reachToParam1Set.contains(globalFlowNodeParam2)) {
1364 // need to propagate an ordering relation s.t. arg1 is higher
1366 System.out.println("---param1=" + paramNode1 + " is higher than param2=" + paramNode2);
1368 NTuple<Location> callerSrcNodeLocTuple =
1369 translateToCallerLocTuple(min, mdCallee, mdCaller, paramLocTuple1);
1371 NTuple<Location> callerDstNodeLocTuple =
1372 translateToCallerLocTuple(min, mdCallee, mdCaller, paramLocTuple2);
1374 System.out.println("---callerSrcNodeLocTuple=" + callerSrcNodeLocTuple);
1375 System.out.println("---callerDstNodeLocTuple=" + callerDstNodeLocTuple);
1377 System.out.println("-----add global value flow :" + callerSrcNodeLocTuple + "->"
1378 + callerDstNodeLocTuple);
1379 callerSubGlobalGraph.addValueFlowEdge(callerSrcNodeLocTuple, callerDstNodeLocTuple);
1380 for (Iterator iterator = PCLocTupleSet.iterator(); iterator.hasNext();) {
1381 NTuple<Location> pcLocTuple = (NTuple<Location>) iterator.next();
1382 System.out.println("-----add global value flow PC :" + pcLocTuple + "->"
1383 + callerSrcNodeLocTuple);
1384 callerSubGlobalGraph.addValueFlowEdge(pcLocTuple, callerSrcNodeLocTuple);
1387 // add a new flow between the corresponding arguments.
1388 callerFlowGraph.addValueFlowEdge(arg1Tuple, arg2Tuple);
1389 System.out.println("arg1=" + arg1Tuple + " arg2=" + arg2Tuple);
1392 // .println("-arg1Tuple=" + arg1Tuple + " is higher than arg2Tuple=" + arg2Tuple);
1396 System.out.println();
1403 private void addValueFlowFromCalleeNode(MethodInvokeNode min, MethodDescriptor mdCaller,
1404 MethodDescriptor mdCallee, GlobalFlowNode calleeSrcNode) {
1406 GlobalFlowGraph calleeSubGlobalGraph = getSubGlobalFlowGraph(mdCallee);
1407 GlobalFlowGraph callerSubGlobalGraph = getSubGlobalFlowGraph(mdCaller);
1409 System.out.println("$addValueFlowFromCalleeNode calleeSrcNode=" + calleeSrcNode);
1411 NTuple<Location> callerSrcNodeLocTuple =
1412 translateToCallerLocTuple(min, mdCallee, mdCaller, calleeSrcNode.getLocTuple());
1413 System.out.println("---callerSrcNodeLocTuple=" + callerSrcNodeLocTuple);
1415 if (callerSrcNodeLocTuple != null && callerSrcNodeLocTuple.size() > 0) {
1416 Set<GlobalFlowNode> outNodeSet = calleeSubGlobalGraph.getOutNodeSet(calleeSrcNode);
1418 for (Iterator iterator = outNodeSet.iterator(); iterator.hasNext();) {
1419 GlobalFlowNode outNode = (GlobalFlowNode) iterator.next();
1420 NTuple<Location> callerDstNodeLocTuple =
1421 translateToCallerLocTuple(min, mdCallee, mdCaller, outNode.getLocTuple());
1422 if (callerDstNodeLocTuple != null) {
1423 callerSubGlobalGraph.addValueFlowEdge(callerSrcNodeLocTuple, callerDstNodeLocTuple);
1430 private NTuple<Location> translateToCallerLocTuple(MethodInvokeNode min,
1431 MethodDescriptor mdCallee, MethodDescriptor mdCaller, NTuple<Location> nodeLocTuple) {
1432 // this method will return the same nodeLocTuple if the corresponding argument is literal
1435 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
1437 NTuple<Descriptor> nodeDescTuple = translateToDescTuple(nodeLocTuple);
1438 if (calleeFlowGraph.isParameter(nodeDescTuple)) {
1439 int paramIdx = calleeFlowGraph.getParamIdx(nodeDescTuple);
1440 NTuple<Descriptor> argDescTuple = mapMethodInvokeNodeToArgIdxMap.get(min).get(paramIdx);
1442 // if (isPrimitive(nodeLocTuple.get(0).getLocDescriptor())) {
1443 // // the type of argument is primitive.
1444 // return nodeLocTuple.clone();
1446 NTuple<Location> argLocTuple = translateToLocTuple(mdCaller, argDescTuple);
1448 NTuple<Location> callerLocTuple = new NTuple<Location>();
1450 callerLocTuple.addAll(argLocTuple);
1451 for (int i = 1; i < nodeLocTuple.size(); i++) {
1452 callerLocTuple.add(nodeLocTuple.get(i));
1454 return callerLocTuple;
1456 return nodeLocTuple.clone();
1461 public static boolean isPrimitive(Descriptor desc) {
1463 if (desc instanceof FieldDescriptor) {
1464 return ((FieldDescriptor) desc).getType().isPrimitive();
1465 } else if (desc instanceof VarDescriptor) {
1466 return ((VarDescriptor) desc).getType().isPrimitive();
1467 } else if (desc instanceof InterDescriptor) {
1474 private NTuple<Descriptor> translateToDescTuple(NTuple<Location> locTuple) {
1476 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
1477 for (int i = 0; i < locTuple.size(); i++) {
1478 descTuple.add(locTuple.get(i).getLocDescriptor());
1484 public LocationSummary getLocationSummary(Descriptor d) {
1485 if (!mapDescToLocationSummary.containsKey(d)) {
1486 if (d instanceof MethodDescriptor) {
1487 mapDescToLocationSummary.put(d, new MethodSummary((MethodDescriptor) d));
1488 } else if (d instanceof ClassDescriptor) {
1489 mapDescToLocationSummary.put(d, new FieldSummary());
1492 return mapDescToLocationSummary.get(d);
1495 private void generateMethodSummary() {
1497 Set<MethodDescriptor> keySet = md2lattice.keySet();
1498 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1499 MethodDescriptor md = (MethodDescriptor) iterator.next();
1501 System.out.println("\nSSJAVA: generate method summary: " + md);
1503 FlowGraph flowGraph = getFlowGraph(md);
1504 MethodSummary methodSummary = getMethodSummary(md);
1506 HierarchyGraph scGraph = getSkeletonCombinationHierarchyGraph(md);
1508 // set the 'this' reference location
1509 if (!md.isStatic()) {
1510 System.out.println("setThisLocName=" + scGraph.getHNode(md.getThis()).getName());
1511 methodSummary.setThisLocName(scGraph.getHNode(md.getThis()).getName());
1514 // set the 'global' reference location if needed
1515 if (methodSummary.hasGlobalAccess()) {
1516 methodSummary.setGlobalLocName(scGraph.getHNode(GLOBALDESC).getName());
1519 // construct a parameter mapping that maps a parameter descriptor to an
1520 // inferred composite location
1521 for (int paramIdx = 0; paramIdx < flowGraph.getNumParameters(); paramIdx++) {
1522 FlowNode flowNode = flowGraph.getParamFlowNode(paramIdx);
1523 CompositeLocation inferredCompLoc =
1524 updateCompositeLocation(flowNode.getCompositeLocation());
1525 // NTuple<Descriptor> descTuple = flowNode.getDescTuple();
1527 // CompositeLocation assignedCompLoc = flowNode.getCompositeLocation();
1528 // CompositeLocation inferredCompLoc;
1529 // if (assignedCompLoc != null) {
1530 // inferredCompLoc = translateCompositeLocation(assignedCompLoc);
1532 // Descriptor locDesc = descTuple.get(0);
1533 // Location loc = new Location(md, locDesc.getSymbol());
1534 // loc.setLocDescriptor(locDesc);
1535 // inferredCompLoc = new CompositeLocation(loc);
1537 System.out.println("-paramIdx=" + paramIdx + " infer=" + inferredCompLoc + " original="
1538 + flowNode.getCompositeLocation());
1540 Descriptor localVarDesc = flowNode.getDescTuple().get(0);
1541 methodSummary.addMapVarNameToInferCompLoc(localVarDesc, inferredCompLoc);
1542 methodSummary.addMapParamIdxToInferLoc(paramIdx, inferredCompLoc);
1549 private boolean hasOrderingRelation(NTuple<Location> locTuple1, NTuple<Location> locTuple2) {
1551 int size = locTuple1.size() >= locTuple2.size() ? locTuple2.size() : locTuple1.size();
1553 for (int idx = 0; idx < size; idx++) {
1554 Location loc1 = locTuple1.get(idx);
1555 Location loc2 = locTuple2.get(idx);
1557 Descriptor desc1 = loc1.getDescriptor();
1558 Descriptor desc2 = loc2.getDescriptor();
1560 if (!desc1.equals(desc2)) {
1561 throw new Error("Fail to compare " + locTuple1 + " and " + locTuple2);
1564 Descriptor locDesc1 = loc1.getLocDescriptor();
1565 Descriptor locDesc2 = loc2.getLocDescriptor();
1567 HierarchyGraph hierarchyGraph = getHierarchyGraph(desc1);
1569 HNode node1 = hierarchyGraph.getHNode(locDesc1);
1570 HNode node2 = hierarchyGraph.getHNode(locDesc2);
1572 System.out.println("---node1=" + node1 + " node2=" + node2);
1573 System.out.println("---hierarchyGraph.getIncomingNodeSet(node2)="
1574 + hierarchyGraph.getIncomingNodeSet(node2));
1576 if (locDesc1.equals(locDesc2)) {
1578 } else if (!hierarchyGraph.getIncomingNodeSet(node2).contains(node1)
1579 && !hierarchyGraph.getIncomingNodeSet(node1).contains(node2)) {
1591 private boolean isHigherThan(NTuple<Location> locTuple1, NTuple<Location> locTuple2) {
1593 int size = locTuple1.size() >= locTuple2.size() ? locTuple2.size() : locTuple1.size();
1595 for (int idx = 0; idx < size; idx++) {
1596 Location loc1 = locTuple1.get(idx);
1597 Location loc2 = locTuple2.get(idx);
1599 Descriptor desc1 = loc1.getDescriptor();
1600 Descriptor desc2 = loc2.getDescriptor();
1602 if (!desc1.equals(desc2)) {
1603 throw new Error("Fail to compare " + locTuple1 + " and " + locTuple2);
1606 Descriptor locDesc1 = loc1.getLocDescriptor();
1607 Descriptor locDesc2 = loc2.getLocDescriptor();
1609 HierarchyGraph hierarchyGraph = getHierarchyGraph(desc1);
1611 HNode node1 = hierarchyGraph.getHNode(locDesc1);
1612 HNode node2 = hierarchyGraph.getHNode(locDesc2);
1614 System.out.println("---node1=" + node1 + " node2=" + node2);
1615 System.out.println("---hierarchyGraph.getIncomingNodeSet(node2)="
1616 + hierarchyGraph.getIncomingNodeSet(node2));
1618 if (locDesc1.equals(locDesc2)) {
1620 } else if (hierarchyGraph.getIncomingNodeSet(node2).contains(node1)) {
1631 private CompositeLocation translateCompositeLocation(CompositeLocation compLoc) {
1632 CompositeLocation newCompLoc = new CompositeLocation();
1634 // System.out.println("compLoc=" + compLoc);
1635 for (int i = 0; i < compLoc.getSize(); i++) {
1636 Location loc = compLoc.get(i);
1637 Descriptor enclosingDescriptor = loc.getDescriptor();
1638 Descriptor locDescriptor = loc.getLocDescriptor();
1640 HNode hnode = getHierarchyGraph(enclosingDescriptor).getHNode(locDescriptor);
1641 // System.out.println("-hnode=" + hnode + " from=" + locDescriptor +
1642 // " enclosingDescriptor="
1643 // + enclosingDescriptor);
1644 // System.out.println("-getLocationSummary(enclosingDescriptor)="
1645 // + getLocationSummary(enclosingDescriptor));
1646 String locName = getLocationSummary(enclosingDescriptor).getLocationName(hnode.getName());
1647 // System.out.println("-locName=" + locName);
1648 Location newLoc = new Location(enclosingDescriptor, locName);
1649 newLoc.setLocDescriptor(locDescriptor);
1650 newCompLoc.addLocation(newLoc);
1656 private void debug_writeLattices() {
1658 Set<Descriptor> keySet = mapDescriptorToSimpleLattice.keySet();
1659 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1660 Descriptor key = (Descriptor) iterator.next();
1661 SSJavaLattice<String> simpleLattice = mapDescriptorToSimpleLattice.get(key);
1662 // HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(key);
1663 HierarchyGraph scHierarchyGraph = getSkeletonCombinationHierarchyGraph(key);
1664 if (key instanceof ClassDescriptor) {
1665 writeInferredLatticeDotFile((ClassDescriptor) key, scHierarchyGraph, simpleLattice,
1667 } else if (key instanceof MethodDescriptor) {
1668 MethodDescriptor md = (MethodDescriptor) key;
1669 writeInferredLatticeDotFile(md.getClassDesc(), md, scHierarchyGraph, simpleLattice,
1673 LocationSummary ls = getLocationSummary(key);
1674 System.out.println("####LOC SUMMARY=" + key + "\n" + ls.getMapHNodeNameToLocationName());
1677 Set<ClassDescriptor> cdKeySet = cd2lattice.keySet();
1678 for (Iterator iterator = cdKeySet.iterator(); iterator.hasNext();) {
1679 ClassDescriptor cd = (ClassDescriptor) iterator.next();
1680 writeInferredLatticeDotFile((ClassDescriptor) cd, getSkeletonCombinationHierarchyGraph(cd),
1681 cd2lattice.get(cd), "");
1684 Set<MethodDescriptor> mdKeySet = md2lattice.keySet();
1685 for (Iterator iterator = mdKeySet.iterator(); iterator.hasNext();) {
1686 MethodDescriptor md = (MethodDescriptor) iterator.next();
1687 writeInferredLatticeDotFile(md.getClassDesc(), md, getSkeletonCombinationHierarchyGraph(md),
1688 md2lattice.get(md), "");
1693 private void buildLattice() {
1695 BuildLattice buildLattice = new BuildLattice(this);
1697 Set<Descriptor> keySet = mapDescriptorToCombineSkeletonHierarchyGraph.keySet();
1698 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1699 Descriptor desc = (Descriptor) iterator.next();
1701 SSJavaLattice<String> simpleLattice = buildLattice.buildLattice(desc);
1703 addMapDescToSimpleLattice(desc, simpleLattice);
1705 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
1706 System.out.println("\n## insertIntermediateNodesToStraightLine:"
1707 + simpleHierarchyGraph.getName());
1708 SSJavaLattice<String> lattice =
1709 buildLattice.insertIntermediateNodesToStraightLine(desc, simpleLattice);
1710 lattice.removeRedundantEdges();
1712 if (desc instanceof ClassDescriptor) {
1714 cd2lattice.put((ClassDescriptor) desc, lattice);
1715 // ssjava.writeLatticeDotFile((ClassDescriptor) desc, null, lattice);
1716 } else if (desc instanceof MethodDescriptor) {
1718 md2lattice.put((MethodDescriptor) desc, lattice);
1719 MethodDescriptor md = (MethodDescriptor) desc;
1720 ClassDescriptor cd = md.getClassDesc();
1721 // ssjava.writeLatticeDotFile(cd, md, lattice);
1724 // System.out.println("\nSSJAVA: Insering Combination Nodes:" + desc);
1725 // HierarchyGraph skeletonGraph = getSkeletonHierarchyGraph(desc);
1726 // HierarchyGraph skeletonGraphWithCombinationNode =
1727 // skeletonGraph.clone();
1728 // skeletonGraphWithCombinationNode.setName(desc + "_SC");
1730 // HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
1731 // System.out.println("Identifying Combination Nodes:");
1732 // skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph);
1733 // skeletonGraphWithCombinationNode.simplifySkeletonCombinationHierarchyGraph();
1734 // mapDescriptorToCombineSkeletonHierarchyGraph.put(desc,
1735 // skeletonGraphWithCombinationNode);
1740 public void addMapDescToSimpleLattice(Descriptor desc, SSJavaLattice<String> lattice) {
1741 mapDescriptorToSimpleLattice.put(desc, lattice);
1744 public SSJavaLattice<String> getSimpleLattice(Descriptor desc) {
1745 return mapDescriptorToSimpleLattice.get(desc);
1748 private void simplifyHierarchyGraph() {
1749 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
1750 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1751 Descriptor desc = (Descriptor) iterator.next();
1752 // System.out.println("SSJAVA: remove redundant edges: " + desc);
1753 HierarchyGraph simpleHierarchyGraph = getHierarchyGraph(desc).clone();
1754 simpleHierarchyGraph.setName(desc + "_SIMPLE");
1755 simpleHierarchyGraph.removeRedundantEdges();
1756 mapDescriptorToSimpleHierarchyGraph.put(desc, simpleHierarchyGraph);
1760 private void insertCombinationNodes() {
1761 Set<Descriptor> keySet = mapDescriptorToSkeletonHierarchyGraph.keySet();
1762 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1763 Descriptor desc = (Descriptor) iterator.next();
1764 System.out.println("\nSSJAVA: Insering Combination Nodes:" + desc);
1765 HierarchyGraph skeletonGraph = getSkeletonHierarchyGraph(desc);
1766 HierarchyGraph skeletonGraphWithCombinationNode = skeletonGraph.clone();
1767 skeletonGraphWithCombinationNode.setName(desc + "_SC");
1769 HierarchyGraph simpleHierarchyGraph = getSimpleHierarchyGraph(desc);
1770 System.out.println("Identifying Combination Nodes:");
1771 skeletonGraphWithCombinationNode.insertCombinationNodesToGraph(simpleHierarchyGraph);
1772 skeletonGraphWithCombinationNode.simplifySkeletonCombinationHierarchyGraph();
1773 mapDescriptorToCombineSkeletonHierarchyGraph.put(desc, skeletonGraphWithCombinationNode);
1777 private void constructSkeletonHierarchyGraph() {
1778 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
1779 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1780 Descriptor desc = (Descriptor) iterator.next();
1781 System.out.println("SSJAVA: Constructing Skeleton Hierarchy Graph: " + desc);
1782 HierarchyGraph simpleGraph = getSimpleHierarchyGraph(desc);
1783 HierarchyGraph skeletonGraph = simpleGraph.generateSkeletonGraph();
1784 skeletonGraph.setMapDescToHNode(simpleGraph.getMapDescToHNode());
1785 skeletonGraph.setMapHNodeToDescSet(simpleGraph.getMapHNodeToDescSet());
1786 skeletonGraph.simplifyHierarchyGraph();
1787 // skeletonGraph.combineRedundantNodes(false);
1788 // skeletonGraph.removeRedundantEdges();
1789 mapDescriptorToSkeletonHierarchyGraph.put(desc, skeletonGraph);
1793 private void debug_writeHierarchyDotFiles() {
1795 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
1796 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1797 Descriptor desc = (Descriptor) iterator.next();
1798 getHierarchyGraph(desc).writeGraph();
1803 private void debug_writeSimpleHierarchyDotFiles() {
1805 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
1806 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1807 Descriptor desc = (Descriptor) iterator.next();
1808 getHierarchyGraph(desc).writeGraph();
1809 getSimpleHierarchyGraph(desc).writeGraph();
1814 private void debug_writeSkeletonHierarchyDotFiles() {
1816 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
1817 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1818 Descriptor desc = (Descriptor) iterator.next();
1819 getSkeletonHierarchyGraph(desc).writeGraph();
1824 private void debug_writeSkeletonCombinationHierarchyDotFiles() {
1826 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
1827 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1828 Descriptor desc = (Descriptor) iterator.next();
1829 getSkeletonCombinationHierarchyGraph(desc).writeGraph();
1834 public HierarchyGraph getSimpleHierarchyGraph(Descriptor d) {
1835 return mapDescriptorToSimpleHierarchyGraph.get(d);
1838 private HierarchyGraph getSkeletonHierarchyGraph(Descriptor d) {
1839 if (!mapDescriptorToSkeletonHierarchyGraph.containsKey(d)) {
1840 mapDescriptorToSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
1842 return mapDescriptorToSkeletonHierarchyGraph.get(d);
1845 public HierarchyGraph getSkeletonCombinationHierarchyGraph(Descriptor d) {
1846 if (!mapDescriptorToCombineSkeletonHierarchyGraph.containsKey(d)) {
1847 mapDescriptorToCombineSkeletonHierarchyGraph.put(d, new HierarchyGraph(d));
1849 return mapDescriptorToCombineSkeletonHierarchyGraph.get(d);
1852 private void constructHierarchyGraph() {
1854 // do fixed-point analysis
1856 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
1858 // Collections.sort(descriptorListToAnalyze, new
1859 // Comparator<MethodDescriptor>() {
1860 // public int compare(MethodDescriptor o1, MethodDescriptor o2) {
1861 // return o1.getSymbol().compareToIgnoreCase(o2.getSymbol());
1865 // current descriptors to visit in fixed-point interprocedural analysis,
1866 // prioritized by dependency in the call graph
1867 methodDescriptorsToVisitStack.clear();
1869 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
1870 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
1872 while (!descriptorListToAnalyze.isEmpty()) {
1873 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
1874 methodDescriptorsToVisitStack.add(md);
1877 // analyze scheduled methods until there are no more to visit
1878 while (!methodDescriptorsToVisitStack.isEmpty()) {
1879 // start to analyze leaf node
1880 MethodDescriptor md = methodDescriptorsToVisitStack.pop();
1882 HierarchyGraph hierarchyGraph = new HierarchyGraph(md);
1883 // MethodSummary methodSummary = new MethodSummary(md);
1885 // MethodLocationInfo methodInfo = new MethodLocationInfo(md);
1886 // curMethodInfo = methodInfo;
1888 System.out.println();
1889 System.out.println("SSJAVA: Construcing the hierarchy graph from " + md);
1891 constructHierarchyGraph(md, hierarchyGraph);
1893 HierarchyGraph prevHierarchyGraph = getHierarchyGraph(md);
1894 // MethodSummary prevMethodSummary = getMethodSummary(md);
1896 if (!hierarchyGraph.equals(prevHierarchyGraph)) {
1898 mapDescriptorToHierarchyGraph.put(md, hierarchyGraph);
1899 // mapDescToLocationSummary.put(md, methodSummary);
1901 // results for callee changed, so enqueue dependents caller for
1903 Iterator<MethodDescriptor> depsItr = ssjava.getDependents(md).iterator();
1904 while (depsItr.hasNext()) {
1905 MethodDescriptor methodNext = depsItr.next();
1906 if (!methodDescriptorsToVisitStack.contains(methodNext)
1907 && methodDescriptorToVistSet.contains(methodNext)) {
1908 methodDescriptorsToVisitStack.add(methodNext);
1917 while (!toAnalyzeIsEmpty()) {
1918 ClassDescriptor cd = toAnalyzeNext();
1919 HierarchyGraph graph = getHierarchyGraph(cd);
1920 for (Iterator iter = cd.getFields(); iter.hasNext();) {
1921 FieldDescriptor fieldDesc = (FieldDescriptor) iter.next();
1922 if (!(fieldDesc.isStatic() && fieldDesc.isFinal())) {
1923 graph.getHNode(fieldDesc);
1928 Set<Descriptor> keySet = mapDescriptorToHierarchyGraph.keySet();
1929 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
1930 Descriptor key = (Descriptor) iterator.next();
1931 HierarchyGraph graph = getHierarchyGraph(key);
1933 Set<HNode> nodeToBeConnected = new HashSet<HNode>();
1934 for (Iterator iterator2 = graph.getNodeSet().iterator(); iterator2.hasNext();) {
1935 HNode node = (HNode) iterator2.next();
1936 if (!node.isSkeleton() && !node.isCombinationNode()) {
1937 if (graph.getIncomingNodeSet(node).size() == 0) {
1938 nodeToBeConnected.add(node);
1943 for (Iterator iterator2 = nodeToBeConnected.iterator(); iterator2.hasNext();) {
1944 HNode node = (HNode) iterator2.next();
1945 System.out.println("NEED TO BE CONNECTED TO TOP=" + node);
1946 graph.addEdge(graph.getHNode(TOPDESC), node);
1953 private HierarchyGraph getHierarchyGraph(Descriptor d) {
1954 if (!mapDescriptorToHierarchyGraph.containsKey(d)) {
1955 mapDescriptorToHierarchyGraph.put(d, new HierarchyGraph(d));
1957 return mapDescriptorToHierarchyGraph.get(d);
1960 private void constructHierarchyGraph(MethodDescriptor md, HierarchyGraph methodGraph) {
1962 // visit each node of method flow graph
1963 FlowGraph fg = getFlowGraph(md);
1964 Set<FlowNode> nodeSet = fg.getNodeSet();
1966 Set<Descriptor> paramDescSet = fg.getMapParamDescToIdx().keySet();
1967 for (Iterator iterator = paramDescSet.iterator(); iterator.hasNext();) {
1968 Descriptor desc = (Descriptor) iterator.next();
1969 methodGraph.getHNode(desc).setSkeleton(true);
1972 // for the method lattice, we need to look at the first element of
1973 // NTuple<Descriptor>
1974 boolean hasGlobalAccess = false;
1975 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
1976 FlowNode originalSrcNode = (FlowNode) iterator.next();
1978 Set<FlowNode> sourceNodeSet = new HashSet<FlowNode>();
1979 if (originalSrcNode instanceof FlowReturnNode) {
1980 FlowReturnNode rnode = (FlowReturnNode) originalSrcNode;
1981 Set<NTuple<Descriptor>> tupleSet = rnode.getTupleSet();
1982 for (Iterator iterator2 = tupleSet.iterator(); iterator2.hasNext();) {
1983 NTuple<Descriptor> nTuple = (NTuple<Descriptor>) iterator2.next();
1984 sourceNodeSet.add(fg.getFlowNode(nTuple));
1985 System.out.println("&&&SOURCE fg.getFlowNode(nTuple)=" + fg.getFlowNode(nTuple));
1988 sourceNodeSet.add(originalSrcNode);
1991 System.out.println("---sourceNodeSet=" + sourceNodeSet);
1992 for (Iterator iterator3 = sourceNodeSet.iterator(); iterator3.hasNext();) {
1993 FlowNode srcNode = (FlowNode) iterator3.next();
1995 // if the srcNode is started with the global descriptor
1996 // need to set as a skeleton node
1997 if (!hasGlobalAccess && srcNode.getDescTuple().startsWith(GLOBALDESC)) {
1998 hasGlobalAccess = true;
2001 Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(originalSrcNode);
2002 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
2003 FlowEdge outEdge = (FlowEdge) iterator2.next();
2004 FlowNode originalDstNode = outEdge.getDst();
2006 Set<FlowNode> dstNodeSet = new HashSet<FlowNode>();
2007 if (originalDstNode instanceof FlowReturnNode) {
2008 FlowReturnNode rnode = (FlowReturnNode) originalDstNode;
2009 Set<NTuple<Descriptor>> tupleSet = rnode.getTupleSet();
2010 for (Iterator iterator4 = tupleSet.iterator(); iterator4.hasNext();) {
2011 NTuple<Descriptor> nTuple = (NTuple<Descriptor>) iterator4.next();
2012 dstNodeSet.add(fg.getFlowNode(nTuple));
2015 dstNodeSet.add(originalDstNode);
2017 System.out.println("---dstNodeSet=" + dstNodeSet);
2018 for (Iterator iterator4 = dstNodeSet.iterator(); iterator4.hasNext();) {
2019 FlowNode dstNode = (FlowNode) iterator4.next();
2021 NTuple<Descriptor> srcNodeTuple = srcNode.getDescTuple();
2022 NTuple<Descriptor> dstNodeTuple = dstNode.getDescTuple();
2024 // if (outEdge.getInitTuple().equals(srcNodeTuple)
2025 // && outEdge.getEndTuple().equals(dstNodeTuple)) {
2027 NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
2028 NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
2030 // System.out.println("-srcCurTuple=" + srcCurTuple + " dstCurTuple=" + dstCurTuple);
2032 if ((srcCurTuple.size() > 1 && dstCurTuple.size() > 1)
2033 && srcCurTuple.get(0).equals(dstCurTuple.get(0))) {
2035 // value flows between fields
2036 Descriptor desc = srcCurTuple.get(0);
2037 ClassDescriptor classDesc;
2039 if (desc.equals(GLOBALDESC)) {
2040 classDesc = md.getClassDesc();
2042 VarDescriptor varDesc = (VarDescriptor) srcCurTuple.get(0);
2043 classDesc = varDesc.getType().getClassDesc();
2045 extractFlowsBetweenFields(classDesc, srcNode, dstNode, 1);
2047 } else if (!srcCurTuple.get(0).equals(dstCurTuple.get(0))) {
2048 // value flow between local var - local var or local var - field
2050 Descriptor srcDesc = srcCurTuple.get(0);
2051 Descriptor dstDesc = dstCurTuple.get(0);
2053 methodGraph.addEdge(srcDesc, dstDesc);
2055 if (fg.isParamDesc(srcDesc)) {
2056 methodGraph.setParamHNode(srcDesc);
2058 if (fg.isParamDesc(dstDesc)) {
2059 methodGraph.setParamHNode(dstDesc);
2073 // If the method accesses static fields
2074 // set hasGloabalAccess true in the method summary.
2075 if (hasGlobalAccess) {
2076 getMethodSummary(md).setHasGlobalAccess();
2078 methodGraph.getHNode(GLOBALDESC).setSkeleton(true);
2080 if (ssjava.getMethodContainingSSJavaLoop().equals(md)) {
2081 // if the current method contains the event loop
2082 // we need to set all nodes of the hierarchy graph as a skeleton node
2083 Set<HNode> hnodeSet = methodGraph.getNodeSet();
2084 for (Iterator iterator = hnodeSet.iterator(); iterator.hasNext();) {
2085 HNode hnode = (HNode) iterator.next();
2086 hnode.setSkeleton(true);
2092 private MethodSummary getMethodSummary(MethodDescriptor md) {
2093 if (!mapDescToLocationSummary.containsKey(md)) {
2094 mapDescToLocationSummary.put(md, new MethodSummary(md));
2096 return (MethodSummary) mapDescToLocationSummary.get(md);
2099 private void addMapClassDefinitionToLineNum(ClassDescriptor cd, String strLine, int lineNum) {
2101 String classSymbol = cd.getSymbol();
2102 int idx = classSymbol.lastIndexOf("$");
2104 classSymbol = classSymbol.substring(idx + 1);
2107 String pattern = "class " + classSymbol + " ";
2108 if (strLine.indexOf(pattern) != -1) {
2109 mapDescToDefinitionLine.put(cd, lineNum);
2113 private void addMapMethodDefinitionToLineNum(Set<MethodDescriptor> methodSet, String strLine,
2115 for (Iterator iterator = methodSet.iterator(); iterator.hasNext();) {
2116 MethodDescriptor md = (MethodDescriptor) iterator.next();
2117 String pattern = md.getMethodDeclaration();
2118 if (strLine.indexOf(pattern) != -1) {
2119 mapDescToDefinitionLine.put(md, lineNum);
2120 methodSet.remove(md);
2127 private void readOriginalSourceFiles() {
2129 SymbolTable classtable = state.getClassSymbolTable();
2131 Set<ClassDescriptor> classDescSet = new HashSet<ClassDescriptor>();
2132 classDescSet.addAll(classtable.getValueSet());
2135 // inefficient implement. it may re-visit the same file if the file
2136 // contains more than one class definitions.
2137 for (Iterator iterator = classDescSet.iterator(); iterator.hasNext();) {
2138 ClassDescriptor cd = (ClassDescriptor) iterator.next();
2140 Set<MethodDescriptor> methodSet = new HashSet<MethodDescriptor>();
2141 methodSet.addAll(cd.getMethodTable().getValueSet());
2143 String sourceFileName = cd.getSourceFileName();
2144 Vector<String> lineVec = new Vector<String>();
2146 mapFileNameToLineVector.put(sourceFileName, lineVec);
2148 BufferedReader in = new BufferedReader(new FileReader(sourceFileName));
2151 lineVec.add(""); // the index is started from 1.
2152 while ((strLine = in.readLine()) != null) {
2153 lineVec.add(lineNum, strLine);
2154 addMapClassDefinitionToLineNum(cd, strLine, lineNum);
2155 addMapMethodDefinitionToLineNum(methodSet, strLine, lineNum);
2161 } catch (IOException e) {
2162 e.printStackTrace();
2167 private String generateLatticeDefinition(Descriptor desc) {
2169 Set<String> sharedLocSet = new HashSet<String>();
2171 SSJavaLattice<String> lattice = getLattice(desc);
2172 String rtr = "@LATTICE(\"";
2174 Map<String, Set<String>> map = lattice.getTable();
2175 Set<String> keySet = map.keySet();
2176 boolean first = true;
2177 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
2178 String key = (String) iterator.next();
2179 if (!key.equals(lattice.getTopItem())) {
2180 Set<String> connectedSet = map.get(key);
2182 if (connectedSet.size() == 1) {
2183 if (connectedSet.iterator().next().equals(lattice.getBottomItem())) {
2191 if (lattice.isSharedLoc(key)) {
2192 rtr += "," + key + "*";
2197 for (Iterator iterator2 = connectedSet.iterator(); iterator2.hasNext();) {
2198 String loc = (String) iterator2.next();
2199 if (!loc.equals(lattice.getBottomItem())) {
2206 rtr += loc + "<" + key;
2207 if (lattice.isSharedLoc(key) && (!sharedLocSet.contains(key))) {
2208 rtr += "," + key + "*";
2209 sharedLocSet.add(key);
2211 if (lattice.isSharedLoc(loc) && (!sharedLocSet.contains(loc))) {
2212 rtr += "," + loc + "*";
2213 sharedLocSet.add(loc);
2223 if (desc instanceof MethodDescriptor) {
2224 System.out.println("#EXTRA LOC DECLARATION GEN=" + desc);
2226 MethodDescriptor md = (MethodDescriptor) desc;
2227 MethodSummary methodSummary = getMethodSummary(md);
2229 if (!ssjava.getMethodContainingSSJavaLoop().equals(desc)) {
2230 TypeDescriptor returnType = ((MethodDescriptor) desc).getReturnType();
2231 if (returnType != null && (!returnType.isVoid())) {
2233 "\n@RETURNLOC(\"" + generateLocationAnnoatation(methodSummary.getRETURNLoc()) + "\")";
2235 CompositeLocation pcLoc = methodSummary.getPCLoc();
2236 if ((pcLoc != null) && (!pcLoc.get(0).isTop())) {
2237 rtr += "\n@PCLOC(\"" + generateLocationAnnoatation(pcLoc) + "\")";
2241 if (!md.isStatic()) {
2242 rtr += "\n@THISLOC(\"" + methodSummary.getThisLocName() + "\")";
2244 rtr += "\n@GLOBALLOC(\"" + methodSummary.getGlobalLocName() + "\")";
2251 private void generateAnnoatedCode() {
2253 readOriginalSourceFiles();
2256 while (!toAnalyzeIsEmpty()) {
2257 ClassDescriptor cd = toAnalyzeNext();
2259 setupToAnalazeMethod(cd);
2261 String sourceFileName = cd.getSourceFileName();
2263 if (cd.isInterface()) {
2267 int classDefLine = mapDescToDefinitionLine.get(cd);
2268 Vector<String> sourceVec = mapFileNameToLineVector.get(sourceFileName);
2270 LocationSummary fieldLocSummary = getLocationSummary(cd);
2272 String fieldLatticeDefStr = generateLatticeDefinition(cd);
2273 String annoatedSrc = fieldLatticeDefStr + newline + sourceVec.get(classDefLine);
2274 sourceVec.set(classDefLine, annoatedSrc);
2276 // generate annotations for field declarations
2277 // Map<Descriptor, CompositeLocation> inferLocMap = fieldLocInfo.getMapDescToInferLocation();
2278 Map<String, String> mapFieldNameToLocName = fieldLocSummary.getMapHNodeNameToLocationName();
2280 for (Iterator iter = cd.getFields(); iter.hasNext();) {
2281 FieldDescriptor fd = (FieldDescriptor) iter.next();
2283 String locAnnotationStr;
2284 // CompositeLocation inferLoc = inferLocMap.get(fd);
2285 String locName = mapFieldNameToLocName.get(fd.getSymbol());
2287 if (locName != null) {
2288 // infer loc is null if the corresponding field is static and final
2289 // locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
2290 locAnnotationStr = "@LOC(\"" + locName + "\")";
2291 int fdLineNum = fd.getLineNum();
2292 String orgFieldDeclarationStr = sourceVec.get(fdLineNum);
2293 String fieldDeclaration = fd.toString();
2294 fieldDeclaration = fieldDeclaration.substring(0, fieldDeclaration.length() - 1);
2295 String annoatedStr = locAnnotationStr + " " + orgFieldDeclarationStr;
2296 sourceVec.set(fdLineNum, annoatedStr);
2301 while (!toAnalyzeMethodIsEmpty()) {
2302 MethodDescriptor md = toAnalyzeMethodNext();
2304 if (!ssjava.needTobeAnnotated(md)) {
2308 SSJavaLattice<String> methodLattice = md2lattice.get(md);
2309 if (methodLattice != null) {
2311 int methodDefLine = md.getLineNum();
2313 // MethodLocationInfo methodLocInfo = getMethodLocationInfo(md);
2314 // Map<Descriptor, CompositeLocation> methodInferLocMap =
2315 // methodLocInfo.getMapDescToInferLocation();
2317 MethodSummary methodSummary = getMethodSummary(md);
2319 Map<Descriptor, CompositeLocation> mapVarDescToInferLoc =
2320 methodSummary.getMapVarDescToInferCompositeLocation();
2321 System.out.println("-----md=" + md);
2322 System.out.println("-----mapVarDescToInferLoc=" + mapVarDescToInferLoc);
2324 Set<Descriptor> localVarDescSet = mapVarDescToInferLoc.keySet();
2326 Set<String> localLocElementSet = methodLattice.getElementSet();
2328 for (Iterator iterator = localVarDescSet.iterator(); iterator.hasNext();) {
2329 Descriptor localVarDesc = (Descriptor) iterator.next();
2330 System.out.println("-------localVarDesc=" + localVarDesc);
2331 CompositeLocation inferLoc = mapVarDescToInferLoc.get(localVarDesc);
2333 String localLocIdentifier = inferLoc.get(0).getLocIdentifier();
2334 if (!localLocElementSet.contains(localLocIdentifier)) {
2335 methodLattice.put(localLocIdentifier);
2338 String locAnnotationStr = "@LOC(\"" + generateLocationAnnoatation(inferLoc) + "\")";
2340 if (!isParameter(md, localVarDesc)) {
2341 if (mapDescToDefinitionLine.containsKey(localVarDesc)) {
2342 int varLineNum = mapDescToDefinitionLine.get(localVarDesc);
2343 String orgSourceLine = sourceVec.get(varLineNum);
2345 orgSourceLine.indexOf(generateVarDeclaration((VarDescriptor) localVarDesc));
2346 System.out.println("idx=" + idx
2347 + " generateVarDeclaration((VarDescriptor) localVarDesc)="
2348 + generateVarDeclaration((VarDescriptor) localVarDesc));
2350 String annoatedStr =
2351 orgSourceLine.substring(0, idx) + locAnnotationStr + " "
2352 + orgSourceLine.substring(idx);
2353 sourceVec.set(varLineNum, annoatedStr);
2356 String methodDefStr = sourceVec.get(methodDefLine);
2359 getParamLocation(methodDefStr,
2360 generateVarDeclaration((VarDescriptor) localVarDesc));
2361 System.out.println("methodDefStr=" + methodDefStr + " localVarDesc=" + localVarDesc
2365 String annoatedStr =
2366 methodDefStr.substring(0, idx) + locAnnotationStr + " "
2367 + methodDefStr.substring(idx);
2368 sourceVec.set(methodDefLine, annoatedStr);
2373 // check if the lattice has to have the location type for the this
2376 // boolean needToAddthisRef = hasThisReference(md);
2377 // if (localLocElementSet.contains("this")) {
2378 // methodLattice.put("this");
2381 String methodLatticeDefStr = generateLatticeDefinition(md);
2382 String annoatedStr = methodLatticeDefStr + newline + sourceVec.get(methodDefLine);
2383 sourceVec.set(methodDefLine, annoatedStr);
2393 private boolean hasThisReference(MethodDescriptor md) {
2395 FlowGraph fg = getFlowGraph(md);
2396 Set<FlowNode> nodeSet = fg.getNodeSet();
2397 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
2398 FlowNode flowNode = (FlowNode) iterator.next();
2399 if (flowNode.getDescTuple().get(0).equals(md.getThis())) {
2407 private int getParamLocation(String methodStr, String paramStr) {
2409 String pattern = paramStr + ",";
2411 int idx = methodStr.indexOf(pattern);
2415 pattern = paramStr + ")";
2416 return methodStr.indexOf(pattern);
2421 private String generateVarDeclaration(VarDescriptor varDesc) {
2423 TypeDescriptor td = varDesc.getType();
2424 String rtr = td.toString();
2426 for (int i = 0; i < td.getArrayCount(); i++) {
2430 rtr += " " + varDesc.getName();
2435 private String generateLocationAnnoatation(CompositeLocation loc) {
2438 Location methodLoc = loc.get(0);
2439 rtr += methodLoc.getLocIdentifier();
2441 for (int i = 1; i < loc.getSize(); i++) {
2442 Location element = loc.get(i);
2443 rtr += "," + element.getDescriptor().getSymbol() + "." + element.getLocIdentifier();
2449 private boolean isParameter(MethodDescriptor md, Descriptor localVarDesc) {
2450 return getFlowGraph(md).isParamDesc(localVarDesc);
2453 private String extractFileName(String fileName) {
2454 int idx = fileName.lastIndexOf("/");
2458 return fileName.substring(idx + 1);
2463 private void codeGen() {
2465 Set<String> originalFileNameSet = mapFileNameToLineVector.keySet();
2466 for (Iterator iterator = originalFileNameSet.iterator(); iterator.hasNext();) {
2467 String orgFileName = (String) iterator.next();
2468 String outputFileName = extractFileName(orgFileName);
2470 Vector<String> sourceVec = mapFileNameToLineVector.get(orgFileName);
2474 FileWriter fileWriter = new FileWriter("./infer/" + outputFileName);
2475 BufferedWriter out = new BufferedWriter(fileWriter);
2477 for (int i = 0; i < sourceVec.size(); i++) {
2478 out.write(sourceVec.get(i));
2482 } catch (IOException e) {
2483 e.printStackTrace();
2490 private void checkLattices() {
2492 LinkedList<MethodDescriptor> descriptorListToAnalyze = ssjava.getSortedDescriptors();
2494 // current descriptors to visit in fixed-point interprocedural analysis,
2496 // dependency in the call graph
2497 methodDescriptorsToVisitStack.clear();
2499 // descriptorListToAnalyze.removeFirst();
2501 Set<MethodDescriptor> methodDescriptorToVistSet = new HashSet<MethodDescriptor>();
2502 methodDescriptorToVistSet.addAll(descriptorListToAnalyze);
2504 while (!descriptorListToAnalyze.isEmpty()) {
2505 MethodDescriptor md = descriptorListToAnalyze.removeFirst();
2506 checkLatticesOfVirtualMethods(md);
2511 private void debug_writeLatticeDotFile() {
2512 // generate lattice dot file
2516 while (!toAnalyzeIsEmpty()) {
2517 ClassDescriptor cd = toAnalyzeNext();
2519 setupToAnalazeMethod(cd);
2521 SSJavaLattice<String> classLattice = cd2lattice.get(cd);
2522 if (classLattice != null) {
2523 ssjava.writeLatticeDotFile(cd, null, classLattice);
2524 debug_printDescriptorToLocNameMapping(cd);
2527 while (!toAnalyzeMethodIsEmpty()) {
2528 MethodDescriptor md = toAnalyzeMethodNext();
2529 SSJavaLattice<String> methodLattice = md2lattice.get(md);
2530 if (methodLattice != null) {
2531 ssjava.writeLatticeDotFile(cd, md, methodLattice);
2532 debug_printDescriptorToLocNameMapping(md);
2539 private void debug_printDescriptorToLocNameMapping(Descriptor desc) {
2541 LocationInfo info = getLocationInfo(desc);
2542 System.out.println("## " + desc + " ##");
2543 System.out.println(info.getMapDescToInferLocation());
2544 LocationInfo locInfo = getLocationInfo(desc);
2545 System.out.println("mapping=" + locInfo.getMapLocSymbolToDescSet());
2546 System.out.println("###################");
2550 private void calculateExtraLocations() {
2552 LinkedList<MethodDescriptor> methodDescList = ssjava.getSortedDescriptors();
2553 for (Iterator iterator = methodDescList.iterator(); iterator.hasNext();) {
2554 MethodDescriptor md = (MethodDescriptor) iterator.next();
2555 if (!ssjava.getMethodContainingSSJavaLoop().equals(md)) {
2556 calculateExtraLocations(md);
2562 private void checkLatticesOfVirtualMethods(MethodDescriptor md) {
2564 if (!md.isStatic()) {
2565 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
2566 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
2568 for (Iterator iterator = setPossibleCallees.iterator(); iterator.hasNext();) {
2569 MethodDescriptor mdCallee = (MethodDescriptor) iterator.next();
2570 if (!md.equals(mdCallee)) {
2571 checkConsistency(md, mdCallee);
2579 private void checkConsistency(MethodDescriptor md1, MethodDescriptor md2) {
2581 // check that two lattice have the same relations between parameters(+PC
2582 // LOC, GLOBAL_LOC RETURN LOC)
2584 List<CompositeLocation> list1 = new ArrayList<CompositeLocation>();
2585 List<CompositeLocation> list2 = new ArrayList<CompositeLocation>();
2587 MethodLocationInfo locInfo1 = getMethodLocationInfo(md1);
2588 MethodLocationInfo locInfo2 = getMethodLocationInfo(md2);
2590 Map<Integer, CompositeLocation> paramMap1 = locInfo1.getMapParamIdxToInferLoc();
2591 Map<Integer, CompositeLocation> paramMap2 = locInfo2.getMapParamIdxToInferLoc();
2593 int numParam = locInfo1.getMapParamIdxToInferLoc().keySet().size();
2595 // add location types of paramters
2596 for (int idx = 0; idx < numParam; idx++) {
2597 list1.add(paramMap1.get(Integer.valueOf(idx)));
2598 list2.add(paramMap2.get(Integer.valueOf(idx)));
2601 // add program counter location
2602 list1.add(locInfo1.getPCLoc());
2603 list2.add(locInfo2.getPCLoc());
2605 if (!md1.getReturnType().isVoid()) {
2606 // add return value location
2607 CompositeLocation rtrLoc1 = getMethodLocationInfo(md1).getReturnLoc();
2608 CompositeLocation rtrLoc2 = getMethodLocationInfo(md2).getReturnLoc();
2613 // add global location type
2614 if (md1.isStatic()) {
2615 CompositeLocation globalLoc1 =
2616 new CompositeLocation(new Location(md1, locInfo1.getGlobalLocName()));
2617 CompositeLocation globalLoc2 =
2618 new CompositeLocation(new Location(md2, locInfo2.getGlobalLocName()));
2619 list1.add(globalLoc1);
2620 list2.add(globalLoc2);
2623 for (int i = 0; i < list1.size(); i++) {
2624 CompositeLocation locA1 = list1.get(i);
2625 CompositeLocation locA2 = list2.get(i);
2626 for (int k = 0; k < list1.size(); k++) {
2628 CompositeLocation locB1 = list1.get(k);
2629 CompositeLocation locB2 = list2.get(k);
2630 boolean r1 = isGreaterThan(getLattice(md1), locA1, locB1);
2632 boolean r2 = isGreaterThan(getLattice(md1), locA2, locB2);
2635 throw new Error("The method " + md1 + " is not consistent with the method " + md2
2636 + ".:: They have a different ordering relation between locations (" + locA1 + ","
2637 + locB1 + ") and (" + locA2 + "," + locB2 + ").");
2645 private String getSymbol(int idx, FlowNode node) {
2646 Descriptor desc = node.getDescTuple().get(idx);
2647 return desc.getSymbol();
2650 private Descriptor getDescriptor(int idx, FlowNode node) {
2651 Descriptor desc = node.getDescTuple().get(idx);
2655 private void calculatePCLOC(MethodDescriptor md) {
2657 System.out.println("#CalculatePCLOC");
2658 MethodSummary methodSummary = getMethodSummary(md);
2659 FlowGraph fg = getFlowGraph(md);
2660 Map<Integer, CompositeLocation> mapParamToLoc = methodSummary.getMapParamIdxToInferLoc();
2662 // calculate the initial program counter location
2663 // PC location is higher than location types of parameters which has incoming flows.
2665 Set<NTuple<Location>> paramLocTupleHavingInFlowSet = new HashSet<NTuple<Location>>();
2666 Set<Descriptor> paramDescNOTHavingInFlowSet = new HashSet<Descriptor>();
2667 // Set<FlowNode> paramNodeNOThavingInFlowSet = new HashSet<FlowNode>();
2669 int numParams = fg.getNumParameters();
2670 for (int i = 0; i < numParams; i++) {
2671 FlowNode paramFlowNode = fg.getParamFlowNode(i);
2672 Descriptor prefix = paramFlowNode.getDescTuple().get(0);
2673 NTuple<Descriptor> paramDescTuple = paramFlowNode.getCurrentDescTuple();
2674 NTuple<Location> paramLocTuple = translateToLocTuple(md, paramDescTuple);
2676 Set<FlowNode> inNodeToParamSet = fg.getIncomingNodeSetByPrefix(prefix);
2677 if (inNodeToParamSet.size() > 0) {
2678 // parameter has in-value flows
2680 for (Iterator iterator = inNodeToParamSet.iterator(); iterator.hasNext();) {
2681 FlowNode inNode = (FlowNode) iterator.next();
2682 Set<FlowEdge> outEdgeSet = fg.getOutEdgeSet(inNode);
2683 for (Iterator iterator2 = outEdgeSet.iterator(); iterator2.hasNext();) {
2684 FlowEdge flowEdge = (FlowEdge) iterator2.next();
2685 if (flowEdge.getEndTuple().startsWith(prefix)) {
2686 NTuple<Location> paramLocTupleWithIncomingFlow =
2687 translateToLocTuple(md, flowEdge.getEndTuple());
2688 paramLocTupleHavingInFlowSet.add(paramLocTupleWithIncomingFlow);
2693 // paramLocTupleHavingInFlowSet.add(paramLocTuple);
2695 // paramNodeNOThavingInFlowSet.add(fg.getFlowNode(paramDescTuple));
2696 paramDescNOTHavingInFlowSet.add(prefix);
2700 System.out.println("paramLocTupleHavingInFlowSet=" + paramLocTupleHavingInFlowSet);
2702 if (paramLocTupleHavingInFlowSet.size() > 0
2703 && !coversAllParamters(md, fg, paramLocTupleHavingInFlowSet)) {
2705 // Here, generates a location in the method lattice that is higher than the
2706 // paramLocTupleHavingInFlowSet
2707 NTuple<Location> pcLocTuple =
2708 generateLocTupleRelativeTo(md, paramLocTupleHavingInFlowSet, PCLOC);
2710 NTuple<Descriptor> pcDescTuple = translateToDescTuple(pcLocTuple);
2712 // System.out.println("pcLoc=" + pcLocTuple);
2714 CompositeLocation curPCLoc = methodSummary.getPCLoc();
2715 if (curPCLoc.get(0).isTop() || pcLocTuple.size() > curPCLoc.getSize()) {
2716 methodSummary.setPCLoc(new CompositeLocation(pcLocTuple));
2718 // add ordering relations s.t. PCLOC is higher than all flow nodes except the set of
2719 // parameters that do not have incoming flows
2720 for (Iterator iterator = fg.getNodeSet().iterator(); iterator.hasNext();) {
2721 FlowNode node = (FlowNode) iterator.next();
2723 if (!paramDescNOTHavingInFlowSet.contains(node.getCurrentDescTuple().get(0))) {
2724 fg.addValueFlowEdge(pcDescTuple, node.getDescTuple());
2727 fg.getFlowNode(translateToDescTuple(pcLocTuple)).setSkeleton(true);
2733 private boolean coversAllParamters(MethodDescriptor md, FlowGraph fg,
2734 Set<NTuple<Location>> paramLocTupleHavingInFlowSet) {
2736 int numParam = fg.getNumParameters();
2737 int size = paramLocTupleHavingInFlowSet.size();
2739 if (!md.isStatic()) {
2741 // if the method is not static && there is a parameter composite location &&
2742 // it is started with 'this',
2743 // paramLocTupleHavingInFlowSet need to have 'this' parameter.
2745 FlowNode thisParamNode = fg.getParamFlowNode(0);
2746 NTuple<Location> thisParamLocTuple =
2747 translateToLocTuple(md, thisParamNode.getCurrentDescTuple());
2749 if (!paramLocTupleHavingInFlowSet.contains(thisParamLocTuple)) {
2751 for (Iterator iterator = paramLocTupleHavingInFlowSet.iterator(); iterator.hasNext();) {
2752 NTuple<Location> paramTuple = (NTuple<Location>) iterator.next();
2753 if (paramTuple.size() > 1 && paramTuple.get(0).getLocDescriptor().equals(md.getThis())) {
2754 // paramLocTupleHavingInFlowSet.add(thisParamLocTuple);
2763 if (size == numParam) {
2771 private void calculateRETURNLOC(MethodDescriptor md) {
2773 System.out.println("#calculateRETURNLOC= " + md);
2774 // calculate a return location:
2775 // the return location type is lower than all parameters and the location of return values
2776 MethodSummary methodSummary = getMethodSummary(md);
2777 FlowGraph fg = getFlowGraph(md);
2778 Map<Integer, CompositeLocation> mapParamToLoc = methodSummary.getMapParamIdxToInferLoc();
2779 Set<Integer> paramIdxSet = mapParamToLoc.keySet();
2781 if (!md.getReturnType().isVoid()) {
2782 // first, generate the set of return value location types that starts
2783 // with 'this' reference
2785 Set<FlowNode> paramFlowNodeFlowingToReturnValueSet = getParamNodeFlowingToReturnValue(md);
2786 // System.out.println("paramFlowNodeFlowingToReturnValueSet="
2787 // + paramFlowNodeFlowingToReturnValueSet);
2789 Set<NTuple<Location>> tupleToBeHigherThanReturnLocSet = new HashSet<NTuple<Location>>();
2790 for (Iterator iterator = paramFlowNodeFlowingToReturnValueSet.iterator(); iterator.hasNext();) {
2791 FlowNode fn = (FlowNode) iterator.next();
2792 NTuple<Descriptor> paramDescTuple = fn.getCurrentDescTuple();
2793 tupleToBeHigherThanReturnLocSet.add(translateToLocTuple(md, paramDescTuple));
2796 Set<FlowNode> returnNodeSet = fg.getReturnNodeSet();
2797 for (Iterator iterator = returnNodeSet.iterator(); iterator.hasNext();) {
2798 FlowNode returnNode = (FlowNode) iterator.next();
2799 NTuple<Descriptor> returnDescTuple = returnNode.getCurrentDescTuple();
2800 tupleToBeHigherThanReturnLocSet.add(translateToLocTuple(md, returnDescTuple));
2802 System.out.println("-flow graph's returnNodeSet=" + returnNodeSet);
2803 System.out.println("tupleSetToBeHigherThanReturnLoc=" + tupleToBeHigherThanReturnLocSet);
2805 // Here, generates a return location in the method lattice that is lower than the
2806 // locFlowingToReturnValueSet
2807 NTuple<Location> returnLocTuple =
2808 generateLocTupleRelativeTo(md, tupleToBeHigherThanReturnLocSet, RLOC);
2810 // System.out.println("returnLocTuple=" + returnLocTuple);
2811 NTuple<Descriptor> returnDescTuple = translateToDescTuple(returnLocTuple);
2812 CompositeLocation curReturnLoc = methodSummary.getRETURNLoc();
2813 if (curReturnLoc == null || returnDescTuple.size() > curReturnLoc.getSize()) {
2814 methodSummary.setRETURNLoc(new CompositeLocation(returnLocTuple));
2816 for (Iterator iterator = tupleToBeHigherThanReturnLocSet.iterator(); iterator.hasNext();) {
2817 NTuple<Location> higherTuple = (NTuple<Location>) iterator.next();
2818 fg.addValueFlowEdge(translateToDescTuple(higherTuple), returnDescTuple);
2820 fg.getFlowNode(returnDescTuple).setSkeleton(true);
2828 private void calculateExtraLocations(MethodDescriptor md) {
2829 // calcualte pcloc, returnloc,...
2831 System.out.println("\nSSJAVA:Calculate PCLOC/RETURNLOC locations: " + md);
2834 calculateRETURNLOC(md);
2838 private NTuple<Location> generateLocTupleRelativeTo(MethodDescriptor md,
2839 Set<NTuple<Location>> paramLocTupleHavingInFlowSet, String locNamePrefix) {
2841 // System.out.println("-generateLocTupleRelativeTo=" + paramLocTupleHavingInFlowSet);
2843 NTuple<Location> higherLocTuple = new NTuple<Location>();
2845 VarDescriptor thisVarDesc = md.getThis();
2846 // check if all paramter loc tuple is started with 'this' reference
2847 boolean hasParamNotStartedWithThisRef = false;
2851 Set<NTuple<Location>> paramLocTupleStartedWithThis = new HashSet<NTuple<Location>>();
2853 next: for (Iterator iterator = paramLocTupleHavingInFlowSet.iterator(); iterator.hasNext();) {
2854 NTuple<Location> paramLocTuple = (NTuple<Location>) iterator.next();
2855 Descriptor paramLocalDesc = paramLocTuple.get(0).getLocDescriptor();
2856 if (!paramLocalDesc.equals(thisVarDesc)) {
2858 Set<FlowNode> inNodeSet = getFlowGraph(md).getIncomingNodeSetByPrefix(paramLocalDesc);
2859 for (Iterator iterator2 = inNodeSet.iterator(); iterator2.hasNext();) {
2860 FlowNode flowNode = (FlowNode) iterator2.next();
2861 if (flowNode.getDescTuple().startsWith(thisVarDesc)) {
2862 // System.out.println("paramLocTuple=" + paramLocTuple + " is lower than THIS");
2866 hasParamNotStartedWithThisRef = true;
2868 } else if (paramLocTuple.size() > 1) {
2869 paramLocTupleStartedWithThis.add(paramLocTuple);
2870 if (minSize == 0 || minSize > paramLocTuple.size()) {
2871 minSize = paramLocTuple.size();
2876 // System.out.println("---paramLocTupleStartedWithThis=" + paramLocTupleStartedWithThis);
2877 Descriptor enclosingDesc = md;
2878 if (hasParamNotStartedWithThisRef) {
2879 // in this case, PCLOC will be the local location
2881 // all parameter is started with 'this', so PCLOC will be set relative to the composite
2882 // location started with 'this'.
2883 for (int idx = 0; idx < minSize - 1; idx++) {
2884 Set<Descriptor> locDescSet = new HashSet<Descriptor>();
2885 Location curLoc = null;
2886 NTuple<Location> paramLocTuple = null;
2887 for (Iterator iterator = paramLocTupleStartedWithThis.iterator(); iterator.hasNext();) {
2888 paramLocTuple = (NTuple<Location>) iterator.next();
2889 // System.out.println("-----paramLocTuple=" + paramLocTuple + " idx=" + idx);
2890 curLoc = paramLocTuple.get(idx);
2891 Descriptor locDesc = curLoc.getLocDescriptor();
2892 locDescSet.add(locDesc);
2894 // System.out.println("-----locDescSet=" + locDescSet + " idx=" + idx);
2895 if (locDescSet.size() != 1) {
2898 Location newLocElement = new Location(curLoc.getDescriptor(), curLoc.getLocDescriptor());
2899 System.out.println("newLocElement" + newLocElement);
2900 higherLocTuple.add(newLocElement);
2901 enclosingDesc = getClassTypeDescriptor(curLoc.getLocDescriptor());
2906 String pcLocIdentifier = locNamePrefix + (locSeed++);
2907 NameDescriptor pcLocDesc = new NameDescriptor(pcLocIdentifier);
2908 Location newLoc = new Location(enclosingDesc, pcLocDesc);
2909 higherLocTuple.add(newLoc);
2910 System.out.println("---new loc tuple=" + higherLocTuple);
2912 return higherLocTuple;
2916 public ClassDescriptor getClassTypeDescriptor(Descriptor in) {
2918 if (in instanceof VarDescriptor) {
2919 return ((VarDescriptor) in).getType().getClassDesc();
2920 } else if (in instanceof FieldDescriptor) {
2921 return ((FieldDescriptor) in).getType().getClassDesc();
2923 // else if (in instanceof LocationDescriptor) {
2924 // // here is the case that the descriptor 'in' is the last element of the assigned composite
2926 // return ((VarDescriptor) locTuple.get(0).getLocDescriptor()).getType().getClassDesc();
2934 private Set<NTuple<Location>> calculateHighestLocTupleSet(
2935 Set<NTuple<Location>> paramLocTupleHavingInFlowSet) {
2937 Set<NTuple<Location>> highestSet = new HashSet<NTuple<Location>>();
2939 Iterator<NTuple<Location>> iterator = paramLocTupleHavingInFlowSet.iterator();
2940 NTuple<Location> highest = iterator.next();
2942 for (; iterator.hasNext();) {
2943 NTuple<Location> curLocTuple = (NTuple<Location>) iterator.next();
2944 if (isHigherThan(curLocTuple, highest)) {
2945 // System.out.println(curLocTuple + " is greater than " + highest);
2946 highest = curLocTuple;
2950 highestSet.add(highest);
2952 MethodDescriptor md = (MethodDescriptor) highest.get(0).getDescriptor();
2953 VarDescriptor thisVarDesc = md.getThis();
2955 // System.out.println("highest=" + highest);
2957 for (Iterator<NTuple<Location>> iter = paramLocTupleHavingInFlowSet.iterator(); iter.hasNext();) {
2958 NTuple<Location> curLocTuple = iter.next();
2960 if (!curLocTuple.equals(highest) && !hasOrderingRelation(highest, curLocTuple)) {
2962 // System.out.println("add it to the highest set=" + curLocTuple);
2963 highestSet.add(curLocTuple);
2972 private Set<String> getHigherLocSymbolThan(SSJavaLattice<String> lattice, String loc) {
2973 Set<String> higherLocSet = new HashSet<String>();
2975 Set<String> locSet = lattice.getTable().keySet();
2976 for (Iterator iterator = locSet.iterator(); iterator.hasNext();) {
2977 String element = (String) iterator.next();
2978 if (lattice.isGreaterThan(element, loc) && (!element.equals(lattice.getTopItem()))) {
2979 higherLocSet.add(element);
2982 return higherLocSet;
2985 private CompositeLocation getLowest(SSJavaLattice<String> methodLattice,
2986 Set<CompositeLocation> set) {
2988 CompositeLocation lowest = set.iterator().next();
2990 if (set.size() == 1) {
2994 for (Iterator iterator = set.iterator(); iterator.hasNext();) {
2995 CompositeLocation loc = (CompositeLocation) iterator.next();
2997 if ((!loc.equals(lowest)) && (!isComparable(methodLattice, lowest, loc))) {
2998 // if there is a case where composite locations are incomparable, just
3003 if ((!loc.equals(lowest)) && isGreaterThan(methodLattice, lowest, loc)) {
3010 private boolean isComparable(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
3011 CompositeLocation comp2) {
3013 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
3015 for (int idx = 0; idx < size; idx++) {
3016 Location loc1 = comp1.get(idx);
3017 Location loc2 = comp2.get(idx);
3019 Descriptor desc1 = loc1.getDescriptor();
3020 Descriptor desc2 = loc2.getDescriptor();
3022 if (!desc1.equals(desc2)) {
3023 throw new Error("Fail to compare " + comp1 + " and " + comp2);
3026 String symbol1 = loc1.getLocIdentifier();
3027 String symbol2 = loc2.getLocIdentifier();
3029 SSJavaLattice<String> lattice;
3031 lattice = methodLattice;
3033 lattice = getLattice(desc1);
3036 if (symbol1.equals(symbol2)) {
3038 } else if (!lattice.isComparable(symbol1, symbol2)) {
3047 private boolean isGreaterThan(SSJavaLattice<String> methodLattice, CompositeLocation comp1,
3048 CompositeLocation comp2) {
3050 int size = comp1.getSize() >= comp2.getSize() ? comp2.getSize() : comp1.getSize();
3052 for (int idx = 0; idx < size; idx++) {
3053 Location loc1 = comp1.get(idx);
3054 Location loc2 = comp2.get(idx);
3056 Descriptor desc1 = loc1.getDescriptor();
3057 Descriptor desc2 = loc2.getDescriptor();
3059 if (!desc1.equals(desc2)) {
3060 throw new Error("Fail to compare " + comp1 + " and " + comp2);
3063 String symbol1 = loc1.getLocIdentifier();
3064 String symbol2 = loc2.getLocIdentifier();
3066 SSJavaLattice<String> lattice;
3068 lattice = methodLattice;
3070 lattice = getLattice(desc1);
3073 if (symbol1.equals(symbol2)) {
3075 } else if (lattice.isGreaterThan(symbol1, symbol2)) {
3086 private GlobalFlowGraph getSubGlobalFlowGraph(MethodDescriptor md) {
3088 if (!mapMethodDescriptorToSubGlobalFlowGraph.containsKey(md)) {
3089 mapMethodDescriptorToSubGlobalFlowGraph.put(md, new GlobalFlowGraph(md));
3091 return mapMethodDescriptorToSubGlobalFlowGraph.get(md);
3094 private void propagateFlowsToCallerWithNoCompositeLocation(MethodInvokeNode min,
3095 MethodDescriptor mdCaller, MethodDescriptor mdCallee) {
3097 // if the parameter A reaches to the parameter B
3098 // then, add an edge the argument A -> the argument B to the caller's flow
3101 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
3102 FlowGraph callerFlowGraph = getFlowGraph(mdCaller);
3103 int numParam = calleeFlowGraph.getNumParameters();
3105 for (int i = 0; i < numParam; i++) {
3106 for (int k = 0; k < numParam; k++) {
3110 FlowNode paramNode1 = calleeFlowGraph.getParamFlowNode(i);
3111 FlowNode paramNode2 = calleeFlowGraph.getParamFlowNode(k);
3113 NTuple<Descriptor> arg1Tuple = getNodeTupleByArgIdx(min, i);
3114 NTuple<Descriptor> arg2Tuple = getNodeTupleByArgIdx(min, k);
3116 // check if the callee propagates an ordering constraints through
3119 Set<FlowNode> localReachSet = calleeFlowGraph.getLocalReachFlowNodeSetFrom(paramNode1);
3120 System.out.println("-param1=" + paramNode1 + " is higher than param2=" + paramNode2);
3121 // System.out.println("-- localReachSet from param1=" + localReachSet);
3123 NTuple<Descriptor> paramDescTuple1 = paramNode1.getCurrentDescTuple();
3124 NTuple<Descriptor> paramDescTuple2 = paramNode2.getCurrentDescTuple();
3126 System.out.println("-param1CurTuple=" + paramDescTuple1 + " param2CurTuple="
3128 if (paramDescTuple1.get(0).equals(paramDescTuple2.get(0))) {
3129 // if two parameters share the same prefix
3130 // it already has been assigned to a composite location
3131 // so we don't need to add an additional ordering relation caused by these two
3136 if (arg1Tuple.size() > 0 && arg2Tuple.size() > 0 && localReachSet.contains(paramNode2)) {
3137 // need to propagate an ordering relation s.t. arg1 is higher
3140 // add a new flow between the corresponding arguments.
3141 callerFlowGraph.addValueFlowEdge(arg1Tuple, arg2Tuple);
3142 System.out.println("arg1=" + arg1Tuple + " arg2=" + arg2Tuple);
3145 // .println("-arg1Tuple=" + arg1Tuple + " is higher than arg2Tuple=" + arg2Tuple);
3149 System.out.println();
3154 // if a parameter has a composite location and the first element of the parameter location
3155 // matches the callee's 'this'
3156 // we have a more specific constraint: the caller's corresponding argument is higher than the
3157 // parameter location which is translated into the caller
3159 for (int idx = 0; idx < numParam; idx++) {
3160 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
3161 CompositeLocation compLoc = paramNode.getCompositeLocation();
3162 if (compLoc != null && compLoc.get(0).getLocDescriptor().equals(min.getMethod().getThis())) {
3163 System.out.println("$$$COMPLOC CASE=" + compLoc);
3164 NTuple<Descriptor> argTuple = getNodeTupleByArgIdx(min, idx);
3165 NTuple<Descriptor> translatedParamTuple =
3166 translateCompositeLocationToCaller(idx, min, compLoc);
3167 System.out.println("add a flow edge= " + argTuple + "->" + translatedParamTuple);
3168 callerFlowGraph.addValueFlowEdge(argTuple, translatedParamTuple);
3170 Set<NTuple<Location>> pcLocTupleSet = getPCLocTupleSet(min);
3171 for (Iterator iterator = pcLocTupleSet.iterator(); iterator.hasNext();) {
3172 NTuple<Location> pcLocTuple = (NTuple<Location>) iterator.next();
3173 callerFlowGraph.addValueFlowEdge(translateToDescTuple(pcLocTuple), translatedParamTuple);
3181 private NTuple<Descriptor> translateCompositeLocationToCaller(int idx, MethodInvokeNode min,
3182 CompositeLocation compLocForParam1) {
3184 NTuple<Descriptor> baseTuple = mapMethodInvokeNodeToBaseTuple.get(min);
3186 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
3187 for (int i = 0; i < baseTuple.size(); i++) {
3188 tuple.add(baseTuple.get(i));
3191 for (int i = baseTuple.size(); i < compLocForParam1.getSize(); i++) {
3192 Location loc = compLocForParam1.get(i);
3193 tuple.add(loc.getLocDescriptor());
3199 private CompositeLocation generateCompositeLocation(NTuple<Location> prefixLocTuple) {
3201 System.out.println("generateCompositeLocation=" + prefixLocTuple);
3203 CompositeLocation newCompLoc = new CompositeLocation();
3204 for (int i = 0; i < prefixLocTuple.size(); i++) {
3205 newCompLoc.addLocation(prefixLocTuple.get(i));
3208 Descriptor lastDescOfPrefix = prefixLocTuple.get(prefixLocTuple.size() - 1).getLocDescriptor();
3210 ClassDescriptor enclosingDescriptor;
3211 if (lastDescOfPrefix instanceof FieldDescriptor) {
3212 enclosingDescriptor = ((FieldDescriptor) lastDescOfPrefix).getType().getClassDesc();
3213 // System.out.println("enclosingDescriptor0=" + enclosingDescriptor);
3214 } else if (lastDescOfPrefix.equals(GLOBALDESC)) {
3215 MethodDescriptor currentMethodDesc = (MethodDescriptor) prefixLocTuple.get(0).getDescriptor();
3216 enclosingDescriptor = currentMethodDesc.getClassDesc();
3218 // var descriptor case
3219 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
3221 // System.out.println("enclosingDescriptor=" + enclosingDescriptor);
3223 LocationDescriptor newLocDescriptor = generateNewLocationDescriptor();
3224 newLocDescriptor.setEnclosingClassDesc(enclosingDescriptor);
3226 Location newLoc = new Location(enclosingDescriptor, newLocDescriptor.getSymbol());
3227 newLoc.setLocDescriptor(newLocDescriptor);
3228 newCompLoc.addLocation(newLoc);
3230 // System.out.println("--newCompLoc=" + newCompLoc);
3234 private CompositeLocation generateCompositeLocation(MethodDescriptor md,
3235 NTuple<Descriptor> paramPrefix) {
3237 System.out.println("generateCompositeLocation=" + paramPrefix);
3239 CompositeLocation newCompLoc = convertToCompositeLocation(md, paramPrefix);
3241 Descriptor lastDescOfPrefix = paramPrefix.get(paramPrefix.size() - 1);
3242 // System.out.println("lastDescOfPrefix=" + lastDescOfPrefix + " kind="
3243 // + lastDescOfPrefix.getClass());
3244 ClassDescriptor enclosingDescriptor;
3245 if (lastDescOfPrefix instanceof FieldDescriptor) {
3246 enclosingDescriptor = ((FieldDescriptor) lastDescOfPrefix).getType().getClassDesc();
3247 // System.out.println("enclosingDescriptor0=" + enclosingDescriptor);
3249 // var descriptor case
3250 enclosingDescriptor = ((VarDescriptor) lastDescOfPrefix).getType().getClassDesc();
3252 // System.out.println("enclosingDescriptor=" + enclosingDescriptor);
3254 LocationDescriptor newLocDescriptor = generateNewLocationDescriptor();
3255 newLocDescriptor.setEnclosingClassDesc(enclosingDescriptor);
3257 Location newLoc = new Location(enclosingDescriptor, newLocDescriptor.getSymbol());
3258 newLoc.setLocDescriptor(newLocDescriptor);
3259 newCompLoc.addLocation(newLoc);
3261 // System.out.println("--newCompLoc=" + newCompLoc);
3265 private List<NTuple<Descriptor>> translatePrefixListToCallee(Descriptor baseRef,
3266 MethodDescriptor mdCallee, List<NTuple<Descriptor>> callerPrefixList) {
3268 List<NTuple<Descriptor>> calleePrefixList = new ArrayList<NTuple<Descriptor>>();
3270 for (int i = 0; i < callerPrefixList.size(); i++) {
3271 NTuple<Descriptor> prefix = callerPrefixList.get(i);
3272 if (prefix.startsWith(baseRef)) {
3273 NTuple<Descriptor> calleePrefix = new NTuple<Descriptor>();
3274 calleePrefix.add(mdCallee.getThis());
3275 for (int k = 1; k < prefix.size(); k++) {
3276 calleePrefix.add(prefix.get(k));
3278 calleePrefixList.add(calleePrefix);
3282 return calleePrefixList;
3286 private List<NTuple<Descriptor>> calculatePrefixList(FlowGraph flowGraph, FlowNode flowNode) {
3288 System.out.println("\n##### calculatePrefixList=" + flowNode);
3290 Set<FlowNode> inNodeSet = flowGraph.getIncomingFlowNodeSet(flowNode);
3291 inNodeSet.add(flowNode);
3293 System.out.println("inNodeSet=" + inNodeSet);
3295 List<NTuple<Descriptor>> prefixList = new ArrayList<NTuple<Descriptor>>();
3297 for (Iterator iterator = inNodeSet.iterator(); iterator.hasNext();) {
3298 FlowNode inNode = (FlowNode) iterator.next();
3300 NTuple<Descriptor> inNodeTuple = inNode.getCurrentDescTuple();
3302 // CompositeLocation inNodeInferredLoc =
3303 // generateInferredCompositeLocation(methodInfo, inNodeTuple);
3304 // NTuple<Location> inNodeInferredLocTuple = inNodeInferredLoc.getTuple();
3306 for (int i = 1; i < inNodeTuple.size(); i++) {
3307 NTuple<Descriptor> prefix = inNodeTuple.subList(0, i);
3308 if (!prefixList.contains(prefix)) {
3309 prefixList.add(prefix);
3314 Collections.sort(prefixList, new Comparator<NTuple<Descriptor>>() {
3315 public int compare(NTuple<Descriptor> arg0, NTuple<Descriptor> arg1) {
3316 int s0 = arg0.size();
3317 int s1 = arg1.size();
3320 } else if (s0 == s1) {
3332 public CompositeLocation convertToCompositeLocation(MethodDescriptor md, NTuple<Descriptor> tuple) {
3334 CompositeLocation compLoc = new CompositeLocation();
3336 Descriptor enclosingDescriptor = md;
3338 for (int i = 0; i < tuple.size(); i++) {
3339 Descriptor curDescriptor = tuple.get(i);
3340 Location locElement = new Location(enclosingDescriptor, curDescriptor.getSymbol());
3341 locElement.setLocDescriptor(curDescriptor);
3342 compLoc.addLocation(locElement);
3344 if (curDescriptor instanceof VarDescriptor) {
3345 enclosingDescriptor = md.getClassDesc();
3346 } else if (curDescriptor instanceof FieldDescriptor) {
3347 enclosingDescriptor = ((FieldDescriptor) curDescriptor).getClassDescriptor();
3348 } else if (curDescriptor instanceof NameDescriptor) {
3349 // it is "GLOBAL LOC" case!
3350 enclosingDescriptor = GLOBALDESC;
3352 enclosingDescriptor = null;
3360 private LocationDescriptor generateNewLocationDescriptor() {
3361 return new LocationDescriptor("Loc" + (locSeed++));
3364 private int getPrefixIndex(NTuple<Descriptor> tuple1, NTuple<Descriptor> tuple2) {
3366 // return the index where the prefix shared by tuple1 and tuple2 is ended
3367 // if there is no prefix shared by both of them, return -1
3369 int minSize = tuple1.size();
3370 if (minSize > tuple2.size()) {
3371 minSize = tuple2.size();
3375 for (int i = 0; i < minSize; i++) {
3376 if (!tuple1.get(i).equals(tuple2.get(i))) {
3386 private CompositeLocation generateInferredCompositeLocation(MethodLocationInfo methodInfo,
3387 NTuple<Location> tuple) {
3389 // first, retrieve inferred location by the local var descriptor
3390 CompositeLocation inferLoc = new CompositeLocation();
3392 CompositeLocation localVarInferLoc =
3393 methodInfo.getInferLocation(tuple.get(0).getLocDescriptor());
3395 localVarInferLoc.get(0).setLocDescriptor(tuple.get(0).getLocDescriptor());
3397 for (int i = 0; i < localVarInferLoc.getSize(); i++) {
3398 inferLoc.addLocation(localVarInferLoc.get(i));
3401 for (int i = 1; i < tuple.size(); i++) {
3402 Location cur = tuple.get(i);
3403 Descriptor enclosingDesc = cur.getDescriptor();
3404 Descriptor curDesc = cur.getLocDescriptor();
3406 Location inferLocElement;
3407 if (curDesc == null) {
3408 // in this case, we have a newly generated location.
3409 inferLocElement = new Location(enclosingDesc, cur.getLocIdentifier());
3411 String fieldLocSymbol =
3412 getLocationInfo(enclosingDesc).getInferLocation(curDesc).get(0).getLocIdentifier();
3413 inferLocElement = new Location(enclosingDesc, fieldLocSymbol);
3414 inferLocElement.setLocDescriptor(curDesc);
3417 inferLoc.addLocation(inferLocElement);
3421 assert (inferLoc.get(0).getLocDescriptor().getSymbol() == inferLoc.get(0).getLocIdentifier());
3425 public LocationInfo getLocationInfo(Descriptor d) {
3426 if (d instanceof MethodDescriptor) {
3427 return getMethodLocationInfo((MethodDescriptor) d);
3429 return getFieldLocationInfo((ClassDescriptor) d);
3433 private MethodLocationInfo getMethodLocationInfo(MethodDescriptor md) {
3435 if (!mapMethodDescToMethodLocationInfo.containsKey(md)) {
3436 mapMethodDescToMethodLocationInfo.put(md, new MethodLocationInfo(md));
3439 return mapMethodDescToMethodLocationInfo.get(md);
3443 private LocationInfo getFieldLocationInfo(ClassDescriptor cd) {
3445 if (!mapClassToLocationInfo.containsKey(cd)) {
3446 mapClassToLocationInfo.put(cd, new LocationInfo(cd));
3449 return mapClassToLocationInfo.get(cd);
3453 private void addPrefixMapping(Map<NTuple<Location>, Set<NTuple<Location>>> map,
3454 NTuple<Location> prefix, NTuple<Location> element) {
3456 if (!map.containsKey(prefix)) {
3457 map.put(prefix, new HashSet<NTuple<Location>>());
3459 map.get(prefix).add(element);
3462 private boolean containsNonPrimitiveElement(Set<Descriptor> descSet) {
3463 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
3464 Descriptor desc = (Descriptor) iterator.next();
3466 if (desc.equals(LocationInference.GLOBALDESC)) {
3468 } else if (desc instanceof VarDescriptor) {
3469 if (!((VarDescriptor) desc).getType().isPrimitive()) {
3472 } else if (desc instanceof FieldDescriptor) {
3473 if (!((FieldDescriptor) desc).getType().isPrimitive()) {
3482 private SSJavaLattice<String> getLattice(Descriptor d) {
3483 if (d instanceof MethodDescriptor) {
3484 return getMethodLattice((MethodDescriptor) d);
3486 return getFieldLattice((ClassDescriptor) d);
3490 private SSJavaLattice<String> getMethodLattice(MethodDescriptor md) {
3491 if (!md2lattice.containsKey(md)) {
3492 md2lattice.put(md, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
3494 return md2lattice.get(md);
3497 private void setMethodLattice(MethodDescriptor md, SSJavaLattice<String> lattice) {
3498 md2lattice.put(md, lattice);
3501 private void extractFlowsBetweenFields(ClassDescriptor cd, FlowNode srcNode, FlowNode dstNode,
3504 NTuple<Descriptor> srcCurTuple = srcNode.getCurrentDescTuple();
3505 NTuple<Descriptor> dstCurTuple = dstNode.getCurrentDescTuple();
3507 if (srcCurTuple.get(idx).equals(dstCurTuple.get(idx)) && srcCurTuple.size() > (idx + 1)
3508 && dstCurTuple.size() > (idx + 1)) {
3509 // value flow between fields: we don't need to add a binary relation
3512 Descriptor desc = srcCurTuple.get(idx);
3513 ClassDescriptor classDesc;
3516 classDesc = ((VarDescriptor) desc).getType().getClassDesc();
3518 if (desc instanceof FieldDescriptor) {
3519 classDesc = ((FieldDescriptor) desc).getType().getClassDesc();
3521 // this case is that the local variable has a composite location assignment
3522 // the following element after the composite location to the local variable
3523 // has the enclosing descriptor of the local variable
3524 Descriptor localDesc = srcNode.getDescTuple().get(0);
3525 classDesc = ((VarDescriptor) localDesc).getType().getClassDesc();
3528 extractFlowsBetweenFields(classDesc, srcNode, dstNode, idx + 1);
3532 Descriptor srcFieldDesc = srcCurTuple.get(idx);
3533 Descriptor dstFieldDesc = dstCurTuple.get(idx);
3536 getHierarchyGraph(cd).addEdge(srcFieldDesc, dstFieldDesc);
3542 public SSJavaLattice<String> getFieldLattice(ClassDescriptor cd) {
3543 if (!cd2lattice.containsKey(cd)) {
3544 cd2lattice.put(cd, new SSJavaLattice<String>(SSJavaAnalysis.TOP, SSJavaAnalysis.BOTTOM));
3546 return cd2lattice.get(cd);
3549 public LinkedList<MethodDescriptor> computeMethodList() {
3551 Set<MethodDescriptor> toSort = new HashSet<MethodDescriptor>();
3555 Set<MethodDescriptor> visited = new HashSet<MethodDescriptor>();
3556 Set<MethodDescriptor> reachableCallee = new HashSet<MethodDescriptor>();
3558 while (!toAnalyzeIsEmpty()) {
3559 ClassDescriptor cd = toAnalyzeNext();
3561 setupToAnalazeMethod(cd);
3562 temp_toanalyzeMethodList.removeAll(visited);
3564 while (!toAnalyzeMethodIsEmpty()) {
3565 MethodDescriptor md = toAnalyzeMethodNext();
3566 if ((!visited.contains(md))
3567 && (ssjava.needTobeAnnotated(md) || reachableCallee.contains(md))) {
3569 // creates a mapping from a method descriptor to virtual methods
3570 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
3571 if (md.isStatic()) {
3572 setPossibleCallees.add(md);
3574 setPossibleCallees.addAll(ssjava.getCallGraph().getMethods(md));
3577 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getCalleeSet(md);
3578 Set<MethodDescriptor> needToAnalyzeCalleeSet = new HashSet<MethodDescriptor>();
3580 for (Iterator iterator = calleeSet.iterator(); iterator.hasNext();) {
3581 MethodDescriptor calleemd = (MethodDescriptor) iterator.next();
3582 if ((!ssjava.isTrustMethod(calleemd))
3583 && (!ssjava.isSSJavaUtil(calleemd.getClassDesc()))) {
3584 if (!visited.contains(calleemd)) {
3585 temp_toanalyzeMethodList.add(calleemd);
3587 reachableCallee.add(calleemd);
3588 needToAnalyzeCalleeSet.add(calleemd);
3592 mapMethodToCalleeSet.put(md, needToAnalyzeCalleeSet);
3601 return ssjava.topologicalSort(toSort);
3605 public boolean isTransitivelyCalledFrom(MethodDescriptor callee, MethodDescriptor caller) {
3606 // if the callee is transitively invoked from the caller
3609 int callerIdx = toanalyze_methodDescList.indexOf(caller);
3610 int calleeIdx = toanalyze_methodDescList.indexOf(callee);
3612 if (callerIdx < calleeIdx) {
3620 public void constructFlowGraph() {
3622 System.out.println("");
3623 toanalyze_methodDescList = computeMethodList();
3625 LinkedList<MethodDescriptor> methodDescList =
3626 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
3628 System.out.println("@@@methodDescList=" + methodDescList);
3631 while (!methodDescList.isEmpty()) {
3632 MethodDescriptor md = methodDescList.removeLast();
3633 if (state.SSJAVADEBUG) {
3634 System.out.println();
3635 System.out.println("SSJAVA: Constructing a flow graph: " + md);
3637 // creates a mapping from a parameter descriptor to its index
3638 Map<Descriptor, Integer> mapParamDescToIdx = new HashMap<Descriptor, Integer>();
3640 if (!md.isStatic()) {
3642 mapParamDescToIdx.put(md.getThis(), 0);
3645 for (int i = 0; i < md.numParameters(); i++) {
3646 Descriptor paramDesc = (Descriptor) md.getParameter(i);
3647 mapParamDescToIdx.put(paramDesc, new Integer(i + offset));
3650 FlowGraph fg = new FlowGraph(md, mapParamDescToIdx);
3651 mapMethodDescriptorToFlowGraph.put(md, fg);
3653 analyzeMethodBody(md.getClassDesc(), md);
3655 // System.out.println("##constructSubGlobalFlowGraph");
3656 // GlobalFlowGraph subGlobalFlowGraph = constructSubGlobalFlowGraph(fg);
3657 // mapMethodDescriptorToSubGlobalFlowGraph.put(md, subGlobalFlowGraph);
3660 // System.out.println("##addValueFlowsFromCalleeSubGlobalFlowGraph");
3661 // addValueFlowsFromCalleeSubGlobalFlowGraph(md, subGlobalFlowGraph);
3662 // subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
3664 // propagateFlowsFromCalleesWithNoCompositeLocation(md);
3668 // _debug_printGraph();
3672 private void constructGlobalFlowGraph() {
3673 LinkedList<MethodDescriptor> methodDescList =
3674 (LinkedList<MethodDescriptor>) toanalyze_methodDescList.clone();
3676 while (!methodDescList.isEmpty()) {
3677 MethodDescriptor md = methodDescList.removeLast();
3678 if (state.SSJAVADEBUG) {
3679 System.out.println();
3680 System.out.println("SSJAVA: Constructing a sub global flow graph: " + md);
3682 GlobalFlowGraph subGlobalFlowGraph = constructSubGlobalFlowGraph(getFlowGraph(md));
3685 System.out.println("-add Value Flows From CalleeSubGlobalFlowGraph");
3686 addValueFlowsFromCalleeSubGlobalFlowGraph(md, subGlobalFlowGraph);
3687 // subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
3689 // System.out.println("-propagate Flows From Callees With No CompositeLocation");
3690 // propagateFlowsFromCalleesWithNoCompositeLocation(md);
3696 private Set<MethodInvokeNode> getMethodInvokeNodeSet(MethodDescriptor md) {
3697 if (!mapMethodDescriptorToMethodInvokeNodeSet.containsKey(md)) {
3698 mapMethodDescriptorToMethodInvokeNodeSet.put(md, new HashSet<MethodInvokeNode>());
3700 return mapMethodDescriptorToMethodInvokeNodeSet.get(md);
3703 private void propagateFlowsFromCalleesWithNoCompositeLocation(MethodDescriptor mdCaller) {
3705 // the transformation for a call site propagates flows through parameters
3706 // if the method is virtual, it also grab all relations from any possible
3709 Set<MethodInvokeNode> setMethodInvokeNode =
3710 mapMethodDescriptorToMethodInvokeNodeSet.get(mdCaller);
3712 if (setMethodInvokeNode != null) {
3714 for (Iterator iterator = setMethodInvokeNode.iterator(); iterator.hasNext();) {
3715 MethodInvokeNode min = (MethodInvokeNode) iterator.next();
3716 MethodDescriptor mdCallee = min.getMethod();
3717 Set<MethodDescriptor> setPossibleCallees = new HashSet<MethodDescriptor>();
3718 if (mdCallee.isStatic()) {
3719 setPossibleCallees.add(mdCallee);
3721 Set<MethodDescriptor> calleeSet = ssjava.getCallGraph().getMethods(mdCallee);
3722 // removes method descriptors that are not invoked by the caller
3723 calleeSet.retainAll(mapMethodToCalleeSet.get(mdCaller));
3724 setPossibleCallees.addAll(calleeSet);
3727 for (Iterator iterator2 = setPossibleCallees.iterator(); iterator2.hasNext();) {
3728 MethodDescriptor possibleMdCallee = (MethodDescriptor) iterator2.next();
3729 propagateFlowsToCallerWithNoCompositeLocation(min, mdCaller, possibleMdCallee);
3737 private void analyzeMethodBody(ClassDescriptor cd, MethodDescriptor md) {
3738 BlockNode bn = state.getMethodBody(md);
3739 NodeTupleSet implicitFlowTupleSet = new NodeTupleSet();
3740 analyzeFlowBlockNode(md, md.getParameterTable(), bn, implicitFlowTupleSet);
3743 private void analyzeFlowBlockNode(MethodDescriptor md, SymbolTable nametable, BlockNode bn,
3744 NodeTupleSet implicitFlowTupleSet) {
3746 bn.getVarTable().setParent(nametable);
3747 for (int i = 0; i < bn.size(); i++) {
3748 BlockStatementNode bsn = bn.get(i);
3749 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
3754 private void analyzeBlockStatementNode(MethodDescriptor md, SymbolTable nametable,
3755 BlockStatementNode bsn, NodeTupleSet implicitFlowTupleSet) {
3757 switch (bsn.kind()) {
3758 case Kind.BlockExpressionNode:
3759 analyzeBlockExpressionNode(md, nametable, (BlockExpressionNode) bsn, implicitFlowTupleSet);
3762 case Kind.DeclarationNode:
3763 analyzeFlowDeclarationNode(md, nametable, (DeclarationNode) bsn, implicitFlowTupleSet);
3766 case Kind.IfStatementNode:
3767 analyzeFlowIfStatementNode(md, nametable, (IfStatementNode) bsn, implicitFlowTupleSet);
3771 analyzeFlowLoopNode(md, nametable, (LoopNode) bsn, implicitFlowTupleSet);
3774 case Kind.ReturnNode:
3775 analyzeFlowReturnNode(md, nametable, (ReturnNode) bsn, implicitFlowTupleSet);
3778 case Kind.SubBlockNode:
3779 analyzeFlowSubBlockNode(md, nametable, (SubBlockNode) bsn, implicitFlowTupleSet);
3782 case Kind.ContinueBreakNode:
3785 case Kind.SwitchStatementNode:
3786 analyzeSwitchStatementNode(md, nametable, (SwitchStatementNode) bsn, implicitFlowTupleSet);
3793 private void analyzeSwitchBlockNode(MethodDescriptor md, SymbolTable nametable,
3794 SwitchBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
3796 analyzeFlowBlockNode(md, nametable, sbn.getSwitchBlockStatement(), implicitFlowTupleSet);
3800 private void analyzeSwitchStatementNode(MethodDescriptor md, SymbolTable nametable,
3801 SwitchStatementNode ssn, NodeTupleSet implicitFlowTupleSet) {
3803 NodeTupleSet condTupleNode = new NodeTupleSet();
3804 analyzeFlowExpressionNode(md, nametable, ssn.getCondition(), condTupleNode, null,
3805 implicitFlowTupleSet, false);
3807 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
3809 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
3810 newImplicitTupleSet.addTupleSet(condTupleNode);
3812 if (needToGenerateInterLoc(newImplicitTupleSet)) {
3813 // need to create an intermediate node for the GLB of conditional
3814 // locations & implicit flows
3816 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
3817 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
3818 NTuple<Descriptor> tuple = idxIter.next();
3819 addFlowGraphEdge(md, tuple, interTuple);
3821 newImplicitTupleSet.clear();
3822 newImplicitTupleSet.addTuple(interTuple);
3825 BlockNode sbn = ssn.getSwitchBody();
3826 for (int i = 0; i < sbn.size(); i++) {
3827 analyzeSwitchBlockNode(md, nametable, (SwitchBlockNode) sbn.get(i), newImplicitTupleSet);
3832 private void analyzeFlowSubBlockNode(MethodDescriptor md, SymbolTable nametable,
3833 SubBlockNode sbn, NodeTupleSet implicitFlowTupleSet) {
3834 analyzeFlowBlockNode(md, nametable, sbn.getBlockNode(), implicitFlowTupleSet);
3837 private void analyzeFlowReturnNode(MethodDescriptor md, SymbolTable nametable, ReturnNode rn,
3838 NodeTupleSet implicitFlowTupleSet) {
3840 // System.out.println("-analyzeFlowReturnNode=" + rn.printNode(0));
3841 ExpressionNode returnExp = rn.getReturnExpression();
3843 if (returnExp != null) {
3844 NodeTupleSet nodeSet = new NodeTupleSet();
3845 // if a return expression returns a literal value, nodeSet is empty
3846 analyzeFlowExpressionNode(md, nametable, returnExp, nodeSet, false);
3847 FlowGraph fg = getFlowGraph(md);
3849 // if (implicitFlowTupleSet.size() == 1
3851 // fg.getFlowNode(implicitFlowTupleSet.iterator().next()).isIntermediate())
3854 // // since there is already an intermediate node for the GLB of implicit
3856 // // we don't need to create another intermediate node.
3857 // // just re-use the intermediate node for implicit flows.
3859 // FlowNode meetNode =
3860 // fg.getFlowNode(implicitFlowTupleSet.iterator().next());
3862 // for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
3863 // NTuple<Descriptor> returnNodeTuple = (NTuple<Descriptor>)
3865 // fg.addValueFlowEdge(returnNodeTuple, meetNode.getDescTuple());
3870 NodeTupleSet currentFlowTupleSet = new NodeTupleSet();
3872 // add tuples from return node
3873 currentFlowTupleSet.addTupleSet(nodeSet);
3875 // add tuples corresponding to the current implicit flows
3876 currentFlowTupleSet.addTupleSet(implicitFlowTupleSet);
3878 // System.out.println("---currentFlowTupleSet=" + currentFlowTupleSet);
3880 if (needToGenerateInterLoc(currentFlowTupleSet)) {
3881 FlowNode meetNode = fg.createIntermediateNode();
3882 for (Iterator iterator = currentFlowTupleSet.iterator(); iterator.hasNext();) {
3883 NTuple<Descriptor> currentFlowTuple = (NTuple<Descriptor>) iterator.next();
3884 fg.addValueFlowEdge(currentFlowTuple, meetNode.getDescTuple());
3886 fg.addReturnFlowNode(meetNode.getDescTuple());
3888 // currentFlowTupleSet = removeLiteralTuple(currentFlowTupleSet);
3889 for (Iterator iterator = currentFlowTupleSet.iterator(); iterator.hasNext();) {
3890 NTuple<Descriptor> currentFlowTuple = (NTuple<Descriptor>) iterator.next();
3891 fg.addReturnFlowNode(currentFlowTuple);
3899 private NodeTupleSet removeLiteralTuple(NodeTupleSet inSet) {
3900 NodeTupleSet tupleSet = new NodeTupleSet();
3901 for (Iterator<NTuple<Descriptor>> iter = inSet.iterator(); iter.hasNext();) {
3902 NTuple<Descriptor> tuple = iter.next();
3903 if (!tuple.get(0).equals(LITERALDESC)) {
3904 tupleSet.addTuple(tuple);
3910 private boolean needToGenerateInterLoc(NodeTupleSet tupleSet) {
3912 for (Iterator<NTuple<Descriptor>> iter = tupleSet.iterator(); iter.hasNext();) {
3913 NTuple<Descriptor> descTuple = iter.next();
3914 if (!descTuple.get(0).equals(LITERALDESC)) {
3925 private void analyzeFlowLoopNode(MethodDescriptor md, SymbolTable nametable, LoopNode ln,
3926 NodeTupleSet implicitFlowTupleSet) {
3928 if (ln.getType() == LoopNode.WHILELOOP || ln.getType() == LoopNode.DOWHILELOOP) {
3930 NodeTupleSet condTupleNode = new NodeTupleSet();
3931 analyzeFlowExpressionNode(md, nametable, ln.getCondition(), condTupleNode, null,
3932 implicitFlowTupleSet, false);
3934 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
3936 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
3937 newImplicitTupleSet.addTupleSet(condTupleNode);
3939 if (needToGenerateInterLoc(newImplicitTupleSet)) {
3940 // need to create an intermediate node for the GLB of conditional
3941 // locations & implicit flows
3943 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
3944 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter
3946 NTuple<Descriptor> tuple = idxIter.next();
3947 addFlowGraphEdge(md, tuple, interTuple);
3949 newImplicitTupleSet.clear();
3950 newImplicitTupleSet.addTuple(interTuple);
3955 // System.out.println("condTupleNode="+condTupleNode);
3956 // NTuple<Descriptor> interTuple =
3957 // getFlowGraph(md).createIntermediateNode().getDescTuple();
3959 // for (Iterator<NTuple<Descriptor>> idxIter = condTupleNode.iterator();
3960 // idxIter.hasNext();) {
3961 // NTuple<Descriptor> tuple = idxIter.next();
3962 // addFlowGraphEdge(md, tuple, interTuple);
3965 // for (Iterator<NTuple<Descriptor>> idxIter =
3966 // implicitFlowTupleSet.iterator(); idxIter
3968 // NTuple<Descriptor> tuple = idxIter.next();
3969 // addFlowGraphEdge(md, tuple, interTuple);
3972 // NodeTupleSet newImplicitSet = new NodeTupleSet();
3973 // newImplicitSet.addTuple(interTuple);
3974 analyzeFlowBlockNode(md, nametable, ln.getBody(), newImplicitTupleSet);
3977 // condTupleNode.addTupleSet(implicitFlowTupleSet);
3979 // add edges from condNodeTupleSet to all nodes of conditional nodes
3980 // analyzeFlowBlockNode(md, nametable, ln.getBody(), condTupleNode);
3983 // check 'for loop' case
3984 BlockNode bn = ln.getInitializer();
3985 bn.getVarTable().setParent(nametable);
3986 for (int i = 0; i < bn.size(); i++) {
3987 BlockStatementNode bsn = bn.get(i);
3988 analyzeBlockStatementNode(md, bn.getVarTable(), bsn, implicitFlowTupleSet);
3991 NodeTupleSet condTupleNode = new NodeTupleSet();
3992 analyzeFlowExpressionNode(md, bn.getVarTable(), ln.getCondition(), condTupleNode, null,
3993 implicitFlowTupleSet, false);
3995 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
3996 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
3997 newImplicitTupleSet.addTupleSet(condTupleNode);
3999 if (needToGenerateInterLoc(newImplicitTupleSet)) {
4000 // need to create an intermediate node for the GLB of conditional
4001 // locations & implicit flows
4003 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4004 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter
4006 NTuple<Descriptor> tuple = idxIter.next();
4007 addFlowGraphEdge(md, tuple, interTuple);
4009 newImplicitTupleSet.clear();
4010 newImplicitTupleSet.addTuple(interTuple);
4015 // NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4017 // for (Iterator<NTuple<Descriptor>> idxIter = condTupleNode.iterator(); idxIter.hasNext();) {
4018 // NTuple<Descriptor> tuple = idxIter.next();
4019 // addFlowGraphEdge(md, tuple, interTuple);
4022 // for (Iterator<NTuple<Descriptor>> idxIter = implicitFlowTupleSet.iterator(); idxIter
4024 // NTuple<Descriptor> tuple = idxIter.next();
4025 // addFlowGraphEdge(md, tuple, interTuple);
4028 // NodeTupleSet newImplicitSet = new NodeTupleSet();
4029 // newImplicitSet.addTuple(interTuple);
4030 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(), newImplicitTupleSet);
4031 analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(), newImplicitTupleSet);
4034 // condTupleNode.addTupleSet(implicitFlowTupleSet);
4036 // analyzeFlowBlockNode(md, bn.getVarTable(), ln.getUpdate(),
4038 // analyzeFlowBlockNode(md, bn.getVarTable(), ln.getBody(),
4045 private void analyzeFlowIfStatementNode(MethodDescriptor md, SymbolTable nametable,
4046 IfStatementNode isn, NodeTupleSet implicitFlowTupleSet) {
4048 // System.out.println("analyzeFlowIfStatementNode=" + isn.printNode(0));
4050 NodeTupleSet condTupleNode = new NodeTupleSet();
4051 analyzeFlowExpressionNode(md, nametable, isn.getCondition(), condTupleNode, null,
4052 implicitFlowTupleSet, false);
4054 NodeTupleSet newImplicitTupleSet = new NodeTupleSet();
4056 newImplicitTupleSet.addTupleSet(implicitFlowTupleSet);
4057 newImplicitTupleSet.addTupleSet(condTupleNode);
4059 System.out.println("$$$GGGcondTupleNode=" + condTupleNode.getGlobalLocTupleSet());
4061 // System.out.println("-condTupleNode=" + condTupleNode);
4062 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
4063 // System.out.println("-newImplicitTupleSet=" + newImplicitTupleSet);
4065 if (needToGenerateInterLoc(newImplicitTupleSet)) {
4066 System.out.println("2");
4067 // need to create an intermediate node for the GLB of conditional locations & implicit flows
4068 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4069 for (Iterator<NTuple<Descriptor>> idxIter = newImplicitTupleSet.iterator(); idxIter.hasNext();) {
4070 NTuple<Descriptor> tuple = idxIter.next();
4071 addFlowGraphEdge(md, tuple, interTuple);
4073 newImplicitTupleSet.clear();
4074 newImplicitTupleSet.addTuple(interTuple);
4077 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
4078 for (Iterator<NTuple<Location>> iterator = condTupleNode.globalIterator(); iterator.hasNext();) {
4079 NTuple<Location> calleeReturnLocTuple = iterator.next();
4080 for (Iterator<NTuple<Descriptor>> iter2 = newImplicitTupleSet.iterator(); iter2.hasNext();) {
4081 NTuple<Descriptor> callerImplicitTuple = iter2.next();
4082 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
4083 translateToLocTuple(md, callerImplicitTuple));
4087 analyzeFlowBlockNode(md, nametable, isn.getTrueBlock(), newImplicitTupleSet);
4089 if (isn.getFalseBlock() != null) {
4090 analyzeFlowBlockNode(md, nametable, isn.getFalseBlock(), newImplicitTupleSet);
4095 private void analyzeFlowDeclarationNode(MethodDescriptor md, SymbolTable nametable,
4096 DeclarationNode dn, NodeTupleSet implicitFlowTupleSet) {
4098 VarDescriptor vd = dn.getVarDescriptor();
4099 mapDescToDefinitionLine.put(vd, dn.getNumLine());
4100 NTuple<Descriptor> tupleLHS = new NTuple<Descriptor>();
4102 FlowNode fn = getFlowGraph(md).createNewFlowNode(tupleLHS);
4103 fn.setDeclarationNode();
4105 if (dn.getExpression() != null) {
4107 NodeTupleSet nodeSetRHS = new NodeTupleSet();
4108 analyzeFlowExpressionNode(md, nametable, dn.getExpression(), nodeSetRHS, null,
4109 implicitFlowTupleSet, false);
4111 // creates edges from RHS to LHS
4112 NTuple<Descriptor> interTuple = null;
4113 if (needToGenerateInterLoc(nodeSetRHS)) {
4115 interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4118 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
4119 NTuple<Descriptor> fromTuple = iter.next();
4120 addFlowGraphEdge(md, fromTuple, interTuple, tupleLHS);
4123 // creates edges from implicitFlowTupleSet to LHS
4124 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
4125 NTuple<Descriptor> implicitTuple = iter.next();
4126 addFlowGraphEdge(md, implicitTuple, tupleLHS);
4129 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
4130 for (Iterator<NTuple<Location>> iterator = nodeSetRHS.globalIterator(); iterator.hasNext();) {
4131 NTuple<Location> calleeReturnLocTuple = iterator.next();
4132 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple, translateToLocTuple(md, tupleLHS));
4139 private void analyzeBlockExpressionNode(MethodDescriptor md, SymbolTable nametable,
4140 BlockExpressionNode ben, NodeTupleSet implicitFlowTupleSet) {
4141 analyzeFlowExpressionNode(md, nametable, ben.getExpression(), null, null, implicitFlowTupleSet,
4145 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
4146 ExpressionNode en, NodeTupleSet nodeSet, boolean isLHS) {
4147 return analyzeFlowExpressionNode(md, nametable, en, nodeSet, null, new NodeTupleSet(), isLHS);
4150 private NTuple<Descriptor> analyzeFlowExpressionNode(MethodDescriptor md, SymbolTable nametable,
4151 ExpressionNode en, NodeTupleSet nodeSet, NTuple<Descriptor> base,
4152 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
4154 // note that expression node can create more than one flow node
4155 // nodeSet contains of flow nodes
4156 // base is always assigned to null except the case of a name node!
4157 System.out.println("en=" + en.printNode(0));
4158 NTuple<Descriptor> flowTuple;
4160 switch (en.kind()) {
4162 case Kind.AssignmentNode:
4163 analyzeFlowAssignmentNode(md, nametable, (AssignmentNode) en, nodeSet, base,
4164 implicitFlowTupleSet);
4167 case Kind.FieldAccessNode:
4169 analyzeFlowFieldAccessNode(md, nametable, (FieldAccessNode) en, nodeSet, base,
4170 implicitFlowTupleSet, isLHS);
4171 if (flowTuple != null) {
4172 nodeSet.addTuple(flowTuple);
4177 NodeTupleSet nameNodeSet = new NodeTupleSet();
4179 analyzeFlowNameNode(md, nametable, (NameNode) en, nameNodeSet, base, implicitFlowTupleSet);
4180 if (flowTuple != null) {
4181 nodeSet.addTuple(flowTuple);
4186 analyzeFlowOpNode(md, nametable, (OpNode) en, nodeSet, implicitFlowTupleSet);
4189 case Kind.CreateObjectNode:
4190 analyzeCreateObjectNode(md, nametable, (CreateObjectNode) en);
4193 case Kind.ArrayAccessNode:
4194 analyzeFlowArrayAccessNode(md, nametable, (ArrayAccessNode) en, nodeSet, isLHS);
4197 case Kind.LiteralNode:
4198 analyzeFlowLiteralNode(md, nametable, (LiteralNode) en, nodeSet);
4201 case Kind.MethodInvokeNode:
4202 analyzeFlowMethodInvokeNode(md, nametable, (MethodInvokeNode) en, nodeSet,
4203 implicitFlowTupleSet);
4206 case Kind.TertiaryNode:
4207 analyzeFlowTertiaryNode(md, nametable, (TertiaryNode) en, nodeSet, implicitFlowTupleSet);
4211 analyzeFlowCastNode(md, nametable, (CastNode) en, nodeSet, base, implicitFlowTupleSet);
4213 // case Kind.InstanceOfNode:
4214 // checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
4217 // case Kind.ArrayInitializerNode:
4218 // checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en,
4222 // case Kind.ClassTypeNode:
4223 // checkClassTypeNode(md, nametable, (ClassTypeNode) en, td);
4226 // case Kind.OffsetNode:
4227 // checkOffsetNode(md, nametable, (OffsetNode)en, td);
4231 if (nodeSet != null) {
4232 System.out.println("#EXP nodeGLOBALSet=" + nodeSet.getGlobalLocTupleSet());
4238 private void analyzeFlowCastNode(MethodDescriptor md, SymbolTable nametable, CastNode cn,
4239 NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
4241 analyzeFlowExpressionNode(md, nametable, cn.getExpression(), nodeSet, base,
4242 implicitFlowTupleSet, false);
4246 private void analyzeFlowTertiaryNode(MethodDescriptor md, SymbolTable nametable, TertiaryNode tn,
4247 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
4249 NodeTupleSet tertiaryTupleNode = new NodeTupleSet();
4250 analyzeFlowExpressionNode(md, nametable, tn.getCond(), tertiaryTupleNode, null,
4251 implicitFlowTupleSet, false);
4253 // add edges from tertiaryTupleNode to all nodes of conditional nodes
4254 tertiaryTupleNode.addTupleSet(implicitFlowTupleSet);
4255 analyzeFlowExpressionNode(md, nametable, tn.getTrueExpr(), tertiaryTupleNode, null,
4256 implicitFlowTupleSet, false);
4258 analyzeFlowExpressionNode(md, nametable, tn.getFalseExpr(), tertiaryTupleNode, null,
4259 implicitFlowTupleSet, false);
4261 nodeSet.addTupleSet(tertiaryTupleNode);
4265 private void addMapCallerMethodDescToMethodInvokeNodeSet(MethodDescriptor caller,
4266 MethodInvokeNode min) {
4267 Set<MethodInvokeNode> set = mapMethodDescriptorToMethodInvokeNodeSet.get(caller);
4269 set = new HashSet<MethodInvokeNode>();
4270 mapMethodDescriptorToMethodInvokeNodeSet.put(caller, set);
4275 private void addParamNodeFlowingToReturnValue(MethodDescriptor md, FlowNode fn) {
4277 if (!mapMethodDescToParamNodeFlowsToReturnValue.containsKey(md)) {
4278 mapMethodDescToParamNodeFlowsToReturnValue.put(md, new HashSet<FlowNode>());
4280 mapMethodDescToParamNodeFlowsToReturnValue.get(md).add(fn);
4283 private Set<FlowNode> getParamNodeFlowingToReturnValue(MethodDescriptor md) {
4285 if (!mapMethodDescToParamNodeFlowsToReturnValue.containsKey(md)) {
4286 mapMethodDescToParamNodeFlowsToReturnValue.put(md, new HashSet<FlowNode>());
4289 return mapMethodDescToParamNodeFlowsToReturnValue.get(md);
4292 private Set<NTuple<Location>> getPCLocTupleSet(MethodInvokeNode min) {
4293 if (!mapMethodInvokeNodeToPCLocTupleSet.containsKey(min)) {
4294 mapMethodInvokeNodeToPCLocTupleSet.put(min, new HashSet<NTuple<Location>>());
4296 return mapMethodInvokeNodeToPCLocTupleSet.get(min);
4299 private void analyzeFlowMethodInvokeNode(MethodDescriptor mdCaller, SymbolTable nametable,
4300 MethodInvokeNode min, NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
4302 System.out.println("analyzeFlowMethodInvokeNode=" + min.printNode(0));
4304 Set<NTuple<Location>> pcLocTupleSet = getPCLocTupleSet(min);
4305 for (Iterator iterator = implicitFlowTupleSet.iterator(); iterator.hasNext();) {
4306 NTuple<Descriptor> pcDescTuple = (NTuple<Descriptor>) iterator.next();
4307 if (!pcDescTuple.get(0).equals(LITERALDESC)) {
4308 // here we don't need to add the literal value as a PC location
4309 pcLocTupleSet.add(translateToLocTuple(mdCaller, pcDescTuple));
4313 mapMethodInvokeNodeToArgIdxMap.put(min, new HashMap<Integer, NTuple<Descriptor>>());
4315 if (nodeSet == null) {
4316 nodeSet = new NodeTupleSet();
4319 MethodDescriptor mdCallee = min.getMethod();
4321 NameDescriptor baseName = min.getBaseName();
4322 boolean isSystemout = false;
4323 if (baseName != null) {
4324 isSystemout = baseName.getSymbol().equals("System.out");
4327 if (!ssjava.isSSJavaUtil(mdCallee.getClassDesc()) && !ssjava.isTrustMethod(mdCallee)
4330 addMapCallerMethodDescToMethodInvokeNodeSet(mdCaller, min);
4332 FlowGraph calleeFlowGraph = getFlowGraph(mdCallee);
4333 System.out.println("mdCallee=" + mdCallee);
4334 Set<FlowNode> calleeReturnSet = calleeFlowGraph.getReturnNodeSet();
4336 System.out.println("---calleeReturnSet=" + calleeReturnSet);
4338 // when generating the global flow graph,
4339 // we need to add ordering relations from the set of callee return loc tuple to LHS of the
4340 // caller assignment
4341 for (Iterator iterator = calleeReturnSet.iterator(); iterator.hasNext();) {
4342 FlowNode calleeReturnNode = (FlowNode) iterator.next();
4343 NTuple<Location> calleeReturnLocTuple =
4344 translateToLocTuple(mdCallee, calleeReturnNode.getDescTuple());
4345 nodeSet.addGlobalFlowTuple(calleeReturnLocTuple);
4348 NodeTupleSet tupleSet = new NodeTupleSet();
4350 if (min.getExpression() != null) {
4352 NodeTupleSet baseNodeSet = new NodeTupleSet();
4353 analyzeFlowExpressionNode(mdCaller, nametable, min.getExpression(), baseNodeSet, null,
4354 implicitFlowTupleSet, false);
4356 assert (baseNodeSet.size() == 1);
4357 NTuple<Descriptor> baseTuple = baseNodeSet.iterator().next();
4358 mapMethodInvokeNodeToBaseTuple.put(min, baseTuple);
4360 if (!min.getMethod().isStatic()) {
4361 addArgIdxMap(min, 0, baseTuple);
4363 for (Iterator iterator = calleeReturnSet.iterator(); iterator.hasNext();) {
4364 FlowNode returnNode = (FlowNode) iterator.next();
4365 NTuple<Descriptor> returnDescTuple = returnNode.getDescTuple();
4366 if (returnDescTuple.startsWith(mdCallee.getThis())) {
4367 // the location type of the return value is started with 'this'
4369 NTuple<Descriptor> inFlowTuple = new NTuple<Descriptor>(baseTuple.getList());
4370 inFlowTuple.addAll(returnDescTuple.subList(1, returnDescTuple.size()));
4371 // nodeSet.addTuple(inFlowTuple);
4372 tupleSet.addTuple(inFlowTuple);
4375 Set<FlowNode> inFlowSet = calleeFlowGraph.getIncomingFlowNodeSet(returnNode);
4376 // System.out.println("inFlowSet=" + inFlowSet + " from retrunNode=" + returnNode);
4377 for (Iterator iterator2 = inFlowSet.iterator(); iterator2.hasNext();) {
4378 FlowNode inFlowNode = (FlowNode) iterator2.next();
4379 if (inFlowNode.getDescTuple().startsWith(mdCallee.getThis())) {
4380 // nodeSet.addTupleSet(baseNodeSet);
4381 tupleSet.addTupleSet(baseNodeSet);
4390 // analyze parameter flows
4392 if (min.numArgs() > 0) {
4395 if (min.getMethod().isStatic()) {
4401 for (int i = 0; i < min.numArgs(); i++) {
4402 ExpressionNode en = min.getArg(i);
4403 int idx = i + offset;
4404 NodeTupleSet argTupleSet = new NodeTupleSet();
4405 analyzeFlowExpressionNode(mdCaller, nametable, en, argTupleSet, false);
4406 // if argument is liternal node, argTuple is set to NULL
4408 NTuple<Descriptor> argTuple = new NTuple<Descriptor>();
4409 if (needToGenerateInterLoc(argTupleSet)) {
4410 NTuple<Descriptor> interTuple =
4411 getFlowGraph(mdCaller).createIntermediateNode().getDescTuple();
4412 for (Iterator<NTuple<Descriptor>> idxIter = argTupleSet.iterator(); idxIter.hasNext();) {
4413 NTuple<Descriptor> tuple = idxIter.next();
4414 addFlowGraphEdge(mdCaller, tuple, interTuple);
4416 argTuple = interTuple;
4417 } else if (argTupleSet.size() == 1) {
4418 argTuple = argTupleSet.iterator().next();
4420 argTuple = new NTuple<Descriptor>();
4423 if (argTuple.size() > 0 && argTuple.get(argTuple.size() - 1).equals(LITERALDESC)) {
4424 argTuple = new NTuple<Descriptor>();
4427 // if an argument is static value
4428 // we need to create an itermediate node so that we could assign a composite location to
4429 // that node if needed
4430 System.out.println("argTuple=" + argTuple);
4431 if (argTuple.size() > 0 && argTuple.get(0).equals(GLOBALDESC)) {
4432 System.out.println("***GLOBAL ARG TUPLE CASE=" + argTuple);
4433 NTuple<Descriptor> interTuple =
4434 getFlowGraph(mdCaller).createIntermediateNode().getDescTuple();
4435 addFlowGraphEdge(mdCaller, argTuple, interTuple);
4436 argTuple = interTuple;
4439 addArgIdxMap(min, idx, argTuple);
4441 FlowNode paramNode = calleeFlowGraph.getParamFlowNode(idx);
4443 // check whether a param node in the callee graph has incoming flows
4444 // if it has incoming flows, the corresponding arg should be lower than the current PC
4445 // Descriptor prefix = paramNode.getDescTuple().get(0);
4446 // if (calleeFlowGraph.getIncomingNodeSetByPrefix(prefix).size() > 0) {
4447 // for (Iterator<NTuple<Descriptor>> iterator = implicitFlowTupleSet.iterator(); iterator
4449 // NTuple<Descriptor> pcTuple = iterator.next();
4450 // System.out.println("add edge pcTuple =" + pcTuple + " -> " + argTuple);
4451 // addFlowGraphEdge(md, pcTuple, argTuple);
4455 if (hasInFlowTo(calleeFlowGraph, paramNode, calleeReturnSet)
4456 || mdCallee.getModifiers().isNative()) {
4457 addParamNodeFlowingToReturnValue(mdCallee, paramNode);
4458 // nodeSet.addTupleSet(argTupleSet);
4459 tupleSet.addTupleSet(argTupleSet);
4465 if (mdCallee.getReturnType() != null && !mdCallee.getReturnType().isVoid()) {
4466 FlowReturnNode setNode = getFlowGraph(mdCaller).createReturnNode(min);
4467 nodeSet.addTuple(setNode.getDescTuple());
4470 // propagateFlowsFromCallee(min, md, min.getMethod());
4472 System.out.println("min nodeSet=" + nodeSet);
4477 private boolean hasInFlowTo(FlowGraph fg, FlowNode inNode, Set<FlowNode> nodeSet) {
4478 // return true if inNode has in-flows to nodeSet
4480 // Set<FlowNode> reachableSet = fg.getReachFlowNodeSetFrom(inNode);
4481 Set<FlowNode> reachableSet = fg.getReachableSetFrom(inNode.getDescTuple());
4482 // System.out.println("inNode=" + inNode + " reachalbeSet=" + reachableSet);
4484 for (Iterator iterator = reachableSet.iterator(); iterator.hasNext();) {
4485 FlowNode fn = (FlowNode) iterator.next();
4486 if (nodeSet.contains(fn)) {
4493 private NTuple<Descriptor> getNodeTupleByArgIdx(MethodInvokeNode min, int idx) {
4494 return mapMethodInvokeNodeToArgIdxMap.get(min).get(new Integer(idx));
4497 private void addArgIdxMap(MethodInvokeNode min, int idx, NTuple<Descriptor> argTuple /*
4501 Map<Integer, NTuple<Descriptor>> mapIdxToTuple = mapMethodInvokeNodeToArgIdxMap.get(min);
4502 if (mapIdxToTuple == null) {
4503 mapIdxToTuple = new HashMap<Integer, NTuple<Descriptor>>();
4504 mapMethodInvokeNodeToArgIdxMap.put(min, mapIdxToTuple);
4506 mapIdxToTuple.put(new Integer(idx), argTuple);
4509 private void analyzeFlowLiteralNode(MethodDescriptor md, SymbolTable nametable, LiteralNode en,
4510 NodeTupleSet nodeSet) {
4511 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
4512 tuple.add(LITERALDESC);
4513 nodeSet.addTuple(tuple);
4516 private void analyzeFlowArrayAccessNode(MethodDescriptor md, SymbolTable nametable,
4517 ArrayAccessNode aan, NodeTupleSet nodeSet, boolean isLHS) {
4519 // System.out.println("analyzeFlowArrayAccessNode aan=" + aan.printNode(0));
4520 String currentArrayAccessNodeExpStr = aan.printNode(0);
4521 arrayAccessNodeStack.push(aan.printNode(0));
4523 // System.out.println("-exp=" + aan.getExpression().printNode(0));
4524 NodeTupleSet expNodeTupleSet = new NodeTupleSet();
4525 NTuple<Descriptor> base =
4526 analyzeFlowExpressionNode(md, nametable, aan.getExpression(), expNodeTupleSet, isLHS);
4528 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
4529 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, isLHS);
4531 arrayAccessNodeStack.pop();
4534 // need to create an edge from idx to array
4535 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
4536 NTuple<Descriptor> idxTuple = idxIter.next();
4537 for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
4538 NTuple<Descriptor> arrTuple = arrIter.next();
4539 getFlowGraph(md).addValueFlowEdge(idxTuple, arrTuple);
4543 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
4544 for (Iterator<NTuple<Location>> iterator = idxNodeTupleSet.globalIterator(); iterator
4546 NTuple<Location> calleeReturnLocTuple = iterator.next();
4547 for (Iterator<NTuple<Descriptor>> arrIter = expNodeTupleSet.iterator(); arrIter.hasNext();) {
4548 NTuple<Descriptor> arrTuple = arrIter.next();
4549 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple, translateToLocTuple(md, arrTuple));
4553 nodeSet.addTupleSet(expNodeTupleSet);
4556 NodeTupleSet nodeSetArrayAccessExp = new NodeTupleSet();
4558 nodeSetArrayAccessExp.addTupleSet(expNodeTupleSet);
4559 nodeSetArrayAccessExp.addTupleSet(idxNodeTupleSet);
4561 if (arrayAccessNodeStack.isEmpty()
4562 || !arrayAccessNodeStack.peek().startsWith(currentArrayAccessNodeExpStr)) {
4564 if (needToGenerateInterLoc(nodeSetArrayAccessExp)) {
4565 NTuple<Descriptor> interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4567 for (Iterator<NTuple<Descriptor>> iter = nodeSetArrayAccessExp.iterator(); iter.hasNext();) {
4568 NTuple<Descriptor> higherTuple = iter.next();
4569 addFlowGraphEdge(md, higherTuple, interTuple);
4571 nodeSetArrayAccessExp.clear();
4572 nodeSetArrayAccessExp.addTuple(interTuple);
4576 nodeSet.addGlobalFlowTupleSet(idxNodeTupleSet.getGlobalLocTupleSet());
4577 nodeSet.addTupleSet(nodeSetArrayAccessExp);
4583 private void analyzeCreateObjectNode(MethodDescriptor md, SymbolTable nametable,
4584 CreateObjectNode en) {
4585 // TODO Auto-generated method stub
4589 private void analyzeFlowOpNode(MethodDescriptor md, SymbolTable nametable, OpNode on,
4590 NodeTupleSet nodeSet, NodeTupleSet implicitFlowTupleSet) {
4592 NodeTupleSet leftOpSet = new NodeTupleSet();
4593 NodeTupleSet rightOpSet = new NodeTupleSet();
4596 analyzeFlowExpressionNode(md, nametable, on.getLeft(), leftOpSet, null, implicitFlowTupleSet,
4599 if (on.getRight() != null) {
4601 analyzeFlowExpressionNode(md, nametable, on.getRight(), rightOpSet, null,
4602 implicitFlowTupleSet, false);
4605 Operation op = on.getOp();
4607 switch (op.getOp()) {
4609 case Operation.UNARYPLUS:
4610 case Operation.UNARYMINUS:
4611 case Operation.LOGIC_NOT:
4613 nodeSet.addTupleSet(leftOpSet);
4616 case Operation.LOGIC_OR:
4617 case Operation.LOGIC_AND:
4618 case Operation.COMP:
4619 case Operation.BIT_OR:
4620 case Operation.BIT_XOR:
4621 case Operation.BIT_AND:
4622 case Operation.ISAVAILABLE:
4623 case Operation.EQUAL:
4624 case Operation.NOTEQUAL:
4631 case Operation.MULT:
4634 case Operation.LEFTSHIFT:
4635 case Operation.RIGHTSHIFT:
4636 case Operation.URIGHTSHIFT:
4638 // there are two operands
4639 nodeSet.addTupleSet(leftOpSet);
4640 nodeSet.addTupleSet(rightOpSet);
4642 nodeSet.addGlobalFlowTupleSet(leftOpSet.getGlobalLocTupleSet());
4643 nodeSet.addGlobalFlowTupleSet(rightOpSet.getGlobalLocTupleSet());
4648 throw new Error(op.toString());
4653 private NTuple<Descriptor> analyzeFlowNameNode(MethodDescriptor md, SymbolTable nametable,
4654 NameNode nn, NodeTupleSet nodeSet, NTuple<Descriptor> base, NodeTupleSet implicitFlowTupleSet) {
4657 base = new NTuple<Descriptor>();
4660 NameDescriptor nd = nn.getName();
4662 if (nd.getBase() != null) {
4664 analyzeFlowExpressionNode(md, nametable, nn.getExpression(), nodeSet, base,
4665 implicitFlowTupleSet, false);
4667 // base node has the top location
4671 String varname = nd.toString();
4672 if (varname.equals("this")) {
4674 base.add(md.getThis());
4678 Descriptor d = (Descriptor) nametable.get(varname);
4680 if (d instanceof VarDescriptor) {
4681 VarDescriptor vd = (VarDescriptor) d;
4683 } else if (d instanceof FieldDescriptor) {
4684 // the type of field descriptor has a location!
4685 FieldDescriptor fd = (FieldDescriptor) d;
4686 if (fd.isStatic()) {
4688 // if it is 'static final', no need to have flow node for the TOP
4692 // if 'static', assign the default GLOBAL LOCATION to the first
4693 // element of the tuple
4694 base.add(GLOBALDESC);
4697 // the location of field access starts from this, followed by field
4699 base.add(md.getThis());
4703 } else if (d == null) {
4704 // access static field
4705 base.add(GLOBALDESC);
4706 base.add(nn.getField());
4709 // FieldDescriptor fd = nn.getField();addFlowGraphEdge
4711 // MethodLattice<String> localLattice = ssjava.getMethodLattice(md);
4712 // String globalLocId = localLattice.getGlobalLoc();
4713 // if (globalLocId == null) {
4715 // Error("Method lattice does not define global variable location at "
4716 // + generateErrorMessage(md.getClassDesc(), nn));
4718 // loc.addLocation(new Location(md, globalLocId));
4720 // Location fieldLoc = (Location) fd.getType().getExtension();
4721 // loc.addLocation(fieldLoc);
4727 getFlowGraph(md).createNewFlowNode(base);
4733 private NTuple<Descriptor> analyzeFlowFieldAccessNode(MethodDescriptor md, SymbolTable nametable,
4734 FieldAccessNode fan, NodeTupleSet nodeSet, NTuple<Descriptor> base,
4735 NodeTupleSet implicitFlowTupleSet, boolean isLHS) {
4736 // System.out.println("analyzeFlowFieldAccessNode=" + fan.printNode(0));
4738 String currentArrayAccessNodeExpStr = null;
4739 ExpressionNode left = fan.getExpression();
4740 TypeDescriptor ltd = left.getType();
4741 FieldDescriptor fd = fan.getField();
4742 ArrayAccessNode aan = null;
4744 String varName = null;
4745 if (left.kind() == Kind.NameNode) {
4746 NameDescriptor nd = ((NameNode) left).getName();
4747 varName = nd.toString();
4750 if (ltd.isClassNameRef() || (varName != null && varName.equals("this"))) {
4751 // using a class name directly or access using this
4752 if (fd.isStatic() && fd.isFinal()) {
4757 NodeTupleSet idxNodeTupleSet = new NodeTupleSet();
4759 boolean isArrayCase = false;
4760 if (left instanceof ArrayAccessNode) {
4763 aan = (ArrayAccessNode) left;
4765 currentArrayAccessNodeExpStr = aan.printNode(0);
4766 arrayAccessNodeStack.push(currentArrayAccessNodeExpStr);
4768 left = aan.getExpression();
4769 analyzeFlowExpressionNode(md, nametable, aan.getIndex(), idxNodeTupleSet, base,
4770 implicitFlowTupleSet, isLHS);
4772 nodeSet.addTupleSet(idxNodeTupleSet);
4775 analyzeFlowExpressionNode(md, nametable, left, nodeSet, base, implicitFlowTupleSet, isLHS);
4778 // in this case, field is TOP location
4782 NTuple<Descriptor> flowFieldTuple = new NTuple<Descriptor>(base.toList());
4784 if (!left.getType().isPrimitive()) {
4785 if (!fd.getSymbol().equals("length")) {
4786 // array.length access, just have the location of the array
4787 flowFieldTuple.add(fd);
4788 nodeSet.removeTuple(base);
4791 getFlowGraph(md).createNewFlowNode(flowFieldTuple);
4794 for (Iterator<NTuple<Descriptor>> idxIter = idxNodeTupleSet.iterator(); idxIter.hasNext();) {
4795 NTuple<Descriptor> idxTuple = idxIter.next();
4796 getFlowGraph(md).addValueFlowEdge(idxTuple, flowFieldTuple);
4799 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
4800 for (Iterator<NTuple<Location>> iterator = idxNodeTupleSet.globalIterator(); iterator
4802 NTuple<Location> calleeReturnLocTuple = iterator.next();
4803 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
4804 translateToLocTuple(md, flowFieldTuple));
4809 // if it is the array case and not the LHS case
4811 arrayAccessNodeStack.pop();
4813 if (arrayAccessNodeStack.isEmpty()
4814 || !arrayAccessNodeStack.peek().startsWith(currentArrayAccessNodeExpStr)) {
4815 NodeTupleSet nodeSetArrayAccessExp = new NodeTupleSet();
4817 nodeSetArrayAccessExp.addTuple(flowFieldTuple);
4818 nodeSetArrayAccessExp.addTupleSet(idxNodeTupleSet);
4820 if (needToGenerateInterLoc(nodeSetArrayAccessExp)) {
4821 NTuple<Descriptor> interTuple =
4822 getFlowGraph(md).createIntermediateNode().getDescTuple();
4824 for (Iterator<NTuple<Descriptor>> iter = nodeSetArrayAccessExp.iterator(); iter
4826 NTuple<Descriptor> higherTuple = iter.next();
4827 addFlowGraphEdge(md, higherTuple, interTuple);
4829 flowFieldTuple = interTuple;
4832 nodeSet.addGlobalFlowTupleSet(idxNodeTupleSet.getGlobalLocTupleSet());
4838 return flowFieldTuple;
4843 private void debug_printTreeNode(TreeNode tn) {
4845 System.out.println("DEBUG: " + tn.printNode(0) + " line#=" + tn.getNumLine());
4849 private void analyzeFlowAssignmentNode(MethodDescriptor md, SymbolTable nametable,
4850 AssignmentNode an, NodeTupleSet nodeSet, NTuple<Descriptor> base,
4851 NodeTupleSet implicitFlowTupleSet) {
4853 NodeTupleSet nodeSetRHS = new NodeTupleSet();
4854 NodeTupleSet nodeSetLHS = new NodeTupleSet();
4856 boolean postinc = true;
4857 if (an.getOperation().getBaseOp() == null
4858 || (an.getOperation().getBaseOp().getOp() != Operation.POSTINC && an.getOperation()
4859 .getBaseOp().getOp() != Operation.POSTDEC)) {
4862 // if LHS is array access node, need to capture value flows between an array
4863 // and its index value
4864 analyzeFlowExpressionNode(md, nametable, an.getDest(), nodeSetLHS, null, implicitFlowTupleSet,
4868 // analyze value flows of rhs expression
4869 analyzeFlowExpressionNode(md, nametable, an.getSrc(), nodeSetRHS, null, implicitFlowTupleSet,
4872 // System.out.println("-analyzeFlowAssignmentNode=" + an.printNode(0));
4873 // System.out.println("-nodeSetLHS=" + nodeSetLHS);
4874 // System.out.println("-nodeSetRHS=" + nodeSetRHS);
4875 // System.out.println("-implicitFlowTupleSet=" + implicitFlowTupleSet);
4876 // System.out.println("-");
4878 if (an.getOperation().getOp() >= 2 && an.getOperation().getOp() <= 12) {
4879 // if assignment contains OP+EQ operator, creates edges from LHS to LHS
4881 for (Iterator<NTuple<Descriptor>> iter = nodeSetLHS.iterator(); iter.hasNext();) {
4882 NTuple<Descriptor> fromTuple = iter.next();
4883 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4884 NTuple<Descriptor> toTuple = iter2.next();
4885 addFlowGraphEdge(md, fromTuple, toTuple);
4890 // creates edges from RHS to LHS
4891 NTuple<Descriptor> interTuple = null;
4892 if (needToGenerateInterLoc(nodeSetRHS)) {
4893 interTuple = getFlowGraph(md).createIntermediateNode().getDescTuple();
4896 for (Iterator<NTuple<Descriptor>> iter = nodeSetRHS.iterator(); iter.hasNext();) {
4897 NTuple<Descriptor> fromTuple = iter.next();
4898 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4899 NTuple<Descriptor> toTuple = iter2.next();
4900 addFlowGraphEdge(md, fromTuple, interTuple, toTuple);
4904 // creates edges from implicitFlowTupleSet to LHS
4905 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
4906 NTuple<Descriptor> fromTuple = iter.next();
4907 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4908 NTuple<Descriptor> toTuple = iter2.next();
4909 addFlowGraphEdge(md, fromTuple, toTuple);
4913 // create global flow edges if the callee gives return value flows to the caller
4914 if (nodeSetRHS.globalLocTupleSize() > 0) {
4915 GlobalFlowGraph globalFlowGraph = getSubGlobalFlowGraph(md);
4916 for (Iterator<NTuple<Location>> iterator = nodeSetRHS.globalIterator(); iterator.hasNext();) {
4917 NTuple<Location> calleeReturnLocTuple = iterator.next();
4918 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4919 NTuple<Descriptor> callerLHSTuple = iter2.next();
4920 globalFlowGraph.addValueFlowEdge(calleeReturnLocTuple,
4921 translateToLocTuple(md, callerLHSTuple));
4922 System.out.println("$$$ GLOBAL FLOW ADD=" + calleeReturnLocTuple + " -> "
4923 + translateToLocTuple(md, callerLHSTuple));
4931 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4932 NTuple<Descriptor> tuple = iter2.next();
4933 addFlowGraphEdge(md, tuple, tuple);
4936 // creates edges from implicitFlowTupleSet to LHS
4937 for (Iterator<NTuple<Descriptor>> iter = implicitFlowTupleSet.iterator(); iter.hasNext();) {
4938 NTuple<Descriptor> fromTuple = iter.next();
4939 for (Iterator<NTuple<Descriptor>> iter2 = nodeSetLHS.iterator(); iter2.hasNext();) {
4940 NTuple<Descriptor> toTuple = iter2.next();
4941 addFlowGraphEdge(md, fromTuple, toTuple);
4947 if (nodeSet != null) {
4948 nodeSet.addTupleSet(nodeSetLHS);
4952 public FlowGraph getFlowGraph(MethodDescriptor md) {
4953 return mapMethodDescriptorToFlowGraph.get(md);
4956 private boolean addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
4957 NTuple<Descriptor> to) {
4958 FlowGraph graph = getFlowGraph(md);
4959 graph.addValueFlowEdge(from, to);
4963 private void addFlowGraphEdge(MethodDescriptor md, NTuple<Descriptor> from,
4964 NTuple<Descriptor> inter, NTuple<Descriptor> to) {
4966 FlowGraph graph = getFlowGraph(md);
4968 if (inter != null) {
4969 graph.addValueFlowEdge(from, inter);
4970 graph.addValueFlowEdge(inter, to);
4972 graph.addValueFlowEdge(from, to);
4977 public void writeInferredLatticeDotFile(ClassDescriptor cd, HierarchyGraph simpleHierarchyGraph,
4978 SSJavaLattice<String> locOrder, String nameSuffix) {
4979 writeInferredLatticeDotFile(cd, null, simpleHierarchyGraph, locOrder, nameSuffix);
4982 public void writeInferredLatticeDotFile(ClassDescriptor cd, MethodDescriptor md,
4983 HierarchyGraph simpleHierarchyGraph, SSJavaLattice<String> locOrder, String nameSuffix) {
4985 String fileName = "lattice_";
4988 /* cd.getSymbol().replaceAll("[\\W_]", "") + "_" + */md.toString().replaceAll("[\\W_]", "");
4990 fileName += cd.getSymbol().replaceAll("[\\W_]", "");
4993 fileName += nameSuffix;
4995 Set<Pair<String, String>> pairSet = locOrder.getOrderingPairSet();
4997 Set<String> addedLocSet = new HashSet<String>();
4999 if (pairSet.size() > 0) {
5001 BufferedWriter bw = new BufferedWriter(new FileWriter(fileName + ".dot"));
5003 bw.write("digraph " + fileName + " {\n");
5005 for (Iterator iterator = pairSet.iterator(); iterator.hasNext();) {
5006 // pair is in the form of <higher, lower>
5007 Pair<String, String> pair = (Pair<String, String>) iterator.next();
5009 String highLocId = pair.getFirst();
5010 String lowLocId = pair.getSecond();
5012 if (!addedLocSet.contains(highLocId)) {
5013 addedLocSet.add(highLocId);
5014 drawNode(bw, locOrder, simpleHierarchyGraph, highLocId);
5017 if (!addedLocSet.contains(lowLocId)) {
5018 addedLocSet.add(lowLocId);
5019 drawNode(bw, locOrder, simpleHierarchyGraph, lowLocId);
5022 bw.write(highLocId + " -> " + lowLocId + ";\n");
5027 } catch (IOException e) {
5028 e.printStackTrace();
5035 private String convertMergeSetToString(HierarchyGraph graph, Set<HNode> mergeSet) {
5037 for (Iterator iterator = mergeSet.iterator(); iterator.hasNext();) {
5038 HNode merged = (HNode) iterator.next();
5039 if (merged.isMergeNode()) {
5040 str += convertMergeSetToString(graph, graph.getMapHNodetoMergeSet().get(merged));
5042 str += " " + merged.getName();
5048 private void drawNode(BufferedWriter bw, SSJavaLattice<String> lattice, HierarchyGraph graph,
5049 String locName) throws IOException {
5051 HNode node = graph.getHNode(locName);
5058 if (lattice.isSharedLoc(locName)) {
5059 prettyStr = locName + "*";
5061 prettyStr = locName;
5064 if (node.isMergeNode()) {
5065 Set<HNode> mergeSet = graph.getMapHNodetoMergeSet().get(node);
5066 prettyStr += ":" + convertMergeSetToString(graph, mergeSet);
5068 bw.write(locName + " [label=\"" + prettyStr + "\"]" + ";\n");
5071 public void _debug_writeFlowGraph() {
5072 Set<MethodDescriptor> keySet = mapMethodDescriptorToFlowGraph.keySet();
5074 for (Iterator<MethodDescriptor> iterator = keySet.iterator(); iterator.hasNext();) {
5075 MethodDescriptor md = (MethodDescriptor) iterator.next();
5076 FlowGraph fg = mapMethodDescriptorToFlowGraph.get(md);
5077 GlobalFlowGraph subGlobalFlowGraph = getSubGlobalFlowGraph(md);
5080 subGlobalFlowGraph.writeGraph("_SUBGLOBAL");
5081 } catch (IOException e) {
5082 e.printStackTrace();
5090 class CyclicFlowException extends Exception {
5094 class InterDescriptor extends Descriptor {
5096 public InterDescriptor(String name) {