Search in sources :

Example 1 with FastHierarchy

use of soot.FastHierarchy in project soot by Sable.

the class MergeChecker method checkNode.

protected void checkNode(Node container, Node n, Node upstream) {
    if (container.getReplacement() != container)
        throw new RuntimeException("container " + container + " is illegal");
    if (upstream.getReplacement() != upstream)
        throw new RuntimeException("upstream " + upstream + " is illegal");
    PointsToSetInternal p2set = container.getP2Set();
    FastHierarchy fh = pag.getTypeManager().getFastHierarchy();
    if (!p2set.contains(n) && (fh == null || container.getType() == null || fh.canStoreType(n.getType(), container.getType()))) {
        logger.debug("Check failure: " + container + " does not have " + n + "; upstream is " + upstream);
    }
}
Also used : FastHierarchy(soot.FastHierarchy) PointsToSetInternal(soot.jimple.spark.sets.PointsToSetInternal)

Example 2 with FastHierarchy

use of soot.FastHierarchy in project soot by Sable.

the class ThrowableSet method add.

/**
 * Returns a <code>ThrowableSet</code> which contains <code>e</code> and all
 * of its subclasses as well as the exceptions in this set.
 *
 * <p>
 * <code>e</code> should be an instance of {@link AnySubType} if you know
 * that the compile-time type of the exception you are representing is
 * <code>e</code>, but the exception may be instantiated at run-time by a
 * subclass of <code>e</code>.
 *
 * <p>
 * For example, if you were recording the type of the exception thrown by
 *
 * <pre>
 * catch (IOException e) {
 *    throw e;
 * }
 * </pre>
 *
 * you would call
 *
 * <pre>
 * <code>add(AnySubtype.v(Scene.v().getRefType("java.lang.Exception.IOException")))</code>
 * </pre>
 *
 * since the handler might rethrow any subclass of <code>IOException</code>.
 *
 * @param e
 *            represents a subtree of the exception class hierarchy to add
 *            to this set.
 *
 * @return a set containing <code>e</code> and all its subclasses, as well
 *         as the exceptions represented by this set.
 *
 * @throws ThrowableSet.AlreadyHasExclusionsException
 *             if this <code>ThrowableSet</code> is the result of a
 *             {@link #whichCatchableAs(RefType)} operation and, thus,
 *             unable to represent the addition of <code>e</code>.
 */
public ThrowableSet add(AnySubType e) throws ThrowableSet.AlreadyHasExclusionsException {
    if (INSTRUMENTING) {
        Manager.v().addsOfAnySubType++;
    }
    ThrowableSet result = getMemoizedAdds(e);
    if (result != null) {
        if (INSTRUMENTING) {
            Manager.v().addsInclusionFromMemo++;
            Manager.v().addsExclusionWithoutSearch++;
        }
        return result;
    }
    FastHierarchy hierarchy = Scene.v().getOrMakeFastHierarchy();
    RefType newBase = e.getBase();
    if (INSTRUMENTING) {
        if (exceptionsExcluded.isEmpty()) {
            Manager.v().addsExclusionWithoutSearch++;
        } else {
            Manager.v().addsExclusionWithSearch++;
        }
    }
    for (AnySubType excludedType : exceptionsExcluded) {
        RefType exclusionBase = excludedType.getBase();
        boolean isExcluded = exclusionBase.getSootClass().isPhantom() && exclusionBase.equals(newBase);
        isExcluded |= !exclusionBase.getSootClass().isPhantom() && (hierarchy.canStoreType(newBase, exclusionBase) || hierarchy.canStoreType(exclusionBase, newBase));
        if (isExcluded) {
            if (INSTRUMENTING) {
                // To ensure that the subcategories total properly:
                Manager.v().addsInclusionInterrupted++;
            }
            throw new AlreadyHasExclusionsException("ThrowableSet.add(" + e.toString() + ") to the set [ " + this.toString() + "] where " + exclusionBase.toString() + " is excluded.");
        }
    }
    if (this.exceptionsIncluded.contains(e)) {
        if (INSTRUMENTING) {
            Manager.v().addsInclusionFromMap++;
        }
        return this;
    }
    if (INSTRUMENTING) {
        Manager.v().addsInclusionFromSearch++;
    }
    int changes = 0;
    boolean addNewException = true;
    Set<RefLikeType> resultSet = new HashSet<RefLikeType>();
    for (RefLikeType incumbent : this.exceptionsIncluded) {
        if (incumbent instanceof RefType) {
            if (hierarchy.canStoreType(incumbent, newBase)) {
                // Omit incumbent from result.
                changes++;
            } else {
                resultSet.add(incumbent);
            }
        } else if (incumbent instanceof AnySubType) {
            RefType incumbentBase = ((AnySubType) incumbent).getBase();
            if (newBase.getSootClass().isPhantom()) {
                if (!incumbentBase.equals(newBase))
                    resultSet.add(incumbent);
            } else // the incumbent, or vice versa.
            if (hierarchy.canStoreType(newBase, incumbentBase)) {
                addNewException = false;
                resultSet.add(incumbent);
            } else if (hierarchy.canStoreType(incumbentBase, newBase)) {
                // Omit incumbent from result;
                changes++;
            } else {
                resultSet.add(incumbent);
            }
        } else {
            // assertion failure.
            throw new IllegalStateException("ThrowableSet.add(AnySubType): Set element " + incumbent.toString() + " is neither a RefType nor an AnySubType.");
        }
    }
    if (addNewException) {
        resultSet.add(e);
        changes++;
    }
    if (changes > 0) {
        result = Manager.v().registerSetIfNew(resultSet, this.exceptionsExcluded);
    } else {
        result = this;
    }
    addToMemoizedAdds(e, result);
    return result;
}
Also used : RefType(soot.RefType) RefLikeType(soot.RefLikeType) FastHierarchy(soot.FastHierarchy) AnySubType(soot.AnySubType) HashSet(java.util.HashSet)

