Mercurial > paraspace
annotate paraspace/injection.py @ 106:7821c6e89622
dexfile_redirect_types() is almost ready.
fakefile.dex must be updated to provide correctly constructor signature.
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Fri, 29 Jul 2011 16:17:15 +0800 |
parents | f14c32108164 |
children | 4b3573d039af |
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 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
97 ## \brief Clone a class definition item |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
98 # |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
99 # \param dex is the DEXFile that clazz is cloning for. |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
100 # \param clazz is the class definition item that is cloning. |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
101 # |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
102 def _clone_classdef(dex, clazz): |
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 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
178 def has_classdef(clazz): |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
179 class_typeIds = set([classdef.classIdx |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
180 for classdef in dex.classDefs.items]) |
96
1769e52bdd9d
Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents:
94
diff
changeset
|
181 return clazz.classIdx in class_typeIds |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
182 |
96
1769e52bdd9d
Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents:
94
diff
changeset
|
183 _travel_desc_relocatable(clazz, cloner, visit_log) |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
184 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
185 merge_unique_strdata() |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
186 merge_unique_strid() |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
187 merge_unique_typeid() |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
188 |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
189 for obj in visit_log.values(): |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
190 if isinstance(obj, (_DEX_StringDataItem, |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
191 _DEX_StringId, |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
192 _DEX_TypeId)): |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
193 continue |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
194 relink_dependencies(obj) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
195 dex_append_obj_list(dex, obj) |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
196 pass |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
197 |
96
1769e52bdd9d
Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents:
94
diff
changeset
|
198 if has_classdef(clazz): |
88
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
199 raise RuntimeError, \ |
bbe8d5cbe368
Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents:
87
diff
changeset
|
200 '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
|
201 classdef_name(clazz) |
94
88645ab29aeb
dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents:
88
diff
changeset
|
202 |
96
1769e52bdd9d
Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents:
94
diff
changeset
|
203 clone = visit_log[id(clazz)] |
94
88645ab29aeb
dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents:
88
diff
changeset
|
204 return clone |
87 | 205 |
206 | |
94
88645ab29aeb
dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents:
88
diff
changeset
|
207 ## \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
|
208 # |
88645ab29aeb
dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents:
88
diff
changeset
|
209 # 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
|
210 # insert it into another one. |
88645ab29aeb
dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents:
88
diff
changeset
|
211 # |
88645ab29aeb
dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents:
88
diff
changeset
|
212 # \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
|
213 # \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
|
214 # |
87 | 215 def dexfile_insert_class(dex, class_def): |
94
88645ab29aeb
dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents:
88
diff
changeset
|
216 clone = _clone_classdef(dex, class_def) |
88645ab29aeb
dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents:
88
diff
changeset
|
217 return clone |
104 | 218 |
219 | |
220 def method_redirect_types(dex, method, redirect_map): | |
221 from paraspace.dalvik_opcodes import decode_insn_blk, all_opcodes | |
222 from paraspace.dalvik_opcodes import encode_opcode_vectors | |
223 from paraspace.dexfile import DEXFile_linked | |
224 | |
225 if not method.codeOffRef.is_true: | |
226 return | |
227 | |
228 code = method.codeOffRef.value | |
229 insns_blk = code.insns.data | |
230 op_vectors = decode_insn_blk(insns_blk) | |
231 | |
232 def redirect(opcode, args): | |
233 if opcode == all_opcodes.OP_NEW_INSTANCE: | |
234 typeidx = args[1] | |
235 if typeidx in redirect_map: | |
236 to_type = redirect_map[typeidx] | |
237 return opcode, (args[0], to_type) | |
238 pass | |
239 elif opcode == all_opcodes.OP_INVOKE_DIRECT: | |
240 methodidx = args[2] | |
241 methodid = dex.find_methodid_idx(methodidx) | |
242 method_typeid = methodid.classIdx | |
105
f14c32108164
Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents:
104
diff
changeset
|
243 method_typeidx = dex.get_idx_typeid(method_typeid) |
104 | 244 if method_typeidx not in redirect_map: |
106
7821c6e89622
dexfile_redirect_types() is almost ready.
Thinker K.F. Li <thinker@codemud.net>
parents:
105
diff
changeset
|
245 method_typeid = dex.find_typeid_idx(method_typeidx - 1) |
104 | 246 return opcode, args |
247 | |
248 new_method_typeidx = redirect_map[method_typeidx] | |
249 new_method_typeid = dex.find_typeid_idx(new_method_typeidx) | |
250 classdef = dex.find_class_typeid(new_method_typeid) | |
251 method_name = DEXFile_linked.get_methodid_name(methodid) | |
252 method_proto = methodid.protoIdx | |
253 | |
254 try: | |
255 new_method = dex.find_method_name_proto(method_name, | |
256 method_proto, | |
257 classdef) | |
258 except: | |
106
7821c6e89622
dexfile_redirect_types() is almost ready.
Thinker K.F. Li <thinker@codemud.net>
parents:
105
diff
changeset
|
259 print 'NOT FOUND', method_name, method_proto, dex.get_classdef_name(classdef) |
7821c6e89622
dexfile_redirect_types() is almost ready.
Thinker K.F. Li <thinker@codemud.net>
parents:
105
diff
changeset
|
260 print DEXFile_linked.dump_protoid(method_proto) |
104 | 261 return opcode, args |
262 new_method_idx = dex.get_index_method(new_method) | |
263 return opcode, (args[0], args[1], new_method_idx, | |
264 args[3], args[4], args[5], args[6]) | |
265 return opcode, args | |
266 | |
267 new_op_vectors = [redirect(opcode, args) for opcode, args in op_vectors] | |
268 new_insns_blk = encode_opcode_vectors(new_op_vectors) | |
269 | |
270 code.insns.data = new_insns_blk | |
271 pass | |
272 | |
273 | |
274 def class_redirect_types(dex, classdef, redirect_map): | |
275 if not classdef.classDataOffRef.is_true: | |
276 return | |
277 | |
278 classdata = classdef.classDataOffRef.value | |
279 for method in classdata.directMethods.items: | |
280 method_redirect_types(dex, method, redirect_map) | |
281 pass | |
282 for method in classdata.virtualMethods.items: | |
283 method_redirect_types(dex, method, redirect_map) | |
284 pass | |
285 pass | |
286 | |
287 | |
288 def dexfile_redirect_types(dex, redirect_map, excludes=set([])): | |
289 for classdef in dex.classDefs.items: | |
290 typeid = classdef.classIdx | |
105
f14c32108164
Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents:
104
diff
changeset
|
291 idx = dex.get_idx_typeid(typeid) |
104 | 292 if idx in excludes: |
293 continue | |
294 class_redirect_types(dex, classdef, redirect_map) | |
295 pass | |
296 pass |