예제 #1
0
 /* Generate classes to process, adding or removing package marked by
  * command line options.
  */
 private void prepareClasses() {
   // Remove/add all classes from packageInclusionMask as per -i option
   Chain<SootClass> processedClasses = new HashChain<SootClass>();
   while (true) {
     Chain<SootClass> unprocessedClasses = new HashChain<SootClass>(getClasses());
     unprocessedClasses.removeAll(processedClasses);
     if (unprocessedClasses.isEmpty()) break;
     processedClasses.addAll(unprocessedClasses);
     for (SootClass s : unprocessedClasses) {
       if (s.isPhantom()) continue;
       if (Options.v().app()) {
         s.setApplicationClass();
       }
       if (Options.v().classes().contains(s.getName())) {
         s.setApplicationClass();
         continue;
       }
       if (s.isApplicationClass() && isExcluded(s)) {
         s.setLibraryClass();
       }
       if (isIncluded(s)) {
         s.setApplicationClass();
       }
       if (s.isApplicationClass()) {
         // make sure we have the support
         loadClassAndSupport(s.getName());
       }
     }
   }
 }
예제 #2
0
  public void staticBlockInlining(SootClass sootClass) {
    this.sootClass = sootClass;
    // retrieve the clinit method if any for sootClass
    if (!sootClass.declaresMethod("void <clinit>()")) {
      System.out.println("no clinit");
      return;
    }

    SootMethod clinit = sootClass.getMethod("void <clinit>()");
    // System.out.println(clinit);

    // retireve the active body
    if (!clinit.hasActiveBody())
      throw new RuntimeException("method " + clinit.getName() + " has no active body!");

    Body clinitBody = clinit.getActiveBody();

    Chain units = ((DavaBody) clinitBody).getUnits();

    if (units.size() != 1) {
      throw new RuntimeException("DavaBody AST doesn't have single root.");
    }

    ASTNode AST = (ASTNode) units.getFirst();
    if (!(AST instanceof ASTMethodNode))
      throw new RuntimeException("Starting node of DavaBody AST is not an ASTMethodNode");

    AST.apply(new MethodCallFinder(this));
  }
예제 #3
0
  /**
   * Utility method used in the construction of {@link UnitGraph}s, to be called only after the
   * unitToPreds and unitToSuccs maps have been built.
   *
   * <p><code>UnitGraph</code> provides an implementation of <code>buildHeadsAndTails()</code> which
   * defines the graph's set of heads to include the first {@link Unit} in the graph's body,
   * together with any other <tt>Unit</tt> which has no predecessors. It defines the graph's set of
   * tails to include all <tt>Unit</tt>s with no successors. Subclasses of <code>UnitGraph</code>
   * may override this method to change the criteria for classifying a node as a head or tail.
   */
  protected void buildHeadsAndTails() {
    List tailList = new ArrayList();
    List headList = new ArrayList();

    for (Iterator unitIt = unitChain.iterator(); unitIt.hasNext(); ) {
      Unit s = (Unit) unitIt.next();
      List succs = (List) unitToSuccs.get(s);
      if (succs.size() == 0) {
        tailList.add(s);
      }
      List preds = (List) unitToPreds.get(s);
      if (preds.size() == 0) {
        headList.add(s);
      }
    }

    // Add the first Unit, even if it is the target of
    // a branch.
    Unit entryPoint = (Unit) unitChain.getFirst();
    if (!headList.contains(entryPoint)) {
      headList.add(entryPoint);
    }

    tails = Collections.unmodifiableList(tailList);
    heads = Collections.unmodifiableList(headList);
  }
예제 #4
0
파일: Util.java 프로젝트: pabloloyola/soot
 /** Returns index of local in body, or negative value if not found. */
 public static int getIndexOfLocal(Local l, Body b) {
   int i = 0;
   Chain locals = b.getLocals();
   for (Iterator itLoc = locals.iterator(); itLoc.hasNext(); ) {
     if (l == (Local) itLoc.next()) return i;
     ++i;
   }
   return Integer.MIN_VALUE;
 }
  /**
   * Make sure that all invoke special targets are cloned into the class from ancestors. This might
   * mean that we have to clone hidden methods, and change their names. So clone them in, and update
   * the clone to original map, and update the invoke special Also, this will update invoke specials
   * that target methods cloned in previous call to above cloneReachableNonHiddenAncestorMethods()
   */
  private void cloneHiddenAncestorMethodsAndFixInvokeSpecial() {
    Set<SootClass> parents = SootUtils.getParents(clazz);

    boolean debug = false; // (clazz.getName().contains("ResultDisplayer"));

    boolean cloneAdded = false;
    do {
      cloneAdded = false;
      for (SootMethod method : clazz.getMethods()) {

        if (method.isAbstract() || method.isPhantom() || !method.isConcrete()) continue;

        if (debug) System.out.println(method);

        Body body = null;
        try {
          body = method.retrieveActiveBody();
        } catch (Exception ex) {
          logger.info("Exception retrieving method body {}", ex);
          continue;
        }

        StmtBody stmtBody = (StmtBody) body;

        Chain units = stmtBody.getUnits();
        Iterator stmtIt = units.iterator();

        while (stmtIt.hasNext()) {
          Stmt stmt = (Stmt) stmtIt.next();

          if (stmt.containsInvokeExpr() && stmt.getInvokeExpr() instanceof SpecialInvokeExpr) {
            SpecialInvokeExpr si = (SpecialInvokeExpr) stmt.getInvokeExpr();

            SootMethod target = resolveSpecialInvokeTarget(si); // si.getMethod();

            if (debug) System.out.printf("\t%s %s", si, target);

            if (clonedToOriginal.values().contains(target)) {
              // found target of invoke special, and it has been cloned, so change the invoke
              // special
              SootMethod cloneOfTarget = clonedToOriginal.inverse().get(target);
              si.setMethodRef(cloneOfTarget.makeRef());
              if (debug) System.out.println("\tChange ref " + cloneOfTarget);
            } else if (parents.contains(target.getDeclaringClass())) {
              // target has not been cloned, but should be cloned, so clone it and change ref of
              // invoke
              String name = target.getName() + CLONED_METHOD_SUFFIX + (cloned_method_id++);
              SootMethod clonedMethod = cloneMethod(target, name);
              si.setMethodRef(clonedMethod.makeRef());
              cloneAdded = true;
              if (debug) System.out.println("\tClone and Change ref " + clonedMethod);
            }
          }
        }
      }
    } while (cloneAdded);
  }
