You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromunicornimport*fromunicorn.x86_constimport*definstruction_callback(uc, a, b, c):
print("hello, callback")
#raise Exception("this works as expected")raiseKeyboardInterrupt("this gets ignored")
mu=Uc(UC_ARCH_X86, UC_MODE_32)
mu.mem_map(0x1000, 0x1000)
mu.mem_write(0x1000, b"\xeb\xfe") # infinite loopmu.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.
The text was updated successfully, but these errors were encountered:
I'm still not quite sure what's going on, but I have a workaround, which is to catch the KeyboardInterrupt exception in the wrapper and re-raise it as a regular Exception:
fromunicornimport*fromunicorn.x86_constimport*fromfunctoolsimportwrapsdefmy_wrapper(f):
@wraps(f)defwrapper(*args, **kwds):
try:
returnf(*args, **kwds)
exceptKeyboardInterrupt:
raiseException("KeyboardInterrupt raised in callback")
returnwrapper@my_wrapperdefinstruction_callback(uc, a, b, c):
print("hello, callback")
#raise Exception("this works as expected")raiseKeyboardInterrupt("this gets ignored")
mu=Uc(UC_ARCH_X86, UC_MODE_32)
mu.mem_map(0x1000, 0x1000)
mu.mem_write(0x1000, b"\xeb\xfe") # infinite loopmu.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:
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.
The text was updated successfully, but these errors were encountered: