soot.jimple.ThrowStmt Java Examples

The following examples show how to use soot.jimple.ThrowStmt. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DexNullThrowTransformer.java    From JAADAS with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void internalTransform(Body b, String phaseName, Map<String, String> options) {
	LocalCreation lc = new LocalCreation(b.getLocals(), "ex");
	
	for (Iterator<Unit> unitIt = b.getUnits().snapshotIterator(); unitIt.hasNext(); ) {
		Unit u = unitIt.next();
		
		// Check for a null exception
		if (u instanceof ThrowStmt) {
			ThrowStmt throwStmt = (ThrowStmt) u;
			if (throwStmt.getOp() == NullConstant.v()
					|| throwStmt.getOp().equals(IntConstant.v(0))
					|| throwStmt.getOp().equals(LongConstant.v(0))) {
				createThrowStmt(b, throwStmt, lc);
			}
		}
	}
}
 
Example #2
Source File: InterproceduralConstantValuePropagator.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Checks whether the given method is a library stub method
 * @param method The method to check
 * @return True if the given method is an Android library stub, false
 * otherwise
 */
private boolean methodIsAndroidStub(SootMethod method) {		
	if (!(Options.v().src_prec() == Options.src_prec_apk
			&& method.getDeclaringClass().isLibraryClass()
			&& SystemClassHandler.isClassInSystemPackage(
					method.getDeclaringClass().getName())))
		return false;
	
	// Check whether there is only a single throw statement
	for (Unit u : method.getActiveBody().getUnits()) {
		if (u instanceof DefinitionStmt) {
			DefinitionStmt defStmt = (DefinitionStmt) u;
			if (!(defStmt.getRightOp() instanceof ThisRef)
					&& !(defStmt.getRightOp() instanceof ParameterRef)
					&& !(defStmt.getRightOp() instanceof NewExpr))
				return false;
		}
		else if (u instanceof InvokeStmt) {
			InvokeStmt stmt = (InvokeStmt) u;
			
			// Check for exception constructor invocations
			SootMethod callee = stmt.getInvokeExpr().getMethod();
			if (!callee.getSubSignature().equals("void <init>(java.lang.String)"))
				// Check for super class constructor invocation
				if (!(method.getDeclaringClass().hasSuperclass()
						&& callee.getDeclaringClass() == method.getDeclaringClass().getSuperclass()
						&& callee.getName().equals("<init>")))
					return false;
		}
		else if (!(u instanceof ThrowStmt))
			return false;
	}
	return true;
}
 
Example #3
Source File: UnitThrowAnalysisTest.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testJThrowStmt() {

    // First test with an argument that is included in
    // PERENNIAL_THROW_EXCEPTIONS.
    ThrowStmt s = Jimple.v().newThrowStmt(Jimple.v().newLocal("local0", 
                RefType.v("java.lang.NullPointerException")));
    Set expectedRep = new ExceptionHashSet(utility.PERENNIAL_THROW_EXCEPTIONS);
    expectedRep.remove(utility.NULL_POINTER_EXCEPTION);
    expectedRep.add(AnySubType.v(utility.NULL_POINTER_EXCEPTION));
    assertTrue(ExceptionTestUtility.sameMembers(expectedRep, Collections.EMPTY_SET,
                unitAnalysis.mightThrow(s)));
    assertEquals(utility.PERENNIAL_THROW_EXCEPTIONS_PLUS_SUPERTYPES,
            utility.catchableSubset(unitAnalysis.mightThrow(s)));

    // Throw a local of type IncompatibleClassChangeError.
    Local local = Jimple.v().newLocal("local1", 
            utility.INCOMPATIBLE_CLASS_CHANGE_ERROR);
    s.setOp(local);
    expectedRep = new ExceptionHashSet(utility.THROW_PLUS_INCOMPATIBLE_CLASS_CHANGE);
    expectedRep.remove(utility.INCOMPATIBLE_CLASS_CHANGE_ERROR);
    expectedRep.add(AnySubType.v(utility.INCOMPATIBLE_CLASS_CHANGE_ERROR));
    assertTrue(ExceptionTestUtility.sameMembers(expectedRep, Collections.EMPTY_SET,
                unitAnalysis.mightThrow(s)));
    assertEquals(utility.THROW_PLUS_INCOMPATIBLE_CLASS_CHANGE_PLUS_SUBTYPES_PLUS_SUPERTYPES, 
            utility.catchableSubset(unitAnalysis.mightThrow(s)));

    // Throw a local of unknown type.
    local = Jimple.v().newLocal("local1", soot.UnknownType.v());
    s.setOp(local);
    assertTrue(ExceptionTestUtility.sameMembers(utility.ALL_THROWABLES_REP, 
                Collections.EMPTY_SET,
                unitAnalysis.mightThrow(s)));
    assertEquals(utility.ALL_TEST_THROWABLES, 
            utility.catchableSubset(unitAnalysis.mightThrow(s)));
}
 
Example #4
Source File: ExceptionalUnitGraph.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Utility method, to be called only after the unitToPreds and unitToSuccs
 * maps have been built. It defines the graph's set of heads to include the
 * first {@link Unit} in the graph's body, together with all the
 * <code>Unit</code>s in <code>additionalHeads</code>. It defines the
 * graph's set of tails to include all <code>Unit</code>s which represent
 * some sort of return bytecode or an <code>athrow</code> bytecode which may
 * escape the method.
 */
private void buildHeadsAndTails(Set<Unit> additionalHeads) {
	List<Unit> headList = new ArrayList<Unit>(additionalHeads.size() + 1);
	headList.addAll(additionalHeads);

	if (unitChain.isEmpty())
		throw new IllegalStateException("No body for method "
				+ body.getMethod().getSignature());

	Unit entryPoint = unitChain.getFirst();
	if (!headList.contains(entryPoint)) {
		headList.add(entryPoint);
	}

	List<Unit> tailList = new ArrayList<Unit>();
	for (Unit u : unitChain) {
		if (u instanceof soot.jimple.ReturnStmt
				|| u instanceof soot.jimple.ReturnVoidStmt
				|| u instanceof soot.baf.ReturnInst
				|| u instanceof soot.baf.ReturnVoidInst) {
			tailList.add(u);
		} else if (u instanceof soot.jimple.ThrowStmt
				|| u instanceof soot.baf.ThrowInst) {
			Collection<ExceptionDest> dests = getExceptionDests(u);
			int escapeMethodCount = 0;
			for (ExceptionDest dest : dests) {
				if (dest.getTrap() == null) {
					escapeMethodCount++;
				}
			}
			if (escapeMethodCount > 0) {
				tailList.add(u);
			}
		}
	}
	tails = Collections.unmodifiableList(tailList);
	heads = Collections.unmodifiableList(headList);
}
 
Example #5
Source File: ConstraintCollector.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
public void caseThrowStmt(ThrowStmt stmt) {
	if (uses) {
		if (stmt.getOp() instanceof Local) {
			TypeVariable op = resolver.typeVariable((Local) stmt.getOp());

			op.addParent(resolver.typeVariable(RefType.v("java.lang.Throwable")));
		}
	}
}
 
Example #6
Source File: StmtVisitor.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void caseThrowStmt(ThrowStmt stmt) {
	Value exception = stmt.getOp();
       constantV.setOrigStmt(stmt);
	Register exceptionReg = regAlloc.asImmediate(exception, constantV);
       addInsn(new Insn11x(Opcode.THROW, exceptionReg), stmt);
}
 
Example #7
Source File: UnitThrowAnalysis.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
public ThrowableSet mightThrowImplicitly(ThrowStmt t) {
return implicitThrowExceptions;
   }
 
