1 package Analysis.SSJava;
3 import java.io.BufferedWriter;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.Iterator;
12 import IR.ClassDescriptor;
14 import IR.FieldDescriptor;
15 import IR.MethodDescriptor;
16 import IR.NameDescriptor;
17 import IR.VarDescriptor;
19 public class FlowGraph {
23 Set<FlowNode> nodeSet;
25 Set<FlowNode> returnNodeSet;
28 Map<FlowNode, Set<FlowEdge>> mapFlowNodeToOutEdgeSet;
30 Map<NTuple<Location>, FlowNode> mapLocTupleToFlowNode;
31 Map<FlowNode, NTuple<Location>> mapFlowNodeToLocTuple;
33 // maps the composite representation of field/var descriptors to infer nodes
34 Map<NTuple<Descriptor>, FlowNode> mapDescTupleToInferNode;
36 // maps a paramter descriptor to its index
37 Map<Descriptor, Integer> mapParamDescToIdx;
39 // DS for the lattice generation
40 Map<Integer, FlowNode> mapIdxToFlowNode;
42 public static int interseed = 0;
46 public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
48 this.mapFlowNodeToLocTuple = new HashMap<FlowNode, NTuple<Location>>();
49 this.mapLocTupleToFlowNode = new HashMap<NTuple<Location>, FlowNode>();
50 this.nodeSet = new HashSet<FlowNode>();
51 this.mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
52 this.mapParamDescToIdx = new HashMap<Descriptor, Integer>();
53 this.mapParamDescToIdx.putAll(mapParamDescToIdx);
54 this.returnNodeSet = new HashSet<FlowNode>();
55 this.mapIdxToFlowNode = new HashMap<Integer, FlowNode>();
56 this.mapFlowNodeToOutEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
59 // create a node for 'this' varialbe
60 // NTuple<Descriptor> thisDescTuple = new NTuple<Descriptor>();
61 // thisDescTuple.add(md.getThis());
63 NTuple<Descriptor> thisVarTuple = new NTuple<Descriptor>();
64 thisVarTuple.add(md.getThis());
65 FlowNode thisNode = createNewFlowNode(thisVarTuple);
66 thisNode.setSkeleton(true);
67 thisVarNode = thisNode;
74 public Map<NTuple<Descriptor>, FlowNode> getMapDescTupleToInferNode() {
75 return mapDescTupleToInferNode;
78 public void setMapDescTupleToInferNode(Map<NTuple<Descriptor>, FlowNode> in) {
79 this.mapDescTupleToInferNode.putAll(in);
82 public Map<NTuple<Location>, FlowNode> getMapLocTupleToFlowNode() {
83 return mapLocTupleToFlowNode;
86 public void setMapLocTupleToFlowNode(Map<NTuple<Location>, FlowNode> in) {
87 this.mapLocTupleToFlowNode.putAll(in);
90 public void setNodeSet(Set<FlowNode> in) {
91 this.nodeSet.addAll(in);
94 public void setReturnNodeSet(Set<FlowNode> in) {
95 this.returnNodeSet.addAll(in);
98 public void setThisVarNode(FlowNode thisVarNode) {
99 this.thisVarNode = thisVarNode;
102 public Map<Descriptor, Integer> getMapParamDescToIdx() {
103 return mapParamDescToIdx;
106 public FlowNode createIntermediateNode() {
107 NTuple<Descriptor> tuple = new NTuple<Descriptor>();
108 Descriptor interDesc = new InterDescriptor(LocationInference.INTERLOC + interseed);
109 tuple.add(interDesc);
112 FlowNode newNode = new FlowNode(tuple);
113 newNode.setIntermediate(true);
115 mapDescTupleToInferNode.put(tuple, newNode);
116 nodeSet.add(newNode);
118 System.out.println("create new intermediate node= " + newNode);
123 private void setupMapIdxToDesc() {
125 Set<Descriptor> descSet = mapParamDescToIdx.keySet();
126 for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
127 Descriptor paramDesc = (Descriptor) iterator.next();
128 int idx = mapParamDescToIdx.get(paramDesc);
129 NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
130 descTuple.add(paramDesc);
131 FlowNode paramNode = getFlowNode(descTuple);
132 mapIdxToFlowNode.put(idx, paramNode);
133 paramNode.setSkeleton(true);
138 public int getNumParameters() {
139 return mapIdxToFlowNode.keySet().size();
142 public FlowNode getParamFlowNode(int idx) {
143 return mapIdxToFlowNode.get(idx);
146 public Set<FlowNode> getNodeSet() {
150 public MethodDescriptor getMethodDescriptor() {
154 public boolean isParamDesc(Descriptor desc) {
156 if (mapParamDescToIdx.containsKey(desc)) {
157 int idx = mapParamDescToIdx.get(desc);
158 if (!md.isStatic() && idx == 0) {
167 public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
169 FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
170 FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
172 Set<FlowEdge> fromNodeOutEdgeSet = getOutEdgeSet(fromNode);
173 for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
174 FlowEdge flowEdge = (FlowEdge) iterator.next();
175 if (flowEdge.getDst().equals(toNode)) {
178 if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
187 public Set<FlowEdge> getOutEdgeSet(FlowNode node) {
188 if (!mapFlowNodeToOutEdgeSet.containsKey(node)) {
189 mapFlowNodeToOutEdgeSet.put(node, new HashSet<FlowEdge>());
191 return mapFlowNodeToOutEdgeSet.get(node);
194 public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
196 FlowNode fromNode = getFlowNode(fromDescTuple);
197 FlowNode toNode = getFlowNode(toDescTuple);
199 System.out.println("create an edge from " + fromNode + " to " + toNode);
201 int fromTupleSize = fromDescTuple.size();
202 NTuple<Descriptor> curFromTuple = new NTuple<Descriptor>();
203 for (int i = 0; i < fromTupleSize; i++) {
204 Descriptor desc = fromDescTuple.get(i);
205 curFromTuple.add(desc);
206 int toTupleSize = toDescTuple.size();
207 NTuple<Descriptor> curToTuple = new NTuple<Descriptor>();
208 for (int k = 0; k < toTupleSize; k++) {
209 Descriptor toDesc = toDescTuple.get(k);
210 curToTuple.add(toDesc);
211 addFlowEdge(getFlowNode(curFromTuple), getFlowNode(curToTuple), fromDescTuple, toDescTuple);
215 // int fromTupleSize = fromDescTuple.size();
216 // NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
217 // for (int i = 0; i < fromTupleSize; i++) {
218 // Descriptor desc = fromDescTuple.get(i);
219 // curTuple.add(desc);
220 // addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
223 // int toTupleSize = toDescTuple.size();
224 // curTuple = new NTuple<Descriptor>();
225 // for (int i = 0; i < toTupleSize; i++) {
226 // Descriptor desc = toDescTuple.get(i);
227 // curTuple.add(desc);
228 // addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
233 private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
234 NTuple<Descriptor> endTuple) {
236 FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
237 addOutEdge(fromNode, edge);
239 // System.out.println("add a new edge=" + edge);
242 private void addOutEdge(FlowNode fromNode, FlowEdge edge) {
243 if (!mapFlowNodeToOutEdgeSet.containsKey(fromNode)) {
244 mapFlowNodeToOutEdgeSet.put(fromNode, new HashSet<FlowEdge>());
246 mapFlowNodeToOutEdgeSet.get(fromNode).add(edge);
249 public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
250 if (!mapDescTupleToInferNode.containsKey(descTuple)) {
251 FlowNode node = createNewFlowNode(descTuple);
252 mapDescTupleToInferNode.put(descTuple, node);
254 return mapDescTupleToInferNode.get(descTuple);
257 public FlowNode getThisVarNode() {
261 public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
263 if (!mapDescTupleToInferNode.containsKey(tuple)) {
264 FlowNode node = new FlowNode(tuple);
265 mapDescTupleToInferNode.put(tuple, node);
268 mapLocTupleToFlowNode.put(getLocationTuple(node), node);
270 if (tuple.size() > 1) {
271 NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
272 getFlowNode(baseTuple).addFieldNode(node);
275 // System.out.println("Creating new node=" + node);
278 return mapDescTupleToInferNode.get(tuple);
283 public void addReturnFlowNode(NTuple<Descriptor> tuple) {
285 if (!mapDescTupleToInferNode.containsKey(tuple)) {
286 createNewFlowNode(tuple);
289 FlowNode node = mapDescTupleToInferNode.get(tuple);
290 node.setReturn(true);
292 returnNodeSet.add(node);
295 public Set<FlowNode> getReturnNodeSet() {
296 return returnNodeSet;
299 public Set<FlowNode> getLocalReachFlowNodeSetFrom(FlowNode fn) {
300 Set<FlowNode> set = new HashSet<FlowNode>();
301 recurLocalReachFlowNodeSet(fn, set);
305 private void recurLocalReachFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
307 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
308 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
309 FlowEdge edge = (FlowEdge) iterator.next();
310 FlowNode dstNode = edge.getDst();
312 if (!visited.contains(dstNode)) {
313 visited.add(dstNode);
314 recurLocalReachFlowNodeSet(dstNode, visited);
320 private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
322 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
323 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
324 FlowEdge edge = (FlowEdge) iterator.next();
326 if (fn.equals(getFlowNode(edge.getInitTuple()))) {
327 FlowNode dstNode = getFlowNode(edge.getEndTuple());
328 if (!visited.contains(dstNode)) {
329 visited.add(dstNode);
330 getReachFlowNodeSetFrom(dstNode, visited);
337 public Set<FlowNode> getReachFlowNodeSetFrom(FlowNode fn) {
338 Set<FlowNode> set = new HashSet<FlowNode>();
339 getReachFlowNodeSetFrom(fn, set);
343 // private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
345 // for (Iterator iterator = fn.getOutEdgeSet().iterator();
346 // iterator.hasNext();) {
347 // FlowEdge edge = (FlowEdge) iterator.next();
349 // if (fn.equals(getFlowNode(edge.getInitTuple()))) {
351 // FlowNode dstNode = getFlowNode(edge.getEndTuple());
353 // if (!visited.contains(dstNode)) {
354 // visited.add(dstNode);
355 // getReachFlowNodeSetFrom(dstNode, visited);
362 public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
364 Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
366 for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
367 FlowEdge edge = (FlowEdge) iterator.next();
369 if (fn.getDescTuple().equals(edge.getInitTuple())) {
370 FlowNode dstNode = getFlowNode(edge.getEndTuple());
371 NTuple<Location> dstTuple = getLocationTuple(dstNode);
373 if (!visited.contains(dstTuple)) {
374 visited.add(dstTuple);
375 visited.addAll(getReachableFlowTupleSet(visited, dstNode));
383 public NTuple<Location> getLocationTuple(NTuple<Descriptor> descTuple) {
384 return getLocationTuple(getFlowNode(descTuple));
387 public NTuple<Location> getLocationTuple(FlowNode fn) {
389 if (!mapFlowNodeToLocTuple.containsKey(fn)) {
390 NTuple<Descriptor> descTuple = fn.getDescTuple();
391 NTuple<Location> locTuple = new NTuple<Location>();
392 ClassDescriptor cd = null;
394 Descriptor localDesc = fn.getDescTuple().get(0);
396 if (fn.isIntermediate()) {
397 Location interLoc = new Location(md, localDesc.getSymbol());
398 interLoc.setLocDescriptor(localDesc);
399 locTuple.add(interLoc);
400 } else if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
401 Location topLoc = new Location(md, Location.TOP);
402 topLoc.setLocDescriptor(LocationInference.TOPDESC);
403 locTuple.add(topLoc);
404 } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
405 Location globalLoc = new Location(md, LocationInference.GLOBALLOC);
406 globalLoc.setLocDescriptor(LocationInference.GLOBALDESC);
407 locTuple.add(globalLoc);
410 for (int i = 0; i < descTuple.size(); i++) {
411 Descriptor curDesc = descTuple.get(i);
414 loc = new Location(md, curDesc.getSymbol());
415 loc.setLocDescriptor(curDesc);
416 cd = ((VarDescriptor) curDesc).getType().getClassDesc();
418 loc = new Location(cd, curDesc.getSymbol());
419 loc.setLocDescriptor(curDesc);
421 if (curDesc instanceof FieldDescriptor) {
422 cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
424 cd = ((LocationDescriptor) curDesc).getEnclosingClassDesc();
432 mapFlowNodeToLocTuple.put(fn, locTuple);
434 return mapFlowNodeToLocTuple.get(fn);
437 public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
438 Set<FlowNode> set = new HashSet<FlowNode>();
439 getIncomingFlowNodeSet(node, set);
443 public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
445 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
446 FlowNode curNode = (FlowNode) iterator.next();
447 Set<FlowEdge> edgeSet = getOutEdgeSet(curNode);
449 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
450 FlowEdge flowEdge = (FlowEdge) iterator2.next();
452 if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
453 FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
455 if (!visited.contains(incomingNode)) {
456 visited.add(incomingNode);
457 getIncomingFlowNodeSet(incomingNode, visited);
465 public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
467 NTuple<Descriptor> dstTuple = fn.getDescTuple();
469 Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
471 ClassDescriptor cd = null;
473 for (Iterator iterator = nodeSet.iterator(); iterator.hasNext();) {
474 FlowNode node = (FlowNode) iterator.next();
476 Set<FlowEdge> edgeSet = getOutEdgeSet(node);
477 for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
478 FlowEdge flowEdge = (FlowEdge) iterator2.next();
479 if (dstTuple.equals(flowEdge.getEndTuple())) {
480 NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
481 NTuple<Location> locTuple = new NTuple<Location>();
482 for (int i = 0; i < initTuple.size(); i++) {
483 Descriptor d = initTuple.get(i);
486 loc = new Location(md, d.getSymbol());
487 cd = ((VarDescriptor) d).getType().getClassDesc();
489 loc = new Location(cd, d.getSymbol());
490 cd = ((FieldDescriptor) d).getType().getClassDesc();
501 public boolean isParameter(NTuple<Descriptor> tuple) {
502 // return true if a descriptor tuple is started with a parameter descriptor
503 Descriptor firstIdxDesc = tuple.get(0);
504 return mapParamDescToIdx.containsKey(firstIdxDesc);
507 public FlowGraph clone() {
508 FlowGraph clone = new FlowGraph(md, mapParamDescToIdx);
510 clone.setNodeSet(getNodeSet());
511 clone.setMapLocTupleToFlowNode(getMapLocTupleToFlowNode());
512 clone.setMapFlowNodeToLocTuple(getMapFlowNodeToLocTuple());
513 clone.setMapDescTupleToInferNode(getMapDescTupleToInferNode());
515 clone.setMapFlowNodeToOutEdgeSet(getMapFlowNodeToOutEdgeSet());
516 clone.setReturnNodeSet(getReturnNodeSet());
517 clone.setThisVarNode(getThisVarNode());
522 public Map<FlowNode, NTuple<Location>> getMapFlowNodeToLocTuple() {
523 return mapFlowNodeToLocTuple;
526 public void setMapFlowNodeToLocTuple(Map<FlowNode, NTuple<Location>> in) {
527 this.mapFlowNodeToLocTuple.putAll(in);
530 public Map<FlowNode, Set<FlowEdge>> getMapFlowNodeToOutEdgeSet() {
531 return mapFlowNodeToOutEdgeSet;
534 public void setMapFlowNodeToOutEdgeSet(Map<FlowNode, Set<FlowEdge>> inMap) {
535 Set<FlowNode> keySet = inMap.keySet();
536 for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
537 FlowNode key = (FlowNode) iterator.next();
538 Set<FlowEdge> newEdgeSet = new HashSet<FlowEdge>();
539 newEdgeSet.addAll(inMap.get(key));
540 mapFlowNodeToOutEdgeSet.put(key, newEdgeSet);
544 private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
545 Set<FlowEdge> addedEdgeSet) throws IOException {
547 Set<FlowEdge> edgeSet = getOutEdgeSet(node);
549 for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
550 FlowEdge flowEdge = iterator.next();
552 FlowNode u = flowEdge.getSrc();
553 FlowNode v = flowEdge.getDst();
555 if (u.getDescTuple().equals(flowEdge.getInitTuple())
556 && v.getDescTuple().equals(flowEdge.getEndTuple())) {
557 // only draw an edge of the actual value flow
559 if (!addedEdgeSet.contains(flowEdge)) {
561 if (!addedNodeSet.contains(u)) {
565 if (!addedNodeSet.contains(v)) {
570 bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
571 addedEdgeSet.add(flowEdge);
579 private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
580 bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
583 public void writeGraph() throws java.io.IOException {
587 public void writeGraph(String suffix) throws java.io.IOException {
589 String graphName = "flowgraph_" + md.toString() + suffix;
590 graphName = graphName.replaceAll("[\\W]", "");
592 BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
593 bw.write("digraph " + graphName + " {\n");
594 bw.write("compound=true;\n");
596 // then visit every flow node
598 Iterator<FlowNode> iter = nodeSet.iterator();
600 Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
601 Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
603 while (iter.hasNext()) {
604 FlowNode node = iter.next();
606 if (node.getDescTuple().size() == 1) {
607 // here, we just care about the local variable
608 if (node.getFieldNodeSet().size() > 0) {
609 drawSubgraph(node, bw, addedEdgeSet);
612 drawEdges(node, bw, addedNodeSet, addedEdgeSet);
621 public boolean constainsNode(FlowNode node) {
622 return nodeSet.contains(node);
625 private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
628 bw.write("subgraph cluster_" + node.getID() + "{\n");
629 bw.write("label=\"" + node.getPrettyID() + "\";\n");
631 Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
632 for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
633 FlowNode fieldNode = (FlowNode) iterator.next();
634 if (fieldNode.getFieldNodeSet().size() > 0) {
635 drawSubgraph(fieldNode, bw, addedSet);
637 Descriptor desc = fieldNode.getDescTuple().getLastElement();
638 if (desc instanceof VarDescriptor) {
639 VarDescriptor varDesc = (VarDescriptor) desc;
640 if (varDesc.getType().isPrimitive()) {
641 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
643 } else if (desc instanceof FieldDescriptor) {
644 FieldDescriptor fieldDesc = (FieldDescriptor) desc;
645 if (fieldDesc.getType().isPrimitive()) {
646 bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");