Commit db84cc3
committed
Try to create a JreRuntime
BROKEN BROKEN BROKEN BROKEN
What do we want? To create a `JreRuntime` instance!
…which is shockingly difficult.
But first, *why* do we want to create a `JreRuntime` instance?
(It's not like we've needed it so far!)
Force the issue by changing `NativeAOTInit.sayHello()` to return a
`String` instance, thus requiring that for `NativeAOTInit.sayHello()`
to return a non-`null` value, it needs a `JreRuntime` instance!
This is where everything falls apart. `JreRuntime` and
`JreRuntimeOptions` were not designed with this scenario in mind!
Update `JreRuntime` & co so that it doesn't require `JvmLibraryPath`
when `InvocationPointer` isn't null, and don't require that
`JreRuntimeOptions.ClassPath` contain the path to `java-interop.jar`
when `InvocationPointer` isn't null.
This allows us to create the `JreRuntimeOptions` instance. Yay!
The next problem is that setting
`%(ProjectReference.AdditionalProperties)` to include `Standalone=true`
doesn't appear to work, in that the `Java.Interop.dll` that is
included still contains P/Invokes instead of the `JniNativeMethods`
class. Update `Java.Interop.csproj` so that `$(Standalone)`=true
is now the default. (This *shouldn't* break anybody, but… it now means
the P/Invoke backend is getting NO testing. ¯\_(ツ)_/¯ )
The next part is where it blows up GOOD: `options.CreateJreVM()`
throws, which aborts everything:
% (cd bin/Release/osx-x64/publish ; java -cp hello-from-java.jar:java-interop.jar com/microsoft/hello_from_jni/App)
Hello from Java!
# jonp: JNI_OnLoad: vm=10a9c0b00
# jonp: JNI_OnLoad: created options…
# jonp: builder.InvocationPointer=10a9c0b00
JNI_OnLoad: error: System.TypeInitializationException: A type initializer threw an exception. To determine which type, inspect the InnerException's StackTrace property.
---> System.NotSupportedException: 'Java.Interop.ManagedPeer+ConstructMarshalMethod' is missing delegate marshalling data. This can happen for code that is not compatible with AOT. Inspect and fix AOT related warnings that were generated when the app was published. For more information see https://aka.ms/nativeaot-compatibility
at Internal.Runtime.CompilerHelpers.RuntimeInteropData.GetDelegateMarshallingStub(RuntimeTypeHandle, Boolean) + 0x78
at System.Runtime.InteropServices.PInvokeMarshal.AllocateThunk(Delegate del) + 0x6b
at System.Runtime.CompilerServices.ConditionalWeakTable`2.GetValueLocked(TKey, ConditionalWeakTable`2.CreateValueCallback) + 0x27
at System.Runtime.CompilerServices.ConditionalWeakTable`2.GetValue(TKey, ConditionalWeakTable`2.CreateValueCallback) + 0x41
at System.Runtime.InteropServices.PInvokeMarshal.GetFunctionPointerForDelegate(Delegate) + 0xd5
at libHello-NativeAOTFromJNI!<BaseAddress>+0x8adeb
at Java.Interop.JniEnvironment.Types._RegisterNatives(JniObjectReference, JniNativeMethodRegistration[], Int32) + 0x90
at Java.Interop.JniEnvironment.Types.RegisterNatives(JniObjectReference, JniNativeMethodRegistration[], Int32) + 0x65
at Java.Interop.JniType.RegisterNativeMethods(JniNativeMethodRegistration[]) + 0x30
at Java.Interop.ManagedPeer..cctor() + 0x175
at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0xb4
--- End of inner exception stack trace ---
at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0x147
at System.Runtime.CompilerServices.ClassConstructorRunner.CheckStaticClassConstructionReturnGCStaticBase(StaticClassConstructionContext*, Object) + 0x9
at Java.Interop.JniRuntime..ctor(JniRuntime.CreationOptions) + 0x5a2
at Java.Interop.JreRuntime..ctor(JreRuntimeOptions) + 0x22
at Hello_NativeAOTFromJNI.JNIEnvInit.JNI_OnLoad(IntPtr, IntPtr) + 0x9e
Exception in thread "main" java.lang.UnsatisfiedLinkError: unsupported JNI version 0x00000000 required by /Volumes/Xamarin-Work/src/xamarin/Java.Interop/samples/Hello-NativeAOTFromJNI/bin/Release/osx-x64/publish/libHello-NativeAOTFromJNI.dylib
at java.base/jdk.internal.loader.NativeLibraries.load(Native Method)
at java.base/jdk.internal.loader.NativeLibraries$NativeLibraryImpl.open(NativeLibraries.java:388)
at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:232)
at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:174)
at java.base/jdk.internal.loader.NativeLibraries.findFromPaths(NativeLibraries.java:315)
at java.base/jdk.internal.loader.NativeLibraries.loadLibrary(NativeLibraries.java:287)
at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2427)
at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:818)
at java.base/java.lang.System.loadLibrary(System.java:1989)
at com.microsoft.hello_from_jni.NativeAOTInit.<clinit>(NativeAOTInit.java:5)
at com.microsoft.hello_from_jni.App.main(App.java:7)
Within the `JniRuntime` constructor, we're hitting:
#if !XA_JI_EXCLUDE
ManagedPeer.Init ();
#endif // !XA_JI_EXCLUDE
This invokes the `ManagedPeer` static constructor, which attempts to
call `JniType.RegisterNativeMethods()`, which attempts to call
`JNIEnv::RegisterNatives()`, but before it can get that far it needs
to marshal things:
1. The `JniNativeMethodRegistration` struct, which in turn requires
2. The `JniNativeMethodRegistration.Marshaler` delegate field.
This apparently isn't supported by NativeAOT, at least not without
additional work that I am not currently privy to.
Given that `JniNativeMethodRegistration` structure marshaling is how
*all* JNI method registration is done in .NET Android, this is a bit
of a blocker for this sample.
TODO? Figure out how to allow NativeAOT to marshal
`JniNativeMethodRegistration` with a minimum of effort?
TODO? *Require* `jnimarshalmethod-gen`, and update it so that it
emits `[UnmanagedCallersOnlyAttribute]` on all generated marshal
methods *and* emits `UnmanagedCallersOnlyAttribute.EntryPoint` to
a `Java_…` symbol name so that we *don't* hit the
`JniNativeMethodRegistration` struct marshaling codepath.
(See also 77800dd).
[0]:https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html#RegisterNatives1 parent 007317b commit db84cc3
File tree
6 files changed
+42
-15
lines changed- samples/Hello-NativeAOTFromJNI
- java/com/microsoft/hello_from_jni
- src
- Java.Interop
- Java.Runtime.Environment/Java.Interop
6 files changed
+42
-15
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | | - | |
16 | | - | |
17 | | - | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
18 | 23 | | |
19 | 24 | | |
20 | | - | |
| 25 | + | |
21 | 26 | | |
22 | 27 | | |
23 | 28 | | |
| |||
30 | 35 | | |
31 | 36 | | |
32 | 37 | | |
33 | | - | |
| 38 | + | |
34 | 39 | | |
35 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
36 | 46 | | |
37 | 47 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
55 | 59 | | |
56 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
57 | 64 | | |
58 | 65 | | |
59 | 66 | | |
| |||
Lines changed: 2 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
| 42 | + | |
47 | 43 | | |
48 | 44 | | |
49 | 45 | | |
| |||
80 | 76 | | |
81 | 77 | | |
82 | 78 | | |
83 | | - | |
| 79 | + | |
84 | 80 | | |
85 | 81 | | |
86 | 82 | | |
| |||
99 | 95 | | |
100 | 96 | | |
101 | 97 | | |
| 98 | + | |
102 | 99 | | |
103 | 100 | | |
| 101 | + | |
104 | 102 | | |
105 | 103 | | |
106 | 104 | | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
107 | 115 | | |
108 | 116 | | |
109 | 117 | | |
| |||
0 commit comments