comparison paraspace/tests/injection_test.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 f31bfe55d9c2
children 372009418896
comparison
equal deleted inserted replaced
136:f31bfe55d9c2 137:987fead83ce3
589 for anno in setitem.annotationOffs.items: 589 for anno in setitem.annotationOffs.items:
590 assert anno.offset in hello_linked.annotationItems.items 590 assert anno.offset in hello_linked.annotationItems.items
591 pass 591 pass
592 pass 592 pass
593 pass 593 pass
594
595
596 ## \brief Test case for dalvik complaining fail to verify code.
597 #
598 # W/dalvikvm( 317): VFY: expected 1 args, found more (I)
599 # W/dalvikvm( 317): VFY: rejecting call to Landroid/app/Activity;\
600 # .setContentView (I)V
601 # W/dalvikvm( 317): VFY: rejecting opcode 0x6e at 0x0006
602 # W/dalvikvm( 317): VFY: rejected Lcom/codemud/helloworld/helloworld;\
603 # .onClick (Landroid/view/View;)V
604 # W/dalvikvm( 317): Verifier rejected class Lcom/codemud/helloworld/\
605 # helloworld;
606 # W/dalvikvm( 317): Class init failed in newInstance call (Lcom/codemud/\
607 # helloworl
608 #
609 # DEXFile.methodIds is a sorted array. The indices of _DEX_MethodIds would
610 # be changed after injection and sorting, dex_sort_sorted_arrays().
611 # It means indices in code blocks are invalid. So, code blocks must be
612 # rewrote with valid indices.
613 #
614 def code_verify_test():
615 from paraspace.dex_deptracker import prepare_dep_decls
616 from paraspace.injection import dex_sort_sorted_arrays_consistent
617 from paraspace.injection import inject_redir_no_restore
618 from paraspace.dalvik_opcodes import decode_insn_blk
619
620 _install_dexfile_4_deptracker()
621
622 all_dep_decls = prepare_dep_decls()
623
624 srcdir = os.path.dirname(__file__)
625 datapath = os.path.join(srcdir, '..', '..', 'data')
626
627 fake_path = os.path.join(datapath, 'fakefile.dex')
628 hello_path = os.path.join(datapath, 'helloworld.dex')
629
630 fake_dex = dexfile.DEXFile.open(fake_path)
631 fake_linked = dexfile.DEXFile_linked.build_dependencies(fake_dex,
632 all_dep_decls)
633
634 hello_dex = dexfile.DEXFile.open(hello_path)
635 hello_linked = dexfile.DEXFile_linked.build_dependencies(hello_dex,
636 all_dep_decls)
637
638 helloworld_typeid = \
639 hello_linked.find_typeid_name('Lcom/codemud/helloworld/helloworld;')
640 helloworld_methodids = \
641 hello_linked.find_methodids_typeid(helloworld_typeid)
642 for methodid in helloworld_methodids:
643 methodname = hello_linked.get_methodid_name(methodid)
644 if methodname == 'onClick':
645 methodidx = hello_linked.get_idx_methodid(methodid)
646 method = hello_linked.find_method_idx(methodidx)
647 blk = dexfile.DEXFile_linked.get_code_block_method(method)
648 opvectors = decode_insn_blk(blk)
649 pass
650 pass
651
652 inject_redir_no_restore(fake_linked, 'Lcom/codemud/fakefile/fakefile;',
653 hello_linked, 'Ljava/io/File;', all_dep_decls)
654 dex_sort_sorted_arrays_consistent(hello_linked)
655
656 for methodid in hello_linked.methodIds.items:
657 methodname = hello_linked.get_methodid_name(methodid)
658 if methodname == 'setContentView':
659 protoid = methodid.protoIdx
660 sig = hello_linked.dump_protoid(protoid)
661 assert sig == '(I) --> V'
662 pass
663 pass
664
665 helloworld_typeid = \
666 hello_linked.find_typeid_name('Lcom/codemud/helloworld/helloworld;')
667 helloworld_methodids = \
668 hello_linked.find_methodids_typeid(helloworld_typeid)
669 for methodid in helloworld_methodids:
670 methodname = hello_linked.get_methodid_name(methodid)
671 if methodname == 'onClick':
672 methodidx = hello_linked.get_idx_methodid(methodid)
673 method = hello_linked.find_method_idx(methodidx)
674 blk = dexfile.DEXFile_linked.get_code_block_method(method)
675 opvectors = decode_insn_blk(blk)
676 pass
677 pass
678 pass