You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Loading jnidispatch on Android 15 with 16 KB page size leads to crash #1647
Version and vendor of the java virtual machine
ART, Android 15
System.getProperty("java.vm.version") = "2.1.0"
Operating system
Android 15 with 16 KB page size
System architecture (CPU type, bitness of the JVM)
arm64-v8a
Complete description of the problem
Loading jnidispatch using System.loadLibrary("jnidispatch") is OK on Android 15 or less with 4 KB page size, but is not OK (leads to SIGSEGV crash) on Android 15 with 16 KB page size (support of this page size in Android 15 is described in docs). System.loadLibrary("jnidispatch") is called on Android platform in loadNativeDispatchLibrary function that is called in static block of JNA class Native, so you cannot properly use, for example, Native.load function on Android 15 with 16 KB page size.
To test this behaviour, we used the corresponding Android Studio emulator and Google Pixel 8 and Google Pixel 9 physical devices with 16 KB mode enabled.
Info about emulator:
Emulator version: 35.1.13-11943732 (HVF 14.5.0)
Host machine: macOS 14.5
Api level: 35
Type: Google APIs PlayStore Page Size 16 KB
Info about Google Pixel 8:
Api level: 35
Build number: AP41.240925.009
Info about Google Pixel 9:
Api level: 35
Build number: BP11.241121.010
It is unclear why it does not and it is unclear why that was not found when testing. This needs someone with interest in Android and willing to see where this goes wrong.
I can't reproduce the problem. I modified the onCreate method in the sample app (btw: thanks, very useful) to this:
Log.d("MY_LOGS", "Loading library jnidispatch")
System.loadLibrary("jnidispatch") // This causes crash on Android 15 emulator with 16 KB page sizevar p =Runtime.getRuntime().exec(arrayOf("getconf", "PAGE_SIZE"));
p.waitFor();
var pageSize = p.inputStream.readAllBytes().toString(Charsets.UTF_8);
Log.d("MY_LOGS", "jnidispatch is loaded. Version: "+Native.VERSION+" / Version Native: "+Native.VERSION_NATIVE+" / Page size: "+ pageSize);
And get the expected result
Loading library jnidispatch
Load /data/app/~~QPQEWTIZJtTKYiMg6KMS_w==/com.example.testapp-8ycvvd3YvKiQlClPrJpUJw==/base.apk!/lib/x86_64/libjnidispatch.so using ns clns-7 from class loader (caller=/data/data/com.example.testapp/code_cache/.overlay/base.apk/classes4.dex): ok
jnidispatch is loaded. Version: 5.16.0 / Version Native: 7.0.3 / Page size: 16384
This looks sane to me. I can not check on arm64 though as the emulator can't be started:
matthias@enterprise:~$ bin/android-sdk-linux_86/emulator/emulator @Pixel_9_API_35_arm64_-_16k_pages
INFO | Android emulator version 35.2.10.0 (build_id 12414864) (CL:N/A)
INFO | Graphics backend: gfxstream
INFO | Found systemPath /home/matthias/bin/android-sdk-linux_86/system-images/android-35/google_apis_ps16k/arm64-v8a/
PANIC: Avd's CPU Architecture 'arm64' is not supported by the QEMU2 emulator on x86_64 host.
matthias@enterprise:~$
What device (or emulator) do you use for testing? With x64 architecture?
I check your code on my Google Pixel 9 ARM64 physical device with 4 KB page size and 16 KB page size, Runtime.getRuntime().exec(arrayOf("getconf", "PAGE_SIZE")) returned 4096 for 4 KB page size and 16384 for 16 KB page size (as expected), but SIGSEGV crash occured in case of 16 KB page size.
@lisa-bella97 I used the emulator for x86-64. Google did not only decided to break ABI they also ensured, that it is ugly to debug. The emulator is not able to emulate arm64 on x86-64 (at least it claims so and starting does not work). My Pixel 7a is not among the few blessed devises that can be switched to 16k mode, so that ends my journey.
I can't reproduce the problem. I modified the onCreate method in the sample app (btw: thanks, very useful) to this:
Log.d("MY_LOGS", "Loading library jnidispatch")
System.loadLibrary("jnidispatch") // This causes crash on Android 15 emulator with 16 KB page size
var p = Runtime.getRuntime().exec(arrayOf("getconf", "PAGE_SIZE"));
p.waitFor();
var pageSize = p.inputStream.readAllBytes().toString(Charsets.UTF_8);
Log.d("MY_LOGS", "jnidispatch is loaded. Version: " + Native.VERSION + " / Version Native: " + Native.VERSION_NATIVE + " / Page size: " + pageSize);
When I call this code in the onCreate, System.loadLibrary("jnidispatch") crashed directly.
D Loading library jnidispatch
D Load /data/app/~~jaqWmGyrcRhNN-TDaAtWMw==/io.github.kingsword09.dweblib-u2Sivirptb7GzpkKIiFQOQ==/base.apk!/lib/arm64-v8a/libjnidispatch.so using class loader ns clns-7 (caller=/data/data/io.github.kingsword09.dweblib/code_cache/.overlay/base.apk/classes3.dex): ok
A Fatal signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x7e479e57e590 in tid 5410 (sword09.dweblib), pid 5410 (sword09.dweblib)
After commenting out System.loadLibrary("jnidispatch"), my log output is also
jnidispatch is loaded. Version: 5.16.0 / Version Native: 7.0.3 / Page size: 16384
@lisa-bella97 I used the emulator for x86-64. Google did not only decided to break ABI they also ensured, that it is ugly to debug. The emulator is not able to emulate arm64 on x86-64 (at least it claims so and starting does not work). My Pixel 7a is not among the few blessed devises that can be switched to 16k mode, so that ends my journey.
When I use an x86-64 emulator, everything works fine.
@rvandermeulen currently the android native binaries are build with NDK 12b. If I remember correctly at some point google switched from gcc to llvm. I have doubts, that newer SDKs can currently build JNA.
Reading the Android docs again , I just noticed this bit:
Note: Due to bugs in some lower versions of ld, you might also need to set common-page-size=16384. However, we strongly recommended that you update your tooling to avoid these bugs.
Oddly, I can't find any usage of common-page-size after NDK r22b, so presumably all mention was removed along with GNU binutils in r23.
This tracks with what I noticed looking at the ELF alignment in libjnidispatch.so: previous to 5.16, the LOAD section alignment for arm64 was already 64KB. Setting the max page size to 16KB actually reduced the p_align values.
@BugsBeGone if you tell me where to modify the Makefile, I'll create an updated build.
@matthiasblaesing I'd love to test this, but don't have any new enough devices. Since @lisa-bella97 found it on real hardware, maybe they can check on ones we know for sure have crashed already?
I think putting LDFLAGS+=-Wl,-z,common-page-size=16384somewhere here should fix it. For example:
Also not sure if the previous LDFLAGS fix #1618 belongs in the ifeq ($(OS),android) condition instead of just for ifeq ($(ARCH),aarch64), but maybe it is harmless on other targets? I still don't fully understand the binutils bug, or why it made no difference at all on previous ARM builds.
Not to go off topic, but it can't be a coincidence that the note about common-page-size was only added some time between 2024-12-03 and 2024-12-16. Seems like the NDK devs didn't really test this with old toolchains. But if you want to support older versions of Android, there is no choice.
@BugsBeGone thank you for the reference and idea. Based on your suggestion I updated the original change to include the common-page-size fix. The change can be found in #1654.
A prebuild binary can be found here: jna.zip (JAR and AAR are both included).
It would be great if the people having access to an aarch64 emulator could test this (and also the ones just doing android development)
Also tested it now with provided jna.aar binary and it finally works now on arm64 emulators running on macOS (M3 MacBook). Previous versions resulted in app crashing on Android 15 with 16KB page size, with same SIGSEGV crash as already reported.
I am using it to load libsodium binaries for Android app, and for now it all seems to work fine on other Android versions as well.
Will do more testing but it seems I can finally start to target Android 15, thank you!!
Eventually managed to get a 16KB page size x64 VM to work with arm64 libjnidispatch.so in ARM translation mode. For prebuild libs at least, Android Studio tries to be clever, so by default only includes the native libs that exactly match the target. The trick is to manually set the ABIs you want in build.gradle, for example:
@BugsBeGone thank you for the reference and idea. Based on your suggestion I updated the original change to include the common-page-size fix. The change can be found in #1654.
A prebuild binary can be found here: jna.zip (JAR and AAR are both included).
It would be great if the people having access to an aarch64 emulator could test this (and also the ones just doing android development)
When will the correct 16kb jna version be released? Our app strongly depends on this so~~
Activity
matthiasblaesing commentedon Dec 23, 2024
This should have fixed the issue: 17f4e59
It is unclear why it does not and it is unclear why that was not found when testing. This needs someone with interest in Android and willing to see where this goes wrong.
matthiasblaesing commentedon Dec 23, 2024
I can't reproduce the problem. I modified the
onCreatemethod in the sample app (btw: thanks, very useful) to this:And get the expected result
This looks sane to me. I can not check on arm64 though as the emulator can't be started:
lisa-bella97 commentedon Dec 24, 2024
What device (or emulator) do you use for testing? With x64 architecture?
I check your code on my Google Pixel 9 ARM64 physical device with 4 KB page size and 16 KB page size,
Runtime.getRuntime().exec(arrayOf("getconf", "PAGE_SIZE"))returned 4096 for 4 KB page size and 16384 for 16 KB page size (as expected), but SIGSEGV crash occured in case of 16 KB page size.I recorded videos to show this behaviour: https://drive.google.com/drive/folders/12vZfRMl4FKeDgM4lqOTGZLhHY_KK54G-?usp=sharing
matthiasblaesing commentedon Dec 24, 2024
@lisa-bella97 I used the emulator for x86-64. Google did not only decided to break ABI they also ensured, that it is ugly to debug. The emulator is not able to emulate arm64 on x86-64 (at least it claims so and starting does not work). My Pixel 7a is not among the few blessed devises that can be switched to 16k mode, so that ends my journey.
rvandermeulen commentedon Feb 6, 2025
FYI, NDK r28 was released today which enables 16KB page size by default. Maybe worth rebuilding with that?
https://github.com/android/ndk/wiki/Changelog-r28
kingsword09 commentedon Feb 14, 2025
When I call this code in the onCreate,
System.loadLibrary("jnidispatch")crashed directly.After commenting out
System.loadLibrary("jnidispatch"), my log output is alsomy arm64 emulator:
kingsword09 commentedon Feb 14, 2025
When I use an x86-64 emulator, everything works fine.
rvandermeulen commentedon Feb 24, 2025
@matthiasblaesing Is there a way to get a build created with NDK r28 to see if that improves things?
matthiasblaesing commentedon Feb 24, 2025
@rvandermeulen currently the android native binaries are build with NDK 12b. If I remember correctly at some point google switched from gcc to llvm. I have doubts, that newer SDKs can currently build JNA.
BugsBeGone commentedon Feb 25, 2025
Reading the Android docs again , I just noticed this bit:
Note: Due to bugs in some lower versions of ld, you might also need to set common-page-size=16384. However, we strongly recommended that you update your tooling to avoid these bugs.Oddly, I can't find any usage of
common-page-sizeafter NDK r22b, so presumably all mention was removed along with GNU binutils in r23.BugsBeGone commentedon Feb 25, 2025
The note above mentioning
common-page-sizeseems to relate to this bug in GNUld: https://sourceware.org/bugzilla/show_bug.cgi?id=28689Here's a GH mirror of the fix, since gitweb can be very slow: https://github.com/bminor/binutils-gdb/commit/74e315dbfe5200c473b226e937935fb8ce391489
This tracks with what I noticed looking at the ELF alignment in
libjnidispatch.so: previous to 5.16, the LOAD section alignment for arm64 was already 64KB. Setting the max page size to 16KB actually reduced the p_align values.matthiasblaesing commentedon Feb 26, 2025
@BugsBeGone if you tell me where to modify the Makefile, I'll create an updated build.
BugsBeGone commentedon Feb 26, 2025
@matthiasblaesing I'd love to test this, but don't have any new enough devices. Since @lisa-bella97 found it on real hardware, maybe they can check on ones we know for sure have crashed already?
I think putting
LDFLAGS+=-Wl,-z,common-page-size=16384somewhere here should fix it. For example:Also not sure if the previous
LDFLAGSfix #1618 belongs in theifeq ($(OS),android)condition instead of just forifeq ($(ARCH),aarch64), but maybe it is harmless on other targets? I still don't fully understand the binutils bug, or why it made no difference at all on previous ARM builds.BugsBeGone commentedon Feb 27, 2025
Ah, of course, you still want testing on x86-64 VMs to work. As it happens, recent NDKs just always set 64-bit targets for ARM and x86 to default to 16KB: https://android.googlesource.com/platform/ndk/+/02d2d9adccdf353bcea0a678c28b3de62dc2cf33^!/
So perhaps then, the fix is to remove
max-page-sizefrom the generic Android LDFLAGS, and instead have:Keeping them separate also makes it easier for custom builds to undo the change only for x86-64, if that does causes any problems.
BugsBeGone commentedon Feb 27, 2025
Not to go off topic, but it can't be a coincidence that the note about
common-page-sizewas only added some time between 2024-12-03 and 2024-12-16. Seems like the NDK devs didn't really test this with old toolchains. But if you want to support older versions of Android, there is no choice.Edit: I finally found the bug tracker for the docs. Here is the issue that prompted adding the note.
Set common-page-size to 16384 for android builds
Set common-page-size to 16384 for android builds
matthiasblaesing commentedon Feb 28, 2025
@BugsBeGone thank you for the reference and idea. Based on your suggestion I updated the original change to include the
common-page-sizefix. The change can be found in #1654.A prebuild binary can be found here: jna.zip (JAR and AAR are both included).
It would be great if the people having access to an aarch64 emulator could test this (and also the ones just doing android development)
Thomyrock commentedon Mar 3, 2025
Found a way to test it on aarch64 emulator, with the sample of this issue, and it seems to have fixed it, well done @BugsBeGone.
Interesting, it explains why I didn't see it when doing the first fix attempt.
zlmr commentedon Mar 3, 2025
Also tested it now with provided jna.aar binary and it finally works now on arm64 emulators running on macOS (M3 MacBook). Previous versions resulted in app crashing on Android 15 with 16KB page size, with same SIGSEGV crash as already reported.
I am using it to load libsodium binaries for Android app, and for now it all seems to work fine on other Android versions as well.
Will do more testing but it seems I can finally start to target Android 15, thank you!!
BugsBeGone commentedon Mar 3, 2025
Eventually managed to get a 16KB page size x64 VM to work with arm64
libjnidispatch.soin ARM translation mode. For prebuild libs at least, Android Studio tries to be clever, so by default only includes the native libs that exactly match the target. The trick is to manually set the ABIs you want inbuild.gradle, for example:Also, only the Google API & Play Store (typically larger) images support ARM translation: https://android-developers.googleblog.com/2020/03/run-arm-apps-on-android-emulator.html
Edit: If you use API level 28 (9.0) or 30 (11.0) images with ARM translation, they should work with native 32-bit arm & x86 libs as well:
But of course, you can't test the new 16 KB page size requirement on images that old.
shn1233 commentedon Mar 4, 2025
When will the correct 16kb jna version be released? Our app strongly depends on this so~~
rvandermeulen commentedon Mar 14, 2025
@matthiasblaesing Just curious, is there an ETA on the 5.17.0 release? Not sure what your release cadence normally is 😄
matthiasblaesing commentedon Mar 16, 2025
Release 5.17.0 is out. I wanted to have a look at some other things, but time ran out, so just a small release.
rvandermeulen commentedon Mar 16, 2025
Thank you very much! Appreciate all the hard work you do to maintain this dependency!
chore(deps): update all non-major dependencies (mulk/mulkcms2!58)