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.RESOURCE_PREFIX on Darwin x64 and M1 #1313

Closed
phxql opened this issue Feb 12, 2021 · 12 comments
Closed

Platform.RESOURCE_PREFIX on Darwin x64 and M1 #1313

phxql opened this issue Feb 12, 2021 · 12 comments

Comments

@phxql
Copy link

phxql commented Feb 12, 2021

  • Version of JNA and related jars: 5.7.0
  • Version and vendor of the java virtual machine: OpenJDK 11
  • Operating system: OSX
  • System architecture (CPU type, bitness of the JVM): x64 and M1 aarch64

Hello! I maintain argon2-jvm, which includes a compiled argon2 library in the JAR for different operating systems (windows x86 & x64, linux x86, x64, arm, aarch64, etc.).

I now want to support both x64 macs and M1 macs. But there's a problem: Platform.RESOURCE_PREFIX returns darwin on both x64 and M1 architecture. It doesn't include the architecture, and it prevents me from supporting both the M1 and x64 macs at the same time when using the "extract library from classpath feature".

I expected the resource prefix to be something like darwin-aarch64. The argon2 lib for OSX x64 is stored in darwin subfolder, i can't put the M1 version in the same folder, as it would overwrite the x64 one.

See phxql/argon2-jvm#78

Is there any workaround for this?

Thanks a lot!

@matthiasblaesing
Copy link
Member

matthiasblaesing commented Feb 12, 2021

Yes there is. Darwin is a special case in the JNA codebase. Darwin uses libraries, that support multiple architectures in a single library. Apple has a strange way to support this, but IMHO that is one of the the few very nice things mac OS has to offer.

Have a look at the libjnidispatch.jnilib file in the jna.jar. It contains the dispatch library for amd64 and aarch64. In the past darwin binaries contained ppc and x86 code, or x86 and x64. The tool you are looking for is lipo - as a final hint, the main build script of JNA holds the necessary commands to merge the native libraries.

@phxql
Copy link
Author

phxql commented Feb 15, 2021

Thanks for your support and the hints, gonna look into it!

@matthiasblaesing
Copy link
Member

I decided to see if we can unify the behavior of darwin and the rest of the world. My intention is this:

  • move the default resource prefix for darwin to "darwin-$arch"
  • modify the code, so that for darwin the default resource path and the legacy prefix will be searched

@dbwiddis
Copy link
Contributor

One other thing to consider is the JVM in use. If using an x86/x64 JVM on M1 hardware, Rosetta will indicate a virtual processor to match, for compatibility. You need to be running on aarch64 JVM to get Platform class (or any Java class) to report the correct hardware. See https://developer.apple.com/documentation/apple_silicon/about_the_rosetta_translation_environment

@matthiasblaesing
Copy link
Member

@dbwiddis from my POV this is the correct assumption and the same on x86. If you run a 32 bit VM, you will get 32 bit libraries, if you run a 64bit JVM you get 64bit ones. It is the same here.

matthiasblaesing added a commit to matthiasblaesing/jna that referenced this issue Feb 16, 2021
…h and support darwin as fallback

The darwin platform traditionally used fatbinaries to support multiple
architectures. This works, but is a problem:

a) when apple decides, that only certain architectures may be
   bundled (it was reported, that the presence of x86 caused
   appstore validation to fail)

b) when upstream does not bundle the artifacts, but builds them
   in isolation.

Given, that JNA also hit (b) and needed post processing to merge
binaries, moving darwin to the same scheme as other OSes (<os>-<arch>)
is considered sensible.

While the resource prefix is changed, the old "darwin" prefix is still
considered for extraction and thus old libraries will continue to work.
matthiasblaesing added a commit to matthiasblaesing/jna that referenced this issue Feb 16, 2021
…h and support darwin as fallback

The darwin platform traditionally used fatbinaries to support multiple
architectures. This works, but is a problem:

a) when apple decides, that only certain architectures may be
   bundled (it was reported, that the presence of x86 caused
   appstore validation to fail)

b) when upstream does not bundle the artifacts, but builds them
   in isolation.

