Skip to content

Commit ddedcc3

Browse files
committed
[WIP] Investigating a Mono.Android crash
9-15 22:56:28.431 15070 15090 W monodroid: jclass java_interop_jnienv_find_class(JNIEnv *, jthrowable *, const char *) looking for 'android/content/Intent$ShortcutIconResource' --------- beginning of crash 09-15 22:56:28.432 15070 15090 F libc : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xbb60ca20 in tid 15090 (Instrumentation), pid 15070 (droid.NET_Tests) 09-15 22:56:28.757 15103 15103 F DEBUG : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** 09-15 22:56:28.757 15103 15103 F DEBUG : Build fingerprint: 'google/raven/raven:13/TP1A.220624.021/8877034:user/release-keys' 09-15 22:56:28.757 15103 15103 F DEBUG : Revision: 'MP1.0' 09-15 22:56:28.757 15103 15103 F DEBUG : ABI: 'arm64' 09-15 22:56:28.757 15103 15103 F DEBUG : Timestamp: 2022-09-15 22:56:28.545823727+0200 09-15 22:56:28.757 15103 15103 F DEBUG : Process uptime: 3s 09-15 22:56:28.757 15103 15103 F DEBUG : Cmdline: Mono.Android.NET_Tests 09-15 22:56:28.757 15103 15103 F DEBUG : pid: 15070, tid: 15090, name: Instrumentation >>> Mono.Android.NET_Tests <<< 09-15 22:56:28.757 15103 15103 F DEBUG : uid: 10638 09-15 22:56:28.757 15103 15103 F DEBUG : tagged_addr_ctrl: 0000000000000001 (PR_TAGGED_ADDR_ENABLE) 09-15 22:56:28.757 15103 15103 F DEBUG : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x00000000bb60ca20 09-15 22:56:28.757 15103 15103 F DEBUG : x0 b400006e8ea08350 x1 0000000000000001 x2 0000000000000001 x3 0000000000000010 09-15 22:56:28.757 15103 15103 F DEBUG : x4 0000006dbaea7233 x5 0000006deea682be x6 0000000000000020 x7 0000000000000020 09-15 22:56:28.757 15103 15103 F DEBUG : x8 0000000000000008 x9 00000000bb60c9e0 x10 0000006deea682a7 x11 0000006deea682a7 09-15 22:56:28.757 15103 15103 F DEBUG : x12 3d2120746e696f70 x13 7274706c6c756e20 x14 0000006d3d90b998 x15 000000a74ab3008e 09-15 22:56:28.757 15103 15103 F DEBUG : x16 0000006dbb60f6b8 x17 0000007074e7b9ac x18 0000006d394cc000 x19 0000006d3d90b6d8 09-15 22:56:28.757 15103 15103 F DEBUG : x20 b400006e8ea08350 x21 0000000000000001 x22 0000006d3d90e000 x23 0000006d3d90b6d8 09-15 22:56:28.757 15103 15103 F DEBUG : x24 0000006d3d90b830 x25 0000006d3d90bb80 x26 0000006e7ea14cf0 x27 b400006e8ea08350 09-15 22:56:28.757 15103 15103 F DEBUG : x28 0000000000000000 x29 0000006d3d90b5d0 09-15 22:56:28.757 15103 15103 F DEBUG : lr 0000006dbb16f3dc sp 0000006d3d90b5b0 pc 0000006dbb171d44 pst 0000000080001000 09-15 22:56:28.757 15103 15103 F DEBUG : backtrace: 09-15 22:56:28.757 15103 15103 F DEBUG : #00 pc 0000000000371d44 /apex/com.android.art/lib64/libart.so (art::ArtMethod::PrettyMethod(bool)+76) (BuildId: 56e704c544e6c624201be2ab4933e853) 09-15 22:56:28.757 15103 15103 F DEBUG : #1 pc 000000000036f3d8 /apex/com.android.art/lib64/libart.so (void art::StackVisitor::WalkStack<(art::StackVisitor::CountTransitions)0>(bool)+5464) (BuildId: 56e704c544e6c624201be2ab4933e853) 09-15 22:56:28.757 15103 15103 F DEBUG : #2 pc 00000000005a0370 /apex/com.android.art/lib64/libart.so (art::JNI<true>::FindClass(_JNIEnv*, char const*)+480) (BuildId: 56e704c544e6c624201be2ab4933e853) 09-15 22:56:28.757 15103 15103 F DEBUG : #3 pc 00000000005c8948 /apex/com.android.art/lib64/libart.so (art::(anonymous namespace)::CheckJNI::FindClass(_JNIEnv*, char const*) (.__uniq.99033978352804627313491551960229047428.llvm.5591279935177935698)+228) (BuildId: 56e704c544e6c624201be2ab4933e853) 09-15 22:56:28.757 15103 15103 F DEBUG : dotnet#4 pc 000000000004de28 /data/app/~~aJedheWQLPfk1ulUOfKVyg==/Mono.Android.NET_Tests-XvAL5W7BvZDwkEbYfmLTIQ==/lib/arm64/libmonodroid.so (java_interop_jnienv_find_class+84) (BuildId: a92c56b31adcb233a4674b5eb523c0aaa67a811d) 09-15 22:56:28.757 15103 15103 F DEBUG : dotnet#5 pc 000000000000b220 <anonymous:705fcb1000>
1 parent 32347b7 commit ddedcc3

