Java Code Examples for soot.jimple.InvokeStmt#getInvokeExpr()

The following examples show how to use soot.jimple.InvokeStmt#getInvokeExpr() . 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: TimingBombTransformer.java    From FuzzDroid with Apache License 2.0 7 votes vote down vote up
private void prepareAlarmManagerSet(Body body, InvokeStmt setStmt, SootMethodRef reportRef) {
	Value oldVal = setStmt.getInvokeExpr().getArg(1);
	
	Local longLocal = UtilInstrumenter.generateFreshLocal(body, LongType.v());
	SootMethod currentTimeMillis = Scene.v().getMethod("<java.lang.System: long currentTimeMillis()>");		
	StaticInvokeExpr timeInvoke = Jimple.v().newStaticInvokeExpr(currentTimeMillis.makeRef());		
	AssignStmt timeInitalize = Jimple.v().newAssignStmt(longLocal, timeInvoke);
	
	AddExpr addTime = Jimple.v().newAddExpr(longLocal, LongConstant.v(2000L));
	AssignStmt timeAssign = Jimple.v().newAssignStmt(longLocal, addTime);
			
	
	body.getUnits().insertBefore(timeInitalize, setStmt);
	body.getUnits().insertBefore(timeAssign, setStmt);
	
	InvokeExpr expr = setStmt.getInvokeExpr();
	expr.setArg(0, IntConstant.v(0));
	expr.setArg(1, longLocal);
	
	// Report the change
	InvokeStmt reportStmt = Jimple.v().newInvokeStmt(Jimple.v().newStaticInvokeExpr(
			reportRef, oldVal, longLocal));
	reportStmt.addTag(new InstrumentedCodeTag());
	body.getUnits().insertAfter(reportStmt, setStmt);
}
 
Example 2
Source File: TimingBombTransformer.java    From FuzzDroid with Apache License 2.0 5 votes vote down vote up
@Override
protected void internalTransform(Body body, String phaseName,
		Map<String, String> options) {
	
	if (!canInstrumentMethod(body.getMethod()))
		return;
	
	// Get a reference to the reporter method
	SootMethodRef reporterRef = Scene.v().getMethod("<de.tu_darmstadt.sse.additionalappclasses.tracing.BytecodeLogger: "
			+ "void reportTimingBomb(long,long)>").makeRef();
	
	for (Iterator<Unit> unitIt = body.getUnits().snapshotIterator(); unitIt.hasNext(); ) {
		Unit curUnit = unitIt.next();
		
		if(curUnit instanceof InvokeStmt) {
			InvokeStmt invokeStmt = (InvokeStmt) curUnit;
			InvokeExpr expr = invokeStmt.getInvokeExpr();
			String methodSig = expr.getMethod().getSignature();
			
			if(methodSig.equals("<android.app.AlarmManager: void set(int,long,android.app.PendingIntent)>"))
				prepareAlarmManagerSet(body, invokeStmt, reporterRef);
			else if(methodSig.equals("<android.os.Handler: boolean postDelayed(java.lang.Runnable,long)>"))
				prepareHandlerPostDelayed(body, invokeStmt, reporterRef);
		}
	}
	
}
 