예제 #6
0
  private void printStatementsInBody(Body body, java.io.PrintWriter out) {

    if (Options.v().verbose()) System.out.println("Printing " + body.getMethod().getName());

    Chain<Unit> units = ((DavaBody) body).getUnits();

    if (units.size() != 1) {
      throw new RuntimeException("DavaBody AST doesn't have single root.");
    }

    UnitPrinter up = new DavaUnitPrinter((DavaBody) body);
    ((ASTNode) units.getFirst()).toString(up);
    out.print(up.toString());
  }
예제 #7
0
  protected void testPegChain(Chain chain) {
    System.out.println("******** chain********");
    Iterator it = chain.iterator();
    while (it.hasNext()) {
      /*Object o = it.next();
      System.out.println(o);
      if (!(o instanceof JPegStmt))  System.out.println("not instanceof JPegStmt: "+o);
      JPegStmt s = (JPegStmt)o;
      */
      JPegStmt stmt = (JPegStmt) it.next();
      System.out.println(stmt.toString());
      /*if (stmt.getName().equals("start")){

      System.out.println("find start method in : " + stmt.toString() );
      List list =(List)startToThread.get(stmt);
      Iterator chainIt = list.iterator();
      while (chainIt.hasNext()){
      Chain chain = (Chain)chainIt.next();
      Iterator subit = chain.iterator();
      while (subit.hasNext()){
      System.out.println("**" + ((JPegStmt)subit.next()).toString());
      }
      }
      System.out.println("$$$$$$returing to main chain");
      }
      */
    }
  }
예제 #8
0
  public void removeClass(SootClass c) {
    if (!c.isInScene()) throw new RuntimeException();

    classes.remove(c);

    if (c.isLibraryClass()) {
      libraryClasses.remove(c);
    } else if (c.isPhantomClass()) {
      phantomClasses.remove(c);
    } else if (c.isApplicationClass()) {
      applicationClasses.remove(c);
    }

    c.getType().setSootClass(null);
    c.setInScene(false);
    modifyHierarchy();
  }
예제 #9
0
  public ASTMethodNode inline(SootMethod maybeInline) {
    // check if this method should be inlined

    if (sootClass != null) {
      // 1, method should belong to the same class as the clinit method
      if (sootClass.declaresMethod(maybeInline.getSubSignature())) {
        // System.out.println("The method invoked is from the same class");
        // 2, method should be static

        if (Modifier.isStatic(maybeInline.getModifiers())) {
          // decided to inline
          // send the ASTMethod node of the TO BE INLINED METHOD

          // retireve the active body
          if (!maybeInline.hasActiveBody())
            throw new RuntimeException("method " + maybeInline.getName() + " has no active body!");

          Body bod = maybeInline.getActiveBody();

          Chain units = ((DavaBody) bod).getUnits();

          if (units.size() != 1) {
            throw new RuntimeException("DavaBody AST doesn't have single root.");
          }

          ASTNode ASTtemp = (ASTNode) units.getFirst();
          if (!(ASTtemp instanceof ASTMethodNode))
            throw new RuntimeException("Starting node of DavaBody AST is not an ASTMethodNode");

          // restricting to methods which do not have any variables declared
          ASTMethodNode toReturn = (ASTMethodNode) ASTtemp;

          ASTStatementSequenceNode declarations = toReturn.getDeclarations();
          if (declarations.getStatements().size() == 0) {
            // inline only if there are no declarations in the method inlined
            System.out.println("No declarations in the method. we can inline this method");
            return toReturn;
          }
        }
      }
    }
    return null; // meaning dont inline
  }
