1 19 20 package soot.dava.toolkits.base.renamer; 21 22 import soot.dava.toolkits.base.AST.analysis.*; 23 import soot.*; 24 import soot.jimple.*; 25 import java.util.*; 26 import soot.dava.*; 28 import soot.grimp.*; 29 import soot.grimp.internal.*; 30 import soot.dava.internal.javaRep.*; 31 import soot.dava.internal.asg.*; 32 import soot.jimple.internal.*; 33 import soot.dava.internal.AST.*; 34 35 public class infoGatheringAnalysis extends DepthFirstAdapter { 36 37 public boolean DEBUG=false; 38 39 public final static int CLASSNAME = 0; 41 public final static int METHODNAME = 1; 42 43 public final static int GETSET = 2; 44 45 public final static int IF = 3; 46 47 public final static int WHILE = 4; 48 49 public final static int SWITCH = 5; 50 51 public final static int ARRAYINDEX = 6; 52 53 public final static int MAINARG = 7; 55 public final static int FIELDASSIGN = 8; 57 public final static int FORLOOPUPDATE = 9; 59 public final static int CAST = 10; 60 61 public final static int NUMBITS = 11; 62 63 heuristicSet info; 65 66 boolean inDefinitionStmt = false; 68 69 Local definedLocal = null; 71 72 boolean inIf = false; 74 75 boolean inWhile = false; 77 78 boolean inFor = false; 80 81 public infoGatheringAnalysis(DavaBody davaBody) { 82 info = new heuristicSet(); 83 84 List localList = new ArrayList(); 85 89 HashSet params = new HashSet(); 90 HashSet thisLocals = davaBody.get_ThisLocals(); 93 94 96 Iterator localIt = davaBody.getLocals().iterator(); 97 98 while (localIt.hasNext()) { 99 Local local = (Local) localIt.next(); 100 101 if (params.contains(local) || thisLocals.contains(local)) 102 continue; 103 localList.add(local); 104 } 105 106 Iterator it = localList.iterator(); 109 while (it.hasNext()) { 110 Local local = (Local) it.next(); 111 info.add(local, NUMBITS); 112 debug("infoGatheringAnalysis","added "+local.getName()+ " to the heuristicset"); 113 } 114 115 119 SootMethod method = davaBody.getMethod(); 121 if (method.getSubSignature().compareTo("void main(java.lang.String[])") == 0) { 123 it = davaBody.get_ParamMap().values().iterator(); 125 int num = 0; 126 Local param = null; 127 while (it.hasNext()) { 128 num++; 129 param = (Local) it.next(); 130 } 131 if (num > 1) { 132 throw new DecompilationException("main method has greater than 1 args!!"); 133 } else { 134 info.setHeuristic(param, infoGatheringAnalysis.MAINARG); 135 } 136 } 137 } 138 139 146 public void inDefinitionStmt(DefinitionStmt s) { 147 inDefinitionStmt = true; 148 Value v = s.getLeftOp(); 150 if (v instanceof Local) { 151 158 if(info.contains((Local)v)) 159 definedLocal = (Local) v; 160 else 161 definedLocal = null; 162 163 164 } else { 165 } 167 } 168 169 public void outDefinitionStmt(DefinitionStmt s) { 170 if(definedLocal != null && s.getRightOp() instanceof CastExpr){ 173 Type castType = ((CastExpr)s.getRightOp()).getCastType(); 174 info.addCastString(definedLocal,castType.toString()); 175 } 176 inDefinitionStmt = false; 177 definedLocal = null; 178 } 179 180 181 182 187 public void inStaticFieldRef(StaticFieldRef sfr) { 188 if (inDefinitionStmt && (definedLocal != null)) { 189 SootField field = sfr.getField(); 190 info.setFieldName(definedLocal, field.getName()); 191 } 192 } 193 194 199 200 public void inInstanceFieldRef(InstanceFieldRef ifr) { 201 if (ifr instanceof AbstractInstanceFieldRef) { 202 if (inDefinitionStmt && (definedLocal != null)) { 203 SootField field = ((AbstractInstanceFieldRef) ifr).getField(); 204 info.setFieldName(definedLocal, field.getName()); 206 } 207 } 208 } 209 210 216 public void outInvokeExpr(InvokeExpr ie) { 217 if (inDefinitionStmt && (definedLocal != null)) { 219 if (ie instanceof NewInvokeExpr) { 221 RefType ref = ((NewInvokeExpr) ie).getBaseType(); 223 String className = ref.getClassName(); 224 debug("outInvokeExpr","defined local is"+definedLocal); 225 info.setObjectClassName(definedLocal, className); 226 227 } else { 228 SootMethodRef methodRef = ie.getMethodRef(); 229 String name = methodRef.name(); 230 info.setMethodName(definedLocal, name); 232 } 233 } 234 } 235 236 240 public void inASTUnaryCondition(ASTUnaryCondition uc) { 241 Value val = uc.getValue(); 242 if (val instanceof Local) { 243 if (inIf) 244 info.setHeuristic((Local) val, infoGatheringAnalysis.IF); 245 if (inWhile) 246 info.setHeuristic((Local) val, infoGatheringAnalysis.WHILE); 247 } 248 } 249 250 public void inASTBinaryCondition(ASTBinaryCondition bc) { 251 ConditionExpr condition = bc.getConditionExpr(); 252 253 Local local = checkBooleanUse(condition); 254 if (local != null) { 255 if (inIf) 256 info.setHeuristic(local, infoGatheringAnalysis.IF); 257 if (inWhile) 258 info.setHeuristic(local, infoGatheringAnalysis.WHILE); 259 } 260 } 261 262 265 public void inASTIfNode(ASTIfNode node) { 266 inIf = true; 267 } 268 269 272 public void outASTIfNode(ASTIfNode node) { 273 inIf = false; 274 } 275 276 279 public void inASTIfElseNode(ASTIfElseNode node) { 280 inIf = true; 281 } 282 283 286 public void outASTIfElseNode(ASTIfElseNode node) { 287 inIf = false; 288 } 289 290 293 public void inASTWhileNode(ASTWhileNode node) { 294 inWhile = true; 295 } 296 297 300 public void outASTWhileNode(ASTWhileNode node) { 301 inWhile = false; 302 } 303 304 307 public void inASTDoWhileNode(ASTDoWhileNode node) { 308 inWhile = true; 309 } 310 311 314 public void outASTDoWhileNode(ASTDoWhileNode node) { 315 inWhile = false; 316 } 317 318 321 public void inASTSwitchNode(ASTSwitchNode node) { 322 Value key = node.get_Key(); 323 if (key instanceof Local) 324 info.setHeuristic((Local) key, infoGatheringAnalysis.SWITCH); 325 } 326 327 public void inArrayRef(ArrayRef ar) { 328 Value index = ar.getIndex(); 329 if (index instanceof Local) 330 info.setHeuristic((Local) index, infoGatheringAnalysis.ARRAYINDEX); 331 } 332 333 public void inASTTryNode(ASTTryNode node) { 334 335 } 336 337 340 public void inASTForLoopNode(ASTForLoopNode node) { 341 inFor = true; 342 343 Iterator updateIt = node.getUpdate().iterator(); 344 while (updateIt.hasNext()) { 345 AugmentedStmt as = (AugmentedStmt) updateIt.next(); 346 Stmt s = as.get_Stmt(); 347 if (s instanceof GAssignStmt) { 348 Value leftOp = ((GAssignStmt) s).getLeftOp(); 349 if (leftOp instanceof Local) { 350 info.setHeuristic((Local) leftOp, 351 infoGatheringAnalysis.FORLOOPUPDATE); 352 } 353 } 354 } 355 } 356 357 360 public void outASTForLoopNode(ASTForLoopNode node) { 361 inFor = false; 362 } 363 364 368 public void outASTMethodNode(ASTMethodNode node) { 369 if(DEBUG){ 370 System.out.println("SET START"); 371 info.print(); 372 System.out.println("SET END"); 373 } 374 } 375 376 381 private Local checkBooleanUse(ConditionExpr condition) { 382 boolean booleanUse = false; 383 384 if (condition instanceof NeExpr || condition instanceof EqExpr) { 386 Value op1 = condition.getOp1(); 387 Value op2 = condition.getOp2(); 388 if (op1 instanceof DIntConstant) { 389 Type op1Type = ((DIntConstant) op1).type; 390 if (op1Type instanceof BooleanType) 391 booleanUse = true; 392 } else if (op2 instanceof DIntConstant) { 393 Type op2Type = ((DIntConstant) op2).type; 394 if (op2Type instanceof BooleanType) 395 booleanUse = true; 396 } 397 if (booleanUse) { 398 if (op1 instanceof Local) 401 return (Local) op1; 402 else if (op2 instanceof Local) 403 return (Local) op2; 404 } else 405 return null; } 407 return null; } 409 410 public heuristicSet getHeuristicSet() { 411 return info; 412 } 413 414 public void debug(String methodName, String debug){ 415 416 if(DEBUG) 417 System.out.println(methodName+ " DEBUG: "+debug); 418 } 419 420 421 } 422 423 | Popular Tags |