Mercurial > paraspace
annotate paraspace/injection.py @ 112:650dcb9c01ee
Remove unused code
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Tue, 02 Aug 2011 13:49:41 +0800 |
parents | 3820379b34e8 |
children | ee13c86d84f2 |
rev | line source |
---|---|
87 | 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 | 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 | 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 | 27 if id(obj) in visit_log: |
28 return visit_log[id(obj)] | |
29 | |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
30 result = worker(obj) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
31 visit_log[id(obj)] = result |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
32 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
33 rel_children = _relocatable_children(obj) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
34 for attr, value in rel_children: |
96
1769e52bdd9d
Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents:
94
diff
changeset
|
35 _travel_desc_relocatable(value, worker, visit_log) |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
36 pass |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
37 pass |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
38 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
39 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
40 ## \brief Return name string of a linked class definition item. |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
41 def classdef_name(classdef): |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
42 return classdef.classIdx.descriptorIdx.stringDataOff.data |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
43 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
44 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
45 ## \brief Return a map that map type of a object to the list of a DEXFile. |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
46 def dex_type_2_array_attr_map(): |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
47 global dex_type_2_array_attr_map |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
48 from paraspace.dexfile import DEXFile, array |
98
c0c127c7b37e
Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents:
96
diff
changeset
|
49 from paraspace.dex_deptracker import _marker |
c0c127c7b37e
Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents:
96
diff
changeset
|
50 |
c0c127c7b37e
Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents:
96
diff
changeset
|
51 def skip_marker_type(clazz): |
c0c127c7b37e
Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents:
96
diff
changeset
|
52 while isinstance(clazz, _marker): |
c0c127c7b37e
Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents:
96
diff
changeset
|
53 clazz = clazz.back_type |
c0c127c7b37e
Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents:
96
diff
changeset
|
54 pass |
c0c127c7b37e
Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents:
96
diff
changeset
|
55 return clazz |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
56 |
100
355986e5cfbd
Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents:
99
diff
changeset
|
57 attr_values = [(attr, skip_marker_type(getattr(DEXFile, attr))) |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
58 for attr in dir(DEXFile)] |
100
355986e5cfbd
Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents:
99
diff
changeset
|
59 array_attrs = [(skip_marker_type(value.child_type), attr) |
355986e5cfbd
Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents:
99
diff
changeset
|
60 for attr, value in attr_values |
355986e5cfbd
Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents:
99
diff
changeset
|
61 if isinstance(value, array)] |
355986e5cfbd
Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents:
99
diff
changeset
|
62 type_2_attr = dict(array_attrs) |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
63 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
64 dex_type_2_array_attr_map = lambda: type_2_attr |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
65 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
66 return type_2_attr |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
67 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
68 |
105
f14c32108164
Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents:
104
diff
changeset
|
69 _saved_dex_type_2_array_attr_map = dex_type_2_array_attr_map |
f14c32108164
Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents:
104
diff
changeset
|
70 |
f14c32108164
Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents:
104
diff
changeset
|
71 |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
72 ## \brief Append a object to appropriate list of a DEXFile object. |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
73 # |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
74 # Skip the object if found no appropriate list. |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
75 # |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
76 def dex_append_obj_list(dex, obj): |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
77 from paraspace.dex_deptracker import _dex_tree_get_child |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
78 from paraspace.dex_deptracker import _dex_tree_set_child |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
79 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
80 type_2_attr = dex_type_2_array_attr_map() |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
81 try: |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
82 attr = type_2_attr[obj.__class__] |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
83 except KeyError: |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
84 return |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
85 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
86 array = getattr(dex, attr) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
87 array.items.append(obj) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
88 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
89 count_name = array.count_name |
96
1769e52bdd9d
Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents:
94
diff
changeset
|
90 if count_name: |
1769e52bdd9d
Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents:
94
diff
changeset
|
91 count = _dex_tree_get_child(dex, count_name) |
1769e52bdd9d
Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents:
94
diff
changeset
|
92 _dex_tree_set_child(dex, count_name, count + 1) |
1769e52bdd9d
Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents:
94
diff
changeset
|
93 pass |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
94 pass |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
95 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
96 |
111
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
97 ## \brief Clone a composite object. |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
98 # |
111
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
99 # \param dex is the DEXFile that the composite object is cloning for. |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
100 # \param comobj is composite object that is cloning. |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
101 # |
111
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
102 def _clone_composite(dex, comobj): |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
103 from copy import copy |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
104 from paraspace.dexfile import _DEX_StringDataItem, _DEX_StringId |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
105 from paraspace.dexfile import _DEX_TypeId |
105
f14c32108164
Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents:
104
diff
changeset
|
106 from paraspace.dex_deptracker import _dex_tree_set_child |
f14c32108164
Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents:
104
diff
changeset
|
107 from paraspace.dex_deptracker import _dex_tree_get_child |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
108 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
109 visit_log = {} |
87 | 110 |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
111 def cloner(obj): |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
112 clone = copy(obj) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
113 return clone |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
114 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
115 def relink_dependencies(clone): |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
116 rel_children = _relocatable_children(clone) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
117 for attr, value in rel_children: |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
118 clone_value = visit_log[id(value)] |
105
f14c32108164
Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents:
104
diff
changeset
|
119 _dex_tree_set_child(clone, attr, clone_value) |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
120 pass |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
121 pass |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
122 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
123 def merge_unique_strdata(): |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
124 strdatas = [(obj_id, obj) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
125 for obj_id, obj in visit_log.items() |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
126 if isinstance(obj, _DEX_StringDataItem)] |
99
3898711adb2c
Make sure string data list is consistent.
Thinker K.F. Li <thinker@codemud.net>
parents:
98
diff
changeset
|
127 dex_str_2_strdata = dict([(strdata.data.data, strdata) |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
128 for strdata in dex.stringDataItems.items]) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
129 for obj_id, strdata in strdatas: |
99
3898711adb2c
Make sure string data list is consistent.
Thinker K.F. Li <thinker@codemud.net>
parents:
98
diff
changeset
|
130 if strdata.data.data in dex_str_2_strdata: |
3898711adb2c
Make sure string data list is consistent.
Thinker K.F. Li <thinker@codemud.net>
parents:
98
diff
changeset
|
131 visit_log[obj_id] = dex_str_2_strdata[strdata.data.data] |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
132 else: |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
133 dex_append_obj_list(dex, strdata) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
134 pass |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
135 pass |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
136 pass |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
137 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
138 def merge_unique_strid(): |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
139 strids = [(obj_id, obj) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
140 for obj_id, obj in visit_log.items() |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
141 if isinstance(obj, _DEX_StringId)] |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
142 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
143 for obj_id, strid in strids: |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
144 relink_dependencies(strid) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
145 pass |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
146 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
147 strdata_2_strid = dict([(strid.stringDataOff, strid) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
148 for strid in dex.stringIds.items]) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
149 for obj_id, strid in strids: |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
150 if strid.stringDataOff in strdata_2_strid: |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
151 visit_log[obj_id] = strdata_2_strid[strid.stringDataOff] |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
152 else: |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
153 dex_append_obj_list(dex, strid) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
154 pass |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
155 pass |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
156 pass |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
157 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
158 def merge_unique_typeid(): |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
159 typeids = [(obj_id, obj) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
160 for obj_id, obj in visit_log.items() |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
161 if isinstance(obj, _DEX_TypeId)] |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
162 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
163 for obj_id, typeid in typeids: |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
164 relink_dependencies(typeid) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
165 pass |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
166 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
167 strid_2_typeid = dict([(typeid.descriptorIdx, typeid) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
168 for typeid in dex.typeIds.items]) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
169 for obj_id, typeid in typeids: |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
170 if typeid.descriptorIdx in strid_2_typeid: |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
171 visit_log[obj_id] = strid_2_typeid[typeid.descriptorIdx] |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
172 else: |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
173 dex_append_obj_list(dex, typeid) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
174 pass |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
175 pass |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
176 pass |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
177 |
111
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
178 _travel_desc_relocatable(comobj, cloner, visit_log) |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
179 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
180 merge_unique_strdata() |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
181 merge_unique_strid() |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
182 merge_unique_typeid() |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
183 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
184 for obj in visit_log.values(): |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
185 if isinstance(obj, (_DEX_StringDataItem, |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
186 _DEX_StringId, |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
187 _DEX_TypeId)): |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
188 continue |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
189 relink_dependencies(obj) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
190 dex_append_obj_list(dex, obj) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
191 pass |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
192 |
111
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
193 clone = visit_log[id(comobj)] |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
194 return clone |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
195 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
196 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
197 ## \brief Clone a class definition item |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
198 # |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
199 # \param dex is the DEXFile that clazz is cloning for. |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
200 # \param clazz is the class definition item that is cloning. |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
201 # \return the cloning _DEX_ClassDef. |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
202 # |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
203 def _clone_classdef(dex, clazz): |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
204 from paraspace.dexfile import DEXFile_linked, _DEX_ClassDef |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
205 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
206 def has_classdef(clazz): |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
207 classname = DEXFile_linked.get_classdef_name(clazz) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
208 try: |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
209 dex.find_class_name(classname) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
210 except ValueError: |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
211 return False |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
212 return True |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
213 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
214 assert isinstance(clazz, _DEX_ClassDef) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
215 |
96
1769e52bdd9d
Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents:
94
diff
changeset
|
216 if has_classdef(clazz): |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
217 raise RuntimeError, \ |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
218 'clone a class \'%s\'that is already in the DEXFile' % \ |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
219 classdef_name(clazz) |
94
88645ab29aeb
dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents:
88
diff
changeset
|
220 |
111
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
221 clone = _clone_composite(dex, clazz) |
94
88645ab29aeb
dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents:
88
diff
changeset
|
222 return clone |
87 | 223 |
224 | |
94
88645ab29aeb
dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents:
88
diff
changeset
|
225 ## \brief Clone a class definition and insert into a DEXFile. |
88645ab29aeb
dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents:
88
diff
changeset
|
226 # |
88645ab29aeb
dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents:
88
diff
changeset
|
227 # This function clone a class definition from a linked DEXFile and |
88645ab29aeb
dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents:
88
diff
changeset
|
228 # insert it into another one. |
88645ab29aeb
dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents:
88
diff
changeset
|
229 # |
88645ab29aeb
dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents:
88
diff
changeset
|
230 # \param dex is a DEXFile_linked to insert the clone. |
88645ab29aeb
dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents:
88
diff
changeset
|
231 # \param class_def is a class definition going to be cloned. |
88645ab29aeb
dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents:
88
diff
changeset
|
232 # |
111
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
233 def dexfile_insert_class(dex, classdef): |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
234 clone = _clone_classdef(dex, classdef) |
94
88645ab29aeb
dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents:
88
diff
changeset
|
235 return clone |
104 | 236 |
237 | |
111
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
238 ## \brief Collect info of classes mentioned by the code of given class. |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
239 def _find_class_relative(dex, classdef): |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
240 def classify_typeids_defined(dex, typeids): |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
241 classdefs = [] |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
242 undef_typeids = [] |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
243 for typeid in typeids: |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
244 try: |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
245 classdef = dex.find_class_typeid(typeid) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
246 except ValueError: |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
247 undef_typeids.append(typeid) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
248 else: |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
249 classdefs.append(classdef) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
250 pass |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
251 pass |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
252 return classdefs, undef_typeids |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
253 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
254 typeidxs = collect_typeidxs_mentioned_by_class(dex, classdef) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
255 typeids = [dex.find_typeid_idx(typeidx) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
256 for typeidx in typeidxs] |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
257 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
258 classdefs, typeids = classify_typeids_defined(dex, typeids) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
259 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
260 return classdefs, typeids |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
261 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
262 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
263 def dexfile_insert_classdefs(dex_dst, dex_src, classdefs): |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
264 for classdef in classdefs: |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
265 dexfile_insert_class(dex_dst, classdef) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
266 pass |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
267 pass |
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 ## \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
|
271 # |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
272 # \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
|
273 # \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
|
274 # \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
|
275 # \return the cloning _DEX_TypeId. |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
276 # |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
277 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
|
278 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
|
279 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
280 assert isinstance(typeid, _DEX_TypeId) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
281 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
282 cloning = _clone_composite(dex_dst, typeid) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
283 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
284 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
|
285 for methodid in methodids: |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
286 _clone_composite(dex_dst, methodid) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
287 pass |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
288 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
289 return cloning |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
290 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
291 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
292 ## \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
|
293 def dexfile_insert_typeids(dex_dst, dex_src, typeids): |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
294 for typeid in typeids: |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
295 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
|
296 pass |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
297 pass |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
298 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
299 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
300 ## \brief Collects relative type IDs and classes definition for given class. |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
301 def collect_classdef_relative(dex, classdef): |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
302 rel_classdefs = set([classdef]) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
303 rel_typeids = set() |
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 classdef_queue = [classdef] |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
306 while classdef_queue: |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
307 cur_classdef = classdef_queue.pop(0) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
308 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
309 classdefs, typeids = _find_class_relative(dex, classdef) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
310 rel_typeids.update(typeids) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
311 new_classdefs = list(set(classdefs) - rel_classdefs) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
312 classdef_queue = classdef_queue + new_classdefs |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
313 rel_classdefs.update(new_classdefs) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
314 pass |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
315 return rel_classdefs, rel_typeids |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
316 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
317 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
318 ## \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
|
319 # |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
320 # \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
|
321 # \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
|
322 # \param classdef is a _DEX_ClassDef that will be cloned. |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
323 # |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
324 def dexfile_insert_class_relative(dex_dst, dex_src, classdef): |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
325 from paraspace.dexfile import DEXFile_linked |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
326 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
327 def classdef_not_in_dst(classdef): |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
328 classname = DEXFile_linked.get_classdef_name(classdef) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
329 try: |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
330 dex_dst.find_class_name(classname) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
331 except ValueError: |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
332 return True |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
333 return False |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
334 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
335 def typeid_not_in_dst(typeid): |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
336 typename = DEXFile_linked.get_typeid_name(typeid) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
337 try: |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
338 dex_dst.find_typeid_name(typename) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
339 except ValueError: |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
340 return True |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
341 return False |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
342 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
343 relative_classdefs, relative_typeids = \ |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
344 collect_classdef_relative(dex_src, classdef) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
345 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
346 inserting_classdefs = filter(classdef_not_in_dst, relative_classdefs) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
347 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
|
348 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
349 dexfile_insert_classdefs(dex_dst, dex_src, inserting_classdefs) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
350 dexfile_insert_typeids(dex_dst, dex_src, inserting_typeids) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
351 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
352 classname = DEXFile_linked.get_classdef_name(classdef) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
353 cloning = dex_dst.find_class_name(classname) |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
354 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
355 return cloning |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
356 |
3820379b34e8
Add dexfile_insert_class_relative()
Thinker K.F. Li <thinker@codemud.net>
parents:
110
diff
changeset
|
357 |
108
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
358 ## \brief Redirect types and methods for the code of given method. |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
359 def method_redirect_types(dex, method, types_redir, methods_redir): |
104 | 360 from paraspace.dalvik_opcodes import decode_insn_blk, all_opcodes |
361 from paraspace.dalvik_opcodes import encode_opcode_vectors | |
362 from paraspace.dexfile import DEXFile_linked | |
363 | |
364 if not method.codeOffRef.is_true: | |
365 return | |
366 | |
367 code = method.codeOffRef.value | |
368 insns_blk = code.insns.data | |
369 op_vectors = decode_insn_blk(insns_blk) | |
370 | |
371 def redirect(opcode, args): | |
372 if opcode == all_opcodes.OP_NEW_INSTANCE: | |
373 typeidx = args[1] | |
108
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
374 if typeidx in types_redir: |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
375 to_type = types_redir[typeidx] |
104 | 376 return opcode, (args[0], to_type) |
377 pass | |
378 elif opcode == all_opcodes.OP_INVOKE_DIRECT: | |
379 methodidx = args[2] | |
108
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
380 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
|
381 return opcode, args |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
382 |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
383 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
|
384 args[3], args[4], args[5], args[6]) |
104 | 385 return opcode, args |
386 | |
387 new_op_vectors = [redirect(opcode, args) for opcode, args in op_vectors] | |
388 new_insns_blk = encode_opcode_vectors(new_op_vectors) | |
389 | |
390 code.insns.data = new_insns_blk | |
391 pass | |
392 | |
393 | |
108
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
394 ## \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
|
395 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
|
396 dex_dst, typeid_dst): |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
397 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
|
398 |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
399 methods_src = [(idx, methodid) |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
400 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
|
401 if methodid.classIdx == typeid_src] |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
402 |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
403 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
|
404 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
|
405 proto = methodid_src.protoIdx |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
406 try: |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
407 methodid_dst = \ |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
408 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
|
409 except ValueError: |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
410 return -1 |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
411 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
|
412 return methodidx_dst |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
413 |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
414 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
|
415 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
|
416 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
|
417 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
|
418 if methodidx_dst != -1] |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
419 methods_map = dict(methods_map) |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
420 return methods_map |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
421 |
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 ## \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
|
424 # |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
425 # For code of given class definition, Every mentions of types and |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
426 # methods are rewrote to types and methods according types_redir and |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
427 # methods_redir respectively. |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
428 # |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
429 # \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
|
430 # \param classdef is a class definition. |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
431 # \param types_redir is a map of types. |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
432 # \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
|
433 # |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
434 def class_redirect_types(dex, classdef, types_redir, methods_redir): |
104 | 435 if not classdef.classDataOffRef.is_true: |
436 return | |
437 | |
438 classdata = classdef.classDataOffRef.value | |
439 for method in classdata.directMethods.items: | |
108
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
440 method_redirect_types(dex, method, types_redir, methods_redir) |
104 | 441 pass |
442 for method in classdata.virtualMethods.items: | |
108
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
443 method_redirect_types(dex, method, types_redir, methods_redir) |
104 | 444 pass |
445 pass | |
446 | |
447 | |
108
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
448 ## \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
|
449 # |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
450 # This function create a map to map methods from source types to |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
451 # methods from destinate types in \ref types_redir. |
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 that owns source and destinate types. |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
454 # \param types_redir is a map of types for redirecting types. |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
455 # \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
|
456 # |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
457 def _make_methods_redir_for_types_redir(dex, types_redir): |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
458 methods_map = {} |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
459 for typeidx_src, typeidx_dst in types_redir.items(): |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
460 typeid_src = dex.find_typeid_idx(typeidx_src) |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
461 typeid_dst = dex.find_typeid_idx(typeidx_dst) |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
462 class_methods_map = make_redir_classes_methods_map(dex, |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
463 typeid_src, |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
464 dex, |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
465 typeid_dst) |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
466 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
|
467 pass |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
468 return methods_map |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
469 |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
470 |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
471 ## \biref Redirect types of all code in given DEXFile_linked. |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
472 def dexfile_redirect_types(dex, types_redir, excludes=set([])): |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
473 methods_redir = _make_methods_redir_for_types_redir(dex, types_redir) |
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
474 |
104 | 475 for classdef in dex.classDefs.items: |
476 typeid = classdef.classIdx | |
105
f14c32108164
Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents:
104
diff
changeset
|
477 idx = dex.get_idx_typeid(typeid) |
104 | 478 if idx in excludes: |
479 continue | |
108
18be67af7f1e
Use method redirection map for defining redirection
Thinker K.F. Li <thinker@codemud.net>
parents:
107
diff
changeset
|
480 class_redirect_types(dex, classdef, types_redir, methods_redir) |
104 | 481 pass |
482 pass | |
109
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
483 |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
484 |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
485 ## \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
|
486 # |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
487 # \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
|
488 # \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
|
489 # |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
490 def collect_typeidxs_in_method(dex, method): |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
491 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
|
492 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
|
493 from itertools import chain |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
494 |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
495 assert isinstance(method, _DEX_Method) |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
496 |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
497 def get_typeidx_methodidx(methodidx): |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
498 methodid = dex.find_methodid_idx(methodidx) |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
499 method_typeid = methodid.classIdx |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
500 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
|
501 return method_typeidx |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
502 |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
503 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
|
504 code, args = op_vector |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
505 |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
506 if code == all_opcodes.OP_NEW_INSTANCE: |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
507 return (args[1],) |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
508 |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
509 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
|
510 all_opcodes.OP_INVOKE_VIRTUAL, |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
511 all_opcodes.OP_INVOKE_SUPER, |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
512 all_opcodes.OP_INVOKE_STATIC, |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
513 all_opcodes.OP_INVOKE_INTERFACE): |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
514 methodidx = args[2] |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
515 method_typeidx = get_typeidx_methodidx(methodidx) |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
516 return (method_typeidx,) |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
517 |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
518 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
|
519 all_opcodes.OP_INVOKE_DIRECT_RANGE, |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
520 all_opcodes.OP_INVOKE_SUPER_RANGE, |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
521 all_opcodes.OP_INVOKE_STATIC_RANGE, |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
522 all_opcodes.OP_INVOKE_INTERFACE_RANGE): |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
523 methodidx = args[1] |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
524 method_typeidx = get_typeidx_methodidx(methodidx) |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
525 return (method_typeidx,) |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
526 |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
527 return () |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
528 |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
529 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
|
530 op_vectors = decode_insn_blk(code_blk) |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
531 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
|
532 for op_vector in op_vectors] |
835336632aba
Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents:
108
diff
changeset
|
533 typeidxs = list(chain(*types_insns)) |
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 return typeidxs |
110
6380730a80b4
Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents:
109
diff
changeset
|
536 |
6380730a80b4
Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents:
109
diff
changeset
|
537 |
6380730a80b4
Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents:
109
diff
changeset
|
538 ## \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
|
539 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
|
540 from paraspace.dexfile import DEXFile_linked |
6380730a80b4
Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents:
109
diff
changeset
|
541 |
6380730a80b4
Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents:
109
diff
changeset
|
542 assert isinstance(dex, DEXFile_linked) |
6380730a80b4
Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents:
109
diff
changeset
|
543 |
6380730a80b4
Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents:
109
diff
changeset
|
544 typeidxs = set() |
6380730a80b4
Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents:
109
diff
changeset
|
545 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
|
546 for method in methods: |
6380730a80b4
Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents:
109
diff
changeset
|
547 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
|
548 typeidxs.update(method_typeidxs) |
6380730a80b4
Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents:
109
diff
changeset
|
549 pass |
6380730a80b4
Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents:
109
diff
changeset
|
550 |
6380730a80b4
Add collect_typeidxs_mentioned_by_class()
Thinker K.F. Li <thinker@codemud.net>
parents:
109
diff
changeset
|
551 return list(typeidxs) |