Example 3
Source File: PathExecutionTransformer.java    From FuzzDroid with Apache License 2.0 4 votes vote down vote up
private void instrumentInfoAboutNonApiCaller(Body body, Unit unit)
{	
	//our instrumented code
	if(unit.hasTag(InstrumentedCodeTag.name))
		return;
	
	InvokeExpr invokeExpr = null;
	if(unit instanceof DefinitionStmt)
	{
		DefinitionStmt defStmt = (DefinitionStmt)unit;
		if(defStmt.containsInvokeExpr())
		{
			invokeExpr = defStmt.getInvokeExpr();
		}
	}					
	else if(unit instanceof InvokeStmt)
	{
		InvokeStmt invokeStmt = (InvokeStmt)unit;
		invokeExpr = invokeStmt.getInvokeExpr();
	}				
	
	if(invokeExpr != null)
	{			
		if(!UtilInstrumenter.isApiCall(invokeExpr))
		{
			String invokeExprMethodSignature = invokeExpr.getMethod().getSignature();
			
			List<Value> parameter = invokeExpr.getArgs();
			List<Unit> generated = new ArrayList<Unit>();
			Pair<Value, List<Unit>> arrayRefAndInstrumentation = UtilInstrumenter.generateParameterArray(parameter, body);
			
			List<Unit> generatedArrayInstrumentation = arrayRefAndInstrumentation.getSecond();
			Value arrayRef = arrayRefAndInstrumentation.getFirst();
			
			Unit generatedInvokeStmt = UtilInstrumenter.makeJimpleStaticCallForPathExecution("logInfoAboutNonApiMethodCaller", 	
					RefType.v("java.lang.String"), StringConstant.v(body.getMethod().getSignature()),
					RefType.v("java.lang.String"), StringConstant.v(invokeExprMethodSignature),
					UtilInstrumenter.getParameterArrayType(), (parameter.isEmpty())? NullConstant.v() : arrayRef);
			generatedInvokeStmt.addTag(new InstrumentedCodeTag());
			generated.addAll(generatedArrayInstrumentation);
			generated.add(generatedInvokeStmt);

			body.getUnits().insertBefore(generated, unit);
		}
	}		
}
 
Example 4
Source File: JimpleStmtVisitorImpl.java    From FuzzDroid with Apache License 2.0 4 votes vote down vote up
@Override
public void caseInvokeStmt(InvokeStmt stmt) {
	InvokeExpr invokeExpr = stmt.getInvokeExpr();
	SootClass declaringClass = invokeExpr.getMethod().getDeclaringClass();
	if(exprVisitor.isExpressionThatNeedsToBeConvertedToSMT(invokeExpr))
		exprVisitor.convertSpecialExpressionsToSMT(invokeExpr, stmt);
	else if(UtilInstrumenter.isAppDeveloperCode(declaringClass)) {
		SootMethod method = invokeExpr.getMethod();
		Body body = method.retrieveActiveBody();
		
		SMTBinding newRhs = getBindingForTaintedValue(stmt);
		//if there is no taint-tracking involved (newRhs == null), we do not have to do anything here
		if(newRhs == null)
			return;
		
		int indexOfInterest = -1;
		for(int i = 0; i < invokeExpr.getArgCount(); i++) {
			if(newRhs.getVariableName().equals(invokeExpr.getArg(i).toString())) {
				indexOfInterest = i;
				break;
			}
		}
		
		if(indexOfInterest == -1)
			return;
		
		
		for(Unit unit : body.getUnits()) {
			if(unit instanceof IdentityStmt) {
				IdentityStmt identity = (IdentityStmt)unit;
				Value rhs = identity.getRightOp();
				if(rhs instanceof ParameterRef) {
					ParameterRef param = (ParameterRef)rhs;
					if(param.getIndex() == indexOfInterest) {
						Value lhs = identity.getLeftOp();
						SMTBinding newLhs = createNewBindingForValue(lhs);
						addValueBindingToVariableDeclaration(lhs, newLhs);
						SMTSimpleAssignment simpleAssignment = new SMTSimpleAssignment(newLhs, new SMTBindingValue(newRhs));
						SMTAssertStatement assignmentAssert = new SMTAssertStatement(simpleAssignment);
						addAssertStmtToAllPrograms(assignmentAssert);
					}
				}					
			}
		}
	}		
	else {
		System.err.println(String.format("Double-Check if the following method contains useful information which can be extracted: \n%s", stmt));
	}
	
}
 
Example 5
Source File: StmtTranslator.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
public void caseInvokeStmt(InvokeStmt stmt) {
	InvokeExpr expr = stmt.getInvokeExpr();
	Variable lvar = jt.makeVariable(expr);
	et.translateExpr(lvar, stmt.getInvokeExprBox());
}
 
Example 6
Source File: TypeResolver.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
private void split_new()
{		
	LocalDefs defs = LocalDefs.Factory.newLocalDefs(jb);
	PatchingChain<Unit> units = this.jb.getUnits();
	Stmt[] stmts = new Stmt[units.size()];
	
	units.toArray(stmts);
	
	for ( Stmt stmt : stmts )
	{
		if ( stmt instanceof InvokeStmt )
		{
			InvokeStmt invoke = (InvokeStmt)stmt;
			
			if ( invoke.getInvokeExpr() instanceof SpecialInvokeExpr )
			{
				SpecialInvokeExpr special
					= (SpecialInvokeExpr)invoke.getInvokeExpr();
				
				if ( special.getMethodRef().name().equals("<init>") )
				{
					List<Unit> deflist = defs.getDefsOfAt(
						(Local)special.getBase(), invoke);
					
					while ( deflist.size() == 1 )
					{
						Stmt stmt2 = (Stmt)deflist.get(0);
						
						if ( stmt2 instanceof AssignStmt )
						{
							AssignStmt assign = (AssignStmt)stmt2;
							
							if ( assign.getRightOp() instanceof Local )
							{
								deflist = defs.getDefsOfAt(
									(Local)assign.getRightOp(), assign);
								continue;
							}
							else if ( assign.getRightOp()
								instanceof NewExpr )
							{
								Local newlocal = Jimple.v().newLocal(
									"tmp", null);
								newlocal.setName("tmp$" + System.identityHashCode(newlocal));
								this.jb.getLocals().add(newlocal);
								
								special.setBase(newlocal);
								
								DefinitionStmt assignStmt
									= Jimple.v().newAssignStmt(
									assign.getLeftOp(), newlocal);
								Unit u = Util.findLastIdentityUnit(jb, assign);
								units.insertAfter(assignStmt, u);
								assign.setLeftOp(newlocal);
								
								this.addLocal(newlocal);
								this.initAssignment(assignStmt);
							}
						}
						break;
					}
				}
			}
		}
	}
}
 
Example 7
Source File: TypeResolver.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
private void split_new()
 {
LocalDefs defs = LocalDefs.Factory.newLocalDefs(stmtBody);
   PatchingChain<Unit> units = stmtBody.getUnits();
   Stmt[] stmts = new Stmt[units.size()];

   units.toArray(stmts);
   
   for (Stmt stmt : stmts) {
if(stmt instanceof InvokeStmt)
  {
    InvokeStmt invoke = (InvokeStmt) stmt;
    
    if(invoke.getInvokeExpr() instanceof SpecialInvokeExpr)
      {
	SpecialInvokeExpr special = (SpecialInvokeExpr) invoke.getInvokeExpr();
	
	if("<init>".equals(special.getMethodRef().name()))
	  {
	    List<Unit> deflist = defs.getDefsOfAt((Local) special.getBase(), invoke);
	    
	    while(deflist.size() == 1)
	      {
		Stmt stmt2 = (Stmt) deflist.get(0);
		
		if(stmt2 instanceof AssignStmt)
		  {
		    AssignStmt assign = (AssignStmt) stmt2;
		    
		    if(assign.getRightOp() instanceof Local)
		      {
			deflist = defs.getDefsOfAt((Local) assign.getRightOp(), assign);
			continue;
		      }
		    else if(assign.getRightOp() instanceof NewExpr)
		      {			
			// We split the local.
			//G.v().out.println("split: [" + assign + "] and [" + stmt + "]");
			Local newlocal = Jimple.v().newLocal("tmp", null);
			stmtBody.getLocals().add(newlocal);
			
			special.setBase(newlocal);
			
			units.insertAfter(Jimple.v().newAssignStmt(assign.getLeftOp(), newlocal), assign);
			assign.setLeftOp(newlocal);
		      }
		  }
		break;
	      }
	  }
      }
  }
     }
 }
 
