annotate paraspace/tests/injection_test.py @ 109:835336632aba

Add collect_typeidxs_in_method()
author Thinker K.F. Li <thinker@codemud.net>
date Mon, 01 Aug 2011 14:37:04 +0800
parents 4b3573d039af
children 6380730a80b4
rev   line source
90
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
1 from paraspace import dexfile
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
2 import os
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
3
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
4 def _install_dexfile_4_deptracker():
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
5 global dexfile
93
2fa9b6f21344 Add DEXFile_linked to model linked DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 92
diff changeset
6 import imp, sys
90
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
7 from paraspace import dex_deptracker
105
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
8 from paraspace import injection
90
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
9
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
10 try:
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
11 new_dexfile = imp.load_compiled('dexfile', dexfile.__file__)
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
12 except ImportError:
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
13 new_dexfile = imp.load_source('dexfile', dexfile.__file__)
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
14 pass
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
15 dex_deptracker.dexfile = new_dexfile
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
16 dexfile = new_dexfile
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
17 dex_deptracker._nest_types = (dexfile.array, dexfile.cond, dexfile.switch)
105
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
18
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
19 injection.dex_type_2_array_attr_map = \
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
20 injection._saved_dex_type_2_array_attr_map
93
2fa9b6f21344 Add DEXFile_linked to model linked DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 92
diff changeset
21
2fa9b6f21344 Add DEXFile_linked to model linked DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 92
diff changeset
22 sys.modules['paraspace.dexfile'] = new_dexfile
90
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
23 pass
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
24
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
25
98
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
26 def _find_map(dex, map_type):
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
27 for map in dex.maps.items.items:
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
28 if map.type == map_type:
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
29 return map
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
30 pass
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
31 pass
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
32
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
33
90
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
34 def inject_fakefile_to_helloworld_test():
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
35 from paraspace.dex_deptracker import prepare_dep_decls
96
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 95
diff changeset
36 from paraspace.injection import dexfile_insert_class
90
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
37
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
38 _install_dexfile_4_deptracker()
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
39
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
40 all_dep_decls = prepare_dep_decls()
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
41
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
42 srcdir = os.path.dirname(__file__)
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
43 srcroot = os.path.join(srcdir, '..', '..')
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
44
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
45 helloworld_fn = os.path.join(srcroot, 'data', 'helloworld.dex')
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
46 helloworld_dex = dexfile.DEXFile.open(helloworld_fn)
98
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
47
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
48 classdef_map = _find_map(helloworld_dex, 0x0006)
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
49 saved_classdef_map_sz = classdef_map.size
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
50 saved_methodids_sz = len(helloworld_dex.methodIds.items)
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
51
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
52 codeitems_map = _find_map(helloworld_dex, 0x2001)
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
53 saved_codeitems_sz = codeitems_map.size
98
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
54
93
2fa9b6f21344 Add DEXFile_linked to model linked DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 92
diff changeset
55 helloworld_linked = \
2fa9b6f21344 Add DEXFile_linked to model linked DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 92
diff changeset
56 dexfile.DEXFile_linked.build_dependencies(helloworld_dex,
2fa9b6f21344 Add DEXFile_linked to model linked DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 92
diff changeset
57 all_dep_decls)
90
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
58
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
59 fakefile_fn = os.path.join(srcroot, 'data', 'fakefile.dex')
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
60 fakefile_dex = dexfile.DEXFile.open(fakefile_fn)
93
2fa9b6f21344 Add DEXFile_linked to model linked DEX
Thinker K.F. Li <thinker@codemud.net>
parents: 92
diff changeset
61 fakefile_linked = \
95
9373465643e0 Fix bug of DEXFile_linked.get_classdef_name
Thinker K.F. Li <thinker@codemud.net>
parents: 93
diff changeset
62 dexfile.DEXFile_linked. \
9373465643e0 Fix bug of DEXFile_linked.get_classdef_name
Thinker K.F. Li <thinker@codemud.net>
parents: 93
diff changeset
63 build_dependencies(fakefile_dex, all_dep_decls)
9373465643e0 Fix bug of DEXFile_linked.get_classdef_name
Thinker K.F. Li <thinker@codemud.net>
parents: 93
diff changeset
64
9373465643e0 Fix bug of DEXFile_linked.get_classdef_name
Thinker K.F. Li <thinker@codemud.net>
parents: 93
diff changeset
65 fakefile_def = fakefile_linked. \
9373465643e0 Fix bug of DEXFile_linked.get_classdef_name
Thinker K.F. Li <thinker@codemud.net>
parents: 93
diff changeset
66 find_class_name('Lcom/codemud/fakefile/fakefile;')
96
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 95
diff changeset
67
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
68 fakefile_data = fakefile_def.classDataOffRef.value
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
69 assert len(fakefile_data.directMethods.items) == 1
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
70 assert len(fakefile_data.virtualMethods.items) == 0
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
71 fakefile_dataheader = fakefile_data.header
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
72 assert fakefile_dataheader.directMethodsSize == 1
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
73 assert fakefile_dataheader.virtualMethodsSize == 0
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
74
98
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
75 clone = dexfile_insert_class(helloworld_linked, fakefile_def)
96
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 95
diff changeset
76 assert clone
1769e52bdd9d Make dexfile_insert_class() pass the testcase
Thinker K.F. Li <thinker@codemud.net>
parents: 95
diff changeset
77 assert clone != fakefile_def
97
00cd331f8aa8 Get unlinked one from a linked one
Thinker K.F. Li <thinker@codemud.net>
parents: 96
diff changeset
78
00cd331f8aa8 Get unlinked one from a linked one
Thinker K.F. Li <thinker@codemud.net>
parents: 96
diff changeset
79 helloworld_unlinked = helloworld_linked.get_unlinked()
00cd331f8aa8 Get unlinked one from a linked one
Thinker K.F. Li <thinker@codemud.net>
parents: 96
diff changeset
80 assert helloworld_unlinked
98
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
81
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
82 # map size for classdef must be increased by 1
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
83 classdef_map = _find_map(helloworld_unlinked, 0x0006)
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
84 assert classdef_map.size == saved_classdef_map_sz + 1
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
85
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
86 classdata_map = _find_map(helloworld_unlinked, 0x2000)
c0c127c7b37e Check and fix issues of map sizes
Thinker K.F. Li <thinker@codemud.net>
parents: 97
diff changeset
87 assert classdata_map.size == classdef_map.size
99
3898711adb2c Make sure string data list is consistent.
Thinker K.F. Li <thinker@codemud.net>
parents: 98
diff changeset
88
3898711adb2c Make sure string data list is consistent.
Thinker K.F. Li <thinker@codemud.net>
parents: 98
diff changeset
89 # Check strings
3898711adb2c Make sure string data list is consistent.
Thinker K.F. Li <thinker@codemud.net>
parents: 98
diff changeset
90 strdatas = helloworld_unlinked.stringDataItems.items
3898711adb2c Make sure string data list is consistent.
Thinker K.F. Li <thinker@codemud.net>
parents: 98
diff changeset
91 strs = sorted([strdata.data.data for strdata in strdatas])
3898711adb2c Make sure string data list is consistent.
Thinker K.F. Li <thinker@codemud.net>
parents: 98
diff changeset
92 assert len(strs) == len(set(strs)) # uniquely
3898711adb2c Make sure string data list is consistent.
Thinker K.F. Li <thinker@codemud.net>
parents: 98
diff changeset
93 assert 'Lcom/codemud/fakefile/fakefile;' in strs
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
94
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
95 # Check Method List
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
96 methodids_map = _find_map(helloworld_unlinked, 0x0005) # method ids
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
97 assert methodids_map.size == len(helloworld_unlinked.methodIds.items)
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
98 assert methodids_map.size == saved_methodids_sz + 1
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
99
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
100 # Check Code item List
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
101 codeitems_map = _find_map(helloworld_unlinked, 0x2001)
355986e5cfbd Make surce methods of injected class are also being and bug fixed.
Thinker K.F. Li <thinker@codemud.net>
parents: 99
diff changeset
102 assert codeitems_map.size == saved_codeitems_sz + 1
90
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
103 pass
fe7b3f111d57 Testcase for code injection
Thinker K.F. Li <thinker@codemud.net>
parents:
diff changeset
104
105
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
105
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
106 def redirect_types_test():
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
107 from paraspace.dex_deptracker import prepare_dep_decls
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
108 from paraspace.injection import dexfile_insert_class
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
109 from paraspace.injection import dexfile_redirect_types
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
110 from paraspace import dalvik_opcodes
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
111
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
112 _install_dexfile_4_deptracker()
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
113
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
114 all_dep_decls = prepare_dep_decls()
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
115
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
116 srcdir = os.path.dirname(__file__)
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
117 srcroot = os.path.join(srcdir, '..', '..')
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
118
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
119 helloworld_fn = os.path.join(srcroot, 'data', 'helloworld.dex')
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
120 helloworld_dex = dexfile.DEXFile.open(helloworld_fn)
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
121
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
122 classdef_map = _find_map(helloworld_dex, 0x0006)
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
123 saved_classdef_map_sz = classdef_map.size
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
124 saved_methodids_sz = len(helloworld_dex.methodIds.items)
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
125
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
126 codeitems_map = _find_map(helloworld_dex, 0x2001)
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
127 saved_codeitems_sz = codeitems_map.size
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
128
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
129 helloworld_linked = \
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
130 dexfile.DEXFile_linked.build_dependencies(helloworld_dex,
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
131 all_dep_decls)
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
132
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
133 fakefile_fn = os.path.join(srcroot, 'data', 'fakefile.dex')
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
134 fakefile_dex = dexfile.DEXFile.open(fakefile_fn)
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
135 fakefile_linked = \
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
136 dexfile.DEXFile_linked. \
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
137 build_dependencies(fakefile_dex, all_dep_decls)
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
138
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
139 fakefile_def = \
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
140 fakefile_linked.find_class_name('Lcom/codemud/fakefile/fakefile;')
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
141 clone = dexfile_insert_class(helloworld_linked, fakefile_def)
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
142
107
4b3573d039af Fix bug of DEXFile_linked.find_method_name_proto().
Thinker K.F. Li <thinker@codemud.net>
parents: 106
diff changeset
143 init_method = fakefile_linked.find_method_name('<init>', fakefile_def)
4b3573d039af Fix bug of DEXFile_linked.find_method_name_proto().
Thinker K.F. Li <thinker@codemud.net>
parents: 106
diff changeset
144 init_protoid = init_method.methodIdx.protoIdx
4b3573d039af Fix bug of DEXFile_linked.find_method_name_proto().
Thinker K.F. Li <thinker@codemud.net>
parents: 106
diff changeset
145
105
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
146 File_typeid = helloworld_linked.find_typeid_name('Ljava/io/File;')
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
147 assert File_typeid
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
148 File_typeidx = helloworld_linked.get_idx_typeid(File_typeid)
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
149
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
150 clone_typeidx = helloworld_linked.get_idx_typeid(clone.classIdx)
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
151
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
152 redirect_map = {File_typeidx: clone_typeidx}
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
153 excludes = set([clone_typeidx])
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
154
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
155 fakefile_data = clone.classDataOffRef.value
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
156 fakefile_methods = fakefile_data.directMethods.items
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
157 fakefile_codes = [method.codeOffRef.value for method in fakefile_methods]
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
158
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
159 dexfile_redirect_types(helloworld_linked, redirect_map, excludes)
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
160
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
161 for code in helloworld_linked.codeItems.items:
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
162 op_vectors = dalvik_opcodes.decode_insn_blk(code.insns.data)
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
163 for opcode, args in op_vectors:
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
164 if opcode == dalvik_opcodes.all_opcodes.OP_NEW_INSTANCE:
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
165 assert args[1] != File_typeidx
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
166 pass
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
167 elif opcode == dalvik_opcodes.all_opcodes.OP_INVOKE_DIRECT:
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
168 methodid = helloworld_linked.find_methodid_idx(args[2])
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
169 assert methodid.classIdx != File_typeid or \
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
170 code in fakefile_codes
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
171 pass
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
172 pass
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
173 pass
f14c32108164 Test dexfile_redirect_types
Thinker K.F. Li <thinker@codemud.net>
parents: 100
diff changeset
174 pass
109
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
175
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
176
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
177 def collect_types_in_method_test():
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
178 from paraspace.dex_deptracker import prepare_dep_decls
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
179 from paraspace.injection import collect_typeidxs_in_method
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
180
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
181 _install_dexfile_4_deptracker()
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
182
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
183 all_dep_decls = prepare_dep_decls()
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
184
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
185 srcdir = os.path.dirname(__file__)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
186 srcroot = os.path.join(srcdir, '..', '..')
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
187
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
188 fakefile_fn = os.path.join(srcroot, 'data', 'fakefile.dex')
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
189 fakefile_dex = dexfile.DEXFile.open(fakefile_fn)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
190 fakefile_linked = \
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
191 dexfile.DEXFile_linked. \
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
192 build_dependencies(fakefile_dex, all_dep_decls)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
193
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
194 fakefile_def = \
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
195 fakefile_linked.find_class_name('Lcom/codemud/fakefile/fakefile;')
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
196
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
197 init_method = fakefile_linked.find_method_name('<init>', fakefile_def)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
198
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
199 typeidxs = collect_typeidxs_in_method(fakefile_linked, init_method)
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
200 assert len(typeidxs) == 1
835336632aba Add collect_typeidxs_in_method()
Thinker K.F. Li <thinker@codemud.net>
parents: 107
diff changeset
201 pass