예제 #10
0
  /**
   * Utility method for <tt>UnitGraph</tt> constructors. It computes the edges corresponding to
   * unexceptional control flow.
   *
   * @param unitToSuccs A {@link Map} from {@link Unit}s to {@link List}s of {@link Unit}s. This is
   *     an ``out parameter''; callers must pass an empty {@link Map}.
   *     <tt>buildUnexceptionalEdges</tt> will add a mapping for every <tt>Unit</tt> in the body to
   *     a list of its unexceptional successors.
   * @param unitToPreds A {@link Map} from {@link Unit}s to {@link List}s of {@link Unit}s. This is
   *     an ``out parameter''; callers must pass an empty {@link Map}.
   *     <tt>buildUnexceptionalEdges</tt> will add a mapping for every <tt>Unit</tt> in the body to
   *     a list of its unexceptional predecessors.
   */
  protected void buildUnexceptionalEdges(Map unitToSuccs, Map unitToPreds) {

    // Initialize the predecessor sets to empty
    for (Iterator unitIt = unitChain.iterator(); unitIt.hasNext(); ) {
      unitToPreds.put(unitIt.next(), new ArrayList());
    }

    Iterator unitIt = unitChain.iterator();
    Unit currentUnit, nextUnit;

    nextUnit = unitIt.hasNext() ? (Unit) unitIt.next() : null;

    while (nextUnit != null) {
      currentUnit = nextUnit;
      nextUnit = unitIt.hasNext() ? (Unit) unitIt.next() : null;

      List successors = new ArrayList();

      if (currentUnit.fallsThrough()) {
        // Add the next unit as the successor
        if (nextUnit != null) {
          successors.add(nextUnit);
          ((List) unitToPreds.get(nextUnit)).add(currentUnit);
        }
      }

      if (currentUnit.branches()) {
        for (Iterator targetIt = currentUnit.getUnitBoxes().iterator(); targetIt.hasNext(); ) {
          Unit target = ((UnitBox) targetIt.next()).getUnit();
          // Arbitrary bytecode can branch to the same
          // target it falls through to, so we screen for duplicates:
          if (!successors.contains(target)) {
            successors.add(target);
            ((List) unitToPreds.get(target)).add(currentUnit);
          }
        }
      }

      // Store away successors
      unitToSuccs.put(currentUnit, successors);
    }
  }
예제 #11
0
 public String toString() {
   Iterator it = unitChain.iterator();
   StringBuffer buf = new StringBuffer();
   while (it.hasNext()) {
     Unit u = (Unit) it.next();
     buf.append("// preds: " + getPredsOf(u) + "\n");
     buf.append(u.toString() + '\n');
     buf.append("// succs " + getSuccsOf(u) + "\n");
   }
   return buf.toString();
 }
예제 #12
0
  public void addClass(SootClass c) {
    if (c.isInScene()) throw new RuntimeException("already managed: " + c.getName());

    if (containsClass(c.getName())) throw new RuntimeException("duplicate class: " + c.getName());

    classes.add(c);
    c.setLibraryClass();

    nameToClass.put(c.getName(), c.getType());
    c.getType().setSootClass(c);
    c.setInScene(true);
    modifyHierarchy();
  }
예제 #13
0
  /**
   * Utility method that produces a new map from the {@link Unit}s of this graph's body to the union
   * of the values stored in the two argument {@link Map}s, used to combine the maps of exceptional
   * and unexceptional predecessors and successors into maps of all predecessors and successors. The
   * values stored in both argument maps must be {@link List}s of {@link Unit}s, which are assumed
   * not to contain any duplicate <tt>Unit</tt>s.
   *
   * @param mapA The first map to be combined.
   * @param mapB The second map to be combined.
   */
  protected Map combineMapValues(Map mapA, Map mapB) {
    // The duplicate screen
    Map result = new HashMap(mapA.size() * 2 + 1, 0.7f);
    for (Iterator chainIt = unitChain.iterator(); chainIt.hasNext(); ) {
      Unit unit = (Unit) chainIt.next();
      List listA = (List) mapA.get(unit);
      if (listA == null) {
        listA = Collections.EMPTY_LIST;
      }
      List listB = (List) mapB.get(unit);
      if (listB == null) {
        listB = Collections.EMPTY_LIST;
      }

      int resultSize = listA.size() + listB.size();
      if (resultSize == 0) {
        result.put(unit, Collections.EMPTY_LIST);
      } else {
        List resultList = new ArrayList(resultSize);
        Iterator listIt = null;
        // As a minor optimization of the duplicate screening,
        // copy the longer list first.
        if (listA.size() >= listB.size()) {
          resultList.addAll(listA);
          listIt = listB.iterator();
        } else {
          resultList.addAll(listB);
          listIt = listA.iterator();
        }
        while (listIt.hasNext()) {
          Object element = listIt.next();
          // It is possible for there to be both an exceptional
          // and an unexceptional edge connecting two Units
          // (though probably not in a class generated by
          // javac), so we need to screen for duplicates. On the
          // other hand, we expect most of these lists to have
          // only one or two elements, so it doesn't seem worth
          // the cost to build a Set to do the screening.
          if (!resultList.contains(element)) {
            resultList.add(element);
          }
        }
        result.put(unit, Collections.unmodifiableList(resultList));
      }
    }
    return result;
  }
예제 #14
0
  protected void internalTransform(Body body, String phaseName, Map options) {
    if (Options.v().verbose())
      G.v().out.println("[" + body.getMethod().getName() + "] Tightening trap boundaries...");

    Chain trapChain = body.getTraps();
    Chain unitChain = body.getUnits();
    if (trapChain.size() > 0) {
      ExceptionalUnitGraph graph = new ExceptionalUnitGraph(body);

      for (Iterator trapIt = trapChain.iterator(); trapIt.hasNext(); ) {
        Trap trap = (Trap) trapIt.next();
        Unit firstTrappedUnit = trap.getBeginUnit();
        Unit firstTrappedThrower = null;
        Unit firstUntrappedUnit = trap.getEndUnit();
        Unit lastTrappedUnit = (Unit) unitChain.getPredOf(firstUntrappedUnit);
        Unit lastTrappedThrower = null;
        for (Unit u = firstTrappedUnit;
            u != null && u != firstUntrappedUnit;
            u = (Unit) unitChain.getSuccOf(u)) {
          if (mightThrowTo(graph, u, trap)) {
            firstTrappedThrower = u;
            break;
          }
        }
        if (firstTrappedThrower != null) {
          for (Unit u = lastTrappedUnit; u != null; u = (Unit) unitChain.getPredOf(u)) {
            if (mightThrowTo(graph, u, trap)) {
              lastTrappedThrower = u;
              break;
            }
          }
        }
        if (firstTrappedThrower != null && firstTrappedUnit != firstTrappedThrower) {
          trap.setBeginUnit(firstTrappedThrower);
        }
        if (lastTrappedThrower == null) {
          lastTrappedThrower = firstTrappedUnit;
        }
        if (lastTrappedUnit != lastTrappedThrower) {
          trap.setEndUnit((Unit) unitChain.getSuccOf(lastTrappedThrower));
        }
      }
    }
  }
