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.66k stars 1.35k forks source link

Unicorn 2.1.0 does not work with Python 3 on x86 macOS - incompatible architecture #2009

Closed gerph closed 1 month ago

gerph commented 2 months ago

Summary

Attempting to install Unicorn 2.1.0 with pip install -U unicorn to get the latest version works, but when you try to use it, it fails to load the dynamic library.

Reproduction steps

Failing output

Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.9/3.9.1_6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 188, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/usr/local/Cellar/python@3.9/3.9.1_6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 147, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/usr/local/Cellar/python@3.9/3.9.1_6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "/usr/local/lib/python3.9/site-packages/unicorn/__init__.py", line 4, in <module>
    from .unicorn import Uc, ucsubclass, uc_version, uc_arch_supported, version_bind, debug, UcError, __version__
  File "/usr/local/lib/python3.9/site-packages/unicorn/unicorn.py", line 14, in <module>
    from .unicorn_py3 import *
  File "/usr/local/lib/python3.9/site-packages/unicorn/unicorn_py3/__init__.py", line 1, in <module>
    from .unicorn import *
  File "/usr/local/lib/python3.9/site-packages/unicorn/unicorn_py3/unicorn.py", line 187, in <module>
    uclib = __load_uc_lib()
  File "/usr/local/lib/python3.9/site-packages/unicorn/unicorn_py3/unicorn.py", line 131, in __load_uc_lib
    raise ImportError('Failed to load the Unicorn dynamic library')
ImportError: Failed to load the Unicorn dynamic library

Diagnostics

This information can be augmented by editing the file unicorn_py3/unicorn.py, to replace the _load_lib function with:

    def _load_lib(path: Path, lib_name: str):
        if platform in ('win32', 'cygwin'):
            __load_win_support(path)

        lib_file = path / lib_name
        print("Trying: %s" % (path / lib_name,))

        try:
            return ctypes.cdll.LoadLibrary(str(lib_file))
        except OSError as exc:
            print("  Exception: %r" % (exc,))
            return None

(that is, adding messages reporting the problems)

The output from this includes the actual failure reason:

Trying: /usr/local/lib/python3.9/site-packages/unicorn/lib/libunicorn.2.dylib
  Exception: OSError("dlopen(/usr/local/lib/python3.9/site-packages/unicorn/lib/libunicorn.2.dylib, 0x0006): tried: '/usr/local/lib/python3.9/site-packages/unicorn/lib/libunicorn.2.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64h' or 'x86_64')), '/System/Volumes/Preboot/Cryptexes/OS/usr/local/lib/python3.9/site-packages/unicorn/lib/libunicorn.2.dylib' (no such file), '/usr/local/lib/python3.9/site-packages/unicorn/lib/libunicorn.2.dylib' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64h' or 'x86_64'))")

Running file on this binary gives me:

$ file /usr/local/lib/python3.9/site-packages/unicorn/lib/libunicorn.2.dylib
/usr/local/lib/python3.9/site-packages/unicorn/lib/libunicorn.2.dylib: Mach-O 64-bit dynamically linked shared library arm64

So this wheel is only for ARM64, and doesn't contain any build for other macOS architectures.

To be clear the package that is installed is unicorn-2.1.0-py2.py3-none-macosx_10_9_universal2.whl

$ pip3 install unicorn
Collecting unicorn
  Using cached unicorn-2.1.0-py2.py3-none-macosx_10_9_universal2.whl (12.0 MB)
Installing collected packages: unicorn
Successfully installed unicorn-2.1.0
wtdcode commented 2 months ago

I just noticed this too, pypi tagged the universal binary with "x86_64" while we intentionally separately built. See: https://pypi.org/project/unicorn/2.1.0/#files and we have unicorn-2.1.0-py2.py3-none-macosx_12_7_x86_64.whl.

I will investigate and release a post version.

wtdcode commented 1 month ago

Turns out that it is https://github.com/pypa/wheel/issues/573#issuecomment-1901750935

New release is on the way

wtdcode commented 1 month ago

Resolved.