Skip to content

UC_ERR_EXCEPTION when hook UC_HOOK_INSN_INVALID #1972

@tylzh97

Description

@tylzh97

I'm working on dev branch.

As mentioned at PR #1132 , my code is that:

from unicorn import *
from unicorn.x86_const import *

uc = Uc(UC_ARCH_X86, UC_MODE_64)

"""
    nop
    nop
    xsaves  byte ptr [rcx]
    <garbage>
"""

sc = b"\x90\x90\x0F\xC7\x29\xFF\xFF\xFF"
uc.mem_map(0, 0x1000)
uc.mem_write(0, sc)

def hook0(uc, user_data):
    addr = uc.reg_read(UC_X86_REG_RIP)
    print("hook0 at 0x{:x}".format(addr))
    return False

def hook1(uc, user_data):
    addr = uc.reg_read(UC_X86_REG_RIP)
    print("hook1 at 0x{:x}".format(addr))
    uc.reg_write(UC_X86_REG_RIP, addr+1)
    return True

uc.hook_add(UC_HOOK_INSN_INVALID, hook0)
uc.hook_add(UC_HOOK_INSN_INVALID, hook1)
uc.emu_start(0, 20, 5000)

but i got outputs:

$ python3 test.py 
hook0 at 0x2
hook1 at 0x2
Traceback (most recent call last):
  File "/home/xxx/Workspace/Testspace/unicorn_emu/test.py", line 30, in <module>
    uc.emu_start(0, 20, 5000)
  File "/home/xxx/.venv/unicorn-dev/lib/python3.10/site-packages/unicorn/unicorn_py3/unicorn.py", line 560, in emu_start
    raise UcError(status)
unicorn.unicorn_py3.unicorn.UcError: Unhandled CPU exception (UC_ERR_EXCEPTION)

It seems that return value of UC_HOOK_INSN_INVALID function not working. I want to ignore the invalid instruction, how could I do it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions