Skip to content

Python callbacks suppress KeyboardInterrupt exceptions #2129

Open
@DavidBuchanan314

Description

@DavidBuchanan314
from unicorn import *
from unicorn.x86_const import *

def instruction_callback(uc, a, b, c):
	print("hello, callback")
	#raise Exception("this works as expected")
	raise KeyboardInterrupt("this gets ignored")

mu = Uc(UC_ARCH_X86, UC_MODE_32)
mu.mem_map(0x1000, 0x1000)
mu.mem_write(0x1000, b"\xeb\xfe") # infinite loop
mu.hook_add(UC_HOOK_CODE, instruction_callback)
mu.emu_start(0x1000, -1, count=5)
print("ended without errors (unexpected!)")

Here's a simple example program.

If the commented line is uncommented, the program works as I'd expect/like - emu_start() raises an exception and the final print() does not execute.

But as written (i.e. with the callback raising a KeyboardInterrupt exception), it goes like this:

hello, callback
Exception ignored on calling ctypes callback function: <bound method Uc._hookcode_cb of <unicorn.unicorn.Uc object at 0xffff42acbf50>>
Traceback (most recent call last):
  File "/home/david/.local/lib/python3.12/site-packages/unicorn/unicorn.py", line 392, in wrapper
    return func(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/david/.local/lib/python3.12/site-packages/unicorn/unicorn.py", line 663, in _hookcode_cb
    cb(self, address, size, data)
  File "/home/david/re/TDS864A/TDS684A/unicorn_hook_poc.py", line 7, in instruction_callback
    raise KeyboardInterrupt("this gets ignored")
KeyboardInterrupt: this gets ignored
hello, callback
Exception ignored on calling ctypes callback function: <bound method Uc._hookcode_cb of <unicorn.unicorn.Uc object at 0xffff42acbf50>>
Traceback (most recent call last):
  File "/home/david/.local/lib/python3.12/site-packages/unicorn/unicorn.py", line 392, in wrapper
    return func(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/david/.local/lib/python3.12/site-packages/unicorn/unicorn.py", line 663, in _hookcode_cb
    cb(self, address, size, data)
  File "/home/david/re/TDS864A/TDS684A/unicorn_hook_poc.py", line 7, in instruction_callback
    raise KeyboardInterrupt("this gets ignored")
KeyboardInterrupt: this gets ignored
hello, callback
Exception ignored on calling ctypes callback function: <bound method Uc._hookcode_cb of <unicorn.unicorn.Uc object at 0xffff42acbf50>>
Traceback (most recent call last):
  File "/home/david/.local/lib/python3.12/site-packages/unicorn/unicorn.py", line 392, in wrapper
    return func(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/david/.local/lib/python3.12/site-packages/unicorn/unicorn.py", line 663, in _hookcode_cb
    cb(self, address, size, data)
  File "/home/david/re/TDS864A/TDS684A/unicorn_hook_poc.py", line 7, in instruction_callback
    raise KeyboardInterrupt("this gets ignored")
KeyboardInterrupt: this gets ignored
hello, callback
Exception ignored on calling ctypes callback function: <bound method Uc._hookcode_cb of <unicorn.unicorn.Uc object at 0xffff42acbf50>>
Traceback (most recent call last):
  File "/home/david/.local/lib/python3.12/site-packages/unicorn/unicorn.py", line 392, in wrapper
    return func(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/david/.local/lib/python3.12/site-packages/unicorn/unicorn.py", line 663, in _hookcode_cb
    cb(self, address, size, data)
  File "/home/david/re/TDS864A/TDS684A/unicorn_hook_poc.py", line 7, in instruction_callback
    raise KeyboardInterrupt("this gets ignored")
KeyboardInterrupt: this gets ignored
hello, callback
Exception ignored on calling ctypes callback function: <bound method Uc._hookcode_cb of <unicorn.unicorn.Uc object at 0xffff42acbf50>>
Traceback (most recent call last):
  File "/home/david/.local/lib/python3.12/site-packages/unicorn/unicorn.py", line 392, in wrapper
    return func(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/david/.local/lib/python3.12/site-packages/unicorn/unicorn.py", line 663, in _hookcode_cb
    cb(self, address, size, data)
  File "/home/david/re/TDS864A/TDS684A/unicorn_hook_poc.py", line 7, in instruction_callback
    raise KeyboardInterrupt("this gets ignored")
KeyboardInterrupt: this gets ignored
ended without errors (unexpected!)

I can't really figure out why this is happening. It's unfortunate because in real-world code it results in a program that can't easily be killed via ctrl+C.

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