annotate paraspace/injection.py @ 131:044bfc415577

Fix issue of data map verification. - Dalvik complain that data map generated by inject_redir.py is unexpected. - Test case map_verify_error_test() - Refactor inject_redir.py to paraspace.injection.inject_redir().
author Thinker K.F. Li <thinker@codemud.net>
date Tue, 09 Aug 2011 11:47:43 +0800
parents 4f508f84c8a1
children 3dee4d929e1f
rev   line source
87
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
1
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
2 def _relocatable_children(obj):
106
7821c6e89622 dexfile_redirect_types() is almost ready.
Thinker K.F. Li <thinker@codemud.net>
parents: 105
diff changeset
3 from paraspace.dex_deptracker import _dex_tree_get_child
100
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
4 from paraspace.dexfile import relocatable, array
106
7821c6e89622 dexfile_redirect_types() is almost ready.
Thinker K.F. Li <thinker@codemud.net>
parents: 105
diff changeset
5
100
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
6 if isinstance(obj, array):
106
7821c6e89622 dexfile_redirect_types() is almost ready.
Thinker K.F. Li <thinker@codemud.net>
parents: 105
diff changeset
7 rel_children = [('items.' + str(idx), value)
7821c6e89622 dexfile_redirect_types() is almost ready.
Thinker K.F. Li <thinker@codemud.net>
parents: 105
diff changeset
8 for idx, value in enumerate(obj.items)
7821c6e89622 dexfile_redirect_types() is almost ready.
Thinker K.F. Li <thinker@codemud.net>
parents: 105
diff changeset
9 if isinstance(value, relocatable)]
100
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
10 return rel_children
87
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
11
106
7821c6e89622 dexfile_redirect_types() is almost ready.
Thinker K.F. Li <thinker@codemud.net>
parents: 105
diff changeset
12 attrs = obj.children()
7821c6e89622 dexfile_redirect_types() is almost ready.
Thinker K.F. Li <thinker@codemud.net>
parents: 105
diff changeset
13 attr_value_pairs = [(attr, _dex_tree_get_child(obj, attr))
7821c6e89622 dexfile_redirect_types() is almost ready.
Thinker K.F. Li <thinker@codemud.net>
parents: 105
diff changeset
14 for attr in attrs]
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
15 rel_children = [(attr, value) for attr, value in attr_value_pairs
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
16 if isinstance(value, relocatable)]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
17 return rel_children
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
18
87
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
19
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
20 ## \brief Travel relocatable descendants.
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
21 #
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
22 # \param cloner is the function to return a clone.
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
23 # \param adjuster is called to adjust the clone.
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
24 # \param visit_log is a dictionary to keep clones.
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
25 #
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
26 def _travel_desc_relocatable(obj, worker, visit_log):
87
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
27 if id(obj) in visit_log:
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
28 return visit_log[id(obj)]
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
29
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
30 result = worker(obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
31 visit_log[id(obj)] = result
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
32
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
33 rel_children = _relocatable_children(obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
34 for attr, value in rel_children:
96
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 94
diff changeset
35 _travel_desc_relocatable(value, worker, visit_log)
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
36 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
37 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
38
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
39
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
40 ## \brief Return name string of a linked class definition item.
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
41 def classdef_name(classdef):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
42 return classdef.classIdx.descriptorIdx.stringDataOff.data
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
43
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
44
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
45 ## \brief Return a map that map type of a object to the list of a DEXFile.
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
46 def dex_type_2_array_attr_map():
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
47 global dex_type_2_array_attr_map
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
48 from paraspace.dexfile import DEXFile, array
98
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 96
diff changeset
49 from paraspace.dex_deptracker import _marker
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 96
diff changeset
50
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 96
diff changeset
51 def skip_marker_type(clazz):
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 96
diff changeset
52 while isinstance(clazz, _marker):
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 96
diff changeset
53 clazz = clazz.back_type
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 96
diff changeset
54 pass
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 96
diff changeset
55 return clazz
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
56
100
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
57 attr_values = [(attr, skip_marker_type(getattr(DEXFile, attr)))
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
58 for attr in dir(DEXFile)]
100
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
59 array_attrs = [(skip_marker_type(value.child_type), attr)
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
60 for attr, value in attr_values
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
61 if isinstance(value, array)]
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
62 type_2_attr = dict(array_attrs)
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
63
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
64 dex_type_2_array_attr_map = lambda: type_2_attr
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
65
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
66 return type_2_attr
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
67
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
68
105
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 104
diff changeset
69 _saved_dex_type_2_array_attr_map = dex_type_2_array_attr_map
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 104
diff changeset
70
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 104
diff changeset
71
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
72 ## \brief Append a object to appropriate list of a DEXFile object.
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
73 #
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
74 # Skip the object if found no appropriate list.
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
75 #
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
76 def dex_append_obj_list(dex, obj):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
77 from paraspace.dex_deptracker import _dex_tree_get_child
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
78 from paraspace.dex_deptracker import _dex_tree_set_child
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
79
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
80 type_2_attr = dex_type_2_array_attr_map()
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
81 try:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
82 attr = type_2_attr[obj.__class__]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
83 except KeyError:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
84 return
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
85
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
86 array = getattr(dex, attr)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
87 array.items.append(obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
89 count_name = array.count_name
96
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 94
diff changeset
90 if count_name:
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 94
diff changeset
91 count = _dex_tree_get_child(dex, count_name)
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 94
diff changeset
92 _dex_tree_set_child(dex, count_name, count + 1)
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 94
diff changeset
93 pass
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
94 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
95
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
96
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
97 ## \brief Clone a composite object.
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
98 #
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
99 # \param dex is the DEXFile that the composite object is cloning for.
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
100 # \param comobj is composite object that is cloning.
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
101 #
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
102 def _clone_composite(dex, comobj):
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
103 from copy import copy
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
104 from paraspace.dexfile import _DEX_StringDataItem, _DEX_StringId
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
105 from paraspace.dexfile import _DEX_TypeId
105
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 104
diff changeset
106 from paraspace.dex_deptracker import _dex_tree_set_child
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 104
diff changeset
107 from paraspace.dex_deptracker import _dex_tree_get_child
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
108
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
109 visit_log = {}
87
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
110
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
111 def cloner(obj):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
112 clone = copy(obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
113 return clone
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
114
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
115 def relink_dependencies(clone):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
116 rel_children = _relocatable_children(clone)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
117 for attr, value in rel_children:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
118 clone_value = visit_log[id(value)]
105
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 104
diff changeset
119 _dex_tree_set_child(clone, attr, clone_value)
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
120 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
121 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
122
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
123 def merge_unique_strdata():
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
124 strdatas = [(obj_id, obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
125 for obj_id, obj in visit_log.items()
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
126 if isinstance(obj, _DEX_StringDataItem)]
99
3898711adb2c Make sure string data list is consistent.
Thinker K.F. Li <thinker@codemud.net>
parents: 98
diff changeset
127 dex_str_2_strdata = dict([(strdata.data.data, strdata)
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
128 for strdata in dex.stringDataItems.items])
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
129 for obj_id, strdata in strdatas:
99
3898711adb2c Make sure string data list is consistent.
Thinker K.F. Li <thinker@codemud.net>
parents: 98
diff changeset
130 if strdata.data.data in dex_str_2_strdata:
3898711adb2c Make sure string data list is consistent.
Thinker K.F. Li <thinker@codemud.net>
parents: 98
diff changeset
131 visit_log[obj_id] = dex_str_2_strdata[strdata.data.data]
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
132 else:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
133 dex_append_obj_list(dex, strdata)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
134 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
135 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
136 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
137
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
138 def merge_unique_strid():
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
139 strids = [(obj_id, obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
140 for obj_id, obj in visit_log.items()
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
141 if isinstance(obj, _DEX_StringId)]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
142
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
143 for obj_id, strid in strids:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
144 relink_dependencies(strid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
145 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
146
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
147 strdata_2_strid = dict([(strid.stringDataOff, strid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
148 for strid in dex.stringIds.items])
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
149 for obj_id, strid in strids:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
150 if strid.stringDataOff in strdata_2_strid:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
151 visit_log[obj_id] = strdata_2_strid[strid.stringDataOff]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
152 else:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
153 dex_append_obj_list(dex, strid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
154 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
155 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
156 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
157
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
158 def merge_unique_typeid():
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
159 typeids = [(obj_id, obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
160 for obj_id, obj in visit_log.items()
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
161 if isinstance(obj, _DEX_TypeId)]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
162
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
163 for obj_id, typeid in typeids:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
164 relink_dependencies(typeid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
165 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
166
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
167 strid_2_typeid = dict([(typeid.descriptorIdx, typeid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
168 for typeid in dex.typeIds.items])
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
169 for obj_id, typeid in typeids:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
170 if typeid.descriptorIdx in strid_2_typeid:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
171 visit_log[obj_id] = strid_2_typeid[typeid.descriptorIdx]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
172 else:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
173 dex_append_obj_list(dex, typeid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
174 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
175 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
176 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
177
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
178 _travel_desc_relocatable(comobj, cloner, visit_log)
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
179
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
180 merge_unique_strdata()
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
181 merge_unique_strid()
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
182 merge_unique_typeid()
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
183
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
184 for obj in visit_log.values():
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
185 if isinstance(obj, (_DEX_StringDataItem,
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
186 _DEX_StringId,
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
187 _DEX_TypeId)):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
188 continue
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
189 relink_dependencies(obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
190 dex_append_obj_list(dex, obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
191 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
192
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
193 clone = visit_log[id(comobj)]
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
194 return clone
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
195
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
196
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
197 ## \brief Clone a class definition item
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
198 #
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
199 # \param dex is the DEXFile that clazz is cloning for.
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
200 # \param clazz is the class definition item that is cloning.
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
201 # \return the cloning _DEX_ClassDef.
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
202 #
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
203 def _clone_classdef(dex, clazz):
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
204 from paraspace.dexfile import DEXFile_linked, _DEX_ClassDef
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
205
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
206 def has_classdef(clazz):
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
207 classname = DEXFile_linked.get_classdef_name(clazz)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
208 try:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
209 dex.find_class_name(classname)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
210 except ValueError:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
211 return False
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
212 return True
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
213
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
214 assert isinstance(clazz, _DEX_ClassDef)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
215
96
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 94
diff changeset
216 if has_classdef(clazz):
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
217 raise RuntimeError, \
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
218 'clone a class \'%s\'that is already in the DEXFile' % \
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
219 classdef_name(clazz)
94
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
220
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
221 clone = _clone_composite(dex, clazz)
94
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
222 return clone
87
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
223
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
224
94
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
225 ## \brief Clone a class definition and insert into a DEXFile.
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
226 #
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
227 # This function clone a class definition from a linked DEXFile and
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
228 # insert it into another one.
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
229 #
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
230 # \param dex is a DEXFile_linked to insert the clone.
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
231 # \param class_def is a class definition going to be cloned.
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
232 #
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
233 def dexfile_insert_class(dex, classdef):
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
234 clone = _clone_classdef(dex, classdef)
94
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
235 return clone
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
236
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
237
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
238 ## \brief Collect info of classes mentioned by the code of given class.
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
239 def _find_class_relative(dex, classdef):
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
240 def classify_typeids_defined(dex, typeids):
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
241 classdefs = []
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
242 undef_typeids = []
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
243 for typeid in typeids:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
244 try:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
245 classdef = dex.find_class_typeid(typeid)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
246 except ValueError:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
247 undef_typeids.append(typeid)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
248 else:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
249 classdefs.append(classdef)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
250 pass
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
251 pass
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
252 return classdefs, undef_typeids
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
253
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
254 typeidxs = collect_typeidxs_mentioned_by_class(dex, classdef)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
255 typeids = [dex.find_typeid_idx(typeidx)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
256 for typeidx in typeidxs]
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
257
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
258 classdefs, typeids = classify_typeids_defined(dex, typeids)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
259
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
260 return classdefs, typeids
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
261
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
262
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
263 def dexfile_insert_classdefs(dex_dst, dex_src, classdefs):
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
264 clones = [dexfile_insert_class(dex_dst, classdef)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
265 for classdef in classdefs]
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
266 return clones
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
267
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
268
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
269 ## \brief Clone and insert a _DEX_TypeId to another DEXFile_linked.
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
270 #
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
271 # \param dex_dst is a DEXFile_linked where the cloning one is inserted.
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
272 # \param dex_src is a DEXFile_linked where the cloned one is from.
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
273 # \param typeid is a _DEX_TypeId that is cloned.
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
274 # \return the cloning _DEX_TypeId.
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
275 #
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
276 def dexfile_insert_typeid(dex_dst, dex_src, typeid):
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
277 from paraspace.dexfile import _DEX_TypeId, DEXFile_linked
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
278
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
279 assert isinstance(typeid, _DEX_TypeId)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
280
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
281 cloning = _clone_composite(dex_dst, typeid)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
282
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
283 methodids = dex_src.find_methodids_typeid(dex_src, typeid)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
284 for methodid in methodids:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
285 _clone_composite(dex_dst, methodid)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
286 pass
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
287
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
288 return cloning
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
289
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
290
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
291 ## \brief Clone and insert a list of _DEX_TypeId objects to a DEXFile_linked.
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
292 def dexfile_insert_typeids(dex_dst, dex_src, typeids):
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
293 clones = [dexfile_insert_typeid(dex_dst, dex_src, typeid)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
294 for typeid in typeids]
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
295 return clones
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
296
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
297
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
298 ## \brief Collects relative type IDs and classes definition for given class.
114
867184e01852 Change interface of dexfile_insert_classdefs_relative
Thinker K.F. Li <thinker@codemud.net>
parents: 113
diff changeset
299 def collect_classdefs_relative(dex, classdefs):
867184e01852 Change interface of dexfile_insert_classdefs_relative
Thinker K.F. Li <thinker@codemud.net>
parents: 113
diff changeset
300 rel_classdefs = set(classdefs)
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
301 rel_typeids = set()
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
302
114
867184e01852 Change interface of dexfile_insert_classdefs_relative
Thinker K.F. Li <thinker@codemud.net>
parents: 113
diff changeset
303 classdef_queue = list(classdefs)
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
304 while classdef_queue:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
305 cur_classdef = classdef_queue.pop(0)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
306
114
867184e01852 Change interface of dexfile_insert_classdefs_relative
Thinker K.F. Li <thinker@codemud.net>
parents: 113
diff changeset
307 classdefs, typeids = _find_class_relative(dex, cur_classdef)
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
308 rel_typeids.update(typeids)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
309 new_classdefs = list(set(classdefs) - rel_classdefs)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
310 classdef_queue = classdef_queue + new_classdefs
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
311 rel_classdefs.update(new_classdefs)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
312 pass
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
313 return rel_classdefs, rel_typeids
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
314
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
315
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
316 ## \brief Clone and insert given and relative classes into another DEXFile.
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
317 #
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
318 # \param dex_dst is a DEXFile_linked where the class will be inserted.
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
319 # \param dex_src is a DEXFile_linked where the cloned class is from.
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
320 # \param classdef is a _DEX_ClassDef that will be cloned.
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
321 # \return a vector of a list of classdefs and a list of typeids.
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
322 #
114
867184e01852 Change interface of dexfile_insert_classdefs_relative
Thinker K.F. Li <thinker@codemud.net>
parents: 113
diff changeset
323 def dexfile_insert_classdefs_relative(dex_dst, dex_src, classdefs):
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
324 from paraspace.dexfile import DEXFile_linked
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
325
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
326 def classdef_not_in_dst(classdef):
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
327 classname = DEXFile_linked.get_classdef_name(classdef)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
328 try:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
329 dex_dst.find_class_name(classname)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
330 except ValueError:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
331 return True
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
332 return False
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
333
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
334 def typeid_not_in_dst(typeid):
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
335 typename = DEXFile_linked.get_typeid_name(typeid)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
336 try:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
337 dex_dst.find_typeid_name(typename)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
338 except ValueError:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
339 return True
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
340 return False
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
341
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
342 relative_classdefs, relative_typeids = \
114
867184e01852 Change interface of dexfile_insert_classdefs_relative
Thinker K.F. Li <thinker@codemud.net>
parents: 113
diff changeset
343 collect_classdefs_relative(dex_src, classdefs)
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
344
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
345 for classdef in relative_classdefs:
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
346 if classdef_not_in_dst(classdef):
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
347 continue
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
348 raise ValueError, '%s is already in DEX %s: can not insert it' % \
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
349 (repr(classdef), repr(dex_dst))
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
350
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
351 inserting_typeids = filter(typeid_not_in_dst, relative_typeids)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
352
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
353 cloning_classdefs = \
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
354 dexfile_insert_classdefs(dex_dst, dex_src, relative_classdefs)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
355 cloning_typeids = \
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
356 dexfile_insert_typeids(dex_dst, dex_src, inserting_typeids)
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
357
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
358 return cloning_classdefs, cloning_typeids
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
359
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
360
108
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
361 ## \brief Redirect types and methods for the code of given method.
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
362 def method_redirect_typeidxs(dex, method, typeidxs_redir, methods_redir):
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
363 from paraspace.dalvik_opcodes import decode_insn_blk, all_opcodes
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
364 from paraspace.dalvik_opcodes import encode_opcode_vectors
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
365 from paraspace.dexfile import DEXFile_linked
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
366
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
367 if not method.codeOffRef.is_true:
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
368 return
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
369
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
370 code = method.codeOffRef.value
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
371 insns_blk = code.insns.data
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
372 op_vectors = decode_insn_blk(insns_blk)
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
373
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
374 def redirect(opcode, args):
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
375 if opcode == all_opcodes.OP_NEW_INSTANCE:
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
376 typeidx = args[1]
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
377 if typeidx in typeidxs_redir:
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
378 to_type = typeidxs_redir[typeidx]
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
379 return opcode, (args[0], to_type)
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
380 pass
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
381 elif opcode in (all_opcodes.OP_INVOKE_DIRECT,
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
382 all_opcodes.OP_INVOKE_VIRTUAL,
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
383 all_opcodes.OP_INVOKE_SUPER,
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
384 all_opcodes.OP_INVOKE_STATIC,
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
385 all_opcodes.OP_INVOKE_INTERFACE):
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
386 methodidx = args[2]
108
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
387 if methodidx not in methods_redir:
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
388 return opcode, args
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
389
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
390 return opcode, (args[0], args[1], methods_redir[methodidx],
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
391 args[3], args[4], args[5], args[6])
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
392 elif opcode in (all_opcodes.OP_INVOKE_VIRTUAL_RANGE,
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
393 all_opcodes.OP_INVOKE_DIRECT_RANGE,
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
394 all_opcodes.OP_INVOKE_SUPER_RANGE,
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
395 all_opcodes.OP_INVOKE_STATIC_RANGE,
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
396 all_opcodes.OP_INVOKE_INTERFACE_RANGE):
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
397 methodidx = args[1]
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
398 if methodidx not in methods_redir:
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
399 return opcode, args
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
400
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
401 return opcode, (args[0], methodidx, args[2])
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
402 return opcode, args
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
403
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
404 new_op_vectors = [redirect(opcode, args) for opcode, args in op_vectors]
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
405 new_insns_blk = encode_opcode_vectors(new_op_vectors)
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
406
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
407 code.insns.data = new_insns_blk
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
408 pass
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
409
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
410
108
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
411 ## \brief Make a map for methods from source type ID to ones from desinate.
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
412 def make_redir_classes_methods_map(dex_src, typeid_src,
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
413 dex_dst, typeid_dst):
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
414 from paraspace.dexfile import DEXFile_linked
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
415
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
416 methods_src = [(idx, methodid)
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
417 for idx, methodid in enumerate(dex_src.methodIds.items)
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
418 if methodid.classIdx == typeid_src]
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
419
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
420 def make_map_methodid(methodid_src):
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
421 name = DEXFile_linked.get_methodid_name(methodid_src)
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
422 proto = methodid_src.protoIdx
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
423 try:
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
424 methodid_dst = \
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
425 dex_dst.find_methodid_name_proto(name, proto, typeid_dst)
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
426 except ValueError:
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
427 return -1
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
428 methodidx_dst = dex_dst.get_idx_methodid(methodid_dst)
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
429 return methodidx_dst
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
430
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
431 methods_map = [(methodidx_src, make_map_methodid(methodid_src))
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
432 for methodidx_src, methodid_src in methods_src]
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
433 methods_map = [(methodidx_src, methodidx_dst)
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
434 for methodidx_src, methodidx_dst in methods_map
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
435 if methodidx_dst != -1]
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
436 methods_map = dict(methods_map)
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
437 return methods_map
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
438
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
439
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
440 ## \brief Redirect types and methods mentioned in the code of a class.
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
441 #
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
442 # For code of given class definition, Every mentions of types and
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
443 # methods are rewrote to types and methods according typeidxs_redir and
108
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
444 # methods_redir respectively.
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
445 #
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
446 # \param dex is a DEXFile_linked.
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
447 # \param classdef is a class definition.
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
448 # \param typeidxs_redir is a map of types.
108
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
449 # \param methods_redir is a map of methods.
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
450 #
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
451 def class_redirect_types(dex, classdef, typeidxs_redir, methods_redir):
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
452 if not classdef.classDataOffRef.is_true:
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
453 return
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
454
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
455 classdata = classdef.classDataOffRef.value
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
456 for method in classdata.directMethods.items:
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
457 method_redirect_typeidxs(dex, method, typeidxs_redir, methods_redir)
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
458 pass
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
459 for method in classdata.virtualMethods.items:
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
460 method_redirect_typeidxs(dex, method, typeidxs_redir, methods_redir)
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
461 pass
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
462 pass
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
463
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
464
108
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
465 ## \brief Make a map to map methods from source types to destinate types.
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
466 #
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
467 # This function create a map to map methods from source types to
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
468 # methods from destinate types in \ref typeidxs_redir.
108
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
469 #
119
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents: 115
diff changeset
470 # \param dex_src is a DEXFile_linked that owns source types.
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents: 115
diff changeset
471 # \param dex_dst is a DEXFile_linked that owns destinate types.
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
472 # \param typeidxs_redir is a map of type indices for redirecting types.
108
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
473 # \return a map of method indices.
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
474 #
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
475 def make_methodidxs_redir_map(dex_src, dex_dst, typeidxs_redir):
108
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
476 methods_map = {}
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
477 for typeidx_src, typeidx_dst in typeidxs_redir.items():
113
ee13c86d84f2 Let user specify redirection map for methods
Thinker K.F. Li <thinker@codemud.net>
parents: 112
diff changeset
478 typeid_src = dex_src.find_typeid_idx(typeidx_src)
ee13c86d84f2 Let user specify redirection map for methods
Thinker K.F. Li <thinker@codemud.net>
parents: 112
diff changeset
479 typeid_dst = dex_dst.find_typeid_idx(typeidx_dst)
ee13c86d84f2 Let user specify redirection map for methods
Thinker K.F. Li <thinker@codemud.net>
parents: 112
diff changeset
480 class_methods_map = make_redir_classes_methods_map(dex_src,
108
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
481 typeid_src,
113
ee13c86d84f2 Let user specify redirection map for methods
Thinker K.F. Li <thinker@codemud.net>
parents: 112
diff changeset
482 dex_dst,
108
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
483 typeid_dst)
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
484 methods_map.update(class_methods_map)
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
485 pass
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
486 return methods_map
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
487
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
488
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
489 ## \biref Redirect types of all code in given DEXFile_linked.
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
490 def dexfile_redirect_types(dex, typeidxs_redir, methods_redir,
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
491 excludes=set([])):
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
492 for classdef in dex.classDefs.items:
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
493 typeid = classdef.classIdx
105
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 104
diff changeset
494 idx = dex.get_idx_typeid(typeid)
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
495 if idx in excludes:
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
496 continue
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
497 class_redirect_types(dex, classdef, typeidxs_redir, methods_redir)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
498 pass
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
499 pass
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
500
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
501
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
502 ## \brief Redirect types for code of types specified by given indices.
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
503 def dexfile_redirect_types_typeidxs(dex, typeidxs_redir, methodidxs_redir,
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
504 typeidxs):
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
505 typeidxs = set(typeidxs)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
506 for classdef in dex.classDefs.items:
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
507 typeid = classdef.classIdx
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
508 idx = dex.get_idx_typeid(typeid)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
509 if idx not in typeidxs:
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
510 continue
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
511 class_redirect_types(dex, classdef, typeidxs_redir, methodidxs_redir)
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
512 pass
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
513 pass
109
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
514
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
515
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
516 ## \brief Collect all type indices mentioned in the code of given method.
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
517 #
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
518 # \param method is a \ref _DEX_Method.
110
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
519 # \return a list of type indices mentioned in the code.
109
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
520 #
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
521 def collect_typeidxs_in_method(dex, method):
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
522 from paraspace.dexfile import _DEX_Method, DEXFile_linked
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
523 from paraspace.dalvik_opcodes import decode_insn_blk, all_opcodes
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
524 from itertools import chain
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
525
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
526 assert isinstance(method, _DEX_Method)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
527
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
528 def get_typeidx_methodidx(methodidx):
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
529 methodid = dex.find_methodid_idx(methodidx)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
530 method_typeid = methodid.classIdx
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
531 method_typeidx = dex.get_idx_typeid(method_typeid)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
532 return method_typeidx
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
533
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
534 def collect_types_in_op_vector(op_vector):
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
535 code, args = op_vector
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
536
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
537 if code == all_opcodes.OP_NEW_INSTANCE:
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
538 return (args[1],)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
539
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
540 if code in (all_opcodes.OP_INVOKE_DIRECT,
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
541 all_opcodes.OP_INVOKE_VIRTUAL,
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
542 all_opcodes.OP_INVOKE_SUPER,
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
543 all_opcodes.OP_INVOKE_STATIC,
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
544 all_opcodes.OP_INVOKE_INTERFACE):
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
545 methodidx = args[2]
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
546 method_typeidx = get_typeidx_methodidx(methodidx)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
547 return (method_typeidx,)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
548
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
549 if code in (all_opcodes.OP_INVOKE_VIRTUAL_RANGE,
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
550 all_opcodes.OP_INVOKE_DIRECT_RANGE,
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
551 all_opcodes.OP_INVOKE_SUPER_RANGE,
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
552 all_opcodes.OP_INVOKE_STATIC_RANGE,
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
553 all_opcodes.OP_INVOKE_INTERFACE_RANGE):
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
554 methodidx = args[1]
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
555 method_typeidx = get_typeidx_methodidx(methodidx)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
556 return (method_typeidx,)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
557
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
558 return ()
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
559
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
560 code_blk = DEXFile_linked.get_code_block_method(method)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
561 op_vectors = decode_insn_blk(code_blk)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
562 types_insns = [collect_types_in_op_vector(op_vector)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
563 for op_vector in op_vectors]
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
564 typeidxs = list(chain(*types_insns))
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
565
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
566 return typeidxs
110
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
567
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
568
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
569 ## \brief Collect all type indices mentioned by the code of given class.
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
570 def collect_typeidxs_mentioned_by_class(dex, classdef):
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
571 from paraspace.dexfile import DEXFile_linked
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
572
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
573 assert isinstance(dex, DEXFile_linked)
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
574
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
575 typeidxs = set()
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
576 methods = DEXFile_linked.get_methods_classdef(classdef)
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
577 for method in methods:
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
578 method_typeidxs = collect_typeidxs_in_method(dex, method)
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
579 typeidxs.update(method_typeidxs)
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
580 pass
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
581
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
582 return list(typeidxs)
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
583
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
584
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
585 ## \brief Make a mapping for type indices of injection.
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
586 def make_typeidxs_map_after_injection(dex_dst, dex_src,
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
587 relative_classdefs,
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
588 relative_typeids):
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
589 from paraspace.dexfile import DEXFile_linked
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
590
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
591 def map_src_dst_typeid(typeid):
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
592 idx_src = dex_src.get_idx_typeid(typeid)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
593 typename = DEXFile_linked.get_typeid_name(typeid)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
594 typeid_dst = dex_dst.find_typeid_name(typename)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
595 idx_dst = dex_dst.get_idx_typeid(typeid_dst)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
596 dex_dst.find_typeid_idx(idx_dst)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
597 return idx_src, idx_dst
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
598
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
599 def map_src_dst_classdef(classdef):
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
600 typeid = classdef.classIdx
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
601 idx_src, idx_dst = map_src_dst_typeid(typeid)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
602 return idx_src, idx_dst
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
603
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
604 typeidxs_classdefs = [map_src_dst_classdef(classdef)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
605 for classdef in relative_classdefs]
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
606 typeidxs_typeids = [map_src_dst_typeid(typeid)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
607 for typeid in relative_typeids]
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
608 typeidxs_map = dict(typeidxs_classdefs + typeidxs_typeids)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
609 return typeidxs_map
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
610
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
611
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
612 ## \brief Redirect code for methods of injected classes.
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
613 def redirect_injected_code(dex_dst, dex_src, classdefs):
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
614 relative_classdefs, relative_typeids = \
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
615 collect_classdefs_relative(dex_src, classdefs)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
616
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
617 typeidxs_redir = \
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
618 make_typeidxs_map_after_injection(dex_dst, dex_src, \
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
619 relative_classdefs, \
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
620 relative_typeids)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
621 methodidxs_redir = \
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
622 make_methodidxs_redir_map(dex_src, dex_dst, typeidxs_redir)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
623
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
624 dexfile_redirect_types_typeidxs(dex_dst, typeidxs_redir,
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
625 methodidxs_redir,
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
626 typeidxs_redir.values())
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
627 pass
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
628
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
629
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
630 ## \brief Inject classes and relative information to a DEX file.
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
631 #
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
632 # \param dex_dst is a DEXFile_linked where to inject classes.
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
633 # \param dex_src is a DEXFile_linked where the class is from.
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
634 # \param classdefs is a list of _DEX_ClassDef instances from \ref dex_src.
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
635 # \return a list of _DEX_ClassDef instances for injection.
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
636 #
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
637 def inject_classdefs(dex_dst, dex_src, classdefs):
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
638 from paraspace.dexfile import DEXFile_linked
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
639
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
640 assert isinstance(classdefs, (list, tuple))
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
641 assert isinstance(dex_dst, DEXFile_linked)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
642 assert isinstance(dex_src, DEXFile_linked)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
643
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
644 injected_classdefs, injected_typeids = \
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
645 dexfile_insert_classdefs_relative(dex_dst, dex_src, classdefs)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
646 redirect_injected_code(dex_dst, dex_src, classdefs)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
647 return injected_classdefs
131
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
648
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
649
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
650 ## \brief Inject and redirect a _DEX_ClassDef from one linked to another.
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
651 #
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
652 # The _DEX_ClassDef given by inj_classname would be inserted to dst_linked,
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
653 # and redirect all invoking of type, given by redir_classname, to
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
654 # the injected one.
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
655 #
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
656 def inject_redir(src_linked, inj_classname,
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
657 dst_linked, redir_classname, decls):
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
658 from paraspace.dex_deptracker import dex_sort_sorted_arrays
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
659 from paraspace.dex_deptracker import restore_dependencies
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
660
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
661 inj_classdef = src_linked.find_class_name(inj_classname)
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
662 injected_classdefs = inject_classdefs(dst_linked, src_linked,
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
663 [inj_classdef])
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
664
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
665 redir_typeid = dst_linked.find_typeid_name(redir_classname)
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
666 redir_typeidx = dst_linked.get_idx_typeid(redir_typeid)
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
667 inj_typeid = dst_linked.find_typeid_name(inj_classname)
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
668 inj_typeidx = dst_linked.get_idx_typeid(inj_typeid)
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
669 typeidxs_redir = {redir_typeidx: inj_typeidx}
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
670
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
671 methodidxs_redir = \
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
672 make_methodidxs_redir_map(dst_linked, dst_linked, typeidxs_redir)
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
673
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
674 injected_typeidxs = [dst_linked.get_idx_classdef(classdef)
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
675 for classdef in injected_classdefs]
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
676 dexfile_redirect_types(dst_linked, typeidxs_redir, methodidxs_redir,
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
677 excludes=injected_typeidxs)
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
678
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
679 dex_sort_sorted_arrays(dst_linked)
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
680 restore_dependencies(dst_linked, decls)
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
681 pass