Skip to content

Commit

Permalink
Fix uccallback
Browse files Browse the repository at this point in the history
  • Loading branch information
wtdcode committed Sep 21, 2024
1 parent d877913 commit 78cb4af
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
6 changes: 3 additions & 3 deletions bindings/python/unicorn/unicorn_py3/arch/arm64.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def hook_add(self, htype: int, callback: Callable, user_data: Any = None, begin:
insn = ctypes.c_int(aux1)

def __hook_insn_sys():
@uccallback(HOOK_INSN_SYS_CFUNC)
def __hook_insn_sys_cb(handle: int, reg: int, pcp_reg: Any, key: int) -> int:
@uccallback(self, HOOK_INSN_SYS_CFUNC)
def __hook_insn_sys_cb(uc: Uc, reg: int, pcp_reg: Any, key: int) -> int:
cp_reg = ctypes.cast(pcp_reg, ctypes.POINTER(UcRegCP64)).contents

class CpReg(NamedTuple):
Expand All @@ -65,7 +65,7 @@ class CpReg(NamedTuple):

cp_reg = CpReg(cp_reg.crn, cp_reg.crm, cp_reg.op0, cp_reg.op1, cp_reg.op2, cp_reg.val)

return callback(self, reg, cp_reg, user_data)
return callback(uc, reg, cp_reg, user_data)

return __hook_insn_sys_cb

Expand Down
24 changes: 12 additions & 12 deletions bindings/python/unicorn/unicorn_py3/arch/intel.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,30 @@ def hook_add(self, htype: int, callback: Callable, user_data: Any = None, begin:
insn = ctypes.c_int(aux1)

def __hook_insn_in():
@uccallback(HOOK_INSN_IN_CFUNC)
def __hook_insn_in_cb(handle: int, port: int, size: int, key: int) -> int:
return callback(self, port, size, user_data)
@uccallback(self, HOOK_INSN_IN_CFUNC)
def __hook_insn_in_cb(uc: Uc, port: int, size: int, key: int) -> int:
return callback(uc, port, size, user_data)

return __hook_insn_in_cb

def __hook_insn_out():
@uccallback(HOOK_INSN_OUT_CFUNC)
def __hook_insn_out_cb(handle: int, port: int, size: int, value: int, key: int):
callback(self, port, size, value, user_data)
@uccallback(self, HOOK_INSN_OUT_CFUNC)
def __hook_insn_out_cb(uc: Uc, port: int, size: int, value: int, key: int):
callback(uc, port, size, value, user_data)

return __hook_insn_out_cb

def __hook_insn_syscall():
@uccallback(HOOK_INSN_SYSCALL_CFUNC)
def __hook_insn_syscall_cb(handle: int, key: int):
callback(self, user_data)
@uccallback(self, HOOK_INSN_SYSCALL_CFUNC)
def __hook_insn_syscall_cb(uc: Uc, key: int):
callback(uc, user_data)

return __hook_insn_syscall_cb

def __hook_insn_cpuid():
@uccallback(HOOK_INSN_CPUID_CFUNC)
def __hook_insn_cpuid_cb(handle: int, key: int) -> int:
return callback(self, user_data)
@uccallback(self, HOOK_INSN_CPUID_CFUNC)
def __hook_insn_cpuid_cb(uc: Uc, key: int) -> int:
return callback(uc, user_data)

return __hook_insn_cpuid_cb

Expand Down
58 changes: 29 additions & 29 deletions bindings/python/unicorn/unicorn_py3/unicorn.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def debug() -> str:
_CFP = TypeVar('_CFP', bound=ctypes._FuncPointer)


def uccallback(functype: Type[_CFP]):
def uccallback(uc: Uc, functype: Type[_CFP]):
"""Unicorn callback decorator.
Wraps a Python function meant to be dispatched by Unicorn as a hook callback.
Expand All @@ -294,7 +294,7 @@ def uccallback(functype: Type[_CFP]):
def decorate(func) -> _CFP:

@functools.wraps(func)
def wrapper(uc: Uc, *args, **kwargs):
def wrapper(handle: Uc, *args, **kwargs):
try:
return func(uc, *args, **kwargs)
except Exception as e:
Expand Down Expand Up @@ -754,17 +754,17 @@ def mmio_map(self, address: int, size: int,
read_cb: Optional[UC_MMIO_READ_TYPE], user_data_read: Any,
write_cb: Optional[UC_MMIO_WRITE_TYPE], user_data_write: Any) -> None:

@uccallback(MMIO_READ_CFUNC)
def __mmio_map_read_cb(handle: int, offset: int, size: int, key: int) -> int:
@uccallback(self, MMIO_READ_CFUNC)
def __mmio_map_read_cb(uc: Uc, offset: int, size: int, key: int) -> int:
assert read_cb is not None

return read_cb(self, offset, size, user_data_read)
return read_cb(uc, offset, size, user_data_read)

@uccallback(MMIO_WRITE_CFUNC)
def __mmio_map_write_cb(handle: int, offset: int, size: int, value: int, key: int) -> None:
@uccallback(self, MMIO_WRITE_CFUNC)
def __mmio_map_write_cb(uc: Uc, offset: int, size: int, value: int, key: int) -> None:
assert write_cb is not None

write_cb(self, offset, size, value, user_data_write)
write_cb(uc, offset, size, value, user_data_write)

read_cb_fptr = read_cb and __mmio_map_read_cb
write_cb_fptr = write_cb and __mmio_map_write_cb
Expand Down Expand Up @@ -885,9 +885,9 @@ def hook_add(self, htype: int, callback: Callable, user_data: Any = None, begin:
"""

def __hook_intr():
@uccallback(HOOK_INTR_CFUNC)
def __hook_intr_cb(handle: int, intno: int, key: int):
callback(self, intno, user_data)
@uccallback(self, HOOK_INTR_CFUNC)
def __hook_intr_cb(uc: Uc, intno: int, key: int):
callback(uc, intno, user_data)

return __hook_intr_cb,

Expand All @@ -898,44 +898,44 @@ def __hook_insn():
raise UcError(uc.UC_ERR_ARG)

def __hook_code():
@uccallback(HOOK_CODE_CFUNC)
def __hook_code_cb(handle: int, address: int, size: int, key: int):
callback(self, address, size, user_data)
@uccallback(self, HOOK_CODE_CFUNC)
def __hook_code_cb(uc: Uc, address: int, size: int, key: int):
callback(uc, address, size, user_data)

return __hook_code_cb,

def __hook_invalid_mem():
@uccallback(HOOK_MEM_INVALID_CFUNC)
def __hook_mem_invalid_cb(handle: int, access: int, address: int, size: int, value: int, key: int) -> bool:
return callback(self, access, address, size, value, user_data)
@uccallback(self, HOOK_MEM_INVALID_CFUNC)
def __hook_mem_invalid_cb(uc: Uc, access: int, address: int, size: int, value: int, key: int) -> bool:
return callback(uc, access, address, size, value, user_data)

return __hook_mem_invalid_cb,

def __hook_mem():
@uccallback(HOOK_MEM_ACCESS_CFUNC)
def __hook_mem_access_cb(handle: int, access: int, address: int, size: int, value: int, key: int) -> None:
callback(self, access, address, size, value, user_data)
@uccallback(self, HOOK_MEM_ACCESS_CFUNC)
def __hook_mem_access_cb(uc: Uc, access: int, address: int, size: int, value: int, key: int) -> None:
callback(uc, access, address, size, value, user_data)

return __hook_mem_access_cb,

def __hook_invalid_insn():
@uccallback(HOOK_INSN_INVALID_CFUNC)
def __hook_insn_invalid_cb(handle: int, key: int) -> bool:
return callback(self, user_data)
@uccallback(self, HOOK_INSN_INVALID_CFUNC)
def __hook_insn_invalid_cb(uc: Uc, key: int) -> bool:
return callback(uc, user_data)

return __hook_insn_invalid_cb,

def __hook_edge_gen():
@uccallback(HOOK_EDGE_GEN_CFUNC)
def __hook_edge_gen_cb(handle: int, cur: ctypes._Pointer[uc_tb], prev: ctypes._Pointer[uc_tb], key: int):
callback(self, cur.contents, prev.contents, user_data)
@uccallback(self, HOOK_EDGE_GEN_CFUNC)
def __hook_edge_gen_cb(uc: Uc, cur: ctypes._Pointer[uc_tb], prev: ctypes._Pointer[uc_tb], key: int):
callback(uc, cur.contents, prev.contents, user_data)

return __hook_edge_gen_cb,

def __hook_tcg_opcode():
@uccallback(HOOK_TCG_OPCODE_CFUNC)
def __hook_tcg_op_cb(handle: int, address: int, arg1: int, arg2: int, size: int, key: int):
callback(self, address, arg1, arg2, size, user_data)
@uccallback(self, HOOK_TCG_OPCODE_CFUNC)
def __hook_tcg_op_cb(uc: Uc, address: int, arg1: int, arg2: int, size: int, key: int):
callback(uc, address, arg1, arg2, size, user_data)

opcode = ctypes.c_uint64(aux1)
flags = ctypes.c_uint64(aux2)
Expand Down

0 comments on commit 78cb4af

Please sign in to comment.