forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSootToDexUtils.java
More file actions
270 lines (239 loc) · 7.91 KB
/
SootToDexUtils.java
File metadata and controls
270 lines (239 loc) · 7.91 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
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.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jf.dexlib2.Opcode;
import soot.ArrayType;
import soot.BooleanType;
import soot.ByteType;
import soot.CharType;
import soot.DoubleType;
import soot.FloatType;
import soot.IntType;
import soot.LongType;
import soot.Modifier;
import soot.RefLikeType;
import soot.RefType;
import soot.ShortType;
import soot.SootMethod;
import soot.Type;
import soot.Unit;
import soot.Value;
import soot.VoidType;
import soot.jimple.InvokeExpr;
import soot.jimple.Stmt;
/**
* Utility class for the conversion from soot to dex.
*/
public class SootToDexUtils {
private static final Map<Class<? extends Type>, String> sootToDexTypeDescriptor;
static {
sootToDexTypeDescriptor = new HashMap<Class<? extends Type>, String>();
sootToDexTypeDescriptor.put(BooleanType.class, "Z");
sootToDexTypeDescriptor.put(ByteType.class, "B");
sootToDexTypeDescriptor.put(CharType.class, "C");
sootToDexTypeDescriptor.put(DoubleType.class, "D");
sootToDexTypeDescriptor.put(FloatType.class, "F");
sootToDexTypeDescriptor.put(IntType.class, "I");
sootToDexTypeDescriptor.put(LongType.class, "J");
sootToDexTypeDescriptor.put(ShortType.class, "S");
sootToDexTypeDescriptor.put(VoidType.class, "V");
}
public static String getDexTypeDescriptor(Type sootType) {
if (sootType == null) {
throw new NullPointerException("Soot type was null");
}
final String typeDesc;
if (sootType instanceof RefType) {
typeDesc = getDexClassName(((RefType) sootType).getClassName());
} else if (sootType instanceof ArrayType) {
typeDesc = getDexArrayTypeDescriptor((ArrayType) sootType);
} else {
typeDesc = sootToDexTypeDescriptor.get(sootType.getClass());
}
if (typeDesc == null || typeDesc.isEmpty()) {
throw new RuntimeException("Could not create type descriptor for class " + sootType);
}
return typeDesc;
}
public static String getDexClassName(String dottedClassName) {
if (dottedClassName == null || dottedClassName.isEmpty()) {
throw new RuntimeException("Empty class name detected");
}
String slashedName = dottedClassName.replace('.', '/');
if (slashedName.startsWith("L") && slashedName.endsWith(";")) {
return slashedName;
}
return "L" + slashedName + ";";
}
public static int getDexAccessFlags(SootMethod m) {
int dexAccessFlags = m.getModifiers();
// dex constructor flag is not included in the Soot modifiers, so add it if
// necessary
if (m.isConstructor() || m.getName().equals(SootMethod.staticInitializerName)) {
dexAccessFlags |= Modifier.CONSTRUCTOR;
}
// add declared_synchronized for dex if synchronized
if (m.isSynchronized()) {
dexAccessFlags |= Modifier.DECLARED_SYNCHRONIZED;
// even remove synchronized if not native, since only allowed there
if (!m.isNative()) {
dexAccessFlags &= ~Modifier.SYNCHRONIZED;
}
}
return dexAccessFlags;
}
public static String getArrayTypeDescriptor(ArrayType type) {
Type baseType;
if (type.numDimensions > 1) {
baseType = ArrayType.v(type.baseType, 1);
} else {
baseType = type.baseType;
}
return getDexTypeDescriptor(baseType);
}
private static String getDexArrayTypeDescriptor(ArrayType sootArray) {
if (sootArray.numDimensions > 255) {
throw new RuntimeException(
"dex does not support more than 255 dimensions! " + sootArray + " has " + sootArray.numDimensions);
}
String baseTypeDescriptor = getDexTypeDescriptor(sootArray.baseType);
StringBuilder sb = new StringBuilder(sootArray.numDimensions + baseTypeDescriptor.length());
for (int i = 0; i < sootArray.numDimensions; i++) {
sb.append('[');
}
sb.append(baseTypeDescriptor);
return sb.toString();
}
public static boolean isObject(String typeDescriptor) {
if (typeDescriptor.isEmpty()) {
return false;
}
char first = typeDescriptor.charAt(0);
return first == 'L' || first == '[';
}
public static boolean isObject(Type sootType) {
return sootType instanceof RefLikeType;
}
public static boolean isWide(String typeDescriptor) {
return typeDescriptor.equals("J") || typeDescriptor.equals("D");
}
public static boolean isWide(Type sootType) {
return sootType instanceof LongType || sootType instanceof DoubleType;
}
public static int getRealRegCount(List<Register> regs) {
int regCount = 0;
for (Register r : regs) {
Type regType = r.getType();
regCount += getDexWords(regType);
}
return regCount;
}
public static int getDexWords(Type sootType) {
return isWide(sootType) ? 2 : 1;
}
public static int getDexWords(List<Type> sootTypes) {
int dexWords = 0;
for (Type t : sootTypes) {
dexWords += getDexWords(t);
}
return dexWords;
}
public static int getOutWordCount(Collection<Unit> units) {
int outWords = 0;
for (Unit u : units) {
Stmt stmt = (Stmt) u;
if (stmt.containsInvokeExpr()) {
int wordsForParameters = 0;
InvokeExpr invocation = stmt.getInvokeExpr();
List<Value> args = invocation.getArgs();
for (Value arg : args) {
wordsForParameters += getDexWords(arg.getType());
}
if (!invocation.getMethod().isStatic()) {
wordsForParameters++; // extra word for "this"
}
if (wordsForParameters > outWords) {
outWords = wordsForParameters;
}
}
}
return outWords;
}
// we could use some fancy shift operations...
public static boolean fitsSigned4(long literal) {
return literal >= -8 && literal <= 7;
}
public static boolean fitsSigned8(long literal) {
return literal >= -128 && literal <= 127;
}
public static boolean fitsSigned16(long literal) {
return literal >= -32768 && literal <= 32767;
}
public static boolean fitsSigned32(long literal) {
return literal >= -2147483648 && literal <= 2147483647;
}
public static boolean isNormalMove(Opcode opc) {
return opc.name.startsWith("move") && !opc.name.startsWith("move-result");
}
/**
* Split the signature string using the same algorithm as in method 'Annotation makeSignature(CstString signature)' in dx
* (dx/src/com/android/dx/dex/file/AnnotationUtils.java)
*
* Rules are: "" - scan to ';' or '<'. Consume ';' but not '<'. - scan to 'L' without consuming it. ""
*
* @param sig
* @return
*/
public static List<String> splitSignature(String sig) {
List<String> split = new ArrayList<String>();
int len = sig.length();
int i = 0;
int j = 0;
while (i < len) {
char c = sig.charAt(i);
if (c == 'L') {
j = i + 1;
while (j < len) {
c = sig.charAt(j);
if (c == ';') {
j++;
break;
} else if (c == '<') {
break;
}
j++;
}
} else {
for (j = i + 1; j < len && sig.charAt(j) != 'L'; j++) {
}
}
split.add(sig.substring(i, j));
i = j;
}
return split;
}
}