# HG changeset patch # User Thinker K.F. Li # Date 1313482052 -28800 # Node ID 91fabeaffce829edc1519dff04c232460ccd5dc8 # Parent 1eb1b2ca5de41caaff828c7da7b28fc5e00f5e87 Move functions diff -r 1eb1b2ca5de4 -r 91fabeaffce8 paraspace/injection.py --- a/paraspace/injection.py Tue Aug 16 14:53:41 2011 +0800 +++ b/paraspace/injection.py Tue Aug 16 16:07:32 2011 +0800 @@ -292,6 +292,90 @@ pass +## \brief Collect all type and string indices mentioned in the method code. +# +# \param method is a \ref _DEX_Method. +# \return a list of type indices mentioned in the code. +# +def collect_typeidxs_stridxs_in_method(dex, method): + from paraspace.dexfile import _DEX_Method, DEXFile_linked + from paraspace.dalvik_opcodes import decode_insn_blk, all_opcodes + from itertools import chain + + assert isinstance(method, _DEX_Method) + + def get_typeidx_methodidx(methodidx): + methodid = dex.find_methodid_idx(methodidx) + method_typeid = methodid.classIdx + method_typeidx = dex.get_idx_typeid(method_typeid) + return method_typeidx + + def collect_types_in_op_vector(op_vector): + code, args = op_vector + + if code == all_opcodes.OP_NEW_INSTANCE: + return args[1] + + if code in (all_opcodes.OP_INVOKE_DIRECT, + all_opcodes.OP_INVOKE_VIRTUAL, + all_opcodes.OP_INVOKE_SUPER, + all_opcodes.OP_INVOKE_STATIC, + all_opcodes.OP_INVOKE_INTERFACE): + methodidx = args[2] + method_typeidx = get_typeidx_methodidx(methodidx) + return method_typeidx + + if code in (all_opcodes.OP_INVOKE_VIRTUAL_RANGE, + all_opcodes.OP_INVOKE_DIRECT_RANGE, + all_opcodes.OP_INVOKE_SUPER_RANGE, + all_opcodes.OP_INVOKE_STATIC_RANGE, + all_opcodes.OP_INVOKE_INTERFACE_RANGE): + methodidx = args[1] + method_typeidx = get_typeidx_methodidx(methodidx) + return method_typeidx + + return None + + def collect_strings_in_op_vector(op_vector): + code, args = op_vector + if code == all_opcodes.OP_CONST_STRING: + stridx = args[1] + return stridx + + return None + + code_blk = DEXFile_linked.get_code_block_method(method) + op_vectors = decode_insn_blk(code_blk) + types_insns = [collect_types_in_op_vector(op_vector) + for op_vector in op_vectors] + typeidxs = [idx for idx in types_insns if idx is not None] + + strs_insns = [collect_strings_in_op_vector(op_vectors) + for op_vectors in op_vectors] + stridxs = [idx for idx in strs_insns if idx is not None] + + return typeidxs, stridxs + + +## \brief Collect all type and string indices mentioned by the class code. +def collect_typeidxs_stridxs_mentioned_by_class(dex, classdef): + from paraspace.dexfile import DEXFile_linked + + assert isinstance(dex, DEXFile_linked) + + typeidxs = set() + stridxs = set() + methods = DEXFile_linked.get_methods_classdef(classdef) + for method in methods: + method_typeidxs, method_stridxs = \ + collect_typeidxs_stridxs_in_method(dex, method) + typeidxs.update(method_typeidxs) + stridxs.update(method_stridxs) + pass + + return list(typeidxs), list(stridxs) + + ## \brief Collect info of classes mentioned by the code of given class. def _find_class_relative(dex, classdef): def classify_typeids_defined(dex, typeids): @@ -633,90 +717,6 @@ pass -## \brief Collect all type and string indices mentioned in the method code. -# -# \param method is a \ref _DEX_Method. -# \return a list of type indices mentioned in the code. -# -def collect_typeidxs_stridxs_in_method(dex, method): - from paraspace.dexfile import _DEX_Method, DEXFile_linked - from paraspace.dalvik_opcodes import decode_insn_blk, all_opcodes - from itertools import chain - - assert isinstance(method, _DEX_Method) - - def get_typeidx_methodidx(methodidx): - methodid = dex.find_methodid_idx(methodidx) - method_typeid = methodid.classIdx - method_typeidx = dex.get_idx_typeid(method_typeid) - return method_typeidx - - def collect_types_in_op_vector(op_vector): - code, args = op_vector - - if code == all_opcodes.OP_NEW_INSTANCE: - return args[1] - - if code in (all_opcodes.OP_INVOKE_DIRECT, - all_opcodes.OP_INVOKE_VIRTUAL, - all_opcodes.OP_INVOKE_SUPER, - all_opcodes.OP_INVOKE_STATIC, - all_opcodes.OP_INVOKE_INTERFACE): - methodidx = args[2] - method_typeidx = get_typeidx_methodidx(methodidx) - return method_typeidx - - if code in (all_opcodes.OP_INVOKE_VIRTUAL_RANGE, - all_opcodes.OP_INVOKE_DIRECT_RANGE, - all_opcodes.OP_INVOKE_SUPER_RANGE, - all_opcodes.OP_INVOKE_STATIC_RANGE, - all_opcodes.OP_INVOKE_INTERFACE_RANGE): - methodidx = args[1] - method_typeidx = get_typeidx_methodidx(methodidx) - return method_typeidx - - return None - - def collect_strings_in_op_vector(op_vector): - code, args = op_vector - if code == all_opcodes.OP_CONST_STRING: - stridx = args[1] - return stridx - - return None - - code_blk = DEXFile_linked.get_code_block_method(method) - op_vectors = decode_insn_blk(code_blk) - types_insns = [collect_types_in_op_vector(op_vector) - for op_vector in op_vectors] - typeidxs = [idx for idx in types_insns if idx is not None] - - strs_insns = [collect_strings_in_op_vector(op_vectors) - for op_vectors in op_vectors] - stridxs = [idx for idx in strs_insns if idx is not None] - - return typeidxs, stridxs - - -## \brief Collect all type and string indices mentioned by the class code. -def collect_typeidxs_stridxs_mentioned_by_class(dex, classdef): - from paraspace.dexfile import DEXFile_linked - - assert isinstance(dex, DEXFile_linked) - - typeidxs = set() - stridxs = set() - methods = DEXFile_linked.get_methods_classdef(classdef) - for method in methods: - method_typeidxs, method_stridxs = \ - collect_typeidxs_stridxs_in_method(dex, method) - typeidxs.update(method_typeidxs) - stridxs.update(method_stridxs) - pass - - return list(typeidxs), list(stridxs) - - ## \brief Make a mapping for type indices of injection. def make_typeidxs_map_after_injection(dex_dst, dex_src, relative_classdefs,