/** * Indicates whether this ThrowableSet includes some exception that might be caught by a handler * argument of the type <code>catcher</code>. * * @param catcher type of the handler parameter to be tested. * @return <code>true</code> if this set contains an exception type that might be caught by <code> * catcher</code>, false if it does not. */ public boolean catchableAs(RefType catcher) { if (INSTRUMENTING) { Manager.v().catchableAsQueries++; } FastHierarchy h = Scene.v().getOrMakeFastHierarchy(); if (exceptionsExcluded.size() > 0) { if (INSTRUMENTING) { Manager.v().catchableAsFromSearch++; } for (Iterator i = exceptionsExcluded.iterator(); i.hasNext(); ) { AnySubType exclusion = (AnySubType) i.next(); if (h.canStoreType(catcher, exclusion.getBase())) { return false; } } } if (exceptionsIncluded.contains(catcher)) { if (INSTRUMENTING) { if (exceptionsExcluded.size() == 0) { Manager.v().catchableAsFromMap++; } else { Manager.v().catchableAsFromSearch++; } } return true; } else { if (INSTRUMENTING) { if (exceptionsExcluded.size() == 0) { Manager.v().catchableAsFromSearch++; } } for (Iterator i = exceptionsIncluded.iterator(); i.hasNext(); ) { RefLikeType thrownType = (RefLikeType) i.next(); if (thrownType instanceof RefType) { if (thrownType == catcher) { // assertion failure. throw new IllegalStateException( "ThrowableSet.catchableAs(RefType): exceptions.contains() failed to match contained RefType " + catcher); } else if (h.canStoreType(thrownType, catcher)) { return true; } } else { RefType thrownBase = ((AnySubType) thrownType).getBase(); // At runtime, thrownType might be instantiated by any // of thrownBase's subtypes, so: if (h.canStoreType(thrownBase, catcher) || h.canStoreType(catcher, thrownBase)) { return true; } } } return false; } }
/** * Partitions the exceptions in this <code>ThrowableSet</code> into those which would be caught by * a handler with the passed <code>catch</code> parameter type and those which would not. * * @param catcher type of the handler parameter to be tested. * @return a pair of <code>ThrowableSet</code>s, one containing the types in this <code> * ThrowableSet</code> which would be be caught as <code>catcher</code> and the other * containing the types in this <code>ThrowableSet</code> which would not be caught as <code> * catcher</code>. */ public Pair whichCatchableAs(RefType catcher) { if (INSTRUMENTING) { Manager.v().removesOfAnySubType++; } FastHierarchy h = Scene.v().getOrMakeFastHierarchy(); Set caughtIncluded = null; Set caughtExcluded = null; Set uncaughtIncluded = null; Set uncaughtExcluded = null; if (INSTRUMENTING) { Manager.v().removesFromSearch++; } for (Iterator i = exceptionsExcluded.iterator(); i.hasNext(); ) { AnySubType exclusion = (AnySubType) i.next(); RefType exclusionBase = exclusion.getBase(); if (h.canStoreType(catcher, exclusionBase)) { // Because the add() operations ban additions to sets // with exclusions, we can be sure no types in this are // caught by catcher. return new Pair(ThrowableSet.Manager.v().EMPTY, this); } else if (h.canStoreType(exclusionBase, catcher)) { // exclusion wouldn't be in exceptionsExcluded if one // of its supertypes were not in exceptionsIncluded, // so we know the next loop will add either that supertype // or catcher to caughtIncluded. Thus: caughtExcluded = addExceptionToSet(exclusion, caughtExcluded); } else { uncaughtExcluded = addExceptionToSet(exclusion, uncaughtExcluded); } } for (Iterator i = exceptionsIncluded.iterator(); i.hasNext(); ) { RefLikeType inclusion = (RefLikeType) i.next(); if (inclusion instanceof RefType) { if (h.canStoreType(inclusion, catcher)) { caughtIncluded = addExceptionToSet(inclusion, caughtIncluded); } else { uncaughtIncluded = addExceptionToSet(inclusion, uncaughtIncluded); } } else { RefType base = ((AnySubType) inclusion).getBase(); if (h.canStoreType(base, catcher)) { // All subtypes of base will be caught. Any exclusions // will already have been copied to caughtExcluded by // the preceding loop. caughtIncluded = addExceptionToSet(inclusion, caughtIncluded); } else if (h.canStoreType(catcher, base)) { // Some subtypes of base will be caught, and // we know that not all of those catchable subtypes // are among exceptionsExcluded, since in that case we // would already have returned from within the // preceding loop. So, remove AnySubType(catcher) // from the uncaught types. uncaughtIncluded = addExceptionToSet(inclusion, uncaughtIncluded); uncaughtExcluded = addExceptionToSet(AnySubType.v(catcher), uncaughtExcluded); caughtIncluded = addExceptionToSet(AnySubType.v(catcher), caughtIncluded); // Any already excluded subtypes of inclusion // which are subtypes of catcher will have been // added to caughtExcluded by the previous loop. } else { uncaughtIncluded = addExceptionToSet(inclusion, uncaughtIncluded); } } } ThrowableSet caughtSet = Manager.v().registerSetIfNew(caughtIncluded, caughtExcluded); ThrowableSet uncaughtSet = Manager.v().registerSetIfNew(uncaughtIncluded, uncaughtExcluded); return new Pair(caughtSet, uncaughtSet); }
/** * 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; } else { FastHierarchy hierarchy = Scene.v().getOrMakeFastHierarchy(); RefType newBase = e.getBase(); if (INSTRUMENTING) { if (exceptionsExcluded.size() != 0) { Manager.v().addsExclusionWithSearch++; } else { Manager.v().addsExclusionWithoutSearch++; } } for (Iterator i = exceptionsExcluded.iterator(); i.hasNext(); ) { RefType exclusionBase = ((AnySubType) i.next()).getBase(); if (hierarchy.canStoreType(newBase, exclusionBase) || hierarchy.canStoreType(exclusionBase, newBase)) { 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; } else { if (INSTRUMENTING) { Manager.v().addsInclusionFromSearch++; } int changes = 0; boolean addNewException = true; Set resultSet = new HashSet(); for (Iterator i = this.exceptionsIncluded.iterator(); i.hasNext(); ) { RefLikeType incumbent = (RefLikeType) i.next(); 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(); // We have to use the base types in these hierarchy calls // because we want to know if _all_ possible // types represented by e can be represented by // 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; } memoizedAdds.put(e, result); return result; } } }
/** * Constructs a <code>ThrowableSet.Manager</code> for inclusion in Soot's global variable * manager, {@link G}. * * @param g guarantees that the constructor may only be called from {@link Singletons}. */ public Manager(Singletons.Global g) { // First ensure the Exception classes are represented in Soot. // Runtime errors: RUNTIME_EXCEPTION = Scene.v().getRefType("java.lang.RuntimeException"); ARITHMETIC_EXCEPTION = Scene.v().getRefType("java.lang.ArithmeticException"); ARRAY_STORE_EXCEPTION = Scene.v().getRefType("java.lang.ArrayStoreException"); CLASS_CAST_EXCEPTION = Scene.v().getRefType("java.lang.ClassCastException"); ILLEGAL_MONITOR_STATE_EXCEPTION = Scene.v().getRefType("java.lang.IllegalMonitorStateException"); INDEX_OUT_OF_BOUNDS_EXCEPTION = Scene.v().getRefType("java.lang.IndexOutOfBoundsException"); ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION = Scene.v().getRefType("java.lang.ArrayIndexOutOfBoundsException"); NEGATIVE_ARRAY_SIZE_EXCEPTION = Scene.v().getRefType("java.lang.NegativeArraySizeException"); NULL_POINTER_EXCEPTION = Scene.v().getRefType("java.lang.NullPointerException"); INSTANTIATION_ERROR = Scene.v().getRefType("java.lang.InstantiationError"); EMPTY = registerSetIfNew(null, null); Set allThrowablesSet = new HashSet(); allThrowablesSet.add(AnySubType.v(Scene.v().getRefType("java.lang.Throwable"))); ALL_THROWABLES = registerSetIfNew(allThrowablesSet, null); Set vmErrorSet = new HashSet(); vmErrorSet.add(Scene.v().getRefType("java.lang.InternalError")); vmErrorSet.add(Scene.v().getRefType("java.lang.OutOfMemoryError")); vmErrorSet.add(Scene.v().getRefType("java.lang.StackOverflowError")); vmErrorSet.add(Scene.v().getRefType("java.lang.UnknownError")); // The Java library's deprecated Thread.stop(Throwable) method // would actually allow _any_ Throwable to be delivered // asynchronously, not just java.lang.ThreadDeath. vmErrorSet.add(Scene.v().getRefType("java.lang.ThreadDeath")); VM_ERRORS = registerSetIfNew(vmErrorSet, null); Set resolveClassErrorSet = new HashSet(); resolveClassErrorSet.add(Scene.v().getRefType("java.lang.ClassCircularityError")); // We add AnySubType(ClassFormatError) so that we can // avoid adding its subclass, // UnsupportedClassVersionError, explicitly. This is a // hack to allow Soot to analyze older class libraries // (UnsupportedClassVersionError was added in JDK 1.2). // TODO: The class "ClassFormatError" does not exist in TakaTuka so our best guess is that // commenting the following line out won't affect the analysis in any way. But this // is a "fault point" to look at if any problems arise in the future. // resolveClassErrorSet.add(AnySubType.v(Scene.v().getRefType("java.lang.ClassFormatError"))); resolveClassErrorSet.add(Scene.v().getRefType("java.lang.IllegalAccessError")); resolveClassErrorSet.add(Scene.v().getRefType("java.lang.IncompatibleClassChangeError")); resolveClassErrorSet.add(Scene.v().getRefType("java.lang.LinkageError")); resolveClassErrorSet.add(Scene.v().getRefType("java.lang.NoClassDefFoundError")); resolveClassErrorSet.add(Scene.v().getRefType("java.lang.VerifyError")); RESOLVE_CLASS_ERRORS = registerSetIfNew(resolveClassErrorSet, null); Set resolveFieldErrorSet = new HashSet(resolveClassErrorSet); resolveFieldErrorSet.add(Scene.v().getRefType("java.lang.NoSuchFieldError")); RESOLVE_FIELD_ERRORS = registerSetIfNew(resolveFieldErrorSet, null); Set resolveMethodErrorSet = new HashSet(resolveClassErrorSet); resolveMethodErrorSet.add(Scene.v().getRefType("java.lang.AbstractMethodError")); resolveMethodErrorSet.add(Scene.v().getRefType("java.lang.NoSuchMethodError")); resolveMethodErrorSet.add(Scene.v().getRefType("java.lang.UnsatisfiedLinkError")); RESOLVE_METHOD_ERRORS = registerSetIfNew(resolveMethodErrorSet, null); // The static initializers of a newly loaded class might // throw any Error (if they threw an Exception---even a // RuntimeException---it would be replaced by an // ExceptionInInitializerError): // Set initializationErrorSet = new HashSet(); initializationErrorSet.add(AnySubType.v(Scene.v().getRefType("java.lang.Error"))); INITIALIZATION_ERRORS = registerSetIfNew(initializationErrorSet, null); }