dart-lang / http

A composable API for making HTTP requests in Dart.
https://pub.dev/packages/http
BSD 3-Clause "New" or "Revised" License
1.01k stars 343 forks source link

[cronet_http] Crash occurring on 32bit android devices since version 1.2.1 #1213

Closed veloce closed 1 month ago

veloce commented 1 month ago

Using the official example, I consistently reproduce a crash when launching the application on a Samsung Galaxy A11 running on android 10 (using Browserstack App Live).

I'm building the application using flutter main channel.

Here are the logs:

05-28 10:47:48.334 E/FlutterLoader(22190): java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException: Could not load libflutter.so this is possibly because the application is running on an architecture that Flutter Android does not support (e.g. x86) see https://docs.flutter.dev/deployment/android#what-are-the-supported-target-architectures for more detail.

05-28 10:47:48.340 E/AndroidRuntime(22190): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cronet_crash_demo/com.example.cronet_crash_demo.MainActivity}: java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException: Could not load libflutter.so this is possibly because the application is running on an architecture that Flutter Android does not support (e.g. x86) see https://docs.flutter.dev/deployment/android#what-are-the-supported-target-architectures for more detail.

05-28 10:47:48.340 E/AndroidRuntime(22190): Caused by: java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException: Could not load libflutter.so this is possibly because the application is running on an architecture that Flutter Android does not support (e.g. x86) see https://docs.flutter.dev/deployment/android#what-are-the-supported-target-architectures for more detail.

05-28 10:47:48.340 E/AndroidRuntime(22190): Caused by: java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException: Could not load libflutter.so this is possibly because the application is running on an architecture that Flutter Android does not support (e.g. x86) see https://docs.flutter.dev/deployment/android#what-are-the-supported-target-architectures for more detail.

The application runs fine on the same device with cronet_http version 1.2.0.

I cannot ascertain it, but based on crash reports in my application it looks like these also suffer from the crash (and probably others):

brianquinlan commented 1 month ago

What version of Android are you using on your Galaxy A11?

brianquinlan commented 1 month ago

Can you reproduce this with package:cronet_http 1.3.0?

brianquinlan commented 1 month ago

