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

[XABT] Avoid non-blittable types in native callback methods #9724

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jpobst
Copy link
Contributor

@jpobst jpobst commented Jan 29, 2025

Context: dotnet/java-interop#1296
Context: dotnet/java-interop#1027

Changing callback methods to blittable types (bool -> sbyte and char -> ushort) causes warnings when the marshal method classifier verifies that the marshal types match the native types:

Xamarin.Android.Common.targets(1502,3): Method 'System.SByte Java.Lang.Object::n_Equals_Ljava_lang_Object_(System.IntPtr,System.IntPtr,System.IntPtr)' doesn't match native callback signature (invalid return type: expected 'System.Boolean', found 'System.SByte')
Xamarin.Android.Common.targets(1502,3): Unable to find native callback method 'n_Equals_Ljava_lang_Object_' in type 'Java.Lang.Object', matching the 'System.Boolean Java.Lang.Object::Equals(Java.Lang.Object)' signature (jniName: 'equals') [Arch: X86_64; Assembly: obj/Release/android-x64/linked/Mono.Android.dll]
Xamarin.Android.Common.targets(1502,3): Method 'System.Boolean Java.Lang.Object::Equals(Java.Lang.Object)' will be registered dynamically [Arch: X86_64; Assembly: obj/Release/android-x64/linked/Mono.Android.dll]
Xamarin.Android.Common.targets(1502,3): Method 'System.SByte Java.Lang.Object::n_Equals_Ljava_lang_Object_(System.IntPtr,System.IntPtr,System.IntPtr)' doesn't match native callback signature (invalid return type: expected 'System.Boolean', found 'System.SByte')
Xamarin.Android.Common.targets(1502,3): Unable to find native callback method 'n_Equals_Ljava_lang_Object_' in type 'Java.Lang.Object', matching the 'System.Boolean Java.Lang.Object::Equals(Java.Lang.Object)' signature (jniName: 'equals') [Arch: Arm64; Assembly: obj/Release/android-arm64/linked/Mono.Android.dll]
Xamarin.Android.Common.targets(1502,3): Method 'System.Boolean Java.Lang.Object::Equals(Java.Lang.Object)' will be registered dynamically [Arch: Arm64; Assembly: obj/Release/android-arm64/linked/Mono.Android.dll]
Xamarin.Android.Common.targets(1502,3): Type 'Android.Runtime.JavaObject, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065' will register some of its Java override methods dynamically. This may adversely affect runtime performance. See preceding warnings for names of dynamically registered methods.
Xamarin.Android.Common.targets(1502,3): [Arm64] Number of methods in the project that will be registered dynamically: 1
Xamarin.Android.Common.targets(1502,3): [X86_64] Number of methods in the project that will be registered dynamically: 1

Fix this by treating our blittable types as equivalent to their native types in the marshal method classifier.

Additionally, Mono.Android has "built-in delegates" that marshal bool types (eg: _JniMarshal_PP_Z). Because generator no longer creates these delegates (it will now create _JniMarshal_PP_B instead), we need to add them manually.

@jpobst jpobst force-pushed the dev/jpobst/blittable branch 4 times, most recently from a23c154 to 38e8ca4 Compare February 4, 2025 18:12
@jpobst jpobst changed the title Test JI 1296. [XABT] Avoid non-blittable types in native callback methods Feb 4, 2025
@jpobst jpobst force-pushed the dev/jpobst/blittable branch from 38e8ca4 to bffa04c Compare February 6, 2025 23:29
@jpobst jpobst force-pushed the dev/jpobst/blittable branch from bffa04c to 58858cd Compare February 7, 2025 18:03
@jpobst jpobst marked this pull request as ready for review February 10, 2025 18:54

// Because these types are marshaled as different blittable types,
// we need to accept them as equivalent
static readonly Tuple<string, string>[] equivalent_types = new [] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer the use of "named ValueTuple<…>":

static readonly (string Source, string Replacement)[] equivalent_types = new[]{
    (Source: "System.Boolean", Replacement: "System.SByte"),
    (Source: "System.Char", Replacement: "System.UInt16"),
};

Then TypeMatches() can use .Source instead of .Item1, etc.

(Source: "System.Char", Replacement: "System.UInt16"),
];

static bool TypeMatches (string type, string methodType)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The need for this entire method confuses me.

From the PR message:

Changing callback methods to blittable types (bool -> sbyte and char -> ushort) causes warnings when the marshal method classifier verifies that the marshal types match the native types:

Xamarin.Android.Common.targets(1502,3): Method 'System.SByte Java.Lang.Object::n_Equals_Ljava_lang_Object_(System.IntPtr,System.IntPtr,System.IntPtr)' doesn't match native callback signature (invalid return type: expected 'System.Boolean', found 'System.SByte')

but why? The invalid return type: expected message is from here:

if (String.Compare (returnType, method.ReturnType.FullName, StringComparison.Ordinal) != 0) {
log.LogWarning ($"Method '{method.FullName}' doesn't match native callback signature (invalid return type: expected '{returnType}', found '{method.ReturnType.FullName}')");
return false;
}

returnType is MapType(target.ReturnType), which uses the verbatimTypes set, and thus should be System.SByte (which is in verbatimTypes), but apparently isn't?

Why doesn't the existing code work? Why is this method needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm understanding what this code does, it is trying to match bound methods to their native callback methods.

Thus, given this bound method:

[Register ("markSupported", "()Z", "GetMarkSupportedHandler")]
public virtual unsafe bool MarkSupported () { ... }

It is trying to find this callback with a matching signature:

static bool n_MarkSupported (IntPtr jnienv, IntPtr native__this) { ... }

However, because the native callback is now sbyte instead of bool the return types no longer match and thus the "new" callback is rejected because bool != sbyte:

static sbyte n_MarkSupported (IntPtr jnienv, IntPtr native__this) { ... }

The method I added simply says that for this purpose bool and sbyte are the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants