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

Merged
merged 3 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion external/Java.Interop
Submodule Java.Interop updated 20 files
+0 −1 tests/Java.Interop.Dynamic-Tests/Java.Interop.Dynamic-Tests.csproj
+142 −0 tests/generator-Tests/Unit-Tests/BlittableTypeTests.cs
+6 −6 tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/Common/WriteDuplicateInterfaceEventArgs.txt
+6 −6 ...generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteDuplicateInterfaceEventArgs.txt
+9 −0 tests/generator-Tests/Unit-Tests/CodeGeneratorTestBase.cs
+3 −3 tests/generator-Tests/expected.xaji/GenericArguments/Com.Google.Android.Exoplayer.Drm.IExoMediaCrypto.cs
+2 −2 tests/generator-Tests/expected.xaji/GenericArguments/__NamespaceMapping__.cs
+3 −3 tests/generator-Tests/expected.xaji/Streams/Java.IO.InputStream.cs
+2 −2 tests/generator-Tests/expected.xaji/Streams/__NamespaceMapping__.cs
+3 −3 tests/generator-Tests/expected.xaji/TestInterface/Java.Util.ICollection.cs
+3 −3 tests/generator-Tests/expected.xaji/TestInterface/Java.Util.IDeque.cs
+3 −3 tests/generator-Tests/expected.xaji/TestInterface/Java.Util.IQueue.cs
+2 −2 tests/generator-Tests/expected.xaji/TestInterface/__NamespaceMapping__.cs
+2 −0 tools/generator/CodeGenerationOptions.cs
+2 −2 tools/generator/Java.Interop.Tools.Generator.ObjectModel/ReturnValue.cs
+2 −0 tools/generator/Java.Interop.Tools.Generator.ObjectModel/Symbols/FormatSymbol.cs
+13 −0 tools/generator/Java.Interop.Tools.Generator.ObjectModel/Symbols/ISymbol.cs
+2 −2 tools/generator/Java.Interop.Tools.Generator.ObjectModel/Symbols/SymbolTable.cs
+7 −4 tools/generator/SourceWriters/BoundFieldAsProperty.cs
+1 −1 tools/generator/SourceWriters/Extensions/SourceWriterExtensions.cs
30 changes: 30 additions & 0 deletions src/Mono.Android/Android.Runtime/ExtraDelegates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// There are built-in delegates for these, but we no longer generate them in Mono.Android.dll
// because bool is not a blittable type. But we still need them for pre-existing bindings.
using System;