I looked at the change history - the only thing that changed in package:cronet_http between 1.2.0 and 1.2.1 was upgrading the version of package:jni used from 0.7.3 to 0.9.1 and removal of the bindings_type: dart_only configuration in jnigen.yaml (but this is the default inpackage:jnigen so it shouldn't have affected anything).

I couldn't find any similar issues reported on package:jni/package:jnigen but that doesn't mean that those aren't the problem.

Any help with a reproduction that doesn't require owning a specific phone module would be helpful.

veloce commented 1 month ago

I do reproduce with cronet_http 1.3.0.

But I can only reproduce on these 2 devices: Samsung Galaxy A10 (android 9) and Samsung Galaxy A11 (android 10).

Capture d’écran 2024-05-29 à 09 27 25 Capture d’écran 2024-05-29 à 09 25 15

That's about all the information I can provide now. Let me know if I can help in any other way.

brianquinlan commented 1 month ago

@HosseinYousefi Can you think of any way that this issue could be related to package:jni/package:jnigen?

If not, I'll see more help to reproduce @veloce

HosseinYousefi commented 1 month ago

I mean if the only change is the jni version. Then maybe that causes the issue. The error message doesn't seem to be related to jni though. Does it also happen on flutter stable?

veloce commented 1 month ago

I could reproduce on flutter stable (still with the Galaxy A11). This time I managed to get more logs:

05-30 19:20:27.977 E/FlutterLoader(12954): Flutter initialization failed.
05-30 19:20:27.977 E/FlutterLoader(12954): java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException: Could not load libflutter.so this is possibly because the application is running on an architecture that Flutter Android does not support (e.g. x86) see https://docs.flutter.dev/deployment/android#what-are-the-supported-target-architectures for more detail.
05-30 19:20:27.977 E/FlutterLoader(12954): App is using cpu architecture: armv8l, and the native libraries directory (with path /data/app/com.example.cronet_crash_demo_stable-NRd6mA3lmJgtLwratNLKng==/lib/arm) contains the following files: [libdartjni.so]
05-30 19:20:27.977 E/FlutterLoader(12954):  at java.util.concurrent.FutureTask.report(FutureTask.java:123)
05-30 19:20:27.977 E/FlutterLoader(12954):  at java.util.concurrent.FutureTask.get(FutureTask.java:193)
05-30 19:20:27.977 E/FlutterLoader(12954):  at io.flutter.embedding.engine.loader.FlutterLoader.ensureInitializationComplete(FlutterLoader.java:260)
05-30 19:20:27.977 E/FlutterLoader(12954):  at io.flutter.embedding.engine.FlutterEngineGroup.<init>(FlutterEngineGroup.java:63)
05-30 19:20:27.977 E/FlutterLoader(12954):  at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.setUpFlutterEngine(FlutterActivityAndFragmentDelegate.java:328)
05-30 19:20:27.977 E/FlutterLoader(12954):  at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onAttach(FlutterActivityAndFragmentDelegate.java:193)
05-30 19:20:27.977 E/FlutterLoader(12954):  at io.flutter.embedding.android.FlutterActivity.onCreate(FlutterActivity.java:638)
05-30 19:20:27.977 E/FlutterLoader(12954):  at android.app.Activity.performCreate(Activity.java:7955)
05-30 19:20:27.977 E/FlutterLoader(12954):  at android.app.Activity.performCreate(Activity.java:7944)
05-30 19:20:27.977 E/FlutterLoader(12954):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
05-30 19:20:27.977 E/FlutterLoader(12954):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3423)
05-30 19:20:27.977 E/FlutterLoader(12954):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595)
05-30 19:20:27.977 E/FlutterLoader(12954):  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
05-30 19:20:27.977 E/FlutterLoader(12954):  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
05-30 19:20:27.977 E/FlutterLoader(12954):  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
05-30 19:20:27.977 E/FlutterLoader(12954):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2147)
05-30 19:20:27.977 E/FlutterLoader(12954):  at android.os.Handler.dispatchMessage(Handler.java:107)
05-30 19:20:27.977 E/FlutterLoader(12954):  at android.os.Looper.loop(Looper.java:237)
05-30 19:20:27.977 E/FlutterLoader(12954):  at android.app.ActivityThread.main(ActivityThread.java:7814)
05-30 19:20:27.977 E/FlutterLoader(12954):  at java.lang.reflect.Method.invoke(Native Method)
05-30 19:20:27.977 E/FlutterLoader(12954):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
05-30 19:20:27.977 E/FlutterLoader(12954):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
05-30 19:20:27.977 E/FlutterLoader(12954): Caused by: java.lang.UnsupportedOperationException: Could not load libflutter.so this is possibly because the application is running on an architecture that Flutter Android does not support (e.g. x86) see https://docs.flutter.dev/deployment/android#what-are-the-supported-target-architectures for more detail.
05-30 19:20:27.977 E/FlutterLoader(12954): App is using cpu architecture: armv8l, and the native libraries directory (with path /data/app/com.example.cronet_crash_demo_stable-NRd6mA3lmJgtLwratNLKng==/lib/arm) contains the following files: [libdartjni.so]
05-30 19:20:27.977 E/FlutterLoader(12954):  at io.flutter.embedding.engine.loader.FlutterLoader$1.call(FlutterLoader.java:209)
05-30 19:20:27.977 E/FlutterLoader(12954):  at io.flutter.embedding.engine.loader.FlutterLoader$1.call(FlutterLoader.java:176)
05-30 19:20:27.977 E/FlutterLoader(12954):  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
05-30 19:20:27.977 E/FlutterLoader(12954):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
05-30 19:20:27.977 E/FlutterLoader(12954):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
05-30 19:20:27.977 E/FlutterLoader(12954):  at java.lang.Thread.run(Thread.java:919)
05-30 19:20:27.977 E/FlutterLoader(12954): Caused by: java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.cronet_crash_demo_stable-NRd6mA3lmJgtLwratNLKng==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.cronet_crash_demo_stable-NRd6mA3lmJgtLwratNLKng==/lib/arm, /data/app/com.example.cronet_crash_demo_stable-NRd6mA3lmJgtLwratNLKng==/base.apk!/lib/armeabi-v7a, /system/lib, /product/lib]]] couldn't find "libflutter.so"
05-30 19:20:27.977 E/FlutterLoader(12954):  at java.lang.Runtime.loadLibrary0(Runtime.java:1067)
05-30 19:20:27.977 E/FlutterLoader(12954):  at java.lang.Runtime.loadLibrary0(Runtime.java:1007)
05-30 19:20:27.977 E/FlutterLoader(12954):  at java.lang.System.loadLibrary(System.java:1667)
05-30 19:20:27.977 E/FlutterLoader(12954):  at io.flutter.embedding.engine.FlutterJNI.loadLibrary(FlutterJNI.java:147)
05-30 19:20:27.978 E/FlutterLoader(12954):  at io.flutter.embedding.engine.loader.FlutterLoader$1.call(FlutterLoader.java:183)
05-30 19:20:27.978 E/FlutterLoader(12954):  ... 5 more
05-30 19:20:27.978 D/AndroidRuntime(12954): Shutting down VM
05-30 19:20:27.983 E/AndroidRuntime(12954): FATAL EXCEPTION: main
05-30 19:20:27.983 E/AndroidRuntime(12954): Process: com.example.cronet_crash_demo_stable, PID: 12954
05-30 19:20:27.983 E/AndroidRuntime(12954): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cronet_crash_demo_stable/com.example.cronet_crash_demo_stable.MainActivity}: java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException: Could not load libflutter.so this is possibly because the application is running on an architecture that Flutter Android does not support (e.g. x86) see https://docs.flutter.dev/deployment/android#what-are-the-supported-target-architectures for more detail.
05-30 19:20:27.983 E/AndroidRuntime(12954): App is using cpu architecture: armv8l, and the native libraries directory (with path /data/app/com.example.cronet_crash_demo_stable-NRd6mA3lmJgtLwratNLKng==/lib/arm) contains the following files: [libdartjni.so]
05-30 19:20:27.983 E/AndroidRuntime(12954):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3448)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2147)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at android.os.Handler.dispatchMessage(Handler.java:107)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at android.os.Looper.loop(Looper.java:237)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at android.app.ActivityThread.main(ActivityThread.java:7814)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at java.lang.reflect.Method.invoke(Native Method)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
05-30 19:20:27.983 E/AndroidRuntime(12954): Caused by: java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException: Could not load libflutter.so this is possibly because the application is running on an architecture that Flutter Android does not support (e.g. x86) see https://docs.flutter.dev/deployment/android#what-are-the-supported-target-architectures for more detail.
05-30 19:20:27.983 E/AndroidRuntime(12954): App is using cpu architecture: armv8l, and the native libraries directory (with path /data/app/com.example.cronet_crash_demo_stable-NRd6mA3lmJgtLwratNLKng==/lib/arm) contains the following files: [libdartjni.so]
05-30 19:20:27.983 E/AndroidRuntime(12954):     at io.flutter.embedding.engine.loader.FlutterLoader.ensureInitializationComplete(FlutterLoader.java:377)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at io.flutter.embedding.engine.FlutterEngineGroup.<init>(FlutterEngineGroup.java:63)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.setUpFlutterEngine(FlutterActivityAndFragmentDelegate.java:328)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onAttach(FlutterActivityAndFragmentDelegate.java:193)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at io.flutter.embedding.android.FlutterActivity.onCreate(FlutterActivity.java:638)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at android.app.Activity.performCreate(Activity.java:7955)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at android.app.Activity.performCreate(Activity.java:7944)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3423)
05-30 19:20:27.983 E/AndroidRuntime(12954):     ... 11 more
05-30 19:20:27.983 E/AndroidRuntime(12954): Caused by: java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException: Could not load libflutter.so this is possibly because the application is running on an architecture that Flutter Android does not support (e.g. x86) see https://docs.flutter.dev/deployment/android#what-are-the-supported-target-architectures for more detail.
05-30 19:20:27.983 E/AndroidRuntime(12954): App is using cpu architecture: armv8l, and the native libraries directory (with path /data/app/com.example.cronet_crash_demo_stable-NRd6mA3lmJgtLwratNLKng==/lib/arm) contains the following files: [libdartjni.so]
05-30 19:20:27.983 E/AndroidRuntime(12954):     at java.util.concurrent.FutureTask.report(FutureTask.java:123)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at java.util.concurrent.FutureTask.get(FutureTask.java:193)
05-30 19:20:27.983 E/AndroidRuntime(12954):     at io.flutter.embedding.engine.loader.FlutterLoader.ensureInitializationComplete(FlutterLoader.java:260)
05-30 19:20:27.983 E/AndroidRuntime(12954):     ... 19 more
05-30 19:20:27.984 E/AndroidRuntime(12954): Caused by: java.lang.UnsupportedOperationException: Could not load libflutter.so this is possibly because the application is running on an architecture that Flutter Android does not support (e.g. x86) see https://docs.flutter.dev/deployment/android#what-are-the-supported-target-architectures for more detail.
05-30 19:20:27.984 E/AndroidRuntime(12954): App is using cpu architecture: armv8l, and the native libraries directory (with path /data/app/com.example.cronet_crash_demo_stable-NRd6mA3lmJgtLwratNLKng==/lib/arm) contains the following files: [libdartjni.so]
05-30 19:20:27.984 E/AndroidRuntime(12954):     at io.flutter.embedding.engine.loader.FlutterLoader$1.call(FlutterLoader.java:209)
05-30 19:20:27.984 E/AndroidRuntime(12954):     at io.flutter.embedding.engine.loader.FlutterLoader$1.call(FlutterLoader.java:176)
05-30 19:20:27.984 E/AndroidRuntime(12954):     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
05-30 19:20:27.984 E/AndroidRuntime(12954):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
05-30 19:20:27.984 E/AndroidRuntime(12954):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
05-30 19:20:27.984 E/AndroidRuntime(12954):     at java.lang.Thread.run(Thread.java:919)
05-30 19:20:27.984 E/AndroidRuntime(12954): Caused by: java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.cronet_crash_demo_stable-NRd6mA3lmJgtLwratNLKng==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.cronet_crash_demo_stable-NRd6mA3lmJgtLwratNLKng==/lib/arm, /data/app/com.example.cronet_crash_demo_stable-NRd6mA3lmJgtLwratNLKng==/base.apk!/lib/armeabi-v7a, /system/lib, /product/lib]]] couldn't find "libflutter.so"
05-30 19:20:27.984 E/AndroidRuntime(12954):     at java.lang.Runtime.loadLibrary0(Runtime.java:1067)
05-30 19:20:27.984 E/AndroidRuntime(12954):     at java.lang.Runtime.loadLibrary0(Runtime.java:1007)
05-30 19:20:27.984 E/AndroidRuntime(12954):     at java.lang.System.loadLibrary(System.java:1667)
05-30 19:20:27.984 E/AndroidRuntime(12954):     at io.flutter.embedding.engine.FlutterJNI.loadLibrary(FlutterJNI.java:147)
05-30 19:20:27.984 E/AndroidRuntime(12954):     at io.flutter.embedding.engine.loader.FlutterLoader$1.call(FlutterLoader.java:183)
05-30 19:20:27.984 E/AndroidRuntime(12954):     ... 5 more