Example #8
Source File: BackwardBoomerangSolver.java    From SPDS with Eclipse Public License 2.0 4 votes vote down vote up
protected Collection<? extends State> computeCallFlow(SootMethod caller, Statement returnSite, Statement callSite,
        InvokeExpr invokeExpr, Val fact, SootMethod callee, Stmt calleeSp) {
    if (!callee.hasActiveBody())
        return Collections.emptySet();
    if (calleeSp instanceof ThrowStmt) {
        return Collections.emptySet();
    }
    Body calleeBody = callee.getActiveBody();
    Set<State> out = Sets.newHashSet();
    if (invokeExpr instanceof InstanceInvokeExpr) {
        InstanceInvokeExpr iie = (InstanceInvokeExpr) invokeExpr;
        if (iie.getBase().equals(fact.value()) && !callee.isStatic()) {
            out.add(new PushNode<Statement, Val, Statement>(new Statement(calleeSp, callee),
                    new Val(calleeBody.getThisLocal(), callee), returnSite, PDSSystem.CALLS));
        }
    }
    List<Local> parameterLocals = calleeBody.getParameterLocals();
    int i = 0;
    for (Value arg : invokeExpr.getArgs()) {
        if (arg.equals(fact.value()) && parameterLocals.size() > i) {
            Local param = parameterLocals.get(i);
            out.add(new PushNode<Statement, Val, Statement>(new Statement(calleeSp, callee), new Val(param, callee),
                    returnSite, PDSSystem.CALLS));
        }
        i++;
    }

    if (callSite.getUnit().get() instanceof AssignStmt && calleeSp instanceof ReturnStmt) {
        AssignStmt as = (AssignStmt) callSite.getUnit().get();
        ReturnStmt retStmt = (ReturnStmt) calleeSp;
        if (as.getLeftOp().equals(fact.value())) {
            out.add(new PushNode<Statement, Val, Statement>(new Statement(calleeSp, callee),
                    new Val(retStmt.getOp(), callee), returnSite, PDSSystem.CALLS));
        }
    }
    if (fact.isStatic()) {
        out.add(new PushNode<Statement, Val, Statement>(new Statement(calleeSp, callee),
                new StaticFieldVal(fact.value(), ((StaticFieldVal) fact).field(), callee), returnSite,
                PDSSystem.CALLS));
    }
    return out;
}
 
Example #9
Source File: UnitThrowAnalysisTest.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
@Test
public void testGThrowStmt() {
    ThrowStmt s = Grimp.v().newThrowStmt(Grimp.v().newLocal("local0", 
                RefType.v("java.util.zip.ZipException")));

    Set expectedRep = new ExceptionHashSet(utility.PERENNIAL_THROW_EXCEPTIONS);
    expectedRep.add(AnySubType.v(Scene.v().getRefType("java.util.zip.ZipException")));
    assertTrue(ExceptionTestUtility.sameMembers(expectedRep, Collections.EMPTY_SET,
                unitAnalysis.mightThrow(s)));

    Set expectedCatch = new ExceptionHashSet(utility.PERENNIAL_THROW_EXCEPTIONS_PLUS_SUPERTYPES);
    // We don't need to add java.util.zip.ZipException, since it is not
    // in the universe of test Throwables.
    assertEquals(expectedCatch, 
            utility.catchableSubset(unitAnalysis.mightThrow(s)));

    // Now throw a new IncompatibleClassChangeError.
    s = Grimp.v().newThrowStmt(
            Grimp.v().newNewInvokeExpr(
                utility.INCOMPATIBLE_CLASS_CHANGE_ERROR,
                Scene.v().makeMethodRef(utility.INCOMPATIBLE_CLASS_CHANGE_ERROR.getSootClass(),
                    "void <init>", Collections.EMPTY_LIST, 
                    VoidType.v(), false),
                new ArrayList()
                )
            );
    assertTrue(ExceptionTestUtility.sameMembers(utility.THROW_PLUS_INCOMPATIBLE_CLASS_CHANGE, 
                Collections.EMPTY_SET,
                unitAnalysis.mightThrow(s)));
    assertEquals(utility.THROW_PLUS_INCOMPATIBLE_CLASS_CHANGE_PLUS_SUPERTYPES, 
            utility.catchableSubset(unitAnalysis.mightThrow(s)));

    // Throw a local of type IncompatibleClassChangeError.
    Local local = Grimp.v().newLocal("local1", 
            utility.INCOMPATIBLE_CLASS_CHANGE_ERROR);
    s.setOp(local);
    expectedRep = new ExceptionHashSet(utility.PERENNIAL_THROW_EXCEPTIONS);
    expectedRep.remove(utility.INCOMPATIBLE_CLASS_CHANGE_ERROR);
    expectedRep.add(AnySubType.v(utility.INCOMPATIBLE_CLASS_CHANGE_ERROR));
    assertTrue(ExceptionTestUtility.sameMembers(expectedRep, Collections.EMPTY_SET,
                unitAnalysis.mightThrow(s)));
    assertEquals(utility.THROW_PLUS_INCOMPATIBLE_CLASS_CHANGE_PLUS_SUBTYPES_PLUS_SUPERTYPES, 
            utility.catchableSubset(unitAnalysis.mightThrow(s)));

    // Throw a local of unknown type.
    local = Jimple.v().newLocal("local1", soot.UnknownType.v());
    s.setOp(local);
    assertTrue(ExceptionTestUtility.sameMembers(utility.ALL_THROWABLES_REP, 
                Collections.EMPTY_SET,
                unitAnalysis.mightThrow(s)));
    assertEquals(utility.ALL_TEST_THROWABLES, 
            utility.catchableSubset(unitAnalysis.mightThrow(s)));
}
 
