unicorn-engine/unicorn

Exceptions handled incorrectly in python callback functions

mrj0n3s opened this issue · 1 comments

Tested with Python 3.8

import binascii
from unicorn import Uc, UC_HOOK_CODE, UC_ARCH_X86, UC_MODE_64

def code_hook(mu, address, size, value):
    print(1/0)
    return True
       
uc = Uc(UC_ARCH_X86, UC_MODE_64)
uc.hook_add(UC_HOOK_CODE, code_hook)

code = binascii.unhexlify('C3')
addr = 0
uc.mem_map(0, 0x1000)
uc.mem_write(addr, code)
uc.emu_start(addr, addr + len(code), count=1)

Expected behavior

Traceback (most recent call last):
  File "test.py", line 15, in <module>
    uc.emu_start(addr, addr + len(code), count=1)
  File "/home/unicorn/.local/lib/python3/site-packages/unicorn/unicorn.py", line 550, in emu_start
    raise self._hook_exception
  File "/home/unicorn/.local/lib/python3/site-packages/unicorn/unicorn.py", line 392, in wrapper
    return func(self, *args, **kwargs)
  File "/home/unicorn/.local/lib/python3/site-packages/unicorn/unicorn.py", line 663, in _hookcode_cb
    cb(self, address, size, data)
  File "test.py", line 5, in code_hook
    print(1/0)
ZeroDivisionError: division by zero

Dev branch behavior

Traceback (most recent call last):
  File "/home/unicorn/.local/lib/python3/site-packages/unicorn/unicorn_py3/unicorn.py", line 302, in wrapper
    if uc._hook_exception is None:
AttributeError: 'int' object has no attribute '_hook_exception'

Fixed in 78cb4af