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

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

Open
lisa-bella97 opened this issue Dec 23, 2024 · 7 comments
Open

Comments

@lisa-bella97
Copy link

lisa-bella97 commented Dec 23, 2024

  1. Version of JNA and related jars
    5.16.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, 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

  1. Steps to reproduce
    You can find minimal sample here. Launching this app on Android 15, 16-KB-based emulator or some Google Pixel physical devices with 16 KB mode enabled will lead to SIGSEGV crash, full log with dump.

Related issue: #1618

@matthiasblaesing
Copy link
Member

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
Copy link
Member

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);

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:~$

@lisa-bella97
Copy link
Author

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
Copy link
Member

@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
Copy link

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
Copy link

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

my arm64 emulator:

Image

Properties
avd.ini.displayname              Pixel 9 Pro API Baklava
avd.ini.encoding                 UTF-8
AvdId                            Pixel_9_Pro_API_Baklava
disk.dataPartition.size          6442450944
fastboot.chosenSnapshotFile      
fastboot.forceChosenSnapshotBoot no
fastboot.forceColdBoot           no
fastboot.forceFastBoot           yes
hw.accelerometer                 yes
hw.arc                           false
hw.audioInput                    yes
hw.battery                       yes
hw.camera.back                   virtualscene
hw.camera.front                  emulated
hw.cpu.ncore                     4
hw.device.hash2                  MD5:73e7b35d09e3a8055043aca4688e0dad
hw.device.manufacturer           Google
hw.device.name                   pixel_9_pro
hw.dPad                          no
hw.gps                           yes
hw.gpu.enabled                   yes
hw.gpu.mode                      auto
hw.initialOrientation            portrait
hw.keyboard                      yes
hw.lcd.density                   480
hw.lcd.height                    2856
hw.lcd.width                     1280
hw.mainKeys                      no
hw.ramSize                       11548
hw.sdCard                        yes
hw.sensors.orientation           yes
hw.sensors.proximity             yes
hw.trackBall                     no
image.androidVersion.api         34
image.androidVersion.codename    Baklava
image.sysdir.1                   system-images/android-Baklava/google_apis_playstore_ps16k/arm64-v8a/
PlayStore.enabled                true
runtime.network.latency          none
runtime.network.speed            full
showDeviceFrame                  yes
skin.dynamic                     yes
tag.display                      16 KB Page Size
tag.displaynames                 16 KB Page Size,Google APIs PlayStore
tag.id                           page_size_16kb
tag.ids                          page_size_16kb,google_apis_playstore
vm.heapSize                      256

@kingsword09
Copy link

@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.

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