forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExprVisitor.java
More file actions
826 lines (733 loc) · 31.5 KB
/
ExprVisitor.java
File metadata and controls
826 lines (733 loc) · 31.5 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
package soot.toDex;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 1997 - 2018 Raja Vallée-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 java.util.ArrayList;
import java.util.List;
import org.jf.dexlib2.Opcode;
import org.jf.dexlib2.iface.reference.MethodReference;
import org.jf.dexlib2.iface.reference.TypeReference;
import soot.ArrayType;
import soot.DoubleType;
import soot.FloatType;
import soot.IntType;
import soot.IntegerType;
import soot.Local;
import soot.LongType;
import soot.NullType;
import soot.PrimType;
import soot.RefType;
import soot.SootClass;
import soot.SootMethod;
import soot.Type;
import soot.Value;
import soot.jimple.AddExpr;
import soot.jimple.AndExpr;
import soot.jimple.CastExpr;
import soot.jimple.CmpExpr;
import soot.jimple.CmpgExpr;
import soot.jimple.CmplExpr;
import soot.jimple.DivExpr;
import soot.jimple.DynamicInvokeExpr;
import soot.jimple.EqExpr;
import soot.jimple.Expr;
import soot.jimple.ExprSwitch;
import soot.jimple.GeExpr;
import soot.jimple.GtExpr;
import soot.jimple.InstanceInvokeExpr;
import soot.jimple.InstanceOfExpr;
import soot.jimple.IntConstant;
import soot.jimple.InterfaceInvokeExpr;
import soot.jimple.InvokeExpr;
import soot.jimple.LeExpr;
import soot.jimple.LengthExpr;
import soot.jimple.LongConstant;
import soot.jimple.LtExpr;
import soot.jimple.MulExpr;
import soot.jimple.NeExpr;
import soot.jimple.NegExpr;
import soot.jimple.NewArrayExpr;
import soot.jimple.NewExpr;
import soot.jimple.NewMultiArrayExpr;
import soot.jimple.NullConstant;
import soot.jimple.OrExpr;
import soot.jimple.RemExpr;
import soot.jimple.ShlExpr;
import soot.jimple.ShrExpr;
import soot.jimple.SpecialInvokeExpr;
import soot.jimple.StaticInvokeExpr;
import soot.jimple.Stmt;
import soot.jimple.SubExpr;
import soot.jimple.UshrExpr;
import soot.jimple.VirtualInvokeExpr;
import soot.jimple.XorExpr;
import soot.toDex.instructions.Insn;
import soot.toDex.instructions.Insn10x;
import soot.toDex.instructions.Insn11x;
import soot.toDex.instructions.Insn12x;
import soot.toDex.instructions.Insn21c;
import soot.toDex.instructions.Insn21t;
import soot.toDex.instructions.Insn22b;
import soot.toDex.instructions.Insn22c;
import soot.toDex.instructions.Insn22s;
import soot.toDex.instructions.Insn22t;
import soot.toDex.instructions.Insn23x;
import soot.toDex.instructions.Insn35c;
import soot.toDex.instructions.Insn3rc;
import soot.toDex.instructions.InsnWithOffset;
import soot.util.Switchable;
/**
* A visitor that builds a list of instructions from the Jimple expressions it visits.<br>
* <br>
* Use {@link Switchable#apply(soot.util.Switch)} with this visitor to add statements. These are added to the instructions in
* the {@link StmtVisitor}.<br>
* If the expression is part of an assignment or an if statement, use {@link #setDestinationReg(Register)} and
* {@link #setTargetForOffset(Stmt)}, respectively.
*
* @see StmtVisitor
*/
public class ExprVisitor implements ExprSwitch {
protected StmtVisitor stmtV;
protected ConstantVisitor constantV;
protected RegisterAllocator regAlloc;
protected Register destinationReg;
protected Stmt targetForOffset;
protected Stmt origStmt;
private int lastInvokeInstructionPosition;
public ExprVisitor(StmtVisitor stmtV, ConstantVisitor constantV, RegisterAllocator regAlloc) {
this.stmtV = stmtV;
this.constantV = constantV;
this.regAlloc = regAlloc;
}
public void setDestinationReg(Register destinationReg) {
this.destinationReg = destinationReg;
}
public void setOrigStmt(Stmt stmt) {
this.origStmt = stmt;
}
public void setTargetForOffset(Stmt targetForOffset) {
this.targetForOffset = targetForOffset;
}
@Override
public void defaultCase(Object o) {
// rsub-int and rsub-int/lit8 aren't generated, since we cannot detect
// there usage in jimple
throw new Error("unknown Object (" + o.getClass() + ") as Expression: " + o);
}
@Override
public void caseDynamicInvokeExpr(DynamicInvokeExpr v) {
throw new Error("DynamicInvokeExpr not supported: " + v);
}
@Override
public void caseSpecialInvokeExpr(SpecialInvokeExpr sie) {
MethodReference method = DexPrinter.toMethodReference(sie.getMethodRef());
List<Register> arguments = getInstanceInvokeArgumentRegs(sie);
lastInvokeInstructionPosition = stmtV.getInstructionCount();
if (isCallToConstructor(sie) || isCallToPrivate(sie)) {
stmtV.addInsn(buildInvokeInsn("INVOKE_DIRECT", method, arguments), origStmt);
} else if (isCallToSuper(sie)) {
stmtV.addInsn(buildInvokeInsn("INVOKE_SUPER", method, arguments), origStmt);
} else {
// This should normally never happen, but if we have such a
// broken call (happens in malware for instance), we fix it.
stmtV.addInsn(buildInvokeInsn("INVOKE_VIRTUAL", method, arguments), origStmt);
}
}
private Insn buildInvokeInsn(String invokeOpcode, MethodReference method, List<Register> argumentRegs) {
Insn invokeInsn;
int regCountForArguments = SootToDexUtils.getRealRegCount(argumentRegs);
if (regCountForArguments <= 5) {
Register[] paddedArray = pad35cRegs(argumentRegs);
Opcode opc = Opcode.valueOf(invokeOpcode);
invokeInsn = new Insn35c(opc, regCountForArguments, paddedArray[0], paddedArray[1], paddedArray[2], paddedArray[3],
paddedArray[4], method);
} else if (regCountForArguments <= 255) {
Opcode opc = Opcode.valueOf(invokeOpcode + "_RANGE");
invokeInsn = new Insn3rc(opc, argumentRegs, (short) regCountForArguments, method);
} else {
throw new Error(
"too many parameter registers for invoke-* (> 255): " + regCountForArguments + " or registers too big (> 4 bits)");
}
// save the return type for the move-result insn
stmtV.setLastReturnTypeDescriptor(method.getReturnType());
return invokeInsn;
}
private boolean isCallToPrivate(SpecialInvokeExpr sie) {
return sie.getMethod().isPrivate();
}
private boolean isCallToConstructor(SpecialInvokeExpr sie) {
return sie.getMethodRef().name().equals(SootMethod.constructorName);
}
private boolean isCallToSuper(SpecialInvokeExpr sie) {
SootClass classWithInvokation = sie.getMethod().getDeclaringClass();
SootClass currentClass = stmtV.getBelongingClass();
while (currentClass != null) {
currentClass = currentClass.getSuperclassUnsafe();
if (currentClass != null) {
// If we're dealing with phantom classes, we might not actually
// arrive at java.lang.Object. In this case, we should not fail
// the check
if ((currentClass == classWithInvokation)
|| (currentClass.isPhantom() && !currentClass.getName().equals("java.lang.Object"))) {
return true;
}
}
}
return false; // we arrived at java.lang.Object and did not find a
// declaration
}
@Override
public void caseVirtualInvokeExpr(VirtualInvokeExpr vie) {
/*
* for final methods we build an invoke-virtual opcode, too, although the dex spec says that a virtual method is not
* final. An alternative would be the invoke-direct opcode, but this is inconsistent with dx's output...
*/
MethodReference method = DexPrinter.toMethodReference(vie.getMethodRef());
List<Register> argumentRegs = getInstanceInvokeArgumentRegs(vie);
lastInvokeInstructionPosition = stmtV.getInstructionCount();
stmtV.addInsn(buildInvokeInsn("INVOKE_VIRTUAL", method, argumentRegs), origStmt);
}
private List<Register> getInvokeArgumentRegs(InvokeExpr ie) {
constantV.setOrigStmt(origStmt);
List<Register> argumentRegs = new ArrayList<Register>();
for (Value arg : ie.getArgs()) {
Register currentReg = regAlloc.asImmediate(arg, constantV);
argumentRegs.add(currentReg);
}
return argumentRegs;
}
private List<Register> getInstanceInvokeArgumentRegs(InstanceInvokeExpr iie) {
constantV.setOrigStmt(origStmt);
List<Register> argumentRegs = getInvokeArgumentRegs(iie);
// always add reference to callee as first parameter (instance !=
// static)
Local callee = (Local) iie.getBase();
Register calleeRegister = regAlloc.asLocal(callee);
argumentRegs.add(0, calleeRegister);
return argumentRegs;
}
private Register[] pad35cRegs(List<Register> realRegs) {
// pad real registers to an array of five bytes, as required by the
// instruction format "35c"
Register[] paddedArray = new Register[5];
int nextReg = 0;
for (Register realReg : realRegs) {
paddedArray[nextReg] = realReg;
/*
* we include the second half of a wide with an empty reg for the "gap". this will be made explicit for dexlib later
* on.
*/
if (realReg.isWide()) {
nextReg++;
paddedArray[nextReg] = Register.EMPTY_REGISTER;
}
nextReg++;
}
// do the real padding with empty regs
for (; nextReg < 5; nextReg++) {
paddedArray[nextReg] = Register.EMPTY_REGISTER;
}
return paddedArray;
}
@Override
public void caseInterfaceInvokeExpr(InterfaceInvokeExpr iie) {
MethodReference method = DexPrinter.toMethodReference(iie.getMethodRef());
List<Register> arguments = getInstanceInvokeArgumentRegs(iie);
lastInvokeInstructionPosition = stmtV.getInstructionCount();
stmtV.addInsn(buildInvokeInsn("INVOKE_INTERFACE", method, arguments), origStmt);
}
@Override
public void caseStaticInvokeExpr(StaticInvokeExpr sie) {
MethodReference method = DexPrinter.toMethodReference(sie.getMethodRef());
List<Register> arguments = getInvokeArgumentRegs(sie);
lastInvokeInstructionPosition = stmtV.getInstructionCount();
stmtV.addInsn(buildInvokeInsn("INVOKE_STATIC", method, arguments), origStmt);
}
private void buildCalculatingBinaryInsn(final String binaryOperation, Value firstOperand, Value secondOperand,
Expr originalExpr) {
constantV.setOrigStmt(origStmt);
Register firstOpReg = regAlloc.asImmediate(firstOperand, constantV);
// use special "/lit"-opcodes if int is the destination's type, the
// second op is
// an integer literal and the opc is not sub (no sub*/lit opc available)
if (destinationReg.getType() instanceof IntType && secondOperand instanceof IntConstant
&& !binaryOperation.equals("SUB")) {
int secondOpConstant = ((IntConstant) secondOperand).value;
if (SootToDexUtils.fitsSigned8(secondOpConstant)) {
stmtV.addInsn(buildLit8BinaryInsn(binaryOperation, firstOpReg, (byte) secondOpConstant), origStmt);
return;
}
if (SootToDexUtils.fitsSigned16(secondOpConstant)) {
if (!binaryOperation.equals("SHL") && !binaryOperation.equals("SHR") && !binaryOperation.equals("USHR")) {
// no shift opc available for /lit16
stmtV.addInsn(buildLit16BinaryInsn(binaryOperation, firstOpReg, (short) secondOpConstant), origStmt);
return;
}
}
// constant is too big, so use it as second op and build normally
}
// Double-check that we are not carrying out any arithmetic operations
// on non-primitive values
if (!(secondOperand.getType() instanceof PrimType)) {
throw new RuntimeException("Invalid value type for primitive operation");
}
// For some data types, there are no calculating opcodes in Dalvik. In
// this case, we need to use a bigger opcode and cast the result back.
PrimitiveType destRegType = PrimitiveType.getByName(destinationReg.getType().toString());
Register secondOpReg = regAlloc.asImmediate(secondOperand, constantV);
Register orgDestReg = destinationReg;
if (isSmallerThan(destRegType, PrimitiveType.INT)) {
destinationReg = regAlloc.asTmpReg(IntType.v());
} else {
// If the second register is not of the precise target type, we need
// to
// cast. This can happen, because we cannot load BYTE values. We
// instead
// load INT constants and then need to typecast.
if (isBiggerThan(PrimitiveType.getByName(secondOpReg.getType().toString()), destRegType)) {
destinationReg = regAlloc.asTmpReg(secondOpReg.getType());
} else if (isBiggerThan(PrimitiveType.getByName(firstOpReg.getType().toString()), destRegType)) {
destinationReg = regAlloc.asTmpReg(firstOpReg.getType());
}
}
// use special "/2addr"-opcodes if destination equals first op
if (destinationReg.getNumber() == firstOpReg.getNumber()) {
stmtV.addInsn(build2AddrBinaryInsn(binaryOperation, secondOpReg), origStmt);
} else {
// no optimized binary opcode possible
stmtV.addInsn(buildNormalBinaryInsn(binaryOperation, firstOpReg, secondOpReg), origStmt);
}
// If we have used a temporary register, we must copy over the result
if (orgDestReg != destinationReg) {
Register tempReg = destinationReg.clone();
destinationReg = orgDestReg.clone();
castPrimitive(tempReg, originalExpr, destinationReg.getType());
}
}
private String fixIntTypeString(String typeString) {
if (typeString.equals("boolean") || typeString.equals("byte") || typeString.equals("char")
|| typeString.equals("short")) {
return "int";
}
return typeString;
}
private Insn build2AddrBinaryInsn(final String binaryOperation, Register secondOpReg) {
String localTypeString = destinationReg.getTypeString();
localTypeString = fixIntTypeString(localTypeString);
Opcode opc = Opcode.valueOf(binaryOperation + "_" + localTypeString.toUpperCase() + "_2ADDR");
return new Insn12x(opc, destinationReg, secondOpReg);
}
private Insn buildNormalBinaryInsn(String binaryOperation, Register firstOpReg, Register secondOpReg) {
String localTypeString = firstOpReg.getTypeString();
localTypeString = fixIntTypeString(localTypeString);
Opcode opc = Opcode.valueOf(binaryOperation + "_" + localTypeString.toUpperCase());
return new Insn23x(opc, destinationReg, firstOpReg, secondOpReg);
}
private Insn buildLit16BinaryInsn(String binaryOperation, Register firstOpReg, short secondOpLieteral) {
Opcode opc = Opcode.valueOf(binaryOperation + "_INT_LIT16");
return new Insn22s(opc, destinationReg, firstOpReg, secondOpLieteral);
}
private Insn buildLit8BinaryInsn(String binaryOperation, Register firstOpReg, byte secondOpLiteral) {
Opcode opc = Opcode.valueOf(binaryOperation + "_INT_LIT8");
return new Insn22b(opc, destinationReg, firstOpReg, secondOpLiteral);
}
@Override
public void caseAddExpr(AddExpr ae) {
buildCalculatingBinaryInsn("ADD", ae.getOp1(), ae.getOp2(), ae);
}
@Override
public void caseSubExpr(SubExpr se) {
buildCalculatingBinaryInsn("SUB", se.getOp1(), se.getOp2(), se);
}
@Override
public void caseMulExpr(MulExpr me) {
buildCalculatingBinaryInsn("MUL", me.getOp1(), me.getOp2(), me);
}
@Override
public void caseDivExpr(DivExpr de) {
buildCalculatingBinaryInsn("DIV", de.getOp1(), de.getOp2(), de);
}
@Override
public void caseRemExpr(RemExpr re) {
buildCalculatingBinaryInsn("REM", re.getOp1(), re.getOp2(), re);
}
@Override
public void caseAndExpr(AndExpr ae) {
buildCalculatingBinaryInsn("AND", ae.getOp1(), ae.getOp2(), ae);
}
@Override
public void caseOrExpr(OrExpr oe) {
buildCalculatingBinaryInsn("OR", oe.getOp1(), oe.getOp2(), oe);
}
@Override
public void caseXorExpr(XorExpr xe) {
Value firstOperand = xe.getOp1();
Value secondOperand = xe.getOp2();
constantV.setOrigStmt(origStmt);
// see for unary ones-complement shortcut, may be a result of Dexpler's
// conversion
if (secondOperand.equals(IntConstant.v(-1)) || secondOperand.equals(LongConstant.v(-1))) {
PrimitiveType destRegType = PrimitiveType.getByName(destinationReg.getType().toString());
Register orgDestReg = destinationReg;
// We may need a temporary register if we need a typecast
if (isBiggerThan(PrimitiveType.getByName(secondOperand.getType().toString()), destRegType)) {
destinationReg = regAlloc.asTmpReg(IntType.v());
}
if (secondOperand.equals(IntConstant.v(-1))) {
Register sourceReg = regAlloc.asImmediate(firstOperand, constantV);
stmtV.addInsn(new Insn12x(Opcode.NOT_INT, destinationReg, sourceReg), origStmt);
} else if (secondOperand.equals(LongConstant.v(-1))) {
Register sourceReg = regAlloc.asImmediate(firstOperand, constantV);
stmtV.addInsn(new Insn12x(Opcode.NOT_LONG, destinationReg, sourceReg), origStmt);
}
// If we have used a temporary register, we must copy over the
// result
if (orgDestReg != destinationReg) {
Register tempReg = destinationReg.clone();
destinationReg = orgDestReg.clone();
castPrimitive(tempReg, secondOperand, destinationReg.getType());
}
} else {
// No shortcut, create normal binary operation
buildCalculatingBinaryInsn("XOR", firstOperand, secondOperand, xe);
}
}
@Override
public void caseShlExpr(ShlExpr sle) {
buildCalculatingBinaryInsn("SHL", sle.getOp1(), sle.getOp2(), sle);
}
@Override
public void caseShrExpr(ShrExpr sre) {
buildCalculatingBinaryInsn("SHR", sre.getOp1(), sre.getOp2(), sre);
}
@Override
public void caseUshrExpr(UshrExpr usre) {
buildCalculatingBinaryInsn("USHR", usre.getOp1(), usre.getOp2(), usre);
}
private Insn buildComparingBinaryInsn(String binaryOperation, Value firstOperand, Value secondOperand) {
constantV.setOrigStmt(origStmt);
Value realFirstOperand = fixNullConstant(firstOperand);
Value realSecondOperand = fixNullConstant(secondOperand);
Register firstOpReg = regAlloc.asImmediate(realFirstOperand, constantV);
// select fitting opcode ("if" or "if-zero")
InsnWithOffset comparingBinaryInsn;
String opcodeName = "IF_" + binaryOperation;
boolean secondOpIsInt = realSecondOperand instanceof IntConstant;
boolean secondOpIsZero = secondOpIsInt && ((IntConstant) realSecondOperand).value == 0;
if (secondOpIsZero) {
Opcode opc = Opcode.valueOf(opcodeName.concat("Z"));
comparingBinaryInsn = new Insn21t(opc, firstOpReg);
comparingBinaryInsn.setTarget(targetForOffset);
} else {
Opcode opc = Opcode.valueOf(opcodeName);
Register secondOpReg = regAlloc.asImmediate(realSecondOperand, constantV);
comparingBinaryInsn = new Insn22t(opc, firstOpReg, secondOpReg);
comparingBinaryInsn.setTarget(targetForOffset);
}
return comparingBinaryInsn;
}
private Value fixNullConstant(Value potentialNullConstant) {
/*
* The bytecode spec says: "In terms of bitwise representation, (Object) null == (int) 0." So use an IntConstant(0) for
* null comparison in if-*z opcodes.
*/
if (potentialNullConstant instanceof NullConstant) {
return IntConstant.v(0);
}
return potentialNullConstant;
}
@Override
public void caseEqExpr(EqExpr ee) {
stmtV.addInsn(buildComparingBinaryInsn("EQ", ee.getOp1(), ee.getOp2()), origStmt);
}
@Override
public void caseGeExpr(GeExpr ge) {
stmtV.addInsn(buildComparingBinaryInsn("GE", ge.getOp1(), ge.getOp2()), origStmt);
}
@Override
public void caseGtExpr(GtExpr ge) {
stmtV.addInsn(buildComparingBinaryInsn("GT", ge.getOp1(), ge.getOp2()), origStmt);
}
@Override
public void caseLeExpr(LeExpr le) {
stmtV.addInsn(buildComparingBinaryInsn("LE", le.getOp1(), le.getOp2()), origStmt);
}
@Override
public void caseLtExpr(LtExpr le) {
stmtV.addInsn(buildComparingBinaryInsn("LT", le.getOp1(), le.getOp2()), origStmt);
}
@Override
public void caseNeExpr(NeExpr ne) {
stmtV.addInsn(buildComparingBinaryInsn("NE", ne.getOp1(), ne.getOp2()), origStmt);
}
@Override
public void caseCmpExpr(CmpExpr v) {
stmtV.addInsn(buildCmpInsn("CMP_LONG", v.getOp1(), v.getOp2()), origStmt);
}
@Override
public void caseCmpgExpr(CmpgExpr v) {
stmtV.addInsn(buildCmpInsn("CMPG", v.getOp1(), v.getOp2()), origStmt);
}
@Override
public void caseCmplExpr(CmplExpr v) {
stmtV.addInsn(buildCmpInsn("CMPL", v.getOp1(), v.getOp2()), origStmt);
}
private Insn buildCmpInsn(String opcodePrefix, Value firstOperand, Value secondOperand) {
constantV.setOrigStmt(origStmt);
Register firstReg = regAlloc.asImmediate(firstOperand, constantV);
Register secondReg = regAlloc.asImmediate(secondOperand, constantV);
// select fitting opcode according to the prefix and the type of the
// operands
Opcode opc = null;
// we assume that the type of the operands are equal and use only the
// first one
if (opcodePrefix.equals("CMP_LONG")) {
opc = Opcode.CMP_LONG;
} else if (firstReg.isFloat()) {
opc = Opcode.valueOf(opcodePrefix + "_FLOAT");
} else if (firstReg.isDouble()) {
opc = Opcode.valueOf(opcodePrefix + "_DOUBLE");
} else {
throw new RuntimeException("unsupported type of operands for cmp* opcode: " + firstOperand.getType());
}
return new Insn23x(opc, destinationReg, firstReg, secondReg);
}
@Override
public void caseLengthExpr(LengthExpr le) {
Value array = le.getOp();
constantV.setOrigStmt(origStmt);
// In buggy code, the parameter could be a NullConstant.
// This is why we use .asImmediate() and not .asLocal()
Register arrayReg = regAlloc.asImmediate(array, constantV);
stmtV.addInsn(new Insn12x(Opcode.ARRAY_LENGTH, destinationReg, arrayReg), origStmt);
}
@Override
public void caseNegExpr(NegExpr ne) {
Value source = ne.getOp();
constantV.setOrigStmt(origStmt);
Register sourceReg = regAlloc.asImmediate(source, constantV);
Opcode opc;
Type type = source.getType();
if (type instanceof IntegerType) {
opc = Opcode.NEG_INT;
} else if (type instanceof FloatType) {
opc = Opcode.NEG_FLOAT;
} else if (type instanceof DoubleType) {
opc = Opcode.NEG_DOUBLE;
} else if (type instanceof LongType) {
opc = Opcode.NEG_LONG;
} else {
throw new RuntimeException("unsupported value type for neg-* opcode: " + type);
}
stmtV.addInsn(new Insn12x(opc, destinationReg, sourceReg), origStmt);
}
@Override
public void caseInstanceOfExpr(InstanceOfExpr ioe) {
final Value referenceToCheck = ioe.getOp();
// There are some strange apps that use constants here
constantV.setOrigStmt(origStmt);
Register referenceToCheckReg = regAlloc.asImmediate(referenceToCheck, constantV);
TypeReference checkType = DexPrinter.toTypeReference(ioe.getCheckType());
stmtV.addInsn(new Insn22c(Opcode.INSTANCE_OF, destinationReg, referenceToCheckReg, checkType), origStmt);
}
@Override
public void caseCastExpr(CastExpr ce) {
Type castType = ce.getCastType();
Value source = ce.getOp();
constantV.setOrigStmt(origStmt);
Register sourceReg = regAlloc.asImmediate(source, constantV);
if (SootToDexUtils.isObject(castType)) {
castObject(sourceReg, castType);
} else {
castPrimitive(sourceReg, source, castType);
}
}
protected void castObject(Register sourceReg, Type castType) {
/*
* No real "cast" is done: move the object to a tmp reg, check the cast with check-cast and finally move to the "cast"
* object location. This way a) the old reg is not touched by check-cast, and b) the new reg does change its type only
* after a successful check-cast.
*
* a) is relevant e.g. for "r1 = (String) r0" if r0 contains null, since the internal type of r0 would change from Null
* to String after the check-cast opcode, which alerts the verifier in future uses of r0 although nothing should change
* during execution.
*
* b) is relevant for exceptional control flow: if we move to the new reg and do the check-cast there, an exception
* between the end of the move's execution and the end of the check-cast execution leaves the new reg with the type of
* the old reg.
*/
TypeReference castTypeItem = DexPrinter.toTypeReference(castType);
if (sourceReg.getNumber() == destinationReg.getNumber()) {
// simplyfied case if reg numbers do not differ
stmtV.addInsn(new Insn21c(Opcode.CHECK_CAST, destinationReg, castTypeItem), origStmt);
} else {
// move to tmp reg, check cast, move to destination
Register tmp = regAlloc.asTmpReg(sourceReg.getType());
stmtV.addInsn(StmtVisitor.buildMoveInsn(tmp, sourceReg), origStmt);
stmtV.addInsn(new Insn21c(Opcode.CHECK_CAST, tmp.clone(), castTypeItem), origStmt);
stmtV.addInsn(StmtVisitor.buildMoveInsn(destinationReg, tmp.clone()), origStmt);
}
}
private void castPrimitive(Register sourceReg, Value source, Type castSootType) {
PrimitiveType castType = PrimitiveType.getByName(castSootType.toString());
// Fix null_types on the fly. This should not be necessary, but better
// be safe
// than sorry and it's easy to fix it here.
if (castType == PrimitiveType.INT && source.getType() instanceof NullType) {
source = IntConstant.v(0);
}
// select fitting conversion opcode, depending on the source and cast
// type
Type srcType = source.getType();
if (srcType instanceof RefType) {
throw new RuntimeException("Trying to cast reference type " + srcType + " to a primitive");
}
PrimitiveType sourceType = PrimitiveType.getByName(srcType.toString());
if (castType == PrimitiveType.BOOLEAN) {
// there is no "-to-boolean" opcode, so just pretend to move an int
// to an int
castType = PrimitiveType.INT;
sourceType = PrimitiveType.INT;
}
if (shouldCastFromInt(sourceType, castType)) {
// pretend to cast from int since that is OK
sourceType = PrimitiveType.INT;
Opcode opc = getCastOpc(sourceType, castType);
stmtV.addInsn(new Insn12x(opc, destinationReg, sourceReg), origStmt);
} else if (isMoveCompatible(sourceType, castType)) {
/*
* no actual cast needed, just move the reg content if regs differ
*/
if (destinationReg.getNumber() != sourceReg.getNumber()) {
stmtV.addInsn(StmtVisitor.buildMoveInsn(destinationReg, sourceReg), origStmt);
}
// Make sure that we have at least some statement in case we need
// one for jumps
else if (!origStmt.getBoxesPointingToThis().isEmpty()) {
stmtV.addInsn(new Insn10x(Opcode.NOP), origStmt);
}
} else if (needsCastThroughInt(sourceType, castType)) {
/*
* an unsupported "dest = (cast) src" is broken down to "tmp = (int) src" and "dest = (cast) tmp", using a tmp reg to
* not mess with the original reg types
*/
Opcode castToIntOpc = getCastOpc(sourceType, PrimitiveType.INT);
Opcode castFromIntOpc = getCastOpc(PrimitiveType.INT, castType);
Register tmp = regAlloc.asTmpReg(IntType.v());
stmtV.addInsn(new Insn12x(castToIntOpc, tmp, sourceReg), origStmt);
stmtV.addInsn(new Insn12x(castFromIntOpc, destinationReg, tmp.clone()), origStmt);
} else {
// the leftover simple cases, where we just cast as stated
Opcode opc = getCastOpc(sourceType, castType);
stmtV.addInsn(new Insn12x(opc, destinationReg, sourceReg), origStmt);
}
}
private boolean needsCastThroughInt(PrimitiveType sourceType, PrimitiveType castType) {
// source >= long && cast < int -> too far away to cast directly
return isEqualOrBigger(sourceType, PrimitiveType.LONG) && !isEqualOrBigger(castType, PrimitiveType.INT);
}
private boolean isMoveCompatible(PrimitiveType sourceType, PrimitiveType castType) {
if ((sourceType == castType) || (castType == PrimitiveType.INT && !isBiggerThan(sourceType, PrimitiveType.INT))) {
// there is no "upgrade" cast from "smaller than int" to int, so
// move it
return true;
}
return false;
}
private boolean shouldCastFromInt(PrimitiveType sourceType, PrimitiveType castType) {
if (isEqualOrBigger(sourceType, PrimitiveType.INT) || (castType == PrimitiveType.INT)) {
// would lead to an int-to-int cast, so leave it as it is
return false;
}
return true;
}
private boolean isEqualOrBigger(PrimitiveType type, PrimitiveType relativeTo) {
return type.compareTo(relativeTo) >= 0;
}
private boolean isBiggerThan(PrimitiveType type, PrimitiveType relativeTo) {
return type.compareTo(relativeTo) > 0;
}
private boolean isSmallerThan(PrimitiveType type, PrimitiveType relativeTo) {
return type.compareTo(relativeTo) < 0;
}
private Opcode getCastOpc(PrimitiveType sourceType, PrimitiveType castType) {
Opcode opc = Opcode.valueOf(sourceType.getName().toUpperCase() + "_TO_" + castType.getName().toUpperCase());
if (opc == null) {
throw new RuntimeException("unsupported cast from " + sourceType + " to " + castType);
}
return opc;
}
@Override
public void caseNewArrayExpr(NewArrayExpr nae) {
Value size = nae.getSize();
constantV.setOrigStmt(origStmt);
Register sizeReg = regAlloc.asImmediate(size, constantV);
// Create the array type
Type baseType = nae.getBaseType();
int numDimensions = 1;
while (baseType instanceof ArrayType) {
ArrayType at = (ArrayType) baseType;
baseType = at.getElementType();
numDimensions++;
}
ArrayType arrayType = ArrayType.v(baseType, numDimensions);
TypeReference arrayTypeItem = DexPrinter.toTypeReference(arrayType);
stmtV.addInsn(new Insn22c(Opcode.NEW_ARRAY, destinationReg, sizeReg, arrayTypeItem), origStmt);
}
@Override
public void caseNewMultiArrayExpr(NewMultiArrayExpr nmae) {
constantV.setOrigStmt(origStmt);
// get array dimensions
if (nmae.getSizeCount() > 255) {
throw new RuntimeException(
"number of dimensions is too high (> 255) for the filled-new-array* opcodes: " + nmae.getSizeCount());
}
short dimensions = (short) nmae.getSizeCount();
// get array base type
ArrayType arrayType = ArrayType.v(nmae.getBaseType().baseType, dimensions);
TypeReference arrayTypeItem = DexPrinter.toTypeReference(arrayType);
// get the dimension size registers
List<Register> dimensionSizeRegs = new ArrayList<Register>();
for (int i = 0; i < dimensions; i++) {
Value currentDimensionSize = nmae.getSize(i);
Register currentReg = regAlloc.asImmediate(currentDimensionSize, constantV);
dimensionSizeRegs.add(currentReg);
}
// create filled-new-array instruction, depending on the dimension sizes
if (dimensions <= 5) {
Register[] paddedRegs = pad35cRegs(dimensionSizeRegs);
stmtV.addInsn(new Insn35c(Opcode.FILLED_NEW_ARRAY, dimensions, paddedRegs[0], paddedRegs[1], paddedRegs[2],
paddedRegs[3], paddedRegs[4], arrayTypeItem), null);
} else {
stmtV.addInsn(new Insn3rc(Opcode.FILLED_NEW_ARRAY_RANGE, dimensionSizeRegs, dimensions, arrayTypeItem), null);
} // check for > 255 is done already
// move the resulting array into the destination register
stmtV.addInsn(new Insn11x(Opcode.MOVE_RESULT_OBJECT, destinationReg), origStmt);
}
@Override
public void caseNewExpr(NewExpr ne) {
TypeReference baseType = DexPrinter.toTypeReference(ne.getBaseType());
stmtV.addInsn(new Insn21c(Opcode.NEW_INSTANCE, destinationReg, baseType), origStmt);
}
public int getLastInvokeInstructionPosition() {
return lastInvokeInstructionPosition;
}
}