Skip to content

Commit

Permalink
Fix for macOS ARM segfault crash in darkdetect
Browse files Browse the repository at this point in the history
Pulled latest darkdetect sources from upstream to fix crash on ARM.

The reason it was crashing is because objc_msgSend has a variadic
prototype and CPython changed the way they call into such functions
in a breaking way on Python 3.9 and above. It's hard to explain but
to not have it break on ARM you need to declare the prototype properly.
:/
  • Loading branch information
cculianu committed Apr 30, 2024
1 parent 6b64508 commit 0e913e2
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions electroncash_gui/qt/utils/darkdetect/_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@
import ctypes
import ctypes.util

appkit = ctypes.cdll.LoadLibrary(ctypes.util.find_library('AppKit'))
objc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('objc'))

try:
# macOS Big Sur+ use "a built-in dynamic linker cache of all system-provided libraries"
appkit = ctypes.cdll.LoadLibrary('AppKit.framework/AppKit')
objc = ctypes.cdll.LoadLibrary('libobjc.dylib')
except OSError:
# revert to full path for older OS versions and hardened programs
appkit = ctypes.cdll.LoadLibrary(ctypes.util.find_library('AppKit'))
objc = ctypes.cdll.LoadLibrary(ctypes.util.find_library('objc'))

void_p = ctypes.c_void_p
ull = ctypes.c_uint64

objc.objc_getClass.restype = void_p
objc.sel_registerName.restype = void_p
objc.objc_msgSend.restype = void_p
objc.objc_msgSend.argtypes = [void_p, void_p]

msg = objc.objc_msgSend
# See https://docs.python.org/3/library/ctypes.html#function-prototypes for arguments description
MSGPROTOTYPE = ctypes.CFUNCTYPE(void_p, void_p, void_p, void_p)
msg = MSGPROTOTYPE(('objc_msgSend', objc), ((1, '', None), (1, '', None), (1, '', None)))

def _utf8(s):
if not isinstance(s, bytes):
Expand Down

0 comments on commit 0e913e2

Please sign in to comment.