annotate paraspace/injection.py @ 134:3dee4d929e1f

Wrap _DEX_TypeList to _DEX_TypeList_align before append to list. - Fix the issue described by map_verify_error_test(). - When inject a _DEX_ClassDef, dexfile_insert_classdefs_relative() should also inject related _DEX_TypeList. - But, dex_append_obj_list() does not known that _DEX_TypeList should be wrapped to to _DEX_TypeList_align and appended to DEXFile.typeLists. We add a special case in dex_append_obj_list() to wrap _DEX_TypeList before trying to insert to a list.
author Thinker K.F. Li <thinker@codemud.net>
date Tue, 09 Aug 2011 17:23:01 +0800
parents 044bfc415577
children b488ca519709
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
134
3dee4d929e1f Wrap _DEX_TypeList to _DEX_TypeList_align before append to list.
Thinker K.F. Li <thinker@codemud.net>
parents: 131
diff changeset
79 from paraspace.dexfile import _DEX_TypeList, _DEX_TypeList_align
3dee4d929e1f Wrap _DEX_TypeList to _DEX_TypeList_align before append to list.
Thinker K.F. Li <thinker@codemud.net>
parents: 131
diff changeset
80
3dee4d929e1f Wrap _DEX_TypeList to _DEX_TypeList_align before append to list.
Thinker K.F. Li <thinker@codemud.net>
parents: 131
diff changeset
81 if isinstance(obj, _DEX_TypeList):
3dee4d929e1f Wrap _DEX_TypeList to _DEX_TypeList_align before append to list.
Thinker K.F. Li <thinker@codemud.net>
parents: 131
diff changeset
82 wrapper = _DEX_TypeList_align()
3dee4d929e1f Wrap _DEX_TypeList to _DEX_TypeList_align before append to list.
Thinker K.F. Li <thinker@codemud.net>
parents: 131
diff changeset
83 wrapper.value = obj
3dee4d929e1f Wrap _DEX_TypeList to _DEX_TypeList_align before append to list.
Thinker K.F. Li <thinker@codemud.net>
parents: 131
diff changeset
84 obj = wrapper
3dee4d929e1f Wrap _DEX_TypeList to _DEX_TypeList_align before append to list.
Thinker K.F. Li <thinker@codemud.net>
parents: 131
diff changeset
85 pass
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
86
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
87 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
88 try:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
89 attr = type_2_attr[obj.__class__]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
90 except KeyError:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
91 return
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
92
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
93 array = getattr(dex, attr)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
94 array.items.append(obj)
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 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
97 if count_name:
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 94
diff changeset
98 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
99 _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
100 pass
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
101 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
102
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
103
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
104 ## \brief Clone a composite object.
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
105 #
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
106 # \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
107 # \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
108 #
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
109 def _clone_composite(dex, comobj):
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
110 from copy import copy
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
111 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
112 from paraspace.dexfile import _DEX_TypeId
105
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 104
diff changeset
113 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
114 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
115
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
116 visit_log = {}
87
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
117
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
118 def cloner(obj):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
119 clone = copy(obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
120 return clone
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
121
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
122 def relink_dependencies(clone):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
123 rel_children = _relocatable_children(clone)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
124 for attr, value in rel_children:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
125 clone_value = visit_log[id(value)]
105
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 104
diff changeset
126 _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
127 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
128 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
129
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
130 def merge_unique_strdata():
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
131 strdatas = [(obj_id, obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
132 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
133 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
134 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
135 for strdata in dex.stringDataItems.items])
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
136 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
137 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
138 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
139 else:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
140 dex_append_obj_list(dex, strdata)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
141 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
142 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
143 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
144
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
145 def merge_unique_strid():
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
146 strids = [(obj_id, obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
147 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
148 if isinstance(obj, _DEX_StringId)]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
149
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
150 for obj_id, strid in strids:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
151 relink_dependencies(strid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
152 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
153
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
154 strdata_2_strid = dict([(strid.stringDataOff, strid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
155 for strid in dex.stringIds.items])
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
156 for obj_id, strid in strids:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
157 if strid.stringDataOff in strdata_2_strid:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
158 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
159 else:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
160 dex_append_obj_list(dex, strid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
161 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
162 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
163 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
164
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
165 def merge_unique_typeid():
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
166 typeids = [(obj_id, obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
167 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
168 if isinstance(obj, _DEX_TypeId)]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
169
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
170 for obj_id, typeid in typeids:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
171 relink_dependencies(typeid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
172 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
173
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
174 strid_2_typeid = dict([(typeid.descriptorIdx, typeid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
175 for typeid in dex.typeIds.items])
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
176 for obj_id, typeid in typeids:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
177 if typeid.descriptorIdx in strid_2_typeid:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
178 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
179 else:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
180 dex_append_obj_list(dex, typeid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
181 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
182 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
183 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
184
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
185 _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
186
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
187 merge_unique_strdata()
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
188 merge_unique_strid()
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
189 merge_unique_typeid()
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
190
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
191 for obj in visit_log.values():
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
192 if isinstance(obj, (_DEX_StringDataItem,
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
193 _DEX_StringId,
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
194 _DEX_TypeId)):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
195 continue
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
196 relink_dependencies(obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
197 dex_append_obj_list(dex, obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
198 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
199
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
200 clone = visit_log[id(comobj)]
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
201 return clone
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
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
204 ## \brief Clone a class definition item
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 # \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
207 # \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
208 # \return the cloning _DEX_ClassDef.
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
209 #
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
210 def _clone_classdef(dex, clazz):
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
211 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
212
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
213 def has_classdef(clazz):
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
214 classname = DEXFile_linked.get_classdef_name(clazz)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
215 try:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
216 dex.find_class_name(classname)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
217 except ValueError:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
218 return False
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
219 return True
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
220
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
221 assert isinstance(clazz, _DEX_ClassDef)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
222
96
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 94
diff changeset
223 if has_classdef(clazz):
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
224 raise RuntimeError, \
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
225 '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
226 classdef_name(clazz)
94
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
227
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
228 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
229 return clone
87
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
230
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
231
94
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
232 ## \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
233 #
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
234 # 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
235 # insert it into another one.
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
236 #
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
237 # \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
238 # \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
239 #
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
240 def dexfile_insert_class(dex, classdef):
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
241 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
242 return clone
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
243
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
244
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
245 ## \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
246 def _find_class_relative(dex, classdef):
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
247 def classify_typeids_defined(dex, typeids):
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
248 classdefs = []
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
249 undef_typeids = []
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
250 for typeid in typeids:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
251 try:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
252 classdef = dex.find_class_typeid(typeid)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
253 except ValueError:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
254 undef_typeids.append(typeid)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
255 else:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
256 classdefs.append(classdef)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
257 pass
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
258 pass
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
259 return classdefs, undef_typeids
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
260
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
261 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
262 typeids = [dex.find_typeid_idx(typeidx)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
263 for typeidx in typeidxs]
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
264
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
265 classdefs, typeids = classify_typeids_defined(dex, typeids)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
266
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
267 return classdefs, typeids
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
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
270 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
271 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
272 for classdef in classdefs]
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
273 return clones
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
274
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 ## \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
277 #
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
278 # \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
279 # \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
280 # \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
281 # \return the cloning _DEX_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 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
284 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
285
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
286 assert isinstance(typeid, _DEX_TypeId)
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 cloning = _clone_composite(dex_dst, typeid)
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 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
291 for methodid in methodids:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
292 _clone_composite(dex_dst, methodid)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
293 pass
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
294
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
295 return cloning
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 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
299 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
300 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
301 for typeid in typeids]
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
302 return clones
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
303
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
304
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
305 ## \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
306 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
307 rel_classdefs = set(classdefs)
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
308 rel_typeids = set()
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
309
114
867184e01852 Change interface of dexfile_insert_classdefs_relative
Thinker K.F. Li <thinker@codemud.net>
parents: 113
diff changeset
310 classdef_queue = list(classdefs)
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
311 while classdef_queue:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
312 cur_classdef = classdef_queue.pop(0)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
313
114
867184e01852 Change interface of dexfile_insert_classdefs_relative
Thinker K.F. Li <thinker@codemud.net>
parents: 113
diff changeset
314 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
315 rel_typeids.update(typeids)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
316 new_classdefs = list(set(classdefs) - rel_classdefs)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
317 classdef_queue = classdef_queue + new_classdefs
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
318 rel_classdefs.update(new_classdefs)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
319 pass
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
320 return rel_classdefs, rel_typeids
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
321
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
322
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
323 ## \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
324 #
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
325 # \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
326 # \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
327 # \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
328 # \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
329 #
114
867184e01852 Change interface of dexfile_insert_classdefs_relative
Thinker K.F. Li <thinker@codemud.net>
parents: 113
diff changeset
330 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
331 from paraspace.dexfile import DEXFile_linked
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
332
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
333 def classdef_not_in_dst(classdef):
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
334 classname = DEXFile_linked.get_classdef_name(classdef)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
335 try:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
336 dex_dst.find_class_name(classname)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
337 except ValueError:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
338 return True
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
339 return False
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
340
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
341 def typeid_not_in_dst(typeid):
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
342 typename = DEXFile_linked.get_typeid_name(typeid)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
343 try:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
344 dex_dst.find_typeid_name(typename)
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
345 except ValueError:
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
346 return True
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
347 return False
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
348
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
349 relative_classdefs, relative_typeids = \
114
867184e01852 Change interface of dexfile_insert_classdefs_relative
Thinker K.F. Li <thinker@codemud.net>
parents: 113
diff changeset
350 collect_classdefs_relative(dex_src, classdefs)
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
351
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
352 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
353 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
354 continue
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
355 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
356 (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
357
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
358 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
359
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
360 cloning_classdefs = \
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
361 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
362 cloning_typeids = \
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
363 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
364
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
365 return cloning_classdefs, cloning_typeids
111
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
366
3820379b34e8 Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents: 110
diff changeset
367
108
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
368 ## \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
369 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
370 from paraspace.dalvik_opcodes import decode_insn_blk, all_opcodes
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
371 from paraspace.dalvik_opcodes import encode_opcode_vectors
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
372 from paraspace.dexfile import DEXFile_linked
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 if not method.codeOffRef.is_true:
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
375 return
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
376
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
377 code = method.codeOffRef.value
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
378 insns_blk = code.insns.data
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
379 op_vectors = decode_insn_blk(insns_blk)
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
380
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
381 def redirect(opcode, args):
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
382 if opcode == all_opcodes.OP_NEW_INSTANCE:
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
383 typeidx = args[1]
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
384 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
385 to_type = typeidxs_redir[typeidx]
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
386 return opcode, (args[0], to_type)
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
387 pass
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
388 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
389 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
390 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
391 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
392 all_opcodes.OP_INVOKE_INTERFACE):
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
393 methodidx = args[2]
108
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
394 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
395 return opcode, args
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
396
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
397 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
398 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
399 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
400 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
401 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
402 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
403 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
404 methodidx = args[1]
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
405 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
406 return opcode, args
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
407
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
408 return opcode, (args[0], methodidx, args[2])
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
409 return opcode, args
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
410
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
411 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
412 new_insns_blk = encode_opcode_vectors(new_op_vectors)
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
413
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
414 code.insns.data = new_insns_blk
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
415 pass
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
416
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
417
108
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
418 ## \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
419 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
420 dex_dst, typeid_dst):
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
421 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
422
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
423 methods_src = [(idx, methodid)
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
424 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
425 if methodid.classIdx == typeid_src]
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
426
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
427 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
428 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
429 proto = methodid_src.protoIdx
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
430 try:
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
431 methodid_dst = \
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
432 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
433 except ValueError:
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
434 return -1
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
435 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
436 return methodidx_dst
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
437
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
438 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
439 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
440 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
441 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
442 if methodidx_dst != -1]
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
443 methods_map = dict(methods_map)
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
444 return methods_map
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
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
447 ## \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
448 #
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
449 # 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
450 # 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
451 # methods_redir respectively.
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
452 #
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
453 # \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
454 # \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
455 # \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
456 # \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
457 #
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
458 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
459 if not classdef.classDataOffRef.is_true:
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
460 return
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
461
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
462 classdata = classdef.classDataOffRef.value
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
463 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
464 method_redirect_typeidxs(dex, method, typeidxs_redir, methods_redir)
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
465 pass
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
466 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
467 method_redirect_typeidxs(dex, method, typeidxs_redir, methods_redir)
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
468 pass
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
469 pass
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
470
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
471
108
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
472 ## \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
473 #
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
474 # 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
475 # 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
476 #
119
4f508f84c8a1 Add inject_redir.py example
Thinker K.F. Li <thinker@codemud.net>
parents: 115
diff changeset
477 # \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
478 # \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
479 # \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
480 # \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
481 #
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
482 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
483 methods_map = {}
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
484 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
485 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
486 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
487 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
488 typeid_src,
113
ee13c86d84f2 Let user specify redirection map for methods
Thinker K.F. Li <thinker@codemud.net>
parents: 112
diff changeset
489 dex_dst,
108
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
490 typeid_dst)
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
491 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
492 pass
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
493 return methods_map
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
494
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
495
18be67af7f1e Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
496 ## \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
497 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
498 excludes=set([])):
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
499 for classdef in dex.classDefs.items:
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
500 typeid = classdef.classIdx
105
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 104
diff changeset
501 idx = dex.get_idx_typeid(typeid)
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
502 if idx in excludes:
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
503 continue
115
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
504 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
505 pass
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
506 pass
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
507
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
508
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
509 ## \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
510 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
511 typeidxs):
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
512 typeidxs = set(typeidxs)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
513 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
514 typeid = classdef.classIdx
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
515 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
516 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
517 continue
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
518 class_redirect_types(dex, classdef, typeidxs_redir, methodidxs_redir)
104
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
519 pass
61cef1662035 Redirect types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
520 pass
109
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
521
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
522
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
523 ## \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
524 #
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
525 # \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
526 # \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
527 #
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
528 def collect_typeidxs_in_method(dex, method):
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
529 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
530 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
531 from itertools import chain
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
532
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
533 assert isinstance(method, _DEX_Method)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
534
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
535 def get_typeidx_methodidx(methodidx):
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
536 methodid = dex.find_methodid_idx(methodidx)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
537 method_typeid = methodid.classIdx
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
538 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
539 return method_typeidx
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
540
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
541 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
542 code, args = op_vector
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
543
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
544 if code == all_opcodes.OP_NEW_INSTANCE:
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
545 return (args[1],)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
546
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
547 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
548 all_opcodes.OP_INVOKE_VIRTUAL,
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
549 all_opcodes.OP_INVOKE_SUPER,
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
550 all_opcodes.OP_INVOKE_STATIC,
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
551 all_opcodes.OP_INVOKE_INTERFACE):
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
552 methodidx = args[2]
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
553 method_typeidx = get_typeidx_methodidx(methodidx)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
554 return (method_typeidx,)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
555
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
556 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
557 all_opcodes.OP_INVOKE_DIRECT_RANGE,
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
558 all_opcodes.OP_INVOKE_SUPER_RANGE,
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
559 all_opcodes.OP_INVOKE_STATIC_RANGE,
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
560 all_opcodes.OP_INVOKE_INTERFACE_RANGE):
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
561 methodidx = args[1]
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
562 method_typeidx = get_typeidx_methodidx(methodidx)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
563 return (method_typeidx,)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
564
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
565 return ()
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
566
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
567 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
568 op_vectors = decode_insn_blk(code_blk)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
569 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
570 for op_vector in op_vectors]
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
571 typeidxs = list(chain(*types_insns))
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
572
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 108
diff changeset
573 return typeidxs
110
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
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
576 ## \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
577 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
578 from paraspace.dexfile import DEXFile_linked
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
579
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
580 assert isinstance(dex, DEXFile_linked)
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 typeidxs = set()
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
583 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
584 for method in methods:
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
585 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
586 typeidxs.update(method_typeidxs)
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
587 pass
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
588
6380730a80b4 Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents: 109
diff changeset
589 return list(typeidxs)
115
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
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
592 ## \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
593 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
594 relative_classdefs,
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
595 relative_typeids):
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
596 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
597
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
598 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
599 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
600 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
601 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
602 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
603 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
604 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
605
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
606 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
607 typeid = classdef.classIdx
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
608 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
609 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
610
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
611 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
612 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
613 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
614 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
615 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
616 return typeidxs_map
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
617
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
618
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
619 ## \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
620 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
621 relative_classdefs, relative_typeids = \
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
622 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
623
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
624 typeidxs_redir = \
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
625 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
626 relative_classdefs, \
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
627 relative_typeids)
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
628 methodidxs_redir = \
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
629 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
630
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
631 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
632 methodidxs_redir,
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
633 typeidxs_redir.values())
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
634 pass
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
635
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 ## \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
638 #
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
639 # \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
640 # \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
641 # \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
642 # \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
643 #
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
644 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
645 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
646
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
647 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
648 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
649 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
650
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
651 injected_classdefs, injected_typeids = \
d112c27f657a Add inject_classdefs() for injecting classdef to DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 114
diff changeset
652 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
653 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
654 return injected_classdefs
131
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
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
657 ## \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
658 #
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
659 # 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
660 # 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
661 # the injected one.
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
662 #
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
663 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
664 dst_linked, redir_classname, decls):
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
665 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
666 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
667
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
668 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
669 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
670 [inj_classdef])
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
671
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
672 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
673 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
674 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
675 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
676 typeidxs_redir = {redir_typeidx: inj_typeidx}
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
677
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
678 methodidxs_redir = \
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
679 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
680
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
681 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
682 for classdef in injected_classdefs]
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
683 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
684 excludes=injected_typeidxs)
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
685
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
686 dex_sort_sorted_arrays(dst_linked)
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
687 restore_dependencies(dst_linked, decls)
044bfc415577 Fix issue of data map verification.
Thinker K.F. Li <thinker@codemud.net>
parents: 119
diff changeset
688 pass