comparison 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
comparison
equal deleted inserted replaced
136:f31bfe55d9c2 137:987fead83ce3
702 dexfile_redirect_types(dst_linked, typeidxs_redir, methodidxs_redir, 702 dexfile_redirect_types(dst_linked, typeidxs_redir, methodidxs_redir,
703 excludes=injected_typeidxs) 703 excludes=injected_typeidxs)
704 pass 704 pass
705 705
706 706
707 ## \brief Make indices map for typeid and methodid.
708 #
709 # It is used to create indices maps for typeid and methodid after
710 # changing order. For example, after sorting sorted array after an
711 # injection, this function create maps for remapping indices mentioned
712 # in the code.
713 #
714 def _make_idx_map(saved_typeids, saved_methodids,
715 new_typeids, new_methodids):
716 methodidxs_map = dict([(idx, new_methodids.index(methodid))
717 for idx, methodid in enumerate(saved_methodids)])
718 typeidxs_map = dict([(idx, new_typeids.index(typeid))
719 for idx, typeid in enumerate(saved_typeids)])
720 return typeidxs_map, methodidxs_map
721
722
723 ## \brief Sort sorted arrays and remapping indices for code blocks.
724 #
725 # Since sorting changes the order of sorted arrays, code blocks should
726 # be updated by remapping indices, typeid indices and methodid indices.
727 #
728 def dex_sort_sorted_arrays_consistent(dex_linked):
729 from paraspace.dex_deptracker import dex_sort_sorted_arrays
730
731 saved_typeids = list(dex_linked.typeIds.items)
732 saved_methodids = list(dex_linked.methodIds.items)
733
734 dex_sort_sorted_arrays(dex_linked)
735
736 new_typeids = dex_linked.typeIds.items
737 new_methodids = dex_linked.methodIds.items
738 methodidxs_map, typeidxs_map = \
739 _make_idx_map(saved_typeids, saved_methodids, \
740 new_typeids, new_methodids)
741
742 dexfile_redirect_types(dex_linked, typeidxs_map, methodidxs_map)
743 pass
744
745
707 ## \brief Inject and redirect a _DEX_ClassDef from one linked to another. 746 ## \brief Inject and redirect a _DEX_ClassDef from one linked to another.
708 # 747 #
709 # The _DEX_ClassDef given by inj_classname would be inserted to dst_linked, 748 # The _DEX_ClassDef given by inj_classname would be inserted to dst_linked,
710 # and redirect all invoking of type, given by redir_classname, to 749 # and redirect all invoking of type, given by redir_classname, to
711 # the injected one. 750 # the injected one.
712 # 751 #
713 def inject_redir(src_linked, inj_classname, 752 def inject_redir(src_linked, inj_classname,
714 dst_linked, redir_classname, decls): 753 dst_linked, redir_classname, decls):
715 from paraspace.dex_deptracker import dex_sort_sorted_arrays
716 from paraspace.dex_deptracker import restore_dependencies 754 from paraspace.dex_deptracker import restore_dependencies
717 755
718 inject_redir_no_restore(src_linked, inj_classname, 756 inject_redir_no_restore(src_linked, inj_classname,
719 dst_linked, redir_classname, decls) 757 dst_linked, redir_classname, decls)
720 dex_sort_sorted_arrays(dst_linked) 758 dex_sort_sorted_arrays_consistent(dst_linked)
721 restore_dependencies(dst_linked, decls) 759 restore_dependencies(dst_linked, decls)
722 pass 760 pass