annotate paraspace/injection.py @ 96:1769e52bdd9d

Make dexfile_insert_class() pass the testcase
author Thinker K.F. Li <thinker@codemud.net>
date Mon, 25 Jul 2011 16:49:28 +0800
parents 88645ab29aeb
children c0c127c7b37e
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
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
40
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
41 attr_values = [(attr, getattr(DEXFile, attr))
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
42 for attr in dir(DEXFile)]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
43 type_2_attr = dict([(value.child_type, attr)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
44 for attr, value in attr_values
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
45 if isinstance(value, array)])
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
46
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
47 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
48
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
49 return type_2_attr
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
50
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
51
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
52 ## \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
53 #
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
54 # 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
55 #
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
56 def dex_append_obj_list(dex, obj):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
57 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
58 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
59
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
60 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
61 try:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
62 attr = type_2_attr[obj.__class__]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
63 except KeyError:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
64 return
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 array = getattr(dex, attr)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
67 array.items.append(obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
68
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
69 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
70 if count_name:
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 94
diff changeset
71 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
72 _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
73 pass
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
74 pass
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
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
77 ## \brief Clone a class definition item
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
78 #
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
79 # \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
80 # \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
81 #
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
82 def _clone_classdef(dex, clazz):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
83 from copy import copy
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
84 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
85 from paraspace.dexfile import _DEX_TypeId
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
86
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
87 visit_log = {}
87
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
88
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
89 def cloner(obj):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
90 clone = copy(obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
91 return clone
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
92
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
93 def relink_dependencies(clone):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
94 rel_children = _relocatable_children(clone)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
95 for attr, value in rel_children:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
96 clone_value = visit_log[id(value)]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
97 setattr(clone, attr, clone_value)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
98 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
99 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
100
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
101 def merge_unique_strdata():
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
102 strdatas = [(obj_id, obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
103 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
104 if isinstance(obj, _DEX_StringDataItem)]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
105 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
106 for strdata in dex.stringDataItems.items])
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
107 for obj_id, strdata in strdatas:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
108 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
109 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
110 else:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
111 dex_append_obj_list(dex, strdata)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
112 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
113 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
114 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
115
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
116 def merge_unique_strid():
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
117 strids = [(obj_id, obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
118 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
119 if isinstance(obj, _DEX_StringId)]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
120
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
121 for obj_id, strid in strids:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
122 relink_dependencies(strid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
123 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
124
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
125 strdata_2_strid = dict([(strid.stringDataOff, strid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
126 for strid in dex.stringIds.items])
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
127 for obj_id, strid in strids:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
128 if strid.stringDataOff in strdata_2_strid:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
129 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
130 else:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
131 dex_append_obj_list(dex, strid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
132 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
133 pass
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
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
136 def merge_unique_typeid():
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
137 typeids = [(obj_id, obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
138 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
139 if isinstance(obj, _DEX_TypeId)]
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
140
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
141 for obj_id, typeid in typeids:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
142 relink_dependencies(typeid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
143 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
144
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
145 strid_2_typeid = dict([(typeid.descriptorIdx, typeid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
146 for typeid in dex.typeIds.items])
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
147 for obj_id, typeid in typeids:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
148 if typeid.descriptorIdx in strid_2_typeid:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
149 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
150 else:
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
151 dex_append_obj_list(dex, typeid)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
152 pass
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
153 pass
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
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
156 def has_classdef(clazz):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
157 class_typeIds = set([classdef.classIdx
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
158 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
159 return clazz.classIdx in class_typeIds
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
160
96
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 94
diff changeset
161 _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
162
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
163 merge_unique_strdata()
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
164 merge_unique_strid()
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
165 merge_unique_typeid()
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 for obj in visit_log.values():
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
168 if isinstance(obj, (_DEX_StringDataItem,
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
169 _DEX_StringId,
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
170 _DEX_TypeId)):
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
171 continue
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
172 relink_dependencies(obj)
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
173 dex_append_obj_list(dex, obj)
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
96
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 94
diff changeset
176 if has_classdef(clazz):
88
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
177 raise RuntimeError, \
bbe8d5cbe368 Clone objects with meta info
Thinker K.F. Li <thinker@codemud.net>
parents: 87
diff changeset
178 '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
179 classdef_name(clazz)
94
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
180
96
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 94
diff changeset
181 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
182 return clone
87
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
183
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
184
94
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
185 ## \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
186 #
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
187 # 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
188 # insert it into another one.
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
189 #
88645ab29aeb dexfile_insert_class() returns a clone object
Thinker K.F. Li <thinker@codemud.net>
parents: 88
diff changeset
190 # \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
191 # \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
192 #
87
cd1ee85853f4 Add injection.py
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
193 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
194 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
195 return clone