annotate paraspace/injection.py @ 98:c0c127c7b37e

Check and fix issues of map sizes
author Thinker K.F. Li <thinker@codemud.net>
date Mon, 25 Jul 2011 20:37:32 +0800
parents 1769e52bdd9d
children 3898711adb2c
rev   line source
87
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
1
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
2 def _relocatable_children(obj):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
3 from paraspace.dexfile import relocatable
87
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
4
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
5 attr_value_pairs = [(attr, getattr(obj, attr)) for attr in dir(obj)]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
6 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
7 if isinstance(value, relocatable)]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
8 return rel_children
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
9
87
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
10
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
11 ## \brief Travel relocatable descendants.
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
12 #
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
13 # \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
14 # \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
15 # \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
16 #
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
17 def _travel_desc_relocatable(obj, worker, visit_log):
87
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
18 if id(obj) in visit_log:
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
19 return visit_log[id(obj)]
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
20
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
21 result = worker(obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
22 visit_log[id(obj)] = result
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
23
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
24 rel_children = _relocatable_children(obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
25 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
26 _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
27 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
28 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
29
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
30
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
31 ## \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
32 def classdef_name(classdef):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
33 return classdef.classIdx.descriptorIdx.stringDataOff.data
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
34
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
35
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
36 ## \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
37 def dex_type_2_array_attr_map():
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
38 global dex_type_2_array_attr_map
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
39 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
40 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
41
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 96
diff changeset
42 def skip_marker_type(clazz):
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 96
diff changeset
43 while isinstance(clazz, _marker):
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 96
diff changeset
44 clazz = clazz.back_type
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 96
diff changeset
45 pass
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 96
diff changeset
46 return clazz
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
47
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
48 attr_values = [(attr, getattr(DEXFile, attr))
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
49 for attr in dir(DEXFile)]
98
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 96
diff changeset
50 type_2_attr = dict([(skip_marker_type(value.child_type), attr)
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
51 for attr, value in attr_values
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
52 if isinstance(value, array)])
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
53
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
54 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
55
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
56 return type_2_attr
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
57
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
58
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
59 ## \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
60 #
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
61 # 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
62 #
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
63 def dex_append_obj_list(dex, obj):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
64 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
65 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
66
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
67 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
68 try:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
69 attr = type_2_attr[obj.__class__]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
70 except KeyError:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
71 return
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
72
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
73 array = getattr(dex, attr)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
74 array.items.append(obj)
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 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
77 if count_name:
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 94
diff changeset
78 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
79 _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
80 pass
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
81 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
82
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
83
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
84 ## \brief Clone a class definition item
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 # \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
87 # \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
88 #
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
89 def _clone_classdef(dex, clazz):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
90 from copy import copy
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
91 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
92 from paraspace.dexfile import _DEX_TypeId
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
93
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
94 visit_log = {}
87
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
95
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
96 def cloner(obj):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
97 clone = copy(obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
98 return clone
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
99
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
100 def relink_dependencies(clone):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
101 rel_children = _relocatable_children(clone)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
102 for attr, value in rel_children:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
103 clone_value = visit_log[id(value)]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
104 setattr(clone, attr, clone_value)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
105 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
106 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
107
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
108 def merge_unique_strdata():
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
109 strdatas = [(obj_id, obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
110 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
111 if isinstance(obj, _DEX_StringDataItem)]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
112 dex_str_2_strdata = dict([(strdata.data, strdata)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
113 for strdata in dex.stringDataItems.items])
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
114 for obj_id, strdata in strdatas:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
115 if strdata.data in dex_str_2_strdata:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
116 visit_log[obj_id] = dex_str_2_strdata[strdata]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
117 else:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
118 dex_append_obj_list(dex, strdata)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
119 pass
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_strid():
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
124 strids = [(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_StringId)]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
127
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
128 for obj_id, strid in strids:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
129 relink_dependencies(strid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
130 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
131
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
132 strdata_2_strid = dict([(strid.stringDataOff, strid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
133 for strid in dex.stringIds.items])
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
134 for obj_id, strid in strids:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
135 if strid.stringDataOff in strdata_2_strid:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
136 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
137 else:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
138 dex_append_obj_list(dex, strid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
139 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
140 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
141 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
142
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
143 def merge_unique_typeid():
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
144 typeids = [(obj_id, obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
145 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
146 if isinstance(obj, _DEX_TypeId)]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
147
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
148 for obj_id, typeid in typeids:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
149 relink_dependencies(typeid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
150 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
151
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
152 strid_2_typeid = dict([(typeid.descriptorIdx, typeid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
153 for typeid in dex.typeIds.items])
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
154 for obj_id, typeid in typeids:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
155 if typeid.descriptorIdx in strid_2_typeid:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
156 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
157 else:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
158 dex_append_obj_list(dex, typeid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
159 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
160 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
161 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
162
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
163 def has_classdef(clazz):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
164 class_typeIds = set([classdef.classIdx
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
165 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
166 return clazz.classIdx in class_typeIds
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
167
96
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 94
diff changeset
168 _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
169
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
170 merge_unique_strdata()
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
171 merge_unique_strid()
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
172 merge_unique_typeid()
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
173
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
174 for obj in visit_log.values():
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
175 if isinstance(obj, (_DEX_StringDataItem,
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
176 _DEX_StringId,
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
177 _DEX_TypeId)):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
178 continue
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
179 relink_dependencies(obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
180 dex_append_obj_list(dex, obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
181 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
182
96
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 94
diff changeset
183 if has_classdef(clazz):
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
184 raise RuntimeError, \
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
185 '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
186 classdef_name(clazz)
94
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
187
96
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 94
diff changeset
188 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
189 return clone
87
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
190
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
191
94
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
192 ## \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
193 #
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
194 # 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
195 # insert it into another one.
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
196 #
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
197 # \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
198 # \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
199 #
87
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
200 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
201 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
202 return clone