public List getSuccsOf(Object s) { if (!unitToSuccs.containsKey(s)) { return new ArrayList(); // throw new RuntimeException("Invalid stmt:" + s); } return unitToSuccs.get(s); }
private boolean removeBeginNode() { List heads = getHeads(); if (heads.size() != 1) { // System.out.println("heads: "+heads); // System.out.println("Error: the size of heads is not equal to 1!"); return false; // System.exit(1); } else { JPegStmt head = (JPegStmt) heads.get(0); // System.out.println("test head: "+head); if (!head.getName().equals("begin")) { System.err.println("Error: the head is not begin node!"); System.exit(1); } // remove begin node from heads list heads.remove(0); // set the preds list of the succs of head to a new list and put succs of head into heads Iterator succOfHeadIt = getSuccsOf(head).iterator(); while (succOfHeadIt.hasNext()) { JPegStmt succOfHead = (JPegStmt) succOfHeadIt.next(); unitToPreds.put(succOfHead, new ArrayList()); // put succs of head into heads heads.add(succOfHead); } // remove begin node from inlinee Peg if (!mainPegChain.remove(head)) { System.err.println("fail to remove begin node in from mainPegChain!"); System.exit(1); } if (!allNodes.contains(head)) { System.err.println("fail to find begin node in FlowSet allNodes!"); System.exit(1); } else { allNodes.remove(head); } // remove begin node from unitToSuccs if (unitToSuccs.containsKey(head)) { unitToSuccs.remove(head); } } return true; }
public void internalTransform(String phaseName, Map opts) { Iterator it = Scene.v().getApplicationClasses().iterator(); while (it.hasNext()) { SootClass sc = (SootClass) it.next(); // make map of first line to each method HashMap<Integer, SootMethod> lineToMeth = new HashMap<Integer, SootMethod>(); Iterator methIt = sc.getMethods().iterator(); while (methIt.hasNext()) { SootMethod meth = (SootMethod) methIt.next(); if (!meth.isConcrete()) continue; Body body = meth.retrieveActiveBody(); Stmt s = (Stmt) body.getUnits().getFirst(); while (s instanceof IdentityStmt) { s = (Stmt) body.getUnits().getSuccOf(s); } if (s.hasTag("LineNumberTag")) { LineNumberTag tag = (LineNumberTag) s.getTag("LineNumberTag"); lineToMeth.put(new Integer(tag.getLineNumber()), meth); } } Iterator methIt2 = sc.getMethods().iterator(); while (methIt2.hasNext()) { SootMethod meth = (SootMethod) methIt2.next(); if (!meth.isConcrete()) continue; Body body = meth.retrieveActiveBody(); Stmt s = (Stmt) body.getUnits().getFirst(); while (s instanceof IdentityStmt) { s = (Stmt) body.getUnits().getSuccOf(s); } if (s.hasTag("LineNumberTag")) { LineNumberTag tag = (LineNumberTag) s.getTag("LineNumberTag"); int line_num = tag.getLineNumber() - 1; // already taken if (lineToMeth.containsKey(new Integer(line_num))) { meth.addTag(new LineNumberTag(line_num + 1)); } // still available - so use it for this meth else { meth.addTag(new LineNumberTag(line_num)); } } } } }
private void updateMonitor(MonitorSet ms, Object unit) { // System.out.println("===inside updateMonitor==="); // ml.test(); Iterator it = ms.iterator(); while (it.hasNext()) { Object obj = it.next(); if (obj instanceof MonitorDepth) { MonitorDepth md = (MonitorDepth) obj; String objName = md.getObjName(); if (monitor.containsKey(objName)) { if (md.getDepth() > 0) { monitor.get(objName).add(unit); // System.out.println("add to monitorset "+unit); } } else { FlowSet monitorObjs = new ArraySparseSet(); monitorObjs.add(unit); monitor.put(objName, monitorObjs); // System.out.println("put into monitor: "+objName); } } } }
private void buildHeadsAndTails() { List tailList = new ArrayList(); List headList = new ArrayList(); // Build the sets { Iterator unitIt = mainPegChain.iterator(); while (unitIt.hasNext()) { JPegStmt s = (JPegStmt) unitIt.next(); List succs = unitToSuccs.get(s); if (succs.size() == 0) tailList.add(s); if (!unitToPreds.containsKey(s)) { System.err.println("unitToPreds does not contain key: " + s); System.exit(1); } List preds = unitToPreds.get(s); if (preds.size() == 0) headList.add(s); // System.out.println("head is:"); } } tails = (List) tailList; heads = (List) headList; // tails = Collections.unmodifiableList(tailList); // heads = Collections.unmodifiableList(headList); Iterator tmpIt = heads.iterator(); while (tmpIt.hasNext()) { Object temp = tmpIt.next(); // System.out.println(temp); } buildPredecessor(mainPegChain); }
protected void buildSuccsForInlining(JPegStmt stmt, Chain chain, PegGraph inlinee) { // System.out.println("entering buildSuccsForInlining..."); Tag tag = (Tag) stmt.getTags().get(0); // System.out.println("stmt is: "+tag+" "+stmt); /*connect heads of inlinee with the preds of invokeStmt and * delete stmt from the succs list from the preds */ Iterator predIt = getPredsOf(stmt).iterator(); // System.out.println("preds list: "+getPredsOf(stmt)); // System.out.println("preds size: "+getPredsOf(stmt).size()); Iterator headsIt = inlinee.getHeads().iterator(); { // System.out.println("heads: "+inlinee.getHeads()); while (predIt.hasNext()) { JPegStmt pred = (JPegStmt) predIt.next(); // System.out.println("pred: "+pred); List succList = (List) getSuccsOf(pred); // System.out.println("succList of pred: "+succList); int pos = succList.indexOf(stmt); // System.out.println("remove : "+stmt + " from succList: \n"+succList+ "\n of pred" ); // remove invokeStmt succList.remove(pos); while (headsIt.hasNext()) { succList.add(headsIt.next()); } unitToSuccs.put(pred, succList); } { while (headsIt.hasNext()) { Object head = headsIt.next(); List predsOfHeads = new ArrayList(); predsOfHeads.addAll(getPredsOf(head)); unitToPreds.put(head, predsOfHeads); } } /* { predIt = getPredsOf(stmt).iterator(); while (predIt.hasNext()){ JPegStmt s = (JPegStmt)predIt.next(); if (unitToSuccs.containsKey(s)){ Iterator succIt = ((List) unitToSuccs.get(s)).iterator(); while(succIt.hasNext()){ //Object successor = succIt.next(); JPegStmt successor = (JPegStmt)succIt.next(); List predList = (List) unitToPreds.get(successor); if (predList != null) { try { predList.add(s); } catch(NullPointerException e) { System.out.println(s + "successor: " + successor); throw e; } } } } } }*/ } /*connect tails of inlinee with the succ of invokeStmt and * delete stmt from the */ Iterator tailsIt = inlinee.getTails().iterator(); { // System.out.println("tails: "+inlinee.getTails()); while (tailsIt.hasNext()) { Iterator succIt = getSuccsOf(stmt).iterator(); JPegStmt tail = (JPegStmt) tailsIt.next(); List succList = null; if (unitToSuccs.containsKey(tail)) { // System.out.println("error: unitToSucc containsKey: "+tail); succList = (List) getSuccsOf(tail); // System.out.println("succList: "+succList); } else { succList = new ArrayList(); } while (succIt.hasNext()) { JPegStmt succ = (JPegStmt) succIt.next(); succList.add(succ); // System.out.println("succ: "+succ); // remove stmt from the preds list of the succs of itself. List predListOfSucc = getPredsOf(succ); if (predListOfSucc == null) { System.err.println("Error: predListOfSucc is null!"); System.exit(1); } else { if (predListOfSucc.size() != 0) { int pos = predListOfSucc.indexOf(stmt); if (pos > 0 || pos == 0) { // System.out.println("remove stmt: "+stmt+" from the preds list"+predListOfSucc+" // of the succ"); predListOfSucc.remove(pos); } // System.out.println("remove(from PRED): "); } } unitToPreds.put(succ, predListOfSucc); } unitToSuccs.put(tail, succList); // System.out.println("put: "+tail); // System.out.println("succList: "+succList+ "into unitToSucc"); } } // add Nov 1 { tailsIt = inlinee.getTails().iterator(); while (tailsIt.hasNext()) { JPegStmt s = (JPegStmt) tailsIt.next(); if (unitToSuccs.containsKey(s)) { Iterator succIt = unitToSuccs.get(s).iterator(); while (succIt.hasNext()) { // Object successor = succIt.next(); JPegStmt successor = (JPegStmt) succIt.next(); List<JPegStmt> predList = unitToPreds.get(successor); if (predList != null && !predList.contains(s)) { try { predList.add(s); /* Tag tag = (Tag)successor.getTags().get(0); System.out.println("add "+s+" to predlist of "+tag+" "+successor); */ } catch (NullPointerException e) { System.out.println(s + "successor: " + successor); throw e; } } } } } } // end add Nov 1 // System.out.println("stmt: "+stmt); // remove stmt from allNodes and mainPegChain // System.out.println("mainPegChain contains stmt: "+mainPegChain.contains(stmt)); // testPegChain(); if (!allNodes.contains(stmt)) { System.err.println("fail to find begin node in allNodes!"); System.exit(1); } else { allNodes.remove(stmt); // System.out.println("remove from allNode: "+stmt); } if (!chain.contains(stmt)) { System.err.println("Error! Chain does not contains stmt (extending point)!"); System.exit(1); } else { if (!chain.remove(stmt)) { System.err.println("fail to remove invoke stmt in from Chain!"); System.exit(1); } } /* if (!mainPegChain.contains(stmt)){ boolean find = false; //System.out.println("main chain does not contain AFTER"); Set maps = startToThread.entrySet(); for(Iterator iter=maps.iterator(); iter.hasNext();){ Map.Entry entry = (Map.Entry)iter.next(); Object startNode = entry.getKey(); Iterator runIt = ((List)entry.getValue()).iterator(); while (runIt.hasNext()){ Chain chain=(Chain)runIt.next(); if (chain.contains(stmt)) { find = true; if (!chain.remove(stmt)){ System.err.println("fail to remove begin node in from mainPegChain!"); System.exit(1); } break; } } if (find == false){ System.err.println("fail to find stmt: "+stmt+" in chains!"); System.exit(1); } } //this.toString(); } else{ if (!mainPegChain.remove(stmt)) { System.err.println("fail to remove begin node in from mainPegChain!"); System.exit(1); } else{ // System.out.println("remove(from mainchain): "+stmt); } } */ // remove stmt from unitToSuccs and unitToPreds if (unitToSuccs.containsKey(stmt)) { unitToSuccs.remove(stmt); } if (unitToPreds.containsKey(stmt)) { unitToPreds.remove(stmt); } }
/* private void deleteExitToException(){ Iterator it = iterator(); while (it.hasNext()){ JPegStmt stmt = (JPegStmt)it.next(); Unit unit = stmt.getUnit(); UnitGraph unitGraph = stmt.getUnitGraph(); if (unit instanceof ExitMonitorStmt){ Iterator succIt = unitGraph.getSuccsOf(unit).iterator(); while(succIt.next && exceHandlers.contains(un) ){ System.out.println("====find it! unit: "+unit+"\n un: "+un); continue; } } } */ private void buildPredecessor(Chain pegChain) { // System.out.println("==building predcessor==="); // initialize the pred sets to empty { JPegStmt s = null; Iterator unitIt = pegChain.iterator(); while (unitIt.hasNext()) { s = (JPegStmt) unitIt.next(); unitToPreds.put(s, new ArrayList()); } } // System.out.println("==finish init of unitToPred==="); { Iterator unitIt = pegChain.iterator(); while (unitIt.hasNext()) { Object s = unitIt.next(); // System.out.println("s is: "+s); // Modify preds set for each successor for this statement if (unitToSuccs.containsKey(s)) { List succList = unitToSuccs.get(s); Iterator succIt = succList.iterator(); // System.out.println("unitToSuccs contains "+s); // System.out.println("succList is: "+succList); while (succIt.hasNext()) { // Object successor = succIt.next(); JPegStmt successor = (JPegStmt) succIt.next(); // System.out.println("successor is: "+successor); List<Object> predList = unitToPreds.get(successor); // System.out.println("predList is: "+predList); if (predList != null && !predList.contains(s)) { try { predList.add(s); /* Tag tag1 = (Tag)((JPegStmt)s).getTags().get(0); System.out.println("add "+tag1+" "+s+" to predListof"); Tag tag2 = (Tag)((JPegStmt)successor).getTags().get(0); System.out.println(tag2+" "+successor); */ } catch (NullPointerException e) { System.out.println(s + "successor: " + successor); throw e; } // if (((JPegStmt)successor).getName().equals("start")){ if (successor instanceof StartStmt) { List runMethodChainList = startToThread.get(successor); if (runMethodChainList == null) { throw new RuntimeException("null runmehtodchain: \n" + successor.getUnit()); } Iterator possibleMethodIt = runMethodChainList.iterator(); while (possibleMethodIt.hasNext()) { Chain subChain = (Chain) possibleMethodIt.next(); buildPredecessor(subChain); } } } else { System.err.println("predlist of " + s + " is null"); // System.exit(1); } // unitToPreds.put(successor, predList); } } else { System.err.println("unitToSuccs does not contains key" + s); System.exit(1); } } } }
private void buildSuccessor(Chain pegChain) { // Add regular successors { HashMap unitToPeg = (HashMap) unitToPegMap.get(pegChain); Iterator pegIt = pegChain.iterator(); JPegStmt currentNode, nextNode; currentNode = pegIt.hasNext() ? (JPegStmt) pegIt.next() : null; // June 19 add for begin node if (currentNode != null) { // System.out.println("currentNode: "+currentNode); // if the unit is "begin" node nextNode = pegIt.hasNext() ? (JPegStmt) pegIt.next() : null; if (currentNode.getName().equals("begin")) { List<JPegStmt> successors = new ArrayList<JPegStmt>(); successors.add(nextNode); unitToSuccs.put(currentNode, successors); currentNode = nextNode; } // end June 19 add for begin node while (currentNode != null) { // System.out.println("currentNode: "+currentNode); /* If unitToSuccs contains currentNode, it is the point to inline methods, * we need not compute its successors again */ if (unitToSuccs.containsKey(currentNode) && !currentNode.getName().equals("wait")) { currentNode = pegIt.hasNext() ? (JPegStmt) pegIt.next() : null; continue; } List<JPegStmt> successors = new ArrayList<JPegStmt>(); Unit unit = currentNode.getUnit(); UnitGraph unitGraph = currentNode.getUnitGraph(); List unitSucc = unitGraph.getSuccsOf(unit); Iterator succIt = unitSucc.iterator(); while (succIt.hasNext()) { Unit un = (Unit) succIt.next(); // Don't build the edge from "monitor exit" to exception handler if (unit instanceof ExitMonitorStmt && exceHandlers.contains(un)) { // System.out.println("====find it! unit: "+unit+"\n un: "+un); continue; } else if (unitToPeg.containsKey(un)) { JPegStmt pp = (JPegStmt) (unitToPeg.get(un)); if (pp != null && !successors.contains(pp)) successors.add(pp); } } // end while if (currentNode.getName().equals("wait")) { while (!(currentNode.getName().equals("notified-entry"))) { currentNode = pegIt.hasNext() ? (JPegStmt) pegIt.next() : null; } unitToSuccs.put(currentNode, successors); // System.out.println("put key: "+currentNode+" into unitToSucc"); } else { unitToSuccs.put(currentNode, successors); } if (currentNode.getName().equals("start")) { // System.out.println("-----build succ for start----"); if (startToThread.containsKey(currentNode)) { List runMethodChainList = startToThread.get(currentNode); Iterator possibleMethodIt = runMethodChainList.iterator(); while (possibleMethodIt.hasNext()) { Chain subChain = (Chain) possibleMethodIt.next(); if (subChain != null) { // System.out.println("build succ for subChain"); // buildSuccessor(subGraph, subChain, addExceptionEdges); buildSuccessor(subChain); } else System.out.println("*********subgraph is null!!!"); } } } currentNode = pegIt.hasNext() ? (JPegStmt) pegIt.next() : null; } // while // June 19 add for begin node } // end June 19 add for begin node } }
public List getPredsOf(Object s) { if (!unitToPreds.containsKey(s)) throw new RuntimeException("Invalid stmt" + s); return unitToPreds.get(s); }