Skip to content

Commit bafc859

Browse files
committed
Simplify hook_del
1 parent 048c136 commit bafc859

File tree

2 files changed

+20
-37
lines changed

2 files changed

+20
-37
lines changed

qiling/core_hooks.py

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -317,42 +317,25 @@ def hook_insn(self, callback, arg1, user_data=None, begin=1, end=0):
317317
return self.ql_hook(UC_HOOK_INSN, callback, user_data, begin, end, arg1)
318318

319319

320-
def hook_del(self, *args):
321-
if len(args) != 1 and len(args) != 2:
322-
return
320+
def hook_del(self, hret: HookRet):
321+
h = hret.obj
322+
hook_type = hret.type
323323

324-
if isinstance(args[0], HookRet):
325-
args[0].remove()
326-
return
324+
def __remove(hooks_map, handles_map, key: int) -> None:
325+
if key in hooks_map:
326+
hooks_list = hooks_map[key]
327327

328-
hook_type, h = args
328+
if h in hooks_list:
329+
hooks_list.remove(h)
329330

330-
def __handle_common(t: int) -> None:
331-
if t in self._hook:
332-
if h in self._hook[t]:
333-
del self._hook[t][self._hook[t].index(h)]
331+
if not hooks_list:
332+
uc_handle = handles_map.pop(key)
334333

335-
if len(self._hook[t]) == 0:
336-
self._h_uc.hook_del(self._hook_fuc[t])
337-
del self._hook_fuc[t]
334+
self._h_uc.hook_del(uc_handle)
338335

339-
def __handle_insn(t: int) -> None:
340-
if t in self._insn_hook:
341-
if h in self._insn_hook[t]:
342-
del self._insn_hook[t][self._insn_hook[t].index(h)]
343-
344-
if len(self._insn_hook[t]) == 0:
345-
self._h_uc.hook_del(self._insn_hook_fuc[t])
346-
del self._insn_hook_fuc[t]
347-
348-
def __handle_addr(t: int) -> None:
349-
if t in self._addr_hook:
350-
if h in self._addr_hook[t]:
351-
del self._addr_hook[t][self._addr_hook[t].index(h)]
352-
353-
if len(self._addr_hook[t]) == 0:
354-
self._h_uc.hook_del(self._addr_hook_fuc[t])
355-
del self._addr_hook_fuc[t]
336+
__handle_common = lambda k: __remove(self._hook, self._hook_fuc, k)
337+
__handle_insn = lambda i: __remove(self._insn_hook, self._insn_hook_fuc, i)
338+
__handle_addr = lambda a: __remove(self._addr_hook, self._addr_hook_fuc, a)
356339

357340
type_handlers = (
358341
(UC_HOOK_INTR, __handle_common),

qiling/core_hooks_types.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ def check(self, intno: int) -> bool:
4646

4747

4848
class HookRet:
49-
def __init__(self, ql, t, h):
50-
self._ql = ql
51-
self._t = t
52-
self._h = h
49+
def __init__(self, ql, hook_type: int, hook_obj: Hook):
50+
self.type = hook_type
51+
self.obj = hook_obj
5352

53+
self.__remove = ql.hook_del
5454

55-
def remove(self):
56-
self._ql.hook_del(self._t, self._h)
55+
def remove(self) -> None:
56+
self.__remove(self)

0 commit comments

Comments
 (0)