Full log here:

app-debug.apk_2024_5_30_21_28_7.log

brianquinlan commented 1 month ago

There are some Cronet-related log lines before the failure e.g. 05-30 19:19:50.265 I/cr_CronetLibraryLoader( 8024): Cronet version: 114.0.5735.84, arch: armv8l so cronet itself is probably not the issue.

Could you try testing these scenarios (to see if you get the same error):

  1. try running with cronetHttpNoPlay=true e.g. flutter run --dart-define=cronetHttpNoPlay=true
  2. remove the dependency on cronet_http
  3. remove the dependency on cronet_http but add one on package:jni (and maybe make one call e.g. Jni.getCachedApplicationContext()
veloce commented 1 month ago

Looks like it is not due to cronet itself. Based on this results I had doubts on what I submitted earlier, so I tried again with cronet_http:1.2.0 and now I see the same error (perhaps I forgot to flutter clean last time?).

For each scenario I used flutter stable and a debug build (and flutter clean in between). Here are the results:

with cronetHttpNoPlay

app-debug.apk_2024_5_31_9_46_1.log

2 Remove dependency on cronet_http

App won't even install, I got this error:

App install failed: INSTALL_FAILED_NO_MATCHING_ABIS

3. remove the dependency on cronet_http but add one on package:jni

With package:jni version 0.9.2: same error

app-debug.apk_2024_5_31_10_14_53.log

With package:jni version 0.7.3, same error.

app-debug.apk_2024_5_31_10_24_1.log

veloce commented 1 month ago

Now I could finally reproduce the problem happening in cronet. I got confused because I was seeing both errors in crashlytics (this one and the libflutter.so not found).

With this Gradle config:

        ndk {
            abiFilters 'armeabi-v7a'
        }

The cronet example app is now running on the Galaxy A11.

And now I can reproduce the difference between cronet_http version 1.2.1+ where I got this error:

05-31 09:53:41.395 E/flutter (19099): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Invalid argument(s):  Unknown public builder cache mode
05-31 09:53:41.395 E/flutter (19099): #0      CronetEngine.build (package:cronet_http/src/cronet_client.dart:137)
05-31 09:53:41.395 E/flutter (19099): #1      main (package:cronet_http_example/main.dart:20)
05-31 09:53:41.395 E/flutter (19099): #2      _runMain.<anonymous closure> (dart:ui/hooks.dart:301)
05-31 09:53:41.395 E/flutter (19099): #3      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297)
05-31 09:53:41.395 E/flutter (19099): #4      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184)
05-31 09:53:41.395 E/flutter (19099): 

app-release.apk_2024_5_31_12_0_30.log

And cronet_http version 1.2.0 where it runs fine.

app-release.apk_2024_5_31_12_16_11.log

EDIT

I also tried with --dart-define=cronetHttpNoPlay=true and got the same error. Also made a debug build to send the logs in case it is useful:

app-debug.apk_2024_5_31_12_35_25.log

brianquinlan commented 1 month ago

It looks like that exception comes from here: https://chromium.googlesource.com/chromium/src/+/lkgr/components/cronet/android/java/src/org/chromium/net/impl/CronetEngineBuilderImpl.java?autodive=0%2F%2F#123

It looks like the documented cache mode constants: https://developer.android.com/develop/connectivity/cronet/reference/org/chromium/net/CronetEngine.Builder.html#constants

Match what package:cronet_http is using: https://github.com/dart-lang/http/blob/6337ee3f6d1f641192ba40e133f27085c69aa815/pkgs/cronet_http/lib/src/cronet_client.dart#L43

What cache mode are you using in CronetEngine.build?

veloce commented 1 month ago

Using whatever CacheMode mode fails actually.

If I remove these lines in CronetClient: https://github.com/dart-lang/http/blob/6337ee3f6d1f641192ba40e133f27085c69aa815/pkgs/cronet_http/lib/src/cronet_client.dart#L105-L109 then it works.

So the origin of the bug is in the new jni_bindings:

https://github.com/dart-lang/http/commit/76deb751b41a675ac214d0ce2b9919f406175137#diff-a5b5707c935a2d47feed8e55b3d6f5a8fa7118d44d6847ea5dc0dd892de91a2fR2549

Where the _enableHttpCache function is changed to:

  static final _enableHttpCache = ProtectedJniExtensions.lookup<
              ffi.NativeFunction<
                  jni.JniResult Function(ffi.Pointer<ffi.Void>,
                      jni.JMethodIDPtr, ffi.VarArgs<(ffi.Int64, ffi.Int64)>)>>(
          "globalEnv_CallObjectMethod")
      .asFunction<
          jni.JniResult Function(
              ffi.Pointer<ffi.Void>, jni.JMethodIDPtr, int, int)>();

Specifically this line I think: jni.JMethodIDPtr, ffi.VarArgs<(ffi.Int64, ffi.Int64)>) ; Making it not compatible anymore with 32 bit devices.

brianquinlan commented 1 month ago

@veloce Amazing analysis!

  static final _id_enableHttpCache = _class.instanceMethodId(
    r"enableHttpCache",
    r"(IJ)Lorg/chromium/net/CronetEngine$Builder;",
  );

+ static final _enableHttpCache = ProtectedJniExtensions.lookup<
+             ffi.NativeFunction<
+                 jni.JniResult Function(ffi.Pointer<ffi.Void>,
+                     jni.JMethodIDPtr, ffi.VarArgs<(ffi.Int64, ffi.Int64)>)>>(
+         "globalEnv_CallObjectMethod")
+     .asFunction<
+         jni.JniResult Function(
+             ffi.Pointer<ffi.Void>, jni.JMethodIDPtr, int, int)>();

  /// from: public org.chromium.net.CronetEngine$Builder enableHttpCache(int i, long j)
  /// The returned object must be released after use, by calling the [release] method.
  CronetEngine_Builder enableHttpCache(
    int i,
    int j,
  ) {
-   return _id_enableHttpCache(
-       this, const $CronetEngine_BuilderType(), [jni.JValueInt(i), j]);
+   return _enableHttpCache(
+           reference.pointer, _id_enableHttpCache as jni.JMethodIDPtr, i, j)
+       .object(const $CronetEngine_BuilderType());
  }
