/** Returns a list of direct implementers of c, excluding itself. */ public List getDirectImplementersOf(SootClass i) { i.checkLevel(SootClass.HIERARCHY); if (!i.isInterface()) throw new RuntimeException("interface needed; got " + i); checkState(); return Collections.unmodifiableList(interfaceToDirImplementers.get(i)); }
/** Returns a list of direct subinterfaces of c. */ public List getDirectSubinterfacesOf(SootClass c) { c.checkLevel(SootClass.HIERARCHY); if (!c.isInterface()) throw new RuntimeException("interface needed!"); checkState(); return interfaceToDirSubinterfaces.get(c); }
/** Returns a list of direct subclasses of c, excluding c. */ public List getDirectSubclassesOf(SootClass c) { c.checkLevel(SootClass.HIERARCHY); if (c.isInterface()) throw new RuntimeException("class needed!"); checkState(); return Collections.unmodifiableList(classToDirSubclasses.get(c)); }
public List getSuccsOf(Object s) { if (!unitToSuccs.containsKey(s)) { return new ArrayList(); // throw new RuntimeException("Invalid stmt:" + s); } return unitToSuccs.get(s); }
/** Returns a list of direct subinterfaces of c, including itself. */ public List<SootClass> getDirectSubinterfacesOfIncluding(SootClass c) { c.checkLevel(SootClass.HIERARCHY); if (!c.isInterface()) throw new RuntimeException("interface needed!"); checkState(); List<SootClass> l = new ArrayList<SootClass>(); l.addAll(interfaceToDirSubinterfaces.get(c)); l.add(c); return Collections.unmodifiableList(l); }
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); }
/** Returns a list of subinterfaces of c, excluding itself. */ public List<SootClass> getSubinterfacesOf(SootClass c) { c.checkLevel(SootClass.HIERARCHY); if (!c.isInterface()) throw new RuntimeException("interface needed!"); checkState(); // If already cached, return the value. if (interfaceToSubinterfaces.get(c) != null) return interfaceToSubinterfaces.get(c); // Otherwise, build up the hashmap. List<SootClass> l = new ArrayList<SootClass>(); ListIterator it = interfaceToDirSubinterfaces.get(c).listIterator(); while (it.hasNext()) { l.addAll(getSubinterfacesOfIncluding((SootClass) it.next())); } interfaceToSubinterfaces.put(c, Collections.unmodifiableList(l)); return Collections.unmodifiableList(l); }
/** Returns a list of subclasses of c, excluding itself. */ public List<SootClass> getSubclassesOf(SootClass c) { c.checkLevel(SootClass.HIERARCHY); if (c.isInterface()) throw new RuntimeException("class needed!"); checkState(); // If already cached, return the value. if (classToSubclasses.get(c) != null) return classToSubclasses.get(c); // Otherwise, build up the hashmap. List<SootClass> l = new ArrayList<SootClass>(); ListIterator it = classToDirSubclasses.get(c).listIterator(); while (it.hasNext()) { SootClass cls = (SootClass) it.next(); if (cls.resolvingLevel() < SootClass.HIERARCHY) continue; l.addAll(getSubclassesOfIncluding(cls)); } l = Collections.unmodifiableList(l); classToSubclasses.put(c, l); return l; }
/** Constructs a hierarchy from the current scene. */ public Hierarchy() { this.sc = Scene.v(); state = sc.getState(); // Well, this used to be describable by 'Duh'. // Construct the subclasses hierarchy and the subinterfaces hierarchy. { Chain allClasses = sc.getClasses(); classToSubclasses = new HashMap<SootClass, List<SootClass>>(allClasses.size() * 2 + 1, 0.7f); interfaceToSubinterfaces = new HashMap<SootClass, List<SootClass>>(allClasses.size() * 2 + 1, 0.7f); classToDirSubclasses = new HashMap<SootClass, List>(allClasses.size() * 2 + 1, 0.7f); interfaceToDirSubinterfaces = new HashMap<SootClass, List>(allClasses.size() * 2 + 1, 0.7f); interfaceToDirImplementers = new HashMap<SootClass, List>(allClasses.size() * 2 + 1, 0.7f); Iterator classesIt = allClasses.iterator(); while (classesIt.hasNext()) { SootClass c = (SootClass) classesIt.next(); if (c.resolvingLevel() < SootClass.HIERARCHY) continue; if (c.isInterface()) { interfaceToDirSubinterfaces.put(c, new ArrayList()); interfaceToDirImplementers.put(c, new ArrayList()); } else classToDirSubclasses.put(c, new ArrayList()); } classesIt = allClasses.iterator(); while (classesIt.hasNext()) { SootClass c = (SootClass) classesIt.next(); if (c.resolvingLevel() < SootClass.HIERARCHY) continue; if (c.hasSuperclass()) { if (c.isInterface()) { Iterator subIt = c.getInterfaces().iterator(); while (subIt.hasNext()) { SootClass i = (SootClass) subIt.next(); if (c.resolvingLevel() < SootClass.HIERARCHY) continue; List<SootClass> l = interfaceToDirSubinterfaces.get(i); l.add(c); } } else { List<SootClass> l = classToDirSubclasses.get(c.getSuperclass()); l.add(c); Iterator subIt = c.getInterfaces().iterator(); while (subIt.hasNext()) { SootClass i = (SootClass) subIt.next(); if (c.resolvingLevel() < SootClass.HIERARCHY) continue; l = interfaceToDirImplementers.get(i); l.add(c); } } } } // Fill the directImplementers lists with subclasses. { classesIt = allClasses.iterator(); while (classesIt.hasNext()) { SootClass c = (SootClass) classesIt.next(); if (c.resolvingLevel() < SootClass.HIERARCHY) continue; if (c.isInterface()) { List<SootClass> imp = interfaceToDirImplementers.get(c); Set<SootClass> s = new ArraySet(); Iterator<SootClass> impIt = imp.iterator(); while (impIt.hasNext()) { SootClass c0 = impIt.next(); if (c.resolvingLevel() < SootClass.HIERARCHY) continue; s.addAll(getSubclassesOfIncluding(c0)); } imp.clear(); imp.addAll(s); } } } classesIt = allClasses.iterator(); while (classesIt.hasNext()) { SootClass c = (SootClass) classesIt.next(); if (c.resolvingLevel() < SootClass.HIERARCHY) continue; if (c.isInterface()) { interfaceToDirSubinterfaces.put( c, Collections.unmodifiableList(interfaceToDirSubinterfaces.get(c))); interfaceToDirImplementers.put( c, Collections.unmodifiableList(interfaceToDirImplementers.get(c))); } else classToDirSubclasses.put(c, Collections.unmodifiableList(classToDirSubclasses.get(c))); } } }
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); }