From 7f7ca7d9ad76977390f2c567ad78f2536a7b9612 Mon Sep 17 00:00:00 2001 From: Zoltan Varga Date: Tue, 1 Nov 2022 03:56:54 -0400 Subject: [PATCH 1/4] [mono][wasm] Handle delegates decorated with [UnmanagedFunctionPointer] in the interp-to-native generator. Fixes https://github.com/dotnet/runtime/issues/76930. --- .../WasmAppBuilder/PInvokeTableGenerator.cs | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs b/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs index 254969f201233b..58ff10ca7e8d69 100644 --- a/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs +++ b/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs @@ -79,6 +79,22 @@ private void CollectPInvokes(List pinvokes, List callb } } + if (HasAttribute(type, new string[] {"System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute"})) { + var method = type.GetMethod("Invoke"); + + if (method != null) + { + string? signature = SignatureMapper.MethodToSignature(method!); + if (signature == null) + { + throw new NotSupportedException($"Unsupported parameter type in method '{type.FullName}.{method.Name}'"); + } + + Log.LogMessage(MessageImportance.Low, $"Adding pinvoke signature {signature} for method '{type.FullName}.{method.Name}'"); + signatures.Add(signature); + } + } + void CollectPInvokesForMethod(MethodInfo method) { if ((method.Attributes & MethodAttributes.PinvokeImpl) != 0) @@ -151,6 +167,24 @@ static bool MethodHasCallbackAttributes(MethodInfo method) } } + private static bool HasAttribute(Type type, string[] attributeNames) + { + foreach (CustomAttributeData cattr in CustomAttributeData.GetCustomAttributes(type)) + { + try + { + for (int i = 0; i < attributeNames.Length; ++i) + if (cattr.AttributeType.FullName == attributeNames [i]) + return true; + } + catch + { + // Assembly not found, ignore + } + } + return false; + } + private void EmitPInvokeTable(StreamWriter w, Dictionary modules, List pinvokes) { w.WriteLine("// GENERATED FILE, DO NOT MODIFY"); From a45eba4d0ad0004d731aa2f4c27335e091153f99 Mon Sep 17 00:00:00 2001 From: Zoltan Varga Date: Tue, 1 Nov 2022 05:05:51 -0400 Subject: [PATCH 2/4] Update src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs Co-authored-by: Ankit Jain --- src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs b/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs index 58ff10ca7e8d69..85ea5b29d61edd 100644 --- a/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs +++ b/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs @@ -79,7 +79,8 @@ private void CollectPInvokes(List pinvokes, List callb } } - if (HasAttribute(type, new string[] {"System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute"})) { + if (HasAttribute(type, new string[] {"System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute"})) + { var method = type.GetMethod("Invoke"); if (method != null) From 29222766567ae05ba1582bd8cb886cf8e02b4bf6 Mon Sep 17 00:00:00 2001 From: Zoltan Varga Date: Tue, 1 Nov 2022 05:06:05 -0400 Subject: [PATCH 3/4] Update src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs Co-authored-by: Ankit Jain --- src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs b/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs index 85ea5b29d61edd..effa6a8550a959 100644 --- a/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs +++ b/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs @@ -87,9 +87,8 @@ private void CollectPInvokes(List pinvokes, List callb { string? signature = SignatureMapper.MethodToSignature(method!); if (signature == null) - { - throw new NotSupportedException($"Unsupported parameter type in method '{type.FullName}.{method.Name}'"); - } + throw new NotSupportedException($"Unsupported parameter type in method '{type.FullName}.{method.Name}'"); + Log.LogMessage(MessageImportance.Low, $"Adding pinvoke signature {signature} for method '{type.FullName}.{method.Name}'"); signatures.Add(signature); From cacfe6335ba4cdded6a227def29b21a0880c431a Mon Sep 17 00:00:00 2001 From: Zoltan Varga Date: Tue, 1 Nov 2022 05:06:40 -0400 Subject: [PATCH 4/4] Update src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs Co-authored-by: Ankit Jain --- src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs b/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs index effa6a8550a959..74349b38166dde 100644 --- a/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs +++ b/src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs @@ -79,7 +79,7 @@ private void CollectPInvokes(List pinvokes, List callb } } - if (HasAttribute(type, new string[] {"System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute"})) + if (HasAttribute(type, "System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute")) { var method = type.GetMethod("Invoke"); @@ -167,15 +167,20 @@ static bool MethodHasCallbackAttributes(MethodInfo method) } } - private static bool HasAttribute(Type type, string[] attributeNames) + private static bool HasAttribute(MemberInfo element, params string[] attributeNames) { - foreach (CustomAttributeData cattr in CustomAttributeData.GetCustomAttributes(type)) + foreach (CustomAttributeData cattr in CustomAttributeData.GetCustomAttributes(element)) { try { for (int i = 0; i < attributeNames.Length; ++i) - if (cattr.AttributeType.FullName == attributeNames [i]) + { + if (cattr.AttributeType.FullName == attributeNames [i] || + cattr.AttributeType.Name == attributeNames[i]) + { return true; + } + } } catch {