Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

platform: support macOS and newer CPython #4

Closed
wants to merge 1 commit into from

Conversation

shizmob
Copy link

@shizmob shizmob commented Aug 29, 2022

This PR fixes util/configure_jep_native_libraries.py to use the importlib.metadata and pkg_resources modules if available, before falling back to the old sys.path search. It also adds a fallback in case (lib)jep.* can't be found, trying again and interposing the implementation cache tag. Native libraries on macOS have been known to use this filename format (e.g.: jep.cpython-310-darwin.so). It then changes GhidrathonInterpreter to search for jep.so as well.

With this, I was able to get Ghidrathon to work under macOS with gradle -PGHIDRA_INSTALL_DIR=/usr/local/Caskroom/ghidra/10.1.4-20220519/ghidra_10.1.4_PUBLIC -PPYTHON_BIN=python3 buildExtension.

Fix util/configure_jep_native_libraries.py to use importlib.metadata and
pkg_resources if available before falling back to the old sys.path search.
Also add a fallback in case (lib)jep.* can't be found, trying again by
interposing the implementation cache tag. Native libraries on macOS have
been known to use this filename format (e.g.: jep.cpython-310-darwin.so).

Fix GhidrathonInterpreter to search for jep.so as well.
@shizmob
Copy link
Author

shizmob commented Aug 29, 2022

In general though, it would be better if the build process didn't pollute my global Python environment, so it's probably worth making sure that part is run in a virtualenv, or something. Also, instead of cherry-picking files from the Jep site-packages directory, why not copy it over wholesale under the arch-specific Ghidra directory and add it to the Python interpreter path?

Copy link
Collaborator

@mike-hunhoff mike-hunhoff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shizmob this is great work! Very excited about adding MacOS to the list of supported operating systems. I've left one small comment for your review.

# Get Ghidra names
ghidra_lib_path = Path(GHIDRA_JAVA_LIB_PATH)
if sys.platform in GHIDRA_OS_LIB_NAMES:
ghidra_os_lib_path = Path(GHIDRA_OS_LIB_PATH) / (GHIDRA_OS_LIB_NAMES[sys.platform] + os.uname().machine)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.uname does not work on Windows - Ghidra only supports 64-bit so we can be confident using hard-coded paths for the os directory e.g. win_x86_64. We could include the hard-coded paths for each supported OS in GHIDRA_OS_LIB_NAMES.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well noticed, thanks! I'll look for another way, but I don't think we can include the hard-coded directories as e.g. macOS and Linux support multiple different 64-bit architectures. It seemsplatform.machine() with a mapping may do the trick, though.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True but Ghidra only supports a subset of those architectures: https://github.com/NationalSecurityAgency/ghidra/blob/c51183f1a262c80d45aac48d4f3343c67b6bd41a/GPL/nativePlatforms.gradle#L21-L28. We just need to make sure that the Jep native binaries are copied to the appropriate os directory where Ghidra knows to look e.g. win_x86_x64.

@mike-hunhoff
Copy link
Collaborator

In general though, it would be better if the build process didn't pollute my global Python environment, so it's probably worth making sure that part is run in a virtualenv, or something. Also, instead of cherry-picking files from the Jep site-packages directory, why not copy it over wholesale under the arch-specific Ghidra directory and add it to the Python interpreter path?

Thank you for the feedback! We haven't found a straightforward way to run Jep with Ghidra that doesn't require including the targeted Jep Java and native binaries in specific folders of the extension directory. We could copy everything over but I'm not sure what that gains us - we would still need to cherry-pick the Jep Java binary over to the extension's lib directory. It may be worth investigating the pip --target switch to install Jep directly to the Ghidrathon extension folder and then configure things from there. I'll open a new issue highlighting these ideas.

@rchtsang
Copy link

MacOS support would be nice. I did try out @shizmob's PR but wasn't able to get it working, even with some of the mentioned fixes in this thread.

I'm currently on an M1 Pro (OSX v13.0.1).

Interestingly, I did manage to get Ghidrathon working, but i had to manually finagle some of the library names. i used libjep.jnilib during the build instead of jep.cpython-310-darwin.so, then renamed it in the mac_arm_64 folder after extracting the dist result to jep.dll and somehow Ghidra was fine with that.

@mike-hunhoff mike-hunhoff mentioned this pull request Jan 30, 2023
@mike-hunhoff
Copy link
Collaborator

@shizmob checking to see if you are still working this PR? We have more interest coming in for MacOS and ARM support (see #27) and would love to get this PR wrapped up and merged.

@mike-hunhoff
Copy link
Collaborator

Thank you for your contribution - this has been superseded by 32f3969.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants