1 19 20 25 26 package soot.jimple.toolkits.invoke; 27 28 import soot.*; 29 import soot.jimple.*; 30 import soot.util.*; 31 import java.util.*; 32 33 34 public class ThrowManager 35 { 36 45 46 public static Stmt getNullPointerExceptionThrower(JimpleBody b) 47 { 48 Chain units = b.getUnits(); 49 Set trappedUnits = TrapManager.getTrappedUnitsOf(b); 50 51 for (Stmt s = (Stmt)units.getLast(); s != units.getFirst(); 52 s = (Stmt)units.getPredOf(s)) 53 { 54 if (trappedUnits.contains(s)) 55 continue; 56 if (s instanceof ThrowStmt) 57 { 58 Value throwee = ((ThrowStmt)s).getOp(); 59 if (throwee instanceof Constant) 60 continue; 61 62 if (s == units.getFirst()) 63 break; 64 Stmt prosInvoke = (Stmt)units.getPredOf(s); 65 66 if (!(prosInvoke instanceof InvokeStmt)) 67 continue; 68 69 if (prosInvoke == units.getFirst()) 70 break; 71 Stmt prosNew = (Stmt)units.getPredOf(prosInvoke); 72 73 if (!(prosNew instanceof AssignStmt)) 74 continue; 75 76 InvokeExpr ie = (InvokeExpr)((InvokeStmt)prosInvoke).getInvokeExpr(); 77 if (!(ie instanceof SpecialInvokeExpr)) 78 continue; 79 80 if (((SpecialInvokeExpr)ie).getBase() != throwee || 81 !ie.getMethodRef().name().equals("<init>")) 82 continue; 83 84 Value lo = ((AssignStmt)prosNew).getLeftOp(); 85 Value ro = ((AssignStmt)prosNew).getRightOp(); 86 if (lo != throwee || !(ro instanceof NewExpr)) 87 continue; 88 89 Type newType = ((NewExpr)ro).getBaseType(); 90 if (!newType.equals(RefType.v("java.lang.NullPointerException"))) 91 continue; 92 93 return prosNew; 95 } 96 } 97 98 Stmt last = (Stmt)units.getLast(); 100 101 return addThrowAfter(b, last); 102 } 103 104 static Stmt addThrowAfter(JimpleBody b, Stmt target) 105 { 106 Chain units = b.getUnits(); 107 Chain locals = b.getLocals(); 108 int i = 0; 109 110 boolean canAddI = false; 112 do 113 { 114 canAddI = true; 115 Iterator localIt = locals.iterator(); 116 while (localIt.hasNext()) 117 { 118 Local l = (Local)localIt.next(); 119 if (l.getName().equals("__throwee"+i)) 120 canAddI = false; 121 } 122 if (!canAddI) 123 i++; 124 } 125 while (!canAddI); 126 127 Local l = Jimple.v().newLocal("__throwee"+i, RefType.v("java.lang.NullPointerException")); 128 b.getLocals().add(l); 129 130 Stmt newStmt = Jimple.v().newAssignStmt 131 (l, Jimple.v().newNewExpr(RefType.v("java.lang.NullPointerException"))); 132 133 Stmt invStmt = Jimple.v().newInvokeStmt 134 (Jimple.v().newSpecialInvokeExpr(l, Scene.v().getMethod("<java.lang.NullPointerException: void <init>()>").makeRef())); 135 136 Stmt throwStmt = Jimple.v().newThrowStmt(l); 137 138 units.insertAfter(newStmt, target); 139 units.insertAfter(invStmt, newStmt); 140 units.insertAfter(throwStmt, invStmt); 141 return newStmt; 142 } 143 144 146 static boolean isExceptionCaughtAt(SootClass e, Stmt stmt, Body b) 147 { 148 151 152 Hierarchy h = new Hierarchy(); 153 154 Iterator trapsIt = b.getTraps().iterator(); 155 156 while (trapsIt.hasNext()) 157 { 158 Trap t = (Trap)trapsIt.next(); 159 160 161 if (h.isClassSubclassOfIncluding(e, t.getException())) 162 { 163 Iterator it = b.getUnits().iterator(t.getBeginUnit(), 164 t.getEndUnit()); 165 while (it.hasNext()) 166 if (stmt.equals(it.next())) 167 return true; 168 } 169 } 170 171 return false; 172 } 173 } 174 | Popular Tags |