Java Code Examples for soot.util.Chain#getFirst()

The following examples show how to use soot.util.Chain#getFirst() . 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: BodyBuilder.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
public static void updateTraps(Unit oldu, Unit newu, Chain<Trap> traps) {
  int size = traps.size();
  if (size == 0) return;
  
  Trap t = traps.getFirst();
  do {
    if (t.getBeginUnit() == oldu)
      t.setBeginUnit(newu);
    if (t.getEndUnit() == oldu)
      t.setEndUnit(newu);
    if (t.getHandlerUnit() == oldu)
      t.setHandlerUnit(newu);
  } while ((--size > 0) && (t = traps.getSuccOf(t)) != null);
}
 
Example 2
Source File: NullCheckEliminator.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
public void internalTransform(Body body, String phaseName, Map<String,String> options) {

	// really, the analysis should be able to use its own results to determine
	// that some branches are dead, but since it doesn't we just iterate.
	boolean changed;
	do {
	    changed=false;

	    NullnessAnalysis analysis=analysisFactory.newAnalysis(new ExceptionalUnitGraph(body));
	    
	    Chain<Unit> units=body.getUnits();
	    Stmt s;
	    for(s=(Stmt) units.getFirst();s!=null;s=(Stmt) units.getSuccOf(s)) {
		if(!(s instanceof IfStmt)) continue;
		IfStmt is=(IfStmt) s;
		Value c=is.getCondition();
		if(!(c instanceof EqExpr || c instanceof NeExpr)) continue;
		BinopExpr e=(BinopExpr) c;
		Immediate i=null;
		if(e.getOp1() instanceof NullConstant) i=(Immediate) e.getOp2();
		if(e.getOp2() instanceof NullConstant) i=(Immediate) e.getOp1();
		if(i==null) continue;
		boolean alwaysNull = analysis.isAlwaysNullBefore(s, i);
		boolean alwaysNonNull = analysis.isAlwaysNonNullBefore(s, i);
		int elim=0; // -1 => condition is false, 1 => condition is true
		if(alwaysNonNull) elim=c instanceof EqExpr ? -1 : 1;
		if(alwaysNull) elim=c instanceof EqExpr ? 1 : -1;
		Stmt newstmt=null;
		if(elim==-1) newstmt=Jimple.v().newNopStmt();
		if(elim==1) newstmt=Jimple.v().newGotoStmt(is.getTarget());
		if(newstmt!=null) {
		    units.swapWith(s,newstmt);
		    s=newstmt;
		    changed=true;
		}
	    }
	} while(changed);
    }
 
Example 3
Source File: DavaStaticBlockCleaner.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
public void staticBlockInlining(SootClass sootClass){
	this.sootClass=sootClass;
	//retrieve the clinit method if any for sootClass
	//the clinit method gets converted into the static block which could initialize the final variable
	if(!sootClass.declaresMethod("void <clinit>()")){
		//System.out.println("no clinit");
		return;
	}

	SootMethod clinit = sootClass.getMethod("void <clinit>()");
	//System.out.println(clinit);

	//retireve the active body
	if (!clinit.hasActiveBody())
		throw new RuntimeException("method "+ clinit.getName()+ " has no active body!");

	
	Body clinitBody = clinit.getActiveBody();	
    Chain units = ((DavaBody) clinitBody).getUnits();

    if (units.size() != 1) {
        throw new RuntimeException("DavaBody AST doesn't have single root.");
    }

    ASTNode AST = (ASTNode) units.getFirst();
    if(! (AST instanceof ASTMethodNode))
    	throw new RuntimeException("Starting node of DavaBody AST is not an ASTMethodNode");

    //running methodCallFinder on the Clinit method 	
    AST.apply(new MethodCallFinder(this));
}
 
