MCPcopy Index your code
hub / github.com/keystone-engine/keypatch / patch_code

Method patch_code

keypatch.py:670–757  ·  view source on GitHub ↗
(self, address, assembly, syntax, padding, save_origcode, orig_asm=None, patch_data=None, patch_comment=None, undo=False)

Source from the content-addressed store, hash-verified

668 # -2 Can't read original data
669 # -3 Invalid address
670 def patch_code(self, address, assembly, syntax, padding, save_origcode, orig_asm=None, patch_data=None, patch_comment=None, undo=False):
671 global patch_info
672
673 if self.check_address(address) != 1:
674 # not a valid address
675 return -3
676
677 orig_comment = idc.Comment(address)
678 if orig_comment == None:
679 orig_comment = ''
680
681 nop_comment = ""
682 padding_len = 0
683 if not undo:
684 # we are patching via Patcher
685 (orig_encoding, orig_len) = self.ida_get_item(address)
686 if (orig_encoding, orig_len) == (None, 0):
687 return -2
688
689 (encoding, count) = self.assemble(assembly, address, syntax=syntax)
690 if encoding is None:
691 return 0
692
693 patch_len = len(encoding)
694 patch_data = ''.join(chr(c) for c in encoding)
695
696 if patch_data == orig_encoding:
697 #print("Keypatch: no need to patch, same encoding data [{0}] at 0x{1:X}".format(to_hexstr(orig_encoding), address))
698 return orig_len
699
700 # for now, only support NOP padding on Intel CPU
701 if padding and self.arch == KS_ARCH_X86:
702 if patch_len < orig_len:
703 padding_len = orig_len - patch_len
704 patch_len = orig_len
705 patch_data = patch_data.ljust(patch_len, X86_NOP)
706 elif patch_len > orig_len:
707 patch_end = address + patch_len - 1
708 ins_end = ItemEnd(patch_end)
709 padding_len = ins_end - patch_end - 1
710
711 if padding_len > 0:
712 patch_len = ins_end - address
713 patch_data = patch_data.ljust(patch_len, X86_NOP)
714
715 if padding_len > 0:
716 nop_comment = "\nKeypatch padded NOP to next boundary: {0} bytes".format(padding_len)
717
718 orig_asm = self.ida_get_disasm_range(address, address + patch_len)
719 else:
720 # we are reverting the change via "Undo" menu
721 patch_len = len(patch_data)
722
723 (plen, p_orig_data) = self.patch(address, patch_data, patch_len)
724 if plen == None:
725 # failed to patch
726 return -1
727

Callers 2

undoMethod · 0.80
patcherMethod · 0.80

Calls 6

check_addressMethod · 0.95
ida_get_itemMethod · 0.95
assembleMethod · 0.95
ida_get_disasm_rangeMethod · 0.95
patchMethod · 0.95
to_hexstrFunction · 0.85

Tested by

no test coverage detected