diff paraspace/injection.py @ 137:987fead83ce3

Fix issue that dalvik complaining fail to verify code. - Since order of typeId and methodId are changed, index values in code block of methods are invalid after sorting sorted arrays. - We add dex_sort_sorted_arrays_consistent() function as a wrapper of dex_sort_sorted_arrays(). It remap indices mentioned in code blocks after sorting.
author Thinker K.F. Li <thinker@codemud.net>
date Wed, 10 Aug 2011 14:04:02 +0800
parents b488ca519709
children 372009418896
line wrap: on
line diff
--- a/paraspace/injection.py	Tue Aug 09 21:52:05 2011 +0800
+++ b/paraspace/injection.py	Wed Aug 10 14:04:02 2011 +0800
@@ -704,6 +704,45 @@
     pass
 
 
+## \brief Make indices map for typeid and methodid.
+#
+# It is used to create indices maps for typeid and methodid after
+# changing order.  For example, after sorting sorted array after an
+# injection, this function create maps for remapping indices mentioned
+# in the code.
+# 
+def _make_idx_map(saved_typeids, saved_methodids,
+                  new_typeids, new_methodids):
+    methodidxs_map = dict([(idx, new_methodids.index(methodid))
+                           for idx, methodid in enumerate(saved_methodids)])
+    typeidxs_map = dict([(idx, new_typeids.index(typeid))
+                         for idx, typeid in enumerate(saved_typeids)])
+    return typeidxs_map, methodidxs_map
+
+
+## \brief Sort sorted arrays and remapping indices for code blocks.
+#
+# Since sorting changes the order of sorted arrays, code blocks should
+# be updated by remapping indices, typeid indices and methodid indices.
+#
+def dex_sort_sorted_arrays_consistent(dex_linked):
+    from paraspace.dex_deptracker import dex_sort_sorted_arrays
+    
+    saved_typeids = list(dex_linked.typeIds.items)
+    saved_methodids = list(dex_linked.methodIds.items)
+    
+    dex_sort_sorted_arrays(dex_linked)
+
+    new_typeids = dex_linked.typeIds.items
+    new_methodids = dex_linked.methodIds.items
+    methodidxs_map, typeidxs_map = \
+        _make_idx_map(saved_typeids, saved_methodids, \
+                          new_typeids, new_methodids)
+    
+    dexfile_redirect_types(dex_linked, typeidxs_map, methodidxs_map)
+    pass
+
+
 ## \brief Inject and redirect a _DEX_ClassDef from one linked to another.
 #
 # The _DEX_ClassDef given by inj_classname would be inserted to dst_linked,
@@ -712,11 +751,10 @@
 #
 def inject_redir(src_linked, inj_classname,
                  dst_linked, redir_classname, decls):
-    from paraspace.dex_deptracker import dex_sort_sorted_arrays
     from paraspace.dex_deptracker import restore_dependencies
     
     inject_redir_no_restore(src_linked, inj_classname,
                             dst_linked, redir_classname, decls)
-    dex_sort_sorted_arrays(dst_linked)
+    dex_sort_sorted_arrays_consistent(dst_linked)
     restore_dependencies(dst_linked, decls)
     pass