From d026b14e0cd63e004f18071f8366ab90e75e2eb2 Mon Sep 17 00:00:00 2001 From: Matous Kozak Date: Wed, 14 Feb 2024 15:23:25 +0000 Subject: [PATCH 1/2] block SIMD types in Swift interop --- src/mono/mono/metadata/marshal.c | 2 +- .../SwiftInvalidCallConv/SwiftInvalidCallConv.cs | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/mono/mono/metadata/marshal.c b/src/mono/mono/metadata/marshal.c index 1e10d82ee40885..a5af0541fa6c26 100644 --- a/src/mono/mono/metadata/marshal.c +++ b/src/mono/mono/metadata/marshal.c @@ -3711,7 +3711,7 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions, swift_error_args++; } else if (param_klass == swift_self) { swift_self_args++; - } else if (!m_class_is_blittable (param_klass) && m_class_get_this_arg (param_klass)->type != MONO_TYPE_FNPTR) { + } else if (!m_class_is_blittable (param_klass) || m_class_is_simd_type (param_klass)) { swift_error_args = swift_self_args = 0; mono_error_set_generic_error (emitted_error, "System", "InvalidProgramException", "Passing non-primitive value types to a P/Invoke with the Swift calling convention is unsupported."); break; diff --git a/src/tests/Interop/Swift/SwiftInvalidCallConv/SwiftInvalidCallConv.cs b/src/tests/Interop/Swift/SwiftInvalidCallConv/SwiftInvalidCallConv.cs index 41c98e3791f91c..9e4bc140829fd9 100644 --- a/src/tests/Interop/Swift/SwiftInvalidCallConv/SwiftInvalidCallConv.cs +++ b/src/tests/Interop/Swift/SwiftInvalidCallConv/SwiftInvalidCallConv.cs @@ -5,6 +5,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.InteropServices.Swift; +using System.Numerics; using Xunit; public class InvalidCallingConvTests @@ -36,6 +37,10 @@ public class StringClass [DllImport(SwiftLib, EntryPoint = "$s20SwiftInvalidCallConv10simpleFuncyyF")] public static extern void FuncWithNonPrimitiveArg(StringClass arg1); + [UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })] + [DllImport(SwiftLib, EntryPoint = "$s20SwiftInvalidCallConv10simpleFuncyyF")] + public static extern void FuncWithSIMDArg(Vector4 vec); + [Fact] public static void TestFuncWithTwoSelfParameters() { @@ -77,4 +82,12 @@ public static void TestFuncWithNonPrimitiveArg() arg1.value = "fail"; Assert.Throws(() => FuncWithNonPrimitiveArg(arg1)); } + + [Fact] + public static void TestFuncWithSIMDArg() + { + // Invalid due to a SIMD argument. + Vector4 vec = new Vector4(); // Using Vector4 as it is a SIMD type across all architectures for Mono + Assert.Throws(() => FuncWithSIMDArg(vec)); + } } From adcc95a45b661b1fec1e9cf893756f6ea3f12185 Mon Sep 17 00:00:00 2001 From: Matous Kozak <55735845+matouskozak@users.noreply.github.com> Date: Thu, 15 Feb 2024 09:08:04 +0100 Subject: [PATCH 2/2] Update src/mono/mono/metadata/marshal.c Co-authored-by: Milos Kotlar --- src/mono/mono/metadata/marshal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/mono/metadata/marshal.c b/src/mono/mono/metadata/marshal.c index a5af0541fa6c26..c57fa32127a9f7 100644 --- a/src/mono/mono/metadata/marshal.c +++ b/src/mono/mono/metadata/marshal.c @@ -3713,7 +3713,7 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions, swift_self_args++; } else if (!m_class_is_blittable (param_klass) || m_class_is_simd_type (param_klass)) { swift_error_args = swift_self_args = 0; - mono_error_set_generic_error (emitted_error, "System", "InvalidProgramException", "Passing non-primitive value types to a P/Invoke with the Swift calling convention is unsupported."); + mono_error_set_generic_error (emitted_error, "System", "InvalidProgramException", "Passing non-blittable types to a P/Invoke with the Swift calling convention is unsupported."); break; } }