brianquinlan commented 1 month ago

The signature is CronetEngine.Builder enableHttpCache(int cacheMode, long maxSize)

@HosseinYousefi can you take a look at this? This looks like a package:jnigen bug.

HosseinYousefi commented 1 month ago

The signature is CronetEngine.Builder enableHttpCache(int cacheMode, long maxSize)

@HosseinYousefi can you take a look at this? This looks like a package:jnigen bug.

@veloce Thanks for the analysis.

I will fix it.

HosseinYousefi commented 1 month ago

The reason that this is Int64 to begin with is: https://github.com/dart-lang/sdk/issues/55471

From https://github.com/dart-lang/native/pull/1090#issue-2246050136:

For now I'm using the larger types like Int64 and Double, for this to work on my machine. Once the fix for dart-lang/sdk#55471 lands in stable, we can change this. Not sure if this workaround works everywhere, will wait for CI run. – Seems to be working!

Not sure this workaround works everywhere.

Famous last words!

dcharkes commented 1 month ago

The reason that this is Int64 to begin with is: dart-lang/sdk#55471

We've addressed this bug, but it's not yet in a stable release. https://github.com/dart-lang/sdk/commit/c0b938c2286d99be3ff65f62bb3e627dd0fb2361 is in 3.5.0-71.0.dev and later.