Example #10
Source File: UnitThrowAnalysis.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void caseThrowStmt(ThrowStmt s) {
    result = mightThrowImplicitly(s);
    result = result.add(mightThrowExplicitly(s));
}
 
Example #11
Source File: JimpleStmtVisitorImpl.java    From FuzzDroid with Apache License 2.0 4 votes vote down vote up
@Override
public void caseThrowStmt(ThrowStmt stmt) {
	throw new RuntimeException("todo");
	
}
 
Example #12
Source File: ConstraintCollector.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
public void caseThrowStmt(ThrowStmt stmt) {
}
 
Example #13
Source File: ConstraintChecker.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
public void caseThrowStmt(ThrowStmt stmt) {
}
 
Example #14
Source File: UseChecker.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
public void caseThrowStmt(ThrowStmt stmt)
{
	stmt.setOp(this.uv.visit(
		stmt.getOp(), RefType.v("java.lang.Throwable"), stmt));
}
 
Example #15
Source File: StmtTemplatePrinter.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
public void caseThrowStmt(ThrowStmt stmt) {
	String varName = printValueAssignment(stmt.getOp(),"op");
	printStmt(stmt, varName);
}
 
Example #16
Source File: InterproceduralConstantValuePropagator.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
private void fixExceptions(SootMethod caller, Unit callSite, Set<SootClass> doneSet) {
	ThrowAnalysis ta = Options.v().src_prec() == Options.src_prec_apk
			? DalvikThrowAnalysis.v() : UnitThrowAnalysis.v();
	ThrowableSet throwSet = ta.mightThrow(callSite);
	
	for (final Trap t : caller.getActiveBody().getTraps())
		if (doneSet.add(t.getException())
				&& throwSet.catchableAs(t.getException().getType())) {
			SootMethod thrower = exceptionThrowers.get(t.getException());
			if (thrower == null) {
				if (exceptionClass == null) {
					exceptionClass = new SootClass("FLOWDROID_EXCEPTIONS", Modifier.PUBLIC);
					Scene.v().addClass(exceptionClass);
				}
				
				// Create the new method
				thrower = new SootMethod("throw" + exceptionThrowers.size(),
						Collections.<Type>emptyList(), VoidType.v());
				thrower.setModifiers(Modifier.PUBLIC | Modifier.STATIC);
				
				final Body body = Jimple.v().newBody(thrower);
				thrower.setActiveBody(body);
				final SootMethod meth = thrower;
				
				IEntryPointCreator epc = new BaseEntryPointCreator() {
	
					@Override
					public Collection<String> getRequiredClasses() {
						return Collections.emptySet();
					}
	
					@Override
					protected SootMethod createDummyMainInternal(SootMethod emptySootMethod) {
				 		LocalGenerator generator = new LocalGenerator(body);
						
				 		// Create the counter used for the opaque predicate
						int conditionCounter = 0;
						Value intCounter = generator.generateLocal(IntType.v());
						AssignStmt assignStmt = new JAssignStmt(intCounter, IntConstant.v(conditionCounter));
						body.getUnits().add(assignStmt);
						
						Stmt afterEx = Jimple.v().newNopStmt();
						IfStmt ifStmt = Jimple.v().newIfStmt(Jimple.v().newEqExpr(intCounter,
								IntConstant.v(conditionCounter)), afterEx);
						body.getUnits().add(ifStmt);
						conditionCounter++;
						
						Local lcEx = generator.generateLocal(t.getException().getType());
						AssignStmt assignNewEx = Jimple.v().newAssignStmt(lcEx,
								Jimple.v().newNewExpr(t.getException().getType()));
						body.getUnits().add(assignNewEx);

						InvokeStmt consNewEx = Jimple.v().newInvokeStmt(Jimple.v().newVirtualInvokeExpr(lcEx,
								Scene.v().makeConstructorRef(exceptionClass, Collections.<Type>emptyList())));
						body.getUnits().add(consNewEx);
						
						ThrowStmt throwNewEx = Jimple.v().newThrowStmt(lcEx);
						body.getUnits().add(throwNewEx);
						
						body.getUnits().add(afterEx);
						return meth;
					}
									
				};
				epc.createDummyMain(thrower);
				exceptionThrowers.put(t.getException(), thrower);
				exceptionClass.addMethod(thrower);
			}
			
			// Call the exception thrower after the old call site
			Stmt throwCall = Jimple.v().newInvokeStmt(Jimple.v().newStaticInvokeExpr(thrower.makeRef()));
			caller.getActiveBody().getUnits().insertBefore(throwCall, callSite);
		}
}
 
