Skip to content

Commit

Permalink
Handle crash when [out] parameter of type int is passed to delegate (#…
Browse files Browse the repository at this point in the history
…81276)

When a delegate is passed an [out] parameter of type "int" it will crash
since this data type is not handled in emit_managed_wrapper_ilgen.
This will lead to a crash. With this fix [out] parameter of type "int"
is handled after adding an addition case statement to handle MONO_TYPE_I
in emit_managed_wrapper_ilgen. Supporting UT is added.

Co-authored-by: Giridhar Trivedi <giridhar.trivedi@ibm.com>
  • Loading branch information
giritrivedi and Giridhar Trivedi committed Feb 8, 2023
1 parent 081d93a commit bd54093
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/mono/mono/metadata/marshal-lightweight.c
Original file line number Diff line number Diff line change
Expand Up @@ -2630,6 +2630,7 @@ emit_managed_wrapper_ilgen (MonoMethodBuilder *mb, MonoMethodSignature *invoke_s
case MONO_TYPE_CLASS:
case MONO_TYPE_VALUETYPE:
case MONO_TYPE_PTR:
case MONO_TYPE_I:
mono_emit_marshal (m, i, invoke_sig->params [i], mspecs [i + 1], tmp_locals [i], NULL, MARSHAL_ACTION_MANAGED_CONV_OUT);
break;
default:
Expand Down
25 changes: 25 additions & 0 deletions src/tests/Interop/MarshalAPI/FunctionPointer/FunctionPointer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ static class FunctionPointerNative

[DllImport(nameof(FunctionPointerNative))]
static unsafe extern void FillOutPtr(IntPtr* p);

[DllImport(nameof(FunctionPointerNative))]
static unsafe extern void FillOutIntParameter(out IntPtr p);
}

delegate void VoidDelegate();
Expand Down Expand Up @@ -81,13 +84,35 @@ public static void RunGetDelForOutPtrTest()
Assert.Equal(expectedValue, outVar);
}

[DllImport(nameof(FunctionPointerNative))]
static unsafe extern void FillOutIntParameter(out IntPtr p);

private unsafe delegate void DelegateToFillOutIntParameter(out IntPtr p);

public static void RunGetDelForOutIntTest()
{
Console.WriteLine($"Running {nameof(RunGetDelForOutIntTest)}...");
IntPtr outVar = 0;
int expectedValue = 50;
unsafe
{
DelegateToFillOutIntParameter d = new DelegateToFillOutIntParameter(FillOutIntParameter);
IntPtr ptr = Marshal.GetFunctionPointerForDelegate(d);
DelegateToFillOutIntParameter OutPtrDelegate = Marshal.GetDelegateForFunctionPointer<DelegateToFillOutIntParameter>(ptr);
OutPtrDelegate(out outVar);
GC.KeepAlive(d);
}
Assert.Equal(expectedValue, outVar);
}

public static int Main()
{
try
{
RunGetDelForFcnPtrTest();
RunGetFcnPtrSingleMulticastTest();
RunGetDelForOutPtrTest();
RunGetDelForOutIntTest();
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ extern "C" DLL_EXPORT void FillOutPtr(intptr_t *p)
{
*p = 60;
}

extern "C" DLL_EXPORT void FillOutIntParameter(intptr_t *p)
{
*p = 50;
}

0 comments on commit bd54093

Please sign in to comment.