forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAsmMethodSource.java
More file actions
2464 lines (2332 loc) · 83.8 KB
/
AsmMethodSource.java
File metadata and controls
2464 lines (2332 loc) · 83.8 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.asm;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 1997 - 2014 Raja Vallee-Rai and others
* %%
* 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 static org.objectweb.asm.Opcodes.ACONST_NULL;
import static org.objectweb.asm.Opcodes.ALOAD;
import static org.objectweb.asm.Opcodes.ANEWARRAY;
import static org.objectweb.asm.Opcodes.ARETURN;
import static org.objectweb.asm.Opcodes.ARRAYLENGTH;
import static org.objectweb.asm.Opcodes.ASTORE;
import static org.objectweb.asm.Opcodes.ATHROW;
import static org.objectweb.asm.Opcodes.BIPUSH;
import static org.objectweb.asm.Opcodes.CHECKCAST;
import static org.objectweb.asm.Opcodes.D2F;
import static org.objectweb.asm.Opcodes.D2I;
import static org.objectweb.asm.Opcodes.D2L;
import static org.objectweb.asm.Opcodes.DADD;
import static org.objectweb.asm.Opcodes.DALOAD;
import static org.objectweb.asm.Opcodes.DASTORE;
import static org.objectweb.asm.Opcodes.DCMPG;
import static org.objectweb.asm.Opcodes.DCMPL;
import static org.objectweb.asm.Opcodes.DCONST_0;
import static org.objectweb.asm.Opcodes.DCONST_1;
import static org.objectweb.asm.Opcodes.DDIV;
import static org.objectweb.asm.Opcodes.DLOAD;
import static org.objectweb.asm.Opcodes.DMUL;
import static org.objectweb.asm.Opcodes.DNEG;
import static org.objectweb.asm.Opcodes.DREM;
import static org.objectweb.asm.Opcodes.DRETURN;
import static org.objectweb.asm.Opcodes.DSTORE;
import static org.objectweb.asm.Opcodes.DSUB;
import static org.objectweb.asm.Opcodes.DUP;
import static org.objectweb.asm.Opcodes.DUP2;
import static org.objectweb.asm.Opcodes.DUP2_X1;
import static org.objectweb.asm.Opcodes.DUP2_X2;
import static org.objectweb.asm.Opcodes.DUP_X1;
import static org.objectweb.asm.Opcodes.DUP_X2;
import static org.objectweb.asm.Opcodes.F2D;
import static org.objectweb.asm.Opcodes.F2I;
import static org.objectweb.asm.Opcodes.F2L;
import static org.objectweb.asm.Opcodes.FCMPG;
import static org.objectweb.asm.Opcodes.FCMPL;
import static org.objectweb.asm.Opcodes.FCONST_0;
import static org.objectweb.asm.Opcodes.FCONST_2;
import static org.objectweb.asm.Opcodes.GETFIELD;
import static org.objectweb.asm.Opcodes.GETSTATIC;
import static org.objectweb.asm.Opcodes.GOTO;
import static org.objectweb.asm.Opcodes.I2B;
import static org.objectweb.asm.Opcodes.I2C;
import static org.objectweb.asm.Opcodes.I2D;
import static org.objectweb.asm.Opcodes.I2F;
import static org.objectweb.asm.Opcodes.I2L;
import static org.objectweb.asm.Opcodes.I2S;
import static org.objectweb.asm.Opcodes.IADD;
import static org.objectweb.asm.Opcodes.IALOAD;
import static org.objectweb.asm.Opcodes.IAND;
import static org.objectweb.asm.Opcodes.IASTORE;
import static org.objectweb.asm.Opcodes.ICONST_0;
import static org.objectweb.asm.Opcodes.ICONST_5;
import static org.objectweb.asm.Opcodes.ICONST_M1;
import static org.objectweb.asm.Opcodes.IDIV;
import static org.objectweb.asm.Opcodes.IFEQ;
import static org.objectweb.asm.Opcodes.IFGE;
import static org.objectweb.asm.Opcodes.IFGT;
import static org.objectweb.asm.Opcodes.IFLE;
import static org.objectweb.asm.Opcodes.IFLT;
import static org.objectweb.asm.Opcodes.IFNE;
import static org.objectweb.asm.Opcodes.IFNONNULL;
import static org.objectweb.asm.Opcodes.IFNULL;
import static org.objectweb.asm.Opcodes.IF_ACMPEQ;
import static org.objectweb.asm.Opcodes.IF_ACMPNE;
import static org.objectweb.asm.Opcodes.IF_ICMPEQ;
import static org.objectweb.asm.Opcodes.IF_ICMPGE;
import static org.objectweb.asm.Opcodes.IF_ICMPGT;
import static org.objectweb.asm.Opcodes.IF_ICMPLE;
import static org.objectweb.asm.Opcodes.IF_ICMPLT;
import static org.objectweb.asm.Opcodes.IF_ICMPNE;
import static org.objectweb.asm.Opcodes.ILOAD;
import static org.objectweb.asm.Opcodes.IMUL;
import static org.objectweb.asm.Opcodes.INEG;
import static org.objectweb.asm.Opcodes.INSTANCEOF;
import static org.objectweb.asm.Opcodes.INVOKEINTERFACE;
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
import static org.objectweb.asm.Opcodes.IOR;
import static org.objectweb.asm.Opcodes.IREM;
import static org.objectweb.asm.Opcodes.IRETURN;
import static org.objectweb.asm.Opcodes.ISHL;
import static org.objectweb.asm.Opcodes.ISHR;
import static org.objectweb.asm.Opcodes.ISTORE;
import static org.objectweb.asm.Opcodes.ISUB;
import static org.objectweb.asm.Opcodes.IUSHR;
import static org.objectweb.asm.Opcodes.IXOR;
import static org.objectweb.asm.Opcodes.JSR;
import static org.objectweb.asm.Opcodes.L2D;
import static org.objectweb.asm.Opcodes.L2F;
import static org.objectweb.asm.Opcodes.L2I;
import static org.objectweb.asm.Opcodes.LADD;
import static org.objectweb.asm.Opcodes.LALOAD;
import static org.objectweb.asm.Opcodes.LAND;
import static org.objectweb.asm.Opcodes.LASTORE;
import static org.objectweb.asm.Opcodes.LCMP;
import static org.objectweb.asm.Opcodes.LCONST_0;
import static org.objectweb.asm.Opcodes.LCONST_1;
import static org.objectweb.asm.Opcodes.LDIV;
import static org.objectweb.asm.Opcodes.LLOAD;
import static org.objectweb.asm.Opcodes.LMUL;
import static org.objectweb.asm.Opcodes.LNEG;
import static org.objectweb.asm.Opcodes.LOR;
import static org.objectweb.asm.Opcodes.LREM;
import static org.objectweb.asm.Opcodes.LRETURN;
import static org.objectweb.asm.Opcodes.LSHL;
import static org.objectweb.asm.Opcodes.LSHR;
import static org.objectweb.asm.Opcodes.LSTORE;
import static org.objectweb.asm.Opcodes.LSUB;
import static org.objectweb.asm.Opcodes.LUSHR;
import static org.objectweb.asm.Opcodes.LXOR;
import static org.objectweb.asm.Opcodes.MONITORENTER;
import static org.objectweb.asm.Opcodes.MONITOREXIT;
import static org.objectweb.asm.Opcodes.NEW;
import static org.objectweb.asm.Opcodes.NEWARRAY;
import static org.objectweb.asm.Opcodes.NOP;
import static org.objectweb.asm.Opcodes.POP;
import static org.objectweb.asm.Opcodes.POP2;
import static org.objectweb.asm.Opcodes.PUTFIELD;
import static org.objectweb.asm.Opcodes.RET;
import static org.objectweb.asm.Opcodes.RETURN;
import static org.objectweb.asm.Opcodes.SALOAD;
import static org.objectweb.asm.Opcodes.SASTORE;
import static org.objectweb.asm.Opcodes.SIPUSH;
import static org.objectweb.asm.Opcodes.SWAP;
import static org.objectweb.asm.Opcodes.T_BOOLEAN;
import static org.objectweb.asm.Opcodes.T_BYTE;
import static org.objectweb.asm.Opcodes.T_CHAR;
import static org.objectweb.asm.Opcodes.T_DOUBLE;
import static org.objectweb.asm.Opcodes.T_FLOAT;
import static org.objectweb.asm.Opcodes.T_INT;
import static org.objectweb.asm.Opcodes.T_LONG;
import static org.objectweb.asm.Opcodes.T_SHORT;
import static org.objectweb.asm.tree.AbstractInsnNode.FIELD_INSN;
import static org.objectweb.asm.tree.AbstractInsnNode.FRAME;
import static org.objectweb.asm.tree.AbstractInsnNode.IINC_INSN;
import static org.objectweb.asm.tree.AbstractInsnNode.INSN;
import static org.objectweb.asm.tree.AbstractInsnNode.INT_INSN;
import static org.objectweb.asm.tree.AbstractInsnNode.INVOKE_DYNAMIC_INSN;
import static org.objectweb.asm.tree.AbstractInsnNode.JUMP_INSN;
import static org.objectweb.asm.tree.AbstractInsnNode.LABEL;
import static org.objectweb.asm.tree.AbstractInsnNode.LDC_INSN;
import static org.objectweb.asm.tree.AbstractInsnNode.LINE;
import static org.objectweb.asm.tree.AbstractInsnNode.LOOKUPSWITCH_INSN;
import static org.objectweb.asm.tree.AbstractInsnNode.METHOD_INSN;
import static org.objectweb.asm.tree.AbstractInsnNode.MULTIANEWARRAY_INSN;
import static org.objectweb.asm.tree.AbstractInsnNode.TABLESWITCH_INSN;
import static org.objectweb.asm.tree.AbstractInsnNode.TYPE_INSN;
import static org.objectweb.asm.tree.AbstractInsnNode.VAR_INSN;
import com.google.common.base.Optional;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Table;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import org.objectweb.asm.Handle;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.FieldInsnNode;
import org.objectweb.asm.tree.IincInsnNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.IntInsnNode;
import org.objectweb.asm.tree.InvokeDynamicInsnNode;
import org.objectweb.asm.tree.JumpInsnNode;
import org.objectweb.asm.tree.LabelNode;
import org.objectweb.asm.tree.LdcInsnNode;
import org.objectweb.asm.tree.LineNumberNode;
import org.objectweb.asm.tree.LocalVariableNode;
import org.objectweb.asm.tree.LookupSwitchInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MultiANewArrayInsnNode;
import org.objectweb.asm.tree.TableSwitchInsnNode;
import org.objectweb.asm.tree.TryCatchBlockNode;
import org.objectweb.asm.tree.TypeInsnNode;
import org.objectweb.asm.tree.VarInsnNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import soot.ArrayType;
import soot.Body;
import soot.BooleanType;
import soot.ByteType;
import soot.CharType;
import soot.DoubleType;
import soot.FloatType;
import soot.IntType;
import soot.LambdaMetaFactory;
import soot.Local;
import soot.LongType;
import soot.MethodSource;
import soot.Modifier;
import soot.ModuleScene;
import soot.ModuleUtil;
import soot.PackManager;
import soot.PhaseOptions;
import soot.RefType;
import soot.Scene;
import soot.ShortType;
import soot.SootClass;
import soot.SootFieldRef;
import soot.SootMethod;
import soot.SootMethodRef;
import soot.Trap;
import soot.Type;
import soot.Unit;
import soot.UnitBox;
import soot.UnitPatchingChain;
import soot.UnknownType;
import soot.Value;
import soot.ValueBox;
import soot.VoidType;
import soot.coffi.Util;
import soot.jimple.AddExpr;
import soot.jimple.ArrayRef;
import soot.jimple.AssignStmt;
import soot.jimple.BinopExpr;
import soot.jimple.CastExpr;
import soot.jimple.CaughtExceptionRef;
import soot.jimple.ClassConstant;
import soot.jimple.ConditionExpr;
import soot.jimple.Constant;
import soot.jimple.DefinitionStmt;
import soot.jimple.DoubleConstant;
import soot.jimple.FieldRef;
import soot.jimple.FloatConstant;
import soot.jimple.GotoStmt;
import soot.jimple.IdentityStmt;
import soot.jimple.InstanceFieldRef;
import soot.jimple.InstanceInvokeExpr;
import soot.jimple.InstanceOfExpr;
import soot.jimple.IntConstant;
import soot.jimple.InvokeExpr;
import soot.jimple.Jimple;
import soot.jimple.JimpleBody;
import soot.jimple.LongConstant;
import soot.jimple.LookupSwitchStmt;
import soot.jimple.MethodHandle;
import soot.jimple.MethodType;
import soot.jimple.MonitorStmt;
import soot.jimple.NewArrayExpr;
import soot.jimple.NewMultiArrayExpr;
import soot.jimple.NopStmt;
import soot.jimple.NullConstant;
import soot.jimple.ReturnStmt;
import soot.jimple.StringConstant;
import soot.jimple.TableSwitchStmt;
import soot.jimple.ThrowStmt;
import soot.jimple.UnopExpr;
import soot.options.Options;
import soot.tagkit.LineNumberTag;
import soot.tagkit.Tag;
import soot.util.Chain;
/**
* Generates Jimple bodies from bytecode.
*
* @author Aaloan Miftah
*/
public class AsmMethodSource implements MethodSource {
private static final Logger logger = LoggerFactory.getLogger(AsmMethodSource.class);
private static final Operand DWORD_DUMMY = new Operand(null, null);
private static final String METAFACTORY_SIGNATURE = "<java.lang.invoke.LambdaMetafactory: java.lang.invoke.CallSite "
+ "metafactory(java.lang.invoke.MethodHandles$Lookup,java.lang.String,java.lang.invoke.MethodType,"
+ "java.lang.invoke.MethodType,java.lang.invoke.MethodHandle,java.lang.invoke.MethodType)>";
private static final String ALT_METAFACTORY_SIGNATURE = "<java.lang.invoke.LambdaMetafactory: java.lang.invoke.CallSite "
+ "altMetafactory(java.lang.invoke.MethodHandles$Lookup,"
+ "java.lang.String,java.lang.invoke.MethodType,java.lang.Object[])>";
/* -const fields- */
private final String module;
private final int maxLocals;
private final InsnList instructions;
private final List<LocalVariableNode> localVars;
private final List<TryCatchBlockNode> tryCatchBlocks;
private final Set<LabelNode> inlineExceptionLabels = new LinkedHashSet<LabelNode>();
private final Map<LabelNode, Unit> inlineExceptionHandlers = new LinkedHashMap<LabelNode, Unit>();
private final CastAndReturnInliner castAndReturnInliner = new CastAndReturnInliner();
/* -state fields- */
protected int nextLocal;
protected Map<Integer, Local> locals;
private Multimap<LabelNode, UnitBox> labels;
private Map<AbstractInsnNode, Unit> units;
private ArrayList<Operand> stack;
private Map<AbstractInsnNode, StackFrame> frames;
private Multimap<LabelNode, UnitBox> trapHandlers;
private JimpleBody body;
private int lastLineNumber = -1;
private Table<AbstractInsnNode, AbstractInsnNode, Edge> edges;
private ArrayDeque<Edge> conversionWorklist;
public AsmMethodSource(int maxLocals, InsnList insns, List<LocalVariableNode> localVars,
List<TryCatchBlockNode> tryCatchBlocks, String module) {
this.maxLocals = maxLocals;
this.instructions = insns;
this.localVars = localVars;
this.tryCatchBlocks = tryCatchBlocks;
this.module = module;
}
private StackFrame getFrame(AbstractInsnNode insn) {
StackFrame frame = frames.get(insn);
if (frame == null) {
frame = new StackFrame(this);
frames.put(insn, frame);
}
return frame;
}
private SootClass getClassFromScene(String className) {
SootClass result;
if (ModuleUtil.module_mode()) {
result = ModuleScene.v().getSootClassUnsafe(className, Optional.fromNullable(this.module));
} else {
result = Scene.v().getSootClassUnsafe(className);
}
if (result == null) {
String msg = String.format("%s was not found on classpath.", className);
if (Options.v().allow_phantom_refs()) {
RefType ref = RefType.v(className);
// make sure nobody else creates the same class
synchronized (ref) {
logger.warn(msg);
result = Scene.v().makeSootClass(className, Modifier.PUBLIC);
Scene.v().addClass(result);
result.setPhantomClass();
return ref.getSootClass();
}
} else {
throw new RuntimeException(msg);
}
}
return result;
}
private Local getLocal(int idx) {
if (idx >= maxLocals) {
throw new IllegalArgumentException("Invalid local index: " + idx);
}
Integer i = idx;
Local l = locals.get(i);
if (l == null) {
String name = getLocalName(idx);
l = Jimple.v().newLocal(name, UnknownType.v());
locals.put(i, l);
}
return l;
}
protected String getLocalName(int idx) {
String name;
if (localVars != null) {
name = null;
for (LocalVariableNode lvn : localVars) {
// Ignore LocalVariableNode which don't cover any real units
if (lvn.index == idx && lvn.start != lvn.end) {
name = lvn.name;
break;
}
}
/* normally for try-catch blocks */
if (name == null) {
name = "l" + idx;
}
} else {
name = "l" + idx;
}
return name;
}
private void push(Operand opr) {
stack.add(opr);
}
private void pushDual(Operand opr) {
stack.add(DWORD_DUMMY);
stack.add(opr);
}
private Operand peek() {
return stack.get(stack.size() - 1);
}
private void push(Type t, Operand opr) {
if (AsmUtil.isDWord(t)) {
pushDual(opr);
} else {
push(opr);
}
}
private Operand pop() {
if (stack.isEmpty()) {
throw new RuntimeException("Stack underrun");
}
return stack.remove(stack.size() - 1);
}
private Operand popDual() {
Operand o = pop();
Operand o2 = pop();
if (o2 != DWORD_DUMMY && o2 != o) {
throw new AssertionError("Not dummy operand, " + o2.value + " -- " + o.value);
}
return o;
}
private Operand pop(Type t) {
return AsmUtil.isDWord(t) ? popDual() : pop();
}
private Operand popLocal(Operand o) {
Value v = o.value;
Local l = o.stack;
if (l == null && !(v instanceof Local)) {
l = o.stack = newStackLocal();
setUnit(o.insn, Jimple.v().newAssignStmt(l, v));
o.updateBoxes();
}
return o;
}
private Operand popImmediate(Operand o) {
Value v = o.value;
Local l = o.stack;
if (l == null && !(v instanceof Local) && !(v instanceof Constant)) {
l = o.stack = newStackLocal();
setUnit(o.insn, Jimple.v().newAssignStmt(l, v));
o.updateBoxes();
}
return o;
}
private Operand popStackConst(Operand o) {
Value v = o.value;
Local l = o.stack;
if (l == null && !(v instanceof Constant)) {
l = o.stack = newStackLocal();
setUnit(o.insn, Jimple.v().newAssignStmt(l, v));
o.updateBoxes();
}
return o;
}
private Operand popLocal() {
return popLocal(pop());
}
private Operand popLocalDual() {
return popLocal(popDual());
}
@SuppressWarnings("unused")
private Operand popLocal(Type t) {
return AsmUtil.isDWord(t) ? popLocalDual() : popLocal();
}
private Operand popImmediate() {
return popImmediate(pop());
}
private Operand popImmediateDual() {
return popImmediate(popDual());
}
private Operand popImmediate(Type t) {
return AsmUtil.isDWord(t) ? popImmediateDual() : popImmediate();
}
private Operand popStackConst() {
return popStackConst(pop());
}
private Operand popStackConstDual() {
return popStackConst(popDual());
}
@SuppressWarnings("unused")
private Operand popStackConst(Type t) {
return AsmUtil.isDWord(t) ? popStackConstDual() : popStackConst();
}
void setUnit(AbstractInsnNode insn, Unit u) {
if (Options.v().keep_line_number() && lastLineNumber >= 0) {
Tag lineTag = u.getTag(LineNumberTag.NAME);
if (lineTag == null) {
lineTag = new LineNumberTag(lastLineNumber);
u.addTag(lineTag);
} else if (((LineNumberTag) lineTag).getLineNumber() != lastLineNumber) {
throw new RuntimeException("Line tag mismatch");
}
}
Unit o = units.put(insn, u);
if (o != null) {
throw new AssertionError(insn.getOpcode() + " already has a unit, " + o);
}
}
void mergeUnits(AbstractInsnNode insn, Unit u) {
Unit prev = units.put(insn, u);
if (prev != null) {
Unit merged = new UnitContainer(prev, u);
units.put(insn, merged);
}
}
protected Local newStackLocal() {
Integer idx = nextLocal++;
Local l = Jimple.v().newLocal("$stack" + idx, UnknownType.v());
locals.put(idx, l);
return l;
}
@SuppressWarnings("unchecked")
<A extends Unit> A getUnit(AbstractInsnNode insn) {
return (A) units.get(insn);
}
private void assignReadOps(Local l) {
if (stack.isEmpty()) {
return;
}
for (Operand opr : stack) {
if (opr == DWORD_DUMMY || opr.stack != null || (l == null && opr.value instanceof Local)) {
continue;
}
if (l != null && !opr.value.equivTo(l)) {
List<ValueBox> uses = opr.value.getUseBoxes();
boolean noref = true;
for (ValueBox use : uses) {
Value val = use.getValue();
if (val.equivTo(l)) {
noref = false;
break;
}
}
if (noref) {
continue;
}
}
int op = opr.insn.getOpcode();
if (l == null && op != GETFIELD && op != GETSTATIC && (op < IALOAD && op > SALOAD)) {
continue;
}
Local stack = newStackLocal();
opr.stack = stack;
AssignStmt as = Jimple.v().newAssignStmt(stack, opr.value);
opr.updateBoxes();
setUnit(opr.insn, as);
}
}
private void convertGetFieldInsn(FieldInsnNode insn) {
StackFrame frame = getFrame(insn);
Operand[] out = frame.out();
Operand opr;
Type type;
if (out == null) {
SootClass declClass = this.getClassFromScene(AsmUtil.toQualifiedName(insn.owner));
type = AsmUtil.toJimpleType(insn.desc, Optional.fromNullable(this.body.getMethod().getDeclaringClass().moduleName));
Value val;
SootFieldRef ref;
if (insn.getOpcode() == GETSTATIC) {
ref = Scene.v().makeFieldRef(declClass, insn.name, type, true);
val = Jimple.v().newStaticFieldRef(ref);
} else {
Operand base = popLocal();
ref = Scene.v().makeFieldRef(declClass, insn.name, type, false);
InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(base.stackOrValue(), ref);
val = ifr;
base.addBox(ifr.getBaseBox());
frame.in(base);
frame.boxes(ifr.getBaseBox());
}
opr = new Operand(insn, val);
frame.out(opr);
} else {
opr = out[0];
type = opr.<FieldRef>value().getFieldRef().type();
if (insn.getOpcode() == GETFIELD) {
frame.mergeIn(pop());
}
}
push(type, opr);
}
private void convertPutFieldInsn(FieldInsnNode insn) {
boolean instance = insn.getOpcode() == PUTFIELD;
StackFrame frame = getFrame(insn);
Operand[] out = frame.out();
Operand opr, rvalue;
Type type;
if (out == null) {
SootClass declClass = this.getClassFromScene(AsmUtil.toQualifiedName(insn.owner));
type = AsmUtil.toJimpleType(insn.desc, Optional.fromNullable(this.body.getMethod().getDeclaringClass().moduleName));
Value val;
SootFieldRef ref;
rvalue = popImmediate(type);
if (!instance) {
ref = Scene.v().makeFieldRef(declClass, insn.name, type, true);
val = Jimple.v().newStaticFieldRef(ref);
frame.in(rvalue);
} else {
Operand base = popLocal();
ref = Scene.v().makeFieldRef(declClass, insn.name, type, false);
InstanceFieldRef ifr = Jimple.v().newInstanceFieldRef(base.stackOrValue(), ref);
val = ifr;
base.addBox(ifr.getBaseBox());
frame.in(rvalue, base);
}
opr = new Operand(insn, val);
frame.out(opr);
AssignStmt as = Jimple.v().newAssignStmt(val, rvalue.stackOrValue());
rvalue.addBox(as.getRightOpBox());
if (!instance) {
frame.boxes(as.getRightOpBox());
} else {
frame.boxes(as.getRightOpBox(), ((InstanceFieldRef) val).getBaseBox());
}
setUnit(insn, as);
} else {
opr = out[0];
type = opr.<FieldRef>value().getFieldRef().type();
rvalue = pop(type);
if (!instance) {
/* PUTSTATIC only needs one operand on the stack, the rvalue */
frame.mergeIn(rvalue);
} else {
/* PUTFIELD has a rvalue and a base */
frame.mergeIn(rvalue, pop());
}
}
/*
* in case any static field or array is read from, and the static constructor or the field this instruction writes to,
* modifies that field, write out any previous read from field/array
*/
assignReadOps(null);
}
private void convertFieldInsn(FieldInsnNode insn) {
int op = insn.getOpcode();
if (op == GETSTATIC || op == GETFIELD) {
convertGetFieldInsn(insn);
} else {
convertPutFieldInsn(insn);
}
}
private void convertIincInsn(IincInsnNode insn) {
Local local = getLocal(insn.var);
assignReadOps(local);
if (!units.containsKey(insn)) {
AddExpr add = Jimple.v().newAddExpr(local, IntConstant.v(insn.incr));
setUnit(insn, Jimple.v().newAssignStmt(local, add));
}
}
private void convertConstInsn(InsnNode insn) {
int op = insn.getOpcode();
StackFrame frame = getFrame(insn);
Operand[] out = frame.out();
Operand opr;
if (out == null) {
Value v;
if (op == ACONST_NULL) {
v = NullConstant.v();
} else if (op >= ICONST_M1 && op <= ICONST_5) {
v = IntConstant.v(op - ICONST_0);
} else if (op == LCONST_0 || op == LCONST_1) {
v = LongConstant.v(op - LCONST_0);
} else if (op >= FCONST_0 && op <= FCONST_2) {
v = FloatConstant.v(op - FCONST_0);
} else if (op == DCONST_0 || op == DCONST_1) {
v = DoubleConstant.v(op - DCONST_0);
} else {
throw new AssertionError("Unknown constant opcode: " + op);
}
opr = new Operand(insn, v);
frame.out(opr);
} else {
opr = out[0];
}
if (op == LCONST_0 || op == LCONST_1 || op == DCONST_0 || op == DCONST_1) {
pushDual(opr);
} else {
push(opr);
}
}
/*
* Following version is more complex, using stack frames as opposed to simply swapping
*/
/*
* StackFrame frame = getFrame(insn); Operand[] out = frame.out(); Operand dup, dup2 = null, dupd, dupd2 = null; if (out ==
* null) { dupd = popImmediate(); dup = new Operand(insn, dupd.stackOrValue()); if (dword) { dupd2 = peek(); if (dupd2 ==
* DWORD_DUMMY) { pop(); dupd2 = dupd; } else { dupd2 = popImmediate(); } dup2 = new Operand(insn, dupd2.stackOrValue());
* frame.out(dup, dup2); frame.in(dupd, dupd2); } else { frame.out(dup); frame.in(dupd); } } else { dupd = pop(); dup =
* out[0]; if (dword) { dupd2 = pop(); if (dupd2 == DWORD_DUMMY) dupd2 = dupd; dup2 = out[1]; frame.mergeIn(dupd, dupd2); }
* else { frame.mergeIn(dupd); } }
*/
private void convertArrayLoadInsn(InsnNode insn) {
StackFrame frame = getFrame(insn);
Operand[] out = frame.out();
Operand opr;
if (out == null) {
Operand indx = popImmediate();
Operand base = popImmediate();
// We have a sample of totally broken code with a reference to a null array
// x = null[i]
// We silently fix this issue and return a null value
if (base.value == NullConstant.v()) {
opr = new Operand(insn, NullConstant.v());
frame.in(indx, base);
frame.out(opr);
} else {
ArrayRef ar = Jimple.v().newArrayRef(base.stackOrValue(), indx.stackOrValue());
indx.addBox(ar.getIndexBox());
base.addBox(ar.getBaseBox());
opr = new Operand(insn, ar);
frame.in(indx, base);
frame.boxes(ar.getIndexBox(), ar.getBaseBox());
frame.out(opr);
}
} else {
opr = out[0];
frame.mergeIn(pop(), pop());
}
int op = insn.getOpcode();
if (op == DALOAD || op == LALOAD) {
pushDual(opr);
} else {
push(opr);
}
}
private void convertArrayStoreInsn(InsnNode insn) {
int op = insn.getOpcode();
boolean dword = op == LASTORE || op == DASTORE;
StackFrame frame = getFrame(insn);
if (!units.containsKey(insn)) {
Operand valu = dword ? popImmediateDual() : popImmediate();
Operand indx = popImmediate();
Operand base = popLocal();
ArrayRef ar = Jimple.v().newArrayRef(base.stackOrValue(), indx.stackOrValue());
indx.addBox(ar.getIndexBox());
base.addBox(ar.getBaseBox());
AssignStmt as = Jimple.v().newAssignStmt(ar, valu.stackOrValue());
valu.addBox(as.getRightOpBox());
frame.in(valu, indx, base);
frame.boxes(as.getRightOpBox(), ar.getIndexBox(), ar.getBaseBox());
setUnit(insn, as);
} else {
frame.mergeIn(dword ? popDual() : pop(), pop(), pop());
}
}
private void convertDupInsn(InsnNode insn) {
int op = insn.getOpcode();
// Get the top stack value which we need in either case
Operand dupd = popImmediate();
Operand dupd2 = null;
// Some instructions allow operands that take two registers
boolean dword = op == DUP2 || op == DUP2_X1 || op == DUP2_X2;
if (dword) {
if (peek() == DWORD_DUMMY) {
pop();
dupd2 = dupd;
} else {
dupd2 = popImmediate();
}
}
switch (op) {
case DUP: {
// val -> val, val
push(dupd);
push(dupd);
break;
}
case DUP_X1: {
// val2, val1 -> val1, val2, val1
// value1, value2 must not be of type double or long
Operand o2 = popImmediate();
push(dupd);
push(o2);
push(dupd);
break;
}
case DUP_X2: {
// value3, value2, value1 -> value1, value3, value2, value1
Operand o2 = popImmediate();
Operand o3 = peek() == DWORD_DUMMY ? pop() : popImmediate();
push(dupd);
push(o3);
push(o2);
push(dupd);
break;
}
case DUP2: {
// value2, value1 -> value2, value1, value2, value1
push(dupd2);
push(dupd);
push(dupd2);
push(dupd);
break;
}
case DUP2_X1: {
// value3, value2, value1 -> value2, value1, value3, value2, value1
// Attention: value2 may be
Operand o2 = popImmediate();
push(dupd2);
push(dupd);
push(o2);
push(dupd2);
push(dupd);
break;
}
case DUP2_X2: {
// (value4, value3), (value2, value1) -> (value2, value1), (value4, value3), (value2, value1)
Operand o2 = popImmediate();
Operand o2h = peek() == DWORD_DUMMY ? pop() : popImmediate();
push(dupd2);
push(dupd);
push(o2h);
push(o2);
push(dupd2);
push(dupd);
break;
}
default:
break;
}
}
private void convertBinopInsn(InsnNode insn) {
int op = insn.getOpcode();
boolean dword = op == DADD || op == LADD || op == DSUB || op == LSUB || op == DMUL || op == LMUL || op == DDIV
|| op == LDIV || op == DREM || op == LREM || op == LSHL || op == LSHR || op == LUSHR || op == LAND || op == LOR
|| op == LXOR || op == LCMP || op == DCMPL || op == DCMPG;
StackFrame frame = getFrame(insn);
Operand[] out = frame.out();
Operand opr;
if (out == null) {
Operand op2 = (dword && op != LSHL && op != LSHR && op != LUSHR) ? popImmediateDual() : popImmediate();
Operand op1 = dword ? popImmediateDual() : popImmediate();
Value v1 = op1.stackOrValue();
Value v2 = op2.stackOrValue();
BinopExpr binop;
if (op >= IADD && op <= DADD) {
binop = Jimple.v().newAddExpr(v1, v2);
} else if (op >= ISUB && op <= DSUB) {
binop = Jimple.v().newSubExpr(v1, v2);
} else if (op >= IMUL && op <= DMUL) {
binop = Jimple.v().newMulExpr(v1, v2);
} else if (op >= IDIV && op <= DDIV) {
binop = Jimple.v().newDivExpr(v1, v2);
} else if (op >= IREM && op <= DREM) {
binop = Jimple.v().newRemExpr(v1, v2);
} else if (op >= ISHL && op <= LSHL) {
binop = Jimple.v().newShlExpr(v1, v2);
} else if (op >= ISHR && op <= LSHR) {
binop = Jimple.v().newShrExpr(v1, v2);
} else if (op >= IUSHR && op <= LUSHR) {
binop = Jimple.v().newUshrExpr(v1, v2);
} else if (op >= IAND && op <= LAND) {
binop = Jimple.v().newAndExpr(v1, v2);
} else if (op >= IOR && op <= LOR) {
binop = Jimple.v().newOrExpr(v1, v2);
} else if (op >= IXOR && op <= LXOR) {
binop = Jimple.v().newXorExpr(v1, v2);
} else if (op == LCMP) {
binop = Jimple.v().newCmpExpr(v1, v2);
} else if (op == FCMPL || op == DCMPL) {
binop = Jimple.v().newCmplExpr(v1, v2);
} else if (op == FCMPG || op == DCMPG) {
binop = Jimple.v().newCmpgExpr(v1, v2);
} else {
throw new AssertionError("Unknown binop: " + op);
}
op1.addBox(binop.getOp1Box());
op2.addBox(binop.getOp2Box());
opr = new Operand(insn, binop);
frame.in(op2, op1);
frame.boxes(binop.getOp2Box(), binop.getOp1Box());
frame.out(opr);
} else {
opr = out[0];
if (dword) {
if (op != LSHL && op != LSHR && op != LUSHR) {
frame.mergeIn(popDual(), popDual());
} else {
frame.mergeIn(pop(), popDual());
}
} else {
frame.mergeIn(pop(), pop());
}
}
if (dword && (op < LCMP || op > DCMPG)) {
pushDual(opr);
} else {
push(opr);
}
}
private void convertUnopInsn(InsnNode insn) {
int op = insn.getOpcode();
boolean dword = op == LNEG || op == DNEG;
StackFrame frame = getFrame(insn);
Operand[] out = frame.out();
Operand opr;
if (out == null) {
Operand op1 = dword ? popImmediateDual() : popImmediate();
Value v1 = op1.stackOrValue();
UnopExpr unop;
if (op >= INEG && op <= DNEG) {
unop = Jimple.v().newNegExpr(v1);
} else if (op == ARRAYLENGTH) {
unop = Jimple.v().newLengthExpr(v1);
} else {
throw new AssertionError("Unknown unop: " + op);
}
op1.addBox(unop.getOpBox());
opr = new Operand(insn, unop);
frame.in(op1);
frame.boxes(unop.getOpBox());
frame.out(opr);
} else {
opr = out[0];
frame.mergeIn(dword ? popDual() : pop());
}
if (dword) {
pushDual(opr);
} else {
push(opr);
}
}
private void convertPrimCastInsn(InsnNode insn) {
int op = insn.getOpcode();
boolean tod = op == I2L || op == I2D || op == F2L || op == F2D || op == D2L || op == L2D;
boolean fromd = op == D2L || op == L2D || op == D2I || op == L2I || op == D2F || op == L2F;
StackFrame frame = getFrame(insn);
Operand[] out = frame.out();
Operand opr;
if (out == null) {
Type totype;
switch (op) {
case I2L: