Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jnimarshalmethod-gen] Finish support of delegates for methods with 15+ parameters #709

Open
radekdoulik opened this issue Sep 7, 2020 · 0 comments
Labels
callable-wrappers Issues with Java Callable Wrappers enhancement Proposed change to current functionality

Comments

@radekdoulik
Copy link
Member

Commit 857b9a9 introduced the support of 15+ parameters methods. The changes in jnimarshalmethod-gen are incomplete though and we end up with a generated code, which uses reflection to get a type, which doesn't exist.

Like the generated code here (from Mono.Android-JniMarshalMethods.dll):

    IL_0122:  ldstr      "Delegate18$1"
    IL_0127:  ldc.i4.1
    IL_0128:  call       [mscorlib]System.Type [mscorlib]System.Type::GetType(string, bool)

It happens, because the Delegate18$1 type is created by SLE on the fly and exists in memory only. jnimarshalmethod-gen doesn't know about that yet.

We need to save the in-memory-only type to our marshaling class and change the name to reflect the parameter types.

Currently the in-memory-only type is placed in Snippets assembly. An example:

assembly qualified name: Delegate18$1, Snippets, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
the delegate:            lambda (__jnienv, __class, srcName, srcTarget, srcLevel, srcX, srcY, srcZ, dstName, dstTarget, dstLevel, dstX, dstY, dstZ, srcWidth, srcHeight, srcDepth) => {var __envp;var __jvm; ... }

We should save the type from the example to the assembly containing the marshaling __<$>_jni_marshal_methods class and name it something like __<$>_JniMarshal_PPIIIIIIIIIIIIIII_V or maybe to be more consistent as __<$>_jni_marshal_PPIIIIIIIIIIIIIII_V. And also update the generated code to use it.

@radekdoulik radekdoulik self-assigned this Sep 7, 2020
@radekdoulik radekdoulik changed the title Fix delegates for methods with 15+ parameters [jnimarshalmethod-gen] Finish support of delegates for methods with 15+ parameters Sep 7, 2020
@jpobst jpobst added enhancement Proposed change to current functionality callable-wrappers Issues with Java Callable Wrappers labels Sep 8, 2020
radekdoulik added a commit that referenced this issue Sep 8, 2020
The TypeMover was crashing, when we cannot resolve the type during
register method optimization.

Like this:

    Value cannot be null.
    Parameter name: key
    System.ArgumentNullException: Value cannot be null.
    Parameter name: key
      at System.Collections.Generic.Dictionary`2[TKey,TValue].FindEntry (TKey key) [0x00175] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/debug/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/Dictionary.cs:470
      at System.Collections.Generic.Dictionary`2[TKey,TValue].ContainsKey (TKey key) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/debug/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/Dictionary.cs:286
      at Xamarin.Android.Tools.JniMarshalMethodGenerator.TypeMover.Resolve (Mono.Cecil.TypeReference type) [0x00001] in /Users/rodo/git/xa-clean/external/Java.Interop/tools/jnimarshalmethod-gen/TypeMover.cs:143
      at Xamarin.Android.Tools.JniMarshalMethodGenerator.TypeMover.GetActionConstructor (Mono.Cecil.TypeReference type, Mono.Cecil.ModuleDefinition module) [0x00012] in /Users/rodo/git/xa-clean/external/Java.Interop/tools/jnimarshalmethod-gen/TypeMover.cs:335
      at Xamarin.Android.Tools.JniMarshalMethodGenerator.TypeMover.AnalyzeAndImprove (Mono.Collections.Generic.Collection`1[T] instructions, Mono.Collections.Generic.Collection`1[T] newInstructions, System.Int32 idx, System.String typeName, System.Int32& skipCount, Mono.Cecil.ModuleDefinition module) [0x003b6] in /Users/rodo/git/xa-clean/external/Java.Interop/tools/jnimarshalmethod-gen/TypeMover.cs:417

The tool was crashing on IL like this:

    IL_0122:  ldstr      "Delegate18$1"
    IL_0127:  ldc.i4.1
    IL_0128:  call       [mscorlib]System.Type [mscorlib]System.Type::GetType(string,
                                                                              bool)

The fix makes us more error resistant. It doesn't fix the source of the problem,
which will be fixed elsewhere.

More information about the issue which led to the crash: #709
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
callable-wrappers Issues with Java Callable Wrappers enhancement Proposed change to current functionality
Projects
None yet
Development

No branches or pull requests

2 participants