albertosottile / darkdetect

Detect OS Dark Mode from Python
Other
171 stars 18 forks source link

SIGSEGV (Address boundary error) on Apple Silicon M1, Big Sur 11.5.2 #12

Closed psobolewskiPhD closed 2 years ago

psobolewskiPhD commented 2 years ago

This is related to: https://github.com/albertosottile/darkdetect/issues/11 I reported crashes on Big Sur, but it seems my issue wasn't related to Big Sur per se, but to architecture (arm64, Apple Silicon M1). In fact the version detection added in 0.4 doesn't seem to be needed in my environment (Big Sur 11.5.2, python 3.9.6/7)

I've narrowed down the fault to the mismatch in args between: https://github.com/albertosottile/darkdetect/blob/e6d1db58dafd17740aa2917e394d27d9b80bd751/darkdetect/_mac_detect.py#L27 and https://github.com/albertosottile/darkdetect/blob/e6d1db58dafd17740aa2917e394d27d9b80bd751/darkdetect/_mac_detect.py#L52-L53 I believe this is due to arch changes, based on: https://developer.apple.com/documentation/apple-silicon/addressing-architectural-differences-in-your-macos-code

As a simple test, add at line 41: (after C is defined)

NSString = C('NSString')
objc.objc_msgSend(NSString, n("stringWithUTF8String:"), _utf8("reason"))

with: objc.objc_msgSend.argtypes = [void_p, void_p] this crashes. with: objc.objc_msgSend.argtypes = [void_p, void_p, void_p] this runs.

As such, I was able to patch this locally by changing the number of argtypes and args for all msg. I can do a PR, but it will for sure need some extra testing/review.

Edit: I have a more elegant fix, where just the .argtypes is changed just for the 3 arg msg lines.

albertosottile commented 2 years ago

Thanks for narrowing this down. Could you try the code I just pushed in https://github.com/albertosottile/darkdetect/tree/cfunctype and check if it works on your M1 Mac?

EDIT: I did not realize you also had a fix to avoid passing None as third argument or change the other lines... I apologize if you still want to open a PR.

psobolewskiPhD commented 2 years ago

Works for both light and dark!

psobolewskiPhD commented 2 years ago

EDIT: I did not realize you also had a fix to avoid passing None as third argument or change the other lines... I apologize if you still want to open a PR.

It's not a problem! I was just hacking at it to see how/why. Your solution seems much more elegant!

albertosottile commented 2 years ago

Ok, I will then proceed with tests (for what I can run, though, as I still do not own a M1 Mac :( ) and release this fix as soon as possible.

Once again, thanks for finding and debugging this issue. I wonder how this code had worked with x86_64 up to now...

albertosottile commented 2 years ago

Fixed in 0.5.0.

psobolewskiPhD commented 2 years ago

Yup! And live on pip

Successfully installed darkdetect-0.5.0
╭─ ~ ···································································· ✔ ─╮
╰─ ipython                                                      (napari-test) ─╯
Python 3.9.7 | packaged by conda-forge | (default, Sep  2 2021, 17:55:16) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.25.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import darkdetect

In [2]: darkdetect.theme()
Out[2]: 'Light'
<<<SWITCH TO DARK>>>
In [3]: darkdetect.theme()
Out[3]: 'Dark'