Example 3 with FastHierarchy

use of soot.FastHierarchy in project soot by Sable.

the class ThrowableSet method add.

/**
 * Returns a <code>ThrowableSet</code> which contains all the exceptions in
 * <code>addedExceptions</code> in addition to those in this
 * <code>ThrowableSet</code>.
 *
 * @param addedExceptions
 *            a set of {@link RefLikeType} and {@link AnySubType} objects to
 *            be added to the types included in this
 *            <code>ThrowableSet</code>.
 *
 * @return a set containing all the <code>addedExceptions</code> as well as
 *         the exceptions in this set.
 */
private ThrowableSet add(Set<RefLikeType> addedExceptions) {
    Set<RefLikeType> resultSet = new HashSet<RefLikeType>(this.exceptionsIncluded);
    int changes = 0;
    FastHierarchy hierarchy = Scene.v().getOrMakeFastHierarchy();
    for (RefLikeType newType : addedExceptions) {
        if (!resultSet.contains(newType)) {
            boolean addNewType = true;
            if (newType instanceof RefType) {
                for (RefLikeType incumbentType : resultSet) {
                    if (incumbentType instanceof RefType) {
                        if (newType == incumbentType) {
                            // assertion failure.
                            throw new IllegalStateException("ThrowableSet.add(Set): resultSet.contains() failed to screen duplicate RefType " + newType);
                        }
                    } else if (incumbentType instanceof AnySubType) {
                        RefType incumbentBase = ((AnySubType) incumbentType).getBase();
                        if (hierarchy.canStoreType(newType, incumbentBase)) {
                            // No need to add this class.
                            addNewType = false;
                        }
                    } else {
                        // assertion failure.
                        throw new IllegalStateException("ThrowableSet.add(Set): incumbent Set element " + incumbentType + " is neither a RefType nor an AnySubType.");
                    }
                }
            } else if (newType instanceof AnySubType) {
                RefType newBase = ((AnySubType) newType).getBase();
                for (Iterator<RefLikeType> j = resultSet.iterator(); j.hasNext(); ) {
                    RefLikeType incumbentType = j.next();
                    if (incumbentType instanceof RefType) {
                        RefType incumbentBase = (RefType) incumbentType;
                        if (hierarchy.canStoreType(incumbentBase, newBase)) {
                            j.remove();
                            changes++;
                        }
                    } else if (incumbentType instanceof AnySubType) {
                        RefType incumbentBase = ((AnySubType) incumbentType).getBase();
                        if (newBase == incumbentBase) {
                            // assertion failure.
                            throw new IllegalStateException("ThrowableSet.add(Set): resultSet.contains() failed to screen duplicate AnySubType " + newBase);
                        } else if (hierarchy.canStoreType(incumbentBase, newBase)) {
                            j.remove();
                            changes++;
                        } else if (hierarchy.canStoreType(newBase, incumbentBase)) {
                            // No need to add this class.
                            addNewType = false;
                        }
                    } else {
                        // assertion failure.
                        throw new IllegalStateException("ThrowableSet.add(Set): old Set element " + incumbentType + " is neither a RefType nor an AnySubType.");
                    }
                }
            } else {
                // assertion failure.
                throw new IllegalArgumentException("ThrowableSet.add(Set): new Set element " + newType + " is neither a RefType nor an AnySubType.");
            }
            if (addNewType) {
                changes++;
                resultSet.add(newType);
            }
        }
    }
    ThrowableSet result = null;
    if (changes > 0) {
        result = Manager.v().registerSetIfNew(resultSet, this.exceptionsExcluded);
    } else {
        result = this;
    }
    return result;
}
Also used : RefLikeType(soot.RefLikeType) RefType(soot.RefType) FastHierarchy(soot.FastHierarchy) Iterator(java.util.Iterator) AnySubType(soot.AnySubType) HashSet(java.util.HashSet)

