Commit 06b1d7f
[monodroid] typemaps may need to load assemblies (#8625)
Context: dotnet/java-interop@005c914
Context: dotnet/java-interop#1181
Context: 25d1f00
When attempting to bump to dotnet/java-interop@005c9141, multiple
unit tests would fail, e.g.
Java.Lang.LinkageError : net.dot.jni.test.CallVirtualFromConstructorDerived
----> System.NotSupportedException : Could not find System.Type corresponding to Java type JniTypeSignature(TypeName=net/dot/jni/test/CallVirtualFromConstructorDerived ArrayRank=0 Keyword=False) .
This happened because dotnet/java-interop@005c9141 implicitly
required that typemaps exist for `Java.Interop.JavaObject` subclasses.
Fair enough; enter xamasrin/java.interop#1181, which added support to
`Java.Interop.Tools.JavaCallableWrappers` to emit typemaps for
`Java.Interop.JavaObject` subclasses.
That caused *crashes* in `tests/Mono.Android-Tests`:
E droid.NET_Test: JNI ERROR (app bug): accessed stale Local 0x75 (index 7 in a table of size 7)
F droid.NET_Test: java_vm_ext.cc:570] JNI DETECTED ERROR IN APPLICATION: use of deleted local reference 0x75
…
F droid.NET_Test: runtime.cc:630] native: #13 pc 00000000003ce865 /apex/com.android.runtime/lib64/libart.so (art::(anonymous namespace)::CheckJNI::GetObjectClass(_JNIEnv*, _jobject*)+837)
The immediate cause of the crash was a "use after free" bug within
`TypeManager.CreateInstance()` in a never-hit-before error path; the
"use after free" bug was fixed in 25d1f00.
However, the cause of the hitting a never-hit-before error path is
because `EmbeddedAssemblies::typemap_java_to_managed()` would only
map Java types to `System.Type` instances for assemblies that have
*already been loaded*. If the assembly had not yet been loaded, then
`EmbeddedAssemblies::typemap_java_to_managed()` would return `null`,
and if the binding it couldn't find happens to be for
`java.lang.Object`, we hit the (buggy!) "Where is the Java.Lang.Object
wrapper" error condition.
Commit 25d1f00 fixes that and a great many other related issues.
What's left is `EmbeddedAssemblies::typemap_java_to_managed()`:
it should *never* return null *unless* there is no typemap at all.
Whether the target assembly has been loaded or not should be
irrelevant.
Update `EmbeddedAssemblies::typemap_java_to_managed()` so that it
will load the target assembly if necessary.
Additionally, before we figured out that we had a "use after free"
bug, all we had to go on was that *something* related to
`JNIEnv::GetObjectClass()` was involved. Review JNI usage around
`JNIEnv::GetObjectClass()` and related invocations, and cleanup:
* Simplify logic related to `JNIEnv::DeleteLocalRef()`.
* Decrease scope of local variables.
* Clear variables passed to `JNIEnv.DeleteLocalRef()`.
Co-authored-by: Jonathan Pryor <[email protected]>
Co-authored-by: Marek Habersack <[email protected]>1 parent bf73889 commit 06b1d7f
File tree
3 files changed
+36
-9
lines changed- src
- Mono.Android/Java.Interop
- monodroid/jni
3 files changed
+36
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
267 | 267 | | |
268 | 268 | | |
269 | 269 | | |
270 | | - | |
| 270 | + | |
271 | 271 | | |
272 | 272 | | |
273 | 273 | | |
| |||
279 | 279 | | |
280 | 280 | | |
281 | 281 | | |
| 282 | + | |
282 | 283 | | |
283 | | - | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
284 | 287 | | |
285 | 288 | | |
286 | 289 | | |
287 | | - | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
288 | 294 | | |
289 | 295 | | |
290 | 296 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
695 | 695 | | |
696 | 696 | | |
697 | 697 | | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
698 | 722 | | |
699 | 723 | | |
700 | 724 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
657 | 657 | | |
658 | 658 | | |
659 | 659 | | |
| 660 | + | |
660 | 661 | | |
661 | 662 | | |
662 | | - | |
663 | | - | |
664 | 663 | | |
665 | 664 | | |
666 | 665 | | |
667 | 666 | | |
668 | | - | |
669 | 667 | | |
670 | 668 | | |
671 | 669 | | |
| |||
910 | 908 | | |
911 | 909 | | |
912 | 910 | | |
913 | | - | |
914 | 911 | | |
915 | 912 | | |
916 | 913 | | |
| |||
942 | 939 | | |
943 | 940 | | |
944 | 941 | | |
945 | | - | |
| 942 | + | |
946 | 943 | | |
| 944 | + | |
947 | 945 | | |
948 | 946 | | |
949 | 947 | | |
| |||
957 | 955 | | |
958 | 956 | | |
959 | 957 | | |
960 | | - | |
961 | 958 | | |
962 | 959 | | |
963 | 960 | | |
| |||
0 commit comments