[runtime] Throw a MarshalDirectiveException when returning an array from a pinvoke...
authormonojenkins <jo.shields+jenkins@xamarin.com>
Thu, 24 Sep 2020 03:19:22 +0000 (23:19 -0400)
committerGitHub <noreply@github.com>
Thu, 24 Sep 2020 03:19:22 +0000 (23:19 -0400)
Fixes https://github.com/mono/mono/issues/20367.

Co-authored-by: vargaz <vargaz@users.noreply.github.com>
src/mono/mono/metadata/marshal-ilgen.c
src/mono/mono/tests/libtest.c
src/mono/mono/tests/pinvoke2.cs

index 1312f42..ccd558a 100644 (file)
@@ -2737,10 +2737,12 @@ emit_marshal_array_ilgen (EmitMarshalContext *m, int argnum, MonoType *t,
                        mono_mb_emit_ldloc (mb, conv_arg);
                break;
 
-       case MARSHAL_ACTION_CONV_RESULT:
-               /* fixme: we need conversions here */
-               mono_mb_emit_stloc (mb, 3);
+       case MARSHAL_ACTION_CONV_RESULT: {
+               mono_mb_emit_byte (mb, CEE_POP);
+               char *msg = g_strdup_printf ("Cannot marshal 'return value': Invalid managed/unmanaged type combination.");
+               mono_mb_emit_exception_marshal_directive (mb, msg);
                break;
+       }
 
        case MARSHAL_ACTION_MANAGED_CONV_IN: {
                guint32 label1, label2, label3;
index 0ee5fdb..1e624ea 100644 (file)
@@ -8251,6 +8251,14 @@ mono_test_ccw_class_type_auto_dispatch (IDispatch *disp)
        return 0;
 }
 
+static guint8 static_arr[] = { 1, 2, 3, 4 };
+
+LIBTEST_API guint8*
+mono_test_marshal_return_array (void)
+{
+       return static_arr;
+}
+
 #ifdef __cplusplus
 } // extern C
 #endif
index e9280db..6c37516 100644 (file)
@@ -2147,5 +2147,16 @@ public unsafe class Tests {
                return 0;
        }
 
+       [DllImport ("libtest", EntryPoint="mono_test_marshal_return_array")]
+       public static extern int[] mono_test_marshal_return_array ();
+
+       public static int test_0_return_array () {
+               try {
+                       var arr = mono_test_marshal_return_array ();
+                       return 1;
+               } catch (MarshalDirectiveException) {
+                       return 0;
+               }
+       }
 }