Skip to content

Commit

Permalink
[WIP] Investigating a Mono.Android crash
Browse files Browse the repository at this point in the history
    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>
  • Loading branch information
grendello committed Sep 15, 2022
1 parent 32347b7 commit ddedcc3
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/Mono.Android/Android.Runtime/JNIEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,15 +420,21 @@ public static unsafe void FinishCreateInstance (IntPtr instance, string jniCtorS
public static unsafe void InvokeConstructor (IntPtr instance, string jniCtorSignature, JValue* constructorParameters)
{
IntPtr lrefClass = GetObjectClass (instance);
monodroid_log (LogLevel.Warn, LogCategories.Default, $"InvokeConstructor: lrefClass == {lrefClass}");
try {
IntPtr ctor = JNIEnv.GetMethodID (lrefClass, "<init>", jniCtorSignature);
if (ctor == IntPtr.Zero)
throw new ArgumentException (string.Format ("Could not find constructor JNI signature '{0}' on type '{1}'.",
jniCtorSignature, Java.Interop.TypeManager.GetClassName (lrefClass)));
CallNonvirtualVoidMethod (instance, lrefClass, ctor, constructorParameters);
} catch (Exception ex) {
monodroid_log (LogLevel.Warn, LogCategories.Default, $"Exception in InvokeConstructor: {ex.GetType()}");
// throw;
} finally {
DeleteLocalRef (lrefClass);
//DeleteLocalRef (lrefClass);
}

// DeleteLocalRef (lrefClass);
}

public static unsafe void InvokeConstructor (IntPtr instance, string jniCtorSignature, params JValue[] constructorParameters)
Expand Down
6 changes: 6 additions & 0 deletions src/monodroid/jni/java_interop_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/

#include "java_interop_api.h"
#ifdef ANDROID
#include <android/log.h>
#endif

JI_API jint
java_interop_jnienv_get_version (JNIEnv *env)
Expand All @@ -25,6 +28,9 @@ java_interop_jnienv_define_class (JNIEnv *env, jthrowable *_thrown, const char*
JI_API jclass
java_interop_jnienv_find_class (JNIEnv *env, jthrowable *_thrown, const char* classname)
{
#if defined (ANDROID) && defined (NET)
__android_log_print (ANDROID_LOG_WARN, "monodroid", "%s looking for '%s'", __PRETTY_FUNCTION__, classname);
#endif
*_thrown = 0;
jclass _r_ = (*env)->FindClass (env, classname);
*_thrown = (*env)->ExceptionOccurred (env);
Expand Down
10 changes: 9 additions & 1 deletion src/monodroid/jni/xamarin-android-app-context.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <mono/metadata/class.h>
#include <mono/metadata/debug-helpers.h>

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

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

// We need to do that, as Mono APIs cannot be invoked from threads that aren't attached to the runtime.
mono_thread_attach (mono_get_root_domain());

// We don't check for valid return values from image loader, class and method lookup because if any
// of them fails to find the requested entity, they will return `null`. In consequence, we can pass
// these pointers without checking all the way to `mono_method_get_unmanaged_callers_only_ftnptr`, after
Expand All @@ -66,7 +71,10 @@ MonodroidRuntime::get_function_pointer (uint32_t mono_image_index, uint32_t clas

MonoMethod *method = mono_get_method (image, method_token, klass.klass);
MonoError error;
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);
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);
void *ret = mono_method_get_unmanaged_callers_only_ftnptr (method, &error);
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);

if (XA_LIKELY (ret != nullptr)) {
if constexpr (NeedsLocking) {
Expand All @@ -75,7 +83,7 @@ MonodroidRuntime::get_function_pointer (uint32_t mono_image_index, uint32_t clas
target_ptr = ret;
}

log_debug (LOG_ASSEMBLY, "Loaded pointer to method %s (%p)", mono_method_get_name (method), ret);
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);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<Compile Include="$(JavaInteropTestDirectory)**\*.cs" Exclude="$(JavaInteropTestDirectory)obj\**;$(JavaInteropTestDirectory)bin\**" />
<Compile Remove="$(JavaInteropTestDirectory)Java.Interop\JavaVMFixture.cs" />
<Compile Remove="$(JavaInteropTestDirectory)Java.Interop\JniReferenceSafeHandleTest.cs" />
<Compile Remove="$(JavaInteropTestDirectory)obj\Release\net7.0-android/designtime/Resource.designer.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="Mono.Android.NET_Tests">
<uses-sdk android:minSdkVersion="21" />
<application android:label="Microsoft.Android.Runtime" android:name="android.apptests.App" android:usesCleartextTraffic="true">
<application android:label="Microsoft.Android.Runtime" android:name="android.apptests.App" android:usesCleartextTraffic="true" android:debuggable="true">
<activity android:name="android.apptests.RenamedActivity" android:configChanges="keyboardHidden" />
</application>
<uses-permission android:name="android.permission.INTERNET" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

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

0 comments on commit ddedcc3

Please sign in to comment.