Example 8
Source File: TypeResolverBV.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
private void split_new()
 {
LocalDefs defs = LocalDefs.Factory.newLocalDefs(stmtBody);
   PatchingChain<Unit> units = stmtBody.getUnits();
   Stmt[] stmts = new Stmt[units.size()];

   units.toArray(stmts);
   
   for (Stmt stmt : stmts) {
if(stmt instanceof InvokeStmt)
  {
    InvokeStmt invoke = (InvokeStmt) stmt;
    
    if(invoke.getInvokeExpr() instanceof SpecialInvokeExpr)
      {
	SpecialInvokeExpr special = (SpecialInvokeExpr) invoke.getInvokeExpr();
	
	if(special.getMethodRef().name().equals("<init>"))
	  {
	    List<Unit> deflist = defs.getDefsOfAt((Local) special.getBase(), invoke);
	    
	    while(deflist.size() == 1)
	      {
		Stmt stmt2 = (Stmt) deflist.get(0);
		
		if(stmt2 instanceof AssignStmt)
		  {
		    AssignStmt assign = (AssignStmt) stmt2;
		    
		    if(assign.getRightOp() instanceof Local)
		      {
			deflist = defs.getDefsOfAt((Local) assign.getRightOp(), assign);
			continue;
		      }
		    else if(assign.getRightOp() instanceof NewExpr)
		      {			
			// We split the local.
			//G.v().out.println("split: [" + assign + "] and [" + stmt + "]");
			Local newlocal = Jimple.v().newLocal("tmp", null);
			stmtBody.getLocals().add(newlocal);
			
			special.setBase(newlocal);
			
			units.insertAfter(Jimple.v().newAssignStmt(assign.getLeftOp(), newlocal), assign);
			assign.setLeftOp(newlocal);
		      }
		  }
		break;
	      }
	  }
      }
  }
     }
 }
 
Example 9
Source File: MethodCallFinder.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
public void inInvokeStmt(InvokeStmt s){
   	InvokeExpr invokeExpr = s.getInvokeExpr();
   	SootMethod maybeInline = invokeExpr.getMethod();

   	//check whether we want to inline
   	ASTMethodNode toInlineASTMethod = cleaner.inline(maybeInline);
   	if(toInlineASTMethod ==null){
   		//not to inline
   		return;
   	}
   	else{//yes we want to inline 
   		// we know that the method to be inlined has no declarations.
   		List<Object> subBodies = toInlineASTMethod.get_SubBodies();
   		if(subBodies.size() != 1){
   			throw new RuntimeException ("Found ASTMEthod node with more than one subBodies");
   		}
   		List body = (List)subBodies.get(0);

    
   		ASTParentNodeFinder finder = new ASTParentNodeFinder();
   		underAnalysis.apply(finder);
    
   		List<ASTStatementSequenceNode> newChangedBodyPart = createChangedBodyPart(s,body,finder);


   		boolean replaced = replaceSubBody(s,newChangedBodyPart,finder);

    
   		if(replaced){
   			//so the invoke stmt has been replaced with the body of the method invoked

   			/*
   			 * if the inlined method contained an assignment to a static field
   			 * we want to replace that with a throw stmt
   			 */
   			StaticDefinitionFinder defFinder = new StaticDefinitionFinder(maybeInline);
   			toInlineASTMethod.apply(defFinder);
   			
   			if(defFinder.anyFinalFieldDefined()){
   				//create throw stmt to be added to inlined method

   				//create a SootMethodRef
   				SootClass runtime = Scene.v().loadClassAndSupport("java.lang.RuntimeException");
   				if(runtime.declaresMethod("void <init>(java.lang.String)")){
		SootMethod sootMethod = runtime.getMethod("void <init>(java.lang.String)");
		SootMethodRef methodRef = sootMethod.makeRef();
		RefType myRefType = RefType.v(runtime);
		StringConstant tempString = StringConstant.v("This method used to have a definition of a final variable. "+
							     "Dava inlined the definition into the static initializer");
		List list = new ArrayList();
		list.add(tempString);
		
		GNewInvokeExpr newInvokeExpr = new GNewInvokeExpr(myRefType,methodRef,list);

		GThrowStmt throwStmt = new GThrowStmt(newInvokeExpr);
					
		AugmentedStmt augStmt = new AugmentedStmt(throwStmt);
		List<Object> sequence = new ArrayList<Object>();
		sequence.add(augStmt);
		ASTStatementSequenceNode seqNode = new ASTStatementSequenceNode(sequence);
		List<Object> subBody = new ArrayList<Object>();
		subBody.add(seqNode);

		toInlineASTMethod.replaceBody(subBody);
	    }
	}
    }

}
   }
 