[global::System.Runtime.InteropServices.UnmanagedFunctionPointer (global::System.Runtime.InteropServices.CallingConvention.Winapi)]
delegate bool _JniMarshal_PP_Z (IntPtr jnienv, IntPtr klass);
[global::System.Runtime.InteropServices.UnmanagedFunctionPointer (global::System.Runtime.InteropServices.CallingConvention.Winapi)]
delegate bool _JniMarshal_PPL_Z (IntPtr jnienv, IntPtr klass, IntPtr p0);
[global::System.Runtime.InteropServices.UnmanagedFunctionPointer (global::System.Runtime.InteropServices.CallingConvention.Winapi)]
delegate bool _JniMarshal_PPJ_Z (IntPtr jnienv, IntPtr klass, long p0);
[global::System.Runtime.InteropServices.UnmanagedFunctionPointer (global::System.Runtime.InteropServices.CallingConvention.Winapi)]
delegate void _JniMarshal_PPLZ_V (IntPtr jnienv, IntPtr klass, IntPtr p0, bool p1);
[global::System.Runtime.InteropServices.UnmanagedFunctionPointer (global::System.Runtime.InteropServices.CallingConvention.Winapi)]
delegate bool _JniMarshal_PPLL_Z (IntPtr jnienv, IntPtr klass, IntPtr p0, IntPtr p1);
[global::System.Runtime.InteropServices.UnmanagedFunctionPointer (global::System.Runtime.InteropServices.CallingConvention.Winapi)]
delegate bool _JniMarshal_PPIL_Z (IntPtr jnienv, IntPtr klass, int p0, IntPtr p1);
[global::System.Runtime.InteropServices.UnmanagedFunctionPointer (global::System.Runtime.InteropServices.CallingConvention.Winapi)]
delegate bool _JniMarshal_PPLII_Z (IntPtr jnienv, IntPtr klass, IntPtr p0, int p1, int p2);
[global::System.Runtime.InteropServices.UnmanagedFunctionPointer (global::System.Runtime.InteropServices.CallingConvention.Winapi)]
delegate bool _JniMarshal_PPLLJ_Z (IntPtr jnienv, IntPtr klass, IntPtr p0, IntPtr p1, long p2);
[global::System.Runtime.InteropServices.UnmanagedFunctionPointer (global::System.Runtime.InteropServices.CallingConvention.Winapi)]
delegate bool _JniMarshal_PPLIL_Z (IntPtr jnienv, IntPtr klass, IntPtr p0, int p1, IntPtr p2);
[global::System.Runtime.InteropServices.UnmanagedFunctionPointer (global::System.Runtime.InteropServices.CallingConvention.Winapi)]
delegate bool _JniMarshal_PPLLL_Z (IntPtr jnienv, IntPtr klass, IntPtr p0, IntPtr p1, IntPtr p2);
[global::System.Runtime.InteropServices.UnmanagedFunctionPointer (global::System.Runtime.InteropServices.CallingConvention.Winapi)]
delegate IntPtr _JniMarshal_PPIZI_L (IntPtr jnienv, IntPtr klass, int p0, bool p1, int p2);
[global::System.Runtime.InteropServices.UnmanagedFunctionPointer (global::System.Runtime.InteropServices.CallingConvention.Winapi)]
delegate bool _JniMarshal_PPLZZL_Z (IntPtr jnienv, IntPtr klass, IntPtr p0, bool p1, bool p2, IntPtr p3);
[global::System.Runtime.InteropServices.UnmanagedFunctionPointer (global::System.Runtime.InteropServices.CallingConvention.Winapi)]
delegate void _JniMarshal_PPZIIII_V (IntPtr jnienv, IntPtr klass, bool p0, int p1, int p2, int p3, int p4);
1 change: 1 addition & 0 deletions src/Mono.Android/Mono.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<Compile Include="Android.Icu\DateIntervalFormat.cs" />
<Compile Include="Android.Net.Wifi.P2p\WifiP2pManager.cs" />
<Compile Include="Android.Runtime\DynamicMethodNameCounter.cs" />
<Compile Include="Android.Runtime\ExtraDelegates.cs" />
<Compile Include="Android.Runtime\IJavaObjectValueMarshaler.cs" />
<Compile Include="Android.Telecom\InCallService.cs" />
<Compile Include="Android.Telephony.Mbms\StreamingService.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public bool Matches (MethodDefinition method)
return false;
}

if (String.Compare (returnType, method.ReturnType.FullName, StringComparison.Ordinal) != 0) {
if (!TypeMatches (returnType, method.ReturnType.FullName)) {
log.LogWarning ($"Method '{method.FullName}' doesn't match native callback signature (invalid return type: expected '{returnType}', found '{method.ReturnType.FullName}')");
return false;
}
Expand All @@ -223,14 +223,37 @@ public bool Matches (MethodDefinition method)
parameterTypeName = pd.ParameterType.FullName;
}

if (String.Compare (parameterTypeName, paramTypes[i], StringComparison.Ordinal) != 0) {
if (!TypeMatches (parameterTypeName, paramTypes[i])) {
log.LogWarning ($"Method '{method.FullName}' doesn't match native callback signature, expected parameter type '{paramTypes[i]}' at position {i}, found '{parameterTypeName}'");
return false;
}
}

return true;
}

// Because these types are marshaled as different blittable types,
// we need to accept them as equivalent
static readonly (string Source, string Replacement)[] equivalent_types = [
(Source: "System.Boolean", Replacement: "System.SByte"),
(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.

{
if (String.Compare (type, methodType, StringComparison.Ordinal) == 0)
return true;

foreach (var eq in equivalent_types) {
if (string.Compare (eq.Source, type, StringComparison.Ordinal) == 0 && string.Compare (eq.Replacement, methodType, StringComparison.Ordinal) == 0)
return true;

if (string.Compare (eq.Source, methodType, StringComparison.Ordinal) == 0 && string.Compare (eq.Replacement, type, StringComparison.Ordinal) == 0)
return true;
}

return false;
}
}

TypeDefinitionCache tdCache;
Expand Down
Loading