Skip to content

Commit 6a9232a

Browse files
vargazUnityAlex
authored andcommitted
[runtime] Throw a MarshalDirectiveException when returning an array from a pinvoke method. (mono#20425)
Fixes mono#20367. Some light massaging done by @UnityAlex to cherry pick
1 parent 2823f4f commit 6a9232a

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

mono/metadata/marshal.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6822,10 +6822,12 @@ emit_marshal_array (EmitMarshalContext *m, int argnum, MonoType *t,
68226822
mono_mb_emit_ldloc (mb, conv_arg);
68236823
break;
68246824

6825-
case MARSHAL_ACTION_CONV_RESULT:
6826-
/* fixme: we need conversions here */
6827-
mono_mb_emit_stloc (mb, 3);
6825+
case MARSHAL_ACTION_CONV_RESULT: {
6826+
mono_mb_emit_byte (mb, CEE_POP);
6827+
char *msg = g_strdup_printf ("Cannot marshal 'return value': Invalid managed/unmanaged type combination.");
6828+
mono_mb_emit_exception_marshal_directive (mb, msg);
68286829
break;
6830+
}
68296831

68306832
case MARSHAL_ACTION_MANAGED_CONV_IN: {
68316833
MonoClass *eklass;

mono/tests/libtest.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7556,3 +7556,12 @@ mono_test_native_to_managed_exception_rethrow (NativeToManagedExceptionRethrowFu
75567556
pthread_join (t, NULL);
75577557
}
75587558
#endif
7559+
7560+
static guint8 static_arr[] = { 1, 2, 3, 4 };
7561+
7562+
LIBTEST_API guint8*
7563+
mono_test_marshal_return_array (void)
7564+
{
7565+
return static_arr;
7566+
}
7567+

mono/tests/pinvoke2.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,5 +1987,17 @@ public static unsafe int test_0_fixed_buffer_unicode () {
19871987
return 2;
19881988
return 0;
19891989
}
1990+
1991+
[DllImport ("libtest", EntryPoint="mono_test_marshal_return_array")]
1992+
public static extern int[] mono_test_marshal_return_array ();
1993+
1994+
public static int test_0_return_array () {
1995+
try {
1996+
var arr = mono_test_marshal_return_array ();
1997+
return 1;
1998+
} catch (MarshalDirectiveException) {
1999+
return 0;
2000+
}
2001+
}
19902002
}
19912003

0 commit comments

Comments
 (0)