Example 4 with FastHierarchy

use of soot.FastHierarchy in project soot by Sable.

the class ThrowableSet method add.

/**
 * Returns a <code>ThrowableSet</code> which contains <code>e</code> in
 * addition to the exceptions in this <code>ThrowableSet</code>.
 *
 * <p>
 * Add <code>e</code> as a {@link RefType} when you know that the run-time
 * class of the exception you are representing is necessarily <code>e</code>
 * and cannot be a subclass of <code>e</code>.
 *
 * <p>
 * For example, if you were recording the type of the exception thrown by
 *
 * <pre>
 * throw new IOException(&quot;Permission denied&quot;);
 * </pre>
 *
 * you would call
 *
 * <pre>
 * <code>add(Scene.v().getRefType("java.lang.Exception.IOException"))</code>
 * </pre>
 *
 * since the class of the exception is necessarily <code>IOException</code>.
 *
 * @param e
 *            the exception class
 *
 * @return a set containing <code>e</code> as well as the exceptions in this
 *         set.
 *
 * @throws {@link ThrowableSet.IllegalStateException} if this
 *         <code>ThrowableSet</code> is the result of a
 *         {@link #whichCatchableAs(RefType)} operation and, thus, unable to
 *         represent the addition of <code>e</code>.
 */
public ThrowableSet add(RefType e) throws ThrowableSet.AlreadyHasExclusionsException {
    if (INSTRUMENTING) {
        Manager.v().addsOfRefType++;
    }
    if (this.exceptionsIncluded.contains(e)) {
        if (INSTRUMENTING) {
            Manager.v().addsInclusionFromMap++;
            Manager.v().addsExclusionWithoutSearch++;
        }
        return this;
    }
    ThrowableSet result = getMemoizedAdds(e);
    if (result != null) {
        if (INSTRUMENTING) {
            Manager.v().addsInclusionFromMemo++;
            Manager.v().addsExclusionWithoutSearch++;
        }
        return result;
    }
    if (INSTRUMENTING) {
        Manager.v().addsInclusionFromSearch++;
        if (exceptionsExcluded.isEmpty()) {
            Manager.v().addsExclusionWithoutSearch++;
        } else {
            Manager.v().addsExclusionWithSearch++;
        }
    }
    FastHierarchy hierarchy = Scene.v().getOrMakeFastHierarchy();
    for (AnySubType excludedType : exceptionsExcluded) {
        RefType exclusionBase = excludedType.getBase();
        if ((e.getSootClass().isPhantom() && exclusionBase.equals(e)) || (!e.getSootClass().isPhantom() && hierarchy.canStoreType(e, exclusionBase))) {
            throw new AlreadyHasExclusionsException("ThrowableSet.add(RefType): adding" + e.toString() + " to the set [ " + this.toString() + "] where " + exclusionBase.toString() + " is excluded.");
        }
    }
    // in the list through subtyping.
    if (!e.getSootClass().isPhantom())
        for (RefLikeType incumbent : exceptionsIncluded) {
            if (incumbent instanceof AnySubType) {
                // Need to use incumbent.getBase() because
                // hierarchy.canStoreType() assumes that parent
                // is not an AnySubType.
                RefType incumbentBase = ((AnySubType) incumbent).getBase();
                if (hierarchy.canStoreType(e, incumbentBase)) {
                    addToMemoizedAdds(e, this);
                    return this;
                }
            } else if (!(incumbent instanceof RefType)) {
                // assertion failure.
                throw new IllegalStateException("ThrowableSet.add(RefType): Set element " + incumbent.toString() + " is neither a RefType nor an AnySubType.");
            }
        }
    Set<RefLikeType> resultSet = new HashSet<RefLikeType>(this.exceptionsIncluded);
    resultSet.add(e);
    result = Manager.v().registerSetIfNew(resultSet, this.exceptionsExcluded);
    addToMemoizedAdds(e, result);
    return result;
}
Also used : RefType(soot.RefType) RefLikeType(soot.RefLikeType) FastHierarchy(soot.FastHierarchy) AnySubType(soot.AnySubType) HashSet(java.util.HashSet)

Example 5 with FastHierarchy

use of soot.FastHierarchy in project soot by Sable.

the class ClassRenamer method internalTransform.

@Override
protected void internalTransform(String phaseName, Map<String, String> options) {
    if (isVerbose()) {
        logger.debug("Transforming Class Names...");
    }
    BodyBuilder.retrieveAllBodies();
    BodyBuilder.retrieveAllNames();
    final SootClass mainClass = getMainClassSafely();
    // iterate through application classes, rename classes with junk
    for (SootClass sootClass : Scene.v().getApplicationClasses()) {
        final String className = sootClass.getName();
        if (sootClass.equals(mainClass) || oldToNewClassNames.containsValue(className) || soot.jbco.Main.getWeight(phaseName, className) == 0) {
            continue;
        }
        String newClassName = oldToNewClassNames.get(className);
        if (newClassName == null) {
            newClassName = getNewName(getPackageName(className), className);
        }
        sootClass.setName(newClassName);
        RefType crt = RefType.v(newClassName);
        crt.setSootClass(sootClass);
        sootClass.setRefType(crt);
        sootClass.setResolvingLevel(SootClass.BODIES);
        // will this fix dangling classes?
        // scene.addRefType(sootClass.getType());
        newNameToClass.put(newClassName, sootClass);
        if (isVerbose()) {
            logger.info("\tRenaming " + className + " to " + newClassName);
        }
    }
    Scene.v().releaseActiveHierarchy();
    Scene.v().setFastHierarchy(new FastHierarchy());
    if (isVerbose()) {
        logger.info("\r\tUpdating bytecode class references");
    }
    for (SootClass sootClass : Scene.v().getApplicationClasses()) {
        for (SootMethod sootMethod : sootClass.getMethods()) {
            if (!sootMethod.isConcrete()) {
                continue;
            }
            if (isVerbose()) {
                logger.info("\t\t" + sootMethod.getSignature());
            }
            Body aBody;
            try {
                aBody = sootMethod.getActiveBody();
            } catch (Exception e) {
                continue;
            }
            for (Unit u : aBody.getUnits()) {
                for (ValueBox vb : u.getUseAndDefBoxes()) {
                    Value v = vb.getValue();
                    if (v instanceof ClassConstant) {
                        ClassConstant constant = (ClassConstant) v;
                        RefType type = (RefType) constant.toSootType();
                        RefType updatedType = type.getSootClass().getType();
                        vb.setValue(ClassConstant.fromType(updatedType));
                    } else if (v instanceof Expr) {
                        if (v instanceof CastExpr) {
                            CastExpr castExpr = (CastExpr) v;
                            updateType(castExpr.getCastType());
                        } else if (v instanceof InstanceOfExpr) {
                            InstanceOfExpr instanceOfExpr = (InstanceOfExpr) v;
                            updateType(instanceOfExpr.getCheckType());
                        }
                    } else if (v instanceof Ref) {
                        updateType(v.getType());
                    }
                }
            }
        }
    }
    Scene.v().releaseActiveHierarchy();
    Scene.v().setFastHierarchy(new FastHierarchy());
}
Also used : SootClass(soot.SootClass) Unit(soot.Unit) InstanceOfExpr(soot.jimple.InstanceOfExpr) RefType(soot.RefType) FastHierarchy(soot.FastHierarchy) Ref(soot.jimple.Ref) Expr(soot.jimple.Expr) InstanceOfExpr(soot.jimple.InstanceOfExpr) CastExpr(soot.jimple.CastExpr) ValueBox(soot.ValueBox) Value(soot.Value) CastExpr(soot.jimple.CastExpr) SootMethod(soot.SootMethod) Body(soot.Body) ClassConstant(soot.jimple.ClassConstant)

Aggregations

FastHierarchy (soot.FastHierarchy)14 RefType (soot.RefType)8 AnySubType (soot.AnySubType)7 SootMethod (soot.SootMethod)7 HashSet (java.util.HashSet)6 SootClass (soot.SootClass)6 RefLikeType (soot.RefLikeType)5 Type (soot.Type)5 ArrayList (java.util.ArrayList)4 Body (soot.Body)4 Unit (soot.Unit)4 Value (soot.Value)4 ValueBox (soot.ValueBox)4 ArrayType (soot.ArrayType)3 SootMethodRef (soot.SootMethodRef)3 SpecialInvokeExpr (soot.jimple.SpecialInvokeExpr)3 Iterator (java.util.Iterator)2 Set (java.util.Set)2 Local (soot.Local)2 NullType (soot.NullType)2