Example 4
Source File: DavaStaticBlockCleaner.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
public ASTMethodNode inline(SootMethod maybeInline){
//check if this method should be inlined

if(sootClass !=null){
    //1, method should belong to the same class as the clinit method
    if(sootClass.declaresMethod(maybeInline.getSubSignature())){
	//System.out.println("The method invoked is from the same class");
	//2, method should be static
	
	if (Modifier.isStatic(maybeInline.getModifiers())){
	    //decided to inline
	    //send the ASTMethod node of the TO BE INLINED METHOD
	    
	    //retireve the active body
	    if (!maybeInline.hasActiveBody())
		throw new RuntimeException("method "+ maybeInline.getName()+ " has no active body!");


	    Body bod = maybeInline.getActiveBody();

	    Chain units = ((DavaBody) bod).getUnits();

	    if (units.size() != 1) {
		throw new RuntimeException("DavaBody AST doesn't have single root.");
	    }

	    ASTNode ASTtemp = (ASTNode) units.getFirst();
	    if(! (ASTtemp instanceof ASTMethodNode))
		throw new RuntimeException("Starting node of DavaBody AST is not an ASTMethodNode");

	    //restricting to methods which do not have any variables declared
	    ASTMethodNode toReturn = (ASTMethodNode)ASTtemp;

	    ASTStatementSequenceNode declarations = toReturn.getDeclarations();
	    if(declarations.getStatements().size() == 0){
		//inline only if there are no declarations in the method inlined
		//System.out.println("No declarations in the method. we can inline this method");
		return toReturn;
	    }
	}
    }
}
return null;//meaning dont inline
   }
 
Example 5
Source File: RemoveEmptyBodyDefaultConstructor.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
public static void checkAndRemoveDefault(SootClass s){
	debug("\n\nRemoveEmptyBodyDefaultConstructor----"+s.getName());
	List methods = s.getMethods();
	Iterator it = methods.iterator();
	List<SootMethod> constructors = new ArrayList<SootMethod>();
	
	while(it.hasNext()){
		SootMethod method = (SootMethod)it.next();
		debug("method name is"+method.getName());
		if(method.getName().indexOf("<init>")>-1){
			//constructor add to constructor list
			constructors.add(method);
		}
	}
	
	if(constructors.size()!=1){
		//cant do anything since there are more than one constructors
		debug("class has more than one constructors cant do anything");
		return;
	}
	
	//only one constructor check its default (no arguments)
	SootMethod constructor = constructors.get(0);
	if(constructor.getParameterCount()!=0){
		//can only deal with default constructors
		debug("constructor is not the default constructor");
		return;
	}
	
	debug("Check that the body is empty....and call to super contains no arguments and delete");
	
	if(!constructor.hasActiveBody()){
		debug("No active body found for the default constructor");
		return;
	}

	Body body = constructor.getActiveBody();
       Chain units = ((DavaBody) body).getUnits();

       if (units.size() != 1) {
       	debug(" DavaBody AST does not have single root");
       	return;
       }

       ASTNode AST = (ASTNode) units.getFirst();
       if(! (AST instanceof ASTMethodNode))
       	throw new RuntimeException("Starting node of DavaBody AST is not an ASTMethodNode");

	ASTMethodNode methodNode = (ASTMethodNode)AST;
	debug("got methodnode check body is empty and super has nothing in it");
	
	List<Object> subBodies = methodNode.get_SubBodies();
	if(subBodies.size()!=1){
		debug("Method node does not have one subBody!!!");
		return;
	}
       
	List methodBody = (List)subBodies.get(0);
	if(methodBody.size()!=0){
		debug("Method body size is greater than 1 so cant do nothing");
		return;
	}

	debug("Method body is empty...check super call is empty");
	
	if(((DavaBody)body).get_ConstructorExpr().getArgCount() != 0){
		debug("call to super not empty");
		return;
	}
	
	debug("REMOVE METHOD");
	s.removeMethod(constructor);
}
 
Example 6
Source File: VoidReturnRemover.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
private static void removeReturn(SootMethod method){
  	// check if this is a void method
  	if(! (method.getReturnType() instanceof VoidType ))
  		return;
	
  	//get the methodnode
  	if(!method.hasActiveBody())
  		return;
  	
      Chain units = ((DavaBody) method.getActiveBody()).getUnits();

      if (units.size() != 1) 
      	return;

      ASTNode AST = (ASTNode) units.getFirst();
      if(! (AST instanceof ASTMethodNode))
      	throw new RuntimeException("Starting node of DavaBody AST is not an ASTMethodNode");

ASTMethodNode node = (ASTMethodNode)AST;

//check there is only 1 subBody		
   List<Object> subBodies = node.get_SubBodies();
   if(subBodies.size()!=1)
   	return;
   
   List subBody = (List)subBodies.get(0);
   //see if the last of this is a stmtseq node
   if(subBody.size()==0){
   	//nothing inside subBody
   	return;
   }

   //check last node is a ASTStatementSequenceNode
   ASTNode last = (ASTNode)subBody.get(subBody.size()-1);
   if(! (last instanceof ASTStatementSequenceNode)) 
   	return;
   	
   	
   //get last statement
   List<Object> stmts = ((ASTStatementSequenceNode)last).getStatements();
   if(stmts.size()==0){
   	//no stmts inside statement sequence node
   	subBody.remove(subBody.size()-1);
   	return;
   }
   AugmentedStmt lastas = (AugmentedStmt)stmts.get(stmts.size()-1);
   Stmt lastStmt = lastas.get_Stmt();
   if(! (lastStmt instanceof ReturnVoidStmt))
   		return;
   			
   //we can remove the lastStmt
   stmts.remove(stmts.size()-1);
   /*
    we need to check if we have made the size 0 in
    which case the stmtSeq Node should also be removed
    */
   if(stmts.size()==0)
   	subBody.remove(subBody.size()-1);
   
   
  }
 