예제 #15
0
  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;
  }
예제 #16
0
  public boolean addPeg(PegGraph pg, Chain chain) {
    if (!pg.removeBeginNode()) return false;
    //		System.out.println("adding one peg into another");

    //		System.out.println("after removeBeginNode===");
    //	pg.testPegChain();
    // System.out.println(pg);

    // put every node of peg into this
    Iterator mainIt = pg.mainIterator();

    while (mainIt.hasNext()) {
      JPegStmt s = (JPegStmt) mainIt.next();
      //			System.out.println("add to mainPegChain: "+s);
      mainPegChain.addLast(s);
      //			if (chain.contains(s)){
      //				System.err.println("error! chain contains: "+s);
      //				System.exit(1);
      //			}
      //			else
      //				chain.addLast(s);

    }
    Iterator it = pg.iterator();
    while (it.hasNext()) {
      JPegStmt s = (JPegStmt) it.next();
      // System.out.println("add to allNodes: "+s);
      if (allNodes.contains(s)) {
        System.err.println("error! allNodes contains: " + s);
        System.exit(1);
      } else allNodes.add(s);
    }
    //	testPegChain();
    //	testIterator();
    unitToSuccs.putAll(pg.getUnitToSuccs());
    unitToPreds.putAll(pg.getUnitToPreds());
    //	testUnitToSucc();
    // testUnitToPred();
    //		buildMaps(pg); // RLH
    return true;
  }
예제 #17
0
  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);
  }
예제 #18
0
  /** Prints the given <code>JimpleBody</code> to the specified <code>PrintWriter</code>. */
  private void printStatementsInBody(
      Body body, java.io.PrintWriter out, LabeledUnitPrinter up, UnitGraph unitGraph) {
    Chain units = body.getUnits();
    Iterator unitIt = units.iterator();
    Unit currentStmt = null, previousStmt;

    while (unitIt.hasNext()) {

      previousStmt = currentStmt;
      currentStmt = (Unit) unitIt.next();

      // Print appropriate header.
      {
        // Put an empty line if the previous node was a branch node, the current node is a join node
        //   or the previous statement does not have body statement as a successor, or if
        //   body statement has a label on it

        if (currentStmt != units.getFirst()) {
          if (unitGraph.getSuccsOf(previousStmt).size() != 1
              || unitGraph.getPredsOf(currentStmt).size() != 1
              || up.labels().containsKey(currentStmt)) {
            up.newline();
          } else {
            // Or if the previous node does not have body statement as a successor.

            List succs = unitGraph.getSuccsOf(previousStmt);

            if (succs.get(0) != currentStmt) {
              up.newline();
            }
          }
        }

        if (up.labels().containsKey(currentStmt)) {
          up.unitRef(currentStmt, true);
          up.literal(":");
          up.newline();
        }

        if (up.references().containsKey(currentStmt)) {
          up.unitRef(currentStmt, false);
        }
      }

      up.startUnit(currentStmt);
      currentStmt.toString(up);
      up.endUnit(currentStmt);

      up.literal(";");
      up.newline();

      // only print them if not generating attributes files
      // because they mess up line number
      // if (!addJimpleLn()) {
      if (Options.v().print_tags_in_output()) {
        Iterator tagIterator = currentStmt.getTags().iterator();
        while (tagIterator.hasNext()) {
          Tag t = (Tag) tagIterator.next();
          up.noIndent();
          up.literal("/*");
          up.literal(t.toString());
          up.literal("*/");
          up.newline();
        }
        /*Iterator udIt = currentStmt.getUseAndDefBoxes().iterator();
        while (udIt.hasNext()) {
            ValueBox temp = (ValueBox)udIt.next();
            Iterator vbtags = temp.getTags().iterator();
            while (vbtags.hasNext()) {
                Tag t = (Tag) vbtags.next();
                up.noIndent();
                up.literal("VB Tag: "+t.toString());
                up.newline();
            }
        }*/
      }
    }

    out.print(up.toString());
    if (addJimpleLn()) {
      setJimpleLnNum(up.getPositionTagger().getEndLn());
    }

    // Print out exceptions
    {
      Iterator trapIt = body.getTraps().iterator();

      if (trapIt.hasNext()) {
        out.println();
        incJimpleLnNum();
      }

      while (trapIt.hasNext()) {
        Trap trap = (Trap) trapIt.next();

        out.println(
            "        catch "
                + Scene.v().quotedNameOf(trap.getException().getName())
                + " from "
                + up.labels().get(trap.getBeginUnit())
                + " to "
                + up.labels().get(trap.getEndUnit())
                + " with "
                + up.labels().get(trap.getHandlerUnit())
                + ";");

        incJimpleLnNum();
      }
    }
  }
