android-ndk-rs
android-ndk-rs copied to clipboard
Error 'use of invalid object' when any attempts ot interacting with activity instance using JNI
I don't sure, this problem actually related to android-ndk-sys nor jni crate but it looks very strange for me. Also I don't quite familiar with JNI, so I may do wrong things here.
My rust code looks like a piece below:
let app = unsafe { AndroidApp::from_ptr(android_glue::get_android_app()) };
let activity = app.activity();
let sdk_version = activity.sdk_version();
println!("SDK VERSION: {}", sdk_version);
let env = vm.attach_current_thread()?;
println!("ENV: {:?}", env.get_version());
println!("Activity: {:?}", activity);
let class = env.find_class("android/app/NativeActivity")?;
println!("Found NativeActivity class: {:?}", class);
let class = env.get_object_class(activity)?;
println!("Actual Activity class: {:?}", class);
But application failed on last line.
12-30 21:33:17.419 10414 10429 D RustStdoutStderr: SDK VERSION: 28
12-30 21:33:17.420 10414 10429 D RustStdoutStderr: ENV: Ok(V6)
12-30 21:33:17.420 10414 10429 D RustStdoutStderr: Activity: JObject { internal: 0x99f0a60c, lifetime: PhantomData }
12-30 21:33:17.420 10414 10429 D RustStdoutStderr: Found NativeActivity class: JClass(JObject { internal: 0x1, lifetime: PhantomData })
12-30 21:33:17.522 10414 10430 F .rust_oboe_dem: java_vm_ext.cc:542] JNI DETECTED ERROR IN APPLICATION: use of invalid jobject 0x99f0a60c
It seems, activity instance object is invalid. But I understand that it may not be what it seems.
I found that I runs armv7 binary on aarch64 OS. Despite the fact that it works this may be one of possible source of problems.
When I runs my app on armv7 OS I gets the follow:
D/RustStdoutStderr(30168): SDK VERSION: 18
D/RustStdoutStderr(30168): ENV: Ok(V6)
D/RustStdoutStderr(30168): Activity: JObject { internal: 0x418d0edc, lifetime: PhantomData }
D/RustStdoutStderr(30168): Found NativeActivity class: JClass(JObject { internal: 0x1d200001, lifetime: PhantomData })
W/dalvikvm(30168): Invalid indirect reference 0x418d0edc in decodeIndirectRef
So the problem still has.
When I run aarch64 on aarch64 I cannot attach to current thread at all due to abort without any message somewhere in libc:
01-01 10:33:15.456 16090 16106 D RustStdoutStderr: SDK VERSION: 28
01-01 10:33:15.470 16090 16107 F libc : Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 16107 (.rust_oboe_demo), pid 16090 (.rust_oboe_demo)
...
01-01 10:33:15.653 16112 16112 F DEBUG : signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
01-01 10:33:15.653 16112 16112 F DEBUG : x0 0000000000000000 x1 0000000000003eeb x2 0000000000000006 x3 0000000000000008
01-01 10:33:15.653 16112 16112 F DEBUG : x4 0000007dfde11200 x5 0000007dfde11200 x6 0000007dfde11200 x7 0000007dfde11000
01-01 10:33:15.653 16112 16112 F DEBUG : x8 0000000000000083 x9 0000007ef69bbb38 x10 fffffff87ffffbdf x11 0000000000000001
01-01 10:33:15.653 16112 16112 F DEBUG : x12 0000000000000000 x13 0000000000000000 x14 ffffffffffffffff x15 0000000000000000
01-01 10:33:15.653 16112 16112 F DEBUG : x16 0000007ef69f32a8 x17 0000007ef69312c8 x18 0000007e5eb2b7d8 x19 0000000000003eda
01-01 10:33:15.653 16112 16112 F DEBUG : x20 0000000000003eeb x21 0000000000000083 x22 0000007dfe038b1c x23 0000007dfdf3b820
01-01 10:33:15.653 16112 16112 F DEBUG : x24 0000007dfdf57570 x25 0000007dfde5a000 x26 0000007ef76f95e0 x27 0000007dfe096c08
01-01 10:33:15.653 16112 16112 F DEBUG : x28 000000000000001c x29 0000007dfdf3a660
01-01 10:33:15.653 16112 16112 F DEBUG : sp 0000007dfdf3a620 lr 0000007ef6925ad0 pc 0000007ef6925afc
01-01 10:33:15.654 16112 16112 F DEBUG :
01-01 10:33:15.654 16112 16112 F DEBUG : backtrace:
01-01 10:33:15.654 16112 16112 F DEBUG : #00 pc 0000000000021afc /system/lib64/libc.so (abort+124)
...
https://github.com/mb64/android-ndk-rs/blob/28afac855f6fa973660718dd7371a2ff5e8f6f10/android-ndk/src/native_activity.rs#L103-L107
It seems the unnecessary reference operator here because clazz already is a pointer. I mean this fragment should looks like:
jni::objects::JObject::from(self.ptr.as_ref().clazz as *const _ as jni::sys::jobject)