예제 #1
0
  private ArrayList<AnnotationElem> handleAnnotationElement(
      AnnotationElement ae, List<? extends EncodedValue> evList) {
    ArrayList<AnnotationElem> aelemList = new ArrayList<AnnotationElem>();

    for (EncodedValue ev : evList) {
      int type = ev.getValueType();
      AnnotationElem elem = null;
      Debug.printDbg("encoded value type: ", type);
      switch (type) {
        case 0x00: // BYTE
          {
            ByteEncodedValue v = (ByteEncodedValue) ev;
            elem = new AnnotationIntElem(v.getValue(), 'B', ae.getName());
            break;
          }
        case 0x02: // SHORT
          {
            ShortEncodedValue v = (ShortEncodedValue) ev;
            elem = new AnnotationIntElem(v.getValue(), 'S', ae.getName());
            break;
          }
        case 0x03: // CHAR
          {
            CharEncodedValue v = (CharEncodedValue) ev;
            elem = new AnnotationIntElem(v.getValue(), 'C', ae.getName());
            break;
          }
        case 0x04: // INT
          {
            IntEncodedValue v = (IntEncodedValue) ev;
            elem = new AnnotationIntElem(v.getValue(), 'I', ae.getName());
            break;
          }
        case 0x06: // LONG
          {
            LongEncodedValue v = (LongEncodedValue) ev;
            elem = new AnnotationLongElem(v.getValue(), 'J', ae.getName());
            break;
          }
        case 0x10: // FLOAT
          {
            FloatEncodedValue v = (FloatEncodedValue) ev;
            elem = new AnnotationFloatElem(v.getValue(), 'F', ae.getName());
            break;
          }
        case 0x11: // DOUBLE
          {
            DoubleEncodedValue v = (DoubleEncodedValue) ev;
            elem = new AnnotationDoubleElem(v.getValue(), 'D', ae.getName());
            break;
          }
        case 0x17: // STRING
          {
            StringEncodedValue v = (StringEncodedValue) ev;
            elem = new AnnotationStringElem(v.getValue(), 's', ae.getName());
            Debug.printDbg("value for string: ", v.getValue());
            break;
          }
        case 0x18: // TYPE
          {
            TypeEncodedValue v = (TypeEncodedValue) ev;
            elem = new AnnotationClassElem(DexType.toSootAT(v.getValue()), 'c', ae.getName());
            break;
          }
        case 0x19: // FIELD (Dalvik specific?)
          {
            FieldEncodedValue v = (FieldEncodedValue) ev;
            FieldReference fr = v.getValue();
            String fieldSig = "";
            fieldSig += DexType.toSootAT(fr.getDefiningClass()) + ": ";
            fieldSig += DexType.toSootAT(fr.getType()) + " ";
            fieldSig += fr.getName();
            Debug.printDbg("FIELD: ", fieldSig);
            elem = new AnnotationStringElem(fieldSig, 'f', ae.getName());
            break;
          }
        case 0x1a: // METHOD (Dalvik specific?)
          {
            MethodEncodedValue v = (MethodEncodedValue) ev;
            MethodReference mr = v.getValue();

            String className = DexType.toSootICAT(mr.getDefiningClass());
            String returnType = DexType.toSootAT(mr.getReturnType());
            String methodName = mr.getName();
            String parameters = "";
            for (CharSequence p : mr.getParameterTypes()) {
              parameters += DexType.toSootAT(p.toString());
            }
            String mSig = className + " |" + methodName + " |" + parameters + " |" + returnType;
            elem = new AnnotationStringElem(mSig, 'M', ae.getName());
            break;
          }
        case 0x1b: // ENUM : Warning -> encoding Dalvik specific!
          {
            EnumEncodedValue v = (EnumEncodedValue) ev;
            FieldReference fr = v.getValue();
            elem =
                new AnnotationEnumElem(
                    DexType.toSootAT(fr.getType()).toString(), fr.getName(), 'e', ae.getName());
            break;
          }
        case 0x1c: // ARRAY
          {
            ArrayEncodedValue v = (ArrayEncodedValue) ev;
            ArrayList<AnnotationElem> l = handleAnnotationElement(ae, v.getValue());
            if (l != null) elem = new AnnotationArrayElem(l, '[', ae.getName());
            break;
          }
        case 0x1d: // ANNOTATION
          {
            AnnotationEncodedValue v = (AnnotationEncodedValue) ev;
            AnnotationTag t = new AnnotationTag(DexType.toSootAT(v.getType()).toString());
            for (AnnotationElement newElem : v.getElements()) {
              List<EncodedValue> l = new ArrayList<EncodedValue>();
              l.add(newElem.getValue());
              List<AnnotationElem> aList = handleAnnotationElement(newElem, l);
              if (aList != null) for (AnnotationElem e : aList) t.addElem(e);
            }
            elem = new AnnotationAnnotationElem(t, '@', ae.getName());
            break;
          }
        case 0x1e: // NULL (Dalvik specific?)
          {
            elem = new AnnotationStringElem(null, 'N', ae.getName());
            break;
          }
        case 0x1f: // BOOLEAN
          {
            BooleanEncodedValue v = (BooleanEncodedValue) ev;
            elem = new AnnotationBooleanElem(v.getValue(), 'Z', ae.getName());
            break;
          }
        default:
          {
            throw new RuntimeException("Unknown annotation element 0x" + Integer.toHexString(type));
          }
      } // switch (type)

      if (elem != null) aelemList.add(elem);
    } // for (EncodedValue)

    return aelemList;
  }
예제 #2
0
  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;
  }
예제 #3
0
  /**
   * Converts method and method parameters annotations from Dexlib to Jimple
   *
   * @param h
   * @param method
   */
  void handleMethodAnnotation(Host h, Method method) {
    Set<? extends Annotation> aSet = method.getAnnotations();
    if (!(aSet == null || aSet.isEmpty())) {
      List<Tag> tags = handleAnnotation(aSet, null);
      if (tags != null)
        for (Tag t : tags)
          if (t != null) {
            h.addTag(t);
            Debug.printDbg("add method annotation: ", t);
          }
    }

    ArrayList<String> parameterNames = new ArrayList<String>();
    boolean addParameterNames = false;
    for (MethodParameter p : method.getParameters()) {
      String name = p.getName();
      parameterNames.add(name);
      if (name != null) addParameterNames = true;
    }
    if (addParameterNames) {
      h.addTag(new ParamNamesTag(parameterNames));
    }

    // Is there any parameter annotation?
    boolean doParam = false;
    List<? extends MethodParameter> parameters = method.getParameters();
    for (MethodParameter p : parameters) {
      Debug.printDbg("parameter ", p, " annotations: ", p.getAnnotations());
      if (p.getAnnotations().size() > 0) {
        doParam = true;
        break;
      }
    }

    if (doParam) {
      VisibilityParameterAnnotationTag tag =
          new VisibilityParameterAnnotationTag(
              parameters.size(), AnnotationConstants.RUNTIME_VISIBLE);
      for (MethodParameter p : parameters) {
        List<Tag> tags = handleAnnotation(p.getAnnotations(), null);

        // If we have no tag for this parameter, add a placeholder
        // so that we keep the order intact.
        if (tags == null) {
          tag.addVisibilityAnnotation(null);
          continue;
        }

        VisibilityAnnotationTag paramVat =
            new VisibilityAnnotationTag(AnnotationConstants.RUNTIME_VISIBLE);
        tag.addVisibilityAnnotation(paramVat);

        for (Tag t : tags) {
          if (t == null) continue;

          AnnotationTag vat = null;
          if (!(t instanceof VisibilityAnnotationTag)) {
            if (t instanceof DeprecatedTag) {
              vat = new AnnotationTag("Ljava/lang/Deprecated;");
            } else if (t instanceof SignatureTag) {
              SignatureTag sig = (SignatureTag) t;

              ArrayList<AnnotationElem> sigElements = new ArrayList<AnnotationElem>();
              for (String s : SootToDexUtils.splitSignature(sig.getSignature()))
                sigElements.add(new AnnotationStringElem(s, 's', "value"));

              AnnotationElem elem = new AnnotationArrayElem(sigElements, 's', "value");
              vat = new AnnotationTag("Ldalvik/annotation/Signature;", Collections.singleton(elem));
            } else {
              throw new RuntimeException(
                  "error: unhandled tag for parameter annotation in method " + h + " (" + t + ").");
            }
          } else {
            vat = ((VisibilityAnnotationTag) t).getAnnotations().get(0);
          }

          Debug.printDbg("add parameter annotation: ", t);
          paramVat.addAnnotation(vat);
        }
      }
      if (tag.getVisibilityAnnotations().size() > 0) h.addTag(tag);
    }
  }