File tree

6 files changed

+25
-3
lines changed

6 files changed

+25
-3
lines changed

src/Mono.Android/Android.Runtime/JNIEnv.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,15 +420,21 @@ public static unsafe void FinishCreateInstance (IntPtr instance, string jniCtorS
420420
public static unsafe void InvokeConstructor (IntPtr instance, string jniCtorSignature, JValue* constructorParameters)
421421
{
422422
IntPtr lrefClass = GetObjectClass (instance);
423+
monodroid_log (LogLevel.Warn, LogCategories.Default, $"InvokeConstructor: lrefClass == {lrefClass}");
423424
try {
424425
IntPtr ctor = JNIEnv.GetMethodID (lrefClass, "<init>", jniCtorSignature);
425426
if (ctor == IntPtr.Zero)
426427
throw new ArgumentException (string.Format ("Could not find constructor JNI signature '{0}' on type '{1}'.",
427428
jniCtorSignature, Java.Interop.TypeManager.GetClassName (lrefClass)));
428429
CallNonvirtualVoidMethod (instance, lrefClass, ctor, constructorParameters);
430+
} catch (Exception ex) {
431+
monodroid_log (LogLevel.Warn, LogCategories.Default, $"Exception in InvokeConstructor: {ex.GetType()}");
432+
// throw;
429433
} finally {
430-
DeleteLocalRef (lrefClass);
434+
//DeleteLocalRef (lrefClass);
431435
}
436+
437+
// DeleteLocalRef (lrefClass);
432438
}
433439

434440
public static unsafe void InvokeConstructor (IntPtr instance, string jniCtorSignature, params JValue[] constructorParameters)

src/monodroid/jni/java_interop_api.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66

77
#include "java_interop_api.h"
8+
#ifdef ANDROID
9+
#include <android/log.h>
10+
#endif
811

912
JI_API jint
1013
java_interop_jnienv_get_version (JNIEnv *env)
@@ -25,6 +28,9 @@ java_interop_jnienv_define_class (JNIEnv *env, jthrowable *_thrown, const char*
2528
JI_API jclass
2629
java_interop_jnienv_find_class (JNIEnv *env, jthrowable *_thrown, const char* classname)
2730
{
31+
#if defined (ANDROID) && defined (NET)
32+
__android_log_print (ANDROID_LOG_WARN, "monodroid", "%s looking for '%s'", __PRETTY_FUNCTION__, classname);
33+
#endif
2834
*_thrown = 0;
2935
jclass _r_ = (*env)->FindClass (env, classname);
3036
*_thrown = (*env)->ExceptionOccurred (env);

src/monodroid/jni/xamarin-android-app-context.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <mono/metadata/class.h>
2+
#include <mono/metadata/debug-helpers.h>
23

34
#include "monodroid-glue-internal.hh"
45
#include "mono-image-loader.hh"
@@ -45,6 +46,7 @@ MonodroidRuntime::get_function_pointer (uint32_t mono_image_index, uint32_t clas
4546
get_class_name (class_index), class_index
4647
);
4748

49+
log_warn (LOG_DEFAULT, " mono_image_index == %u; class_index == %u; method_token == 0x%x", mono_image_index, class_index, method_token);
4850
if (XA_UNLIKELY (class_index >= marshal_methods_number_of_classes)) {
4951
log_fatal (LOG_DEFAULT,
5052
"Internal error: invalid index for class cache (expected at most %u, got %u)",
@@ -54,6 +56,9 @@ MonodroidRuntime::get_function_pointer (uint32_t mono_image_index, uint32_t clas
5456
abort ();
5557
}
5658

59+
// We need to do that, as Mono APIs cannot be invoked from threads that aren't attached to the runtime.
60+
mono_thread_attach (mono_get_root_domain());
61+
5762
// We don't check for valid return values from image loader, class and method lookup because if any
5863
// of them fails to find the requested entity, they will return `null`. In consequence, we can pass
5964
// these pointers without checking all the way to `mono_method_get_unmanaged_callers_only_ftnptr`, after
@@ -66,7 +71,10 @@ MonodroidRuntime::get_function_pointer (uint32_t mono_image_index, uint32_t clas
6671

6772
MonoMethod *method = mono_get_method (image, method_token, klass.klass);
6873
MonoError error;
74+
log_warn (LOG_DEFAULT, " method == %p (mono_image_index == %u; class_index == %u; method_token == 0x%x)", method, mono_image_index, class_index, method_token);
75+
log_warn (LOG_DEFAULT, " pointer to method %s == %p, trying to get funcptr for it (mono_image_index == %u; class_index == %u; method_token == 0x%x)", mono_method_full_name (method, true), method, mono_image_index, class_index, method_token);
6976
void *ret = mono_method_get_unmanaged_callers_only_ftnptr (method, &error);
77+
log_warn (LOG_DEFAULT, " obtained pointer == %p (mono_image_index == %u; class_index == %u; method_token == 0x%x)", ret, mono_image_index, class_index, method_token);
7078

7179
if (XA_LIKELY (ret != nullptr)) {
7280
if constexpr (NeedsLocking) {
@@ -75,7 +83,7 @@ MonodroidRuntime::get_function_pointer (uint32_t mono_image_index, uint32_t clas
7583
target_ptr = ret;
7684
}
7785

78-
log_debug (LOG_ASSEMBLY, "Loaded pointer to method %s (%p)", mono_method_get_name (method), ret);
86+
log_debug (LOG_ASSEMBLY, "Loaded pointer to method %s (%p) (mono_image_index == %u; class_index == %u; method_token == 0x%x)", mono_method_full_name (method, true), ret, mono_image_index, class_index, method_token);
7987
return;
8088
}
8189

tests/Mono.Android-Tests/Java.Interop-Tests/Java.Interop-Tests.NET.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<Compile Include="$(JavaInteropTestDirectory)**\*.cs" Exclude="$(JavaInteropTestDirectory)obj\**;$(JavaInteropTestDirectory)bin\**" />
2525
<Compile Remove="$(JavaInteropTestDirectory)Java.Interop\JavaVMFixture.cs" />
2626
<Compile Remove="$(JavaInteropTestDirectory)Java.Interop\JniReferenceSafeHandleTest.cs" />
27+
<Compile Remove="$(JavaInteropTestDirectory)obj\Release\net7.0-android/designtime/Resource.designer.cs" />
2728
</ItemGroup>
2829

2930
<ItemGroup>

tests/Mono.Android-Tests/Runtime-Microsoft.Android.Sdk/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="Mono.Android.NET_Tests">
22
<uses-sdk android:minSdkVersion="21" />
3-
<application android:label="Microsoft.Android.Runtime" android:name="android.apptests.App" android:usesCleartextTraffic="true">
3+
<application android:label="Microsoft.Android.Runtime" android:name="android.apptests.App" android:usesCleartextTraffic="true" android:debuggable="true">
44
<activity android:name="android.apptests.RenamedActivity" android:configChanges="keyboardHidden" />
55
</application>
66
<uses-permission android:name="android.permission.INTERNET" />

tests/Mono.Android-Tests/Runtime-Microsoft.Android.Sdk/Mono.Android.NET-Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
<ItemGroup>
4343
<Compile Remove="..\Resources\Resource.designer.cs" />
44+
<Compile Remove="..\Java.Interop-Tests\obj\Release\net7.0-android\designtime\Resource.designer.cs" />
4445
<Compile Include="System\AppContextTests.cs" />
4546
<!-- Mono.Data.Sqlite is not supported in .NET 6 -->
4647
<Compile Remove="..\Mono.Data.Sqlite\SqliteTests.cs" />

0 commit comments

Comments
 (0)