java-native-access / jna

Java Native Access
Other
8.49k stars 1.67k forks source link

Loading jnidispatch on Android 15 with 16 KB page size leads to crash #1618

Open lisa-bella97 opened 1 month ago

lisa-bella97 commented 1 month ago
  1. Version of JNA and related jars 5.14.0@aar
  2. Version and vendor of the java virtual machine ART, Android 15 System.getProperty("java.vm.version") = "2.1.0"
  3. Operating system Android 15 with 16 KB page size
  4. System architecture (CPU type, bitness of the JVM) arm64-v8a
  5. 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
  6. Steps to reproduce You can find minimal sample here. Launching this app on Android 15, 16-KB-based emulator will lead to SIGSEGV crash, full log with dump.
pgarba commented 17 hours ago

Yes, this one is critical. Would be great if you could fix it in the upcoming release

dbwiddis commented 16 hours ago

Looks like there are page size assumptions in the native code that are used when compiling: https://github.com/java-native-access/jna/blob/c9e389567554df573e4466b44d78e937812325c8/native/libffi/src/dlmalloc.c#L1215-L1258

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 commented 16 hours ago

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.