1 19 20 package soot.toolkits.exceptions; 21 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import soot.Body; 26 import soot.BodyTransformer; 27 import soot.G; 28 import soot.Singletons; 29 import soot.Trap; 30 import soot.Unit; 31 import soot.options.Options; 32 import soot.util.Chain; 33 import soot.toolkits.graph.ExceptionalUnitGraph; 34 35 51 52 public final class TrapTightener extends BodyTransformer { 53 54 public TrapTightener( Singletons.Global g ) {} 55 public static TrapTightener v() { return soot.G.v().soot_toolkits_exceptions_TrapTightener(); } 56 57 protected void internalTransform(Body body, String phaseName, Map options) { 58 if(Options.v().verbose()) 59 G.v().out.println("[" + body.getMethod().getName() + "] Tightening trap boundaries..."); 60 61 Chain trapChain = body.getTraps(); 62 Chain unitChain = body.getUnits(); 63 if (trapChain.size() > 0) { 64 ExceptionalUnitGraph graph = new ExceptionalUnitGraph(body); 65 66 for (Iterator trapIt = trapChain.iterator(); trapIt.hasNext(); ) { 67 Trap trap = (Trap) trapIt.next(); 68 Unit firstTrappedUnit = trap.getBeginUnit(); 69 Unit firstTrappedThrower = null; 70 Unit firstUntrappedUnit = trap.getEndUnit(); 71 Unit lastTrappedUnit = 72 (Unit) unitChain.getPredOf(firstUntrappedUnit); 73 Unit lastTrappedThrower = null; 74 for (Unit u = firstTrappedUnit; 75 u != null && u != firstUntrappedUnit; 76 u = (Unit) unitChain.getSuccOf(u)) { 77 if (mightThrowTo(graph, u, trap)) { 78 firstTrappedThrower = u; 79 break; 80 } 81 } 82 if (firstTrappedThrower != null) { 83 for (Unit u = lastTrappedUnit; 84 u != null; u = (Unit) unitChain.getPredOf(u)) { 85 if (mightThrowTo(graph, u, trap)) { 86 lastTrappedThrower = u; 87 break; 88 } 89 } 90 } 91 if (firstTrappedThrower != null && 92 firstTrappedUnit != firstTrappedThrower) { 93 trap.setBeginUnit(firstTrappedThrower); 94 } 95 if (lastTrappedThrower == null) { 96 lastTrappedThrower = firstTrappedUnit; 97 } 98 if (lastTrappedUnit != lastTrappedThrower) { 99 trap.setEndUnit((Unit) unitChain.getSuccOf(lastTrappedThrower)); 100 } 101 } 102 } 103 } 104 105 116 protected boolean mightThrowTo(ExceptionalUnitGraph g, Unit u, Trap t) { 117 Collection dests = g.getExceptionDests(u); 118 for (Iterator destIt = dests.iterator(); destIt.hasNext(); ) { 119 ExceptionalUnitGraph.ExceptionDest dest = 120 (ExceptionalUnitGraph.ExceptionDest) destIt.next(); 121 if (dest.getTrap() == t) { 122 return true; 123 } 124 } 125 return false; 126 } 127 } 128 | Popular Tags |