forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDavaBody.java
More file actions
1186 lines (990 loc) · 41.9 KB
/
DavaBody.java
File metadata and controls
1186 lines (990 loc) · 41.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package soot.dava;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 2003 Jerome Miecznikowski
* Copyright (C) 2004 - 2006 Nomair A. Naeem
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
import soot.Body;
import soot.G;
import soot.IntType;
import soot.Local;
import soot.PatchingChain;
import soot.PhaseOptions;
import soot.RefType;
import soot.SootFieldRef;
import soot.SootMethod;
import soot.SootMethodRef;
import soot.Trap;
import soot.Type;
import soot.Unit;
import soot.UnitBox;
import soot.Value;
import soot.ValueBox;
import soot.dava.internal.AST.ASTMethodNode;
import soot.dava.internal.AST.ASTNode;
import soot.dava.internal.SET.SETNode;
import soot.dava.internal.SET.SETTopNode;
import soot.dava.internal.asg.AugmentedStmt;
import soot.dava.internal.asg.AugmentedStmtGraph;
import soot.dava.internal.javaRep.DCmpExpr;
import soot.dava.internal.javaRep.DCmpgExpr;
import soot.dava.internal.javaRep.DCmplExpr;
import soot.dava.internal.javaRep.DInstanceFieldRef;
import soot.dava.internal.javaRep.DIntConstant;
import soot.dava.internal.javaRep.DInterfaceInvokeExpr;
import soot.dava.internal.javaRep.DLengthExpr;
import soot.dava.internal.javaRep.DNegExpr;
import soot.dava.internal.javaRep.DNewArrayExpr;
import soot.dava.internal.javaRep.DNewInvokeExpr;
import soot.dava.internal.javaRep.DNewMultiArrayExpr;
import soot.dava.internal.javaRep.DSpecialInvokeExpr;
import soot.dava.internal.javaRep.DStaticFieldRef;
import soot.dava.internal.javaRep.DStaticInvokeExpr;
import soot.dava.internal.javaRep.DThisRef;
import soot.dava.internal.javaRep.DVirtualInvokeExpr;
import soot.dava.toolkits.base.AST.UselessTryRemover;
import soot.dava.toolkits.base.AST.transformations.ASTCleaner;
import soot.dava.toolkits.base.AST.transformations.ASTCleanerTwo;
import soot.dava.toolkits.base.AST.transformations.AndAggregator;
import soot.dava.toolkits.base.AST.transformations.BooleanConditionSimplification;
import soot.dava.toolkits.base.AST.transformations.DeInliningFinalFields;
import soot.dava.toolkits.base.AST.transformations.DecrementIncrementStmtCreation;
import soot.dava.toolkits.base.AST.transformations.FinalFieldDefinition;
import soot.dava.toolkits.base.AST.transformations.ForLoopCreator;
import soot.dava.toolkits.base.AST.transformations.IfElseSplitter;
import soot.dava.toolkits.base.AST.transformations.LocalVariableCleaner;
import soot.dava.toolkits.base.AST.transformations.LoopStrengthener;
import soot.dava.toolkits.base.AST.transformations.NewStringBufferSimplification;
import soot.dava.toolkits.base.AST.transformations.OrAggregatorFour;
import soot.dava.toolkits.base.AST.transformations.OrAggregatorOne;
import soot.dava.toolkits.base.AST.transformations.OrAggregatorTwo;
import soot.dava.toolkits.base.AST.transformations.PushLabeledBlockIn;
import soot.dava.toolkits.base.AST.transformations.ShortcutArrayInit;
import soot.dava.toolkits.base.AST.transformations.ShortcutIfGenerator;
import soot.dava.toolkits.base.AST.transformations.SuperFirstStmtHandler;
import soot.dava.toolkits.base.AST.transformations.TypeCastingError;
import soot.dava.toolkits.base.AST.transformations.UselessAbruptStmtRemover;
import soot.dava.toolkits.base.AST.transformations.UselessLabeledBlockRemover;
import soot.dava.toolkits.base.AST.traversals.ClosestAbruptTargetFinder;
import soot.dava.toolkits.base.AST.traversals.CopyPropagation;
import soot.dava.toolkits.base.finders.AbruptEdgeFinder;
import soot.dava.toolkits.base.finders.CycleFinder;
import soot.dava.toolkits.base.finders.ExceptionFinder;
import soot.dava.toolkits.base.finders.ExceptionNode;
import soot.dava.toolkits.base.finders.IfFinder;
import soot.dava.toolkits.base.finders.LabeledBlockFinder;
import soot.dava.toolkits.base.finders.SequenceFinder;
import soot.dava.toolkits.base.finders.SwitchFinder;
import soot.dava.toolkits.base.finders.SynchronizedBlockFinder;
import soot.dava.toolkits.base.misc.MonitorConverter;
import soot.dava.toolkits.base.misc.ThrowNullConverter;
import soot.grimp.GrimpBody;
import soot.grimp.NewInvokeExpr;
import soot.jimple.ArrayRef;
import soot.jimple.BinopExpr;
import soot.jimple.CastExpr;
import soot.jimple.CaughtExceptionRef;
import soot.jimple.CmpExpr;
import soot.jimple.CmpgExpr;
import soot.jimple.CmplExpr;
import soot.jimple.ConditionExpr;
import soot.jimple.Constant;
import soot.jimple.DefinitionStmt;
import soot.jimple.Expr;
import soot.jimple.IdentityStmt;
import soot.jimple.IfStmt;
import soot.jimple.InstanceFieldRef;
import soot.jimple.InstanceInvokeExpr;
import soot.jimple.InstanceOfExpr;
import soot.jimple.IntConstant;
import soot.jimple.InterfaceInvokeExpr;
import soot.jimple.InvokeExpr;
import soot.jimple.InvokeStmt;
import soot.jimple.LengthExpr;
import soot.jimple.LookupSwitchStmt;
import soot.jimple.MonitorStmt;
import soot.jimple.NegExpr;
import soot.jimple.NewArrayExpr;
import soot.jimple.NewExpr;
import soot.jimple.NewMultiArrayExpr;
import soot.jimple.ParameterRef;
import soot.jimple.Ref;
import soot.jimple.ReturnStmt;
import soot.jimple.SpecialInvokeExpr;
import soot.jimple.StaticFieldRef;
import soot.jimple.StaticInvokeExpr;
import soot.jimple.Stmt;
import soot.jimple.TableSwitchStmt;
import soot.jimple.ThisRef;
import soot.jimple.ThrowStmt;
import soot.jimple.UnopExpr;
import soot.jimple.VirtualInvokeExpr;
import soot.jimple.internal.JGotoStmt;
import soot.jimple.internal.JimpleLocal;
import soot.toolkits.graph.BriefUnitGraph;
import soot.toolkits.graph.TrapUnitGraph;
import soot.util.IterableSet;
import soot.util.Switchable;
/*
* CHANGE LOG: Nomair - January 2006: Moved the AST Analyses to a separate method
* These are now invoked as a very last staged (just before generating decompiled
* output. Invoked by PackManager
*
* Nomair - 7th Feb, 2006: Starting work on a naming mechanism
* Nomair - 13th Feb 2006: Added db phase options
*
* renamer: on /off DEFAULT:TRUE
* deobfuscate: DEFAULT: FALSE, dead code eliminateion,
* class/field renaming, constant field elimination
* force-recompilability: DEFAULT TRUE, super, final
*
* Nomair: March 28th, 2006: Removed the applyRenamerAnalyses method from DavaBody to InterProceduralAnalyses
* Although currently renaming is done intra-proceduraly there is strong indication that
* inter procedural analyses will be required to get good names
*
* Nomair: March 29th, 2006: dealing with trying to remove fully qualified names
*
*/
/*
* TODO: Nomair- February 7th. Refactor the call
* AST.perform_Analysis( UselessTryRemover.v());
* use the new AnalysisAdapter routines to write this analysis. Then delete these
* obselete and rather clumsy way of writing analyses
*
* TODO: Nomair 14th Feb 2006, Use the Dava options renamer, deobfuscate, force-recompilability
* Specially the deobfuscate option with the boolean constant propagation analysis
*
*/
public class DavaBody extends Body {
public boolean DEBUG = false;
private Map<Integer, Value> pMap;
private Set<Object> consumedConditions;
private HashSet<Object> thisLocals;
private IterableSet<ExceptionNode> synchronizedBlockFacts;
private IterableSet<ExceptionNode> exceptionFacts;
private IterableSet<AugmentedStmt> monitorFacts;
private IterableSet<String> importList;
private Local controlLocal;
private InstanceInvokeExpr constructorExpr; // holds constructorUnit.getInvokeExpr
private Unit constructorUnit; // holds a stmt (this.init<>)
private List<CaughtExceptionRef> caughtrefs;
/**
* Construct an empty DavaBody
*/
DavaBody(SootMethod m) {
super(m);
this.pMap = new HashMap<Integer, Value>();
this.consumedConditions = new HashSet<Object>();
this.thisLocals = new HashSet<Object>();
this.synchronizedBlockFacts = new IterableSet<ExceptionNode>();
this.exceptionFacts = new IterableSet<ExceptionNode>();
this.monitorFacts = new IterableSet<AugmentedStmt>();
this.importList = new IterableSet<String>();
this.caughtrefs = new LinkedList<CaughtExceptionRef>();
this.controlLocal = null;
this.constructorExpr = null;
}
public Unit get_ConstructorUnit() {
return constructorUnit;
}
public List<CaughtExceptionRef> get_CaughtRefs() {
return caughtrefs;
}
public InstanceInvokeExpr get_ConstructorExpr() {
return constructorExpr;
}
public void set_ConstructorExpr(InstanceInvokeExpr expr) {
constructorExpr = expr;
}
public void set_ConstructorUnit(Unit s) {
constructorUnit = s;
}
public Map<Integer, Value> get_ParamMap() {
return pMap;
}
public void set_ParamMap(Map<Integer, Value> map) {
pMap = map;
}
public HashSet<Object> get_ThisLocals() {
return thisLocals;
}
public Local get_ControlLocal() {
if (controlLocal == null) {
controlLocal = new JimpleLocal("controlLocal", IntType.v());
getLocals().add(controlLocal);
}
return controlLocal;
}
public Set<Object> get_ConsumedConditions() {
return consumedConditions;
}
public void consume_Condition(AugmentedStmt as) {
consumedConditions.add(as);
}
public Object clone() {
Body b = Dava.v().newBody(getMethodUnsafe());
b.importBodyContentsFrom(this);
return b;
}
@Override
public Object clone(boolean noLocalsClone) {
// not implemented
return null;
}
public IterableSet<ExceptionNode> get_SynchronizedBlockFacts() {
return synchronizedBlockFacts;
}
public IterableSet<ExceptionNode> get_ExceptionFacts() {
return exceptionFacts;
}
public IterableSet<AugmentedStmt> get_MonitorFacts() {
return monitorFacts;
}
public IterableSet<String> getImportList() {
return importList;
}
/**
* Constructs a DavaBody from the given Body.
*/
DavaBody(Body body) {
this(body.getMethod());
debug("DavaBody", "creating DavaBody for" + body.getMethod().toString());
Dava.v().log("\nstart method " + body.getMethod().toString());
if (DEBUG) {
if (!body.getMethod().getExceptions().isEmpty()) {
debug("DavaBody", "printing NON EMPTY exception list for " + body.getMethod().toString() + " "
+ body.getMethod().getExceptions().toString());
}
}
// copy and "convert" the grimp representation
// DEBUG=true;
copy_Body(body);
// DEBUG=false;
// prime the analysis
AugmentedStmtGraph asg = new AugmentedStmtGraph(new BriefUnitGraph(this), new TrapUnitGraph(this));
// System.out.println(asg.toString());
ExceptionFinder.v().preprocess(this, asg);
SETNode SET = new SETTopNode(asg.get_ChainView());
while (true) {
try {
CycleFinder.v().find(this, asg, SET);
IfFinder.v().find(this, asg, SET);
SwitchFinder.v().find(this, asg, SET);
SynchronizedBlockFinder.v().find(this, asg, SET);
ExceptionFinder.v().find(this, asg, SET);
SequenceFinder.v().find(this, asg, SET);
LabeledBlockFinder.v().find(this, asg, SET);
AbruptEdgeFinder.v().find(this, asg, SET);
} catch (RetriggerAnalysisException rae) {
SET = new SETTopNode(asg.get_ChainView());
consumedConditions = new HashSet<Object>();
continue;
}
break;
}
MonitorConverter.v().convert(this);
ThrowNullConverter.v().convert(this);
ASTNode AST = SET.emit_AST();
// get rid of the grimp representation, put in the new AST
getTraps().clear();
getUnits().clear();
getUnits().addLast(AST);
// perform transformations on the AST
/*
* Nomair This should be refactored to use the new AnalysisAdapter classes
*/
do {
G.v().ASTAnalysis_modified = false;
AST.perform_Analysis(UselessTryRemover.v());
} while (G.v().ASTAnalysis_modified);
/*
* Nomair A Naeem 10-MARCH-2005
*
* IT IS ESSENTIAL TO CALL THIS METHOD This method initializes the locals of the current method being processed Failure
* to invoke this method here will result in no locals being printed out
*/
if (AST instanceof ASTMethodNode) {
((ASTMethodNode) AST).storeLocals(this);
/*
* January 12th, 2006 Deal with the super() problem before continuing
*/
Map<String, String> options = PhaseOptions.v().getPhaseOptions("db.force-recompile");
boolean force = PhaseOptions.getBoolean(options, "enabled");
// System.out.println("force is "+force);
if (force) {
AST.apply(new SuperFirstStmtHandler((ASTMethodNode) AST));
}
debug("DavaBody", "PreInit booleans is" + G.v().SootMethodAddedByDava);
}
Dava.v().log("end method " + body.getMethod().toString());
}
public void applyBugFixes() {
ASTNode AST = (ASTNode) this.getUnits().getFirst();
debug("applyBugFixes", "Applying AST analyzes for method" + this.getMethod().toString());
AST.apply(new ShortcutIfGenerator());
debug("applyBugFixes", "after ShortcutIfGenerator" + G.v().ASTTransformations_modified);
AST.apply(new TypeCastingError());
debug("applyBugFixes", "after TypeCastingError" + G.v().ASTTransformations_modified);
}
/*
* Method is invoked by the packmanager just before it is actually about to generate decompiled code. Works as a separate
* stage from the DavaBody() constructor. All AST transformations should be implemented from within this method.
*
* Method is also invoked from the InterProceduralAnlaysis method once those have been invoked
*/
public void analyzeAST() {
ASTNode AST = (ASTNode) this.getUnits().getFirst();
debug("analyzeAST", "Applying AST analyzes for method" + this.getMethod().toString());
/*
* Nomair A. Naeem tranformations on the AST Any AST Transformations added should be added to the applyASTAnalyses method
* unless we are want to delay the analysis till for example THE LAST THING DONE
*/
applyASTAnalyses(AST);
/*
* Nomair A. Naeem apply structural flow analyses now
*
*/
debug("analyzeAST", "Applying structure analysis" + this.getMethod().toString());
applyStructuralAnalyses(AST);
debug("analyzeAST", "Applying structure analysis DONE" + this.getMethod().toString());
/*
* Renamer March 28th Nomair A. Naeem. Since there is a chance that the analyze method gets involved multiple times we
* dont want renaming done more than once.
*
* hence removing the call of the renamer from here Also looking ahead i have a feeling that we will be going
* interprocedural for the renamer hence i am placing the renamer code inside the interprocedural class
*/
/*
* In the end check 1, if there are labels which can be safely removed 2, int temp; temp=0 to be converted to int temp=0;
*/
// AST.apply(new ExtraLabelNamesRemover());
// System.out.println("\nEND analyzing method"+this.getMethod().toString());
}
private void applyASTAnalyses(ASTNode AST) {
debug("applyASTAnalyses", "initial one time analyses started");
/*
* Nomair A. Naeem Transformations on the AST
*/
// The BooleanConditionSimplification changes flag==false to just flag
// AST.apply(new DepthFirstAdapter(true));
AST.apply(new BooleanConditionSimplification());
AST.apply(new DecrementIncrementStmtCreation());
debug("applyASTAnalyses", "initial one time analyses completed");
boolean flag = true;
G.v().ASTTransformations_modified = false;
G.v().ASTIfElseFlipped = false;
int countFlipping = 0;
if (flag) {
// perform transformations on the AST
do {
debug("applyASTAnalyses", "ITERATION");
G.v().ASTTransformations_modified = false;
AST.apply(new AndAggregator());
debug("applyASTAnalyses", "after AndAggregator" + G.v().ASTTransformations_modified);
/*
* The OrAggregatorOne internally calls UselessLabelFinder which sets the label to null Always apply a
* UselessLabeledBlockRemover in the end to remove such labeled blocks
*/
AST.apply(new OrAggregatorOne());
debug("applyASTAnalyses", "after OraggregatorOne" + G.v().ASTTransformations_modified);
/*
* Note OrAggregatorTwo should always be followed by an emptyElseRemover since orAggregatorTwo can create empty else
* bodies and the ASTIfElseNode can be replaced by ASTIfNodes OrAggregator has two patterns see the class for them
*/
AST.apply(new OrAggregatorTwo());
debug("applyASTAnalyses", "after OraggregatorTwo" + G.v().ASTTransformations_modified);
debug("applyASTAnalyses", "after OraggregatorTwo ifElseFlipped is" + G.v().ASTIfElseFlipped);
AST.apply(new OrAggregatorFour());
debug("applyASTAnalyses", "after OraggregatorFour" + G.v().ASTTransformations_modified);
/*
* ASTCleaner currently does the following tasks: 1, Remove empty Labeled Blocks UselessLabeledBlockRemover 2,
* convert ASTIfElseNodes with empty else bodies to ASTIfNodes 3, Apply OrAggregatorThree
*/
AST.apply(new ASTCleaner());
debug("applyASTAnalyses", "after ASTCleaner" + G.v().ASTTransformations_modified);
/*
* PushLabeledBlockIn should not be called unless we are sure that all labeledblocks have non null labels. A good way
* of ensuring this is to run the ASTCleaner directly before calling this
*/
AST.apply(new PushLabeledBlockIn());
debug("applyASTAnalyses", "after PushLabeledBlockIn" + G.v().ASTTransformations_modified);
AST.apply(new LoopStrengthener());
debug("applyASTAnalyses", "after LoopStrengthener" + G.v().ASTTransformations_modified);
/*
* Pattern two carried out in OrAggregatorTwo restricts some patterns in for loop creation. Pattern two was
* implemented to give loopStrengthening a better chance SEE IfElseBreaker
*/
AST.apply(new ASTCleanerTwo());
debug("applyASTAnalyses", "after ASTCleanerTwo" + G.v().ASTTransformations_modified);
AST.apply(new ForLoopCreator());
debug("applyASTAnalyses", "after ForLoopCreator" + G.v().ASTTransformations_modified);
AST.apply(new NewStringBufferSimplification());
debug("applyASTAnalyses", "after NewStringBufferSimplification" + G.v().ASTTransformations_modified);
AST.apply(new ShortcutArrayInit());
debug("applyASTAnalyses", "after ShortcutArrayInit" + G.v().ASTTransformations_modified);
AST.apply(new UselessLabeledBlockRemover());
debug("applyASTAnalyses", "after UselessLabeledBlockRemover" + G.v().ASTTransformations_modified);
if (!G.v().ASTTransformations_modified) {
AST.apply(new IfElseSplitter());
debug("applyASTAnalyses", "after IfElseSplitter" + G.v().ASTTransformations_modified);
}
if (!G.v().ASTTransformations_modified) {
AST.apply(new UselessAbruptStmtRemover());
debug("applyASTAnalyses", "after UselessAbruptStmtRemover" + G.v().ASTTransformations_modified);
}
AST.apply(new ShortcutIfGenerator());
debug("applyASTAnalyses", "after ShortcutIfGenerator" + G.v().ASTTransformations_modified);
AST.apply(new TypeCastingError());
debug("applyASTAnalyses", "after TypeCastingError" + G.v().ASTTransformations_modified);
/*
* if we matched some useful pattern we reserve the right to flip conditions again
*/
if (G.v().ASTTransformations_modified) {
G.v().ASTIfElseFlipped = false;
countFlipping = 0;
debug("applyASTanalyses", "Transformation modified was true hence will reiterate. set flipped to false");
} else {
// check if only the ifelse was flipped
if (G.v().ASTIfElseFlipped) {
debug("", "ifelseflipped and transformations NOT modified");
// we couldnt transform but we did flip
if (countFlipping == 0) {
debug("", "ifelseflipped and transformations NOT modified count is 0");
// let this go on just once more in the hope of some other pattern being matched
G.v().ASTIfElseFlipped = false;
countFlipping++;
G.v().ASTTransformations_modified = true;
} else {
debug("", "ifelseflipped and transformations NOT modified count is not 0 TERMINATE");
}
}
} // if ASTTransformations was not modified
} while (G.v().ASTTransformations_modified);
// System.out.println("The AST trasnformations has run"+times);
}
/*
* ClosestAbruptTargetFinder should be reinitialized everytime there is a change to the AST This is utilized internally
* by the DavaFlowSet implementation to handle Abrupt Implicit Stmts
*/
AST.apply(ClosestAbruptTargetFinder.v());
debug("applyASTAnalyses", "after ClosestAbruptTargetFinder" + G.v().ASTTransformations_modified);
// 29th Jan 2006
// make sure when recompiling there is no variable might not be initialized error
Map<String, String> options = PhaseOptions.v().getPhaseOptions("db.force-recompile");
boolean force = PhaseOptions.getBoolean(options, "enabled");
// System.out.println("Force is"+force);
if (force) {
debug("applyASTAnalyses", "before FinalFieldDefinition" + G.v().ASTTransformations_modified);
new FinalFieldDefinition((ASTMethodNode) AST);
debug("applyASTAnalyses", "after FinalFieldDefinition" + G.v().ASTTransformations_modified);
}
// this analysis has to be after ShortcutArrayInit to give that analysis more chances
AST.apply(new DeInliningFinalFields());
debug("applyASTAnalyses", "end applyASTAnlayses" + G.v().ASTTransformations_modified);
}
private void applyStructuralAnalyses(ASTNode AST) {
// TESTING REACHING DEFS
// ReachingDefs defs = new ReachingDefs(AST);
// AST.apply(new tester(true,defs));
// TESTING REACHING COPIES
// ReachingCopies copies = new ReachingCopies(AST);
// AST.apply(new tester(true,copies));
// TESTING ASTUSESANDDEFS
// AST.apply(new ASTUsesAndDefs(AST));
/*
* Structural flow analyses.....
*/
// CopyPropagation.DEBUG=true;
debug("applyStructureAnalyses", "invoking copy propagation");
CopyPropagation prop = new CopyPropagation(AST);
AST.apply(prop);
debug("applyStructureAnalyses", "invoking copy propagation DONE");
// copy propagation should be followed by LocalVariableCleaner to get max effect
// ASTUsesAndDefs.DEBUG=true;
debug("applyStructureAnalyses", "Local Variable Cleaner started");
AST.apply(new LocalVariableCleaner(AST));
debug("applyStructureAnalyses", "Local Variable Cleaner DONE");
}
/*
* Copy and patch a GrimpBody so that it can be used to output Java.
*/
private void copy_Body(Body body) {
if (!(body instanceof GrimpBody)) {
throw new RuntimeException("You can only create a DavaBody from a GrimpBody!");
}
GrimpBody grimpBody = (GrimpBody) body;
/*
* Import body contents from Grimp.
*/
{
HashMap<Switchable, Switchable> bindings = new HashMap<Switchable, Switchable>();
HashMap<Unit, Unit> reverse_binding = new HashMap<Unit, Unit>();
// Clone units in body's statement list
for (Unit original : grimpBody.getUnits()) {
Unit copy = (Unit) original.clone();
// Add cloned unit to our unitChain.
getUnits().addLast(copy);
// Build old <-> new map to be able to patch up references to other units
// within the cloned units. (these are still refering to the original
// unit objects).
bindings.put(original, copy);
reverse_binding.put(copy, original);
}
// patch up the switch statments
for (Unit u : getUnits()) {
Stmt s = (Stmt) u;
if (s instanceof TableSwitchStmt) {
TableSwitchStmt ts = (TableSwitchStmt) s;
TableSwitchStmt original_switch = (TableSwitchStmt) reverse_binding.get(u);
ts.setDefaultTarget((Unit) bindings.get(original_switch.getDefaultTarget()));
LinkedList<Unit> new_target_list = new LinkedList<Unit>();
int target_count = ts.getHighIndex() - ts.getLowIndex() + 1;
for (int i = 0; i < target_count; i++) {
new_target_list.add((Unit) bindings.get(original_switch.getTarget(i)));
}
ts.setTargets(new_target_list);
} else if (s instanceof LookupSwitchStmt) {
LookupSwitchStmt ls = (LookupSwitchStmt) s;
LookupSwitchStmt original_switch = (LookupSwitchStmt) reverse_binding.get(u);
ls.setDefaultTarget((Unit) bindings.get(original_switch.getDefaultTarget()));
Unit[] new_target_list = new Unit[original_switch.getTargetCount()];
for (int i = 0; i < original_switch.getTargetCount(); i++) {
new_target_list[i] = (Unit) bindings.get(original_switch.getTarget(i));
}
ls.setTargets(new_target_list);
ls.setLookupValues(original_switch.getLookupValues());
}
}
// Clone locals.
for (Local original : grimpBody.getLocals()) {
Local copy = Dava.v().newLocal(original.getName(), original.getType());
getLocals().add(copy);
// Build old <-> new mapping.
bindings.put(original, copy);
}
// Patch up references within units using our (old <-> new) map.
for (UnitBox box : getAllUnitBoxes()) {
Unit newObject = (Unit) bindings.get(box.getUnit());
// if we have a reference to an old object, replace it with its clone.
if (newObject != null) {
box.setUnit(newObject);
}
}
// backpatch all local variables.
for (ValueBox vb : getUseAndDefBoxes()) {
Value val = vb.getValue();
if (val instanceof Local) {
vb.setValue((Value) bindings.get(val));
}
}
// clone the traps
for (Trap originalTrap : grimpBody.getTraps()) {
Trap cloneTrap = (Trap) originalTrap.clone();
cloneTrap.setHandlerUnit((Unit) bindings.get(originalTrap.getHandlerUnit()));
cloneTrap.setBeginUnit((Unit) bindings.get(originalTrap.getBeginUnit()));
cloneTrap.setEndUnit((Unit) bindings.get(originalTrap.getEndUnit()));
getTraps().add(cloneTrap);
}
}
/*
* Add one level of indirection to "if", "switch", and exceptional control flow. This allows for easy handling of breaks,
* continues and exceptional loops.
*/
{
PatchingChain<Unit> units = getUnits();
for (Iterator<Unit> it = units.snapshotIterator(); it.hasNext();) {
Unit u = it.next();
if (u instanceof IfStmt) {
IfStmt ifs = (IfStmt) u;
JGotoStmt jgs = new JGotoStmt(units.getSuccOf(u));
units.insertAfter(jgs, u);
JGotoStmt jumper = new JGotoStmt(ifs.getTarget());
units.insertAfter(jumper, jgs);
ifs.setTarget(jumper);
} else if (u instanceof TableSwitchStmt) {
TableSwitchStmt tss = (TableSwitchStmt) u;
int targetCount = tss.getHighIndex() - tss.getLowIndex() + 1;
for (int i = 0; i < targetCount; i++) {
JGotoStmt jgs = new JGotoStmt(tss.getTarget(i));
units.insertAfter(jgs, tss);
tss.setTarget(i, jgs);
}
JGotoStmt jgs = new JGotoStmt(tss.getDefaultTarget());
units.insertAfter(jgs, tss);
tss.setDefaultTarget(jgs);
} else if (u instanceof LookupSwitchStmt) {
LookupSwitchStmt lss = (LookupSwitchStmt) u;
for (int i = 0; i < lss.getTargetCount(); i++) {
JGotoStmt jgs = new JGotoStmt(lss.getTarget(i));
units.insertAfter(jgs, lss);
lss.setTarget(i, jgs);
}
JGotoStmt jgs = new JGotoStmt(lss.getDefaultTarget());
units.insertAfter(jgs, lss);
lss.setDefaultTarget(jgs);
}
}
for (Trap t : getTraps()) {
JGotoStmt jgs = new JGotoStmt(t.getHandlerUnit());
units.addLast(jgs);
t.setHandlerUnit(jgs);
}
}
/*
* Fix up the grimp representations of statements so they can be compiled as java.
*/
{
for (Local l : getLocals()) {
Type t = l.getType();
if (t instanceof RefType) {
RefType rt = (RefType) t;
String className = rt.getSootClass().toString();
String packageName = rt.getSootClass().getJavaPackageName();
String classPackageName = packageName;
if (className.lastIndexOf('.') > 0) {
// 0 doesnt make sense
classPackageName = className.substring(0, className.lastIndexOf('.'));
}
if (!packageName.equals(classPackageName)) {
throw new DecompilationException("Unable to retrieve package name for identifier. Please report to developer.");
}
addToImportList(className);
// addPackage(rt.getSootClass().getJavaPackageName());
}
}
for (Unit u : getUnits()) {
if (u instanceof IfStmt) {
javafy(((IfStmt) u).getConditionBox());
} else if (u instanceof ThrowStmt) {
javafy(((ThrowStmt) u).getOpBox());
} else if (u instanceof TableSwitchStmt) {
javafy(((TableSwitchStmt) u).getKeyBox());
} else if (u instanceof LookupSwitchStmt) {
javafy(((LookupSwitchStmt) u).getKeyBox());
} else if (u instanceof MonitorStmt) {
javafy(((MonitorStmt) u).getOpBox());
} else if (u instanceof DefinitionStmt) {
DefinitionStmt ds = (DefinitionStmt) u;
javafy(ds.getRightOpBox());
javafy(ds.getLeftOpBox());
Value rightOp = ds.getRightOp();
if (rightOp instanceof IntConstant) {
ds.getRightOpBox().setValue(DIntConstant.v(((IntConstant) rightOp).value, ds.getLeftOp().getType()));
}
} else if (u instanceof ReturnStmt) {
ReturnStmt rs = (ReturnStmt) u;
Value op = rs.getOp();
if (op instanceof IntConstant) {
rs.getOpBox().setValue(DIntConstant.v(((IntConstant) op).value, body.getMethod().getReturnType()));
} else {
javafy(rs.getOpBox());
}
} else if (u instanceof InvokeStmt) {
javafy(((InvokeStmt) u).getInvokeExprBox());
}
}
}
/*
* Convert references to "this" and parameters.
*/
for (Unit u : getUnits()) {
if (u instanceof IdentityStmt) {
IdentityStmt ids = (IdentityStmt) u;
Value ids_rightOp = ids.getRightOp();
Value ids_leftOp = ids.getLeftOp();
if ((ids_leftOp instanceof Local) && (ids_rightOp instanceof ThisRef)) {
Local thisLocal = (Local) ids_leftOp;
thisLocals.add(thisLocal);
thisLocal.setName("this");
}
}
if (u instanceof DefinitionStmt) {
DefinitionStmt ds = (DefinitionStmt) u;
Value rightOp = ds.getRightOp();
if (rightOp instanceof ParameterRef) {
pMap.put(((ParameterRef) rightOp).getIndex(), ds.getLeftOp());
}
if (rightOp instanceof CaughtExceptionRef) {
caughtrefs.add((CaughtExceptionRef) rightOp);
}
}
}
/*
* Fix up the calls to other constructors. Note, this is seriously underbuilt.
*/
for (Unit u : getUnits()) {
if (u instanceof InvokeStmt) {
InvokeStmt ivs = (InvokeStmt) u;
Value ie = ivs.getInvokeExpr();
if (ie instanceof InstanceInvokeExpr) {
InstanceInvokeExpr iie = (InstanceInvokeExpr) ie;
Value base = iie.getBase();
if ((base instanceof Local) && "this".equals(((Local) base).getName())) {
String name = iie.getMethodRef().name();
if (SootMethod.constructorName.equals(name) || SootMethod.staticInitializerName.equals(name)) {
if (constructorUnit != null) {
throw new RuntimeException("More than one candidate for constructor found.");
}
constructorExpr = iie;
constructorUnit = u;
}
}
}
}
}
}
/*
* The following set of routines takes care of converting the syntax of single grimp statements to java.
*/
private void javafy(ValueBox vb) {
Value v = vb.getValue();
if (v instanceof Expr) {
javafy_expr(vb);
} else if (v instanceof Ref) {
javafy_ref(vb);
} else if (v instanceof Local) {
javafy_local(vb);
} else if (v instanceof Constant) {
javafy_constant(vb);
}
}
private void javafy_expr(ValueBox vb) {
Expr e = (Expr) vb.getValue();
if (e instanceof BinopExpr) {
javafy_binop_expr(vb);
} else if (e instanceof UnopExpr) {
javafy_unop_expr(vb);
} else if (e instanceof CastExpr) {
javafy_cast_expr(vb);
} else if (e instanceof NewArrayExpr) {
javafy_newarray_expr(vb);
} else if (e instanceof NewMultiArrayExpr) {
javafy_newmultiarray_expr(vb);
} else if (e instanceof InstanceOfExpr) {
javafy_instanceof_expr(vb);
} else if (e instanceof InvokeExpr) {
javafy_invoke_expr(vb);
} else if (e instanceof NewExpr) {
javafy_new_expr(vb);
}
}
private void javafy_ref(ValueBox vb) {
Ref r = (Ref) vb.getValue();
if (r instanceof StaticFieldRef) {
SootFieldRef fieldRef = ((StaticFieldRef) r).getFieldRef();
// addPackage(fieldRef.declaringClass().getJavaPackageName());
String className = fieldRef.declaringClass().toString();
String packageName = fieldRef.declaringClass().getJavaPackageName();
String classPackageName = packageName;
if (className.lastIndexOf('.') > 0) {
// 0 doesnt make sense
classPackageName = className.substring(0, className.lastIndexOf('.'));
}
if (!packageName.equals(classPackageName)) {
throw new DecompilationException("Unable to retrieve package name for identifier. Please report to developer.");
}
addToImportList(className);
vb.setValue(new DStaticFieldRef(fieldRef, getMethod().getDeclaringClass().getName()));
} else if (r instanceof ArrayRef) {
ArrayRef ar = (ArrayRef) r;
javafy(ar.getBaseBox());
javafy(ar.getIndexBox());
} else if (r instanceof InstanceFieldRef) {
InstanceFieldRef ifr = (InstanceFieldRef) r;
javafy(ifr.getBaseBox());
vb.setValue(new DInstanceFieldRef(ifr.getBase(), ifr.getFieldRef(), thisLocals));
} else if (r instanceof ThisRef) {
ThisRef tr = (ThisRef) r;
vb.setValue(new DThisRef((RefType) tr.getType()));
}
}
private void javafy_local(ValueBox vb) {
}
private void javafy_constant(ValueBox vb) {
}
private void javafy_binop_expr(ValueBox vb) {
BinopExpr boe = (BinopExpr) vb.getValue();
ValueBox leftOpBox = boe.getOp1Box(), rightOpBox = boe.getOp2Box();
Value leftOp = leftOpBox.getValue(), rightOp = rightOpBox.getValue();
if (rightOp instanceof IntConstant) {
if (!(leftOp instanceof IntConstant)) {
javafy(leftOpBox);
leftOp = leftOpBox.getValue();