unicorn-engine / unicorn

Unicorn CPU emulator framework (ARM, AArch64, M68K, Mips, Sparc, PowerPC, RiscV, S390x, TriCore, X86)
http://www.unicorn-engine.org
GNU General Public License v2.0
7.68k stars 1.35k forks source link

unicorn ctl_remove_cache problem (inconsistent between Python 2 and 3?) #1864

Closed gerph closed 1 year ago

gerph commented 1 year ago

Hiya,

I'm trying to work out how to use ctl_remove_cache efficiently with Unicorn 2 on Python 2.

I've installed the build from the restore-py2 branch (this might be wrong, but it seems like where I should be looking), by running python setup.py bdist and then installing the wheel that's created with pip install dist/unicorn-2.0.2-py2.py3-none-macosx_10_14_x86_64.whl.

However, when I look at the unicorn_py2.py file, I see that the remove_cache call is defined as:

    def ctl_remove_cache(self, addr):
        self.__ctl_w_1_arg(uc.UC_CTL_TB_REMOVE_CACHE, addr, ctypes.c_uint64)

This looks like I have to call the ctl_remove_cache for every single address that I have modified - that can get very tedious if a large block of code has been loaded and needs to be updated.

In the python 3 version in the source (unicorn_py3/unicorn.py), however, I see:

    def ctl_remove_cache(self, addr: int, end: int):
        self.__ctl_w_2_arg(uc.UC_CTL_TB_REMOVE_CACHE, addr, end, ctypes.c_uint64, ctypes.c_uint64)

This looks like it's a range, which is a lot better for efficiency.

I'm not sure whether the interface is actually different on Python 2, or whether they're inconsistent, or if I'm even using the right branch to try to get Python 2 support to work.

Can you help resolve what I need to do?

wtdcode commented 1 year ago

That’s a bug in Py2 binding and I will push a fix for that.

wtdcode commented 1 year ago

I pushed fix at 3c89d0c3ce0047f771135c71310f5d402b4f9a52.

But please note that, before 2.1.0 release, the content in restore-py2 may be changed without notice though the major APIs are guaranteed not changed.

gerph commented 1 year ago

Understood; I'm trying to update my application to handle Unicorn 2 properly - although it worked in the simple cases I was hitting upon the cached instruction problems, and adding this invalidation should help. If I find anything else that's inconsistent or I cannot work, I'll report issues, but I understand that there might be other changes in it.

Thanks for the very speedy response!

wtdcode commented 1 year ago

As fixed already (the patch will go to next release for sure!), I will close this issue.