Example #1
0
 public boolean localRedefined(Local lLocal, List<ValueBox> rUseBoxes) {
   for (ValueBox rUseBox : rUseBoxes) {
     Value rBoxValue = rUseBox.getValue();
     if (rBoxValue instanceof Local) {
       Local rLocal = (Local) rBoxValue;
       if (lLocal.getName().equals(rLocal.getName())
           && lLocal.getType().toString().equals(rLocal.getType().toString())) {
         return true;
       }
     }
   }
   return false;
 }
Example #2
0
  public void outADeclaration(ADeclaration node) {
    List localNameList = (List) mProductions.removeLast();
    Type type = (Type) mProductions.removeLast();
    Iterator it = localNameList.iterator();
    List localList = new ArrayList();

    while (it.hasNext()) {
      Local l = Jimple.v().newLocal((String) it.next(), type);
      mLocals.put(l.getName(), l);
      localList.add(l);
    }
    mProductions.addLast(localList);
  }
Example #3
0
 public static Value getVaribleInMethod(SootMethod method, String name) {
   // TODO Auto-generated method stub
   Chain<Local> localVars = method.getActiveBody().getLocals();
   for (Local local : localVars) {
     if (local.getName().equals(name)) return local;
   }
   try {
     throw new Exception("No variable with name " + name + " in method " + method);
   } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   return null;
 }
Example #4
0
  private Map<Pair<Unit, Set<String>>, Set<Unit>> createProvidesConfigMap(
      Collection<Unit> unitsInSelection, LiftedReachingDefinitions reachingDefinitions, Body body) {
    Map<Pair<Unit, Set<String>>, Set<Unit>> unitConfigurationMap =
        new HashMap<Pair<Unit, Set<String>>, Set<Unit>>();

    for (Unit unitFromSelection : unitsInSelection) {
      if (unitFromSelection instanceof DefinitionStmt) {
        /*
         * exclude definitions when it's $temp on the leftOp.
         */
        DefinitionStmt definition = (DefinitionStmt) unitFromSelection;
        Local leftOp = (Local) definition.getLeftOp();
        if (leftOp.getName().charAt(0) == '$') {
          continue;
        }

        System.out.println("Definition:" + definition);

        // for every unit in the body...
        Iterator<Unit> iterator = body.getUnits().snapshotIterator();
        while (iterator.hasNext()) {
          Unit nextUnit = iterator.next();
          LiftedFlowSet<Collection<Set<Object>>> liftedFlowAfter =
              reachingDefinitions.getFlowAfter(nextUnit);
          Set<String>[] configurations = liftedFlowAfter.getConfigurations();
          FlowSet[] lattices = liftedFlowAfter.getLattices();
          // and for every configuration...
          for (int configurationIndex = 0;
              configurationIndex < configurations.length;
              configurationIndex++) {
            FlowSet flowSet = lattices[configurationIndex];
            Set<String> currConfiguration = configurations[configurationIndex];
            FeatureTag nextUnitTag = (FeatureTag) nextUnit.getTag("FeatureTag");

            // if the unit belongs to the current configuration...
            if (nextUnitTag.belongsToConfiguration(currConfiguration)) {

              // if the definition reaches this unit...
              if (flowSet.contains(definition)) {
                List<ValueBox> useBoxes = nextUnit.getUseBoxes();
                for (ValueBox vbox : useBoxes) {
                  /*
                   * and the definition is used, add to the
                   * map...
                   */
                  if (vbox.getValue().equivTo(leftOp)) {
                    Pair<Unit, Set<String>> currentPair =
                        new Pair<Unit, Set<String>>(definition, currConfiguration);
                    Set<Unit> unitConfigurationReachesSet = unitConfigurationMap.get(currentPair);

                    if (unitConfigurationReachesSet == null) {
                      unitConfigurationReachesSet = new HashSet<Unit>();
                      unitConfigurationReachesSet.add(nextUnit);
                      unitConfigurationMap.put(currentPair, unitConfigurationReachesSet);
                    } else {
                      unitConfigurationReachesSet.add(nextUnit);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    return unitConfigurationMap;
  }
  private static boolean internalAggregate(
      StmtBody body, Map<ValueBox, Zone> boxToZone, boolean onlyStackVars) {
    LocalUses localUses;
    LocalDefs localDefs;
    ExceptionalUnitGraph graph;
    boolean hadAggregation = false;
    Chain<Unit> units = body.getUnits();

    graph = new ExceptionalUnitGraph(body);
    localDefs = new SmartLocalDefs(graph, new SimpleLiveLocals(graph));
    localUses = new SimpleLocalUses(graph, localDefs);

    List<Unit> unitList = new PseudoTopologicalOrderer<Unit>().newList(graph, false);
    for (Unit u : unitList) {
      if (!(u instanceof AssignStmt)) continue;
      AssignStmt s = (AssignStmt) u;

      Value lhs = s.getLeftOp();
      if (!(lhs instanceof Local)) continue;
      Local lhsLocal = (Local) lhs;

      if (onlyStackVars && !lhsLocal.getName().startsWith("$")) continue;

      List<UnitValueBoxPair> lu = localUses.getUsesOf(s);
      if (lu.size() != 1) continue;

      UnitValueBoxPair usepair = lu.get(0);
      Unit use = usepair.unit;
      ValueBox useBox = usepair.valueBox;

      List<Unit> ld = localDefs.getDefsOfAt(lhsLocal, use);
      if (ld.size() != 1) continue;

      // Check to make sure aggregation pair in the same zone
      if (boxToZone.get(s.getRightOpBox()) != boxToZone.get(usepair.valueBox)) {
        continue;
      }

      /* we need to check the path between def and use */
      /* to see if there are any intervening re-defs of RHS */
      /* in fact, we should check that this path is unique. */
      /* if the RHS uses only locals, then we know what
      to do; if RHS has a method invocation f(a, b,
      c) or field access, we must ban field writes, other method
      calls and (as usual) writes to a, b, c. */

      boolean cantAggr = false;
      boolean propagatingInvokeExpr = false;
      boolean propagatingFieldRef = false;
      boolean propagatingArrayRef = false;
      ArrayList<FieldRef> fieldRefList = new ArrayList<FieldRef>();

      LinkedList<Value> localsUsed = new LinkedList<Value>();
      for (ValueBox vb : s.getUseBoxes()) {
        Value v = vb.getValue();
        if (v instanceof Local) localsUsed.add(v);
        else if (v instanceof InvokeExpr) propagatingInvokeExpr = true;
        else if (v instanceof ArrayRef) propagatingArrayRef = true;
        else if (v instanceof FieldRef) {
          propagatingFieldRef = true;
          fieldRefList.add((FieldRef) v);
        }
      }

      // look for a path from s to use in graph.
      // only look in an extended basic block, though.

      List<Unit> path = graph.getExtendedBasicBlockPathBetween(s, use);

      if (path == null) continue;

      Iterator<Unit> pathIt = path.iterator();

      // skip s.
      if (pathIt.hasNext()) pathIt.next();

      while (pathIt.hasNext() && !cantAggr) {
        Stmt between = (Stmt) (pathIt.next());

        if (between != use) {
          // Check for killing definitions

          for (ValueBox vb : between.getDefBoxes()) {
            Value v = vb.getValue();
            if (localsUsed.contains(v)) {
              cantAggr = true;
              break;
            }

            if (propagatingInvokeExpr || propagatingFieldRef || propagatingArrayRef) {
              if (v instanceof FieldRef) {
                if (propagatingInvokeExpr) {
                  cantAggr = true;
                  break;
                } else if (propagatingFieldRef) {
                  // Can't aggregate a field access if passing a definition of a field
                  // with the same name, because they might be aliased
                  for (FieldRef fieldRef : fieldRefList) {
                    if (((FieldRef) v).getField() == fieldRef.getField()) {
                      cantAggr = true;
                      break;
                    }
                  }
                }
              } else if (v instanceof ArrayRef) {
                if (propagatingInvokeExpr) {
                  // Cannot aggregate an invoke expr past an array write
                  cantAggr = true;
                  break;
                } else if (propagatingArrayRef) {
                  // cannot aggregate an array read past a write
                  // this is somewhat conservative
                  // (if types differ they may not be aliased)

                  cantAggr = true;
                  break;
                }
              }
            }
          }

          // Make sure not propagating past a {enter,exit}Monitor
          if (propagatingInvokeExpr && between instanceof MonitorStmt) cantAggr = true;
        }

        // Check for intervening side effects due to method calls
        if (propagatingInvokeExpr || propagatingFieldRef || propagatingArrayRef) {
          for (final ValueBox box : between.getUseBoxes()) {
            if (between == use && box == useBox) {
              // Reached use point, stop looking for
              // side effects
              break;
            }

            Value v = box.getValue();

            if (v instanceof InvokeExpr
                || (propagatingInvokeExpr && (v instanceof FieldRef || v instanceof ArrayRef))) {
              cantAggr = true;
              break;
            }
          }
        }
      }

      // we give up: can't aggregate.
      if (cantAggr) {
        continue;
      }
      /* assuming that the d-u chains are correct, */
      /* we need not check the actual contents of ld */

      Value aggregatee = s.getRightOp();

      if (usepair.valueBox.canContainValue(aggregatee)) {
        boolean wasSimpleCopy = isSimpleCopy(usepair.unit);
        usepair.valueBox.setValue(aggregatee);
        units.remove(s);
        hadAggregation = true;
        // clean up the tags. If s was not a simple copy, the new statement should get
        // the tags of s.
        // OK, this fix was wrong. The condition should not be
        // "If s was not a simple copy", but rather "If usepair.unit
        // was a simple copy". This way, when there's a load of a constant
        // followed by an invoke, the invoke gets the tags.
        if (wasSimpleCopy) {
          // usepair.unit.removeAllTags();
          usepair.unit.addAllTagsOf(s);
        }
      } else {
        /*
        if(Options.v().verbose())
        {
            G.v().out.println("[debug] failed aggregation");
              G.v().out.println("[debug] tried to put "+aggregatee+
                             " into "+usepair.stmt +
                             ": in particular, "+usepair.valueBox);
              G.v().out.println("[debug] aggregatee instanceof Expr: "
                             +(aggregatee instanceof Expr));
        }*/
      }
    }
    return hadAggregation;
  }
Example #6
0
  @Override
  @SuppressWarnings("rawtypes")
  public void internalTransform(Body body, String phaseName, Map options) {
    final SootMethod method = body.getMethod();

    System.out.println();
    System.out.println(
        method.getDeclaringClass().getName() + " :: " + method.getBytecodeSignature());

    System.out.println("--- locals ---");
    for (Local l : body.getLocals()) System.out.println(" " + l.getName() + " : " + l.getType());

    System.out.println("--- units ---");
    Unit[] units = body.getUnits().toArray(new Unit[0]);
    for (int i = 0; i < units.length; i++) {

      if (units[i].hasTag("StringTag")) {
        String info = ((StringTag) units[i].getTag("StringTag")).getInfo();
        if (info.endsWith("Pre")) System.out.println("\n      " + info);
      }

      System.out.printf("%4d ", i);

      if (units[i] instanceof GotoStmt) {
        System.out.println("goto " + getIndex(units, ((GotoStmt) units[i]).getTarget()));

      } else if (units[i] instanceof IfStmt) {
        System.out.println(
            "if "
                + ((IfStmt) units[i]).getCondition()
                + " goto "
                + getIndex(units, ((IfStmt) units[i]).getTarget()));

      } else if (units[i] instanceof TableSwitchStmt) {
        TableSwitchStmt sw = (TableSwitchStmt) units[i];
        System.out.println("tswitch(" + sw.getKey() + ")");
        final int lowIndex = sw.getLowIndex();
        for (int t = 0; t <= sw.getHighIndex() - lowIndex; t++)
          System.out.println(
              "      case " + (t + lowIndex) + ": goto " + getIndex(units, sw.getTarget(t)));
        System.out.println("      default: goto " + getIndex(units, sw.getDefaultTarget()));

      } else if (units[i] instanceof LookupSwitchStmt) {
        LookupSwitchStmt sw = (LookupSwitchStmt) units[i];
        System.out.println("lswitch(" + sw.getKey() + ")");
        for (int v = 0; v < sw.getTargetCount(); v++)
          System.out.println(
              "      case " + sw.getLookupValue(v) + ": goto " + getIndex(units, sw.getTarget(v)));
        System.out.println("      default: goto " + getIndex(units, sw.getDefaultTarget()));

      } else if (units[i] instanceof InvokeStmt) {
        System.out.println(toString(((InvokeStmt) units[i]).getInvokeExpr()));

      } else if (units[i] instanceof AssignStmt) {
        if (((AssignStmt) units[i]).getRightOp() instanceof InvokeExpr)
          System.out.println(
              ((AssignStmt) units[i]).getLeftOp()
                  + " = "
                  + toString((InvokeExpr) ((AssignStmt) units[i]).getRightOp()));
        else System.out.println(units[i]);

      } else {
        System.out.println(units[i]);
      }

      if (units[i].hasTag("StringTag")) {
        String info = ((StringTag) units[i].getTag("StringTag")).getInfo();

        if (!info.endsWith("Pre")) System.out.println("      " + info);
        if (info.endsWith("After")) System.out.println();
      }
    }

    System.out.println("--- traps ---");
    for (Trap t : body.getTraps())
      System.out.println(
          " ["
              + getIndex(units, t.getBeginUnit())
              + "-"
              + getIndex(units, t.getEndUnit())
              + ") -> ("
              + t.getException()
              + ") -> "
              + getIndex(units, t.getHandlerUnit()));

    System.out.println();
  }
 public void caseLocal(Local arg0) {
   m_output.append(" " + arg0.getName() + " ");
   m_previousLocal = arg0.getName();
 }