So we can either:

For now I'm using the larger types like Int64 and Double, for this to work on my machine. Once the fix for dart-lang/sdk#55471 lands in stable, we can change this. Not sure if this workaround works everywhere, will wait for CI run. – Seems to be working!

Do you have mixing and matching of Double and Float based on arch @HosseinYousefi?

HosseinYousefi commented 1 month ago

The reason that this is Int64 to begin with is: dart-lang/sdk#55471

We've addressed this bug, but it's not yet in a stable release.

https://github.com/dart-lang/sdk/commit/c0b938c2286d99be3ff65f62bb3e627dd0fb2361 is in 3.5.0-71.0.dev and later.

So we can either:

  • Bump JNIgen to only work on a Dart 3.5 dev release / Flutter master branch

  • Request a cherry pick to 3.4.

For now I'm using the larger types like Int64 and Double, for this to work on my machine. Once the fix for dart-lang/sdk#55471 lands in stable, we can change this. Not sure if this workaround works everywhere, will wait for CI run. – Seems to be working!

Do you have mixing and matching of Double and Float based on arch @HosseinYousefi?

Yes, unfortunately I do. Otherwise I could use AbiSpecificInteger for now. Could you please request a cherry pick? The problem is I need to use double instead of float for macOS arm64.

I could revert the change back to using arrays but this problem only happens for macOS arm64 (which is not used much with jnigen), so I'd prefer not to do that as we gain significant performance boost from using varargs.

dcharkes commented 1 month ago

How is using AbiSpecificInteger with double and float a workaround here? Wouldn't that only work properly for types that are doubles/floats? I'm trying to understand whether we should add that as a feature for dart:ffi.

HosseinYousefi commented 1 month ago

How is using AbiSpecificInteger with double and float a workaround here? Wouldn't that only work properly for types that are doubles/floats? I'm trying to understand whether we should add that as a feature for dart:ffi.

It's just a temporary solution to fix the current issue, right? We can create an abi specific "floating point number" for float32 that is only float64 for mac arm64 (and float32 for all other archs). This fixes the varargs issue for mac arm64, while providing the same experience for every other arch.

brianquinlan commented 1 month ago

Could I go back to package:jnigen 0.8 for now and then, when the Dart 3.4.3 is released (with the cherrypick), upgrade package:jnigen and set 3.4.3 as the minimum supported SDK version?

HosseinYousefi commented 1 month ago

Could I go back to package:jnigen 0.8 for now and then, when the Dart 3.4.3 is released (with the cherrypick), upgrade package:jnigen and set 3.4.3 as the minimum supported SDK version?

I'd rather make a version that breaks macOS arm64, as no one really uses jni for macOS for the time being. It will just break the CI which is fine.

I'll open a PR on both repos.

brianquinlan commented 1 month ago

Thanks!