Example #17
Source File: PedanticThrowAnalysis.java    From JAADAS with GNU General Public License v3.0 2 votes vote down vote up
/**
    * Returns the set of all <code>Throwable</code>s as the set
    * of types that a <code>throw</code> statement may throw implicitly, 
    * that is, the possible types of errors which might arise in
    * the course of executing the <code>throw</code> statement, rather
    * than the type of the <code>throw</code>'s operand.
    *
    * @param t the {@link ThrowStmt} whose exceptions are to be returned.
    *
    * @return the set of all <code>Throwable</code>s.
    */
   public ThrowableSet mightThrowImplicitly(ThrowStmt t) {
return ThrowableSet.Manager.v().ALL_THROWABLES;
   }
 
Example #18
Source File: ThrowAnalysis.java    From JAADAS with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Returns a set representing the {@link Throwable} types that
 * the specified throw statement might throw explicitly, that is,
 * the possible types for its <code>Throwable</code> argument.
 *
 * @param t {@link ThrowStmt} whose explicit exceptions are
 *          to be returned.
 *
 * @return a representation of the possible types of
 * <code>t</code>'s <code>Throwable</code> operand.
 */
ThrowableSet mightThrowExplicitly(ThrowStmt t);
 
Example #19
Source File: ThrowAnalysis.java    From JAADAS with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Returns a set representing the {@link Throwable} types that
 * the specified throw statement might throw implicitly, that is,
 * the possible types of errors which might arise in the course
 * of executing the <code>throw</code> statement, rather than
 * the type of the <code>throw</code>'s operand.
 *
 * @param t {@link ThrowStmt} whose implicit exceptions are
 *          to be returned.
 *
 * @return a representation of the types of exceptions that 
 * <code>t</code> might throw implicitly.
 */
ThrowableSet mightThrowImplicitly(ThrowStmt t);
 
Example #20
Source File: AbstractThrowAnalysis.java    From JAADAS with GNU General Public License v3.0 votes vote down vote up
abstract public ThrowableSet mightThrowImplicitly(ThrowStmt t);