Skip to content

Commit

Permalink
[mono] Block SIMD types in Swift interop (#98429)
Browse files Browse the repository at this point in the history
Temporarily block SIMD types in Swift Interop to simplify current runtime implementation and not cause unintended behavior.

---------

Co-authored-by: Milos Kotlar <kotlarmilos@gmail.com>
  • Loading branch information
matouskozak and kotlarmilos authored Feb 16, 2024
1 parent 79837bc commit a3f7416
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/mono/mono/metadata/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -3711,9 +3711,9 @@ 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.");
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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -77,4 +82,12 @@ public static void TestFuncWithNonPrimitiveArg()
arg1.value = "fail";
Assert.Throws<InvalidProgramException>(() => 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<InvalidProgramException>(() => FuncWithSIMDArg(vec));
}
}

0 comments on commit a3f7416

Please sign in to comment.