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

The following examples show how to use soot.util.Chain#size() . 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: 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 3
Source File: DavaPrinter.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
private void printStatementsInBody(Body body, java.io.PrintWriter out) {
  
  if (Options.v().verbose())
    System.out.println("Printing "+body.getMethod().getName());
  
    Chain<Unit> units = ((DavaBody) body).getUnits();

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

    UnitPrinter up = new DavaUnitPrinter((DavaBody)body);
    ((ASTNode) units.getFirst()).toString(up);
    out.print( up.toString() );
}
 
Example 4
Source File: UnreachableCodeEliminator.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
protected void internalTransform(Body body, String phaseName, Map<String,String> options) 
{		
	if (Options.v().verbose()) {
		G.v().out.println("[" + body.getMethod().getName() + "] Eliminating unreachable code...");
	}
	
	// Force a conservative ExceptionalUnitGraph() which
	// necessarily includes an edge from every trapped Unit to
	// its handler, so that we retain Traps in the case where
	// trapped units remain, but the default ThrowAnalysis
	// says that none of them can throw the caught exception.
	if (this.throwAnalysis == null)
		this.throwAnalysis = PhaseOptions.getBoolean(options, "remove-unreachable-traps", true)
			? Scene.v().getDefaultThrowAnalysis() : PedanticThrowAnalysis.v();
	ExceptionalUnitGraph graph =  new ExceptionalUnitGraph(body, throwAnalysis, false);

	Chain<Unit> units = body.getUnits();
	int numPruned = units.size();
	
	Set<Unit> reachable = units.isEmpty()
		? Collections.<Unit>emptySet()
		: reachable(units.getFirst(), graph)
		;
	
	// Now eliminate empty traps. (and unreachable handlers)
	//
	// For the most part, this is an atavism, an an artifact of
	// pre-ExceptionalUnitGraph code, when the only way for a trap to 
	// become unreachable was if all its trapped units were removed, and
	// the stmtIt loop did not remove Traps as it removed handler units.
	// We've left this separate test for empty traps here, even though 
	// most such traps would already have been eliminated by the preceding
	// loop, because in arbitrary bytecode you could have
	// handler unit that was still reachable by normal control flow, even
	// though it no longer trapped any units (though such code is unlikely
	// to occur in practice, and certainly no in code generated from Java
	// source.		
	for ( Iterator<Trap> it = body.getTraps().iterator(); it.hasNext(); ) {
		Trap trap = it.next();
		if ( (trap.getBeginUnit() == trap.getEndUnit()) || !reachable.contains(trap.getHandlerUnit()) ) {
			it.remove();
		}
	}
	
	// We must make sure that the end units of all traps which are still
	// alive are kept in the code
	for (Trap t : body.getTraps())
		if (t.getEndUnit() == body.getUnits().getLast())
			reachable.add(t.getEndUnit());
		
	units.retainAll(reachable);   
  	
	numPruned -= units.size();
	
	if (Options.v().verbose()) {
		G.v().out.println("[" + body.getMethod().getName() + "]	 Removed " + numPruned + " statements...");
	}
}
 
Example 5
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 6
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 7
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 8
Source File: TrapTightener.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
protected void internalTransform(Body body, String phaseName, Map<String, String> options) {
	if (this.throwAnalysis == null)
		this.throwAnalysis = Scene.v().getDefaultThrowAnalysis();

	if (Options.v().verbose())
		G.v().out.println("[" + body.getMethod().getName() + "] Tightening trap boundaries...");

	Chain<Trap> trapChain = body.getTraps();
	Chain<Unit> unitChain = body.getUnits();
	if (trapChain.size() > 0) {
		ExceptionalUnitGraph graph = new ExceptionalUnitGraph(body, throwAnalysis);
		Set<Unit> unitsWithMonitor = getUnitsWithMonitor(graph);

		for (Iterator<Trap> trapIt = trapChain.iterator(); trapIt.hasNext();) {
			Trap trap = trapIt.next();
			boolean isCatchAll = trap.getException().getName().equals("java.lang.Throwable");
			Unit firstTrappedUnit = trap.getBeginUnit();
			Unit firstTrappedThrower = null;
			Unit firstUntrappedUnit = trap.getEndUnit();
			Unit lastTrappedUnit = unitChain.getPredOf(firstUntrappedUnit);
			Unit lastTrappedThrower = null;
			for (Unit u = firstTrappedUnit; u != null && u != firstUntrappedUnit; u = unitChain.getSuccOf(u)) {
				if (mightThrowTo(graph, u, trap)) {
					firstTrappedThrower = u;
					break;
				}
				
				// If this is the catch-all block and the current unit has an,
				// active monitor, we need to keep the block
				if (isCatchAll && unitsWithMonitor.contains(u))
					break;
			}
			if (firstTrappedThrower != null) {
				for (Unit u = lastTrappedUnit; u != null; u = unitChain.getPredOf(u)) {						
					if (mightThrowTo(graph, u, trap)) {
						lastTrappedThrower = u;
						break;
					}
					
					// If this is the catch-all block and the current unit has an,
					// active monitor, we need to keep the block
					if (isCatchAll && unitsWithMonitor.contains(u))
						break;
				}
			}
			// If no statement inside the trap can throw an exception, we
			// remove the
			// complete trap.
			if (firstTrappedThrower == null)
				trapIt.remove();
			else {
				if (firstTrappedThrower != null && firstTrappedUnit != firstTrappedThrower) {
					trap.setBeginUnit(firstTrappedThrower);
				}
				if (lastTrappedThrower == null) {
					lastTrappedThrower = firstTrappedUnit;
				}
				if (lastTrappedUnit != lastTrappedThrower) {
					trap.setEndUnit(unitChain.getSuccOf(lastTrappedThrower));
				}
			}
		}
	}
}