Example 7
Source File: EnhancedUnitGraph.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
/**
 * This method removes all the heads in the CFG except the one
 * that corresponds to the first unit in the method. 
 */

protected void removeBogusHeads()
{
	Chain<Unit> units = body.getUnits();
	Unit trueHead = units.getFirst();
	
	while(this.getHeads().size() > 1)
	{
		for(Iterator<Unit> headItr = this.getHeads().iterator(); headItr.hasNext(); )
		{
			Unit head = headItr.next();
			if(trueHead == head)
				continue;
			
			this.unitToPreds.remove(head);
			List<Unit> succs = this.unitToSuccs.get(head);
			for(Iterator<Unit> succsItr = succs.iterator(); succsItr.hasNext(); )
			{
				List<Unit> tobeRemoved = new ArrayList<Unit>();
				
				Unit succ = succsItr.next();
				List<Unit> predOfSuccs = this.unitToPreds.get(succ);
				

				for(Iterator<Unit> predItr = predOfSuccs.iterator(); predItr.hasNext(); )
				{
					Unit pred = predItr.next();
					if(pred == head)
						tobeRemoved.add(pred);	
					
				}
				
				predOfSuccs.removeAll(tobeRemoved);
			}
			
			this.unitToSuccs.remove(head);
			
			if(units.contains(head))
				units.remove(head);
		}
		
		this.buildHeadsAndTails();
	}
}
 
Example 8
Source File: ICCInstrumentDestination.java    From soot-infoflow-android-iccta with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void assignIntent(SootClass hostComponent, SootMethod method, int indexOfArgs)
  {
  	Body body = method.getActiveBody();

  	PatchingChain<Unit> units = body.getUnits();
  	Chain<Local> locals = body.getLocals();
  	Value intentV = null;
int identityStmtIndex = 0;

  	for (Iterator<Unit> iter = units.snapshotIterator(); iter.hasNext(); )
  	{
  		Stmt stmt = (Stmt) iter.next();
	if (! method.isStatic())
	{
   		if (stmt instanceof IdentityStmt)
   		{			
   			if (identityStmtIndex == indexOfArgs)
   			{
   				intentV = ((IdentityStmt) stmt).getLeftOp();
   			}
   			
   			identityStmtIndex++;
   		}
   		else
   		{
   	 		Local thisLocal = locals.getFirst();
   			
   	 		/*
   			Unit setIntentU = Jimple.v().newAssignStmt(     
   					intentV,
   					Jimple.v().newVirtualInvokeExpr(thisLocal, method.getDeclaringClass().getMethodByName("getIntent").makeRef()));
			*/
   	 		
   	 		/* Using the component that the dummyMain() belongs to, as in some cases the invoked method is only available in its superclass.
   	 		 * and its superclass does not contain getIntent() and consequently cause an runtime exception of couldn't find getIntent(). 
   	 		 * 
   	 		 * RuntimeException: couldn't find method getIntent(*) in com.google.android.gcm.GCMBroadcastReceiver
   	 		*/
   	 		Unit setIntentU = Jimple.v().newAssignStmt(     
   					intentV,
   					Jimple.v().newVirtualInvokeExpr(thisLocal, hostComponent.getMethodByName("getIntent").makeRef()));
   	 		
    		units.insertBefore(setIntentU, stmt);
    		
    		System.out.println(body);
    		
    		return;
   		}
	}
	
  		
  	}
  }