Given, that JNA also hit (b) and needed post processing to merge
binaries, moving darwin to the same scheme as other OSes (<os>-<arch>)
is considered sensible.

While the resource prefix is changed, the old "darwin" prefix is still
considered for extraction and thus old libraries will continue to work.
@matthiasblaesing
Copy link
Member

@phxql could you please test whether the this PR helps you #1316? This is the resulting binary, if you can't build from source:

jna.zip

@cricketsamya
Copy link

cricketsamya commented Feb 19, 2021

@matthiasblaesing @phxql
this is what I see when I run under M1.

Java Native Access (JNA) API Version 5
Version: 5.8.0-SNAPSHOT (b0)
 Native: 6.1.1 (147a998f0cbc89681a1ae6c0dd121629)
 Prefix: darwin-x86-64

I am running:
https://www.azul.com/downloads/zulu-community/?version=java-11-lts&os=macos&architecture=arm-64-bit&package=jdk

@cricketsamya
Copy link

I switched to MSFT JDK 16 for Arm and finally I could see something like this 👍🏼

➜  Downloads java -version
openjdk version "16" 2020-11-11
OpenJDK Runtime Environment Microsoft (build 16+10-20201111)
OpenJDK 64-Bit Server VM Microsoft (build 16+10-20201111, mixed mode)
➜  Downloads java -jar jna.jar
Java Native Access (JNA) API Version 5
Version: 5.8.0-SNAPSHOT (b0)
 Native: 6.1.1 (147a998f0cbc89681a1ae6c0dd121629)
 Prefix: darwin-aarch64
➜  Downloads

@matthiasblaesing @phxql This may be helpful!

@matthiasblaesing
Copy link
Member

Thank you - this test shows, that JNA in principle works and it shows, that the profix is switched, what it does ntot show is, whether or not the library actually can be loaded. I did a test with the changed prefix and the current x86-64 build of the argon library and it worked (#1316 (comment)). If you can provide a build of the darwin aarch64 variant of the argon2 library, the sample can be trivially adjusted.

@phxql
Copy link
Author

phxql commented Feb 22, 2021

I just built a small commandline tool to run the Argon2 libraries with JNA. We will test that on M1 and x64 (Rosetta and native) and report back. Thanks for your support!

https://github.com/phxql/argon2-jvm/tree/test/jna-pr-1316/argon2-jvm-runner

matthiasblaesing added a commit to matthiasblaesing/jna that referenced this issue Feb 23, 2021
…h and support darwin as fallback

The darwin platform traditionally used fatbinaries to support multiple
architectures. This works, but is a problem:

a) when apple decides, that only certain architectures may be
   bundled (it was reported, that the presence of x86 caused
   appstore validation to fail)

b) when upstream does not bundle the artifacts, but builds them
   in isolation.

Given, that JNA also hit (b) and needed post processing to merge
binaries, moving darwin to the same scheme as other OSes (<os>-<arch>)
is considered sensible.

While the resource prefix is changed, the old "darwin" prefix is still
considered for extraction and thus old libraries will continue to work.
matthiasblaesing added a commit to matthiasblaesing/jna that referenced this issue Feb 25, 2021
…h and support darwin as fallback

The darwin platform traditionally used fatbinaries to support multiple
architectures. This works, but is a problem:

a) when apple decides, that only certain architectures may be
   bundled (it was reported, that the presence of x86 caused
   appstore validation to fail)

b) when upstream does not bundle the artifacts, but builds them
   in isolation.

Given, that JNA also hit (b) and needed post processing to merge
binaries, moving darwin to the same scheme as other OSes (<os>-<arch>)
is considered sensible.

While the resource prefix is changed, the old "darwin" prefix is still
considered for extraction and thus old libraries will continue to work.
matthiasblaesing added a commit that referenced this issue Feb 26, 2021
[GH-1313] Move default darwin prefix to darwin-$arch and support darwin as fallback
@matthiasblaesing
Copy link
Member

Prefix change was merged to master, we need to see when a new release is cut.

@phxql
Copy link
Author

phxql commented Mar 1, 2021

Thanks a lot for your work :)

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

No branches or pull requests

4 participants