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 #1618
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, corresponding Android Studio emulators were used. 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
Reacted by Andrey Lashin, Felix Deimel, Evgenii Potapov, Benoit Goby, Maxim Sergeev, Marcel Struck, aewetteronline, Daniel García, Patrick Honkonen, Benedict and 2 more
# ifdef_SC_PAGESIZE/* some SVR4 systems omit an underscore */
# ifndef_SC_PAGE_SIZE
# define_SC_PAGE_SIZE _SC_PAGESIZE
# endif
# endif
# ifdef_SC_PAGE_SIZE
# definemalloc_getpagesize sysconf(_SC_PAGE_SIZE)
# else
# if defined(BSD) || defined(DGUX) || defined(HAVE_GETPAGESIZE)
externsize_tgetpagesize();
# definemalloc_getpagesize getpagesize()
# else
# ifdefWIN32/* use supplied emulation of getpagesize */
# definemalloc_getpagesize getpagesize()
# else
# ifndefLACKS_SYS_PARAM_H
# include<sys/param.h>
# endif
# ifdefEXEC_PAGESIZE
# definemalloc_getpagesize EXEC_PAGESIZE
# else
# ifdefNBPG
# ifndefCLSIZE
# definemalloc_getpagesize NBPG
# else
# definemalloc_getpagesize (NBPG * CLSIZE)
# endif
# else
# ifdefNBPC
# definemalloc_getpagesize NBPC
# else
# ifdefPAGESIZE
# definemalloc_getpagesize PAGESIZE
# else/* just guess */
# definemalloc_getpagesize ((size_t)4096U)
# endif
# endif
# endif
# endif
# endif
# endif
# endif
#endif
I assume the native library version with the distribution was likely compiled on a smaller page size. Can you compile the native library on the system with the higher page size and does it resolve the problem?
Based on Googles recommendations the code should be updated;
Check for code instances that reference specific page sizes
Even if your app is 16 KB-aligned, your app can encounter errors if places in your code assume that a device is using a specific page size. To avoid this, complete the following steps:
Remove any hard-coded dependencies that reference the [PAGE_SIZE](https://cs.android.com/android/platform/superproject/main/+/main:bionic/libc/include/bits/page_size.h;l=34-39) constant or instances in your code logic that assume that a device's page size is 4 KB (4096).
Use [getpagesize()](https://cs.android.com/android/platform/superproject/main/+/main:bionic/libc/bionic/getpagesize.cpp;l=32) or [sysconf(_SC_PAGESIZE)](https://cs.android.com/android/platform/superproject/main/+/main:bionic/libc/bionic/sysconf.cpp;l=151) instead.
Hello,
It seems that the cause of the issue is actually the 16KB ELF alignment. I built JNA with the following patch and tried it on @lisa-bella97's app, which now appears to be working fine:
For anyone not aware yet, in #1647 there is a claim, that the change introduced by #1640 were not enough. On x86-64 16kB pages seem to work in emulator, but it is claimed, that the build fails on aarch64 with 16kB pages.
Anyone interested is invited to have a look at the problem.
Activity
pgarba commentedon Oct 10, 2024
Yes, this one is critical. Would be great if you could fix it in the upcoming release
dbwiddis commentedon Oct 10, 2024
Looks like there are page size assumptions in the native code that are used when compiling:
jna/native/libffi/src/dlmalloc.c
Lines 1215 to 1258 in c9e3895
I assume the native library version with the distribution was likely compiled on a smaller page size. Can you compile the native library on the system with the higher page size and does it resolve the problem?
pgarba commentedon Oct 10, 2024
Based on Googles recommendations the code should be updated;
Check for code instances that reference specific page sizes
matthiasblaesing commentedon Oct 14, 2024
@pgarba could you please test if this helps:
The idea follows @dbwiddis observation where the hardcoded pagesize is used.
mugz3m commentedon Oct 25, 2024
Hi,
I'm facing the same problem and tried to fix JNA by making the changes suggested by @matthiasblaesing, but unfortunately, it didn’t help. After making the changes, I built
jna.aar, added it to @lisa-bella97's app, and encountered the same crash again: https://gist.github.com/mugz3m/634707842dfa4a2f4b3a245714140dbe.Could it be that I built the library incorrectly? I followed the Build Environment wiki and created artifacts by simply invoking
ant native dist.Thomyrock commentedon Nov 12, 2024
Hello,
It seems that the cause of the issue is actually the 16KB ELF alignment. I built JNA with the following patch and tried it on @lisa-bella97's app, which now appears to be working fine:
rvandermeulen commentedon Dec 5, 2024
Are there plans to ship a 5.16.0 release with this change so apps depending on JNA can complete their own 16KB page size migrations?
matthiasblaesing commentedon Dec 8, 2024
@Thomyrock this indeed makes sense. Do you want to create a PR with this change (and an entry to the
CHANGES.mdor shall I go ahead?Thomyrock commentedon Dec 17, 2024
Should be fixed by #1642
rvandermeulen commentedon Dec 17, 2024
Thanks! Is there an ETA on when the next release will ship with that fix?
matthiasblaesing commentedon Dec 21, 2024
Fair question. I'll see if I manage to get a release out tomorrow.
matthiasblaesing commentedon Dec 22, 2024
Release is out. https://groups.google.com/g/jna-users/c/HOlDzhEes-U
rvandermeulen commentedon Dec 22, 2024
matthiasblaesing commentedon Dec 30, 2024
For anyone not aware yet, in #1647 there is a claim, that the change introduced by #1640 were not enough. On x86-64 16kB pages seem to work in emulator, but it is claimed, that the build fails on aarch64 with 16kB pages.
Anyone interested is invited to have a look at the problem.
chore(deps): update all non-major dependencies (mulk/mulkcms2!58)
fix(bindings): bump JNA 5.13.0 -> 5.19.1 for 16 KB page-size support