1 19 20 25 26 27 29 30 package soot.jimple.toolkits.annotation.nullcheck; 31 import soot.options.*; 32 33 import soot.*; 34 import soot.jimple.*; 35 import soot.util.*; 36 import java.io.*; 37 import java.util.*; 38 import soot.tagkit.*; 39 import soot.jimple.toolkits.annotation.tags.*; 40 import soot.toolkits.graph.*; 41 import soot.toolkits.scalar.*; 42 43 55 56 public class NullPointerChecker extends BodyTransformer 57 { 58 public NullPointerChecker( Singletons.Global g ) {} 59 public static NullPointerChecker v() { return G.v().soot_jimple_toolkits_annotation_nullcheck_NullPointerChecker(); } 60 61 private boolean isProfiling = false; 62 63 private boolean enableOther = true; 64 65 protected void internalTransform(Body body, String phaseName, Map options) 66 { 67 isProfiling = PhaseOptions.getBoolean(options, "profiling"); 68 enableOther = !PhaseOptions.getBoolean(options, "onlyarrayref"); 69 70 { 71 Date start = new Date(); 72 73 if (Options.v().verbose()) 74 G.v().out.println("[npc] Null pointer check for "+body.getMethod().getName() 75 +" started on "+start); 76 77 BranchedRefVarsAnalysis analysis = new BranchedRefVarsAnalysis( 78 new ExceptionalUnitGraph(body)); 79 80 SootClass counterClass = null; 81 SootMethod increase = null; 82 83 if (isProfiling) 84 { 85 counterClass = Scene.v().loadClassAndSupport("MultiCounter"); 86 increase = counterClass.getMethod("void increase(int)") ; 87 } 88 89 Chain units = body.getUnits(); 90 91 Iterator stmtIt = units.snapshotIterator() ; 92 93 while (stmtIt.hasNext()) 94 { 95 Stmt s = (Stmt)stmtIt.next() ; 96 97 Value obj = null; 98 99 if (s.containsArrayRef()) 100 { 101 ArrayRef aref = (ArrayRef)s.getArrayRef(); 102 obj = aref.getBase(); 103 } 104 else 105 { 106 if (enableOther) 107 { 108 if (s instanceof ThrowStmt) 110 { 111 obj = ((ThrowStmt)s).getOp(); 112 } 113 else 114 if (s instanceof MonitorStmt) 116 { 117 obj = ((MonitorStmt)s).getOp(); 118 } 119 else 120 { 121 Iterator boxIt; 122 boxIt = s.getDefBoxes().iterator(); 123 while (boxIt.hasNext()) 124 { 125 ValueBox vBox = (ValueBox)boxIt.next(); 126 Value v = vBox.getValue(); 127 128 if (v instanceof InstanceFieldRef) 130 { 131 obj = ((InstanceFieldRef)v).getBase(); 132 break; 133 } 134 else 135 if (v instanceof InstanceInvokeExpr) 137 { 138 obj = ((InstanceInvokeExpr)v).getBase(); 139 break; 140 } 141 else 142 if (v instanceof LengthExpr) 144 { 145 obj = ((LengthExpr)v).getOp(); 146 break; 147 } 148 } 149 boxIt = s.getUseBoxes().iterator(); 150 while (boxIt.hasNext()) 151 { 152 ValueBox vBox = (ValueBox)boxIt.next(); 153 Value v = vBox.getValue(); 154 155 if (v instanceof InstanceFieldRef) 157 { 158 obj = ((InstanceFieldRef)v).getBase(); 159 break; 160 } 161 else 162 if (v instanceof InstanceInvokeExpr) 164 { 165 obj = ((InstanceInvokeExpr)v).getBase(); 166 break; 167 } 168 else 169 if (v instanceof LengthExpr) 171 { 172 obj = ((LengthExpr)v).getOp(); 173 break; 174 } 175 } 176 } 177 } 178 } 179 180 if (obj != null) 182 { 183 FlowSet beforeSet = (FlowSet)analysis.getFlowBefore(s); 184 185 int vInfo = analysis.anyRefInfo(obj, beforeSet); 186 187 boolean needCheck = 188 (vInfo != analysis.kNonNull); 189 190 if (isProfiling) 191 { 192 int whichCounter = 5; 193 if (!needCheck) 194 whichCounter = 6; 195 196 units.insertBefore(Jimple.v().newInvokeStmt( 197 Jimple.v().newStaticInvokeExpr(increase.makeRef(), 198 IntConstant.v(whichCounter))), s); 199 } 200 201 { 202 Tag nullTag = new NullCheckTag(needCheck); 203 s.addTag(nullTag); 204 } 205 } 206 } 207 208 Date finish = new Date(); 209 if (Options.v().verbose()) 210 { 211 long runtime = finish.getTime()-start.getTime(); 212 long mins = runtime/60000; 213 long secs = (runtime%60000)/1000; 214 G.v().out.println("[npc] Null pointer checker finished. It took " 215 +mins+" mins and "+secs+" secs."); 216 } 217 } 218 } 219 } 220 | Popular Tags |