Example #1
0
  public static void fixDeclarations(SootClass sc) {
    for (Object s : sc.getFields().toArray()) {
      SootField sf = (SootField) s;
      fixField(sf);
    }

    for (FieldRef sf : map.keySet()) sf.setFieldRef(map.get(sf).makeRef());
  }
 private void analyzeField(SootMethod method, Value value) {
   FieldRef fr = (FieldRef) value;
   boolean fieldIsThreadLocal = tloa.isObjectThreadLocal(fr, method);
   if (fieldIsThreadLocal) {
     G.v().out.println("[lg.tlo] FIELD " + fr.toString() + " is thread-local in method " + method);
   } else {
     G.v()
         .out
         .println("[lg.tlo] FIELD " + fr.toString() + " is thread-shared in method " + method);
   }
 }
 private void searchRef(StringArrayRef ref, Loop loop) {
   String temp_name = ref.getArrayName();
   Iterator<Unit> iter = loop.getBodyBlocks().get(0).getBody().getUnits().iterator();
   while (iter.hasNext()) {
     Unit curr = iter.next();
     if (defines(curr, temp_name) && curr instanceof AssignStmt) {
       AssignStmt assign = (AssignStmt) curr;
       Value right = assign.getRightOp();
       if (right instanceof FieldRef) {
         FieldRef field_ref = (FieldRef) right;
         ref.setFieldSignature(field_ref.getField().getSignature());
         ref.setField(field_ref.getField().getName());
       }
     }
   }
 }
 public void fixFields() {
   Iterator<SootField> iter = m_fieldsToFix.keySet().iterator();
   while (iter.hasNext()) {
     SootField curr = iter.next();
     SootClass soot_class = curr.getDeclaringClass();
     SootField orig = curr;
     SootClass field_cls = curr.getDeclaringClass();
     if (shouldMap(field_cls)) {
       SootClass new_field_cls = getMapping(field_cls);
       curr = new_field_cls.getFieldByName(curr.getName());
     }
     Type type = curr.getType();
     if (type instanceof RefType) {
       RefType ref_type = (RefType) type;
       if (shouldMap(ref_type.getSootClass())) {
         SootClass new_class = getMapping(ref_type.getSootClass());
         curr.setType(new_class.getType());
       }
     } else if (type instanceof ArrayType) {
       ArrayType array_type = (ArrayType) type;
       if (array_type.baseType instanceof RefType == false) {
         continue;
       }
       RefType ref_type = (RefType) array_type.baseType;
       if (shouldMap(ref_type.getSootClass())) {
         SootClass new_class = getMapping(ref_type.getSootClass());
         ArrayType new_type = ArrayType.v(new_class.getType(), array_type.numDimensions);
         curr.setType(new_type);
       }
     }
     List<FieldRef> refs = m_fieldsToFix.get(orig);
     for (FieldRef ref : refs) {
       ref.setFieldRef(curr.makeRef());
     }
   }
 }
  /**
   * Returns <code>true</code> if both references may point to the same memory location. The current
   * implementation is conservative, as it does not take any points-to information into account.
   */
  public static boolean maybeSameLocation(Value abstrRef, Value programRef) {
    // arrays are handled through their "base" pointer
    assert !(abstrRef instanceof ArrayRef);
    assert !(programRef instanceof ArrayRef);

    if (abstrRef == programRef) return true;

    // handle primtive types
    Type abstrRefType = abstrRef.getType();
    Type programRefType = programRef.getType();
    if (programRefType instanceof PrimType) {
      // we don't track primitive types, just Strings, ClassLoaders, etc.
      // ...
      return false;
    }
    if (abstrRefType instanceof PrimType) {
      // we don't track primitive types, just Strings, ClassLoaders, etc.
      // ...
      throw new InternalError("abstraction ref type is " + abstrRefType);
    }

    if (abstrRef instanceof Local && programRef instanceof Local) {
      // two locals only point to the same memory locations if they are
      // the same
      return abstrRef == programRef;
    } else if (abstrRef instanceof FieldRef && programRef instanceof FieldRef) {
      FieldRef fieldRef = (FieldRef) abstrRef;
      FieldRef fieldRef2 = (FieldRef) programRef;
      // references point to the same location if class and field name are
      // identical;
      // note that we ignore the receiver object of InstanceFieldRefs
      return fieldRef
              .getField()
              .getDeclaringClass()
              .equals(fieldRef2.getField().getDeclaringClass())
          && fieldRef.getFieldRef().name().equals(fieldRef2.getFieldRef().name());
    } else {
      return false;
    }
  }
  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;
  }
 private Value mutate(Value value) {
   if (value instanceof FieldRef) {
     FieldRef ref = (FieldRef) value;
     SootField field = ref.getField();
     Type type = field.getType();
     if (type instanceof RefType) {
       RefType ref_type = (RefType) type;
       SootClass soot_class = ref_type.getSootClass();
       if (shouldMap(soot_class)) {
         addField(field, ref);
       }
     } else if (type instanceof ArrayType) {
       ArrayType array_type = (ArrayType) type;
       Type base_type = array_type.baseType;
       if (base_type instanceof RefType) {
         RefType ref_type = (RefType) base_type;
         SootClass soot_class = ref_type.getSootClass();
         if (shouldMap(soot_class)) {
           addField(field, ref);
         }
       }
     }
     SootClass soot_class = field.getDeclaringClass();
     if (shouldMap(soot_class)) {
       addField(field, ref);
     }
     return value;
   } else if (value instanceof InvokeExpr) {
     InvokeExpr expr = (InvokeExpr) value;
     SootMethodRef ref = expr.getMethodRef();
     SootClass soot_class = ref.declaringClass();
     final NumberedString subSignature = ref.getSubSignature();
     if (shouldMap(soot_class)) {
       SootClass new_class = getMapping(soot_class);
       if (new_class.declaresMethod(subSignature)) {
         SootMethod new_method = RootbeerScene.v().getMethod(new_class, subSignature.getString());
         addAddedMethod(new_method);
         fixArguments(new_method);
         RootbeerScene.v().getDfsInfo().addReachableMethodSig(new_method.getSignature());
         expr.setMethodRef(new_method.makeRef());
       }
     } else {
       if (soot_class.declaresMethod(ref.getSubSignature())) {
         SootMethod method = soot_class.getMethod(ref.getSubSignature());
         fixArguments(method);
       }
     }
     ref = remapRef(ref);
     try {
       if (shouldMap(soot_class)) {
         soot_class = getMapping(soot_class);
       }
       SootMethod method = soot_class.getMethod(ref.getSubSignature());
       RootbeerScene.v().getDfsInfo().addReachableMethodSig(method.getSignature());
       expr.setMethodRef(method.makeRef());
     } catch (Exception ex) {
       // ex.printStackTrace();
     }
     return value;
   } else if (value instanceof NewExpr) {
     NewExpr expr = (NewExpr) value;
     RefType base_type = expr.getBaseType();
     SootClass soot_class = base_type.getSootClass();
     if (shouldMap(soot_class)) {
       SootClass new_class = getMapping(soot_class);
       expr.setBaseType(new_class.getType());
     }
     return value;
   } else if (value instanceof NewArrayExpr) {
     NewArrayExpr expr = (NewArrayExpr) value;
     Type base_type = expr.getBaseType();
     base_type = fixType(base_type);
     expr.setBaseType(base_type);
     return value;
   } else if (value instanceof NewMultiArrayExpr) {
     NewMultiArrayExpr expr = (NewMultiArrayExpr) value;
     ArrayType array_type = expr.getBaseType();
     Type base_type = array_type.baseType;
     if (base_type instanceof RefType) {
       RefType ref_type = (RefType) base_type;
       SootClass soot_class = ref_type.getSootClass();
       if (shouldMap(soot_class)) {
         SootClass new_class = getMapping(soot_class);
         ArrayType new_type = ArrayType.v(new_class.getType(), array_type.numDimensions);
         expr.setBaseType(new_type);
       }
     }
     return value;
   } else if (value instanceof CastExpr) {
     CastExpr expr = (CastExpr) value;
     Type cast_type = expr.getCastType();
     cast_type = fixType(cast_type);
     expr.setCastType(cast_type);
     return value;
   } else if (value instanceof ParameterRef) {
     ParameterRef ref = (ParameterRef) value;
     Type new_type = fixType(ref.getType());
     return new ParameterRef(new_type, ref.getIndex());
   } else if (value instanceof ThisRef) {
     ThisRef ref = (ThisRef) value;
     Type new_type = fixType(ref.getType());
     return new ThisRef((RefType) new_type);
   } else if (value instanceof Local) {
     Local local = (Local) value;
     Type type = local.getType();
     local.setType(fixType(type));
     return value;
   } else {
     return value;
   }
 }