1 19 20 21 22 23 24 118 119 120 package soot.dava.toolkits.base.AST.transformations; 121 122 import java.util.*; 123 import soot.*; 124 import soot.jimple.*; 125 import soot.jimple.internal.*; 126 import soot.dava.*; 127 import soot.grimp.internal.*; 128 import soot.dava.internal.AST.*; 129 import soot.dava.internal.asg.*; 130 import soot.dava.internal.javaRep.*; 131 import soot.dava.toolkits.base.AST.analysis.*; 132 import soot.dava.toolkits.base.AST.traversals.*; 133 134 135 public class SuperFirstStmtHandler extends DepthFirstAdapter{ 136 137 public final boolean DEBUG = false; 138 139 ASTMethodNode originalASTMethod; DavaBody originalDavaBody; Unit originalConstructorUnit; InstanceInvokeExpr originalConstructorExpr; SootMethod originalSootMethod; SootClass originalSootClass; Map originalPMap; 146 147 148 List argsOneTypes=null; List argsOneValues=null; 151 List argsTwoValues=null; List argsTwoTypes=null; 154 SootMethod newSootPreInitMethod=null; DavaBody newPreInitDavaBody=null; ASTMethodNode newASTPreInitMethod=null; 159 SootMethod newConstructor = null; DavaBody newConstructorDavaBody=null; ASTMethodNode newASTConstructorMethod=null; 164 165 166 167 168 public SuperFirstStmtHandler(ASTMethodNode AST){ 169 this.originalASTMethod=AST; 170 initialize(); 171 } 172 173 public SuperFirstStmtHandler(boolean verbose,ASTMethodNode AST){ 174 super(verbose); 175 this.originalASTMethod=AST; 176 initialize(); 177 } 178 179 public void initialize(){ 180 originalDavaBody = originalASTMethod.getDavaBody(); 181 originalConstructorUnit = originalDavaBody.get_ConstructorUnit(); 182 183 originalConstructorExpr = originalDavaBody.get_ConstructorExpr(); 184 if(originalConstructorExpr != null){ 185 argsTwoValues = originalConstructorExpr.getArgs(); 187 188 argsTwoTypes = new ArrayList(); 189 190 Iterator valIt = argsTwoValues.iterator(); 192 while(valIt.hasNext()){ 193 Value val = (Value)valIt.next(); 194 Type type = val.getType(); 195 argsTwoTypes.add(type); 196 } 197 } 198 199 200 201 originalSootMethod = originalDavaBody.getMethod(); 202 originalSootClass = originalSootMethod.getDeclaringClass(); 203 204 originalPMap = originalDavaBody.get_ParamMap(); 205 206 argsOneTypes = originalSootMethod.getParameterTypes(); 207 208 argsOneValues = new ArrayList(); 210 Iterator typeIt = argsOneTypes.iterator(); 211 int count = 0; 212 while (typeIt.hasNext()) { 213 Type t = (Type) typeIt.next(); 214 215 argsOneValues.add(originalPMap.get(new Integer (count))); 216 count++; 217 } 218 219 } 220 221 222 223 226 public void inASTStatementSequenceNode(ASTStatementSequenceNode node){ 227 List stmts = node.getStatements(); 228 Iterator it = stmts.iterator(); 229 while(it.hasNext()){ 230 AugmentedStmt as = (AugmentedStmt)it.next(); 231 Unit u = as.get_Stmt(); 232 233 if (u == originalConstructorUnit){ 234 236 ASTParentNodeFinder parentFinder = new ASTParentNodeFinder(); 238 originalASTMethod.apply(parentFinder); 239 240 Object tempParent = parentFinder.getParentOf(node); 241 if( tempParent != originalASTMethod){ 242 removeInit(); 246 return; 247 } 248 249 251 252 253 254 255 createSootPreInitMethod(); 258 createNewASTPreInitMethod(node); 261 263 if(newASTPreInitMethod == null){ 264 removeInit(); 268 return; 269 } 270 271 if(!finalizePreInitMethod()){ 272 removeInit(); 275 return; 276 } 277 278 279 280 281 282 283 284 createNewConstructor(); 286 287 createNewASTConstructor(node); 288 289 if(!createCallToSuper()){ 290 removeInit(); 294 return; 295 } 296 finalizeConstructor(); 297 298 299 300 301 302 303 if(changeOriginalAST()){ 304 306 debug("SuperFirstStmtHandler....inASTStatementSeuqneNode","Added PreInit"); 307 G.v().SootMethodAddedByDava=true; 308 G.v().SootMethodsAdded.add(newSootPreInitMethod); 309 G.v().SootMethodsAdded.add(newConstructor); 310 311 312 313 314 315 316 G.v().SootClassNeedsDavaSuperHandlerClass.add(originalSootClass); 320 321 } 323 324 325 } 326 } 327 } 328 329 334 public void removeInit(){ 335 List newBody = new ArrayList(); 337 338 List subBody = (List)originalASTMethod.get_SubBodies(); 339 if(subBody.size()!=1) 340 return; 341 342 List oldBody = (List)subBody.get(0); 343 Iterator oldIt = oldBody.iterator(); 344 while(oldIt.hasNext()){ 345 ASTNode node = (ASTNode)oldIt.next(); 347 348 if(!(node instanceof ASTStatementSequenceNode)){ 350 newBody.add(node); 351 continue; 352 } 353 354 ASTStatementSequenceNode seqNode = (ASTStatementSequenceNode)node; 357 358 List newStmtList = new ArrayList(); 359 360 List stmts = seqNode.getStatements(); 361 Iterator it = stmts.iterator(); 362 while(it.hasNext()){ 363 AugmentedStmt augStmt = (AugmentedStmt)it.next(); 364 Stmt stmtTemp = augStmt.get_Stmt(); 365 if(stmtTemp == originalConstructorUnit){ 366 } 368 else{ 369 newStmtList.add(augStmt); 370 } 371 } 372 if(newStmtList.size()!=0){ 373 newBody.add(new ASTStatementSequenceNode(newStmtList)); 374 } 375 } 376 377 originalASTMethod.replaceBody(newBody); 378 379 } 380 381 382 383 384 385 386 390 public boolean changeOriginalAST(){ 391 393 if(originalConstructorExpr == null){ 395 return false; 398 } 399 400 List thisArgList = new ArrayList(); 401 thisArgList.addAll(argsOneValues); 402 403 DStaticInvokeExpr newInvokeExpr = new DStaticInvokeExpr(newSootPreInitMethod.makeRef(),argsOneValues); 404 thisArgList.add(newInvokeExpr); 405 406 407 InstanceInvokeExpr tempExpr = 409 new DSpecialInvokeExpr(originalConstructorExpr.getBase(),newConstructor.makeRef(),thisArgList); 410 411 originalDavaBody.set_ConstructorExpr(tempExpr); 412 413 GInvokeStmt s = new GInvokeStmt(tempExpr); 415 416 originalDavaBody.set_ConstructorUnit((Unit)s); 417 418 originalASTMethod.setDeclarations(new ASTStatementSequenceNode(new ArrayList())); 420 originalASTMethod.replaceBody(new ArrayList()); 421 return true; 422 423 } 424 425 private SootMethodRef makeMethodRef(String methodName,ArrayList args){ 426 428 SootMethod method = new SootMethod(methodName,args,RefType.v("java.lang.Object")); 429 430 method.setDeclaringClass(new SootClass("DavaSuperHandler")); 432 return method.makeRef(); 433 } 434 435 436 444 private boolean createCallToSuper(){ 445 446 if (originalConstructorExpr == null) { 448 return false; 451 } 452 454 SootClass parentClass =originalSootClass.getSuperclass(); 456 457 if(!(parentClass.declaresMethod("<init>",argsTwoTypes))){ 460 return false; 462 } 463 464 SootMethod superConstructor = parentClass.getMethod("<init>",argsTwoTypes); 465 466 468 472 List argsForConstructor = new ArrayList(); 473 int count=0; 474 475 477 RefType type = (new SootClass("DavaSuperHandler")).getType(); 479 480 Local jimpleLocal = new JimpleLocal("handler",type); 483 ArrayList tempList = new ArrayList(); 485 tempList.add(IntType.v()); 486 SootMethodRef getMethodRef = makeMethodRef("get",tempList); 487 488 List tempArgList = null; 489 490 Iterator typeIt = argsTwoTypes.iterator(); 491 while(typeIt.hasNext()){ 492 Type tempType = (Type)typeIt.next(); 493 494 DIntConstant arg = DIntConstant.v(count,IntType.v()); count++; 496 tempArgList = new ArrayList(); 497 tempArgList.add(arg); 498 499 DVirtualInvokeExpr tempInvokeExpr = 500 new DVirtualInvokeExpr(jimpleLocal,getMethodRef,tempArgList,new HashSet()); 501 502 505 506 Value toAddExpr = null; 508 if(tempType instanceof RefType){ 509 toAddExpr = new GCastExpr(tempInvokeExpr,tempType); 511 } 512 else if(tempType instanceof PrimType){ 513 Type wrapperType=null; 517 518 519 PrimType t = (PrimType)tempType; 520 522 if (t == BooleanType.v()){ 523 Value tempExpr = new GCastExpr(tempInvokeExpr,RefType.v("java.lang.Boolean")); 524 526 SootMethod tempMethod = new SootMethod("booleanValue",new ArrayList(),BooleanType.v()); 527 tempMethod.setDeclaringClass(new SootClass("java.lang.Boolean")); 528 529 SootMethodRef tempMethodRef = tempMethod.makeRef(); 530 toAddExpr = 531 new DVirtualInvokeExpr(tempExpr,tempMethodRef,new ArrayList(),new HashSet()); 532 } 533 else if (t == ByteType.v()){ 534 Value tempExpr = new GCastExpr(tempInvokeExpr,RefType.v("java.lang.Byte")); 535 537 SootMethod tempMethod = new SootMethod("byteValue",new ArrayList(),ByteType.v()); 538 tempMethod.setDeclaringClass(new SootClass("java.lang.Byte")); 539 540 SootMethodRef tempMethodRef = tempMethod.makeRef(); 541 toAddExpr = 542 new DVirtualInvokeExpr(tempExpr,tempMethodRef,new ArrayList(),new HashSet()); 543 } 544 else if (t == CharType.v()){ 545 Value tempExpr = new GCastExpr(tempInvokeExpr,RefType.v("java.lang.Character")); 546 548 SootMethod tempMethod = new SootMethod("charValue",new ArrayList(),CharType.v()); 549 tempMethod.setDeclaringClass(new SootClass("java.lang.Character")); 550 551 SootMethodRef tempMethodRef = tempMethod.makeRef(); 552 toAddExpr = 553 new DVirtualInvokeExpr(tempExpr,tempMethodRef,new ArrayList(),new HashSet()); 554 } 555 else if (t == DoubleType.v()){ 556 Value tempExpr = toAddExpr = new GCastExpr(tempInvokeExpr,RefType.v("java.lang.Double")); 557 559 SootMethod tempMethod = new SootMethod("doubleValue",new ArrayList(),DoubleType.v()); 560 tempMethod.setDeclaringClass(new SootClass("java.lang.Double")); 561 562 SootMethodRef tempMethodRef = tempMethod.makeRef(); 563 toAddExpr = 564 new DVirtualInvokeExpr(tempExpr,tempMethodRef,new ArrayList(),new HashSet()); 565 } 566 else if (t == FloatType.v()){ 567 Value tempExpr = new GCastExpr(tempInvokeExpr,RefType.v("java.lang.Float")); 568 570 SootMethod tempMethod = new SootMethod("floatValue",new ArrayList(),FloatType.v()); 571 tempMethod.setDeclaringClass(new SootClass("java.lang.Float")); 572 573 SootMethodRef tempMethodRef = tempMethod.makeRef(); 574 toAddExpr = 575 new DVirtualInvokeExpr(tempExpr,tempMethodRef,new ArrayList(),new HashSet()); 576 } 577 else if (t == IntType.v()){ 578 Value tempExpr = new GCastExpr(tempInvokeExpr,RefType.v("java.lang.Integer")); 579 581 SootMethod tempMethod = new SootMethod("intValue",new ArrayList(),IntType.v()); 582 tempMethod.setDeclaringClass(new SootClass("java.lang.Integer")); 583 584 SootMethodRef tempMethodRef = tempMethod.makeRef(); 585 toAddExpr = 586 new DVirtualInvokeExpr(tempExpr,tempMethodRef,new ArrayList(),new HashSet()); 587 } 588 else if (t == LongType.v()){ 589 Value tempExpr = new GCastExpr(tempInvokeExpr,RefType.v("java.lang.Long")); 590 592 SootMethod tempMethod = new SootMethod("longValue",new ArrayList(),LongType.v()); 593 tempMethod.setDeclaringClass(new SootClass("java.lang.Long")); 594 595 SootMethodRef tempMethodRef = tempMethod.makeRef(); 596 toAddExpr = 597 new DVirtualInvokeExpr(tempExpr,tempMethodRef,new ArrayList(),new HashSet()); 598 } 599 else if (t == ShortType.v()){ 600 Value tempExpr = new GCastExpr(tempInvokeExpr,RefType.v("java.lang.Short")); 601 603 SootMethod tempMethod = new SootMethod("shortValue",new ArrayList(),ShortType.v()); 604 tempMethod.setDeclaringClass(new SootClass("java.lang.Short")); 605 606 SootMethodRef tempMethodRef = tempMethod.makeRef(); 607 toAddExpr = 608 new DVirtualInvokeExpr(tempExpr,tempMethodRef,new ArrayList(),new HashSet()); 609 } 610 else { 611 throw new DecompilationException("Unhandle primType:"+tempType); 612 } 613 } 614 else{ 615 throw new DecompilationException("The type:"+tempType+" was not a reftye or primtype. PLEASE REPORT."); 616 } 617 618 if(toAddExpr == null) 619 throw new DecompilationException("UNABLE TO CREATE TOADDEXPR:"+tempType); 620 621 622 argsForConstructor.add(toAddExpr); 624 } 625 626 628 DVirtualInvokeExpr virtualInvoke = new DVirtualInvokeExpr(originalConstructorExpr.getBase(),superConstructor.makeRef(), 629 argsForConstructor,new HashSet()); 630 631 newConstructorDavaBody.set_ConstructorExpr(virtualInvoke); 633 634 GInvokeStmt s = new GInvokeStmt(virtualInvoke); 636 637 newConstructorDavaBody.set_ConstructorUnit((Unit)s); 638 639 return true; 641 } 642 643 private void finalizeConstructor(){ 644 newASTConstructorMethod.setDavaBody(newConstructorDavaBody); 646 647 newConstructorDavaBody.getUnits().clear(); 648 newConstructorDavaBody.getUnits().addLast(newASTConstructorMethod); 649 650 System.out.println("Setting declaring class of method"+newConstructor.getSubSignature()); 651 newConstructor.setDeclaringClass(originalSootClass); 652 653 } 657 658 659 private boolean finalizePreInitMethod(){ 661 newASTPreInitMethod.setDavaBody(newPreInitDavaBody); 663 664 665 newPreInitDavaBody.getUnits().clear(); 667 newPreInitDavaBody.getUnits().addLast(newASTPreInitMethod); 668 669 670 673 674 675 676 677 678 679 680 681 List subBodies = (List)newASTPreInitMethod.get_SubBodies(); 685 if(subBodies.size()!=1) 686 return false; 687 688 List body = (List)subBodies.get(0); 689 690 693 Iterator it = body.iterator(); 694 boolean empty = true; 696 while(it.hasNext()){ 697 ASTNode tempNode = (ASTNode)it.next(); 698 if(!(tempNode instanceof ASTStatementSequenceNode)){ 699 empty = false; 701 break; 702 } 703 704 List stmts = ((ASTStatementSequenceNode)tempNode).getStatements(); 705 706 Iterator stmtIt = stmts.iterator(); 708 while(stmtIt.hasNext()){ 709 AugmentedStmt as = (AugmentedStmt)stmtIt.next(); 710 Stmt s = as.get_Stmt(); 711 if(!(s instanceof DVariableDeclarationStmt)){ 712 empty=false; 713 break; 714 } 715 } 716 if(!empty) 717 break; 718 } 719 720 if(empty){ 721 return false; } 724 725 createDavaStoreStmts(); 727 return true; 728 } 729 730 731 732 733 734 735 736 737 738 739 public void createNewASTConstructor(ASTStatementSequenceNode initNode){ 740 741 List newConstructorBody = new ArrayList(); 742 743 List newStmts = new ArrayList(); 745 746 Iterator it = initNode.getStatements().iterator(); 747 while(it.hasNext()){ 748 AugmentedStmt augStmt = (AugmentedStmt)it.next(); 749 Stmt stmtTemp = augStmt.get_Stmt(); 750 if(stmtTemp == originalConstructorUnit){ 751 break; 752 } 753 } 754 while(it.hasNext()){ 755 759 newStmts.add(it.next()); 760 } 761 if(newStmts.size()>0){ 762 newConstructorBody.add(new ASTStatementSequenceNode(newStmts)); 763 } 764 765 766 767 768 List originalASTMethodSubBodies = (List)originalASTMethod.get_SubBodies(); 770 if(originalASTMethodSubBodies.size() != 1) 771 throw new CorruptASTException("size of ASTMethodNode subBody not 1"); 772 773 List oldASTBody = (List)originalASTMethodSubBodies.get(0); 774 775 it = oldASTBody.iterator(); 776 boolean sanity=false; 777 while(it.hasNext()){ 778 ASTNode tempNode = (ASTNode)it.next(); 780 781 if(tempNode instanceof ASTStatementSequenceNode){ 783 if( (((ASTStatementSequenceNode)tempNode).getStatements()).equals(initNode.getStatements()) ){ 784 sanity = true; 785 break; 786 } 787 } 788 } 789 790 if(!sanity){ 791 throw new DecompilationException("never found the init node"); 793 } 794 795 while(it.hasNext()){ 798 newConstructorBody.add(it.next()); 799 } 800 801 802 803 804 805 806 807 810 List newConstructorDeclarations = new ArrayList(); 811 Iterator originalDeclarationsIterator = originalASTMethod.getDeclarations().getStatements().iterator(); 812 while(originalDeclarationsIterator.hasNext()){ 813 AugmentedStmt as = (AugmentedStmt)originalDeclarationsIterator.next(); 814 DVariableDeclarationStmt varDecStmt = (DVariableDeclarationStmt)as.get_Stmt(); 815 newConstructorDeclarations.add(new AugmentedStmt((DVariableDeclarationStmt)varDecStmt.clone())); 816 } 817 ASTStatementSequenceNode newDecs = new ASTStatementSequenceNode(new ArrayList()); 818 if(newConstructorDeclarations.size()>0){ 819 newDecs = new ASTStatementSequenceNode(newConstructorDeclarations); 820 823 newConstructorBody.add(0,newDecs); 825 } 826 827 828 830 831 newASTConstructorMethod = new ASTMethodNode(newConstructorBody); 833 834 newASTConstructorMethod.setDeclarations(newDecs); 836 } 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 private void createNewConstructor(){ 875 876 String uniqueName = "<init>"; 878 879 881 List args = new ArrayList(); 882 args.addAll(argsOneTypes); 883 884 RefType type = (new SootClass("DavaSuperHandler")).getType(); 886 args.add(type); 887 888 newConstructor = new SootMethod(uniqueName,args,IntType.v()); 890 891 newConstructor.setDeclaringClass(originalSootClass); 893 894 newConstructor.setModifiers(soot.Modifier.PUBLIC); 896 897 newConstructorDavaBody = Dava.v().newBody(newConstructor); 899 900 902 Map tempMap = new HashMap(); 904 Iterator typeIt = argsOneTypes.iterator(); 905 int count = 0; 906 while (typeIt.hasNext()) { 907 Type t = (Type) typeIt.next(); 908 909 tempMap.put(new Integer (count),originalPMap.get(new Integer (count))); 910 count++; 911 } 912 913 tempMap.put(new Integer (argsOneTypes.size()),"handler"); 915 916 newConstructorDavaBody.set_ParamMap(tempMap); 918 919 newConstructor.setActiveBody(newConstructorDavaBody); 921 } 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 948 private void createNewASTPreInitMethod(ASTStatementSequenceNode initNode){ 949 List newPreinitBody = new ArrayList(); 950 952 List originalASTMethodSubBodies = (List)originalASTMethod.get_SubBodies(); 953 if(originalASTMethodSubBodies.size() != 1) 954 throw new CorruptASTException("size of ASTMethodNode subBody not 1"); 955 956 List oldASTBody = (List)originalASTMethodSubBodies.get(0); 957 958 Iterator it = oldASTBody.iterator(); 959 boolean sanity=false; 960 while(it.hasNext()){ 961 ASTNode tempNode = (ASTNode)it.next(); 963 964 if(tempNode instanceof ASTStatementSequenceNode){ 966 if( (((ASTStatementSequenceNode)tempNode).getStatements()).equals(initNode.getStatements()) ){ 967 sanity = true; 968 break; 969 } 970 else{ 971 newPreinitBody.add(tempNode); 973 } 974 } 975 else{ 976 newPreinitBody.add(tempNode); 978 } 979 } 980 if(!sanity){ 981 throw new DecompilationException("never found the init node"); 983 } 984 985 986 List newStmts = new ArrayList(); 989 990 it = initNode.getStatements().iterator(); 991 while(it.hasNext()){ 992 AugmentedStmt augStmt = (AugmentedStmt)it.next(); 993 Stmt stmtTemp = augStmt.get_Stmt(); 994 if(stmtTemp == originalConstructorUnit){ 995 break; 996 } 997 1002 newStmts.add(augStmt); 1003 } 1004 if(newStmts.size()>0){ 1005 newPreinitBody.add(new ASTStatementSequenceNode(newStmts)); 1006 } 1007 1008 1009 1010 1011 List newPreinitDeclarations = new ArrayList(); 1014 Iterator originalDeclarationsIterator = originalASTMethod.getDeclarations().getStatements().iterator(); 1015 while(originalDeclarationsIterator.hasNext()){ 1016 AugmentedStmt as = (AugmentedStmt)originalDeclarationsIterator.next(); 1017 DVariableDeclarationStmt varDecStmt = (DVariableDeclarationStmt)as.get_Stmt(); 1018 newPreinitDeclarations.add(new AugmentedStmt((DVariableDeclarationStmt)varDecStmt.clone())); 1019 } 1020 ASTStatementSequenceNode newDecs = new ASTStatementSequenceNode(new ArrayList()); 1021 if(newPreinitDeclarations.size()>0){ 1022 newDecs = new ASTStatementSequenceNode(newPreinitDeclarations); 1023 1026 1029 newPreinitBody.remove(0); 1030 newPreinitBody.add(0,newDecs); 1031 } 1032 1033 1034 if(newPreinitBody.size()<1){ 1037 newASTPreInitMethod = null; return; 1040 } 1041 1042 1043 1045 1046 1048 1050 1053 1054 1055 newASTPreInitMethod = new ASTMethodNode(newPreinitBody); 1057 1058 newASTPreInitMethod.setDeclarations(newDecs); 1060 1061 } 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1095 private void createSootPreInitMethod(){ 1096 String uniqueName = getUniqueName(); 1098 1099 newSootPreInitMethod = new SootMethod(uniqueName,argsOneTypes,(new SootClass("DavaSuperHandler")).getType()); 1101 1102 1103 newSootPreInitMethod.setDeclaringClass(originalSootClass); 1105 1106 newSootPreInitMethod.setModifiers(soot.Modifier.PRIVATE | soot.Modifier.STATIC); 1108 1109 newPreInitDavaBody = Dava.v().newBody(newSootPreInitMethod); 1111 1112 newPreInitDavaBody.set_ParamMap(originalPMap); 1114 1115 newSootPreInitMethod.setActiveBody(newPreInitDavaBody); 1117 } 1118 1119 1120 1121 1122 1123 1128 private String getUniqueName(){ 1129 String toReturn = "preInit"; 1130 int counter=0; 1131 1132 List methodList = originalSootClass.getMethods(); 1133 1134 boolean done = false; while(!done){ done = true; Iterator it = methodList.iterator(); 1138 while(it.hasNext()){ 1139 Object temp = it.next(); 1140 if(temp instanceof SootMethod){ 1141 SootMethod method = (SootMethod)temp; 1142 String name = method.getName(); 1143 if(toReturn.compareTo(name)==0){ 1144 counter++; 1146 toReturn = "preInit"+counter; 1147 done = false; break; } 1150 } 1151 else 1152 throw new DecompilationException("SootClass returned a non SootMethod method"); 1153 } 1154 1155 it = G.v().SootMethodsAdded.iterator(); 1158 while(it.hasNext()){ 1159 SootMethod method = (SootMethod)it.next(); 1161 String name = method.getName(); 1162 if(toReturn.compareTo(name)==0){ 1163 counter++; 1165 toReturn = "preInit"+counter; 1166 done = false; break; } 1169 } 1170 1171 } return toReturn; 1173 } 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1207 1208 1209 private void createDavaStoreStmts(){ 1210 List davaHandlerStmts = new ArrayList(); 1211 1212 1213 SootClass sootClass = new SootClass("DavaSuperHandler"); 1215 1216 Type localType = sootClass.getType(); 1217 Local newLocal = new JimpleLocal("handler",localType); 1218 1219 1220 1221 1222 1225 DVariableDeclarationStmt varStmt = null; 1226 varStmt = new DVariableDeclarationStmt(localType,newPreInitDavaBody); 1227 varStmt.addLocal(newLocal); 1228 AugmentedStmt as = new AugmentedStmt(varStmt); 1229 davaHandlerStmts.add(as); 1230 1231 1232 1233 1234 1237 1238 DNewInvokeExpr invokeExpr = 1240 new DNewInvokeExpr(RefType.v(sootClass),makeMethodRef("DavaSuperHandler",new ArrayList()),new ArrayList()); 1241 1242 GAssignStmt initialization = new GAssignStmt(newLocal,invokeExpr); 1244 1245 davaHandlerStmts.add(new AugmentedStmt(initialization)); 1247 1248 1249 1250 1251 1252 1253 1254 1257 1258 1259 1260 1261 Iterator typeIt = argsTwoTypes.iterator(); 1263 Iterator valIt = argsTwoValues.iterator(); 1264 1265 1267 ArrayList tempList = new ArrayList(); 1268 tempList.add(RefType.v("java.lang.Object")); 1270 SootMethod method = new SootMethod("store",tempList,VoidType.v()); 1271 1272 method.setDeclaringClass(sootClass); 1274 SootMethodRef getMethodRef = method.makeRef(); 1275 1276 1277 1280 1281 while(typeIt.hasNext() && valIt.hasNext()){ 1282 Type tempType = (Type)typeIt.next(); 1283 Value tempVal = (Value)valIt.next(); 1284 1285 if(tempType instanceof RefType){ 1286 1289 davaHandlerStmts.add(createAugmentedStmtToAdd(newLocal,getMethodRef,tempVal)); 1290 } 1291 else if(tempType instanceof PrimType){ 1292 PrimType t = (PrimType)tempType; 1295 1296 ArrayList argList = new ArrayList(); 1298 argList.add(tempVal); 1299 1300 if (t == BooleanType.v()){ 1302 1303 ArrayList typeList = new ArrayList(); 1305 typeList.add(IntType.v()); 1306 1307 DNewInvokeExpr argForStore = 1308 new DNewInvokeExpr(RefType.v("java.lang.Boolean"), 1309 makeMethodRef("Boolean",typeList),argList); 1310 1311 davaHandlerStmts.add(createAugmentedStmtToAdd(newLocal,getMethodRef,argForStore)); 1312 } 1313 else if (t == ByteType.v()){ 1314 ArrayList typeList = new ArrayList(); 1316 typeList.add(ByteType.v()); 1317 1318 DNewInvokeExpr argForStore = 1319 new DNewInvokeExpr(RefType.v("java.lang.Byte"), 1320 makeMethodRef("Byte",typeList),argList); 1321 1322 davaHandlerStmts.add(createAugmentedStmtToAdd(newLocal,getMethodRef,argForStore)); 1323 1324 } 1325 else if (t == CharType.v()){ 1326 ArrayList typeList = new ArrayList(); 1328 typeList.add(CharType.v()); 1329 1330 DNewInvokeExpr argForStore = 1331 new DNewInvokeExpr(RefType.v("java.lang.Character"), 1332 makeMethodRef("Character",typeList),argList); 1333 1334 davaHandlerStmts.add(createAugmentedStmtToAdd(newLocal,getMethodRef,argForStore)); 1335 } 1336 else if (t == DoubleType.v()){ 1337 ArrayList typeList = new ArrayList(); 1339 typeList.add(DoubleType.v()); 1340 1341 DNewInvokeExpr argForStore = 1342 new DNewInvokeExpr(RefType.v("java.lang.Double"), 1343 makeMethodRef("Double",typeList),argList); 1344 1345 davaHandlerStmts.add(createAugmentedStmtToAdd(newLocal,getMethodRef,argForStore)); 1346 } 1347 else if (t == FloatType.v()){ 1348 ArrayList typeList = new ArrayList(); 1350 typeList.add(FloatType.v()); 1351 1352 DNewInvokeExpr argForStore = 1353 new DNewInvokeExpr(RefType.v("java.lang.Float"), 1354 makeMethodRef("Float",typeList),argList); 1355 1356 davaHandlerStmts.add(createAugmentedStmtToAdd(newLocal,getMethodRef,argForStore)); 1357 } 1358 else if (t == IntType.v()){ 1359 ArrayList typeList = new ArrayList(); 1361 typeList.add(IntType.v()); 1362 1363 DNewInvokeExpr argForStore = 1364 new DNewInvokeExpr(RefType.v("java.lang.Integer"), 1365 makeMethodRef("Integer",typeList),argList); 1366 1367 davaHandlerStmts.add(createAugmentedStmtToAdd(newLocal,getMethodRef,argForStore)); 1368 } 1369 else if (t == LongType.v()){ 1370 ArrayList typeList = new ArrayList(); 1372 typeList.add(LongType.v()); 1373 1374 DNewInvokeExpr argForStore = 1375 new DNewInvokeExpr(RefType.v("java.lang.Long"), 1376 makeMethodRef("Long",typeList),argList); 1377 1378 davaHandlerStmts.add(createAugmentedStmtToAdd(newLocal,getMethodRef,argForStore)); 1379 } 1380 else if (t == ShortType.v()){ 1381 ArrayList typeList = new ArrayList(); 1383 typeList.add(ShortType.v()); 1384 1385 DNewInvokeExpr argForStore = 1386 new DNewInvokeExpr(RefType.v("java.lang.Short"), 1387 makeMethodRef("Short",typeList),argList); 1388 1389 davaHandlerStmts.add(createAugmentedStmtToAdd(newLocal,getMethodRef,argForStore)); 1390 } 1391 else { 1392 throw new DecompilationException("UNHANDLED PRIMTYPE:"+tempType); 1393 } 1394 } else{ 1396 throw new DecompilationException("The type:"+tempType+" is neither a reftype or a primtype"); 1397 } 1398 } if(typeIt.hasNext() || valIt.hasNext()) 1401 throw new DecompilationException("Error creating DavaHandler stmts"); 1402 1403 1404 1407 1408 GReturnStmt returnStmt = new GReturnStmt(newLocal); 1409 davaHandlerStmts.add(new AugmentedStmt(returnStmt)); 1410 1411 1412 1414 ASTStatementSequenceNode addedNode = new ASTStatementSequenceNode(davaHandlerStmts); 1416 1417 1418 List subBodies = (List)newASTPreInitMethod.get_SubBodies(); 1420 if(subBodies.size()!=1) 1421 throw new CorruptASTException("ASTMethodNode does not have one subBody"); 1422 List body = (List)subBodies.get(0); 1423 body.add(addedNode); 1424 1425 newASTPreInitMethod.replaceBody(body); 1426 } 1427 1428 1429 1430 1431 1432 private AugmentedStmt createAugmentedStmtToAdd(Local newLocal,SootMethodRef getMethodRef, Value tempVal){ 1433 ArrayList tempArgList = new ArrayList(); 1434 tempArgList.add(tempVal); 1435 1436 DVirtualInvokeExpr tempInvokeExpr = 1437 new DVirtualInvokeExpr(newLocal,getMethodRef,tempArgList,new HashSet()); 1438 1439 GInvokeStmt s = new GInvokeStmt(tempInvokeExpr); 1441 1442 return new AugmentedStmt(s); 1443 } 1444 1445 public void debug(String methodName, String debug){ 1446 if(DEBUG) 1447 System.out.println(methodName+ " DEBUG: "+debug); 1448 } 1449 1450} | Popular Tags |