Example 10
Source File: ConstructorFolder.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
/** This method change all new Obj/<init>(args) pairs to new Obj(args) idioms. */
protected void internalTransform(Body b, String phaseName, Map options)
{
    GrimpBody body = (GrimpBody)b;

    if(Options.v().verbose())
        G.v().out.println("[" + body.getMethod().getName() +
            "] Folding constructors...");

  Chain units = body.getUnits();
  List<Unit> stmtList = new ArrayList<Unit>();
  stmtList.addAll(units);

  Iterator<Unit> it = stmtList.iterator();
  
  LocalUses localUses = LocalUses.Factory.newLocalUses(b);

  /* fold in NewExpr's with specialinvoke's */
  while (it.hasNext())
    {
      Stmt s = (Stmt)it.next();
        
      if (!(s instanceof AssignStmt))
        continue;
        
      /* this should be generalized to ArrayRefs */
      Value lhs = ((AssignStmt)s).getLeftOp();
      if (!(lhs instanceof Local))
        continue;
        
      Value rhs = ((AssignStmt)s).getRightOp();
      if (!(rhs instanceof NewExpr))
        continue;

      /* TO BE IMPLEMENTED LATER: move any copy of the object reference
         for lhs down beyond the NewInvokeExpr, with the rationale
         being that you can't modify the object before the constructor
         call in any case.

         Also, do note that any new's (object creation) without
         corresponding constructors must be dead. */
       
      List lu = localUses.getUsesOf(s);
      Iterator luIter = lu.iterator();
      boolean MadeNewInvokeExpr = false;
       
      while (luIter.hasNext())
        {
          Unit use = ((UnitValueBoxPair)(luIter.next())).unit;
          if (!(use instanceof InvokeStmt))
            continue;
          InvokeStmt is = (InvokeStmt)use;
          if (!(is.getInvokeExpr() instanceof SpecialInvokeExpr) ||
              lhs != ((SpecialInvokeExpr)is.getInvokeExpr()).getBase())
            continue;
          
          SpecialInvokeExpr oldInvoke = 
            ((SpecialInvokeExpr)is.getInvokeExpr());
          LinkedList invokeArgs = new LinkedList();
          for (int i = 0; i < oldInvoke.getArgCount(); i++)
            invokeArgs.add(oldInvoke.getArg(i));
          
          AssignStmt constructStmt = Grimp.v().newAssignStmt
            ((AssignStmt)s);
          constructStmt.setRightOp
            (Grimp.v().newNewInvokeExpr
             (((NewExpr)rhs).getBaseType(), oldInvoke.getMethodRef(), invokeArgs));
          MadeNewInvokeExpr = true;
          
          use.redirectJumpsToThisTo(constructStmt);
          units.insertBefore(constructStmt, use);
          units.remove(use);
        }
      if (MadeNewInvokeExpr)
        {
          units.remove(s);
        }
    }
}