-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSmaliTypes.py
353 lines (252 loc) · 8.1 KB
/
SmaliTypes.py
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
import SmaliAssemblyInstructions
import SmaliClassDef
def from_string(raw_type_string):
constructor_map = {"32-bit": ThirtyTwoBit, "Z": Boolean, "B": Byte,
"S": Short, "C": Char, "I": Int, "F": Float,
"64-bit": SixtyFourBit, "64-bit-2": SixtyFourBit_2,
"J": Long, "D": Double, "J2": Long_2, "D2": Double_2}
if raw_type_string in constructor_map:
obj = constructor_map[raw_type_string]()
elif raw_type_string[0] == "[":
obj = Array(raw_type_string)
elif raw_type_string[0] == "L":
obj = ObjectReference(raw_type_string)
elif raw_type_string == "?":
obj = UnknownType()
else:
raise Exception("Invalid type string: " + str(raw_type_string))
return obj
class SmaliType:
def get_move_instr(self):
return self.move_instr
def __repr__(self):
return str(self)
class UnknownType(SmaliType):
def __init__(self):
self.raw_type_string = "?"
self.specificity_level = 0
def __str__(self):
return self.raw_type_string
def __eq__(self, other):
if(isinstance(other, str)):
return other == self.raw_type_string
if(isinstance(other, UnknownType)):
return True
return False
def get_generic_type(self):
# the children inherit this method
# it can be called on ANY "SmaliType" object
# which is convenient to figure out the generic type
return "?"
def unwrap_layer(self):
return UnknownType()
class ThirtyTwoBit(SmaliType):
def __init__(self):
self.move_instr = SmaliAssemblyInstructions.MOVE_16
self.specificity_level = 2
def __str__(self):
return "32-bit"
def __eq__(self, other):
if(isinstance(other, str)):
# maybe instead do this:
# return other == self.get_generic_type()
#print("comparing" + other + " and " + str(self))
return other == str(self)
if(isinstance(other, ThirtyTwoBit)):
# I'm not sure about this!
return True
return False
def get_generic_type(self):
# the children inherit this method
# it can be called on ANY "SmaliType" object
# which is convenient to figure out the generic type
return "32-bit"
class Boolean(ThirtyTwoBit):
def __str__(self):
return "Z"
class Byte(ThirtyTwoBit):
def __str__(self):
return "B"
class Short(ThirtyTwoBit):
def __str__(self):
return "S"
class Char(ThirtyTwoBit):
def __str__(self):
return "C"
class Int(ThirtyTwoBit):
def __str__(self):
return "I"
class Float(ThirtyTwoBit):
def __str__(self):
return "F"
class SixtyFourBit(SmaliType):
# A.K.A. "wide"
def __init__(self):
self.move_instr = SmaliAssemblyInstructions.MOVE_WIDE_16
self.specificity_level = 2
def __str__(self):
return "64-bit"
def __eq__(self, other):
if(isinstance(other, str)):
# maybe instead do this:
# return other == self.get_generic_type()
return other == str(self)
if(isinstance(other, SixtyFourBit)):
return True
return False
def get_generic_type(self):
# the children inherit this method
# it can be called on ANY "SmaliType" object
# which is convenient to figure out the generic type
return "64-bit"
class Long(SixtyFourBit):
def __str__(self):
return "J"
class Double(SixtyFourBit):
def __str__(Self):
return "D"
class SixtyFourBit_2(SmaliType):
def __init__(self):
self.move_instr = None
self.specificity_level = 2
def __str__(self):
return "64-bit-2"
def __eq__(self, other):
if(isinstance(other, str)):
# maybe instead do this:
# return other == self.get_generic_type()
return other == str(self)
if(isinstance(other, SixtyFourBit_2)):
return True
def get_generic_type(self):
# the children inherit this method
# it can be called on ANY "SmaliType" object
# which is convenient to figure out the generic type
return "64-bit-2"
def get_move_instr(self):
raise Exception("No valid move instruction for 64-bit-2: " + str(raw_line_string))
class Long_2(SixtyFourBit_2):
def __str__(self):
return "J2"
class Double_2(SixtyFourBit_2):
def __str__(Self):
return "D2"
class ObjectReference(SmaliType):
def __init__(self, new_raw_type_string):
self.specificity_level = 2
self.move_instr = SmaliAssemblyInstructions.MOVE_OBJECT_16
if(new_raw_type_string[0] != "[" and new_raw_type_string[0] != "L" and new_raw_type_string != "?"):
raise Exception("Invalid specification of object: " + str(new_raw_type_string))
if("->" in new_raw_type_string):
raise Exception("Invalid specification of object: " + str(new_raw_type_string))
self.raw_type_string = new_raw_type_string
def __str__(self):
return self.raw_type_string
def __eq__(self, other):
if(isinstance(other, str)):
# maybe instead do this:
# return other == self.get_generic_type()
return other == str(self)
if(isinstance(other, ObjectReference)):
return str(other) == str(self)
if(isinstance(other, SmaliClassDef.SmaliClassDef)):
return str(self) == str(other.class_name)
def get_generic_type(self):
# the children inherit this method
# it can be called on ANY "SmaliType" object
# which is convenient to figure out the generic type
return "object"
def get_object_simple_name(self):
return self.raw_type_string.split("/")[-1].strip()
def get_object_smali_file_basename(self):
return self.get_object_simple_name().strip(";") + ".smali"
class NonSpecificObjectReference(ObjectReference):
def __init__(self):
self.move_instr = SmaliAssemblyInstructions.MOVE_OBJECT_16
self.raw_type_string = "Non Specific Object"
self.specificity_level = 1
class Array(ObjectReference):
def unwrap_layer(self):
"""
The special case for unwrapping types of array with an aget-object instruction
Algorithm: remove the first character to check the type
aget-object vX vY vZ
vX is the destination, we will set the type of this register
what is returned from this function
vY is the array, we have src_type which is the type of this array
vZ is the index into vY, it is an int, we don't touch this
e.g
1)
src_type: [[I
return "[I"
2)
src_type: [Ljava/lang/String
return "object"
3) src_type= '?'
return ?
"""
unwrapped = self.raw_type_string[1:]
# assert(unwrapped != 64-bit-2)
obj = from_string(unwrapped)
return obj
class NonSpecificArray(ObjectReference):
def __init__(self):
self.move_instr = SmaliAssemblyInstructions.MOVE_OBJECT_16
self.raw_type_string = "Non Specific Array"
self.specificity_level = 1
def unwrap_layer(self):
return UnknownType()
def main():
print("Testing SmaliType")
print("\ttesting 32-bit types...")
int1 = Int()
int2 = from_string("I")
assert(str(int1) == "I")
assert(int1 == "I")
assert(int1 == int2)
assert(isinstance(int1, Int))
assert(isinstance(int1, ThirtyTwoBit))
assert(isinstance(int2, Int))
assert(isinstance(int2, ThirtyTwoBit))
vague1 = from_string("32-bit")
assert(vague1 == "32-bit")
assert(isinstance(vague1, ThirtyTwoBit))
print("\ttesting 64-bit types...")
long_part1 = from_string("J")
long_part2 = from_string("J2")
assert(str(long_part1) == "J")
assert(str(long_part2) == "J2")
assert(long_part1 == "J")
assert(long_part2 == "J2")
assert(isinstance(long_part1, Long))
assert(isinstance(long_part1, SixtyFourBit))
assert(isinstance(long_part2, Long_2))
assert(isinstance(long_part2, SixtyFourBit_2))
assert(isinstance(long_part2, SixtyFourBit) == False)
assert(isinstance(long_part2, SixtyFourBit_2))
print("\ttesting array types...")
arr = Array("[[I")
arr2 = Array("[[I")
arr3 = Array("[J")
arr4 = arr.unwrap_layer()
int3 = arr4.unwrap_layer()
assert(arr == "[[I")
assert(arr4 == "[I")
assert(int3 == "I")
assert(isinstance(int3, Int))
assert(isinstance(int3, ThirtyTwoBit))
assert(arr2 == arr)
arr = from_string("[Ljava/lang/String;")
obj = ObjectReference("Ljava/lang/String;")
assert(isinstance(arr, Array))
assert(arr.unwrap_layer() == obj)
#print(arr)
print("\ttesting object types...")
obj = ObjectReference("Ljava/lang/String;")
obj2 = from_string("Ljava/lang/String;")
assert(obj == obj2)
assert(obj == "Ljava/lang/String;")
assert(obj.get_object_simple_name() == "String;")
print("ALL SmaliType TESTS PASSED!")
if __name__ == "__main__":
main()