예제 #19
0
 public Iterator iterator() {
   return unitChain.iterator();
 }
예제 #20
0
 public int size() {
   return unitChain.size();
 }
예제 #21
0
 public Iterator mainIterator() {
   return mainPegChain.iterator();
 }
예제 #22
0
  /**
   * Constructs a graph for the units found in the provided Body instance. Each node in the graph
   * corresponds to a unit. The edges are derived from the control flow.
   *
   * @param body The underlying body we want to make a graph for.
   * @param addExceptionEdges If true then the control flow edges associated with exceptions are
   *     added.
   * @param dontAddEdgeFromStmtBeforeAreaOfProtectionToCatchBlock This was added for Dava. If true,
   *     edges are not added from statement before area of protection to catch. If false, edges ARE
   *     added. For Dava, it should be true. For flow analyses, it should be false.
   * @param Hierarchy Using class hierarchy analysis to find the run method of started thread
   * @param PointsToAnalysis Using point to analysis (SPARK package) to improve the precision of
   *     results
   */
  public PegGraph(
      CallGraph callGraph,
      Hierarchy hierarchy,
      PAG pag,
      Set methodsNeedingInlining,
      Set allocNodes,
      List<List> inlineSites,
      Map<SootMethod, String> synchObj,
      Set multiRunAllocNodes,
      Map<AllocNode, String> allocNodeToObj,
      Body unitBody,
      String threadName,
      SootMethod sm,
      boolean addExceEdge,
      boolean dontAddEdgeFromStmtBeforeAreaOfProtectionToCatchBlock) {
    this.allocNodeToObj = allocNodeToObj;
    this.multiRunAllocNodes = multiRunAllocNodes;
    this.synchObj = synchObj;
    this.inlineSites = inlineSites;
    this.allocNodes = allocNodes;
    this.methodsNeedingInlining = methodsNeedingInlining;
    logFile = new File("log.txt");
    try {
      fileWriter = new FileWriter(logFile);
    } catch (IOException io) {
      System.err.println("Errors occur during create FileWriter !");
      //			throw io;
    }

    body = unitBody;
    synch = new HashSet<List>();
    exceHandlers = new HashSet<Unit>();
    needInlining = true;
    monitorObjs = new HashSet<Object>();
    startToBeginNodes = new HashMap();
    unitChain = body.getUnits();
    int size = unitChain.size();
    // initial unitToSuccs, unitToPreds, unitToPegMap, and startToThread
    unitToSuccs = new HashMap(size * 2 + 1, 0.7f);
    unitToPreds = new HashMap(size * 2 + 1, 0.7f);
    // unitToPegMap is the map of a chain to its corresponding (cfg node --> peg node ) map.
    unitToPegMap = new HashMap(size * 2 + 1, 0.7f);
    startToThread = new HashMap(size * 2 + 1, 0.7f);
    startToAllocNodes = new HashMap(size * 2 + 1, 0.7f);
    waitingNodes = new HashMap<String, FlowSet>(size * 2 + 1, 0.7f);
    joinStmtToThread = new HashMap<JPegStmt, Chain>();
    threadNo = new HashMap();
    threadNameToStart = new HashMap();
    this.allocNodeToObj = new HashMap<AllocNode, String>(size * 2 + 1, 0.7f);
    allocNodeToThread = new HashMap<AllocNode, PegChain>(size * 2 + 1, 0.7f);
    notifyAll = new HashMap<String, Set<JPegStmt>>(size * 2 + 1, 0.7f);

    methodsNeedingInlining = new HashSet();
    allNodes = new ArraySparseSet();
    canNotBeCompacted = new HashSet();
    threadAllocSites = new HashSet();
    specialJoin = new HashSet<JPegStmt>();
    //       if(Main.isVerbose)
    //   System.out.println("     Constructing PegGraph...");

    // if(Main.isProfilingOptimization)
    //   Main.graphTimer.start();
    // make a peg for debug
    /*
    mainPegChain = new HashChain();
    specialTreatment1();
    */
    // end make a peg

    UnitGraph mainUnitGraph = new CompleteUnitGraph(body);
    //	mainPegChain = new HashChain();
    mainPegChain =
        new PegChain(
            callGraph,
            hierarchy,
            pag,
            threadAllocSites,
            methodsNeedingInlining,
            allocNodes,
            inlineSites,
            synchObj,
            multiRunAllocNodes,
            allocNodeToObj,
            body,
            sm,
            threadName,
            true,
            this);

    // testPegChain();

    //		System.out.println("finish building chain");
    // testStartToThread();
    // buildSuccessor(mainUnitGraph, mainPegChain,addExceptionEdges);
    //	buildSuccessorForExtendingMethod(mainPegChain);
    // testSet(exceHandlers, "exceHandlers");
    buildSuccessor(mainPegChain);
    // System.out.println("finish building successors");
    // unmodifiableSuccs(mainPegChain);
    // testUnitToSucc );
    buildPredecessor(mainPegChain);
    // System.out.println("finish building predcessors");
    // unmodifiablePreds(mainPegChain);
    // testSynch();
    addMonitorStmt();
    addTag();
    //	System.out.println(this.toString());
    buildHeadsAndTails();

    // testIterator();
    // testWaitingNodes();

    //	System.out.println("finish building heads and tails");

    // testSet(canNotBeCompacted, "canNotBeCompacted");
    //	computeEdgeAndThreadNo();
    //	testExtendingPoints();
    //	testUnitToSucc();

    // testPegChain();
    /*	if (print) {
    PegToDotFile printer1 = new PegToDotFile(this, false, sm.getName());
    }
    */
    try {
      fileWriter.flush();
      fileWriter.close();
    } catch (IOException io) {
      System.err.println("Errors occur during close file  " + logFile.getName());
      //        throw io;
    }
    // System.out.println("==threadAllocaSits==\n"+threadAllocSites.toString());

  }
예제 #23
0
  /** 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)));
      }
    }
  }
예제 #24
0
  void doWeave(
      SootMethod method,
      LocalGeneratorEx lg,
      AdviceApplication adviceappl,
      Residue residue,
      WeavingContext wc) {
    AdviceInliner.v().addShadowMethod(method);
    ShadowPoints shadowpoints = adviceappl.shadowmatch.sp;
    AbstractAdviceDecl advicedecl = adviceappl.advice;

    Body b = method.getActiveBody();
    Chain units = b.getUnits().getNonPatchingChain();

    // end of shadow
    Stmt endshadow = shadowpoints.getEnd();

    NopStmt nop2 = Jimple.v().newNopStmt();
    GotoStmt goto1 = Jimple.v().newGotoStmt(nop2);
    if (advicedecl instanceof CflowSetup) {
      Tagger.tagStmt(goto1, InstructionKindTag.CFLOW_EXIT);
    } else if (advicedecl instanceof PerCflowSetup) {
      Tagger.tagStmt(goto1, InstructionKindTag.PERCFLOW_EXIT);
    } else if (advicedecl instanceof DeclareSoft) {
      Tagger.tagStmt(goto1, InstructionKindTag.EXCEPTION_SOFTENER);
    } else if (advicedecl instanceof DeclareMessage) {
      Tagger.tagStmt(goto1, InstructionKindTag.DECLARE_MESSAGE);
    } else {
      Tagger.tagStmt(goto1, InstructionKindTag.AFTER_THROWING_HANDLER);
    }
    Tagger.tagStmt(goto1, new InstructionSourceTag(advicedecl.sourceId));
    Tagger.tagStmt(goto1, new InstructionShadowTag(adviceappl.shadowmatch.shadowId));
    units.insertBefore(nop2, endshadow);
    units.insertBefore(goto1, nop2);

    // have ...
    //    goto1:      goto nop2;
    //    nop2:       nop;
    //    endshadow:  nop;

    RefType catchType = getCatchType();
    Local exception = lg.generateLocal(catchType, "exception");
    bindException(wc, advicedecl, exception);

    CaughtExceptionRef exceptRef = Jimple.v().newCaughtExceptionRef();
    IdentityStmt idStmt = Jimple.v().newIdentityStmt(exception, exceptRef);
    units.insertAfter(idStmt, goto1);

    ThrowStmt throwStmt = Jimple.v().newThrowStmt(exception);
    throwStmt.addTag(new ThrowCreatedByCompilerTag());

    if (advicedecl instanceof CflowSetup) {
      Tagger.tagStmt(throwStmt, InstructionKindTag.CFLOW_EXIT);
    } else if (advicedecl instanceof PerCflowSetup) {
      Tagger.tagStmt(throwStmt, InstructionKindTag.PERCFLOW_EXIT);
    } else if (advicedecl instanceof DeclareSoft) {
      Tagger.tagStmt(throwStmt, InstructionKindTag.EXCEPTION_SOFTENER);
    } else if (advicedecl instanceof DeclareMessage) {
      Tagger.tagStmt(throwStmt, InstructionKindTag.DECLARE_MESSAGE);
    } else {
      Tagger.tagStmt(throwStmt, InstructionKindTag.AFTER_THROWING_HANDLER);
    }
    Tagger.tagStmt(throwStmt, new InstructionSourceTag(advicedecl.sourceId));
    Tagger.tagStmt(throwStmt, new InstructionShadowTag(adviceappl.shadowmatch.shadowId));

    // store shadow/source tag for this residue in weaving context
    if (advicedecl instanceof CflowSetup) {
      wc.setKindTag(InstructionKindTag.CFLOW_TEST);
    }
    if (advicedecl instanceof PerCflowSetup) {
      wc.setKindTag(InstructionKindTag.CFLOW_TEST);
    }
    wc.setShadowTag(new InstructionShadowTag(adviceappl.shadowmatch.shadowId));
    wc.setSourceTag(new InstructionSourceTag(adviceappl.advice.sourceId));

    Stmt endresidue = residue.codeGen(method, lg, units, idStmt, throwStmt, true, wc);

    // have ...
    //    java.lang.Exception exception;
    //
    //    goto1:      goto nop2;
    //    idStmt:     exception := @caughtexception
    //    nop2:       nop;
    //    endshadow:  nop;

    units.insertAfter(throwStmt, endresidue);

    Chain invokestmts = advicedecl.makeAdviceExecutionStmts(adviceappl, lg, wc);

    for (Iterator stmtlist = invokestmts.iterator(); stmtlist.hasNext(); ) {
      Stmt nextstmt = (Stmt) stmtlist.next();
      units.insertBefore(nextstmt, throwStmt);
    }

    if (method.getName().equals("<clinit>"))
    // have to handle case of ExceptionInInitialzerError
    { //  if (exception instanceof java.lang.ExceptionInIntializerError)
      //     throw (exception);
      debug("Adding extra check in clinit");

      Local isInitError = lg.generateLocal(soot.BooleanType.v(), "isInitError");

      Stmt assignbool =
          Jimple.v()
              .newAssignStmt(
                  isInitError,
                  Jimple.v()
                      .newInstanceOfExpr(
                          exception, RefType.v("java.lang.ExceptionInInitializerError")));

      Stmt ifstmt =
          Jimple.v().newIfStmt(Jimple.v().newNeExpr(isInitError, IntConstant.v(0)), throwStmt);

      //	     ThrowStmt throwInitError = Jimple.v().newThrowStmt(exception);

      //	     units.insertAfter(throwInitError, idStmt);
      units.insertAfter(ifstmt, idStmt);
      units.insertAfter(assignbool, idStmt);
    }

    Stmt beginshadow = shadowpoints.getBegin();
    Stmt begincode = (Stmt) units.getSuccOf(beginshadow);

    // have ...
    //    java.lang.Exception exception;
    //    <AspectType> theAspect;
    //
    //    beginshadow:   nop
    //    begincode:     <some statement>
    //       ....        <stuff in between>
    //    goto1:         goto nop2;
    //    idStmt:        exception := @caughtexception;
    //    assignStmt:    theAspect = new AspectOf();
    //             .... invoke statements ....
    //    throwStmt:     throw exception;
    //    nop2:          nop;
    //    endshadow:     nop;

    Chain traps = b.getTraps();
    Trap t = traps.size() > 0 ? (Trap) traps.getFirst() : null;

    // assume no exception ranges overlap with this one; make sure
    // we go after any that would be enclosed within this one.
    while (t != null
        && (units.follows(t.getBeginUnit(), begincode)
            || (t.getBeginUnit() == begincode && units.follows(idStmt, t.getEndUnit()))))
      t = (Trap) traps.getSuccOf(t);

    Trap newt = Jimple.v().newTrap(catchType.getSootClass(), begincode, idStmt, idStmt);

    if (t == null) traps.addLast(newt);
    else traps.insertBefore(newt, t);

    //  added
    //     catch java.lang.Throwable
    //         from begincode upto idStmt handlewith idStmt

  } // method doWeave
예제 #25
0
  // This method adds the monitorenter/exit statements into whichever pegChain contains the
  // corresponding node statement
  protected void addMonitorStmt() {
    // System.out.println("====entering addMonitorStmt");
    if (synch.size() > 0) {
      // System.out.println("synch: "+synch);
      Iterator<List> it = synch.iterator();
      while (it.hasNext()) {
        List list = it.next();

        JPegStmt node = (JPegStmt) list.get(0);
        JPegStmt enter = (JPegStmt) list.get(1);
        JPegStmt exit = (JPegStmt) list.get(2);
        //		System.out.println("monitor node: "+node);
        // System.out.println("monitor enter: "+enter);
        // System.out.println("monitor exit: "+exit);
        // add for test
        // System.out.println("allNodes contains node: "+allNodes.contains(node));
        // end add for test

        {
          if (!mainPegChain.contains(node)) {

            boolean find = false;
            // System.out.println("main chain does not contain node");
            Set maps = startToThread.entrySet();
            // System.out.println("size of startToThread: "+startToThread.size());
            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();
                //	testPegChain(chain);
                if (chain.contains(node)) {
                  find = true;
                  // System.out.println("---find it---");
                  chain.add(enter);
                  chain.add(exit);
                  break;
                }
              }
            }
            if (find == false) {
              System.err.println("fail to find stmt: " + node + " in chains!");
              System.exit(1);
            }

            // this.toString();
          } else {
            mainPegChain.add(enter);
            mainPegChain.add(exit);
          }
        }

        allNodes.add(enter);
        allNodes.add(exit);

        insertBefore(node, enter);
        insertAfter(node, exit);
      }
    }
    // add for test
    /*
    {
    // System.out.println("===main peg chain===");
     //testPegChain(mainPegChain);
      //System.out.println("===end main peg chain===");
       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();
       testPegChain(chain);
       }
       }
       }
       */
    //	System.out.println(this.toString());
    // end add for test
  }
예제 #26
0
  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
    }
  }
예제 #27
0
  /*
   * Generates the sequence of instructions needed to instrument ifStmtUnit
   *
   * @param ifStmtUnit: the unit to be instrumented
   * @return A Chain of Units that represent the instrumentation of ifStmtUnit
   */
  private static Chain<Unit> generateInstrumentationUnits(Unit ifStmtUnit, Body b) {

    AbstractBinopExpr expression =
        (AbstractBinopExpr)
            ((JIfStmt) ifStmtUnit).getCondition(); // implementation of AbstractBinopExpr
    Value operand1 = expression.getOp1();
    Value operand2 = expression.getOp2();

    JimpleLocal op1Local = (JimpleLocal) operand1;

    // Local localOperand = Jimple.v().newLocal("op1", operand1.getType());
    // b.getLocals().add(localOperand);

    // We need to use these operand as Locals or constants
    /**
     * JimpleLocal test; if(operand1 instanceof soot.jimple.internal.JimpleLocal) test =
     * (JimpleLocal)operand1; else test = null;
     */
    String op = expression.getClass().toString();

    Chain<Unit> resultUnits = new HashChain<Unit>();
    Local tmpRef;
    // Add locals directely on the top of the body, java.io.printStream tmpRef
    tmpRef = Jimple.v().newLocal("tmpRef", RefType.v("java.io.PrintStream"));
    b.getLocals().addFirst(tmpRef);

    // add "tmpRef = java.lang.System.out"
    resultUnits.add(
        Jimple.v()
            .newAssignStmt(
                tmpRef,
                Jimple.v()
                    .newStaticFieldRef(
                        Scene.v()
                            .getField("<java.lang.System: java.io.PrintStream out>")
                            .makeRef())));
    {
      SootMethod toCall =
          Scene.v().getMethod("<java.io.PrintStream: void println(java.lang.String)>");
      resultUnits.add(
          Jimple.v()
              .newInvokeStmt(
                  Jimple.v()
                      .newVirtualInvokeExpr(
                          tmpRef, toCall.makeRef(), StringConstant.v("Operande 1: "))));
      resultUnits.add(
          Jimple.v()
              .newInvokeStmt(
                  Jimple.v()
                      .newVirtualInvokeExpr(
                          tmpRef,
                          toCall.makeRef(),
                          StringConstant.v(operand1.getClass().toString()))));

      resultUnits.add(
          Jimple.v()
              .newInvokeStmt(
                  Jimple.v()
                      .newVirtualInvokeExpr(
                          tmpRef, toCall.makeRef(), StringConstant.v("Operande 2:"))));

      resultUnits.add(
          Jimple.v()
              .newInvokeStmt(
                  Jimple.v()
                      .newVirtualInvokeExpr(
                          tmpRef,
                          toCall.makeRef(),
                          StringConstant.v(operand2.getClass().toString()))));

      resultUnits.add(
          Jimple.v()
              .newInvokeStmt(
                  Jimple.v()
                      .newVirtualInvokeExpr(
                          tmpRef, toCall.makeRef(), StringConstant.v("Operateur: "))));

      resultUnits.add(
          Jimple.v()
              .newInvokeStmt(
                  Jimple.v().newVirtualInvokeExpr(tmpRef, toCall.makeRef(), StringConstant.v(op))));
    }

    return resultUnits;
  }
  /**
   * This method pushes all newExpr down to be the stmt directly before every invoke of the init
   * only if they are in the types list
   */
  public void internalTransform(Body b, String phaseName, Map options) {
    JimpleBody body = (JimpleBody) b;

    if (Options.v().verbose())
      G.v().out.println("[" + body.getMethod().getName() + "] Folding Jimple constructors...");

    Chain units = body.getUnits();
    List<Unit> stmtList = new ArrayList<Unit>();
    stmtList.addAll(units);

    Iterator<Unit> it = stmtList.iterator();
    Iterator<Unit> nextStmtIt = stmtList.iterator();
    // start ahead one
    nextStmtIt.next();

    SmartLocalDefs localDefs = SmartLocalDefsPool.v().getSmartLocalDefsFor(body);
    UnitGraph graph = localDefs.getGraph();
    LocalUses localUses = new SimpleLocalUses(graph, localDefs);

    /* fold in NewExpr's with specialinvoke's */
    while (it.hasNext()) {
      Stmt s = (Stmt) it.next();

      if (!(s instanceof AssignStmt)) continue;

      /* this should be generalized to ArrayRefs */
      // only deal with stmts that are an local = newExpr
      Value lhs = ((AssignStmt) s).getLeftOp();
      if (!(lhs instanceof Local)) continue;

      Value rhs = ((AssignStmt) s).getRightOp();
      if (!(rhs instanceof NewExpr)) continue;

      // check if very next statement is invoke -->
      // this indicates there is no control flow between
      // new and invoke and should do nothing
      if (nextStmtIt.hasNext()) {
        Stmt next = (Stmt) nextStmtIt.next();
        if (next instanceof InvokeStmt) {
          InvokeStmt invoke = (InvokeStmt) next;

          if (invoke.getInvokeExpr() instanceof SpecialInvokeExpr) {
            SpecialInvokeExpr invokeExpr = (SpecialInvokeExpr) invoke.getInvokeExpr();
            if (invokeExpr.getBase() == lhs) {
              break;
            }
          }
        }
      }

      // check if new is in the types list - only process these
      if (!types.contains(((NewExpr) rhs).getType())) continue;

      List lu = localUses.getUsesOf(s);
      Iterator luIter = lu.iterator();
      boolean MadeNewInvokeExpr = false;

      while (luIter.hasNext()) {
        Unit use = ((UnitValueBoxPair) (luIter.next())).unit;
        if (!(use instanceof InvokeStmt)) continue;
        InvokeStmt is = (InvokeStmt) use;
        if (!(is.getInvokeExpr() instanceof SpecialInvokeExpr)
            || lhs != ((SpecialInvokeExpr) is.getInvokeExpr()).getBase()) continue;

        // make a new one here
        AssignStmt constructStmt =
            Jimple.v()
                .newAssignStmt(((DefinitionStmt) s).getLeftOp(), ((DefinitionStmt) s).getRightOp());
        constructStmt.setRightOp(Jimple.v().newNewExpr(((NewExpr) rhs).getBaseType()));
        MadeNewInvokeExpr = true;

        // redirect jumps
        use.redirectJumpsToThisTo(constructStmt);
        // insert new one here
        units.insertBefore(constructStmt, use);

        constructStmt.addTag(s.getTag("SourceLnPosTag"));
      }
      if (MadeNewInvokeExpr) {
        units.remove(s);
      }
    }
  }
예제 #29
0
  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);
    }
  }
예제 #30
0
  /*
  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);
        }
      }
    }
  }